PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Tests/Events/EventBaseFixture.cs

#
C# | 130 lines | 88 code | 26 blank | 16 comment | 0 complexity | 7e4dd4e58f93ac78a7fb2a8d76771618 MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using Microsoft.Practices.Composite.Events;
  19. using Microsoft.VisualStudio.TestTools.UnitTesting;
  20. namespace Microsoft.Practices.Composite.Tests.Events
  21. {
  22. [TestClass]
  23. public class EventBaseFixture
  24. {
  25. [TestMethod]
  26. public void CanPublishSimpleEvents()
  27. {
  28. var eventBase = new TestableEventBase();
  29. var eventSubscription = new MockEventSubscription();
  30. bool eventPublished = false;
  31. eventSubscription.GetPublishActionReturnValue = delegate
  32. {
  33. eventPublished = true;
  34. };
  35. eventBase.Subscribe(eventSubscription);
  36. eventBase.Publish();
  37. Assert.IsTrue(eventSubscription.GetPublishActionCalled);
  38. Assert.IsTrue(eventPublished);
  39. }
  40. [TestMethod]
  41. public void CanHaveMultipleSubscribersAndRaiseCustomEvent()
  42. {
  43. var customEvent = new TestableEventBase();
  44. Payload payload = new Payload();
  45. object[] received1 = null;
  46. object[] received2 = null;
  47. var eventSubscription1 = new MockEventSubscription();
  48. eventSubscription1.GetPublishActionReturnValue = delegate(object[] args) { received1 = args; };
  49. var eventSubscription2 = new MockEventSubscription();
  50. eventSubscription2.GetPublishActionReturnValue = delegate(object[] args) { received2 = args; };
  51. customEvent.Subscribe(eventSubscription1);
  52. customEvent.Subscribe(eventSubscription2);
  53. customEvent.Publish(payload);
  54. Assert.AreEqual(1, received1.Length);
  55. Assert.AreSame(received1[0], payload);
  56. Assert.AreEqual(1, received2.Length);
  57. Assert.AreSame(received2[0], payload);
  58. }
  59. [TestMethod]
  60. public void ShouldSubscribeAndUnsubscribe()
  61. {
  62. var eventBase = new TestableEventBase();
  63. var eventSubscription = new MockEventSubscription();
  64. eventBase.Subscribe(eventSubscription);
  65. Assert.IsNotNull(eventSubscription.SubscriptionToken);
  66. Assert.IsTrue(eventBase.Contains(eventSubscription.SubscriptionToken));
  67. eventBase.Unsubscribe(eventSubscription.SubscriptionToken);
  68. Assert.IsFalse(eventBase.Contains(eventSubscription.SubscriptionToken));
  69. }
  70. [TestMethod]
  71. public void WhenEventSubscriptionActionIsNullPruneItFromList()
  72. {
  73. var eventBase = new TestableEventBase();
  74. var eventSubscription = new MockEventSubscription();
  75. eventSubscription.GetPublishActionReturnValue = null;
  76. var token = eventBase.Subscribe(eventSubscription);
  77. eventBase.Publish();
  78. Assert.IsFalse(eventBase.Contains(token));
  79. }
  80. class TestableEventBase : EventBase
  81. {
  82. public SubscriptionToken Subscribe(IEventSubscription subscription)
  83. {
  84. return base.InternalSubscribe(subscription);
  85. }
  86. public void Publish(params object[] arguments)
  87. {
  88. base.InternalPublish(arguments);
  89. }
  90. }
  91. class MockEventSubscription : IEventSubscription
  92. {
  93. public Action<object[]> GetPublishActionReturnValue;
  94. public bool GetPublishActionCalled;
  95. public Action<object[]> GetExecutionStrategy()
  96. {
  97. GetPublishActionCalled = true;
  98. return GetPublishActionReturnValue;
  99. }
  100. public SubscriptionToken SubscriptionToken { get; set; }
  101. }
  102. class Payload { }
  103. }
  104. }