[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System / Test / System.ComponentModel / ReferenceConverterTest.cs
1 //
2 // MonoTests.System.ComponentModel.ReferenceConverterTest
3 //
4 // Author:
5 //      Ivan N. Zlatev  <contact@i-nz.net>
6 //
7 // Copyright (C) 2008 Ivan N. Zlatev
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.Collections.Generic;
35 using System.ComponentModel;
36 using System.ComponentModel.Design;
37 using System.ComponentModel.Design.Serialization;
38 using System.Globalization;
39
40 using NUnit.Framework;
41
42 namespace MonoTests.System.ComponentModel
43 {
44         [TestFixture]
45         public class ReferenceConverterTest
46         {
47
48                 class TestReferenceService : IReferenceService
49                 {
50                         private Dictionary<string, object> references;
51
52                         public TestReferenceService ()
53                         {
54                                 references = new Dictionary<string, object> ();
55                         }
56
57                         public void AddReference (string name, object reference)
58                         {
59                                 references[name] = reference;
60                         }
61
62                         public void ClearReferences ()
63                         {
64                                 references.Clear ();
65                         }
66
67                         public IComponent GetComponent (object reference)
68                         {
69                                 return null;
70                         }
71
72                         public string GetName (object reference)
73                         {
74                                 foreach (KeyValuePair<string, object> entry in references) {
75                                         if (entry.Value == reference)
76                                                 return entry.Key;
77                                 }
78
79                                 return null;
80                         }
81
82                         public object GetReference (string name)
83                         {
84                                 if (!references.ContainsKey (name))
85                                         return null;
86                                 return references[name];
87                         }
88
89                         public object[] GetReferences()
90                         {
91                                 object[] array = new object[references.Values.Count];
92                                 references.Values.CopyTo (array, 0);
93                                 return array;
94                         }
95
96                         public object[] GetReferences(Type baseType)
97                         {
98                                 object[] references = GetReferences ();
99
100                                 List<object> filtered = new List<object> ();
101                                 foreach (object reference in references) {
102                                         if (baseType.IsInstanceOfType(reference))
103                                                 filtered.Add (reference);
104                                 }
105
106                                 return filtered.ToArray();
107                         }
108                 }
109
110                 class TestTypeDescriptorContext : ITypeDescriptorContext
111                 {
112                         private IReferenceService reference_service = null;
113                         private IContainer container = null;
114
115                         public TestTypeDescriptorContext ()
116                         {
117                         }
118
119                         public TestTypeDescriptorContext (IReferenceService referenceService)
120                         {
121                                 reference_service = referenceService;
122                         }
123
124
125                         public IContainer Container {
126                                 get { return container; }
127                                 set { container = value; }
128                         }
129
130                         public object Instance {
131                                 get { return null; }
132                         }
133
134                         public PropertyDescriptor PropertyDescriptor {
135                                 get { return null; }
136                         }
137
138                         public void OnComponentChanged ()
139                         {
140                         }
141
142                         public bool OnComponentChanging ()
143                         {
144                                 return true;
145                         }
146
147                         public object GetService (Type serviceType)
148                         {
149                                 if (serviceType == typeof (IReferenceService))
150                                         return reference_service;
151                                 return null;
152                         }
153                 }
154                 
155                 interface ITestInterface
156                 {
157                 }
158
159                 class TestComponent : Component
160                 {
161                 }
162
163                 [Test]
164                 public void CanConvertFrom ()
165                 {
166                         ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
167                         // without context
168                         Assert.IsFalse (converter.CanConvertFrom (null, typeof(string)), "#1");
169                         // with context
170                         Assert.IsTrue (converter.CanConvertFrom (new TestTypeDescriptorContext (), typeof(string)), "#2");
171                 }
172
173 #if !MOBILE
174                 [Test]
175                 public void ConvertFrom ()
176                 {
177                         ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
178                         string referenceName = "reference name";
179                         // no context
180                         Assert.IsNull (converter.ConvertFrom (null, null, referenceName), "#1");
181
182                         TestComponent component = new TestComponent();
183
184                         // context with IReferenceService
185                         TestReferenceService referenceService = new TestReferenceService ();
186                         referenceService.AddReference (referenceName, component);
187                         TestTypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);
188                         Assert.AreSame (component, converter.ConvertFrom (context, null, referenceName), "#2");
189                         
190                         // context with Component without IReferenceService
191                         Container container = new Container ();
192                         container.Add (component, referenceName);
193                         context = new TestTypeDescriptorContext ();
194                         context.Container = container;
195                         Assert.AreSame (component, converter.ConvertFrom (context, null, referenceName), "#3");
196                 }
197
198                 [Test]
199                 public void ConvertTo ()
200                 {
201                         ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
202                         string referenceName = "reference name";
203
204                         Assert.AreEqual ("(none)", (string)converter.ConvertTo (null, null, null, typeof(string)), "#1");
205
206                         TestComponent component = new TestComponent();
207
208                         // no context
209                         Assert.AreEqual (String.Empty, (string)converter.ConvertTo (null, null, component, typeof(string)), "#2");
210
211                         // context with IReferenceService
212                         TestReferenceService referenceService = new TestReferenceService ();
213                         referenceService.AddReference (referenceName, component);
214                         TestTypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);
215                         Assert.AreEqual (referenceName, (string)converter.ConvertTo (context, null, component, typeof(string)), "#3");
216                         
217                         // context with Component without IReferenceService
218                         Container container = new Container ();
219                         container.Add (component, referenceName);
220                         context = new TestTypeDescriptorContext ();
221                         context.Container = container;
222                         Assert.AreEqual (referenceName, (string)converter.ConvertTo (context, null, component, typeof(string)), "#4");
223                 }
224
225 #endif
226                 
227                 [Test]
228                 public void CanConvertTo ()
229                 {
230                         ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
231                         Assert.IsTrue (converter.CanConvertTo (new TestTypeDescriptorContext (), typeof(string)), "#1");
232                 }
233
234                 [Test]
235                 public void GetStandardValues ()
236                 {
237                         ReferenceConverter converter = new ReferenceConverter (typeof(TestComponent));
238
239                         TestComponent component1 = new TestComponent();
240                         TestComponent component2 = new TestComponent();
241                         TestReferenceService referenceService = new TestReferenceService ();
242                         referenceService.AddReference ("reference name 1", component1);
243                         referenceService.AddReference ("reference name 2", component2);
244                         ITypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);
245
246                         TypeConverter.StandardValuesCollection values = converter.GetStandardValues (context);
247                         Assert.IsNotNull (values, "#1");
248                         // 2 components + 1 null value
249                         Assert.AreEqual (3, values.Count, "#2");
250                 }
251         }
252 }
253 #endif