public void PrepareSpreadsheet()
        {
            //Create item
            var dataItem = new TestData { Text = "str", FractionalNumber = 1.0 };

            //Create spreadsheet, populate Items collection and build columns
            _spreadsheet = Mock.Create<SpreadsheetView>(Behavior.CallOriginal);
            _spreadsheet.ItemsSource = new List<TestData> { dataItem };
            _spreadsheet.OnApplyTemplate();

            //Need one of the columns built
            var column = _spreadsheet.Columns.FirstOrDefault(x => x.PropertyName == "Text");
            if (column == null)
                Assert.Fail();

            //Create container but suppress cells creation
            var container = new SpreadsheetItemContainer(_spreadsheet);
            Mock.NonPublic.Arrange(container, "InitializeCellsWithValues").DoNothing();
            container.Item = ItemWrap.BuildNew(dataItem);

            //Avoid calling original SetValueBinding and SetEditorParams methods from cell's constructor
            Mock.NonPublic.Arrange(typeof(Cell), "SetValueBinding", container.Item);
            Mock.NonPublic.Arrange(typeof(Cell), "SetEditorParams");
            _cell = column.CreateCell(container) as Cell;

            //Mock behavior
            _cellInteractionBehaviorMock = Mock.Create<CellInteractionBehavior>(Behavior.CallOriginal);

            //Attach behavior to the new control and check that OnAttached method was called
            _contentControl = Mock.Create<CellControl>(Behavior.CallOriginal);
            _contentControl.Content = _cell;
            _contentControl.DataContext = _cell;
            _cellInteractionBehaviorMock.Attach(_contentControl);
            Mock.NonPublic.Assert(_cellInteractionBehaviorMock, "OnAttached", Occurs.Once());
        }
Exemplo n.º 2
0
        public void UnselectMethodTest()
        {
            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);

            //Unselect cells from local spreadsheet
            Mock.Arrange(() => spreadsheetMock.Unselect(Arg.IsAny<CellBase>(), Arg.IsAny<SpreadsheetView>()));
            Mock.Arrange(() => container.Unselect(Arg.IsAny<CellBase>())).IgnoreInstance().OccursOnce();
            var cell = Mock.Create<CellBase>(Constructor.Mocked, Behavior.Loose);
            spreadsheetMock.Unselect(cell);
            Mock.Assert(container);

            //Create child spreadsheets
            var childSpreadsheet1 = Mock.Create<SpreadsheetView>(Constructor.Mocked, Behavior.Loose);
            Mock.Arrange(() => childSpreadsheet1.IsVisible).Returns(true);
            var childSpreadsheet2 = Mock.Create<SpreadsheetView>(Constructor.Mocked, Behavior.Loose);
            Mock.Arrange(() => childSpreadsheet2.IsVisible).Returns(false);
            var childSpreadsheet3 = Mock.Create<SpreadsheetView>(Constructor.Mocked, Behavior.Loose);
            Mock.Arrange(() => childSpreadsheet3.IsVisible).Returns(true);

            //Add child spreadsheets
            var childSpreadsheets = privateAccessor.GetField("_childSpreadsheets") as List<SpreadsheetView>;
            childSpreadsheets.Add(childSpreadsheet1);
            childSpreadsheets.Add(childSpreadsheet2);
            childSpreadsheets.Add(childSpreadsheet3);

            //Call method with child spreadsheets and with caller (the second argument)
            Mock.Arrange(() => spreadsheetMock.Unselect(Arg.IsAny<CellBase>(), Arg.IsAny<SpreadsheetView>())).CallOriginal();
            spreadsheetMock.Unselect(cell, childSpreadsheet1);
            Mock.Assert(() => childSpreadsheet1.Unselect(cell, Arg.IsAny<SpreadsheetView>()), Occurs.Never());
            Mock.Assert(() => childSpreadsheet2.Unselect(cell, Arg.IsAny<SpreadsheetView>()), Occurs.Never());
            Mock.Assert(() => childSpreadsheet3.Unselect(cell, Arg.IsAny<SpreadsheetView>()), Occurs.Once());

            //Call method with child spreadsheets
            Mock.Arrange(() => container.Unselect(Arg.IsAny<CellBase>())).IgnoreInstance().Occurs(2);
            spreadsheetMock.Unselect(cell);
            Mock.Assert(() => childSpreadsheet1.Unselect(cell, Arg.IsAny<SpreadsheetView>()), Occurs.Once());
            Mock.Assert(() => childSpreadsheet2.Unselect(cell, Arg.IsAny<SpreadsheetView>()), Occurs.Never());
            Mock.Assert(() => childSpreadsheet3.Unselect(cell, Arg.IsAny<SpreadsheetView>()), Occurs.Exactly(2));
        }
Exemplo n.º 3
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]);
        }
Exemplo n.º 4
0
        public void AssignItemsSourceFirstTest()
        {
            var spreadsheet = new SpreadsheetView();
            var spreadsheetData = new List<TestData>
            {
                new TestData{Text = "string1", FractionalNumber = 10.0},
                new TestData{Text = "string2", FractionalNumber = 20.0},
                new TestData{Text = "test text", FractionalNumber = 30.2}
            };

            var privateAccessor = new PrivateAccessor(spreadsheet);

            //By default ItemsSource of SpreadsheetView is null
            Assert.IsNull(spreadsheet.ItemsSource);

            Mock.NonPublic.Arrange<SpreadsheetView>("OnItemsSourceChanged", ArgExpr.IsAny<DependencyObject>(), ArgExpr.IsAny<DependencyPropertyChangedEventArgs>())
                          .CallOriginal()
                          .MustBeCalled();
            spreadsheet.ItemsSource = spreadsheetData;
            Assert.AreEqual(spreadsheet.ItemsSource, spreadsheetData);
            Mock.NonPublic.Assert<SpreadsheetView>("OnItemsSourceChanged", ArgExpr.IsAny<DependencyObject>(), ArgExpr.IsAny<DependencyPropertyChangedEventArgs>());

            //FillItemsSource must be called from OnApplyTemplate if it hasn't been invoked yet 
            Mock.NonPublic.Assert(spreadsheet, "FillItemsSource", Occurs.Never(), ArgExpr.IsAny<IEnumerable>());
            Assert.IsFalse((bool)privateAccessor.GetField("_templateApplied"));

            //We've set SpreadsheetView.ItemsSource but ItemsControl.ItemsSource is still null
            var itemsControl = spreadsheet as ItemsControl;
            Assert.IsNull(itemsControl.ItemsSource);

            //Invoke OnApplyTemplate manually
            spreadsheet.OnApplyTemplate();
            Mock.NonPublic.Assert(spreadsheet, "FillItemsSource", Occurs.Once(), ArgExpr.IsAny<IEnumerable>());
            Assert.IsTrue((bool)privateAccessor.GetField("_templateApplied"));

            //FillItemsSource calls PopulateColumns
            Mock.NonPublic.Assert(spreadsheet, "PopulateColumns", Occurs.Once(), ArgExpr.IsAny<SpreadsheetElementCollection>());

            //Now ItemsControl.ItemsSource has value as well
            Assert.IsNotNull(itemsControl.ItemsSource);
            Assert.AreEqual(itemsControl.ItemsSource.GetType(), typeof(SpreadsheetElementCollection));

            //SpreadsheetView.ItemsCollection has only getter which retrieves data from ItemsControl.ItemsSource and casts it to SpreadsheetElementCollection
            Assert.AreEqual(itemsControl.ItemsSource, spreadsheet.ItemsCollection);

            //Assure that all our elements are there
            Assert.AreEqual(((IList)spreadsheet.ItemsSource).Count, spreadsheetData.Count);
            Assert.AreEqual(((IList)itemsControl.ItemsSource).Count, spreadsheetData.Count);
        }
Exemplo n.º 5
0
        public void OnItemsChangedItemResetTest()
        {
            var spreadsheet = new SpreadsheetView();
            var privateAccessor = new PrivateAccessor(spreadsheet);

            var container = new SpreadsheetItemContainer(spreadsheet);

            //Arrange ContainerFromItem and initialize spreadsheet ItemsSource collection
            Mock.Arrange(() => spreadsheet.ItemContainerGenerator.ContainerFromItem(Arg.IsAny<IWrap>())).Returns(container);
            spreadsheet.ItemsSource = new List<TestData> {new TestData{Text = "data1"}};
            spreadsheet.OnApplyTemplate();
            var containersDict = privateAccessor.GetField("_containersDict") as Dictionary<IWrap, SpreadsheetItemContainer>;
            Assert.AreEqual(containersDict.Count, 1);
            Assert.AreEqual(((TestData)containersDict.First().Key.Data).Text, "data1");

            //Re-initialize collection to call OnItemsChanged with Reset action and assert results
            Mock.NonPublic.Arrange(spreadsheet, "DisposeContainers").CallOriginal().MustBeCalled();
            spreadsheet.ItemsSource = new List<TestData> { new TestData { Text = "data2" } };
            Assert.AreEqual(containersDict.Count, 1);
            Assert.AreEqual(((TestData)containersDict.First().Key.Data).Text, "data2");            
            Mock.Assert(spreadsheet);
        }
Exemplo n.º 6
0
        public void OnItemsChangedItemRemoveTest()
        {
            var spreadsheet = new SpreadsheetView();
            var privateAccessor = new PrivateAccessor(spreadsheet);

            //Initialize ItemCollection
            Mock.NonPublic.Arrange(spreadsheet, "OnItemsChanged", ArgExpr.IsAny<NotifyCollectionChangedEventArgs>()).DoNothing();
            var list = new List<TestData>
            {
                new TestData {Text = "fff", FractionalNumber = 0},
                new TestData {Text = "item", FractionalNumber = 55},
                new TestData {Text = "last item", FractionalNumber = -5}
            };
            spreadsheet.ItemsSource = list;
            spreadsheet.OnApplyTemplate();
            Assert.IsNotNull(spreadsheet.ItemsCollection);
            Mock.Reset();

            var item1 = spreadsheet.ItemsCollection[0];
            var item2 = spreadsheet.ItemsCollection[1];
            var item3 = spreadsheet.ItemsCollection[2];

            //Bind item1 to container
            var container = new SpreadsheetItemContainer(spreadsheet);
            Mock.NonPublic.Arrange(container, "InitializeCellsWithValues");
            privateAccessor.CallMethod("BindContainerToItem", container, item1);
            privateAccessor.CallMethod("BindContainerToItem", container, item3);

            //Assert dictionary with containers
            var containerDict = privateAccessor.GetField("_containersDict") as Dictionary<IWrap, SpreadsheetItemContainer>;
            Assert.IsNotNull(containerDict);
            Assert.AreEqual(containerDict.Count, 2);
            containerDict.Add(item2, null);
            Assert.AreEqual(containerDict.Count, 3);

            //Remove second item from collection (when container is null)
            Mock.Arrange(() => containerDict.Remove(item2)).CallOriginal();
            spreadsheet.ItemsCollection.Remove(item2);
            Mock.Assert(() => containerDict.Remove(item2), Occurs.Once());
            Assert.AreEqual(containerDict.Count, 2);

            //Remove first item from collection (when container is disposed)
            Mock.Arrange(() => container.IsDisposed).Returns(true);
            spreadsheet.ItemsCollection.Remove(item1);            
            Mock.Assert(() => containerDict.Remove(item1), Occurs.Once());
            Assert.AreEqual(containerDict.Count, 1);

            //Remove item and dispose container            
            Mock.Arrange(() => container.IsDisposed).Returns(false);            
            spreadsheet.ItemsCollection.Remove(item3);
            Mock.Assert(() => containerDict.Remove(item3), Occurs.Exactly(2));
            Assert.AreEqual(containerDict.Count, 0);
            Mock.Reset();
            Assert.IsTrue(container.IsDisposed);
        }
Exemplo n.º 7
0
        public void OnItemsChangedItemAddTest()
        {
            var spreadsheet = new SpreadsheetView();
            var privateAccessor = new PrivateAccessor(spreadsheet);

            //Initialize ItemCollection
            Mock.NonPublic.Arrange(spreadsheet, "OnItemsChanged", ArgExpr.IsAny<NotifyCollectionChangedEventArgs>()).DoNothing();
            spreadsheet.ItemsSource = new List<TestData>();
            spreadsheet.OnApplyTemplate();
            Assert.IsNotNull(spreadsheet.ItemsCollection);
            Mock.Reset();

            //Create data item and its wrapper
            var dataItem = new TestData {Text = "test", FractionalNumber = 3.5};
            var itemWrap = ItemWrap.BuildNew(dataItem);

            //Arrabge ManageExpandStates and BindContainerToItem
            Mock.Arrange(() => spreadsheet.ManageExpandStates(Arg.IsAny<IDetailColumn>()));
            Mock.NonPublic.Arrange(spreadsheet, "BindContainerToItem", ArgExpr.IsAny<SpreadsheetItemContainer>(), ArgExpr.IsAny<IWrap>());

            //Make spreadsheet disposed
            privateAccessor.SetField("_disposed", true);
            spreadsheet.ItemsCollection.Add(itemWrap);

            //Check ManageExpandStates and BindContainerToItem invocations when spreadsheet is disposed
            Assert.IsTrue((bool)privateAccessor.GetField("_disposed"));
            Mock.NonPublic.Assert(spreadsheet, "BindContainerToItem", Occurs.Never(), ArgExpr.IsAny<SpreadsheetItemContainer>(), ArgExpr.IsAny<IWrap>());
            Mock.Assert(() => spreadsheet.ManageExpandStates(Arg.IsAny<IDetailColumn>()), Occurs.Never());

            //Mock ItemContainerGenerator.ContainerFromItem method and add new item to ItemsCollection
            //this invokes OnItemsChanged
            privateAccessor.SetField("_disposed", false);
            var container = new SpreadsheetItemContainer(spreadsheet);
            Mock.Arrange(() => spreadsheet.ItemContainerGenerator.ContainerFromItem(Arg.IsAny<object>())).Returns(container);
            Mock.Arrange(() => spreadsheet.ParentColumn).Returns(Mock.Create<IDetailColumn>());
            spreadsheet.ItemsCollection.Add(itemWrap);

            //Check ManageExpandStates and BindContainerToItem invocations when spreadsheet is not disposed
            Assert.IsFalse((bool)privateAccessor.GetField("_disposed"));
            Mock.NonPublic.Assert(spreadsheet, "BindContainerToItem", Occurs.Once(), container, itemWrap);
            Mock.Assert(() => spreadsheet.ManageExpandStates(Arg.IsAny<IDetailColumn>()), Occurs.Once());
        }
Exemplo n.º 8
0
        public void AssignItemsSourceAfterTemplateAppliedTest()
        {
            var spreadsheet = new SpreadsheetView();
            var spreadsheetData = new List<TestData>
            {
                new TestData{Text = "string1", FractionalNumber = 10.0},
                new TestData{Text = "string2", FractionalNumber = 20.0},
                new TestData{Text = "test text", FractionalNumber = 30.2}
            };

            var privateAccessor = new PrivateAccessor(spreadsheet);

            //First we call OnApplyTemplate and only then set value to ItemsSource
            spreadsheet.OnApplyTemplate();
            //FillItemsSource can not be called if ItemsSource is null
            Mock.NonPublic.Assert(spreadsheet, "FillItemsSource", Occurs.Never(), ArgExpr.IsAny<IEnumerable>());
            Assert.IsTrue((bool)privateAccessor.GetField("_templateApplied"));

            //FillItemsSource is invoked from OnItemsSourceChanged
            Mock.NonPublic.Arrange(spreadsheet, "FillItemsSource", ArgExpr.IsAny<IEnumerable>()).MustBeCalled();
            spreadsheet.ItemsSource = spreadsheetData;            
            Mock.Assert(spreadsheet);
        }
Exemplo n.º 9
0
        public void AlternateRowsMethodTest()
        {
            //Create spreadsheet and load data
            var spreadsheet = new SpreadsheetView();
            var privateAccessor = new PrivateAccessor(spreadsheet);
            var itemList = new List<TestData>
            {
                new TestData(),
                new TestData()     
            };
            spreadsheet.ItemsSource = itemList;
            spreadsheet.OnApplyTemplate();
            Mock.NonPublic.Arrange(typeof(SpreadsheetView), "PopulateCells", ArgExpr.IsAny<SpreadsheetItemContainer>(), ArgExpr.IsAny<IWrap>()).DoNothing();

            //Get item wrappers
            var item1 = spreadsheet.ItemsCollection[0];
            var item2 = spreadsheet.ItemsCollection[1];

            //Call ItemContainerGenerator overrides 
            Mock.NonPublic.Arrange(typeof(CellBase), "SetEditorParams").IgnoreInstance().DoNothing();
            spreadsheet.ItemContainerStyle = new Style(typeof(SpreadsheetItemContainer));
            var container1 = privateAccessor.CallMethod("GetContainerForItemOverride") as SpreadsheetItemContainer;            
            privateAccessor.CallMethod("PrepareContainerForItemOverride", container1, item1);
            var container2 = privateAccessor.CallMethod("GetContainerForItemOverride") as SpreadsheetItemContainer;            
            privateAccessor.CallMethod("PrepareContainerForItemOverride", container2, item2);

            //Mock SetIsAlternate method for all SpreadsheetItemContainer instances
            var containerMock = Mock.Create<SpreadsheetItemContainer>(Constructor.Mocked);
            Mock.Arrange(() => containerMock.SetIsAlternate()).IgnoreInstance();            

            //Make spreadsheet invisible
            Mock.Arrange(() => spreadsheet.IsVisible).Returns(false);
            Mock.Arrange(() => spreadsheet.Items).CallOriginal().OccursNever();
            spreadsheet.AlternateRows();
            Mock.Assert(spreadsheet);

            //Test AlternateRows method in normal conditions
            Mock.Arrange(() => spreadsheet.IsVisible).Returns(true);
            Mock.Arrange(() => spreadsheet.Items).CallOriginal().MustBeCalled();
            spreadsheet.AlternateRows();
            var containersDict = privateAccessor.GetField("_containersDict") as Dictionary<IWrap, SpreadsheetItemContainer>;
            Mock.Assert(() => containersDict[item1].SetIsAlternate(), Occurs.Once());
            Mock.Assert(() => containersDict[item2].SetIsAlternate(), Occurs.Once());
            Mock.Assert(spreadsheet);
        }