NestedContainerTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // MonoTests.System.ComponentModel.NestedContainerTest
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2007 Ivan N. Zlatev
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.ComponentModel;
  31. using System.Collections;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.ComponentModel
  34. {
  35. [TestFixture]
  36. public class NestedContainerTest : NestedContainer
  37. {
  38. private class ContainerWithService : Container
  39. {
  40. public class DesignModeEnabledSite : ISite {
  41. private IComponent _component;
  42. private ContainerWithService _container;
  43. private string _name;
  44. public DesignModeEnabledSite (string name, IComponent component, ContainerWithService container)
  45. {
  46. _component = component;
  47. _container = container;
  48. _name = name;
  49. }
  50. public IComponent Component {
  51. get { return _component; }
  52. }
  53. public IContainer Container {
  54. get { return _container; }
  55. }
  56. public bool DesignMode {
  57. get { return true; }
  58. }
  59. public string Name {
  60. get { return _name; }
  61. set { _name = value; }
  62. }
  63. public virtual object GetService (Type t)
  64. {
  65. if (typeof (ISite) == t)
  66. return this;
  67. return _container.GetService (t);
  68. }
  69. }
  70. protected override object GetService (Type type)
  71. {
  72. if (typeof (ContainerWithService) == type)
  73. return this;
  74. else
  75. return null;
  76. }
  77. protected override ISite CreateSite (IComponent component, string name)
  78. {
  79. return new ContainerWithService.DesignModeEnabledSite (name, component, this);
  80. }
  81. }
  82. public NestedContainerTest () : base (new Component())
  83. {
  84. }
  85. public NestedContainerTest (IComponent owner) : base (owner)
  86. {
  87. }
  88. [Test]
  89. public void NameTest ()
  90. {
  91. Container container = new Container ();
  92. Component owner = new Component ();
  93. container.Add (owner, "OwnerName");
  94. NestedContainerTest nestedContainer = new NestedContainerTest (owner);
  95. Component nestedComponent = new Component ();
  96. nestedContainer.Add (nestedComponent, "NestedComponentName");
  97. Assert.AreEqual ("OwnerName", nestedContainer.OwnerName, "#1");
  98. Assert.AreEqual ("OwnerName.NestedComponentName", ((INestedSite)nestedComponent.Site).FullName, "#2");
  99. // Prooves that MSDN is wrong.
  100. Assert.AreEqual ("NestedComponentName", nestedComponent.Site.Name, "#3");
  101. container.Remove (owner);
  102. Assert.AreEqual (null, nestedContainer.OwnerName, "#4");
  103. Assert.AreEqual ("NestedComponentName", ((INestedSite)nestedComponent.Site).FullName, "#5");
  104. }
  105. [Test]
  106. public void GetServiceTest ()
  107. {
  108. ContainerWithService container = new ContainerWithService ();
  109. Component owner = new Component ();
  110. container.Add (owner, "OwnerName");
  111. NestedContainerTest nestedContainer = new NestedContainerTest (owner);
  112. Component nestedComponent = new Component ();
  113. nestedContainer.Add (nestedComponent, "NestedComponentName");
  114. Assert.IsNotNull (nestedComponent.Site.GetService (typeof (INestedContainer)), "#1");
  115. // test who provides the ISite service.
  116. Assert.AreEqual (nestedComponent.Site, nestedComponent.Site.GetService (typeof (ISite)), "#2");
  117. // test GetService forwarding to owner. Prooves that MSDN is wrong
  118. Assert.IsNull (nestedComponent.Site.GetService (typeof (ContainerWithService)), "#3");
  119. }
  120. [Test]
  121. public void DesignModeTest ()
  122. {
  123. ContainerWithService container = new ContainerWithService ();
  124. Component owner = new Component ();
  125. container.Add (owner, "OwnerName");
  126. NestedContainerTest nestedContainer = new NestedContainerTest (owner);
  127. Component nestedComponent = new Component ();
  128. nestedContainer.Add (nestedComponent, "NestedComponentName");
  129. Assert.IsTrue (nestedComponent.Site.DesignMode, "#1");
  130. }
  131. }
  132. }
  133. #endif