Example #1
0
        public void RegisteringStateObserverDoesNotChangeState()
        {
            var observer = new ObserverThatObservesFooBar();

            m_CommandDispatcher.RegisterObserver(observer);
            Assert.That(GetState().FooBarStateComponent.Foo, Is.EqualTo(k_StateDefaultValue));
        }
Example #2
0
        public void StateObserverIsNotifiedWhenObservedStateIsModifiedByOtherObserver()
        {
            var observer1 = new ObserverThatObservesFooBar();
            var observer2 = new ObserverThatObservesFewBawAndModifiesFooBar();

            m_CommandDispatcher.RegisterObserver(observer1);
            m_CommandDispatcher.RegisterObserver(observer2);
            m_CommandDispatcher.RegisterCommandHandler <TestGraphToolState, ChangeFewCommand>(ChangeFewCommand.DefaultHandler);

            var testState = m_CommandDispatcher.State as TestGraphToolState;

            Assert.IsNotNull(testState);

            var internalObserver       = (IInternalStateObserver)observer1;
            var initialObserverVersion = internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent));

            m_CommandDispatcher.Dispatch(new ChangeFewCommand(10));
            var beforeNotification = testState.FooBarStateComponent.CurrentVersion;

            m_CommandDispatcher.NotifyObservers();
            var afterNotification = testState.FooBarStateComponent.CurrentVersion;

            // Observer version has changed since initial observation.
            Assert.AreNotEqual(initialObserverVersion, internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent)));

            // Observer version has changed after notifying observers.
            Assert.AreNotEqual(beforeNotification, internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent)));

            // and is equal to current state version.
            Assert.AreEqual(afterNotification, internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent)).Version);
        }
Example #3
0
        public void StateObserverIsNotifiedWhenObservedStateIsModified()
        {
            var observer = new ObserverThatObservesFooBar();

            m_CommandDispatcher.RegisterObserver(observer);
            m_CommandDispatcher.RegisterCommandHandler <TestGraphToolState, ChangeFooCommand>(ChangeFooCommand.DefaultHandler);

            var testState = m_CommandDispatcher.State as TestGraphToolState;

            Assert.IsNotNull(testState);
            Assert.IsTrue(observer.ObservedStateComponents.Contains(nameof(TestGraphToolState.FooBarStateComponent)));

            var internalObserver       = (IInternalStateObserver)observer;
            var initialObserverVersion = internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent));

            m_CommandDispatcher.Dispatch(new ChangeFooCommand(10));
            var currentStateVersion = testState.FooBarStateComponent.CurrentVersion;

            m_CommandDispatcher.NotifyObservers();

            // Observer version has changed
            Assert.AreNotEqual(initialObserverVersion, internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent)));
            // and is equal to current state version.
            Assert.AreEqual(currentStateVersion, internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent)).Version);
        }
Example #4
0
        public void RegisteringStateObserverTwiceThrows()
        {
            var observer = new ObserverThatObservesFooBar();

            m_CommandDispatcher.RegisterObserver(observer);

            Assert.Throws <InvalidOperationException>(() => m_CommandDispatcher.RegisterObserver(observer));
        }
Example #5
0
        public void UnregisteringStateObserverTwiceDoesNotThrow()
        {
            var observer = new ObserverThatObservesFooBar();

            m_CommandDispatcher.RegisterObserver(observer);

            m_CommandDispatcher.UnregisterObserver(observer);
            m_CommandDispatcher.UnregisterObserver(observer);
            LogAssert.NoUnexpectedReceived();
        }
Example #6
0
        public void StateObserverIsNotNotifiedAfterUnregistering()
        {
            var observer = new ObserverThatObservesFooBar();

            m_CommandDispatcher.RegisterObserver(observer);
            m_CommandDispatcher.RegisterCommandHandler <TestGraphToolState, ChangeFooCommand>(ChangeFooCommand.DefaultHandler);

            var testState = m_CommandDispatcher.State as TestGraphToolState;

            Assert.IsNotNull(testState);
            Assert.IsTrue(observer.ObservedStateComponents.Contains(nameof(TestGraphToolState.FooBarStateComponent)));

            var internalObserver       = (IInternalStateObserver)observer;
            var initialObserverVersion = internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent));

            m_CommandDispatcher.Dispatch(new ChangeFooCommand(10));

            m_CommandDispatcher.UnregisterObserver(observer);
            m_CommandDispatcher.NotifyObservers();

            // Observer version did not change
            Assert.AreEqual(initialObserverVersion, internalObserver.GetLastObservedComponentVersion(nameof(TestGraphToolState.FooBarStateComponent)));
        }