|
| 1 | +/* |
| 2 | + PureMVC C# Port by Andy Adamczak <andy.adamczak@puremvc.org>, et al. |
| 3 | + PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved. |
| 4 | + Your reuse is governed by the Creative Commons Attribution 3.0 License |
| 5 | +*/ |
| 6 | +using System; |
| 7 | +using System.Threading; |
| 8 | +using System.Collections.Generic; |
| 9 | + |
| 10 | +using NUnitLite; |
| 11 | +using NUnit.Framework; |
| 12 | + |
| 13 | +using PureMVC.Interfaces; |
| 14 | +using PureMVC.Patterns; |
| 15 | +using PureMVC.Core; |
| 16 | + |
| 17 | +namespace PureMVC.Tests.Core |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Test the PureMVC Controller class. |
| 21 | + */ |
| 22 | + [TestFixture] |
| 23 | + public class ControllerTest : TestCase |
| 24 | + { |
| 25 | + /** |
| 26 | + * Constructor. |
| 27 | + * |
| 28 | + * @param methodName the name of the test method an instance to run |
| 29 | + */ |
| 30 | + public ControllerTest(string methodName) |
| 31 | + : base(methodName) |
| 32 | + { } |
| 33 | + |
| 34 | + /** |
| 35 | + * Create the TestSuite. |
| 36 | + */ |
| 37 | + public static ITest Suite |
| 38 | + { |
| 39 | + get |
| 40 | + { |
| 41 | + TestSuite ts = new TestSuite(typeof(ControllerTest)); |
| 42 | + |
| 43 | + ts.AddTest(new ControllerTest("TestGetInstance")); |
| 44 | + ts.AddTest(new ControllerTest("TestRegisterAndExecuteCommand")); |
| 45 | + ts.AddTest(new ControllerTest("TestRegisterAndRemoveCommand")); |
| 46 | + ts.AddTest(new ControllerTest("TestHasCommand")); |
| 47 | + ts.AddTest(new ControllerTest("TestReregisterAndExecuteCommand")); |
| 48 | + ts.AddTest(new ControllerTest("TestMultiThreadedOperations")); |
| 49 | + |
| 50 | + return ts; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Tests the Controller Singleton Factory Method |
| 56 | + */ |
| 57 | + public void TestGetInstance() |
| 58 | + { |
| 59 | + // Test Factory Method |
| 60 | + IController controller = Controller.Instance; |
| 61 | + |
| 62 | + // test assertions |
| 63 | + Assert.NotNull(controller, "Expecting instance not null"); |
| 64 | + Assert.True(controller is IController, "Expecting instance implements IController"); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Tests Command registration and execution. |
| 69 | + * |
| 70 | + * <P> |
| 71 | + * This test gets the Singleton Controller instance |
| 72 | + * and registers the ControllerTestCommand class |
| 73 | + * to handle 'ControllerTest' Notifications.<P> |
| 74 | + * |
| 75 | + * <P> |
| 76 | + * It then constructs such a Notification and tells the |
| 77 | + * Controller to execute the associated Command. |
| 78 | + * Success is determined by evaluating a property |
| 79 | + * on an object passed to the Command, which will |
| 80 | + * be modified when the Command executes.</P> |
| 81 | + * |
| 82 | + */ |
| 83 | + public void TestRegisterAndExecuteCommand() |
| 84 | + { |
| 85 | + // Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes |
| 86 | + IController controller = Controller.Instance; |
| 87 | + string name = "ControllerTest" + Thread.CurrentThread.Name; |
| 88 | + controller.RegisterCommand(name, typeof(ControllerTestCommand)); |
| 89 | + |
| 90 | + // Create a 'ControllerTest' note |
| 91 | + ControllerTestVO vo = new ControllerTestVO(12); |
| 92 | + INotification note = new Notification(name, vo); |
| 93 | + |
| 94 | + // Tell the controller to execute the Command associated with the note |
| 95 | + // the ControllerTestCommand invoked will multiply the vo.input value |
| 96 | + // by 2 and set the result on vo.result |
| 97 | + controller.ExecuteCommand(note); |
| 98 | + |
| 99 | + // test assertions |
| 100 | + Assert.True(vo.result == 24, "Expecting vo.result == 24"); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Tests Command registration and removal. |
| 105 | + * |
| 106 | + * <P> |
| 107 | + * Tests that once a Command is registered and verified |
| 108 | + * working, it can be removed from the Controller.</P> |
| 109 | + */ |
| 110 | + public void TestRegisterAndRemoveCommand() |
| 111 | + { |
| 112 | + |
| 113 | + // Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes |
| 114 | + IController controller = Controller.Instance; |
| 115 | + string name = "ControllerRemoveTest" + Thread.CurrentThread.Name; |
| 116 | + controller.RegisterCommand(name, typeof(ControllerTestCommand)); |
| 117 | + |
| 118 | + // Create a 'ControllerTest' note |
| 119 | + ControllerTestVO vo = new ControllerTestVO(12); |
| 120 | + INotification note = new Notification(name, vo); |
| 121 | + |
| 122 | + // Tell the controller to execute the Command associated with the note |
| 123 | + // the ControllerTestCommand invoked will multiply the vo.input value |
| 124 | + // by 2 and set the result on vo.result |
| 125 | + controller.ExecuteCommand(note); |
| 126 | + |
| 127 | + // test assertions |
| 128 | + Assert.True(vo.result == 24, "Expecting vo.result == 24"); |
| 129 | + |
| 130 | + // Reset result |
| 131 | + vo.result = 0; |
| 132 | + |
| 133 | + // Remove the Command from the Controller |
| 134 | + controller.RemoveCommand(name); |
| 135 | + |
| 136 | + // Tell the controller to execute the Command associated with the |
| 137 | + // note. This time, it should not be registered, and our vo result |
| 138 | + // will not change |
| 139 | + controller.ExecuteCommand(note); |
| 140 | + |
| 141 | + // test assertions |
| 142 | + Assert.True(vo.result == 0, "Expecting vo.result == 0"); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Test hasCommand method. |
| 148 | + */ |
| 149 | + public void TestHasCommand() { |
| 150 | + // register the ControllerTestCommand to handle 'hasCommandTest' notes |
| 151 | + IController controller = Controller.Instance; |
| 152 | + string name = "HasCommandTest" + Thread.CurrentThread.Name; |
| 153 | + controller.RegisterCommand(name, typeof(ControllerTestCommand)); |
| 154 | + |
| 155 | + // test that hasCommand returns true for hasCommandTest notifications |
| 156 | + Assert.True(controller.HasCommand(name) == true, "Expecting controller.HasCommand(name) == true"); |
| 157 | + |
| 158 | + // Remove the Command from the Controller |
| 159 | + controller.RemoveCommand(name); |
| 160 | + |
| 161 | + // test that hasCommand returns false for hasCommandTest notifications |
| 162 | + Assert.True(controller.HasCommand(name) == false, "Expecting controller.HasCommand(name) == false"); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Tests Removing and Reregistering a Command |
| 167 | + * |
| 168 | + * <P> |
| 169 | + * Tests that when a Command is re-registered that it isn't fired twice. |
| 170 | + * This involves, minimally, registration with the controller but |
| 171 | + * notification via the View, rather than direct execution of |
| 172 | + * the Controller's executeCommand method as is done above in |
| 173 | + * testRegisterAndRemove. The bug under test was fixed in AS3 Standard |
| 174 | + * Version 2.0.2. If you run the unit tests with 2.0.1 this |
| 175 | + * test will fail.</P> |
| 176 | + */ |
| 177 | + public void TestReregisterAndExecuteCommand() { |
| 178 | + |
| 179 | + // Fetch the controller, register the ControllerTestCommand2 to handle 'ControllerTest2' notes |
| 180 | + IController controller = Controller.Instance; |
| 181 | + string name = "ControllerTest2" + Thread.CurrentThread.Name; |
| 182 | + controller.RegisterCommand(name, typeof(ControllerTestCommand2)); |
| 183 | + |
| 184 | + // Remove the Command from the Controller |
| 185 | + controller.RemoveCommand(name); |
| 186 | + |
| 187 | + // Re-register the Command with the Controller |
| 188 | + controller.RegisterCommand(name, typeof(ControllerTestCommand2)); |
| 189 | + |
| 190 | + // Create a 'ControllerTest2' note |
| 191 | + ControllerTestVO vo = new ControllerTestVO(12); |
| 192 | + Notification note = new Notification(name, vo); |
| 193 | + |
| 194 | + // retrieve a reference to the View. |
| 195 | + IView view = View.Instance; |
| 196 | + |
| 197 | + // send the Notification |
| 198 | + view.NotifyObservers(note); |
| 199 | + |
| 200 | + // test assertions |
| 201 | + // if the command is executed once the value will be 24 |
| 202 | + Assert.True(vo.result == 24, "Expecting vo.result == 24"); |
| 203 | + |
| 204 | + // Prove that accumulation works in the VO by sending the notification again |
| 205 | + view.NotifyObservers(note); |
| 206 | + |
| 207 | + // if the command is executed twice the value will be 48 |
| 208 | + Assert.True(vo.result == 48, "Expecting vo.result == 48"); |
| 209 | + } |
| 210 | + |
| 211 | + /// <summary> |
| 212 | + /// Test all of the function above using many threads at once. |
| 213 | + /// </summary> |
| 214 | + public void TestMultiThreadedOperations() |
| 215 | + { |
| 216 | + count = 20; |
| 217 | + IList<Thread> threads = new List<Thread>(); |
| 218 | + |
| 219 | + for (int i = 0; i < count; i++) { |
| 220 | + Thread t = new Thread(new ThreadStart(MultiThreadedTestFunction)); |
| 221 | + t.Name = "ControllerTest" + i; |
| 222 | + threads.Add(t); |
| 223 | + } |
| 224 | + |
| 225 | + foreach (Thread t in threads) |
| 226 | + { |
| 227 | + t.Start(); |
| 228 | + } |
| 229 | + |
| 230 | + while (true) |
| 231 | + { |
| 232 | + if (count <= 0) break; |
| 233 | + Thread.Sleep(100); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + private int count = 0; |
| 238 | + |
| 239 | + private int threadIterationCount = 10000; |
| 240 | + |
| 241 | + private void MultiThreadedTestFunction() |
| 242 | + { |
| 243 | + for (int i = 0; i < threadIterationCount; i++) |
| 244 | + { |
| 245 | + // All we need to do is test the registration and removal of commands. |
| 246 | + TestRegisterAndExecuteCommand(); |
| 247 | + TestRegisterAndRemoveCommand(); |
| 248 | + TestHasCommand(); |
| 249 | + TestReregisterAndExecuteCommand(); |
| 250 | + } |
| 251 | + |
| 252 | + count--; |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments