PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Xaml/Test/System.Xaml/XamlTypeTest.cs

https://bitbucket.org/danipen/mono
C# | 899 lines | 768 code | 80 blank | 51 comment | 13 complexity | 8c052f42dc75c1c273580147962494d4 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. // Copyright (C) 2010 Novell Inc. http://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. using System;
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using System.ComponentModel;
  27. using System.Linq;
  28. using System.Reflection;
  29. using System.Windows.Markup;
  30. using System.Xaml;
  31. using System.Xaml.Schema;
  32. using NUnit.Framework;
  33. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  34. namespace MonoTests.System.Xaml
  35. {
  36. // FIXME: enable DeferringLoader tests.
  37. [TestFixture]
  38. public class XamlTypeTest
  39. {
  40. XamlSchemaContext sctx = new XamlSchemaContext (null, null);
  41. [Test]
  42. [ExpectedException (typeof (ArgumentNullException))]
  43. public void ConstructorTypeNullType ()
  44. {
  45. new XamlType (null, sctx);
  46. }
  47. [Test]
  48. [ExpectedException (typeof (ArgumentNullException))]
  49. public void ConstructorTypeNullSchemaContext ()
  50. {
  51. new XamlType (typeof (int), null);
  52. }
  53. [Test]
  54. public void ConstructorSimpleType ()
  55. {
  56. var t = new XamlType (typeof (int), sctx);
  57. Assert.AreEqual ("Int32", t.Name, "#1");
  58. Assert.AreEqual (typeof (int), t.UnderlyingType, "#2");
  59. Assert.IsNotNull (t.BaseType, "#3-1");
  60. // So, it is type aware. It's weird that t.Name still returns full name just as it is passed to the .ctor.
  61. Assert.AreEqual ("ValueType", t.BaseType.Name, "#3-2");
  62. Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", t.BaseType.PreferredXamlNamespace, "#3-3");
  63. // It is likely only for primitive types such as int.
  64. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, t.PreferredXamlNamespace, "#4");
  65. t = new XamlType (typeof (XamlXmlReader), sctx);
  66. Assert.AreEqual ("XamlXmlReader", t.Name, "#11");
  67. Assert.AreEqual (typeof (XamlXmlReader), t.UnderlyingType, "#12");
  68. Assert.IsNotNull (t.BaseType, "#13");
  69. Assert.AreEqual (typeof (XamlReader), t.BaseType.UnderlyingType, "#13-2");
  70. Assert.AreEqual ("clr-namespace:System.Xaml;assembly=System.Xaml", t.BaseType.PreferredXamlNamespace, "#13-3");
  71. Assert.AreEqual ("clr-namespace:System.Xaml;assembly=System.Xaml", t.PreferredXamlNamespace, "#14");
  72. }
  73. [Test]
  74. public void ConstructorNullTypeInvoker ()
  75. {
  76. // allowed.
  77. new XamlType (typeof (int), sctx, null);
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentNullException))]
  81. public void ConstructorNamesNullName ()
  82. {
  83. new XamlType (String.Empty, null, null, sctx);
  84. }
  85. [Test]
  86. [ExpectedException (typeof (ArgumentNullException))]
  87. public void ConstructorNamesNullSchemaContext ()
  88. {
  89. new XamlType ("System", "Int32", null, null);
  90. }
  91. [Test]
  92. public void ConstructorNames ()
  93. {
  94. // null typeArguments is allowed.
  95. new XamlType ("System", "Int32", null, sctx);
  96. }
  97. [Test]
  98. [ExpectedException (typeof (ArgumentNullException))]
  99. public void ConstructorNameNullName ()
  100. {
  101. new MyXamlType (null, null, sctx);
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentNullException))]
  105. public void ConstructorNameNullSchemaContext ()
  106. {
  107. new MyXamlType ("System.Int32", null, null);
  108. }
  109. [Test]
  110. public void ConstructorNameInvalid ()
  111. {
  112. // ... all allowed.
  113. new XamlType (String.Empty, ".", null, sctx);
  114. new XamlType (String.Empty, "<>", null, sctx);
  115. new XamlType (String.Empty, "", null, sctx);
  116. }
  117. [Test]
  118. public void ConstructorNameWithFullName ()
  119. {
  120. // null typeArguments is allowed.
  121. var t = new MyXamlType ("System.Int32", null, sctx);
  122. Assert.AreEqual ("System.Int32", t.Name, "#1");
  123. Assert.IsNull (t.UnderlyingType, "#2");
  124. Assert.IsNotNull (t.BaseType, "#3-1");
  125. // So, it is type aware. It's weird that t.Name still returns full name just as it is passed to the .ctor.
  126. Assert.AreEqual ("Object", t.BaseType.Name, "#3-2");
  127. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, t.BaseType.PreferredXamlNamespace, "#3-3");
  128. Assert.IsNull (t.BaseType.BaseType, "#3-4");
  129. Assert.AreEqual (String.Empty, t.PreferredXamlNamespace, "#4");
  130. Assert.IsFalse (t.IsArray, "#5");
  131. Assert.IsFalse (t.IsGeneric, "#6");
  132. Assert.IsTrue (t.IsPublic, "#7");
  133. Assert.AreEqual (0, t.GetAllMembers ().Count, "#8");
  134. }
  135. [Test]
  136. public void NoSuchTypeByName ()
  137. {
  138. var t = new MyXamlType ("System.NoSuchType", null, sctx);
  139. Assert.AreEqual ("System.NoSuchType", t.Name, "#1");
  140. Assert.IsNull (t.UnderlyingType, "#2");
  141. Assert.IsNotNull (t.BaseType, "#3-1");
  142. // So, it is type aware. It's weird that t.Name still returns full name just as it is passed to the .ctor.
  143. Assert.AreEqual ("Object", t.BaseType.Name, "#3-2");
  144. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, t.BaseType.PreferredXamlNamespace, "#3-3");
  145. Assert.AreEqual (String.Empty, t.PreferredXamlNamespace, "#4");
  146. }
  147. [Test]
  148. public void NoSuchTypeByNames ()
  149. {
  150. var t = new XamlType ("urn:foo", "System.NoSuchType", null, sctx);
  151. Assert.AreEqual ("System.NoSuchType", t.Name, "#1");
  152. Assert.IsNull (t.UnderlyingType, "#2");
  153. Assert.IsNotNull (t.BaseType, "#3-1");
  154. // So, it is type aware. It's weird that t.Name still returns full name just as it is passed to the .ctor.
  155. Assert.AreEqual ("Object", t.BaseType.Name, "#3-2");
  156. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, t.BaseType.PreferredXamlNamespace, "#3-3");
  157. Assert.AreEqual ("urn:foo", t.PreferredXamlNamespace, "#4");
  158. }
  159. [Test]
  160. [Ignore ("It results in NRE on .NET 4.0 RTM")]
  161. public void EmptyTypeArguments ()
  162. {
  163. var t1 = new MyXamlType ("System.Int32", null, sctx);
  164. var t2 = new MyXamlType ("System.Int32", new XamlType [0], sctx);
  165. Assert.IsTrue (t1 == t2, "#1");
  166. Assert.IsTrue (t1.Equals (t2), "#2");
  167. }
  168. [Test]
  169. public void EmptyTypeArguments2 ()
  170. {
  171. var t1 = new XamlType ("System", "Int32", null, sctx);
  172. var t2 = new XamlType ("System", "Int32", new XamlType [0], sctx);
  173. Assert.IsNull (t1.TypeArguments, "#1");
  174. Assert.IsNull (t2.TypeArguments, "#2");
  175. Assert.IsTrue (t1 == t2, "#3");
  176. Assert.IsTrue (t1.Equals (t2), "#4");
  177. }
  178. [Test]
  179. public void EqualityAcrossConstructors ()
  180. {
  181. var t1 = new XamlType (typeof (int), sctx);
  182. var t2 = new XamlType (t1.PreferredXamlNamespace, t1.Name, null, sctx);
  183. // not sure if it always returns false for different .ctor comparisons...
  184. Assert.IsFalse (t1 == t2, "#3");
  185. Assert.AreNotEqual (XamlLanguage.Type, new XamlSchemaContext ().GetXamlType (typeof (Type)), "#4");
  186. }
  187. [Test]
  188. public void ArrayAndCollection ()
  189. {
  190. var t = new XamlType (typeof (int), sctx);
  191. Assert.IsFalse (t.IsArray, "#1.1");
  192. Assert.IsFalse (t.IsCollection, "#1.2");
  193. Assert.IsNull (t.ItemType, "#1.3");
  194. t = new XamlType (typeof (ArrayList), sctx);
  195. Assert.IsFalse (t.IsArray, "#2.1");
  196. Assert.IsTrue (t.IsCollection, "#2.2");
  197. Assert.IsNotNull (t.ItemType, "#2.3");
  198. Assert.AreEqual ("Object", t.ItemType.Name, "#2.4");
  199. t = new XamlType (typeof (int []), sctx);
  200. Assert.IsTrue (t.IsArray, "#3.1");
  201. Assert.IsFalse (t.IsCollection, "#3.2");
  202. Assert.IsNotNull (t.ItemType, "#3.3");
  203. Assert.AreEqual (typeof (int), t.ItemType.UnderlyingType, "#3.4");
  204. t = new XamlType (typeof (IList), sctx);
  205. Assert.IsFalse (t.IsArray, "#4.1");
  206. Assert.IsTrue (t.IsCollection, "#4.2");
  207. Assert.IsNotNull (t.ItemType, "#4.3");
  208. Assert.AreEqual (typeof (object), t.ItemType.UnderlyingType, "#4.4");
  209. t = new XamlType (typeof (ICollection), sctx); // it is not a XAML collection.
  210. Assert.IsFalse (t.IsArray, "#5.1");
  211. Assert.IsFalse (t.IsCollection, "#5.2");
  212. Assert.IsNull (t.ItemType, "#5.3");
  213. t = new XamlType (typeof (ArrayExtension), sctx);
  214. Assert.IsFalse (t.IsArray, "#6.1");
  215. Assert.IsFalse (t.IsCollection, "#6.2");
  216. Assert.IsNull (t.ItemType, "#6.3");
  217. }
  218. [Test]
  219. public void Dictionary ()
  220. {
  221. var t = new XamlType (typeof (int), sctx);
  222. Assert.IsFalse (t.IsDictionary, "#1.1");
  223. Assert.IsFalse (t.IsCollection, "#1.1-2");
  224. Assert.IsNull (t.KeyType, "#1.2");
  225. t = new XamlType (typeof (Hashtable), sctx);
  226. Assert.IsTrue (t.IsDictionary, "#2.1");
  227. Assert.IsFalse (t.IsCollection, "#2.1-2");
  228. Assert.IsNotNull (t.KeyType, "#2.2");
  229. Assert.IsNotNull (t.ItemType, "#2.2-2");
  230. Assert.AreEqual ("Object", t.KeyType.Name, "#2.3");
  231. Assert.AreEqual ("Object", t.ItemType.Name, "#2.3-2");
  232. t = new XamlType (typeof (Dictionary<int,string>), sctx);
  233. Assert.IsTrue (t.IsDictionary, "#3.1");
  234. Assert.IsFalse (t.IsCollection, "#3.1-2");
  235. Assert.IsNotNull (t.KeyType, "#3.2");
  236. Assert.IsNotNull (t.ItemType, "#3.2-2");
  237. Assert.AreEqual ("Int32", t.KeyType.Name, "#3.3");
  238. Assert.AreEqual ("String", t.ItemType.Name, "#3.3-2");
  239. var ml = t.GetAllMembers ();
  240. Assert.AreEqual (2, ml.Count, "#3.4");
  241. Assert.IsTrue (ml.Any (mi => mi.Name == "Keys"), "#3.4-2");
  242. Assert.IsTrue (ml.Any (mi => mi.Name == "Values"), "#3.4-3");
  243. Assert.IsNotNull (t.GetMember ("Keys"), "#3.4-4");
  244. Assert.IsNotNull (t.GetMember ("Values"), "#3.4-5");
  245. }
  246. public class TestClass1
  247. {
  248. }
  249. class TestClass2
  250. {
  251. internal TestClass2 () {}
  252. }
  253. [Test]
  254. public void IsConstructible ()
  255. {
  256. // ... is it?
  257. Assert.IsTrue (new XamlType (typeof (int), sctx).IsConstructible, "#1");
  258. // ... is it?
  259. Assert.IsFalse (new XamlType (typeof (TestClass1), sctx).IsConstructible, "#2");
  260. Assert.IsFalse (new XamlType (typeof (TestClass2), sctx).IsConstructible, "#3");
  261. Assert.IsTrue (new XamlType (typeof (object), sctx).IsConstructible, "#4");
  262. }
  263. class AttachableClass
  264. {
  265. public event EventHandler<EventArgs> SimpleEvent;
  266. public void AddSimpleHandler (object o, EventHandler h)
  267. {
  268. }
  269. }
  270. // hmm, what can we use to verify this method?
  271. [Test]
  272. public void GetAllAttachableMembers ()
  273. {
  274. var xt = new XamlType (typeof (AttachableClass), sctx);
  275. var l = xt.GetAllAttachableMembers ();
  276. Assert.AreEqual (0, l.Count, "#1");
  277. }
  278. [Test]
  279. public void DefaultValuesType ()
  280. {
  281. var t = new XamlType (typeof (int), sctx);
  282. Assert.IsNotNull (t.Invoker, "#1");
  283. Assert.IsTrue (t.IsNameValid, "#2");
  284. Assert.IsFalse (t.IsUnknown, "#3");
  285. Assert.AreEqual ("Int32", t.Name, "#4");
  286. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, t.PreferredXamlNamespace, "#5");
  287. Assert.IsNull (t.TypeArguments, "#6");
  288. Assert.AreEqual (typeof (int), t.UnderlyingType, "#7");
  289. Assert.IsFalse (t.ConstructionRequiresArguments, "#8");
  290. Assert.IsFalse (t.IsArray, "#9");
  291. Assert.IsFalse (t.IsCollection, "#10");
  292. Assert.IsTrue (t.IsConstructible, "#11");
  293. Assert.IsFalse (t.IsDictionary, "#12");
  294. Assert.IsFalse (t.IsGeneric, "#13");
  295. Assert.IsFalse (t.IsMarkupExtension, "#14");
  296. Assert.IsFalse (t.IsNameScope, "#15");
  297. Assert.IsFalse (t.IsNullable, "#16");
  298. Assert.IsTrue (t.IsPublic, "#17");
  299. Assert.IsFalse (t.IsUsableDuringInitialization, "#18");
  300. Assert.IsFalse (t.IsWhitespaceSignificantCollection, "#19");
  301. Assert.IsFalse (t.IsXData, "#20");
  302. Assert.IsFalse (t.TrimSurroundingWhitespace, "#21");
  303. Assert.IsFalse (t.IsAmbient, "#22");
  304. Assert.IsNull (t.AllowedContentTypes, "#23");
  305. Assert.IsNull (t.ContentWrappers, "#24");
  306. Assert.IsNotNull (t.TypeConverter, "#25");
  307. Assert.IsTrue (t.TypeConverter.ConverterInstance is Int32Converter, "#25-2");
  308. Assert.IsNull (t.ValueSerializer, "#26");
  309. Assert.IsNull (t.ContentProperty, "#27");
  310. //Assert.IsNull (t.DeferringLoader, "#28");
  311. Assert.IsNull (t.MarkupExtensionReturnType, "#29");
  312. Assert.AreEqual (sctx, t.SchemaContext, "#30");
  313. }
  314. [Test]
  315. public void DefaultValuesType2 ()
  316. {
  317. var t = new XamlType (typeof (Type), sctx);
  318. Assert.IsNotNull (t.Invoker, "#1");
  319. Assert.IsTrue (t.IsNameValid, "#2");
  320. Assert.IsFalse (t.IsUnknown, "#3");
  321. Assert.AreEqual ("Type", t.Name, "#4");
  322. // Note that Type is not a standard type. An instance of System.Type is usually represented as TypeExtension.
  323. Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", t.PreferredXamlNamespace, "#5");
  324. Assert.IsNull (t.TypeArguments, "#6");
  325. Assert.AreEqual (typeof (Type), t.UnderlyingType, "#7");
  326. Assert.IsTrue (t.ConstructionRequiresArguments, "#8"); // yes, true.
  327. Assert.IsFalse (t.IsArray, "#9");
  328. Assert.IsFalse (t.IsCollection, "#10");
  329. Assert.IsFalse (t.IsConstructible, "#11"); // yes, false.
  330. Assert.IsFalse (t.IsDictionary, "#12");
  331. Assert.IsFalse (t.IsGeneric, "#13");
  332. Assert.IsFalse (t.IsMarkupExtension, "#14");
  333. Assert.IsFalse (t.IsNameScope, "#15");
  334. Assert.IsTrue (t.IsNullable, "#16");
  335. Assert.IsTrue (t.IsPublic, "#17");
  336. Assert.IsFalse (t.IsUsableDuringInitialization, "#18");
  337. Assert.IsFalse (t.IsWhitespaceSignificantCollection, "#19");
  338. Assert.IsFalse (t.IsXData, "#20");
  339. Assert.IsFalse (t.TrimSurroundingWhitespace, "#21");
  340. Assert.IsFalse (t.IsAmbient, "#22");
  341. Assert.IsNull (t.AllowedContentTypes, "#23");
  342. Assert.IsNull (t.ContentWrappers, "#24");
  343. Assert.IsNotNull (t.TypeConverter, "#25"); // TypeTypeConverter
  344. Assert.IsNull (t.ValueSerializer, "#26");
  345. Assert.IsNull (t.ContentProperty, "#27");
  346. //Assert.IsNull (t.DeferringLoader, "#28");
  347. Assert.IsNull (t.MarkupExtensionReturnType, "#29");
  348. Assert.AreEqual (sctx, t.SchemaContext, "#30");
  349. }
  350. [Test]
  351. public void DefaultValuesName ()
  352. {
  353. var t = new XamlType ("urn:foo", ".", null, sctx);
  354. Assert.IsNotNull (t.Invoker, "#1");
  355. Assert.IsFalse (t.IsNameValid, "#2");
  356. Assert.IsTrue (t.IsUnknown, "#3");
  357. Assert.AreEqual (".", t.Name, "#4");
  358. Assert.AreEqual ("urn:foo", t.PreferredXamlNamespace, "#5");
  359. Assert.IsNull (t.TypeArguments, "#6");
  360. Assert.IsNull (t.UnderlyingType, "#7");
  361. Assert.IsFalse (t.ConstructionRequiresArguments, "#8");
  362. Assert.IsFalse (t.IsArray, "#9");
  363. Assert.IsFalse (t.IsCollection, "#10");
  364. Assert.IsTrue (t.IsConstructible, "#11");
  365. Assert.IsFalse (t.IsDictionary, "#12");
  366. Assert.IsFalse (t.IsGeneric, "#13");
  367. Assert.IsFalse (t.IsMarkupExtension, "#14");
  368. Assert.IsFalse (t.IsNameScope, "#15");
  369. Assert.IsTrue (t.IsNullable, "#16"); // different from int
  370. Assert.IsTrue (t.IsPublic, "#17");
  371. Assert.IsFalse (t.IsUsableDuringInitialization, "#18");
  372. Assert.IsTrue (t.IsWhitespaceSignificantCollection, "#19"); // somehow true ...
  373. Assert.IsFalse (t.IsXData, "#20");
  374. Assert.IsFalse (t.TrimSurroundingWhitespace, "#21");
  375. Assert.IsFalse (t.IsAmbient, "#22");
  376. Assert.IsNull (t.AllowedContentTypes, "#23");
  377. Assert.IsNull (t.ContentWrappers, "#24");
  378. Assert.IsNull (t.TypeConverter, "#25");
  379. Assert.IsNull (t.ValueSerializer, "#26");
  380. Assert.IsNull (t.ContentProperty, "#27");
  381. //Assert.IsNull (t.DeferringLoader, "#28");
  382. Assert.IsNull (t.MarkupExtensionReturnType, "#29");
  383. Assert.AreEqual (sctx, t.SchemaContext, "#30");
  384. }
  385. [Test]
  386. public void DefaultValuesCustomType ()
  387. {
  388. var t = new MyXamlType ("System.Int32", null, sctx);
  389. Assert.IsNotNull (t.Invoker, "#1");
  390. Assert.IsFalse (t.IsNameValid, "#2");
  391. Assert.IsTrue (t.IsUnknown, "#3");
  392. Assert.AreEqual ("System.Int32", t.Name, "#4");
  393. Assert.AreEqual (String.Empty, t.PreferredXamlNamespace, "#5");
  394. Assert.IsNull (t.TypeArguments, "#6");
  395. Assert.IsNull (t.UnderlyingType, "#7");
  396. Assert.IsFalse (t.ConstructionRequiresArguments, "#8");
  397. Assert.IsFalse (t.IsArray, "#9");
  398. Assert.IsFalse (t.IsCollection, "#10");
  399. Assert.IsTrue (t.IsConstructible, "#11");
  400. Assert.IsFalse (t.IsDictionary, "#12");
  401. Assert.IsFalse (t.IsGeneric, "#13");
  402. Assert.IsFalse (t.IsMarkupExtension, "#14");
  403. Assert.IsFalse (t.IsNameScope, "#15");
  404. Assert.IsTrue (t.IsNullable, "#16"); // different from int
  405. Assert.IsTrue (t.IsPublic, "#17");
  406. Assert.IsFalse (t.IsUsableDuringInitialization, "#18");
  407. Assert.IsTrue (t.IsWhitespaceSignificantCollection, "#19"); // somehow true ...
  408. Assert.IsFalse (t.IsXData, "#20");
  409. Assert.IsFalse (t.TrimSurroundingWhitespace, "#21");
  410. Assert.IsFalse (t.IsAmbient, "#22");
  411. Assert.IsNull (t.AllowedContentTypes, "#23");
  412. Assert.IsNull (t.ContentWrappers, "#24");
  413. Assert.IsNull (t.TypeConverter, "#25");
  414. Assert.IsNull (t.ValueSerializer, "#26");
  415. Assert.IsNull (t.ContentProperty, "#27");
  416. //Assert.IsNull (t.DeferringLoader, "#28");
  417. Assert.IsNull (t.MarkupExtensionReturnType, "#29");
  418. Assert.AreEqual (sctx, t.SchemaContext, "#30");
  419. }
  420. [Ambient]
  421. [ContentProperty ("Name")]
  422. [WhitespaceSignificantCollection]
  423. [UsableDuringInitialization (true)]
  424. public class TestClass3
  425. {
  426. public TestClass3 (string name)
  427. {
  428. Name = name;
  429. }
  430. public string Name { get; set; }
  431. }
  432. [Test]
  433. public void DefaultValuesSeverlyAttributed ()
  434. {
  435. var t = new XamlType (typeof (TestClass3), sctx);
  436. Assert.IsNotNull (t.Invoker, "#1");
  437. Assert.IsFalse (t.IsNameValid, "#2"); // see #4
  438. Assert.IsFalse (t.IsUnknown, "#3");
  439. Assert.AreEqual ("XamlTypeTest+TestClass3", t.Name, "#4");
  440. Assert.AreEqual ("clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name, t.PreferredXamlNamespace, "#5");
  441. Assert.IsNull (t.TypeArguments, "#6");
  442. Assert.AreEqual (typeof (TestClass3), t.UnderlyingType, "#7");
  443. Assert.IsTrue (t.ConstructionRequiresArguments, "#8");
  444. Assert.IsFalse (t.IsArray, "#9");
  445. Assert.IsFalse (t.IsCollection, "#10");
  446. Assert.IsFalse (t.IsConstructible, "#11");
  447. Assert.IsFalse (t.IsDictionary, "#12");
  448. Assert.IsFalse (t.IsGeneric, "#13");
  449. Assert.IsFalse (t.IsMarkupExtension, "#14");
  450. Assert.IsFalse (t.IsNameScope, "#15");
  451. Assert.IsTrue (t.IsNullable, "#16");
  452. Assert.IsTrue (t.IsPublic, "#17");
  453. Assert.IsTrue (t.IsUsableDuringInitialization, "#18");
  454. Assert.IsTrue (t.IsWhitespaceSignificantCollection, "#19");
  455. Assert.IsFalse (t.IsXData, "#20");
  456. Assert.IsFalse (t.TrimSurroundingWhitespace, "#21");
  457. Assert.IsTrue (t.IsAmbient, "#22");
  458. Assert.IsNull (t.AllowedContentTypes, "#23");
  459. Assert.IsNull (t.ContentWrappers, "#24");
  460. Assert.IsNull (t.TypeConverter, "#25");
  461. Assert.IsNull (t.ValueSerializer, "#26");
  462. Assert.IsNotNull (t.ContentProperty, "#27");
  463. Assert.AreEqual ("Name", t.ContentProperty.Name, "#27-2");
  464. // Assert.IsNull (t.DeferringLoader, "#28");
  465. Assert.IsNull (t.MarkupExtensionReturnType, "#29");
  466. Assert.AreEqual (sctx, t.SchemaContext, "#30");
  467. }
  468. [Test]
  469. public void DefaultValuesArgumentAttributed ()
  470. {
  471. var t = new XamlType (typeof (ArgumentAttributed), sctx);
  472. Assert.IsNotNull (t.Invoker, "#1");
  473. Assert.IsTrue (t.IsNameValid, "#2");
  474. Assert.IsFalse (t.IsUnknown, "#3");
  475. Assert.AreEqual ("ArgumentAttributed", t.Name, "#4");
  476. Assert.AreEqual ("clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name, t.PreferredXamlNamespace, "#5");
  477. Assert.IsNull (t.TypeArguments, "#6");
  478. Assert.AreEqual (typeof (ArgumentAttributed), t.UnderlyingType, "#7");
  479. Assert.IsTrue (t.ConstructionRequiresArguments, "#8");
  480. Assert.IsFalse (t.IsArray, "#9");
  481. Assert.IsFalse (t.IsCollection, "#10");
  482. Assert.IsTrue (t.IsConstructible, "#11");
  483. Assert.IsFalse (t.IsDictionary, "#12");
  484. Assert.IsFalse (t.IsGeneric, "#13");
  485. Assert.IsFalse (t.IsMarkupExtension, "#14");
  486. Assert.IsFalse (t.IsNameScope, "#15");
  487. Assert.IsTrue (t.IsNullable, "#16");
  488. Assert.IsTrue (t.IsPublic, "#17");
  489. Assert.IsFalse (t.IsUsableDuringInitialization, "#18");
  490. Assert.IsFalse (t.IsWhitespaceSignificantCollection, "#19");
  491. Assert.IsFalse (t.IsXData, "#20");
  492. Assert.IsFalse (t.TrimSurroundingWhitespace, "#21");
  493. Assert.IsFalse (t.IsAmbient, "#22");
  494. Assert.IsNull (t.AllowedContentTypes, "#23");
  495. Assert.IsNull (t.ContentWrappers, "#24");
  496. Assert.IsNull (t.TypeConverter, "#25");
  497. Assert.IsNull (t.ValueSerializer, "#26");
  498. Assert.IsNull (t.ContentProperty, "#27");
  499. // Assert.IsNull (t.DeferringLoader, "#28");
  500. Assert.IsNull (t.MarkupExtensionReturnType, "#29");
  501. Assert.AreEqual (sctx, t.SchemaContext, "#30");
  502. var members = t.GetAllMembers ();
  503. Assert.AreEqual (2, members.Count, "#31");
  504. string [] names = {"Arg1", "Arg2"};
  505. foreach (var member in members)
  506. Assert.IsTrue (Array.IndexOf (names, member.Name) >= 0, "#32");
  507. }
  508. [Test]
  509. public void TypeConverter ()
  510. {
  511. Assert.IsNull (new XamlType (typeof (List<object>), sctx).TypeConverter, "#1");
  512. Assert.IsNotNull (new XamlType (typeof (object), sctx).TypeConverter, "#2");
  513. Assert.IsTrue (new XamlType (typeof (Uri), sctx).TypeConverter.ConverterInstance is UriTypeConverter, "#3");
  514. Assert.IsTrue (new XamlType (typeof (TimeSpan), sctx).TypeConverter.ConverterInstance is TimeSpanConverter, "#4");
  515. Assert.IsNull (new XamlType (typeof (XamlType), sctx).TypeConverter, "#5");
  516. Assert.IsTrue (new XamlType (typeof (char), sctx).TypeConverter.ConverterInstance is CharConverter, "#6");
  517. }
  518. [Test]
  519. public void TypeConverter_Type ()
  520. {
  521. TypeConveter_TypeOrTypeExtension (typeof (Type));
  522. }
  523. [Test]
  524. public void TypeConverter_TypeExtension ()
  525. {
  526. TypeConveter_TypeOrTypeExtension (typeof (TypeExtension));
  527. }
  528. void TypeConveter_TypeOrTypeExtension (Type type)
  529. {
  530. var xtc = new XamlType (type, sctx).TypeConverter;
  531. Assert.IsNotNull (xtc, "#7");
  532. var tc = xtc.ConverterInstance;
  533. Assert.IsNotNull (tc, "#7-2");
  534. Assert.IsFalse (tc.CanConvertTo (typeof (Type)), "#7-3");
  535. Assert.IsFalse (tc.CanConvertTo (typeof (XamlType)), "#7-4");
  536. Assert.IsTrue (tc.CanConvertTo (typeof (string)), "#7-5");
  537. Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}TypeExtension", tc.ConvertToString (XamlLanguage.Type), "#7-6");
  538. Assert.IsFalse (tc.CanConvertFrom (typeof (Type)), "#7-7");
  539. Assert.IsFalse (tc.CanConvertFrom (typeof (XamlType)), "#7-8");
  540. // .NET returns true for type == typeof(Type) case here, which does not make sense. Disabling it now.
  541. //Assert.IsFalse (tc.CanConvertFrom (typeof (string)), "#7-9");
  542. try {
  543. tc.ConvertFromString ("{http://schemas.microsoft.com/winfx/2006/xaml}TypeExtension");
  544. Assert.Fail ("failure");
  545. } catch (NotSupportedException) {
  546. }
  547. }
  548. [Test]
  549. [Category ("NotWorking")]
  550. public void GetXamlNamespaces ()
  551. {
  552. var xt = new XamlType (typeof (string), new XamlSchemaContext (null, null));
  553. var l = xt.GetXamlNamespaces ().ToList ();
  554. l.Sort ();
  555. Assert.AreEqual (2, l.Count, "#1-1");
  556. Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", l [0], "#1-2");
  557. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, l [1], "#1-3");
  558. xt = new XamlType (typeof (TypeExtension), new XamlSchemaContext (null, null));
  559. l = xt.GetXamlNamespaces ().ToList ();
  560. l.Sort ();
  561. Assert.AreEqual (3, l.Count, "#2-1");
  562. Assert.AreEqual ("clr-namespace:System.Windows.Markup;assembly=System.Xaml", l [0], "#2-2");
  563. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, l [1], "#2-3");
  564. Assert.AreEqual (XamlLanguage.Xaml2006Namespace, l [2], "#2-4"); // ??
  565. }
  566. [Test]
  567. public void GetAliasedProperty ()
  568. {
  569. XamlMember xm;
  570. var xt = new XamlType (typeof (SeverlyAliasedClass), new XamlSchemaContext (null, null));
  571. xm = xt.GetAliasedProperty (XamlLanguage.Key);
  572. Assert.IsNotNull (xm, "#1");
  573. xm = xt.GetAliasedProperty (XamlLanguage.Name);
  574. Assert.IsNotNull (xm, "#2");
  575. xm = xt.GetAliasedProperty (XamlLanguage.Uid);
  576. Assert.IsNotNull (xm, "#3");
  577. xm = xt.GetAliasedProperty (XamlLanguage.Lang);
  578. Assert.IsNotNull (xm, "#4");
  579. xt = new XamlType (typeof (Dictionary<int,string>), xt.SchemaContext);
  580. Assert.IsNull (xt.GetAliasedProperty (XamlLanguage.Key), "#5");
  581. }
  582. [Test]
  583. public void GetAliasedPropertyOnAllTypes ()
  584. {
  585. foreach (var xt in XamlLanguage.AllTypes)
  586. foreach (var xd in XamlLanguage.AllDirectives)
  587. Assert.IsNull (xt.GetAliasedProperty (xd), xt.Name + " and " + xd.Name);
  588. }
  589. [DictionaryKeyProperty ("Key")]
  590. [RuntimeNameProperty ("RuntimeTypeName")]
  591. [UidProperty ("UUID")]
  592. [XmlLangProperty ("XmlLang")]
  593. public class SeverlyAliasedClass
  594. {
  595. public string Key { get; set; }
  596. public string RuntimeTypeName { get; set; }
  597. public string UUID { get; set; }
  598. public string XmlLang { get; set; }
  599. }
  600. [Test]
  601. public void ToStringTest ()
  602. {
  603. Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}String", XamlLanguage.String.ToString (), "#1");
  604. Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}TypeExtension", XamlLanguage.Type.ToString (), "#2");
  605. Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}ArrayExtension", XamlLanguage.Array.ToString (), "#3");
  606. }
  607. [Test]
  608. public void GetPositionalParameters ()
  609. {
  610. IList<XamlType> l;
  611. l = XamlLanguage.Type.GetPositionalParameters (1);
  612. Assert.IsNotNull (l, "#1");
  613. Assert.AreEqual (1, l.Count, "#2");
  614. Assert.AreEqual (typeof (Type), l [0].UnderlyingType, "#3"); // not TypeExtension but Type.
  615. Assert.AreEqual ("Type", l [0].Name, "#4");
  616. }
  617. [Test]
  618. public void GetPositionalParametersWrongCount ()
  619. {
  620. Assert.IsNull (XamlLanguage.Type.GetPositionalParameters (2), "#1");
  621. }
  622. [Test]
  623. public void GetPositionalParametersNoMemberExtension ()
  624. {
  625. // wow, so it returns some meaningless method parameters.
  626. Assert.IsNotNull (new XamlType (typeof (MyXamlType), sctx).GetPositionalParameters (3), "#1");
  627. }
  628. [Test]
  629. public void ListMembers ()
  630. {
  631. var xt = new XamlType (typeof (List<int>), sctx);
  632. var ml = xt.GetAllMembers ().ToArray ();
  633. Assert.AreEqual (1, ml.Length, "#1");
  634. Assert.IsNotNull (xt.GetMember ("Capacity"), "#2");
  635. }
  636. [Test]
  637. public void ComplexPositionalParameters ()
  638. {
  639. var xt = new XamlType (typeof (ComplexPositionalParameterWrapper), sctx);
  640. }
  641. [Test]
  642. public void CustomArrayExtension ()
  643. {
  644. var xt = new XamlType (typeof (MyArrayExtension), sctx);
  645. var xm = xt.GetMember ("Items");
  646. Assert.IsNotNull (xt.GetAllMembers ().FirstOrDefault (m => m.Name == "Items"), "#0");
  647. Assert.IsNotNull (xm, "#1");
  648. Assert.IsFalse (xm.IsReadOnly, "#2"); // Surprisingly it is False. Looks like XAML ReadOnly is true only if it lacks set accessor. Having private member does not make it ReadOnly.
  649. Assert.IsTrue (xm.Type.IsCollection, "#3");
  650. Assert.IsFalse (xm.Type.IsConstructible, "#4");
  651. }
  652. [Test]
  653. public void ContentIncluded ()
  654. {
  655. var xt = new XamlType (typeof (ContentIncludedClass), sctx);
  656. var xm = xt.GetMember ("Content");
  657. Assert.AreEqual (xm, xt.ContentProperty, "#1");
  658. Assert.IsTrue (xt.GetAllMembers ().Contains (xm), "#2");
  659. }
  660. [Test]
  661. public void NamedItem ()
  662. {
  663. var xt = new XamlType (typeof (NamedItem), sctx);
  664. var e = xt.GetAllMembers ().GetEnumerator ();
  665. Assert.IsTrue (e.MoveNext (), "#1");
  666. Assert.AreEqual (xt.GetMember ("ItemName"), e.Current, "#2");
  667. Assert.IsTrue (e.MoveNext (), "#3");
  668. Assert.AreEqual (xt.GetMember ("References"), e.Current, "#4");
  669. Assert.IsFalse (e.MoveNext (), "#5");
  670. }
  671. [Test]
  672. public void CanAssignTo ()
  673. {
  674. foreach (var xt1 in XamlLanguage.AllTypes)
  675. foreach (var xt2 in XamlLanguage.AllTypes)
  676. Assert.AreEqual (xt1.UnderlyingType.IsAssignableFrom (xt2.UnderlyingType), xt2.CanAssignTo (xt1), "{0} to {1}", xt1, xt2);
  677. Assert.IsTrue (XamlLanguage.Type.CanAssignTo (XamlLanguage.Object), "x#1"); // specific test
  678. Assert.IsFalse (new MyXamlType ("MyFooBar", null, sctx).CanAssignTo (XamlLanguage.String), "x#2"); // custom type to string -> false
  679. Assert.IsTrue (new MyXamlType ("MyFooBar", null, sctx).CanAssignTo (XamlLanguage.Object), "x#3"); // custom type to object -> true!
  680. }
  681. [Test]
  682. public void IsXData ()
  683. {
  684. Assert.IsFalse (XamlLanguage.XData.IsXData, "#1"); // yes, it is false.
  685. Assert.IsTrue (sctx.GetXamlType (typeof (XmlSerializable)).IsXData, "#2");
  686. }
  687. [Test]
  688. public void XDataMembers ()
  689. {
  690. var xt = sctx.GetXamlType (typeof (XmlSerializableWrapper));
  691. Assert.IsNotNull (xt.GetMember ("Value"), "#1"); // it is read-only, so if wouldn't be retrieved if it were not XData.
  692. Assert.IsNotNull (XamlLanguage.XData.GetMember ("XmlReader"), "#2"); // it is returned, but ignored by XamlObjectReader.
  693. Assert.IsNotNull (XamlLanguage.XData.GetMember ("Text"), "#3");
  694. }
  695. [Test]
  696. public void AttachableProperty ()
  697. {
  698. var xt = new XamlType (typeof (Attachable), sctx);
  699. var apl = xt.GetAllAttachableMembers ();
  700. Assert.IsTrue (apl.Any (ap => ap.Name == "Foo"), "#1");
  701. Assert.IsTrue (apl.Any (ap => ap.Name == "Protected"), "#2");
  702. // oh? SetBaz() has non-void return value, but it seems ignored.
  703. Assert.IsTrue (apl.Any (ap => ap.Name == "Baz"), "#3");
  704. Assert.AreEqual (4, apl.Count, "#4");
  705. Assert.IsTrue (apl.All (ap => ap.IsAttachable), "#5");
  706. var x = apl.First (ap => ap.Name == "X");
  707. Assert.IsTrue (x.IsEvent, "#6");
  708. }
  709. [Test]
  710. [ExpectedException (typeof (ArgumentNullException))]
  711. public void AttachablePropertySetValueNullObject ()
  712. {
  713. var xt = new XamlType (typeof (Attachable), sctx);
  714. var apl = xt.GetAllAttachableMembers ();
  715. var foo = apl.First (ap => ap.Name == "Foo");
  716. Assert.IsTrue (foo.IsAttachable, "#7");
  717. foo.Invoker.SetValue (null, "xxx");
  718. }
  719. [Test]
  720. public void AttachablePropertySetValueSuccess ()
  721. {
  722. var xt = new XamlType (typeof (Attachable), sctx);
  723. var apl = xt.GetAllAttachableMembers ();
  724. var foo = apl.First (ap => ap.Name == "Foo");
  725. Assert.IsTrue (foo.IsAttachable, "#7");
  726. var obj = new object ();
  727. foo.Invoker.SetValue (obj, "xxx"); // obj is non-null, so valid.
  728. // FIXME: this line should be unnecessary.
  729. AttachablePropertyServices.RemoveProperty (obj, new AttachableMemberIdentifier (foo.Type.UnderlyingType, foo.Name));
  730. }
  731. [Test]
  732. public void ReadOnlyPropertyContainer ()
  733. {
  734. var xt = new XamlType (typeof (ReadOnlyPropertyContainer), sctx);
  735. var xm = xt.GetMember ("Bar");
  736. Assert.IsNotNull (xm, "#1");
  737. Assert.IsFalse (xm.IsWritePublic, "#2");
  738. }
  739. [Test]
  740. public void UnknownType ()
  741. {
  742. var xt = new XamlType ("urn:foo", "MyUnknown", null, sctx);
  743. Assert.IsTrue (xt.IsUnknown, "#1");
  744. Assert.IsNotNull (xt.BaseType, "#2");
  745. Assert.IsFalse (xt.BaseType.IsUnknown, "#3");
  746. Assert.AreEqual (typeof (object), xt.BaseType.UnderlyingType, "#4");
  747. }
  748. [Test] // wrt bug #680385
  749. public void DerivedListMembers ()
  750. {
  751. var xt = sctx.GetXamlType (typeof (XamlTest.Configurations));
  752. Assert.IsTrue (xt.GetAllMembers ().Any (xm => xm.Name == "Active"), "#1"); // make sure that the member name is Active, not Configurations.Active ...
  753. }
  754. [Test]
  755. public void EnumType ()
  756. {
  757. var xt = sctx.GetXamlType (typeof (EnumValueType));
  758. Assert.IsTrue (xt.IsConstructible, "#1");
  759. Assert.IsFalse (xt.IsNullable, "#2");
  760. Assert.IsFalse (xt.IsUnknown, "#3");
  761. Assert.IsFalse (xt.IsUsableDuringInitialization, "#4");
  762. Assert.IsNotNull (xt.TypeConverter, "#5");
  763. }
  764. [Test]
  765. public void CollectionContentProperty ()
  766. {
  767. var xt = sctx.GetXamlType (typeof (CollectionContentProperty));
  768. var p = xt.ContentProperty;
  769. Assert.IsNotNull (p, "#1");
  770. Assert.AreEqual ("ListOfItems", p.Name, "#2");
  771. }
  772. [Test]
  773. public void AmbientPropertyContainer ()
  774. {
  775. var xt = sctx.GetXamlType (typeof (SecondTest.ResourcesDict));
  776. Assert.IsTrue (xt.IsAmbient, "#1");
  777. var l = xt.GetAllMembers ().ToArray ();
  778. Assert.AreEqual (2, l.Length, "#2");
  779. // FIXME: enable when string representation difference become compatible
  780. // Assert.AreEqual ("System.Collections.Generic.Dictionary(System.Object, System.Object).Keys", l [0].ToString (), "#3");
  781. // Assert.AreEqual ("System.Collections.Generic.Dictionary(System.Object, System.Object).Values", l [1].ToString (), "#4");
  782. }
  783. [Test]
  784. public void NullableContainer ()
  785. {
  786. var xt = sctx.GetXamlType (typeof (NullableContainer));
  787. Assert.IsFalse (xt.IsGeneric, "#1");
  788. Assert.IsTrue (xt.IsNullable, "#2");
  789. var xm = xt.GetMember ("TestProp");
  790. Assert.IsTrue (xm.Type.IsGeneric, "#3");
  791. Assert.IsTrue (xm.Type.IsNullable, "#4");
  792. Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", xm.Type.PreferredXamlNamespace, "#5");
  793. Assert.AreEqual (1, xm.Type.TypeArguments.Count, "#6");
  794. Assert.AreEqual (XamlLanguage.Int32, xm.Type.TypeArguments [0], "#7");
  795. Assert.IsNotNull (xm.Type.TypeConverter, "#8");
  796. Assert.IsNotNull (xm.Type.TypeConverter.ConverterInstance, "#9");
  797. var obj = new NullableContainer ();
  798. xm.Invoker.SetValue (obj, 5);
  799. xm.Invoker.SetValue (obj, null);
  800. }
  801. [Test]
  802. public void DerivedCollectionAndDictionary ()
  803. {
  804. var xt = sctx.GetXamlType (typeof (IList<int>));
  805. Assert.IsTrue (xt.IsCollection, "#1");
  806. Assert.IsFalse (xt.IsDictionary, "#2");
  807. xt = sctx.GetXamlType (typeof (IDictionary<EnumValueType,int>));
  808. Assert.IsTrue (xt.IsDictionary, "#3");
  809. Assert.IsFalse (xt.IsCollection, "#4");
  810. }
  811. }
  812. class MyXamlType : XamlType
  813. {
  814. public MyXamlType (string fullName, IList<XamlType> typeArguments, XamlSchemaContext context)
  815. : base (fullName, typeArguments, context)
  816. {
  817. }
  818. }
  819. }