Example #1
0
        public void GetEditedCellsMethodTest()
        {
            //Mock spreadsheet
            var spreadsheetMock = new SpreadsheetView();
            var privateAccessor = new PrivateAccessor(spreadsheetMock);

            //Load data            
            Mock.NonPublic.Arrange(typeof(CellBase), "SetEditorParams").IgnoreInstance().DoNothing();
            spreadsheetMock.ItemsSource = new List<TestData> { new TestData { Text = "text 1" }, new TestData { Text = "another text" } };
            spreadsheetMock.OnApplyTemplate();
            var item1 = spreadsheetMock.ItemsCollection[0];
            var item2 = spreadsheetMock.ItemsCollection[1];

            //Call ItemContainerGenerator overrides
            var container = privateAccessor.CallMethod("GetContainerForItemOverride") as SpreadsheetItemContainer;
            Assert.IsNotNull(container);
            spreadsheetMock.ItemContainerStyle = new Style(typeof(SpreadsheetItemContainer));
            privateAccessor.CallMethod("PrepareContainerForItemOverride", container, item1);
            privateAccessor.CallMethod("PrepareContainerForItemOverride", (SpreadsheetItemContainer)null, item2);

            //Assert _containersDict field
            var containersDict = privateAccessor.GetField("_containersDict") as Dictionary<IWrap, SpreadsheetItemContainer>;
            Assert.IsNotNull(containersDict);
            Assert.AreEqual(containersDict.Count, 2);
            Assert.IsTrue(containersDict.ContainsKey(item1));
            Assert.AreEqual(containersDict[item1], container);
            Assert.IsTrue(containersDict.ContainsKey(item2));
            Assert.IsNull(containersDict[item2]);

            //Assert cells
            Assert.IsNotNull(container.Cells);
            Assert.AreEqual(container.Cells.Count, 3);

            //Assert GetEditedCells call with only container from one of two elements and with no child spreadsheets
            var cell = container.Cells[1];
            Mock.Arrange(() => cell.IsEditMode).Returns(true);
            var editedCells = spreadsheetMock.GetEditedCells();
            Assert.AreEqual(editedCells.Count(), 1);
            Assert.AreEqual(editedCells[0], container.Cells[1]);

            //Add child spreadsheet
            var childSpreadsheet = new SpreadsheetView();
            var accessor = new PrivateAccessor(childSpreadsheet);
            childSpreadsheet.ItemsSource = new List<TestData> { new TestData { Text = "child item 1" }, new TestData { Text = "child item 2" } };
            childSpreadsheet.OnApplyTemplate();
            var item3 = childSpreadsheet.ItemsCollection[0];
            var item4 = childSpreadsheet.ItemsCollection[1];
            var childcontainer1 = accessor.CallMethod("GetContainerForItemOverride") as SpreadsheetItemContainer;
            var childcontainer2 = accessor.CallMethod("GetContainerForItemOverride") as SpreadsheetItemContainer;
            childSpreadsheet.ItemContainerStyle = new Style(typeof(SpreadsheetItemContainer));
            accessor.CallMethod("PrepareContainerForItemOverride", childcontainer1, item3);
            accessor.CallMethod("PrepareContainerForItemOverride", childcontainer2, item4);
            var cell2 = childcontainer1.Cells[1];
            Mock.Arrange(() => cell2.IsEditMode).Returns(true);
            var cell3 = childcontainer2.Cells[2];
            Mock.Arrange(() => cell3.IsEditMode).Returns(true);

            ((List<SpreadsheetView>)privateAccessor.GetField("_childSpreadsheets")).Add(childSpreadsheet);

            //Assert GetEditedCells call involving invisible child spreadsheet
            Mock.Arrange(() => childSpreadsheet.IsVisible).Returns(false);
            editedCells = spreadsheetMock.GetEditedCells();
            Assert.AreEqual(editedCells.Count(), 1);
            Assert.AreEqual(editedCells[0], container.Cells[1]);

            //Assert GetEditedCells call involving visible child spreadsheet
            Mock.Arrange(() => childSpreadsheet.IsVisible).Returns(true);
            editedCells = spreadsheetMock.GetEditedCells();
            Assert.AreEqual(editedCells.Count(), 3);
            Assert.AreEqual(editedCells[0], childcontainer1.Cells[1]);
            Assert.AreEqual(editedCells[1], childcontainer2.Cells[2]);
            Assert.AreEqual(editedCells[2], container.Cells[1]);
        }
Example #2
0
        public void KeyNavigationByDemandMethodTest()
        {
            var spreadsheet = new SpreadsheetView();

            //Move right
            Mock.Arrange(() => spreadsheet.MoveRight()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Right);
            Mock.Assert(() => spreadsheet.MoveRight());

            //Move left
            Mock.Arrange(() => spreadsheet.MoveLeft()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Left);
            Mock.Assert(() => spreadsheet.MoveLeft());

            //Move up
            Mock.Arrange(() => spreadsheet.MoveUp()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Up);
            Mock.Assert(() => spreadsheet.MoveUp());

            //Move Down
            Mock.Arrange(() => spreadsheet.MoveDown()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Down);
            Mock.Assert(() => spreadsheet.MoveDown());

            //Start Editing
            Mock.NonPublic.Arrange(spreadsheet, "StartEditing").DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Enter);
            Mock.NonPublic.Assert(spreadsheet, "StartEditing");

            //Insert multiple records (there are cells in edit mode)
            Mock.Arrange(() => Keyboard.Modifiers).Returns(ModifierKeys.Shift);
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[1]);
            Mock.Arrange(() => spreadsheet.InsertMultipleRecords()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Insert, true);
            Mock.Assert(() => spreadsheet.InsertMultipleRecords(), Occurs.Never());

            //Insert multiple records (there are no cells in edit mode)
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[0]);
            spreadsheet.KeyNavigationByDemand(Key.Insert, true);
            Mock.Assert(() => spreadsheet.InsertMultipleRecords(), Occurs.Once());

            //Insert new (there are cells in edit mode)
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[1]);
            Mock.Arrange(() => Keyboard.Modifiers).Returns(ModifierKeys.None);
            Mock.Arrange(() => spreadsheet.InsertRecord()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Insert);
            Mock.Assert(() => spreadsheet.InsertRecord(), Occurs.Never());

            //Insert new (there are no cells in edit mode)
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[0]);
            Mock.Arrange(() => Keyboard.Modifiers).Returns(ModifierKeys.None);
            Mock.Arrange(() => spreadsheet.InsertRecord()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Insert);
            Mock.Assert(() => spreadsheet.InsertRecord(), Occurs.Once());

            //Delete record (there are cells in edit mode)
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[1]);
            Mock.Arrange(() => Keyboard.Modifiers).Returns(ModifierKeys.Shift);
            Mock.Arrange(() => spreadsheet.DeleteRecord()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Delete, true);
            Mock.Assert(() => spreadsheet.DeleteRecord(), Occurs.Never());

            //Delete record (there are no cells in edit mode)
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[0]);
            Mock.Arrange(() => Keyboard.Modifiers).Returns(ModifierKeys.Shift);
            Mock.Arrange(() => spreadsheet.DeleteRecord()).DoNothing();
            spreadsheet.KeyNavigationByDemand(Key.Delete, true);
            Mock.Assert(() => spreadsheet.DeleteRecord(), Occurs.Once());

            //Delete values from selected cells (there are cells in edit mode)
            var cell1 = Mock.Create<Cell>(Constructor.Mocked);
            var cell2 = Mock.Create<StackCell>(Constructor.Mocked);
            Mock.Arrange(() => spreadsheet.GetSelectedCells()).Returns(new CellBase[] {cell1, cell2});
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[1]);
            Mock.Arrange(() => Keyboard.Modifiers).Returns(ModifierKeys.None);
            spreadsheet.KeyNavigationByDemand(Key.Delete);
            Mock.Assert(() => cell1.ClearValue(), Occurs.Never());
            Mock.Assert(() => cell2.ClearValue(), Occurs.Never());

            //Delete values from selected cells (there are no cells in edit mode)
            Mock.Arrange(() => spreadsheet.GetEditedCells()).Returns(new CellBase[0]);
            spreadsheet.KeyNavigationByDemand(Key.Delete);
            Mock.Assert(() => cell1.ClearValue(), Occurs.Once());
            Mock.Assert(() => cell2.ClearValue(), Occurs.Once());
        }