PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System/Test/System.ComponentModel/ReferenceConverterTest.cs

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