PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism.Tests/Regions/SelectorRegionAdapterFixture.cs

#
C# | 128 lines | 85 code | 27 blank | 16 comment | 0 complexity | 952f48b703c27162d925b490f28d8221 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.Collections;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using Microsoft.Practices.Prism.Tests.Mocks;
  24. using Microsoft.Practices.Prism.Regions;
  25. using Microsoft.Practices.Prism.Regions.Behaviors;
  26. using Microsoft.VisualStudio.TestTools.UnitTesting;
  27. namespace Microsoft.Practices.Prism.Tests.Regions
  28. {
  29. [TestClass]
  30. public class SelectorRegionAdapterFixture
  31. {
  32. [TestMethod]
  33. public void AdapterAddsSelectorItemsSourceSyncBehavior()
  34. {
  35. var control = new ListBox();
  36. IRegionAdapter adapter = new TestableSelectorRegionAdapter();
  37. IRegion region = adapter.Initialize(control, "Region1");
  38. Assert.IsNotNull(region);
  39. Assert.IsInstanceOfType(region.Behaviors["SelectorItemsSourceSyncBehavior"], typeof(SelectorItemsSourceSyncBehavior));
  40. }
  41. [TestMethod]
  42. public void AdapterDoesNotPreventRegionFromBeingGarbageCollected()
  43. {
  44. var selector = new ListBox();
  45. object model = new object();
  46. IRegionAdapter adapter = new SelectorRegionAdapter(null);
  47. var region = adapter.Initialize(selector, "Region1");
  48. region.Add(model);
  49. WeakReference regionWeakReference = new WeakReference(region);
  50. WeakReference controlWeakReference = new WeakReference(selector);
  51. Assert.IsTrue(regionWeakReference.IsAlive);
  52. Assert.IsTrue(controlWeakReference.IsAlive);
  53. region = null;
  54. selector = null;
  55. GC.Collect();
  56. GC.Collect();
  57. Assert.IsFalse(regionWeakReference.IsAlive);
  58. Assert.IsFalse(controlWeakReference.IsAlive);
  59. }
  60. [TestMethod]
  61. public void ActivatingTheViewShouldUpdateTheSelectedItem()
  62. {
  63. var selector = new ListBox();
  64. var view1 = new object();
  65. var view2 = new object();
  66. IRegionAdapter adapter = new SelectorRegionAdapter(null);
  67. var region = adapter.Initialize(selector, "Region1");
  68. region.Add(view1);
  69. region.Add(view2);
  70. Assert.AreNotEqual(view1, selector.SelectedItem);
  71. region.Activate(view1);
  72. Assert.AreEqual(view1, selector.SelectedItem);
  73. region.Activate(view2);
  74. Assert.AreEqual(view2, selector.SelectedItem);
  75. }
  76. [TestMethod]
  77. public void DeactivatingTheSelectedViewShouldUpdateTheSelectedItem()
  78. {
  79. var selector = new ListBox();
  80. var view1 = new object();
  81. IRegionAdapter adapter = new SelectorRegionAdapter(null);
  82. var region = adapter.Initialize(selector, "Region1");
  83. region.Add(view1);
  84. region.Activate(view1);
  85. Assert.AreEqual(view1, selector.SelectedItem);
  86. region.Deactivate(view1);
  87. Assert.AreNotEqual(view1, selector.SelectedItem);
  88. }
  89. private class TestableSelectorRegionAdapter : SelectorRegionAdapter
  90. {
  91. public TestableSelectorRegionAdapter()
  92. : base(null)
  93. {
  94. }
  95. protected override IRegion CreateRegion()
  96. {
  97. return new Region();
  98. }
  99. }
  100. }
  101. }