NotificationCenterTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using NUnit.Framework;
  3. namespace CoreTests
  4. {
  5. internal class NotificationCenterTests
  6. {
  7. enum TestingNotificationType
  8. {
  9. TestEvent
  10. }
  11. class TestingNotification : GenericNotification<TestingNotificationType>
  12. {
  13. }
  14. class TestingNotificationCenter : GenericNotificationCenter<TestingNotificationType, TestingNotification>
  15. {
  16. }
  17. const TestingNotificationType testNotificationType = TestingNotificationType.TestEvent;
  18. void TestDelegate(TestingNotification note)
  19. {
  20. //Do nothing
  21. }
  22. /*[SetUp]
  23. public void SetUp()
  24. {
  25. TestingNotificationCenter.Reset_USE_ONLY_FOR_UNIT_TESTS();
  26. }
  27. [TearDown]
  28. public void TearDown()
  29. {
  30. }
  31. [Test]
  32. public void CreateOneDelegate()
  33. {
  34. TestingNotificationCenter.AddListener(TestDelegate, testNotificationType);
  35. ////Assert.AreEqual(TestingNotificationCenter.GetListenerCount(testNotificationType), 1);
  36. Assert.AreEqual(TestingNotificationCenter.GetTotalListenersCount(), 1);
  37. }
  38. [Test]
  39. public void CreateOneDelegateAndRemoveIt()
  40. {
  41. TestingNotificationCenter.AddListener(TestDelegate, testNotificationType);
  42. TestingNotificationCenter.RemoveListener(TestDelegate, testNotificationType);
  43. Assert.AreEqual(TestingNotificationCenter.GetListenerCount(testNotificationType), 0);
  44. Assert.AreEqual(TestingNotificationCenter.GetTotalListenersCount(), 0);
  45. }
  46. [Test]
  47. [ExpectedException(typeof(AssertionFailedException))]
  48. public void TryAddingDuplicateDeligates()
  49. {
  50. #if !DEBUG
  51. Assert.Fail("This test required DEBUG to be defined");
  52. #endif
  53. TestingNotificationCenter.AddListener(TestDelegate, testNotificationType);
  54. TestingNotificationCenter.AddListener(TestDelegate, testNotificationType);
  55. }
  56. [Test]
  57. public void TryRemovingDeligateMoreThanOnce()
  58. {
  59. TestingNotificationCenter.AddListener(TestDelegate, testNotificationType);
  60. TestingNotificationCenter.RemoveListener(TestDelegate, testNotificationType);
  61. TestingNotificationCenter.RemoveListener(TestDelegate, testNotificationType);
  62. Assert.AreEqual(TestingNotificationCenter.GetListenerCount(testNotificationType), 0);
  63. Assert.AreEqual(TestingNotificationCenter.GetTotalListenersCount(), 0);
  64. }
  65. [Test]
  66. public void TestNotificationPosting()
  67. {
  68. int invocations = 0;
  69. TestingNotificationCenter.OnNotificationDelegate notificationDelegate = (note) =>
  70. {
  71. ++invocations;
  72. };
  73. TestingNotificationCenter.AddListener(notificationDelegate, testNotificationType);
  74. TestingNotificationCenter.Post(testNotificationType);
  75. Assert.AreEqual(1, invocations);
  76. TestingNotificationCenter.Post(testNotificationType);
  77. Assert.AreEqual(2, invocations);
  78. }
  79. [Test]
  80. public void TestNotificationPostingAfterRemoving()
  81. {
  82. int invocations = 0;
  83. TestingNotificationCenter.OnNotificationDelegate notificationDelegate = (note) =>
  84. {
  85. ++invocations;
  86. };
  87. TestingNotificationCenter.AddListener(notificationDelegate, testNotificationType);
  88. TestingNotificationCenter.Post(testNotificationType);
  89. Assert.AreEqual(1, invocations);
  90. TestingNotificationCenter.RemoveListener(notificationDelegate, testNotificationType);
  91. TestingNotificationCenter.Post(testNotificationType);
  92. Assert.AreEqual(1, invocations);
  93. }*/
  94. }
  95. }