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());
        }
Esempio n. 2
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());
        }