public void ReregisterAndExecuteCommand() { // Fetch the MainCommand, register the MainCommandTestCommand2 to handle 'MainCommandTest2' notes IMainCommand mainCommand = MainCommand.Instance; int name = int.MinValue + Thread.CurrentThread.ManagedThreadId; mainCommand.RegisterCommand(name, typeof(MainCommandTestCommand2)); // Remove the Command from the MainCommand mainCommand.RemoveCommand(name); // Re-register the Command with the MainCommand mainCommand.RegisterCommand(name, typeof(MainCommandTestCommand2)); // Create a 'MainCommandTest2' note MainCommandTestVO vo = new MainCommandTestVO(12); Notification note = new Notification(name, vo); // retrieve a reference to the MainController. IMainController mainController = MainController.Instance; // send the Notification mainController.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 mainController.NotifyObservers(note); // if the command is executed twice the value will be 48 Assert.IsTrue(vo.result == 48, "Expecting vo.result == 48"); }
public void RegisterAndRemoveCommand() { // Create the MainCommand, register the MainCommandTestCommand to handle 'MainCommandTest' notes IMainCommand mainCommand = MainCommand.Instance; int name = int.MinValue + Thread.CurrentThread.ManagedThreadId; mainCommand.RegisterCommand(name, typeof(MainCommandTestCommand)); // Create a 'MainCommandTest' note MainCommandTestVO vo = new MainCommandTestVO(12); INotification note = new Notification(name, vo); // Tell the MainCommand to execute the Command associated with the note // the MainCommandTestCommand invoked will multiply the vo.input value // by 2 and set the result on vo.result mainCommand.ExecuteCommand(note); // test assertions Assert.IsTrue(vo.result == 24, "Expecting vo.result == 24"); // Reset result vo.result = 0; // Remove the Command from the MainCommand mainCommand.RemoveCommand(name); // Tell the MainCommand to execute the Command associated with the // note. This time, it should not be registered, and our vo result // will not change mainCommand.ExecuteCommand(note); // test assertions Assert.IsTrue(vo.result == 0, "Expecting vo.result == 0"); }