PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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