Exemplo n.º 1
0
        public void CompareNotifyContext()
        {
            // Create observer passing in notification method and context
            IObserver observer = new Observer(this.observerTestMethod, this);

              			object negTestObj = new object();

            // test assertions
               			Assert.IsTrue(observer.CompareNotifyContext(negTestObj) == false, "Expecting observer.compareNotifyContext(negTestObj) == false");
            Assert.IsTrue(observer.CompareNotifyContext(this), "Expecting observer.compareNotifyContext(this) == true");
        }
Exemplo n.º 2
0
        public void ObserverAccessors()
        {
            // Create observer with null args, then
               			// use accessors to set notification method and context
            IObserver observer = new Observer(null, null);
            observer.NotifyContext = this;
               			observer.NotifyMethod = this.observerTestMethod;

               			// create a test event, setting a payload value and notify
               			// the observer with it. since the observer is this class
               			// and the notification method is observerTestMethod,
               			// successful notification will result in our local
               			// observerTestVar being set to the value we pass in
               			// on the note body.
               			INotification note = new Notification(1, 10);
            observer.NotifyObserver(note);

            // test assertions
               			Assert.IsTrue(observerTestVar == 10, "Expecting observerTestVar = 10");
        }
Exemplo n.º 3
0
        public virtual void RegisterController([NotNull] IController controller)
        {
            lock (this.m_syncRoot)
            {
                // do not allow re-registration (you must to removeController fist)
                if (this.m_ControllerMap.ContainsKey(controller.Name)) return;

                // Register the Controller for retrieval by name
                this.m_ControllerMap[controller.Name] = controller;

                // Get Notification interests, if any.
                IList<int> interests = controller.ListNotificationInterests();

                // Register Controller as an observer for each of its notification interests
                if (interests.Count > 0)
                {
                    // Create Observer
                    IObserver observer = new Observer(controller.HandleNotification, controller);

                    // Register Controller as Observer for its list of Notification interests
                    foreach (int t in interests)
                    {
                        this.RegisterObserver(t, observer);
                    }
                }
            }

            // alert the Controller that it has been registered
            controller.OnRegister();
        }
Exemplo n.º 4
0
        public void RegisterAndNotifyObserver()
        {
            // Get the Singleton MainController instance
              			IMainController mainController = MainController.Instance;

               			// Create observer, passing in notification method and context
               			IObserver observer = new Observer(this.MainControllerTestMethod, this);

               			// Register Observer's interest in a particulat Notification with the MainController
            int name = Thread.CurrentThread.ManagedThreadId;

            lock (m_MainControllerTestVarsLock)
            {
                this.mainControllerTestVars.Remove(name);
            }

            mainController.RegisterObserver(MainControllerTestNote.NAME + name, observer);

               			// Create a MainControllerTestNote, setting
               			// a body value, and tell the MainController to notify
               			// Observers. Since the Observer is this class
               			// and the notification method is MainControllerTestMethod,
               			// successful notification will result in our local
               			// MainControllerTestVar being set to the value we pass in
               			// on the note body.
            INotification note = MainControllerTestNote.Create(name, 10);
            mainController.NotifyObservers(note);

            // test assertions
            Assert.IsTrue(this.mainControllerTestVars.ContainsKey(name), "Expecting MainControllerTestVars.ContainsKey(name)");
            Assert.IsTrue(this.mainControllerTestVars[name] == 10, "Expecting MainControllerTestVar[name] = 10");

            mainController.RemoveObserver(MainControllerTestNote.NAME + name, this);
        }
Exemplo n.º 5
0
        public void ObserverConstructor()
        {
            // Create observer passing in notification method and context
            IObserver observer = new Observer(this.observerTestMethod, this);

               			// create a test note, setting a body value and notify
               			// the observer with it. since the observer is this class
               			// and the notification method is observerTestMethod,
               			// successful notification will result in our local
               			// observerTestVar being set to the value we pass in
               			// on the note body.
               			INotification note = new Notification(1, 5);
            observer.NotifyObserver(note);

            // test assertions
               			Assert.IsTrue(observerTestVar == 5, "Expecting observerTestVar = 5");
        }