ContainerTest.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // System.ComponentModel.Container test cases
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Ivan N. Zlatev (contact i-nZ.net)
  7. // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
  8. // Copyright (c) 2006 Ivan N. Zlatev
  9. //
  10. using NUnit.Framework;
  11. using System;
  12. using System.ComponentModel;
  13. using System.ComponentModel.Design;
  14. namespace MonoTests.System.ComponentModel
  15. {
  16. class TestService {
  17. }
  18. class TestContainer : Container {
  19. ServiceContainer _services = new ServiceContainer();
  20. public TestContainer() {
  21. _services.AddService( typeof(TestService), new TestService() );
  22. }
  23. protected override object GetService( Type serviceType ) {
  24. return _services.GetService( serviceType );
  25. }
  26. #if NET_2_0
  27. public void Remove_WithoutUnsiting (IComponent component)
  28. {
  29. base.RemoveWithoutUnsiting (component);
  30. }
  31. #endif
  32. public bool Contains (IComponent component)
  33. {
  34. bool found = false;
  35. foreach (IComponent c in Components) {
  36. if (component.Equals (c)) {
  37. found = true;
  38. break;
  39. }
  40. }
  41. return found;
  42. }
  43. }
  44. class TestComponent : Component {
  45. public override ISite Site {
  46. get {
  47. return base.Site;
  48. }
  49. set {
  50. base.Site = value;
  51. if (value != null) {
  52. Assert.IsNotNull (value.GetService (typeof (ISite)));
  53. Assert.IsNotNull (value.GetService (typeof (TestService)));
  54. }
  55. }
  56. }
  57. }
  58. [TestFixture]
  59. public class ContainerTest
  60. {
  61. private TestContainer _container;
  62. [SetUp]
  63. public void Init ()
  64. {
  65. _container = new TestContainer ();
  66. }
  67. [Test]
  68. public void AddRemove ()
  69. {
  70. TestComponent component = new TestComponent ();
  71. _container.Add (component);
  72. Assert.IsNotNull (component.Site, "#1");
  73. Assert.IsTrue (_container.Contains (component), "#2");
  74. _container.Remove (component);
  75. Assert.IsNull (component.Site, "#3");
  76. Assert.IsFalse (_container.Contains (component), "#4");
  77. #if NET_2_0
  78. _container.Add (component);
  79. _container.Remove_WithoutUnsiting (component);
  80. Assert.IsNotNull (component.Site, "#5");
  81. Assert.IsFalse (_container.Contains (component), "#6");
  82. #endif
  83. }
  84. [Test]
  85. public void GetService1 ()
  86. {
  87. _container.Add (new TestComponent ());
  88. }
  89. }
  90. }