Example #1
0
        public void DataGridViewCellAccessibleObject_Select_TakeFocus()
        {
            using var dataGridView = new SubDataGridView();
            dataGridView.CreateControl();
            Assert.True(dataGridView.IsHandleCreated);

            var mockCell = new Mock <DataGridViewCell>(MockBehavior.Strict);

            mockCell
            .SetupSet(s => s.State = DataGridViewElementStates.Visible)
            .Verifiable();
            mockCell
            .Protected()
            .Setup("Dispose", ItExpr.IsAny <bool>());
            mockCell
            .Protected()
            .Setup("OnDataGridViewChanged");

            var mockObj = mockCell.Object;

            mockObj.DataGridView = dataGridView;

            var accessibleObject = new DataGridViewCellAccessibleObject(mockObj);

            // verify that we check for a flag, not direct comparison. 128 is an arbitrary large flag.
            accessibleObject.Select((AccessibleSelection)128 | AccessibleSelection.TakeFocus);

            // NB: some asserts are implicit - check that nothing was called on the mock that we didn't anticipate
            Assert.True(dataGridView.FocusCalled);
        }
        public void DataGridViewColumnCollection_Add_NoDimensionChangeAllowed_ThrowsInvalidOperationException()
        {
            using var control = new SubDataGridView();
            var collection = new DataGridViewColumnCollection(control);
            int callCount  = 0;

            control.RowValidating += (sender, e) =>
            {
                var column = new DataGridViewColumn(new SubDataGridViewCell());
                Assert.Throws <InvalidOperationException>(() => collection.Add(null));
                Assert.Throws <InvalidOperationException>(() => collection.Add(column));
                callCount++;
            };
            control.OnRowValidating(null);
            Assert.Equal(1, callCount);
        }