2010-04-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml / XamlXmlReaderTest.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.Globalization;
28 using System.IO;
29 using System.Linq;
30 using System.Reflection;
31 using System.Windows.Markup;
32 using System.Xaml;
33 using System.Xaml.Schema;
34 using System.Xml;
35 using NUnit.Framework;
36
37 using Category = NUnit.Framework.CategoryAttribute;
38
39 namespace MonoTests.System.Xaml
40 {
41         [TestFixture]
42         public class XamlXmlReaderTest
43         {
44
45                 // read test
46
47                 XamlReader GetReader (string filename)
48                 {
49                         return new XamlXmlReader (XmlReader.Create (Path.Combine ("Test/XmlFiles", filename), new XmlReaderSettings () { CloseInput =true }));
50                 }
51
52                 void ReadTest (string filename)
53                 {
54                         var r = GetReader (filename);
55                         while (!r.IsEof)
56                                 r.Read ();
57                 }
58
59                 void LoadTest (string filename, Type type)
60                 {
61                         var obj = XamlServices.Load (GetReader (filename));
62                         Assert.AreEqual (type, obj.GetType (), "type");
63                 }
64
65                 [Test]
66                 public void Read_String ()
67                 {
68                         ReadTest ("String.xml");
69                         //LoadTest ("String.xml", typeof (string));
70                 }
71
72                 [Test]
73                 public void Read_Int32 ()
74                 {
75                         ReadTest ("Int32.xml");
76                         //LoadTest ("Int32.xml", typeof (int));
77                 }
78
79                 [Test]
80                 [Category ("NotWorking")]
81                 public void Read_DateTime ()
82                 {
83                         ReadTest ("DateTime.xml");
84                         //LoadTest ("DateTime.xml", typeof (DateTime));
85                 }
86
87                 [Test]
88                 public void Read_TimeSpan ()
89                 {
90                         ReadTest ("TimeSpan.xml");
91                         //LoadTest ("TimeSpan.xml", typeof (TimeSpan));
92                 }
93
94                 [Test]
95                 public void Read_ArrayInt32 ()
96                 {
97                         ReadTest ("Array_Int32.xml");
98                         //LoadTest ("Array_Int32.xml", typeof (int []));
99                 }
100
101                 [Test]
102                 [Category ("NotWorking")]
103                 public void Read_ListInt32 ()
104                 {
105                         ReadTest ("List_Int32.xml");
106                         LoadTest ("List_Int32.xml", typeof (List<int>));
107                 }
108
109                 [Test]
110                 public void Read1 ()
111                 {
112                         var r = GetReader ("Int32.xml");
113
114                         Assert.IsTrue (r.Read (), "ns#1");
115                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "ns#2");
116                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, r.Namespace.Namespace, "ns#3");
117
118                         Assert.IsTrue (r.Read (), "so#1");
119                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "so#2");
120                         Assert.AreEqual (XamlLanguage.Int32, r.Type, "so#3");
121
122                         Assert.IsTrue (r.Read (), "sbase#1");
123                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "sbase#2");
124                         Assert.AreEqual (XamlLanguage.Base, r.Member, "sbase#3");
125
126                         Assert.IsTrue (r.Read (), "vbase#1");
127                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "vbase#2");
128                         Assert.IsTrue (r.Value is string, "vbase#3");
129
130                         Assert.IsTrue (r.Read (), "ebase#21");
131
132                         Assert.IsTrue (r.Read (), "sinit#1");
133                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "sinit#2");
134                         Assert.AreEqual (XamlLanguage.Initialization, r.Member, "sinit#3");
135
136                         Assert.IsTrue (r.Read (), "vinit#1");
137                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "vinit#2");
138                         Assert.AreEqual ("5", r.Value, "vinit#3"); // string
139
140                         Assert.IsTrue (r.Read (), "einit#1");
141                         Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "einit#2");
142
143                         Assert.IsTrue (r.Read (), "eo#1");
144                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "eo#2");
145
146                         Assert.IsFalse (r.Read (), "end");
147                 }
148
149                 [Test]
150                 [Category ("NotWorking")]
151                 public void Read2 ()
152                 {
153                         var r = GetReader ("DateTime.xml");
154
155                         Assert.IsTrue (r.Read (), "ns#1");
156                         Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "ns#2");
157                         Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", r.Namespace.Namespace, "ns#3");
158
159                         Assert.IsTrue (r.Read (), "so#1");
160                         Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "so#2");
161                         Assert.AreEqual (r.SchemaContext.GetXamlType (typeof (DateTime)), r.Type, "so#3");
162
163                         Assert.IsTrue (r.Read (), "sbase#1");
164                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "sbase#2");
165                         Assert.AreEqual (XamlLanguage.Base, r.Member, "sbase#3");
166
167                         Assert.IsTrue (r.Read (), "vbase#1");
168                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "vbase#2");
169                         Assert.IsTrue (r.Value is string, "vbase#3");
170
171                         Assert.IsTrue (r.Read (), "ebase#21");
172
173                         Assert.IsTrue (r.Read (), "sinit#1");
174                         Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "sinit#2");
175                         Assert.AreEqual (XamlLanguage.Initialization, r.Member, "sinit#3");
176
177                         Assert.IsTrue (r.Read (), "vinit#1");
178                         Assert.AreEqual (XamlNodeType.Value, r.NodeType, "vinit#2");
179                         Assert.AreEqual ("2010-04-14", r.Value, "vinit#3"); // string
180
181                         Assert.IsTrue (r.Read (), "einit#1");
182                         Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "einit#2");
183
184                         Assert.IsTrue (r.Read (), "eo#1");
185                         Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "eo#2");
186                         Assert.IsFalse (r.Read (), "end");
187                 }
188         }
189 }