TestToolboxItem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. //
  2. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Authors:
  24. //
  25. // Jordi Mas i Hernandez, [email protected]
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.ComponentModel.Design;
  31. using System.Reflection;
  32. using System.Drawing;
  33. using System.Drawing.Design;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Drawing.Design {
  36. [TestFixture]
  37. public class TestToolboxItem {
  38. private AssemblyName an;
  39. private Bitmap bitmap;
  40. private ToolboxItemFilterAttribute[] filter;
  41. private AssemblyName[] dependent;
  42. class OurToolboxItem: ToolboxItem {
  43. public OurToolboxItem ()
  44. {
  45. }
  46. public void _CheckUnlocked ()
  47. {
  48. CheckUnlocked ();
  49. }
  50. public Type _GetType (IDesignerHost host, AssemblyName an, string tn, bool r)
  51. {
  52. return GetType (host, an, tn, r);
  53. }
  54. #if NET_2_0
  55. public object Filter (string propertyName, object value)
  56. {
  57. return FilterPropertyValue (propertyName, value);
  58. }
  59. public void _ValidatePropertyType (string propertyName, object value, Type expectedType, bool allowNull)
  60. {
  61. ValidatePropertyType (propertyName, value, expectedType, allowNull);
  62. }
  63. public object _ValidatePropertyValue (string propertyName, object value)
  64. {
  65. return ValidatePropertyValue (propertyName, value);
  66. }
  67. #endif
  68. }
  69. [TestFixtureSetUp]
  70. public void FixtureSetUp ()
  71. {
  72. bitmap = new Bitmap (20, 20);
  73. an = new AssemblyName ();
  74. filter = new ToolboxItemFilterAttribute[0];
  75. dependent = new AssemblyName[0];
  76. }
  77. [Test]
  78. public void DefaultValues ()
  79. {
  80. ToolboxItem item = new ToolboxItem ();
  81. Assert.IsNull (item.AssemblyName, "AssemblyName");
  82. Assert.IsNull (item.Bitmap, "Bitmap");
  83. Assert.AreEqual (String.Empty, item.DisplayName, "DisplayName");
  84. Assert.AreEqual (typeof (ToolboxItemFilterAttribute[]), item.Filter.GetType (), "Filter/Type");
  85. Assert.AreEqual (0, item.Filter.Count, "Filter");
  86. Assert.AreEqual (String.Empty, item.TypeName, "TypeName");
  87. #if NET_2_0
  88. Assert.IsNull (item.Company, "Company");
  89. Assert.AreEqual (".NET Component", item.ComponentType, "ComponentType");
  90. Assert.IsNull (item.DependentAssemblies, "DependentAssemblies");
  91. Assert.IsNull (item.Description, "Description");
  92. Assert.IsFalse (item.IsTransient, "IsTransient");
  93. Assert.AreEqual (0, item.Properties.Count, "Properties");
  94. Assert.AreEqual (String.Empty, item.Version, "Version");
  95. #endif
  96. }
  97. [Test]
  98. public void NullValues ()
  99. {
  100. ToolboxItem item = new ToolboxItem ();
  101. item.AssemblyName = null;
  102. Assert.IsNull (item.AssemblyName, "AssemblyName");
  103. item.Bitmap = null;
  104. Assert.IsNull (item.Bitmap, "Bitmap");
  105. item.DisplayName = null;
  106. Assert.AreEqual (String.Empty, item.DisplayName, "DisplayName");
  107. item.Filter = null;
  108. Assert.AreEqual (0, item.Filter.Count, "Filter");
  109. item.TypeName = null;
  110. Assert.AreEqual (String.Empty, item.TypeName, "TypeName");
  111. #if NET_2_0
  112. item.Company = null;
  113. Assert.AreEqual (String.Empty, item.Company, "Company");
  114. // can't assign null without a NRE
  115. item.DependentAssemblies = new AssemblyName [0];
  116. Assert.AreEqual (0, item.DependentAssemblies.Length, "DependentAssemblies");
  117. item.Description = null;
  118. Assert.AreEqual (String.Empty, item.Description, "Description");
  119. item.IsTransient = true;
  120. Assert.IsTrue (item.IsTransient, "IsTransient");
  121. Assert.AreEqual (9, item.Properties.Count, "Properties");
  122. item.Lock ();
  123. Assert.AreEqual (9, item.Properties.Count, "Properties/Lock");
  124. #endif
  125. }
  126. [Test]
  127. public void TestProperties ()
  128. {
  129. ToolboxItem item = new ToolboxItem ();
  130. AssemblyName name = new AssemblyName ();
  131. name.Name = "OurAssembly";
  132. item.AssemblyName = name;
  133. Assert.AreEqual (name.Name.ToString (), "OurAssembly", "TP#1");
  134. item.TypeName = "TypeName1";
  135. Assert.AreEqual ("TypeName1", item.TypeName, "TP#2");
  136. item.DisplayName = "ShowName";
  137. Assert.AreEqual (item.DisplayName, "ShowName", "TP#3");
  138. item.TypeName = "TypeNameSt";
  139. Assert.AreEqual (item.TypeName, "TypeNameSt", "TP#4");
  140. Bitmap bmp = new Bitmap (200, 200);
  141. item.Bitmap = bmp;
  142. Assert.AreEqual (bmp, item.Bitmap, "TP#5");
  143. }
  144. [Test]
  145. [ExpectedException (typeof (InvalidOperationException))]
  146. public void TestCheckUnlocked1 ()
  147. {
  148. OurToolboxItem item = new OurToolboxItem ();
  149. item.Lock ();
  150. item._CheckUnlocked ();
  151. }
  152. [Test]
  153. public void TestCheckUnlocked2 ()
  154. {
  155. OurToolboxItem item = new OurToolboxItem ();
  156. item._CheckUnlocked ();
  157. }
  158. [Test]
  159. [ExpectedException (typeof (ArgumentNullException))]
  160. public void GetType_Null_Null_Null_False ()
  161. {
  162. new OurToolboxItem ()._GetType (null, null, null, false);
  163. }
  164. [Test]
  165. public void GetType_Null_Null_String_False ()
  166. {
  167. OurToolboxItem item = new OurToolboxItem ();
  168. Assert.IsNull (item._GetType (null, null, "string", false), "GetType");
  169. }
  170. [Test]
  171. public void Initialize_Null ()
  172. {
  173. ToolboxItem item = new ToolboxItem ();
  174. item.Initialize (null);
  175. }
  176. [Test]
  177. [ExpectedException (typeof (InvalidOperationException))]
  178. public void Initialize_Locked_Null ()
  179. {
  180. ToolboxItem item = new ToolboxItem ();
  181. item.Lock ();
  182. item.Initialize (null);
  183. }
  184. [Test]
  185. public void Locked_Twice ()
  186. {
  187. ToolboxItem item = new ToolboxItem ();
  188. item.Lock ();
  189. item.Lock ();
  190. }
  191. #if NET_2_0
  192. [Test]
  193. public void TestNewProperties ()
  194. {
  195. ToolboxItem item = new ToolboxItem ();
  196. item.Company = "OurCompany";
  197. Assert.AreEqual ("OurCompany", item.Company, "TNP#1");
  198. Assert.AreEqual (".NET Component", item.ComponentType, "TNP#2");
  199. item.Description = "Description";
  200. Assert.AreEqual ("Description", item.Description, "TNP#3");
  201. item.IsTransient = true;
  202. Assert.AreEqual (true, item.IsTransient, "TNP#4");
  203. }
  204. [Test]
  205. [ExpectedException (typeof (NullReferenceException))]
  206. public void DependentAssemblies_Null ()
  207. {
  208. new ToolboxItem ().DependentAssemblies = null;
  209. }
  210. [Test]
  211. public void DependentAssemblies_Empty ()
  212. {
  213. ToolboxItem item = new ToolboxItem ();
  214. item.DependentAssemblies = new AssemblyName[0];
  215. Assert.AreEqual (0, item.DependentAssemblies.Length, "Length");
  216. }
  217. [Test]
  218. public void DependentAssemblies ()
  219. {
  220. AssemblyName[] names = new AssemblyName [1];
  221. names[0] = new AssemblyName ();
  222. ToolboxItem item = new ToolboxItem ();
  223. item.DependentAssemblies = names;
  224. Assert.AreEqual (1, item.DependentAssemblies.Length, "Length");
  225. Assert.IsTrue (Object.ReferenceEquals (names[0], item.DependentAssemblies[0]), "ReferenceEquals");
  226. names[0] = null;
  227. Assert.IsNotNull (item.DependentAssemblies[0], "0");
  228. }
  229. [Test]
  230. public void Filter ()
  231. {
  232. OurToolboxItem item = new OurToolboxItem ();
  233. Assert.IsNull (item.Filter ("AssemblyName", null), "AssemblyName,null");
  234. // they are "equal" (publicly wise) but have a different hash code
  235. Assert.AreEqual (typeof (AssemblyName), item.Filter ("AssemblyName", an).GetType (), "AssemblyName,an");
  236. Assert.IsNull (item.Filter ("Bitmap", null), "Bitmap,null");
  237. Assert.AreSame (bitmap, item.Filter ("Bitmap", bitmap), "Bitmap,bitmap");
  238. Assert.AreEqual (String.Empty, item.Filter ("DisplayName", null), "DisplayName,null");
  239. Assert.AreSame (String.Empty, item.Filter ("DisplayName", String.Empty), "DisplayName,string");
  240. Assert.AreEqual (filter, item.Filter ("Filter", null), "Filter,null");
  241. Assert.AreSame (filter, item.Filter ("Filter", filter), "Filter,ToolboxItemFilterAttribute[]");
  242. Assert.AreEqual (String.Empty, item.Filter ("TypeName", null), "TypeName,null");
  243. Assert.AreSame (String.Empty, item.Filter ("TypeName", String.Empty), "TypeName,string");
  244. Assert.IsNull (item.Filter ("Company", null), "Company,null");
  245. Assert.AreSame (String.Empty, item.Filter ("Company", String.Empty), "Company,string");
  246. Assert.AreEqual (null, item.Filter ("DependentAssemblies", null), "DependentAssemblies,null");
  247. // note: not same
  248. Assert.AreEqual (dependent, item.Filter ("DependentAssemblies", filter), "DependentAssemblies,AssemblyName[]");
  249. Assert.IsFalse (Object.ReferenceEquals (dependent, item.Filter ("DependentAssemblies", filter)), "DependentAssemblies,AssemblyName[]/Reference");
  250. Assert.IsNull (item.Filter ("Description", null), "Description,null");
  251. Assert.AreSame (String.Empty, item.Filter ("Description", String.Empty), "Description,string");
  252. Assert.IsTrue ((bool) item.Filter ("IsTransient", true), "IsTransient,true");
  253. Assert.IsFalse ((bool) item.Filter ("IsTransient", false), "IsTransient,false");
  254. }
  255. [Test]
  256. public void GetType_Null ()
  257. {
  258. ToolboxItem item = new ToolboxItem ();
  259. Assert.IsNull (item.GetType (null), "GetType(null)");
  260. }
  261. [Test]
  262. public void ValidatePropertyType ()
  263. {
  264. OurToolboxItem item = new OurToolboxItem ();
  265. item._ValidatePropertyType ("IsTransient", true, typeof (bool), false);
  266. item._ValidatePropertyType ("IsTransient", String.Empty, typeof (string), false);
  267. }
  268. [Test]
  269. [ExpectedException (typeof (NullReferenceException))]
  270. public void ValidatePropertyType_Type_Null ()
  271. {
  272. OurToolboxItem item = new OurToolboxItem ();
  273. item._ValidatePropertyType ("IsTransient", true, null, false);
  274. }
  275. [Test]
  276. [ExpectedException (typeof (ArgumentException))]
  277. public void ValidatePropertyType_IsTransient_WrongType ()
  278. {
  279. OurToolboxItem item = new OurToolboxItem ();
  280. item._ValidatePropertyType ("IsTransient", new object (), typeof (bool), false);
  281. }
  282. [Test]
  283. [ExpectedException (typeof (ArgumentNullException))]
  284. public void ValidatePropertyType_DontAllowNull ()
  285. {
  286. OurToolboxItem item = new OurToolboxItem ();
  287. item._ValidatePropertyType ("IsTransient", null, typeof (bool), false);
  288. }
  289. [Test]
  290. public void ValidatePropertyValue ()
  291. {
  292. OurToolboxItem item = new OurToolboxItem ();
  293. object o = new object ();
  294. Assert.IsNull (item._ValidatePropertyValue (null, null), "null,null");
  295. Assert.AreSame (o, item._ValidatePropertyValue (null, o), "null,object");
  296. Assert.IsNull (item._ValidatePropertyValue ("string", null), "string,null");
  297. Assert.AreSame (o, item._ValidatePropertyValue ("string", o), "string,object");
  298. Assert.IsNull (item._ValidatePropertyValue ("AssemblyName", null), "AssemblyName,null");
  299. Assert.AreSame (an, item._ValidatePropertyValue ("AssemblyName", an), "AssemblyName,an");
  300. Assert.IsNull (item._ValidatePropertyValue ("Bitmap", null), "Bitmap,null");
  301. Assert.AreSame (bitmap, item._ValidatePropertyValue ("Bitmap", bitmap), "Bitmap,bitmap");
  302. Assert.AreEqual (String.Empty, item._ValidatePropertyValue ("DisplayName", null), "DisplayName,null");
  303. Assert.AreSame (String.Empty, item._ValidatePropertyValue ("DisplayName", String.Empty), "DisplayName,string");
  304. Assert.AreEqual (filter, item._ValidatePropertyValue ("Filter", null), "Filter,null");
  305. Assert.AreEqual (filter, item._ValidatePropertyValue ("Filter", filter), "Filter,ToolboxItemFilterAttribute[]");
  306. //Assert.IsFalse (Object.ReferenceEquals (filter, item._ValidatePropertyValue ("Filter", filter)), "Filter,ToolboxItemFilterAttribute[]/Reference");
  307. Assert.AreEqual (String.Empty, item._ValidatePropertyValue ("TypeName", null), "TypeName,null");
  308. Assert.AreSame (String.Empty, item._ValidatePropertyValue ("TypeName", String.Empty), "TypeName,string");
  309. Assert.AreEqual (String.Empty, item._ValidatePropertyValue ("Company", null), "Company,null");
  310. Assert.AreSame (String.Empty, item._ValidatePropertyValue ("Company", String.Empty), "Company,string");
  311. Assert.AreEqual (null, item._ValidatePropertyValue ("DependentAssemblies", null), "DependentAssemblies,null");
  312. Assert.AreEqual (dependent, item._ValidatePropertyValue ("DependentAssemblies", dependent), "DependentAssemblies,AssemblyName[]");
  313. Assert.IsTrue (Object.ReferenceEquals (dependent, item._ValidatePropertyValue ("DependentAssemblies", dependent)), "DependentAssemblies,AssemblyName[]/Reference");
  314. Assert.AreEqual (String.Empty, item._ValidatePropertyValue ("Description", null), "Description,null");
  315. Assert.AreSame (String.Empty, item._ValidatePropertyValue ("Description", String.Empty), "Description,string");
  316. Assert.IsTrue ((bool)item._ValidatePropertyValue ("IsTransient", true), "IsTransient,true");
  317. Assert.IsFalse ((bool)item._ValidatePropertyValue ("IsTransient", false), "IsTransient,false");
  318. }
  319. [Test]
  320. [ExpectedException (typeof (ArgumentException))]
  321. public void ValidatePropertyValue_AssemblyName_WrongType ()
  322. {
  323. OurToolboxItem item = new OurToolboxItem ();
  324. item._ValidatePropertyValue ("AssemblyName", false);
  325. }
  326. [Test]
  327. [ExpectedException (typeof (ArgumentException))]
  328. public void ValidatePropertyValue_Bitmap_WrongType ()
  329. {
  330. OurToolboxItem item = new OurToolboxItem ();
  331. item._ValidatePropertyValue ("Bitmap", false);
  332. }
  333. [Test]
  334. [ExpectedException (typeof (ArgumentException))]
  335. public void ValidatePropertyValue_DisplayName_WrongType ()
  336. {
  337. OurToolboxItem item = new OurToolboxItem ();
  338. item._ValidatePropertyValue ("DisplayName", false);
  339. }
  340. [Test]
  341. [ExpectedException (typeof (ArgumentException))]
  342. public void ValidatePropertyValue_Filter_WrongType ()
  343. {
  344. OurToolboxItem item = new OurToolboxItem ();
  345. item._ValidatePropertyValue ("Filter", false);
  346. }
  347. [Test]
  348. [ExpectedException (typeof (ArgumentException))]
  349. public void ValidatePropertyValue_TypeName_WrongType ()
  350. {
  351. OurToolboxItem item = new OurToolboxItem ();
  352. item._ValidatePropertyValue ("TypeName", false);
  353. }
  354. [Test]
  355. [ExpectedException (typeof (ArgumentException))]
  356. public void ValidatePropertyValue_Company_WrongType ()
  357. {
  358. OurToolboxItem item = new OurToolboxItem ();
  359. item._ValidatePropertyValue ("Company", false);
  360. }
  361. [Test]
  362. [ExpectedException (typeof (ArgumentException))]
  363. public void ValidatePropertyValue_Description_WrongType ()
  364. {
  365. OurToolboxItem item = new OurToolboxItem ();
  366. item._ValidatePropertyValue ("Description", false);
  367. }
  368. [Test]
  369. [ExpectedException (typeof (ArgumentNullException))]
  370. public void ValidatePropertyValue_IsTransient_Null ()
  371. {
  372. // only documented case
  373. new OurToolboxItem ()._ValidatePropertyValue ("IsTransient", null);
  374. }
  375. [Test]
  376. [ExpectedException (typeof (ArgumentException))]
  377. public void ValidatePropertyValue_IsTransient_WrongType ()
  378. {
  379. // only documented case
  380. new OurToolboxItem ()._ValidatePropertyValue ("IsTransient", new object ());
  381. }
  382. #endif
  383. }
  384. }