PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Branches/VS2010/Source/Core/Core.Tests/TaskModel/TaskServiceTest.cs

#
C# | 395 lines | 316 code | 72 blank | 7 comment | 7 complexity | 1cada51c3c8e98e9da4f9f21d2f2787a MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1, LGPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using DanielVaughan.TaskModel;
  6. using DanielVaughan.Tests.Mocks;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. namespace DanielVaughan.Tests.TaskModel
  9. {
  10. /// <summary>
  11. ///This is a test class for PropertyChangedNotifierTest and is intended
  12. ///to contain all PropertyChangedNotifierTest Unit Tests
  13. ///</summary>
  14. [TestClass]
  15. public class TaskServiceTest
  16. {
  17. [TestMethod]
  18. public void AGlobalTaskShouldBePerformed()
  19. {
  20. var mockTask = new MockTask();
  21. string expectedValue1 = "1";
  22. var target = new TaskService();
  23. target.PerformTask(mockTask, expectedValue1, null);
  24. Assert.AreEqual(mockTask.LastArgument, expectedValue1);
  25. Assert.AreEqual(mockTask.ExecutionCount, 1);
  26. }
  27. [TestMethod]
  28. public void TaskServiceShouldUndoGlobalTasks()
  29. {
  30. TaskServiceShouldUndoTasks(null);
  31. }
  32. [TestMethod]
  33. public void TaskServiceShouldUndoNonGlobalTasks()
  34. {
  35. TaskServiceShouldUndoTasks(new object());
  36. }
  37. void TaskServiceShouldUndoTasks(object key)
  38. {
  39. var mockTask1 = new MockUndoableTask { RepeatableTest = true };
  40. string expectedValue1 = "1";
  41. string expectedValue2 = "2";
  42. var target = new TaskService();
  43. Assert.IsFalse(target.CanUndo(key));
  44. Assert.IsFalse(target.CanRedo(key));
  45. Assert.IsFalse(target.CanRepeat(key));
  46. target.PerformTask(mockTask1, expectedValue1, key);
  47. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  48. Assert.AreEqual(mockTask1.ExecutionCount, 1);
  49. Assert.IsTrue(target.CanUndo(key));
  50. Assert.IsFalse(target.CanRedo(key));
  51. Assert.IsTrue(target.CanRepeat(key));
  52. target.Undo(key);
  53. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  54. Assert.AreEqual(mockTask1.ExecutionCount, 0);
  55. Assert.IsFalse(target.CanUndo(key));
  56. Assert.IsTrue(target.CanRedo(key));
  57. Assert.IsFalse(target.CanRepeat(key));
  58. target.Redo(key);
  59. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  60. Assert.AreEqual(mockTask1.ExecutionCount, 1);
  61. Assert.IsTrue(target.CanUndo(key));
  62. Assert.IsFalse(target.CanRedo(key));
  63. Assert.IsTrue(target.CanRepeat(key));
  64. target.Repeat(key);
  65. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  66. Assert.AreEqual(mockTask1.ExecutionCount, 2);
  67. Assert.IsTrue(target.CanUndo(key));
  68. Assert.IsFalse(target.CanRedo(key));
  69. Assert.IsTrue(target.CanRepeat(key));
  70. target.Repeat(key);
  71. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  72. Assert.AreEqual(mockTask1.ExecutionCount, 3);
  73. Assert.IsTrue(target.CanUndo(key));
  74. Assert.IsFalse(target.CanRedo(key));
  75. Assert.IsTrue(target.CanRepeat(key));
  76. target.Undo(key);
  77. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  78. Assert.AreEqual(mockTask1.ExecutionCount, 2);
  79. Assert.IsTrue(target.CanUndo(key));
  80. Assert.IsTrue(target.CanRedo(key));
  81. Assert.IsTrue(target.CanRepeat(key));
  82. target.Undo(key);
  83. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  84. Assert.AreEqual(mockTask1.ExecutionCount, 1);
  85. Assert.IsTrue(target.CanUndo(key));
  86. Assert.IsTrue(target.CanRedo(key));
  87. Assert.IsTrue(target.CanRepeat(key));
  88. target.Undo(key);
  89. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  90. Assert.AreEqual(mockTask1.ExecutionCount, 0);
  91. Assert.IsFalse(target.CanUndo(key));
  92. Assert.IsTrue(target.CanRedo(key));
  93. Assert.IsFalse(target.CanRepeat(key));
  94. var mockTask2 = new MockUndoableTask { RepeatableTest = true };
  95. target.PerformTask(mockTask1, expectedValue1, key);
  96. target.PerformTask(mockTask2, expectedValue2, key);
  97. Assert.AreEqual(mockTask1.LastArgument, expectedValue1);
  98. Assert.AreEqual(mockTask1.ExecutionCount, 1);
  99. Assert.AreEqual(mockTask2.LastArgument, expectedValue2);
  100. Assert.AreEqual(mockTask2.ExecutionCount, 1);
  101. Assert.IsTrue(target.CanUndo(key));
  102. Assert.IsFalse(target.CanRedo(key));
  103. Assert.IsTrue(target.CanRepeat(key));
  104. target.Undo(key);
  105. Assert.AreEqual(mockTask1.ExecutionCount, 1);
  106. Assert.AreEqual(mockTask2.ExecutionCount, 0);
  107. Assert.IsTrue(target.CanUndo(key));
  108. Assert.IsTrue(target.CanRedo(key));
  109. Assert.IsTrue(target.CanRepeat(key));
  110. var list = target.GetRepeatableTasks(key);
  111. Assert.IsNotNull(list);
  112. Assert.IsTrue(list.ToList().Count == 1);
  113. }
  114. [TestMethod]
  115. public void GetRepeatableTasksShouldReturnRepeatableTasks()
  116. {
  117. GetRepeatableTasksShouldReturnRepeatableTasks(null);
  118. }
  119. [TestMethod]
  120. public void GetRepeatableGlobalTasksShouldReturnRepeatableTasks()
  121. {
  122. GetRepeatableTasksShouldReturnRepeatableTasks(new object());
  123. }
  124. void GetRepeatableTasksShouldReturnRepeatableTasks(object key)
  125. {
  126. var task1 = new MockTask { RepeatableTest = true };
  127. var target = new TaskService();
  128. var list = target.GetRepeatableTasks(key);
  129. Assert.IsTrue(list.Count() < 1);
  130. target.PerformTask(task1, string.Empty, key);
  131. list = target.GetRepeatableTasks(key);
  132. Assert.IsTrue(list.Count() > 0);
  133. }
  134. [TestMethod]
  135. public void NonRepeatableGlobalTaskShouldNotBeRepeatable()
  136. {
  137. var task1 = new MockTask();
  138. var target = new TaskService();
  139. string arg1 = "1";
  140. Assert.IsFalse(target.CanRepeat(null));
  141. target.PerformTask(task1, arg1, null);
  142. Assert.IsFalse(target.CanRepeat(null));
  143. }
  144. [TestMethod]
  145. public void RepeatableGlobalTaskShouldBeRepeatable()
  146. {
  147. var task1 = new MockTask {RepeatableTest = true};
  148. var target = new TaskService();
  149. string arg1 = "1";
  150. Assert.IsFalse(target.CanRepeat(null));
  151. target.PerformTask(task1, arg1, null);
  152. Assert.IsTrue(target.CanRepeat(null));
  153. target.Repeat(null);
  154. Assert.IsTrue(task1.ExecutionCount == 2);
  155. }
  156. [TestMethod]
  157. public void NonRepeatableTaskShouldNotBeRepeatable()
  158. {
  159. var task1 = new MockTask();
  160. var target = new TaskService();
  161. object key = new object();
  162. Assert.IsFalse(target.CanRepeat(key));
  163. target.PerformTask(task1, "1", key);
  164. Assert.IsFalse(target.CanRepeat(key));
  165. }
  166. [TestMethod]
  167. public void RepeatableTaskShouldBeRepeatable()
  168. {
  169. var task1 = new MockTask { RepeatableTest = true };
  170. var target = new TaskService();
  171. object key = new object();
  172. Assert.IsFalse(target.CanRepeat(key));
  173. target.PerformTask(task1, "1", key);
  174. Assert.IsTrue(target.CanRepeat(key));
  175. target.Repeat(key);
  176. Assert.IsTrue(task1.ExecutionCount == 2);
  177. }
  178. [TestMethod]
  179. public void UndoableTaskShouldBeRedoable()
  180. {
  181. UndoableTaskShouldBeRedoable(null);
  182. }
  183. [TestMethod]
  184. public void UndoableGlobalTaskShouldBeRedoable()
  185. {
  186. UndoableTaskShouldBeRedoable(new object());
  187. }
  188. void UndoableTaskShouldBeRedoable(object key)
  189. {
  190. var task1 = new MockUndoableTask();
  191. var target = new TaskService();
  192. Assert.IsFalse(target.CanRedo(key));
  193. target.PerformTask(task1, "1", key);
  194. target.Undo(key);
  195. Assert.IsTrue(target.CanRedo(key));
  196. }
  197. [TestMethod]
  198. public void NonUndoableTasksShouldNotBeUndoable()
  199. {
  200. NonUndoableTasksShouldNotBeUndoable(null);
  201. }
  202. [TestMethod]
  203. public void NonUndoableGlobalTasksShouldNotBeUndoable()
  204. {
  205. NonUndoableTasksShouldNotBeUndoable(new object());
  206. }
  207. [TestMethod]
  208. public void PerformingGlobalTaskShouldClearUndoableList()
  209. {
  210. PerformingTaskShouldClearUndoableList(null);
  211. }
  212. [TestMethod]
  213. public void PerformingTaskShouldClearUndoableList()
  214. {
  215. PerformingTaskShouldClearUndoableList(new object());
  216. }
  217. void PerformingTaskShouldClearUndoableList(object key)
  218. {
  219. /* First set up some undoable tasks. */
  220. var task1 = new MockUndoableTask();
  221. var target = new TaskService();
  222. target.PerformTask(task1, "1", key);
  223. Assert.IsTrue(target.CanUndo(key));
  224. /* Perform a non-undoable task. This must clear the undoable list. */
  225. var task2 = new MockTask { RepeatableTest = true };
  226. target.PerformTask(task2, "1", key);
  227. Assert.IsFalse(target.CanUndo(key));
  228. }
  229. void NonUndoableTasksShouldNotBeUndoable(object key)
  230. {
  231. var task1 = new MockTask();
  232. var target = new TaskService();
  233. Assert.IsFalse(target.CanUndo(key));
  234. target.PerformTask(task1, "1", key);
  235. Assert.IsFalse(target.CanUndo(key));
  236. }
  237. [TestMethod]
  238. public void CompositeTasksShouldbePerformedInParallel()
  239. {
  240. CompositeTasksShouldBePerformedInParallel(new object());
  241. }
  242. [TestMethod]
  243. public void GlobalCompositeTasksShouldBePerformedInParallel()
  244. {
  245. CompositeTasksShouldBePerformedInParallel(null);
  246. }
  247. void CompositeTasksShouldBePerformedInParallel(object contextKey)
  248. {
  249. var tasks = new Dictionary<TaskBase<string>, string>();
  250. for (int i = 0; i < 100; i++)
  251. {
  252. tasks.Add(new MockTask(), i.ToString());
  253. }
  254. var compositeTask = new CompositeTask<string>(tasks, "1") { Parallel = true };
  255. var target = new TaskService();
  256. target.PerformTask(compositeTask, null, contextKey);
  257. foreach (KeyValuePair<TaskBase<string>, string> keyValuePair in tasks)
  258. {
  259. var mockTask = (MockTask)keyValuePair.Key;
  260. Assert.AreEqual(1, mockTask.ExecutionCount);
  261. }
  262. }
  263. [TestMethod]
  264. public void CompositeUndoableTasksShouldbePerformedInParallel()
  265. {
  266. CompositeUndoableTasksShouldBePerformedInParallel(new object());
  267. }
  268. [TestMethod]
  269. public void GlobalCompositeUndoableTasksShouldBePerformedInParallel()
  270. {
  271. CompositeUndoableTasksShouldBePerformedInParallel(null);
  272. }
  273. void CompositeUndoableTasksShouldBePerformedInParallel(object contextKey)
  274. {
  275. var tasks = new Dictionary<UndoableTaskBase<string>, string>();
  276. for (int i = 0; i < 100; i++)
  277. {
  278. tasks.Add(new MockUndoableTask(), i.ToString());
  279. }
  280. var compositeTask = new CompositeUndoableTask<string>(tasks, "1") { Parallel = true };
  281. var target = new TaskService();
  282. target.PerformTask(compositeTask, null, contextKey);
  283. foreach (KeyValuePair<UndoableTaskBase<string>, string> keyValuePair in tasks)
  284. {
  285. var mockTask = (MockUndoableTask)keyValuePair.Key;
  286. Assert.AreEqual(1, mockTask.ExecutionCount);
  287. }
  288. /* Test undo. */
  289. target.Undo(contextKey);
  290. foreach (KeyValuePair<UndoableTaskBase<string>, string> keyValuePair in tasks)
  291. {
  292. var mockTask = (MockUndoableTask)keyValuePair.Key;
  293. Assert.AreEqual(0, mockTask.ExecutionCount);
  294. }
  295. }
  296. [TestMethod]
  297. public void AGlobalUndoLimitShouldBeEnforced()
  298. {
  299. string key = "key";
  300. var mockTask = new MockTask();
  301. string expectedValue1 = "1";
  302. var target = new TaskService();
  303. target.SetMaximumUndoCount(2, key);
  304. Assert.AreEqual(0, target.GetTaskCount(TaskService.TaskType.Repeatable, key), "Repeatable task count should be 0.");
  305. Assert.AreEqual(0, target.GetTaskCount(TaskService.TaskType.Undoable, key), "Undoable task count should be 0.");
  306. Assert.AreEqual(0, target.GetTaskCount(TaskService.TaskType.Redoable, key), "Redoable task count should be 0.");
  307. for (int i = 0; i < 5; i++)
  308. {
  309. target.PerformTask(mockTask, expectedValue1, key);
  310. }
  311. Assert.AreEqual(2, target.GetTaskCount(TaskService.TaskType.Repeatable, key), "Repeatable tasks.");
  312. Assert.AreEqual(0, target.GetTaskCount(TaskService.TaskType.Undoable, key), "Undoable tasks.");
  313. Assert.AreEqual(0, target.GetTaskCount(TaskService.TaskType.Redoable, key), "Redoable tasks.");
  314. MockUndoableTask mockUndoableTask = new MockUndoableTask();
  315. for (int i = 0; i < 5; i++)
  316. {
  317. target.PerformTask(mockUndoableTask, i.ToString(), key);
  318. }
  319. Assert.AreEqual(2, target.GetTaskCount(TaskService.TaskType.Repeatable, key), "Repeatable tasks.");
  320. Assert.AreEqual(2, target.GetTaskCount(TaskService.TaskType.Undoable, key), "Undoable tasks.");
  321. }
  322. }
  323. }