public void CancelableEventsOnCommandifiedListTests() { CommandifiedList <int> list = new CommandifiedList <int>() { 1, 2, 3 }; EventHandler <CancelableListModificationEventArgs <int> > cancelableHandler = delegate(object sender, CancelableListModificationEventArgs <int> e) { e.Cancel = true; }; list.ElementAdding += cancelableHandler; list.ElementRemoving += cancelableHandler; list.ListClearing += cancelableHandler; // reset command queue manager, so it will only undo commands issued after this reset CommandQueueManagerSingleton.GetInstance().ResetActiveCommandQueue(); list.Add(4); Assert.AreEqual(3, list.Count); Assert.IsFalse(list.Contains(4)); // undo shouldn't have any effect CommandQueueManagerSingleton.GetInstance().UndoLastCommand(); Assert.AreEqual(3, list.Count); Assert.IsFalse(list.Contains(4)); list.Clear(); Assert.AreEqual(3, list.Count); // undo shouldn't have any effect CommandQueueManagerSingleton.GetInstance().UndoLastCommand(); Assert.AreEqual(3, list.Count); list.Remove(1); Assert.AreEqual(3, list.Count); Assert.IsTrue(list.Contains(1)); // undo shouldn't have any effect CommandQueueManagerSingleton.GetInstance().UndoLastCommand(); Assert.AreEqual(3, list.Count); Assert.IsTrue(list.Contains(1)); list.ElementAdding -= cancelableHandler; list.ElementRemoving -= cancelableHandler; list.ListClearing -= cancelableHandler; list.Add(4); Assert.AreEqual(4, list.Count); Assert.IsTrue(list.Contains(4)); list.Remove(1); Assert.AreEqual(3, list.Count); Assert.IsFalse(list.Contains(1)); list.Clear(); Assert.AreEqual(0, list.Count); }
public void MultiThreadedSyncedCommandifiedListAccessTest() { // set up our session. Guid sessionId = Guid.NewGuid(); CQManager.ActivateCommandQueueStack(sessionId); var toTest = new CommandifiedList <string>(isSynchronized: true); for (int i = 0; i < 10; i++) { toTest.Add(i.ToString()); } var waitHandles = new WaitHandle[] { new AutoResetEvent(false), new AutoResetEvent(false) }; Exception caughtException = null; Console.WriteLine("Starting threads..."); var threadA = new Thread(() => MultiThreadedSyncedCommandifiedListAccessTest_ThreadA(toTest, waitHandles[0], (e) => caughtException = e)); var threadB = new Thread(() => MultiThreadedSyncedCommandifiedListAccessTest_ThreadB(toTest, waitHandles[1], (e) => caughtException = e)); threadA.Start(); threadB.Start(); Console.WriteLine("Threads started... waiting for handles"); WaitHandle.WaitAll(waitHandles); if (caughtException != null) { throw caughtException; } Console.WriteLine("All completed."); }
public void CommandifiedListInsertTest() { Guid sessionId = Guid.NewGuid(); CQManager.ActivateCommandQueueStack(sessionId); CommandifiedList <string> toTest = new CommandifiedList <string>() { "Foo", "Bar" }; Assert.AreEqual(2, toTest.Count); Assert.AreEqual("Foo", toTest[0]); Assert.AreEqual("Bar", toTest[1]); // perform an insert operation, this can be triggered by both 'Add' and 'Insert'. We'll undo this later on. toTest.Add("Blah"); Assert.AreEqual(3, toTest.Count); Assert.AreEqual("Blah", toTest[2]); // undo operation. CQManager.UndoLastCommand(); Assert.AreEqual(2, toTest.Count); Assert.AreEqual("Foo", toTest[0]); Assert.AreEqual("Bar", toTest[1]); toTest.Insert(1, "Blah"); Assert.AreEqual(3, toTest.Count); Assert.AreEqual("Blah", toTest[1]); Assert.AreEqual("Bar", toTest[2]); // undo operation. CQManager.UndoLastCommand(); Assert.AreEqual(2, toTest.Count); Assert.AreEqual("Foo", toTest[0]); Assert.AreEqual("Bar", toTest[1]); CQManager.ActivateCommandQueueStack(Guid.Empty); }
public void CommandifiedListInsertTest() { Guid sessionId = Guid.NewGuid(); CQManager.ActivateCommandQueueStack(sessionId); CommandifiedList<string> toTest = new CommandifiedList<string>() { "Foo", "Bar"}; Assert.AreEqual(2, toTest.Count); Assert.AreEqual("Foo", toTest[0]); Assert.AreEqual("Bar", toTest[1]); // perform an insert operation, this can be triggered by both 'Add' and 'Insert'. We'll undo this later on. toTest.Add("Blah"); Assert.AreEqual(3, toTest.Count); Assert.AreEqual("Blah", toTest[2]); // undo operation. CQManager.UndoLastCommand(); Assert.AreEqual(2, toTest.Count); Assert.AreEqual("Foo", toTest[0]); Assert.AreEqual("Bar", toTest[1]); toTest.Insert(1, "Blah"); Assert.AreEqual(3, toTest.Count); Assert.AreEqual("Blah", toTest[1]); Assert.AreEqual("Bar", toTest[2]); // undo operation. CQManager.UndoLastCommand(); Assert.AreEqual(2, toTest.Count); Assert.AreEqual("Foo", toTest[0]); Assert.AreEqual("Bar", toTest[1]); CQManager.ActivateCommandQueueStack(Guid.Empty); }
public void CancelableEventsOnCommandifiedListTests() { CommandifiedList<int> list = new CommandifiedList<int>() { 1, 2, 3 }; EventHandler<CancelableListModificationEventArgs<int>> cancelableHandler = delegate(object sender, CancelableListModificationEventArgs<int> e) { e.Cancel = true; }; list.ElementAdding += cancelableHandler; list.ElementRemoving += cancelableHandler; list.ListClearing += cancelableHandler; // reset command queue manager, so it will only undo commands issued after this reset CommandQueueManagerSingleton.GetInstance().ResetActiveCommandQueue(); list.Add(4); Assert.AreEqual(3, list.Count); Assert.IsFalse(list.Contains(4)); // undo shouldn't have any effect CommandQueueManagerSingleton.GetInstance().UndoLastCommand(); Assert.AreEqual(3, list.Count); Assert.IsFalse(list.Contains(4)); list.Clear(); Assert.AreEqual(3, list.Count); // undo shouldn't have any effect CommandQueueManagerSingleton.GetInstance().UndoLastCommand(); Assert.AreEqual(3, list.Count); list.Remove(1); Assert.AreEqual(3, list.Count); Assert.IsTrue(list.Contains(1)); // undo shouldn't have any effect CommandQueueManagerSingleton.GetInstance().UndoLastCommand(); Assert.AreEqual(3, list.Count); Assert.IsTrue(list.Contains(1)); list.ElementAdding -= cancelableHandler; list.ElementRemoving -= cancelableHandler; list.ListClearing -= cancelableHandler; list.Add(4); Assert.AreEqual(4, list.Count); Assert.IsTrue(list.Contains(4)); list.Remove(1); Assert.AreEqual(3, list.Count); Assert.IsFalse(list.Contains(1)); list.Clear(); Assert.AreEqual(0, list.Count); }