PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/V4/PrismLibrary/Desktop/Prism.Tests/Events/BackgroundEventSubscriptionFixture.cs

#
C# | 58 lines | 35 code | 7 blank | 16 comment | 0 complexity | 0ed8920985e40fb1649ad813d91bc035 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 System.Threading;
  19. using Microsoft.Practices.Prism.Tests.Mocks;
  20. using Microsoft.Practices.Prism.Events;
  21. using Microsoft.VisualStudio.TestTools.UnitTesting;
  22. namespace Microsoft.Practices.Prism.Tests.Events
  23. {
  24. [TestClass]
  25. public class BackgroundEventSubscriptionFixture
  26. {
  27. [TestMethod]
  28. public void ShouldReceiveDelegateOnDifferentThread()
  29. {
  30. int calledThreadId = -1;
  31. ManualResetEvent completeEvent = new ManualResetEvent(false);
  32. Action<object> action = delegate
  33. {
  34. calledThreadId = Thread.CurrentThread.ManagedThreadId;
  35. completeEvent.Set();
  36. };
  37. IDelegateReference actionDelegateReference = new MockDelegateReference() { Target = action };
  38. IDelegateReference filterDelegateReference = new MockDelegateReference() { Target = (Predicate<object>)delegate { return true; } };
  39. var eventSubscription = new BackgroundEventSubscription<object>(actionDelegateReference, filterDelegateReference);
  40. var publishAction = eventSubscription.GetExecutionStrategy();
  41. Assert.IsNotNull(publishAction);
  42. publishAction.Invoke(null);
  43. #if SILVERLIGHT
  44. completeEvent.WaitOne(5000);
  45. #else
  46. completeEvent.WaitOne(5000, false);
  47. #endif
  48. Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, calledThreadId);
  49. }
  50. }
  51. }