示例#1
0
        public void ViewModel_PropertyChanged()
        {
            /* First, create an instance of the viewmodel */
            DummyViewModel testSubject = new DummyViewModel();

            /* Set some properties and fixate the test subject */
            testSubject.BooleanValue = true;
            testSubject.IntegerValue = 42;
            testSubject.Fixate();

            /* Register handlers */
            testSubject.PropertyChanged += this.ReceivePropertyChangedEvent;
            testSubject.Messenger.Register <PropertyChangedMessage <bool> >(this, this.ReceivePropertyChangedMessage);
            testSubject.Messenger.Register <PropertyChangedMessage <int> >(this, this.ReceivePropertyChangedMessage);

            /* Change the first property */
            testSubject.BooleanValue = false;

            /* Allow for the messenger to process the send-request and the registered action to complete */
            int roundtrips = 0;

            while (roundtrips < 5 && !testSubject.IsHandledByEvent && !testSubject.IsHandledByMessage)
            {
                ++roundtrips;
                Thread.Yield();
            }

            /* Check if any events or messages where received */
            Assert.IsTrue(testSubject.IsHandledByEvent);
            Assert.IsTrue(testSubject.IsHandledByMessage);

            /* Reset the handled-flags */
            testSubject.IsHandledByEvent   = false;
            testSubject.IsHandledByMessage = false;

            /* Change the second property */
            testSubject.IntegerValue = 88;

            /* Allow for the messenger to process the send-request and the registered action to complete */
            roundtrips = 0;
            while (roundtrips < 5 && !testSubject.IsHandledByEvent && !testSubject.IsHandledByMessage)
            {
                ++roundtrips;
                Thread.Yield();
            }

            /* Check if any events or messages where received */
            Assert.IsTrue(testSubject.IsHandledByEvent);
            Assert.IsFalse(testSubject.IsHandledByMessage);
        }