Exemplo n.º 1
0
        public void MoveCurrentTest()
        {
            ObservableCollection <TestData> dataList = new ObservableCollection <TestData>()
            {
                new TestData()
                {
                    StrValue = "A"
                },
                new TestData()
                {
                    StrValue = "B"
                },
                new TestData()
                {
                    StrValue = "C"
                }
            };

            DataGrid dataGrid;
            var      target      = new DataGridCurrentItemService();
            var      serviceList = new UIServiceCollection();

            serviceList.Add(new ServiceFactoryMock(target));
            using (TestWindow.Show(dataList, serviceList, out dataGrid))
            {
                dataGrid.Items.MoveCurrentTo(null);
                var helper   = new PrivateAccessHelper <DataGridCurrentItemService, DataGridCurrentItemService_Accessor>(target);
                var provider = helper.Target;
                var previewCurrentChangingEventHandlerHelper = new EventHandlerTestHelper <object, CancelableEventArgs>("PreviewCurrentChanging");
                provider.PreviewCurrentChanging += previewCurrentChangingEventHandlerHelper.Handler;
                var currentChangedEventHandlerHelper = new EventHandlerTestHelper <object, EventArgs>("CurrentChanged");
                provider.CurrentChanged += currentChangedEventHandlerHelper.Handler;

                provider.MoveCurrentTo(dataList[1]);
                Assert.IsTrue(previewCurrentChangingEventHandlerHelper.HandlerInvoked);
                Assert.IsTrue(currentChangedEventHandlerHelper.HandlerInvoked);
                previewCurrentChangingEventHandlerHelper.Reset();
                currentChangedEventHandlerHelper.Reset();

                // Block movement.
                previewCurrentChangingEventHandlerHelper.AdditionalHandling = (sender, args) => { args.Canceled = true; };
                Assert.IsFalse(provider.MoveCurrentTo(dataList[2]));
                Assert.IsTrue(previewCurrentChangingEventHandlerHelper.HandlerInvoked);
                Assert.IsFalse(currentChangedEventHandlerHelper.HandlerInvoked);
                previewCurrentChangingEventHandlerHelper.Reset();
                currentChangedEventHandlerHelper.Reset();

                previewCurrentChangingEventHandlerHelper.AdditionalHandling = null;

                // Move to the same row
                provider.MoveCurrentTo(provider.CurrentItem);
                Assert.IsFalse(previewCurrentChangingEventHandlerHelper.HandlerInvoked);
                Assert.IsFalse(currentChangedEventHandlerHelper.HandlerInvoked);
                previewCurrentChangingEventHandlerHelper.Reset();
                currentChangedEventHandlerHelper.Reset();

                provider.CurrentChanged         -= currentChangedEventHandlerHelper.Handler;
                provider.PreviewCurrentChanging -= previewCurrentChangingEventHandlerHelper.Handler;
            }
        }
Exemplo n.º 2
0
        public void PreviewCurrentChangingEventTest()
        {
            ObservableCollection <TestData> dataList = new ObservableCollection <TestData>()
            {
                new TestData()
                {
                    StrValue = "A"
                },
                new TestData()
                {
                    StrValue = "B"
                },
                new TestData()
                {
                    StrValue = "C"
                }
            };

            DataGrid            dataGrid;
            var                 target      = new DataGridCurrentItemService();
            UIServiceCollection serviceList = new UIServiceCollection();

            serviceList.Add(new ServiceFactoryMock(target));
            using (TestWindow.Show(dataList, serviceList, out dataGrid))
            {
                dataGrid.CurrentItem = null;
                var helper   = new PrivateAccessHelper <DataGridCurrentItemService, DataGridCurrentItemService_Accessor>(target);
                var provider = helper.Target;
                var previewCurrentChangingEventHandlerHelper = new EventHandlerTestHelper <object, CancelableEventArgs>("PreviewCurrentChanging");
                provider.PreviewCurrentChanging += previewCurrentChangingEventHandlerHelper.Handler;

                dataGrid.CurrentCell = new DataGridCellInfo(dataList[0], dataGrid.Columns[0]);
                Assert.IsTrue(previewCurrentChangingEventHandlerHelper.HandlerInvoked);
                Assert.IsFalse(previewCurrentChangingEventHandlerHelper.LastInocationEventArgs.IsCancelable);
                previewCurrentChangingEventHandlerHelper.Reset();

                // Move to another line.
                dataGrid.CurrentCell = new DataGridCellInfo(dataList[1], dataGrid.Columns[0]);
                Assert.IsTrue(previewCurrentChangingEventHandlerHelper.HandlerInvoked);
                Assert.IsFalse(previewCurrentChangingEventHandlerHelper.LastInocationEventArgs.IsCancelable);
                previewCurrentChangingEventHandlerHelper.Reset();

                // Move to another cell on the same line
                dataGrid.CurrentCell = new DataGridCellInfo(dataList[1], dataGrid.Columns[1]);
                Assert.IsFalse(previewCurrentChangingEventHandlerHelper.HandlerInvoked);
                previewCurrentChangingEventHandlerHelper.Reset();

                // Move to another line using the provider.
                provider.MoveCurrentTo(dataList[2]);
                Assert.IsTrue(previewCurrentChangingEventHandlerHelper.HandlerInvoked);
                Assert.IsTrue(previewCurrentChangingEventHandlerHelper.LastInocationEventArgs.IsCancelable);

                provider.PreviewCurrentChanging -= previewCurrentChangingEventHandlerHelper.Handler;
            }
        }
Exemplo n.º 3
0
        public void CurrentItemTest()
        {
            ObservableCollection <TestData> dataList = new ObservableCollection <TestData>()
            {
                new TestData()
                {
                    StrValue = "A"
                },
                new TestData()
                {
                    StrValue = "B"
                },
                new TestData()
                {
                    StrValue = "C"
                }
            };

            DataGrid dataGrid;
            var      target      = new DataGridCurrentItemService();
            var      serviceList = new UIServiceCollection();

            serviceList.Add(new ServiceFactoryMock(target));
            using (TestWindow.Show(dataList, serviceList, out dataGrid))
            {
                dataGrid.Items.MoveCurrentTo(null);
                var helper   = new PrivateAccessHelper <DataGridCurrentItemService, DataGridCurrentItemService_Accessor>(target);
                var provider = helper.Target;

                Assert.IsNull(provider.CurrentItem);
                Assert.AreEqual(-1, provider.CurrentPosition);

                Assert.IsTrue(provider.MoveCurrentToFirst());
                Assert.AreSame(dataList[0], provider.CurrentItem);
                Assert.AreSame(dataGrid.CurrentItem, provider.CurrentItem);
                Assert.AreEqual(0, provider.CurrentPosition);

                Assert.IsTrue(provider.MoveCurrentToNext());
                Assert.AreSame(dataList[1], provider.CurrentItem);
                Assert.AreSame(dataGrid.CurrentItem, provider.CurrentItem);
                Assert.AreEqual(1, provider.CurrentPosition);

                Assert.IsTrue(provider.MoveCurrentToLast());
                Assert.AreSame(dataList[2], provider.CurrentItem);
                Assert.AreSame(dataGrid.CurrentItem, provider.CurrentItem);
                Assert.AreEqual(2, provider.CurrentPosition);

                Assert.IsFalse(provider.MoveCurrentToNext());
                Assert.IsNull(provider.CurrentItem);
                Assert.AreSame(dataGrid.CurrentItem, provider.CurrentItem);
                Assert.AreEqual(dataGrid.Items.Count, provider.CurrentPosition);

                dataGrid.CurrentItem = dataList[1];
                Assert.AreSame(dataList[1], provider.CurrentItem);
                Assert.AreSame(dataGrid.CurrentItem, provider.CurrentItem);
                Assert.AreEqual(1, provider.CurrentPosition);

                dataGrid.CurrentItem = null;
                Assert.IsNull(provider.CurrentItem);
                Assert.AreSame(dataGrid.CurrentItem, provider.CurrentItem);
                Assert.AreEqual(-1, provider.CurrentPosition);
            }
        }