PageRenderTime 32ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/V1/trunk/Source/CAL/Composite.Wpf.Tests/Regions/SelectorRegionAdapterFixture.cs

#
C# | 269 lines | 197 code | 56 blank | 16 comment | 0 complexity | 2618c0a22d06d1424ee7b12330a48edc MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation
  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.Collections;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using Microsoft.Practices.Composite.Regions;
  24. using Microsoft.Practices.Composite.Wpf.Regions;
  25. using Microsoft.Practices.Composite.Wpf.Tests.Mocks;
  26. using Microsoft.VisualStudio.TestTools.UnitTesting;
  27. namespace Microsoft.Practices.Composite.Wpf.Tests.Regions
  28. {
  29. [TestClass]
  30. public class SelectorRegionAdapterFixture
  31. {
  32. [TestMethod]
  33. public void AdapterAssociatesSelectorWithRegion()
  34. {
  35. var control = new TabControl();
  36. IRegionAdapter adapter = new TestableSelectorRegionAdapter();
  37. IRegion region = adapter.Initialize(control);
  38. Assert.IsNotNull(region);
  39. Assert.AreSame(control.ItemsSource, region.Views);
  40. }
  41. [TestMethod]
  42. public void ShouldMoveAlreadyExistingContentInControlToRegion()
  43. {
  44. var control = new TabControl();
  45. var view = new object();
  46. control.Items.Add(view);
  47. IRegionAdapter adapter = new TestableSelectorRegionAdapter();
  48. var region = (MockRegion)adapter.Initialize(control);
  49. Assert.AreEqual(1, region.MockViews.Count());
  50. Assert.AreSame(view, region.MockViews.ElementAt(0));
  51. Assert.AreSame(view, control.Items[0]);
  52. }
  53. [TestMethod]
  54. public void ControlWithExistingItemSourceThrows()
  55. {
  56. TabControl tabControl = new TabControl() { ItemsSource = new List<string>() };
  57. IRegionAdapter adapter = new TestableSelectorRegionAdapter();
  58. try
  59. {
  60. var region = (MockRegion)adapter.Initialize(tabControl);
  61. Assert.Fail();
  62. }
  63. catch (Exception ex)
  64. {
  65. Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
  66. StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
  67. }
  68. }
  69. [TestMethod]
  70. public void ControlWithExistingBindingOnItemsSourceWithNullValueThrows()
  71. {
  72. TabControl tabControl = new TabControl();
  73. Binding binding = new Binding("Enumerable");
  74. binding.Source = new SimpleModel() { Enumerable = null };
  75. BindingOperations.SetBinding(tabControl, TabControl.ItemsSourceProperty, binding);
  76. IRegionAdapter adapter = new TestableSelectorRegionAdapter();
  77. try
  78. {
  79. var region = (MockRegion)adapter.Initialize(tabControl);
  80. Assert.Fail();
  81. }
  82. catch (Exception ex)
  83. {
  84. Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
  85. StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
  86. }
  87. }
  88. [TestMethod]
  89. public void AdapterSynchronizesSelectorSelectionWithRegion()
  90. {
  91. TabControl tabControl = new TabControl();
  92. object model1 = new object();
  93. object model2 = new object();
  94. IRegionAdapter adapter = new SelectorRegionAdapter();
  95. var region = adapter.Initialize(tabControl);
  96. region.Add(model1);
  97. region.Add(model2);
  98. Assert.IsFalse(region.ActiveViews.Contains(model2));
  99. tabControl.SelectedItem = model2;
  100. Assert.IsTrue(region.ActiveViews.Contains(model2));
  101. Assert.IsFalse(region.ActiveViews.Contains(model1));
  102. tabControl.SelectedItem = model1;
  103. Assert.IsTrue(region.ActiveViews.Contains(model1));
  104. Assert.IsFalse(region.ActiveViews.Contains(model2));
  105. }
  106. [TestMethod]
  107. public void AdapterDoesNotPreventRegionFromBeingGarbageCollected()
  108. {
  109. TabControl tabControl = new TabControl();
  110. object model = new object();
  111. IRegionAdapter adapter = new SelectorRegionAdapter();
  112. var region = adapter.Initialize(tabControl);
  113. region.Add(model);
  114. WeakReference regionWeakReference = new WeakReference(region);
  115. WeakReference controlWeakReference = new WeakReference(tabControl);
  116. Assert.IsTrue(regionWeakReference.IsAlive);
  117. Assert.IsTrue(controlWeakReference.IsAlive);
  118. region = null;
  119. tabControl = null;
  120. GC.Collect();
  121. GC.Collect();
  122. Assert.IsFalse(regionWeakReference.IsAlive);
  123. Assert.IsFalse(controlWeakReference.IsAlive);
  124. }
  125. [TestMethod]
  126. public void AdapterDoesNotPreventRegionFromBeingGarbageCollectedWhenSettingItemsSourceToNull()
  127. {
  128. TabControl tabControl = new TabControl();
  129. object model = new object();
  130. IRegionAdapter adapter = new SelectorRegionAdapter();
  131. var region = adapter.Initialize(tabControl);
  132. region.Add(model);
  133. WeakReference regionWeakReference = new WeakReference(region);
  134. Assert.IsTrue(regionWeakReference.IsAlive);
  135. tabControl.ItemsSource = null;
  136. region = null;
  137. GC.Collect();
  138. GC.Collect();
  139. Assert.IsFalse(regionWeakReference.IsAlive);
  140. }
  141. [TestMethod]
  142. public void AdapterDoesNotPreventControlFromBeingGarbageCollectedWhenSettingItemsSourceToNull()
  143. {
  144. TabControl tabControl = new TabControl();
  145. object model = new object();
  146. IRegionAdapter adapter = new SelectorRegionAdapter();
  147. var region = adapter.Initialize(tabControl);
  148. region.Add(model);
  149. WeakReference controlWeakReference = new WeakReference(tabControl);
  150. Assert.IsTrue(controlWeakReference.IsAlive);
  151. tabControl.ItemsSource = null;
  152. tabControl = null;
  153. GC.Collect();
  154. GC.Collect();
  155. Assert.IsFalse(controlWeakReference.IsAlive);
  156. }
  157. [TestMethod]
  158. public void RegionDoesNotGetUpdatedIfTheItemsSourceWasManuallyChanged()
  159. {
  160. TabControl tabControl = new TabControl();
  161. object model = new object();
  162. IRegionAdapter adapter = new SelectorRegionAdapter();
  163. var region = adapter.Initialize(tabControl);
  164. region.Add(model);
  165. tabControl.ItemsSource = null;
  166. tabControl.Items.Add(model);
  167. Assert.IsFalse(region.ActiveViews.Contains(model));
  168. tabControl.SelectedItem = model;
  169. Assert.IsFalse(region.ActiveViews.Contains(model), "The region should not be updated");
  170. }
  171. [TestMethod]
  172. public void ActivatingTheViewShouldUpdateTheSelectedItem()
  173. {
  174. TabControl tabControl = new TabControl();
  175. var view1 = new object();
  176. var view2 = new object();
  177. IRegionAdapter adapter = new SelectorRegionAdapter();
  178. var region = adapter.Initialize(tabControl);
  179. region.Add(view1);
  180. region.Add(view2);
  181. Assert.AreNotEqual(view1, tabControl.SelectedItem);
  182. region.Activate(view1);
  183. Assert.AreEqual(view1, tabControl.SelectedItem);
  184. region.Activate(view2);
  185. Assert.AreEqual(view2, tabControl.SelectedItem);
  186. }
  187. [TestMethod]
  188. public void DeactivatingTheSelectedViewShouldUpdateTheSelectedItem()
  189. {
  190. TabControl tabControl = new TabControl();
  191. var view1 = new object();
  192. IRegionAdapter adapter = new SelectorRegionAdapter();
  193. var region = adapter.Initialize(tabControl);
  194. region.Add(view1);
  195. region.Activate(view1);
  196. Assert.AreEqual(view1, tabControl.SelectedItem);
  197. region.Deactivate(view1);
  198. Assert.AreNotEqual(view1, tabControl.SelectedItem);
  199. }
  200. class SimpleModel
  201. {
  202. public IEnumerable Enumerable { get; set; }
  203. }
  204. internal class TestableSelectorRegionAdapter : SelectorRegionAdapter
  205. {
  206. private MockRegion region = new MockRegion();
  207. protected override IRegion CreateRegion()
  208. {
  209. return region;
  210. }
  211. }
  212. }
  213. }