Inheritance: ReactiveObject
        public void ChangingShouldAlwaysArriveBeforeChanged()
        {
            string before_set = "Foo";
            string after_set = "Bar";

            #if IOS
            Assert.Fail("This crashes Mono in a quite spectacular way");
            #endif

            var fixture = new TestFixture() { IsOnlyOneWord = before_set };

            bool before_fired = false;
            fixture.Changing.Subscribe(x => {
                // XXX: The content of these asserts don't actually get
                // propagated back, it only prevents before_fired from
                // being set - we have to enable 1st-chance exceptions
                // to see the real error
                Assert.Equal("IsOnlyOneWord", x.PropertyName);
                Assert.Equal(fixture.IsOnlyOneWord, before_set);
                before_fired = true;
            });

            bool after_fired = false;
            fixture.Changed.Subscribe(x => {
                Assert.Equal("IsOnlyOneWord", x.PropertyName);
                Assert.Equal(fixture.IsOnlyOneWord, after_set);
                after_fired = true;
            });

            fixture.IsOnlyOneWord = after_set;

            Assert.True(before_fired);
            Assert.True(after_fired);
        }
Esempio n. 2
0
        public void AutoPersistHelperSavesOnInterval()
        {
            (new TestScheduler()).With(sched => {
                var fixture = new TestFixture();
                var manualSave = new Subject<Unit>();

                int timesSaved = 0;
                fixture.AutoPersist(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));

                // No changes = no saving
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(0, timesSaved);

                // Change = one save
                fixture.IsNotNullString = "Foo";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Two fast changes = one save
                fixture.IsNotNullString = "Foo";
                fixture.IsNotNullString = "Bar";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(2, timesSaved);

                // Trigger save twice = one save
                manualSave.OnNext(Unit.Default);
                manualSave.OnNext(Unit.Default);
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(3, timesSaved);
            });
        }
        public void MakeObjectReactiveHelperSmokeTest()
        {
            var fixture = new NonReactiveINPCObjectMadeReactive();
            var output = fixture.Changed.CreateCollection();

            Assert.Equal(0, output.Count);

            var input = new TestFixture();
            fixture.InpcProperty = input;

            Assert.Equal(1, output.Count);
            Assert.Equal(fixture, output[0].Sender);
            Assert.Equal("InpcProperty", output[0].PropertyName);
        }
Esempio n. 4
0
        public void DataContextDoesNotLeakTest()
        {
            ListBox listBox = new ListBox();
            TestFixture vm = new TestFixture();
            listBox.DataContext = vm;

            WeakReference listBoxRef = new WeakReference(listBox);
            GC.Collect();

            Assert.True(listBoxRef.IsAlive);

            //remove ref
            listBox = null;
            GC.Collect();

            Assert.False(listBoxRef.IsAlive);
        }
        public void RaiseAndSetUsingExpression()
        {
            #if IOS
            Assert.Fail("This crashes Mono in a quite spectacular way");
            #endif

            var fixture = new TestFixture() { IsNotNullString = "Foo", IsOnlyOneWord = "Baz" };
            var output = new List<string>();
            fixture.Changed.Subscribe(x => output.Add(x.PropertyName));

            fixture.UsesExprRaiseSet = "Foo";
            fixture.UsesExprRaiseSet = "Foo";   // This one shouldn't raise a change notification

            Assert.Equal("Foo", fixture.UsesExprRaiseSet);
            Assert.Equal(1, output.Count);
            Assert.Equal("UsesExprRaiseSet", output[0]);
        }
Esempio n. 6
0
        public void AutoPersistHelperShouldntTriggerOnNonPersistableProperties()
        {
            (new TestScheduler()).With(sched => {
                var fixture = new TestFixture();
                var manualSave = new Subject<Unit>();

                int timesSaved = 0;
                fixture.AutoPersist(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));

                // No changes = no saving
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(0, timesSaved);

                // Change to not serialized = no saving
                fixture.NotSerialized = "Foo";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(0, timesSaved);
            });
        }
        public void BindToStackOverFlowTest()
        {
            // Before the code changes packed in the same commit
            // as this test the test would go into an infinite
            // event storm. The critical issue is that the
            // property StackOverflowTrigger will clone the
            // value before setting it.
            //
            // If this test executes through without hanging then
            // the problem has been fixed.
            (new TestScheduler()).With(sched => {
                var fixturea = new TestFixture();
                var fixtureb = new TestFixture();

                var source = new BehaviorSubject<List<string>>(new List<string>());

                source.BindTo(fixturea, x => x.StackOverflowTrigger);
            });
        }
        public void GetValueShouldActuallyReturnTheValue()
        {
            var input = new[] {"Foo", "Bar", "Baz"};
            var output = new List<string>();

            (new TestScheduler()).With(sched => {
                var fixture = new TestFixture();

                // ...whereas ObservableForProperty *is* guaranteed to.
                fixture.ObservableForProperty(x => x.IsOnlyOneWord).Subscribe(x => {
                    output.Add(x.GetValue());
                });

                foreach (var v in input) { fixture.IsOnlyOneWord = v; }

                sched.AdvanceToMs(1000);

                input.AssertAreEqual(output);
            });
        }
Esempio n. 9
0
        public void ReactiveObjectSmokeTest()
        {
            var output_changing = new List<string>();
            var output = new List<string>();
            var fixture = new TestFixture();

            fixture.Changing.Subscribe(x => output_changing.Add(x.PropertyName));
            fixture.Changed.Subscribe(x => output.Add(x.PropertyName));

            fixture.IsNotNullString = "Foo Bar Baz";
            fixture.IsOnlyOneWord = "Foo";
            fixture.IsOnlyOneWord = "Bar";
            fixture.IsNotNullString = null;     // Sorry.
            fixture.IsNotNullString = null;

            var results = new[] { "IsNotNullString", "IsOnlyOneWord", "IsOnlyOneWord", "IsNotNullString" };

            Assert.Equal(results.Length, output.Count);

            output.AssertAreEqual(output_changing);
            results.AssertAreEqual(output);
        }
        public void ObservableForPropertyUsingExpression()
        {
            var fixture = new TestFixture() { IsNotNullString = "Foo", IsOnlyOneWord = "Baz" };
            var output = new List<IObservedChange<TestFixture, string>>();
            fixture.ObservableForProperty(x => x.IsNotNullString).Subscribe(x => {
                output.Add(x);
            });

            fixture.IsNotNullString = "Bar";
            fixture.IsNotNullString = "Baz";
            fixture.IsNotNullString = "Baz";

            fixture.IsOnlyOneWord = "Bamf";

            Assert.Equal(2, output.Count);

            Assert.Equal(fixture, output[0].Sender);
            Assert.Equal("IsNotNullString", output[0].PropertyName);
            Assert.Equal("Bar", output[0].Value);

            Assert.Equal(fixture, output[1].Sender);
            Assert.Equal("IsNotNullString", output[1].PropertyName);
            Assert.Equal("Baz", output[1].Value);
        }
        public void AutoPersistCollectionDisconnectsOnDispose()
        {
            new TestScheduler().With(sched =>
            {
                var manualSave = new Subject <Unit>();

                var item    = new TestFixture();
                var fixture = new ObservableCollectionExtended <TestFixture> {
                    item
                };

                var timesSaved = 0;
                var disp       = fixture.AutoPersistCollection(
                    _ =>
                {
                    timesSaved++;
                    return(Observables.Unit);
                },
                    manualSave,
                    TimeSpan.FromMilliseconds(100));

                sched.AdvanceByMs(2 * 100);
                Assert.Equal(0, timesSaved);

                // By being added to collection, AutoPersist is enabled for item
                item.IsNotNullString = "Foo";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Dispose = no save
                disp.Dispose();

                // Removed from collection = no save
                fixture.Clear();
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Item isn't in the collection, it doesn't get persisted anymore
                item.IsNotNullString = "Bar";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Added back item + dispose = no save
                fixture.Add(item);
                sched.AdvanceByMs(100);  // Compensate for scheduling
                item.IsNotNullString = "Baz";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Even if we issue a reset, no save
                fixture.SuspendNotifications().Dispose(); // Will trigger a reset.
                sched.AdvanceByMs(100);                   // Compensate for scheduling
                item.IsNotNullString = "Bamf";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Remove by hand = no save
                fixture.RemoveAt(0);
                item.IsNotNullString = "Blomf";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);
            });
        }
        public void ValueTest()
        {
            var input = new[] {"Foo", "Bar", "Baz"};
            IEnumerable<string> output = null;
            IEnumerable<string> output2 = null;

            (new TestScheduler()).With(sched => {
                var fixture = new TestFixture();

                // Same deal as above
                output = fixture.Changed.Value<object, object, string>().CreateCollection();
                output2 = fixture.ObservableForProperty(x => x.IsOnlyOneWord).Value().CreateCollection();

                foreach (var v in input) { fixture.IsOnlyOneWord = v; }

                sched.AdvanceToMs(1000);

                input.AssertAreEqual(output);
                input.AssertAreEqual(output2);
            });
        }
        public void GetValueShouldActuallyReturnTheValue()
        {
            var input = new[] {"Foo", "Bar", "Baz"};
            var output = new List<string>();
            var output2 = new List<string>();

            (new TestScheduler()).With(sched => {
                var fixture = new TestFixture();

                // Two cases: Changed is guaranteed to *not* set ObservedChange.Value
                fixture.Changed.Subscribe(x => {
                    output.Add((string) x.GetValue());
                });

                // ...whereas ObservableForProperty *is* guaranteed to.
                fixture.ObservableForProperty(x => x.IsOnlyOneWord).Subscribe(x => {
                    output2.Add(x.GetValue());
                });

                foreach (var v in input) { fixture.IsOnlyOneWord = v; }

                sched.RunToMilliseconds(1000);

                input.AssertAreEqual(output);
                input.AssertAreEqual(output2);
            });
        }
        public void WhenAnyShouldWorkEvenWithNormalProperties()
        {
            var fixture = new TestFixture() { IsNotNullString = "Foo", IsOnlyOneWord = "Baz", PocoProperty = "Bamf" };

            var output = new List<IObservedChange<TestFixture, string>>();
            fixture.WhenAny(x => x.PocoProperty, x => x).Subscribe(output.Add);

            Assert.Equal(1, output.Count);
            Assert.Equal(fixture, output[0].Sender);
            Assert.Equal("PocoProperty", output[0].PropertyName);
            Assert.Equal("Bamf", output[0].Value);
        }
        public void OFPSimplePropertyTest()
        {
            (new TestScheduler()).With(sched => {
                var fixture = new TestFixture();
                var changes = fixture.ObservableForProperty(x => x.IsOnlyOneWord).CreateCollection();

                fixture.IsOnlyOneWord = "Foo";
                sched.Start();
                Assert.Equal(1, changes.Count);

                fixture.IsOnlyOneWord = "Bar";
                sched.Start();
                Assert.Equal(2, changes.Count);

                fixture.IsOnlyOneWord = "Baz";
                sched.Start();
                Assert.Equal(3, changes.Count);

                fixture.IsOnlyOneWord = "Baz";
                sched.Start();
                Assert.Equal(3, changes.Count);

                Assert.True(changes.All(x => x.Sender == fixture));
                Assert.True(changes.All(x => x.PropertyName == "IsOnlyOneWord"));
                changes.Select(x => x.Value).AssertAreEqual(new[] {"Foo", "Bar", "Baz"});
            });
        }
Esempio n. 16
0
        public void DeferringNotificationsDontShowUpUntilUndeferred()
        {
            var fixture = new TestFixture();
            var output = fixture.Changed.CreateCollection();

            Assert.Equal(0, output.Count);
            fixture.NullableInt = 4;
            Assert.Equal(1, output.Count);

            var stopDelaying = fixture.DelayChangeNotifications();

            fixture.NullableInt = 5;
            Assert.Equal(1, output.Count);

            fixture.IsNotNullString = "Bar";
            Assert.Equal(1, output.Count);

            fixture.NullableInt = 6;
            Assert.Equal(1, output.Count);

            fixture.IsNotNullString = "Baz";
            Assert.Equal(1, output.Count);

            var stopDelayingMore = fixture.DelayChangeNotifications();

            fixture.IsNotNullString = "Bamf";
            Assert.Equal(1, output.Count);

            stopDelaying.Dispose();

            fixture.IsNotNullString = "Blargh";
            Assert.Equal(1, output.Count);

            // NB: Because we debounce queued up notifications, we should only
            // see a notification from the latest NullableInt and the latest
            // IsNotNullableString
            stopDelayingMore.Dispose();
            Assert.Equal(3, output.Count);
        }
Esempio n. 17
0
        public void ExceptionsThrownInSubscribersShouldMarshalToThrownExceptions()
        {
            var fixture = new TestFixture() { IsOnlyOneWord = "Foo" };

            fixture.Changed.Subscribe(x => { throw new Exception("Die!"); });
            var exceptionList = fixture.ThrownExceptions.CreateCollection();

            fixture.IsOnlyOneWord = "Bar";
            Assert.Equal(1, exceptionList.Count);
        }
Esempio n. 18
0
        public void ReactiveObjectShouldntSerializeAnythingExtra()
        {
            var fixture = new TestFixture() { IsNotNullString = "Foo", IsOnlyOneWord = "Baz" };
            string json = JSONHelper.Serialize(fixture);

            // Should look something like:
            // {"IsNotNullString":"Foo","IsOnlyOneWord":"Baz","NullableInt":null,"PocoProperty":null,"StackOverflowTrigger":null,"TestCollection":[],"UsesExprRaiseSet":null}
            Assert.True(json.Count(x => x == ',') == 6);
            Assert.True(json.Count(x => x == ':') == 7);
            Assert.True(json.Count(x => x == '"') == 18);
        }
        public void ReactiveObjectShouldntSerializeAnythingExtra()
        {
            var fixture = new TestFixture() { IsNotNullString = "Foo", IsOnlyOneWord = "Baz" };
            string json = JSONHelper.Serialize(fixture);
            this.Log().Info(json);

            // Should look something like:
            // {"TestCollection":[],"_IsNotNullString":"Foo","_IsOnlyOneWord":"Baz","_PocoProperty":null,"_UsesExprRaiseSet":null}
            Assert.True(json.Count(x => x == ',') == 4);
            Assert.True(json.Count(x => x == ':') == 5);
            Assert.True(json.Count(x => x == '"') == 14);
        }
Esempio n. 20
0
        public void AutoPersistCollectionDisconnectsOnDispose()
        {
            (new TestScheduler()).With(sched => {
                var manualSave = new Subject<Unit>();

                var item = new TestFixture();
                var fixture = new ReactiveList<TestFixture> { item };

                int timesSaved = 0;
                var disp = fixture.AutoPersistCollection(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));

                sched.AdvanceByMs(2 * 100);
                Assert.Equal(0, timesSaved);

                // By being added to collection, AutoPersist is enabled for item
                item.IsNotNullString = "Foo";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Dispose = no save
                disp.Dispose();

                // Removed from collection = no save
                fixture.Clear();
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Item isn't in the collection, it doesn't get persisted anymore
                item.IsNotNullString = "Bar";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Added back item + dispose = no save
                fixture.Add(item);
                sched.AdvanceByMs(100);  // Compensate for scheduling
                item.IsNotNullString = "Baz";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Even if we issue a reset, no save
                fixture.Reset();
                sched.AdvanceByMs(100);  // Compensate for scheduling
                item.IsNotNullString = "Bamf";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);

                // Remove by hand = no save
                fixture.RemoveAt(0);
                item.IsNotNullString = "Blomf";
                sched.AdvanceByMs(2 * 100);
                Assert.Equal(1, timesSaved);
            });
        }
        public void SubscriptionExceptionsShouldntPermakillReactiveObject()
        {
            Assert.True(false, "This test doesn't work yet");

            var fixture = new TestFixture();
            int i = 0;
            fixture.Changed.Subscribe(x => {
                if (++i == 2)
                    throw new Exception("Deaded!");
            });

            fixture.IsNotNullString = "Foo";
            fixture.IsNotNullString = "Bar";
            fixture.IsNotNullString = "Baz";
            fixture.IsNotNullString = "Bamf";

            var output = new List<string>();
            fixture.Changed.Subscribe(x => output.Add(x.PropertyName));
            fixture.IsOnlyOneWord = "Bar";

            Assert.Equal("IsOnlyOneWord", output[0]);
            Assert.Equal(1, output.Count);
        }