Beispiel #1
0
 public void TestCircularSubscriptions()
 {
     int timesNotified1 = 0;
     int timesNotified2 = 0;
     var property1 = new ObservableProperty<string>("initVal1");
     var property2 = new ObservableProperty<string>("initVal2");
     property1.Subscribe(value =>
     {
         timesNotified1++;
         property2.Value = value;
     },
         property2);
     property2.Subscribe(value =>
     {
         timesNotified2++;
         property1.Value = value;
     },
         property1);
     Assert.AreEqual(0, timesNotified1);
     Assert.AreEqual(0, timesNotified2);
     Assert.AreEqual("initVal1", property1.Value);
     Assert.AreEqual("initVal2", property2.Value);
     property1.Value = "don't loop!";
     Assert.AreEqual("don't loop!", property1.Value);
     Assert.AreEqual("don't loop!", property2.Value);
     Assert.AreEqual(1, timesNotified1);
     Assert.AreEqual(0, timesNotified2);
 }
Beispiel #2
0
 public void TestDontDependWithPeek()
 {
     var property = new ObservableProperty<int>(5);
     var computed = new ComputedObservable<int>(() => property.Peek() + 1);
     Assert.AreEqual(6, computed.Value);
     property.Value = 10;
     Assert.AreEqual(6, computed.Value);
 }
Beispiel #3
0
 public void TestCustomEventIgnoreDefaultNotify()
 {
     var property = new ObservableProperty<string>("initVal");
     string notifiedValue = null;
     var subscription = property.Subscribe(value => notifiedValue = value, "test", "myEvent");
     property.NotifySubscribers("ignore");
     Assert.IsNull(notifiedValue);
 }
Beispiel #4
0
 public void TestNotifyDefaultValueChange()
 {
     var property = new ObservableProperty<string>();
     string notifiedValue = null;
     property.Subscribe(value => notifiedValue = value, "test");
     property.Value = "test";
     Assert.AreEqual("test", notifiedValue);
 }
Beispiel #5
0
 public void TestNotifySubscribers()
 {
     var property = new ObservableProperty<string>();
     string notifiedValue = null;
     property.Subscribe(value => notifiedValue = value, "test");
     property.NotifySubscribers("test");
     Assert.AreEqual("test", notifiedValue);
 }
Beispiel #6
0
 public void TestNotifyCustomEvent()
 {
     var property = new ObservableProperty<string>("initVal");
     string notifiedValue = null;
     var subscription = property.SubscribeEvent<string>(value => notifiedValue = value, "myEvent", "test");
     property.OnNotifySubscribers("bla", "undefinedEvent");
     Assert.IsNull(notifiedValue);
     property.OnNotifySubscribers("expected", "myEvent");
     Assert.AreEqual("expected", notifiedValue);
 }
 public SelectedListViewModel()
 {
     Friends = Observe.List(new Friend("jo"),
         new Friend("Jason"),
         new Friend("Rachael"),
         new Friend("Nathan"),
         new Friend("Bob"));
     SelectedFriend = new ObservableProperty<Friend>();
     SelectedFriendName =
         new ComputedObservable<string>(
             () => SelectedFriend.Value == null ? string.Empty : SelectedFriend.Value.Name.Value,
             value => SelectedFriend.Value.Name.Value = value);
 }
Beispiel #8
0
 public void View(ObservableProperty<Guest> selectedGuest)
 {
     if (_guest != null) throw new InvalidOperationException("guest already setup");
     _guest = selectedGuest;
     _name = Observe.TwoWayComputed(() => _guest.Value.Name);
     _attending = Observe.TwoWayComputed(() => _guest.Value.Attending);
     _invited = Observe.TwoWayComputed(() => _guest.Value.InvitedDate);
     _foodChoice = Observe.TwoWayComputed(() => _guest.Value.FoodChoice);
     _name.BindText(nameTextBox);
     _attending.BindCheckBox(attendingCheckBox);
     _invited.BindDate(invitedDateCalendar);
     _foodChoice.BindSelectedComboBox(foodTypeComboBox);
 }
Beispiel #9
0
 public void TestEvaluateAfterDisposed()
 {
     int timesEvaluated = 0;
     var underlyingProperty = new ObservableProperty<int>(1);
     var computed = new ComputedObservable<int>(() =>
     {
         timesEvaluated++;
         return underlyingProperty.Value;
     });
     Assert.AreEqual(1, timesEvaluated);
     computed.Dispose();
     underlyingProperty.Value = 2;
     Assert.AreEqual(1, timesEvaluated);
     Assert.AreEqual(1, computed.Value);
     Assert.AreEqual(0, computed.DependencyCount);
 }
Beispiel #10
0
        public void TestComplexTwoWayBinding()
        {
            var kevin = new TestModel("Kevin") ;
            var jacob = new TestModel("Jacob") {TestEnumValue = { Value = TestEnum.three}};
            var observableObject = new ObservableProperty<TestModel>(kevin);

            var testEnumObserver = Observe.TwoWayComputed(() => observableObject.Value.TestEnumValue);
            Assert.AreEqual(TestEnum.one, testEnumObserver.Value);
            testEnumObserver.Value = TestEnum.two;
            Assert.AreEqual(TestEnum.two, kevin.TestEnumValue.Value);
            observableObject.Value = jacob;
            Assert.AreEqual(TestEnum.three, testEnumObserver.Value);
            testEnumObserver.Value = TestEnum.one;

            observableObject.Value = kevin;
            Assert.AreEqual(TestEnum.two, testEnumObserver.Value);
        }
 public void TestComputeSubscriptionOnListOfObservables()
 {
     int timesComputed = 0;
     var list = ObservableList.New(5, 6, 7);
     list.Name = "List";
     var computed = new ComputedObservable<int>(() =>
     {
         timesComputed++;
         return list[2].Value + 1;
     }) {Name = "ComputedVal"};
     Assert.AreEqual(8, computed.Value);
     Assert.AreEqual(1, timesComputed);
     list[0].Value = 1;
     Assert.AreEqual(1, timesComputed);
     list[2].Value = 3;
     Assert.AreEqual(4, computed.Value);
     Assert.AreEqual(2, timesComputed);
     list[1] = new ObservableProperty<int>(10);
     Assert.AreEqual(2, timesComputed);
     list[2] = new ObservableProperty<int>(10);
     Assert.AreEqual(3, timesComputed);
     Assert.AreEqual(11, computed.Value);
 }
Beispiel #12
0
 public void TestLongChains()
 {
     try
     {
         int depth = 1000;
         var first = new ObservableProperty<int>(0);
         var last = first;
         for (int i = 0; i < depth; i++)
         {
             var previous = last;
             last = new ComputedObservable<int>(() => previous.Value + 1);
         }
         var all = new ComputedObservable<int>(() => first.Value + last.Value);
         first.Value = 1;
         Assert.AreEqual(depth + 2, all.Value);
     }
     catch (StackOverflowException)
     {
         Assert.Fail("Stack overflow");
     }
 }
Beispiel #13
0
 public void TestNotifyBeforeChange()
 {
     string notifiedValue = null;
     var property = new ObservableProperty<string>("test");
     var computed = new ComputedObservable<string>(() => property + "_computed");
     computed.Subscribe(value => notifiedValue = value, ObservableProperty.BEFORE_VALUE_CHANGED_EVENT, "test");
     Assert.IsNull(notifiedValue);
     property.Value = "is";
     Assert.AreEqual("test_computed", notifiedValue);
     Assert.AreEqual("is_computed", computed.Value);
 }
Beispiel #14
0
 public void TestNotAddingDependencyWhenDisposedDuringEvaluation()
 {
     int timesEvaluated = 0;
     var underlyingProperty = new ObservableProperty<int>(1);
     var propertyToTrigerDipose = new ObservableProperty<bool>(false);
     ComputedObservable<int> computed = null;
     computed = new ComputedObservable<int>(() =>
     {
         if (propertyToTrigerDipose.Value)
         {
             computed.Dispose();
         }
         timesEvaluated++;
         return underlyingProperty.Value;
     });
     Assert.AreEqual(1, timesEvaluated);
     Assert.AreEqual(1, computed.Value);
     Assert.AreEqual(2, computed.DependencyCount);
     Assert.AreEqual(1, underlyingProperty.SubscriptionsCount);
     propertyToTrigerDipose.Value = true;
     Assert.AreEqual(2, timesEvaluated);
     Assert.AreEqual(1, computed.Value);
     Assert.AreEqual(0, computed.DependencyCount);
     Assert.AreEqual(0, underlyingProperty.SubscriptionsCount);
 }
Beispiel #15
0
 public void TestObservableChain()
 {
     var underlyingProperty = new ObservableProperty<int>(1);
     var computed1 = new ComputedObservable<int>(() => 1 + underlyingProperty);
     var computed2 = new ComputedObservable<int>(() => 1 + computed1);
     Assert.AreEqual(3, computed2.Value);
     underlyingProperty.Value = 11;
     Assert.AreEqual(13, computed2.Value);
 }
Beispiel #16
0
 public void TestNotifySubscribers()
 {
     string notifiedValue = null;
     var property = new ObservableProperty<string>("test");
     var computed = new ComputedObservable<string>(() => property + "_computed");
     computed.Subscribe(value => notifiedValue = value, "test");
     Assert.IsNull(notifiedValue);
     property.Value = "is";
     Assert.AreEqual("is_computed", computed.Value);
 }
Beispiel #17
0
 public void TestSubscribeInvokesEvalOnDefer()
 {
     var underlyingProperty = new ObservableProperty<int>(1);
     var computed = new ComputedObservable<int>(() => underlyingProperty.Value, true);
     var result = new ObservableProperty<int>();
     Assert.AreEqual(0, computed.DependencyCount);
     computed.Subscribe(value => result.Value = value, "test");
     Assert.AreEqual(1, computed.DependencyCount);
     Assert.AreEqual(0, result.Value);
     underlyingProperty.Value = 42;
     Assert.AreEqual(42, result.Value);
 }
Beispiel #18
0
 public void TestUnsubscribeCustomEvent()
 {
     var property = new ObservableProperty<string>("initVal");
     string notifiedValue = null;
     var subscription = property.Subscribe(value => notifiedValue = value, "test", "myEvent");
     subscription.Dispose();
     property.OnNotifySubscribers("ignore", "myEvent");
     Assert.IsNull(notifiedValue);
 }
Beispiel #19
0
 public void TestPreventRecursionOnSetDependency()
 {
     int timesEvaluated = 0;
     var underlyingProperty = new ObservableProperty<int>(1);
     var computed = new ComputedObservable<int>(() =>
     {
         timesEvaluated++;
         underlyingProperty.Value = (underlyingProperty.Value + 1);
         return 1;
     });
     Assert.AreEqual(1, timesEvaluated);
 }
Beispiel #20
0
 public void TestPeekComputed()
 {
     var underlyingProperty = new ObservableProperty<int>(1);
     var computed1 = new ComputedObservable<int>(() => 1 + underlyingProperty);
     var computed2 = new ComputedObservable<int>(() => 1 + computed1.Peek());
     Assert.AreEqual(3, computed2.Value);
     Assert.AreEqual(0, computed2.DependencyCount);
     underlyingProperty.Value = 11;
     Assert.AreEqual(3, computed2.Value);
 }
Beispiel #21
0
        public void TestUnsubscribeOnEachChange()
        {
            var propertyA = new ObservableProperty<string>("A");
            var propertyB = new ObservableProperty<string>("B");
            string propertyToUse = "A";
            int timesEvaluated = 0;
            var computed = new ComputedObservable<string>(() =>
            {
                timesEvaluated++;
                return propertyToUse == "A" ? propertyA.Value : propertyB.Value;
            });
            Assert.AreEqual("A", computed.Value);
            Assert.AreEqual(1, timesEvaluated);

            // Changing an unrelated observable doesn't trigger evaluation
            propertyB.Value = "B2";
            Assert.AreEqual(1, timesEvaluated);

            // Switch to other observable
            propertyToUse = "B";
            propertyA.Value = "A2";
            Assert.AreEqual("B2", computed.Value);
            Assert.AreEqual(2, timesEvaluated);

            // Now changing the first observable doesn't trigger evaluation
            Assert.AreEqual(2, timesEvaluated);
            propertyA.Value = "A3";
        }
Beispiel #22
0
 public void TestUpdateWhenDependencyChanges()
 {
     var property = new ObservableProperty<int>(5);
     var computed = new ComputedObservable<int>(() => property.Value + 1);
     Assert.AreEqual(6, computed.Value);
     property.Value = 10;
     Assert.AreEqual(11, computed.Value);
 }
Beispiel #23
0
 public void TestTwoWayComputed()
 {
     var testEnumProperty = new ObservableProperty<TestEnum>();
     var computed = Observe.TwoWayComputed(() => testEnumProperty);
     TestEnum lastNotifyValue = TestEnum.two;
     computed.Subscribe(value => lastNotifyValue = value, "test");
     Assert.AreEqual(TestEnum.one, computed.Value);
     testEnumProperty.Value = TestEnum.two;
     Assert.AreEqual(TestEnum.two, computed.Value);
     Assert.AreEqual(TestEnum.two, lastNotifyValue);
     testEnumProperty.Value = TestEnum.one;
     Assert.AreEqual(TestEnum.one, computed.Value);
     Assert.AreEqual(TestEnum.one, lastNotifyValue);
 }
Beispiel #24
0
 public void TestPreventSubscribeViewChangeNotification()
 {
     var underlyingProperty = new ObservableProperty<int>(1);
     var independentProperty = new ObservableProperty<int>(1);
     var computed = new ComputedObservable<int>(() => underlyingProperty.Value);
     Assert.AreEqual(1, computed.DependencyCount);
     computed.Subscribe(value => { var tmp = independentProperty.Value; }, "test");
     underlyingProperty.Value = 2;
     Assert.AreEqual(1, computed.DependencyCount);
     computed.Subscribe(value => { var tmp = independentProperty.Value; },
         ObservableProperty.BEFORE_VALUE_CHANGED_EVENT,
         "test");
     underlyingProperty.Value = 3;
     Assert.AreEqual(1, computed.DependencyCount);
 }
Beispiel #25
0
 public void TestSingleUpdateOnChain()
 {
     int timesEvaluated = 0;
     var underlyingPropertyLeft = new ObservableProperty<int>(1) {Name = "Left"};
     var underlyingPropertyRight = new ObservableProperty<int>(1) {Name = "Right"};
     var computed1 = new ComputedObservable<int>(() => underlyingPropertyRight + underlyingPropertyLeft)
     {
         Name = "Compute1"
     };
     var computed2 = new ComputedObservable<int>(() =>
     {
         timesEvaluated++;
         return underlyingPropertyLeft + computed1 + underlyingPropertyRight;
     })
     {
         Name = "Compute2"
     };
     Assert.AreEqual(1, timesEvaluated);
     Assert.AreEqual(4, computed2);
     underlyingPropertyLeft.Value = 2;
     Assert.AreEqual(6, computed2);
     Assert.AreEqual(2, timesEvaluated);
     underlyingPropertyRight.Value = 2;
     Assert.AreEqual(8, computed2);
     Assert.AreEqual(3, timesEvaluated);
 }
Beispiel #26
0
 public void TestPreventNotifyAfterUnsubscribe()
 {
     var property = new ObservableProperty<string>();
     ObservableSubscription<string> subscription1 = null;
     ObservableSubscription<string> subscription2 = null;
     subscription1 = property.Subscribe(value => subscription2.Dispose(), "test");
     bool subscription2WasNotified = false;
     subscription2 = property.Subscribe(value => subscription2WasNotified = true, "test");
     property.NotifySubscribers("ignore");
     Assert.AreEqual(false, subscription2WasNotified);
 }
Beispiel #27
0
 public void TestUnsubscribe()
 {
     var property = new ObservableProperty<string>("initVal");
     string notifiedValue = null;
     var subscription = property.Subscribe(value => notifiedValue = value, "test");
     subscription.Dispose();
     property.NotifySubscribers("test");
     Assert.AreEqual(null, notifiedValue);
 }
Beispiel #28
0
 public void TestSubscriptionCounts()
 {
     var underlyingPropertyLeft = new ObservableProperty<int>(1) {Name = "Left"};
     var underlyingPropertyRight = new ObservableProperty<int>(1) {Name = "Right"};
     var simpleComputed = new ComputedObservable<int>(() => underlyingPropertyLeft + 5, true);
     var layerdComputed = new ComputedObservable<int>(() => simpleComputed + underlyingPropertyRight);
     Assert.AreEqual(simpleComputed.DependencyCount, 1);
     Assert.AreEqual(layerdComputed.DependencyCount, 2);
 }
Beispiel #29
0
 public void TestSingleUpdateOnMultipleCalls()
 {
     var property = new ObservableProperty<int>(2);
     var computed = new ComputedObservable<int>(() => property + property);
     Assert.AreEqual(1, computed.DependencyCount);
     List<int> notifiedValues = new List<int>();
     computed.Subscribe(value => notifiedValues.Add(value), "test");
     Assert.AreEqual(4, computed.Value);
     property.Value = 4;
     Assert.AreEqual(1, notifiedValues.Count);
     Assert.AreEqual(8, notifiedValues[0]);
 }