2010-04-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml / XamlObjectReaderTest.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.IO;
28 using System.Linq;
29 using System.Reflection;
30 using System.Windows.Markup;
31 using System.Xaml;
32 using System.Xaml.Schema;
33 using System.Xml;
34 using NUnit.Framework;
35
36 using Category = NUnit.Framework.CategoryAttribute;
37
38 namespace MonoTests.System.Xaml
39 {
40         [TestFixture]
41         public class XamlObjectReaderTest
42         {
43                 [Test]
44                 public void ConstructorNullObject ()
45                 {
46                         // allowed.
47                         new XamlObjectReader (null);
48                 }
49
50                 [Test]
51                 [ExpectedException (typeof (ArgumentNullException))]
52                 public void ConstructorNullSchemaContext ()
53                 {
54                         new XamlObjectReader ("foo", (XamlSchemaContext) null);
55                 }
56
57                 [Test]
58                 public void ConstructorNullSettings ()
59                 {
60                         new XamlObjectReader ("foo", (XamlObjectReaderSettings) null);
61                 }
62
63                 [Test]
64                 [ExpectedException (typeof (ArgumentNullException))]
65                 public void ConstructorNullSchemaContext2 ()
66                 {
67                         new XamlObjectReader ("foo", null, new XamlObjectReaderSettings ());
68                 }
69
70                 [Test]
71                 public void ConstructorNullSettings2 ()
72                 {
73                         new XamlObjectReader ("foo", new XamlSchemaContext (null, null), null);
74                 }
75
76                 [Test]
77                 public void ReadNull ()
78                 {
79                         var r = new XamlObjectReader (null);
80                         Assert.IsTrue (r.Read (), "#1");
81                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#1-2");
82                         Assert.IsTrue (r.Read (), "#2");
83                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#2-2");
84                         Assert.AreEqual (XamlLanguage.Null, r.Type, "#2-3");
85                         Assert.IsTrue (r.Read (), "#3");
86                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#3-2");
87                         Assert.IsFalse (r.Read (), "#4");
88                         Assert.AreEqual (XamlNodeType.None, r.NodeType, "#4-2");
89                 }
90
91                 [Test]
92                 public void Read1 ()
93                 {
94                         var r = new XamlObjectReader ("Foo");
95                         Assert.AreEqual (XamlNodeType.None, r.NodeType, "#1");
96                         Assert.IsNull (r.Member, "#2");
97                         Assert.IsNull (r.Namespace, "#3");
98                         Assert.IsNull (r.Member, "#4");
99                         Assert.IsNull (r.Type, "#5");
100                         Assert.IsNull (r.Value, "#6");
101
102                         Assert.IsTrue (r.Read (), "#11");
103                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#12");
104                         Assert.IsNotNull (r.Namespace, "#13");
105                         Assert.AreEqual ("x", r.Namespace.Prefix, "#13-2");
106                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, r.Namespace.Namespace, "#13-3");
107
108                         Assert.IsTrue (r.Read (), "#21");
109                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#22");
110                         Assert.IsNotNull (r.Type, "#23");
111                         Assert.AreEqual (new XamlType (typeof (string), r.SchemaContext), r.Type, "#23-2");
112                         Assert.IsNull (r.Namespace, "#25");
113
114                         Assert.IsTrue (r.Read (), "#31");
115                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "#32");
116                         Assert.IsNotNull (r.Member, "#33");
117                         Assert.AreEqual (XamlLanguage.Initialization, r.Member, "#33-2");
118                         Assert.IsNull (r.Type, "#34");
119
120                         Assert.IsTrue (r.Read (), "#41");
121                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "#42");
122                         Assert.AreEqual ("Foo", r.Value, "#43");
123                         Assert.IsNull (r.Member, "#44");
124
125                         Assert.IsTrue (r.Read (), "#51");
126                         Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "#52");
127                         Assert.IsNull (r.Type, "#53");
128                         Assert.IsNull (r.Member, "#54");
129
130                         Assert.IsTrue (r.Read (), "#61");
131                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#62");
132                         Assert.IsNull (r.Type, "#63");
133
134                         Assert.IsFalse (r.Read (), "#71");
135                         Assert.IsTrue (r.IsEof, "#72");
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (XamlObjectReaderException))]
140                 [Category ("NotWorking")]
141                 public void ReadNonConstructible ()
142                 {
143                         var r = new XamlObjectReader (XamlLanguage.String);
144                         // XamlType has no default constructor.
145                         r.Read ();
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (XamlObjectReaderException))]
150                 public void NonPublicType ()
151                 {
152                         new XamlObjectReader (new TestClass1 ());
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (XamlObjectReaderException))]
157                 public void NestedType ()
158                 {
159                         new XamlObjectReader (new TestClass2 ());
160                 }
161                 
162                 public class TestClass2
163                 {
164                 }
165
166                 [Test]
167                 public void ConstructibleType ()
168                 {
169                         new XamlObjectReader (new TestClass3 ());
170                 }
171
172                 [Test]
173                 public void Skip ()
174                 {
175                         var r = new XamlObjectReader ("Foo");
176                         r.Skip ();
177                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#1");
178                         r.Skip ();
179                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#2");
180                         r.Skip ();
181                         Assert.IsTrue (r.IsEof, "#3");
182                 }
183
184                 [Test]
185                 public void Skip2 ()
186                 {
187                         var r = new XamlObjectReader ("Foo");
188                         r.Read (); // NamespaceDeclaration
189                         r.Read (); // Type
190                         r.Read (); // Member (Initialization)
191                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "#1");
192                         r.Skip ();
193                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#2");
194                         r.Skip ();
195                         Assert.IsTrue (r.IsEof, "#3");
196                 }
197
198                 [Test]
199                 [Category ("NotWorking")]
200                 public void Read2 ()
201                 {
202                         var doc = new XmlDocument ();
203                         doc.LoadXml ("<root xmlns='urn:foo'><elem attr='val' /></root>");
204                         var r = new XamlObjectReader (doc);
205
206                         for (int i = 0; i < 3; i++) {
207                                 r.Read ();
208                                 Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#1-" + i);
209                         }
210                         r.Read ();
211
212                         Assert.AreEqual (new XamlType (typeof (XmlDocument), r.SchemaContext), r.Type, "#2");
213                         r.Read ();
214                         var l = new List<XamlMember> ();
215                         for (int i = 0; i < 5; i++) {
216                                 Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "#3-" + i);
217                                 l.Add (r.Member);
218                                 r.Skip ();
219                         }
220                         l.First (m => m.Name == "Value");
221                         l.First (m => m.Name == "InnerXml");
222                         l.First (m => m.Name == "Prefix");
223                         l.First (m => m.Name == "PreserveWhitespace");
224                         l.First (m => m.Name == "Schemas");
225                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#4");
226                         Assert.IsFalse (r.Read (), "#5");
227                 }
228
229                 [Test]
230                 [Category ("NotWorking")]
231                 public void Read_NonPrimitive ()
232                 {
233                         var r = new XamlObjectReader (new TestClass3 ());
234                         Assert.AreEqual (XamlNodeType.None, r.NodeType, "#1");
235                         Assert.IsTrue (r.Read (), "#6");
236                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#7");
237                         Assert.AreEqual (String.Empty, r.Namespace.Prefix, "#7-2");
238                         Assert.AreEqual ("clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name, r.Namespace.Namespace, "#7-3");
239
240                         Assert.IsTrue (r.Read (), "#11");
241                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#12");
242                         Assert.AreEqual ("x", r.Namespace.Prefix, "#12-2");
243                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, r.Namespace.Namespace, "#12-3");
244
245                         Assert.IsTrue (r.Read (), "#16");
246                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#17");
247                         var xt = new XamlType (typeof (TestClass3), r.SchemaContext);
248                         Assert.AreEqual (xt, r.Type, "#17-2");
249                         Assert.IsTrue (r.Instance is TestClass3, "#17-3");
250
251                         Assert.IsTrue (r.Read (), "#21");
252                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "#22");
253                         Assert.AreEqual (xt.GetMember ("Nested"), r.Member, "#22-2");
254
255                         Assert.IsTrue (r.Read (), "#26");
256                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#27");
257                         Assert.AreEqual (XamlLanguage.Null, r.Type, "#27-2");
258                         Assert.IsNull (r.Instance, "#27-3");
259
260                         Assert.IsTrue (r.Read (), "#31");
261                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#32");
262
263                         Assert.IsTrue (r.Read (), "#36");
264                         Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "#37");
265
266                         Assert.IsTrue (r.Read (), "#41");
267                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#42");
268
269                         Assert.IsFalse (r.Read (), "#46");
270                         Assert.IsTrue (r.IsEof, "#47");
271                 }
272
273                 [Test]
274                 [Category ("NotWorking")]
275                 public void Read_Type ()
276                 {
277                         var r = new XamlObjectReader (typeof (int));
278                         Read_TypeOrTypeExtension (r);
279                 }
280                 
281                 [Test]
282                 [Category ("NotWorking")]
283                 public void Read_TypeExtension ()
284                 {
285                         var r = new XamlObjectReader (new TypeExtension (typeof (int)));
286                         Read_TypeOrTypeExtension (r);
287                 }
288
289                 void Read_TypeOrTypeExtension (XamlObjectReader r)
290                 {
291                         Assert.IsTrue (r.Read (), "#11");
292                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#12");
293                         Assert.IsNotNull (r.Namespace, "#13");
294                         Assert.AreEqual ("x", r.Namespace.Prefix, "#13-2");
295                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, r.Namespace.Namespace, "#13-3");
296                         Assert.IsNull (r.Instance, "#14");
297
298                         Assert.IsTrue (r.Read (), "#21");
299                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#22");
300                         Assert.IsNotNull (r.Type, "#23");
301                         Assert.AreEqual (new XamlType (typeof (TypeExtension), r.SchemaContext), r.Type, "#23-2");
302                         Assert.IsNull (r.Namespace, "#25");
303                         Assert.IsTrue (r.Instance is TypeExtension, "#26");
304
305                         Assert.IsTrue (r.Read (), "#31");
306                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "#32");
307                         Assert.IsNotNull (r.Member, "#33");
308                         Assert.AreEqual (XamlLanguage.PositionalParameters, r.Member, "#33-2");
309                         Assert.IsNull (r.Type, "#34");
310                         Assert.IsNull (r.Instance, "#35");
311
312                         Assert.IsTrue (r.Read (), "#41");
313                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "#42");
314                         Assert.IsNotNull (r.Value, "#43");
315                         Assert.AreEqual ("x:Int32", r.Value, "#43-2");
316                         Assert.IsNull (r.Member, "#44");
317                         Assert.IsNull (r.Instance, "#45");
318
319                         Assert.IsTrue (r.Read (), "#51");
320                         Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "#52");
321                         Assert.IsNull (r.Type, "#53");
322                         Assert.IsNull (r.Member, "#54");
323                         Assert.IsNull (r.Instance, "#55");
324
325                         Assert.IsTrue (r.Read (), "#61");
326                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#62");
327                         Assert.IsNull (r.Type, "#63");
328
329                         Assert.IsFalse (r.Read (), "#71");
330                         Assert.IsTrue (r.IsEof, "#72");
331                 }
332
333                 [Test]
334                 [Category ("NotWorking")] // namespace node differences
335                 public void Read_Type2 ()
336                 {
337                         var r = new XamlObjectReader (typeof (TestClass1));
338                         Read_TypeOrTypeExtension2 (r);
339                 }
340                 
341                 [Test]
342                 [Category ("NotWorking")] // namespace node differences
343                 public void Read_TypeExtension2 ()
344                 {
345                         var r = new XamlObjectReader (new TypeExtension (typeof (TestClass1)));
346                         Read_TypeOrTypeExtension2 (r);
347                 }
348
349                 void Read_TypeOrTypeExtension2 (XamlObjectReader r)
350                 {
351                         Assert.IsTrue (r.Read (), "#11");
352                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#12");
353                         Assert.AreEqual (String.Empty, r.Namespace.Prefix, "#13-2");
354                         Assert.AreEqual ("clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name, r.Namespace.Namespace, "#13-3");
355
356                         Assert.IsTrue (r.Read (), "#16");
357                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "#17");
358                         Assert.IsNotNull (r.Namespace, "#18");
359                         Assert.AreEqual ("x", r.Namespace.Prefix, "#18-2");
360                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, r.Namespace.Namespace, "#18-3");
361
362                         Assert.IsTrue (r.Read (), "#21");
363                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#22");
364                         Assert.AreEqual (new XamlType (typeof (TypeExtension), r.SchemaContext), r.Type, "#23-2");
365                         Assert.IsTrue (r.Instance is TypeExtension, "#26");
366
367                         Assert.IsTrue (r.Read (), "#31");
368                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "#32");
369                         Assert.AreEqual (XamlLanguage.PositionalParameters, r.Member, "#33-2");
370
371                         Assert.IsTrue (r.Read (), "#41");
372                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "#42");
373                         Assert.AreEqual ("TestClass1", r.Value, "#43-2");
374
375                         Assert.IsTrue (r.Read (), "#51");
376                         Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "#52");
377
378                         Assert.IsTrue (r.Read (), "#61");
379                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#62");
380
381                         Assert.IsFalse (r.Read (), "#71");
382                         Assert.IsTrue (r.IsEof, "#72");
383                 }
384
385                 [Test]
386                 // WTF, It does not give XAML namespace, but XamlSerives.Save()
387                 // serializes DateTime instance in the XAML namespace.
388                 [Category ("NotWorking")]
389                 public void Read_DateTime ()
390                 {
391                         var obj = new DateTime (2010, 4, 15);
392                         var r = new XamlObjectReader (obj);
393                         Read_CommonClrType (r, obj);
394                         Assert.AreEqual ("2010-04-15", Read_Initialization (r, null), "#1");
395                 }
396
397                 [Test]
398                 public void Read_TimeSpan ()
399                 {
400                         Read_CommonXamlPrimitive (TimeSpan.FromMinutes (4));
401                 }
402
403                 [Test]
404                 public void Read_Uri ()
405                 {
406                         Read_CommonXamlPrimitive (new Uri ("urn:foo"));
407                 }
408
409                 [Test]
410                 [ExpectedException (typeof (XamlObjectReaderException))]
411                 [Category ("NotWorking")]
412                 public void Read_XData ()
413                 {
414                         var r = new XamlObjectReader (new XData () {Text = "xdata text"}); // XmlReader implementation is not visible.
415                         while (!r.IsEof)
416                                 r.Read ();
417                 }
418                 
419                 void Read_CommonXamlPrimitive (object obj)
420                 {
421                         var r = new XamlObjectReader (obj);
422                         Read_CommonXamlType (r);
423                         Read_Initialization (r, obj);
424                 }
425
426                 // from StartMember of Initialization to EndMember
427                 string Read_Initialization (XamlObjectReader r, object comparableValue)
428                 {
429                         Assert.IsTrue (r.Read (), "init#1");
430                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "init#2");
431                         Assert.IsNotNull (r.Member, "init#3");
432                         Assert.AreEqual (XamlLanguage.Initialization, r.Member, "init#3-2");
433                         Assert.IsTrue (r.Read (), "init#4");
434                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "init#5");
435                         Assert.AreEqual (typeof (string), r.Value.GetType (), "init#6");
436                         string ret = (string) r.Value;
437                         if (comparableValue != null)
438                                 Assert.AreEqual (comparableValue.ToString (), r.Value, "init#6-2");
439                         Assert.IsTrue (r.Read (), "init#7");
440                         Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "init#8");
441                         return ret;
442                 }
443
444                 // from initial to StartObject
445                 void Read_CommonXamlType (XamlObjectReader r)
446                 {
447                         Assert.IsTrue (r.Read (), "ct#1");
448                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "ct#2");
449                         Assert.IsNotNull (r.Namespace, "ct#3");
450                         Assert.AreEqual ("x", r.Namespace.Prefix, "ct#3-2");
451                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, r.Namespace.Namespace, "ct#3-3");
452                         Assert.IsNull (r.Instance, "ct#4");
453
454                         Assert.IsTrue (r.Read (), "ct#5");
455                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "ct#6");
456                 }
457
458                 // from initial to StartObject
459                 void Read_CommonClrType (XamlObjectReader r, object obj)
460                 {
461                         Assert.IsTrue (r.Read (), "ct#1");
462                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "ct#2");
463                         Assert.IsNotNull (r.Namespace, "ct#3");
464                         Assert.AreEqual (String.Empty, r.Namespace.Prefix, "ct#3-2");
465                         Assert.AreEqual ("clr-namespace:" + obj.GetType ().Namespace + ";assembly=" + obj.GetType ().Assembly.GetName ().Name, r.Namespace.Namespace, "ct#3-3");
466
467 /*
468                         Assert.IsTrue (r.Read (), "ct#4");
469                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "ct#5");
470                         Assert.IsNotNull (r.Namespace, "ct#6");
471                         Assert.AreEqual ("x", r.Namespace.Prefix, "ct#6-2");
472                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, r.Namespace.Namespace, "ct#6-3");
473 */
474
475                         Assert.IsTrue (r.Read (), "ct#7");
476                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "ct#8");
477                 }
478         }
479
480         class TestClass1
481         {
482         }
483
484         public class TestClass3
485         {
486                 public TestClass3 Nested { get; set; }
487         }
488 }