public void CancelEditTest() { // check initial values before we edit TestClass editItem = CollectionView[0] as TestClass; Assert.AreEqual(1, editItem.IntProperty); Assert.AreEqual("A", editItem.StringProperty); // edit the item CollectionView.EditItem(CollectionView[0]); // set new values during the edit editItem.IntProperty = 0; editItem.StringProperty = "Edit"; // some collections or items cannot cancel edits. // we have a separate test for this if (CollectionView.CanCancelEdit) { // check that after we cancel, the old values are reverted CollectionView.CancelEdit(); Assert.AreEqual(1, editItem.IntProperty); Assert.AreEqual("A", editItem.StringProperty); } }
public void IEditableObject() { // we only want to run this test for items that implement IEditableObject // and where the source collection won't handle the editing first EditableTestClass editItem = CollectionView[0] as EditableTestClass; if (editItem == null) { return; } // check that the debug string has not been set Assert.IsNull(editItem.DebugString); // verify that BeginEdit was called on the IEditable interface CollectionView.EditItem(editItem); Assert.AreEqual("BeginEdit", editItem.DebugString); // verify that CancelEdit was called on the IEditable interface CollectionView.CancelEdit(); Assert.AreEqual("CancelEdit", editItem.DebugString); // verify that EndEdit was called on the IEditable interface CollectionView.EditItem(editItem); CollectionView.CommitEdit(); Assert.AreEqual("EndEdit", editItem.DebugString); // if we don't implement IList, we cannot run the rest of // the test as we cannot add/remove items if (this.ImplementsIList) { // verify that when adding a new item, it will call BeginEdit on it editItem = CollectionView.AddNew() as EditableTestClass; Assert.AreEqual("BeginEdit", editItem.DebugString); // verify that when canceling the new item, it will call CancelEdit on it. CollectionView.CancelNew(); Assert.AreEqual("CancelEdit", editItem.DebugString); // verify that when committing a new item, it will call EndEdit on it. editItem = CollectionView.AddNew() as EditableTestClass; CollectionView.CommitNew(); Assert.AreEqual("EndEdit", editItem.DebugString); } }
public void CanCancelEditTest() { object editItem = CollectionView[0]; CollectionView.EditItem(CollectionView[0]); if (editItem is IEditableObject) { Assert.IsTrue(CollectionView.CanCancelEdit); // if we don't implement IList, we cannot run the rest of // the test as we cannot add/remove items if (this.ImplementsIList) { // verify that we cannot call CommitEdit during an AddNew operation CollectionView.AddNew(); AssertExpectedException( new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, PagedCollectionViewResources.OperationNotAllowedDuringTransaction, "CancelEdit", "AddNew")), delegate { CollectionView.CancelEdit(); }); CollectionView.CommitNew(); } // if we are not editing trying to CancelEdit will throw if (!CollectionView.CanCancelEdit) { AssertExpectedException( new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, PagedCollectionViewResources.CancelEditNotSupported)), delegate { CollectionView.CancelEdit(); }); } } else { // if our collection does not implement IEditableCollection // and our data does not impelement IEditableObject, then we // cannot cancel an edit, as we do not have the old data cached Assert.IsFalse(CollectionView.CanCancelEdit); } }