public void TestRegisterAndRemoveCommand() { // Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes IController controller = Controller.GetInstance("ControllerTestKey3", () => new Controller("ControllerTestKey3")); controller.RegisterCommand("ControllerRemoveTest", () => new ControllerTestCommand()); // Create a 'ControllerTest' note ControllerTestVO vo = new ControllerTestVO(12); INotification note = new Notification("ControllerRemoveTest", vo); // Tell the controller to execute the Command associated with the note // the ControllerTestCommand invoked will multiply the vo.input value // by 2 and set the result on vo.result controller.ExecuteCommand(note); // test assertions Assert.AreEqual(24, vo.result, "Expecting vo.result == 24"); // Reset result vo.result = 0; // Remove the Command from the Controller controller.RemoveCommand("ControllerRemoveTest"); // Tell the controller to execute the Command associated with the // note. This time, it should not be registered, and our vo result // will not change controller.ExecuteCommand(note); // test assertions Assert.IsTrue(vo.result == 0, "Expecting vo.result == 0"); }
public void TestReRegisterAndExecuteCommand() { // Fetch the controller, register the ControllerTestCommand2 to handle 'ControllerTest2' notes IController controller = Controller.GetInstance("ControllerTestKey5", () => new Controller("ControllerTestKey5")); controller.RegisterCommand("ControllerTest2", () => new ControllerTestCommand2()); // Remove the Command from the Controller controller.RemoveCommand("ContrllerTest2"); // Re-register the Command with the Controller controller.RegisterCommand("ControllerTest2", () => new ControllerTestCommand2()); // Create a 'ControllerTest2' note ControllerTestVO vo = new ControllerTestVO(12); INotification note = new Notification("ControllerTest2", vo); // retrieve a reference to the View from the same core. IView view = View.GetInstance("ControllerTestKey5", () => new View("ControllerTestKey5")); // send the Notification view.NotifyObservers(note); // test assertions // if the command is executed once the value will be 24 Assert.IsTrue(vo.result == 24, "Expecting vo.result == 24"); // Prove that accumulation works in the VO by sending the notification again view.NotifyObservers(note); // if the command is executed twice the value will be 48 Assert.IsTrue(vo.result == 48, "Expecting vo.result == 48"); }
/// <summary> /// Fabricate a result by multiplying the input by 2 and adding to the existing result /// <para> /// This tests accumulation effect that would show if the command were executed more than once. /// </para> /// </summary> /// <param name="notification">the note carrying the ControllerTestVO</param> public override void Execute(INotification notification) { ControllerTestVO vo = (ControllerTestVO)notification.Body; // Fabricate a result vo.result = vo.result + (2 * vo.input); }
public void TestRegisterAndExecuteCommand() { // Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes IController controller = Controller.GetInstance("ControllerTestKey2", () => new Controller("ControllerTestKey2")); controller.RegisterCommand("ControllerTest", () => new ControllerTestCommand()); // Create a 'ControllerTest' notification var vo = new ControllerTestVO(12); INotification note = new Notification("ControllerTest", vo); // Tell the controller to execute the Command associated with the notification // the ControllerTestCommand invoked will multiply the vo.input value // by 2 and set the result on vo.result controller.ExecuteCommand(note); // test assertions Assert.IsTrue(vo.result == 24, "Expecting vo.result == 24"); }