2008-04-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System / Test / System.ComponentModel / ContainerTest.cs
1 //
2 // System.ComponentModel.Container test cases
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //  Ivan N. Zlatev (contact i-nZ.net)
7
8 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
9 // Copyright (c) 2006 Ivan N. Zlatev
10 //
11
12 using NUnit.Framework;
13 using System;
14 using System.ComponentModel;
15 using System.ComponentModel.Design;
16
17 namespace MonoTests.System.ComponentModel
18 {
19         class TestService {
20         }
21         
22         class TestContainer : Container {
23                 ServiceContainer _services = new ServiceContainer();
24                 
25                 public TestContainer() {
26                         _services.AddService( typeof(TestService), new TestService() );
27                 }
28                 
29                 protected override object GetService( Type serviceType ) {
30                         return _services.GetService( serviceType );
31                 }
32
33 #if NET_2_0
34                 public void Remove_WithoutUnsiting (IComponent component)
35                 {
36                         base.RemoveWithoutUnsiting (component);
37                 }
38 #endif
39                 
40                 public bool Contains (IComponent component)
41                 {
42                         bool found = false;
43                         
44                         foreach (IComponent c in Components) {
45                                 if (component.Equals (c)) {
46                                         found = true;
47                                         break;
48                                 }
49                         }
50                         return found;
51                 }
52         }
53         
54         class TestComponent : Component {
55                 public override ISite Site {
56                         get {
57                                 return base.Site;
58                         }
59                         set {
60                                 base.Site = value;
61                                 if (value != null) {
62                                         Assert.IsNotNull (value.GetService (typeof (ISite)));
63                                         Assert.IsNotNull (value.GetService (typeof (TestService)));
64                                 }
65                         }
66                 }
67         }
68
69         [TestFixture]
70         public class ContainerTest
71         {
72                 
73                 private TestContainer _container;
74                 
75                 [SetUp]
76                 public void Init ()
77                 {
78                         _container = new TestContainer ();
79                 }
80
81                 
82                 [Test]
83                 public void AddRemove ()
84                 {
85                         TestComponent component = new TestComponent ();
86                         
87                         _container.Add (component);
88                         Assert.IsNotNull (component.Site, "#1");
89                         Assert.IsTrue (_container.Contains (component), "#2");
90                         
91                         _container.Remove (component);
92                         Assert.IsNull (component.Site, "#3");
93                         Assert.IsFalse (_container.Contains (component), "#4");
94                         
95 #if NET_2_0
96                         _container.Add (component);
97                         _container.Remove_WithoutUnsiting (component);
98                         Assert.IsNotNull (component.Site, "#5");
99                         Assert.IsFalse (_container.Contains (component), "#6");
100 #endif
101                 }
102
103                 [Test]
104                 public void GetService1 ()
105                 {
106                         _container.Add (new TestComponent ());
107                 }
108
109 #if NET_2_0
110                 [Test]
111                 [ExpectedException (typeof (ArgumentException))]
112                 public void ValidateName ()
113                 {
114                         TestContainer container = new TestContainer ();
115                         TestComponent c1 = new TestComponent ();
116                         container.Add (c1, "dup");
117                         TestComponent c2 = new TestComponent ();
118                         container.Add (c2, "dup");
119                 }
120 #endif
121         }
122 }