2 // MonoTests.System.ComponentModel.ReferenceConverterTest
5 // Ivan N. Zlatev <contact@i-nz.net>
7 // Copyright (C) 2008 Ivan N. Zlatev
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections.Generic;
35 using System.ComponentModel;
36 using System.ComponentModel.Design;
37 using System.ComponentModel.Design.Serialization;
38 using System.Globalization;
40 using NUnit.Framework;
42 namespace MonoTests.System.ComponentModel
45 public class ReferenceConverterTest
48 class TestReferenceService : IReferenceService
50 private Dictionary<string, object> references;
52 public TestReferenceService ()
54 references = new Dictionary<string, object> ();
57 public void AddReference (string name, object reference)
59 references[name] = reference;
62 public void ClearReferences ()
67 public IComponent GetComponent (object reference)
72 public string GetName (object reference)
74 foreach (KeyValuePair<string, object> entry in references) {
75 if (entry.Value == reference)
82 public object GetReference (string name)
84 if (!references.ContainsKey (name))
86 return references[name];
89 public object[] GetReferences()
91 object[] array = new object[references.Values.Count];
92 references.Values.CopyTo (array, 0);
96 public object[] GetReferences(Type baseType)
98 object[] references = GetReferences ();
100 List<object> filtered = new List<object> ();
101 foreach (object reference in references) {
102 if (baseType.IsInstanceOfType(reference))
103 filtered.Add (reference);
106 return filtered.ToArray();
110 class TestTypeDescriptorContext : ITypeDescriptorContext
112 private IReferenceService reference_service = null;
113 private IContainer container = null;
115 public TestTypeDescriptorContext ()
119 public TestTypeDescriptorContext (IReferenceService referenceService)
121 reference_service = referenceService;
125 public IContainer Container {
126 get { return container; }
127 set { container = value; }
130 public object Instance {
134 public PropertyDescriptor PropertyDescriptor {
138 public void OnComponentChanged ()
142 public bool OnComponentChanging ()
147 public object GetService (Type serviceType)
149 if (serviceType == typeof (IReferenceService))
150 return reference_service;
155 interface ITestInterface
159 class TestComponent : Component
164 public void CanConvertFrom ()
166 ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
168 Assert.IsFalse (converter.CanConvertFrom (null, typeof(string)), "#1");
170 Assert.IsTrue (converter.CanConvertFrom (new TestTypeDescriptorContext (), typeof(string)), "#2");
175 public void ConvertFrom ()
177 ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
178 string referenceName = "reference name";
180 Assert.IsNull (converter.ConvertFrom (null, null, referenceName), "#1");
182 TestComponent component = new TestComponent();
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");
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");
199 public void ConvertTo ()
201 ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
202 string referenceName = "reference name";
204 Assert.AreEqual ("(none)", (string)converter.ConvertTo (null, null, null, typeof(string)), "#1");
206 TestComponent component = new TestComponent();
209 Assert.AreEqual (String.Empty, (string)converter.ConvertTo (null, null, component, typeof(string)), "#2");
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");
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");
228 public void CanConvertTo ()
230 ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
231 Assert.IsTrue (converter.CanConvertTo (new TestTypeDescriptorContext (), typeof(string)), "#1");
235 public void GetStandardValues ()
237 ReferenceConverter converter = new ReferenceConverter (typeof(TestComponent));
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);
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");