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