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