public void UpdateTest()
        {
            Logger logger = new Logger();
            var    list   = new ObservableList <string>();

            list.Attach(logger);
            string str1 = "string1",
                   str2 = "string2",
                   str3 = "string3";

            list.Add(str1);
            list.Add(str2);
            list.Insert(2, str3);

            list.Remove(str1);
            list.Detach(logger);
            list.Clear();

            foreach (string entry in logger.Log)
            {
                Trace.WriteLine(entry);
            }

            Assert.AreEqual(logger.Log.Count, 4);
        }
Exemple #2
0
        public void NotifyObservers_MultipleObserversDetachingOrAttachingOthers_NoUpdatesForAttachedAndDetachedObservers()
        {
            // Setup
            var mocks          = new MockRepository();
            var observableList = new ObservableList <double>();

            var observer1 = mocks.Stub <IObserver>();
            var observer2 = mocks.Stub <IObserver>();
            var observer3 = mocks.Stub <IObserver>();
            var observer4 = mocks.Stub <IObserver>();
            var observer5 = mocks.Stub <IObserver>();
            var observer6 = mocks.Stub <IObserver>();

            observableList.Attach(observer1);
            observableList.Attach(observer2);
            observableList.Attach(observer3);
            observableList.Attach(observer4);
            observableList.Attach(observer6);

            observer1.Expect(o => o.UpdateObserver());
            observer2.Expect(o => o.UpdateObserver()).Do((Action)(() => observableList.Detach(observer3)));
            observer3.Expect(o => o.UpdateObserver()).Repeat.Never(); // A detached observer should no longer be updated
            observer4.Expect(o => o.UpdateObserver()).Do((Action)(() => observableList.Attach(observer5)));
            observer5.Expect(o => o.UpdateObserver()).Repeat.Never(); // An attached observer should not be updated too
            observer6.Expect(o => o.UpdateObserver());

            mocks.ReplayAll();

            // Call
            observableList.NotifyObservers();

            // Assert
            mocks.VerifyAll();
        }
Exemple #3
0
        public void NotifyObserver_AttachedObserverDetachedAgain_ObserverNoLongerNotified()
        {
            // Setup
            var mocks    = new MockRepository();
            var observer = mocks.StrictMock <IObserver>();

            mocks.ReplayAll();

            var observableList = new ObservableList <object>();

            observableList.Attach(observer);
            observableList.Detach(observer);

            // Call
            observableList.NotifyObservers();

            // Assert
            mocks.VerifyAll(); // Expect no calls on 'observer'
        }