summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2017-01-31 06:10:28 -0700
committerRui Marinho <me@ruimarinho.net>2017-01-31 13:10:28 +0000
commitb4fe4e0c902177ff7c175f691a5304d7c7ef518d (patch)
tree50bbf0a4c2ff4fef7492f3ba0dee9f7d0c445d67 /Xamarin.Forms.Core.UnitTests
parentd80be6fdb228567cc372e6c242052a6ba5853681 (diff)
downloadxamarin-forms-b4fe4e0c902177ff7c175f691a5304d7c7ef518d.tar.gz
xamarin-forms-b4fe4e0c902177ff7c175f691a5304d7c7ef518d.tar.bz2
xamarin-forms-b4fe4e0c902177ff7c175f691a5304d7c7ef518d.zip
Make MessagingCenter testable (#723)
* Make MessagingCenter testable * Eagerly create MessagingCenter instancef * More succinct version
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests')
-rw-r--r--Xamarin.Forms.Core.UnitTests/MessagingCenterTests.cs75
1 files changed, 70 insertions, 5 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/MessagingCenterTests.cs b/Xamarin.Forms.Core.UnitTests/MessagingCenterTests.cs
index 3cd91ed7..2107513d 100644
--- a/Xamarin.Forms.Core.UnitTests/MessagingCenterTests.cs
+++ b/Xamarin.Forms.Core.UnitTests/MessagingCenterTests.cs
@@ -246,7 +246,7 @@ namespace Xamarin.Forms.Core.UnitTests
GC.Collect();
GC.WaitForPendingFinalizers();
-
+
Assert.IsTrue(wr.IsAlive); // The closure in Subscribe should be keeping the subscriber alive
Assert.IsNotNull(wr.Target as TestSubcriber);
@@ -272,10 +272,10 @@ namespace Xamarin.Forms.Core.UnitTests
MessagingCenter.Subscribe<TestPublisher>(subscriber, "test", p => subscriber.SetSuccess());
})();
- Assert.IsNotNull(wr.Target as TestSubcriber);
+ Assert.IsNotNull(wr.Target as TestSubcriber);
MessagingCenter.Unsubscribe<TestPublisher>(wr.Target, "test");
-
+
GC.Collect();
GC.WaitForPendingFinalizers();
@@ -309,12 +309,12 @@ namespace Xamarin.Forms.Core.UnitTests
var source = new MessagingCenterTestsCallbackSource();
MessagingCenter.Subscribe<TestPublisher>(_subscriber, "test", p => source.SuccessCallback(ref success));
-
+
GC.Collect();
GC.WaitForPendingFinalizers();
var pub = new TestPublisher();
- pub.Test();
+ pub.Test();
Assert.True(success); // TestCallbackSource.SuccessCallback() should be invoked to make success == true
}
@@ -372,5 +372,70 @@ namespace Xamarin.Forms.Core.UnitTests
i = i + 1;
}
}
+
+ [Test(Description = "This is a demonstration of what a test with a fake/mock/substitute IMessagingCenter might look like")]
+ public void TestMessagingCenterSubstitute()
+ {
+ var mc = new FakeMessagingCenter();
+
+ // In the real world, you'd construct this with `new ComponentWithMessagingDependency(MessagingCenter.Instance);`
+ var component = new ComponentWithMessagingDependency(mc);
+ component.DoAThing();
+
+ Assert.IsTrue(mc.WasSubscribeCalled, "ComponentWithMessagingDependency should have subscribed in its constructor");
+ Assert.IsTrue(mc.WasSendCalled, "The DoAThing method should send a message");
+ }
+
+ class ComponentWithMessagingDependency
+ {
+ readonly IMessagingCenter _messagingCenter;
+
+ public ComponentWithMessagingDependency(IMessagingCenter messagingCenter)
+ {
+ _messagingCenter = messagingCenter;
+ _messagingCenter.Subscribe<ComponentWithMessagingDependency>(this, "test", dependency => Console.WriteLine("test"));
+ }
+
+ public void DoAThing()
+ {
+ _messagingCenter.Send(this, "test");
+ }
+ }
+
+ internal class FakeMessagingCenter : IMessagingCenter
+ {
+ public bool WasSubscribeCalled { get; private set; }
+ public bool WasSendCalled { get; private set; }
+
+ public void Send<TSender, TArgs>(TSender sender, string message, TArgs args) where TSender : class
+ {
+ WasSendCalled = true;
+ }
+
+ public void Send<TSender>(TSender sender, string message) where TSender : class
+ {
+ WasSendCalled = true;
+ }
+
+ public void Subscribe<TSender, TArgs>(object subscriber, string message, Action<TSender, TArgs> callback, TSender source = default(TSender)) where TSender : class
+ {
+ WasSubscribeCalled = true;
+ }
+
+ public void Subscribe<TSender>(object subscriber, string message, Action<TSender> callback, TSender source = default(TSender)) where TSender : class
+ {
+ WasSubscribeCalled = true;
+ }
+
+ public void Unsubscribe<TSender, TArgs>(object subscriber, string message) where TSender : class
+ {
+
+ }
+
+ public void Unsubscribe<TSender>(object subscriber, string message) where TSender : class
+ {
+
+ }
+ }
}
}