Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml / XamlSchemaContextTest.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.Linq;
27 using System.Reflection;
28 using System.Windows.Markup;
29 using System.Xaml;
30 using System.Xaml.Schema;
31 using NUnit.Framework;
32
33 [assembly:XmlnsDefinition ("urn:mono-test", "MonoTests.System.Xaml.NamespaceTest")]
34 [assembly:XmlnsDefinition ("urn:mono-test2", "MonoTests.System.Xaml.NamespaceTest2")]
35 [assembly:XmlnsCompatibleWith ("urn:foo", "urn:bar")]
36
37 namespace MonoTests.System.Xaml
38 {
39         [TestFixture]
40         public class XamlSchemaContextTest
41         {
42                 XamlSchemaContext NewStandardContext ()
43                 {
44                         return new XamlSchemaContext (new Assembly [] {typeof (XamlSchemaContext).Assembly });
45                 }
46
47                 XamlSchemaContext NewThisAssemblyContext ()
48                 {
49                         return new XamlSchemaContext (new Assembly [] {GetType ().Assembly });
50                 }
51
52                 [Test]
53                 public void ConstructorNullAssemblies ()
54                 {
55                         // allowed.
56                         var ctx = new XamlSchemaContext ((Assembly []) null);
57                         Assert.IsFalse (ctx.FullyQualifyAssemblyNamesInClrNamespaces, "#1");
58                         Assert.IsFalse (ctx.SupportMarkupExtensionsWithDuplicateArity, "#2");
59                         Assert.IsNull (ctx.ReferenceAssemblies, "#3");
60                 }
61
62                 [Test]
63                 public void ConstructorNullSettings ()
64                 {
65                         // allowed.
66                         var ctx = new XamlSchemaContext ((XamlSchemaContextSettings) null);
67                 }
68
69                 [Test]
70                 public void ConstructorNoAssembly ()
71                 {
72                         var ctx = new XamlSchemaContext (new Assembly [0]);
73                 }
74
75                 [Test]
76                 public void Constructor ()
77                 {
78                         var ctx = new XamlSchemaContext (new Assembly [] {typeof (XamlSchemaContext).Assembly });
79                         Assert.AreEqual (1, ctx.ReferenceAssemblies.Count, "#1");
80                 }
81
82                 [Test]
83                 public void GetAllXamlNamespaces ()
84                 {
85                         var ctx = new XamlSchemaContext (null, null);
86                         var arr = ctx.GetAllXamlNamespaces ().ToArray ();
87                         Assert.AreEqual (5, arr.Length, "#1");
88                         Assert.IsTrue (arr.Contains (XamlLanguage.Xaml2006Namespace), "#1-2");
89                         Assert.IsTrue (arr.Contains ("urn:mono-test"), "#1-3");
90                         Assert.IsTrue (arr.Contains ("urn:mono-test2"), "#1-4");
91
92                         ctx = NewStandardContext ();
93                         arr = ctx.GetAllXamlNamespaces ().ToArray ();
94                         Assert.AreEqual (1, arr.Length, "#2");
95                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, arr [0], "#2-2");
96
97                         ctx = NewThisAssemblyContext ();
98                         arr = ctx.GetAllXamlNamespaces ().ToArray ();
99                         Assert.AreEqual (4, arr.Length, "#3");
100                         Assert.IsTrue (arr.Contains ("urn:mono-test"), "#3-2");
101                         Assert.IsTrue (arr.Contains ("urn:mono-test2"), "#3-3");
102                 }
103
104                 [Test]
105                 [ExpectedException (typeof (ArgumentNullException))]
106                 public void GetPreferredPrefixNull ()
107                 {
108                         var ctx = new XamlSchemaContext (null, null);
109                         ctx.GetPreferredPrefix (null);
110                 }
111
112                 [Test]
113                 public void GetPreferredPrefix ()
114                 {
115                         var ctx = new XamlSchemaContext (null, null);
116                         Assert.AreEqual ("x", ctx.GetPreferredPrefix (XamlLanguage.Xaml2006Namespace), "#1");
117                         Assert.AreEqual ("p", ctx.GetPreferredPrefix ("urn:4mbw93w89mbh"), "#2"); // ... WTF "p" ?
118                         Assert.AreEqual ("p", ctx.GetPreferredPrefix ("urn:etbeoesmj"), "#3"); // ... WTF "p" ?
119                 }
120
121                 [Test]
122                 [ExpectedException (typeof (ArgumentNullException))]
123                 public void TryGetCompatibleXamlNamespaceNull ()
124                 {
125                         var ctx = new XamlSchemaContext (null, null);
126                         string dummy;
127                         ctx.TryGetCompatibleXamlNamespace (null, out dummy);
128                 }
129
130                 [Test]
131                 [Category ("NotDotNet")] // TryGetCompatibleXamlNamespace() never worked like documented.
132                 public void TryGetCompatibleXamlNamespace ()
133                 {
134                         var ctx = new XamlSchemaContext (null, null);
135                         string dummy;
136                         Assert.IsFalse (ctx.TryGetCompatibleXamlNamespace (String.Empty, out dummy), "#1");
137                         Assert.IsNull (dummy, "#1-2"); // this shows the fact that the out result value for false case is not trustworthy.
138
139                         ctx = NewThisAssemblyContext ();
140                         Assert.IsFalse (ctx.TryGetCompatibleXamlNamespace (String.Empty, out dummy), "#2");
141                         Assert.IsFalse (ctx.TryGetCompatibleXamlNamespace ("urn:bar", out dummy), "#3");
142                         // why does .NET return false here?
143                         Assert.IsTrue (ctx.TryGetCompatibleXamlNamespace ("urn:foo", out dummy), "#4");
144                         Assert.AreEqual ("urn:bar", dummy, "#5");
145                 }
146
147 /*
148                         var settings = new XamlSchemaContextSettings () { FullyQualifyAssemblyNamesInClrNamespaces = true };
149                         ctx = new XamlSchemaContext (new Assembly [] {typeof (XamlSchemaContext).Assembly }, settings);
150
151                         ctx = new XamlSchemaContext (new Assembly [] {GetType ().Assembly }, settings);
152                         arr = ctx.GetAllXamlNamespaces ().ToArray ();
153                         Assert.AreEqual (2, arr.Length, "#5");
154                         Assert.IsTrue (arr.Contains ("urn:mono-test"), "#5-2");
155                         Assert.IsTrue (arr.Contains ("urn:mono-test2"), "#5-3");
156                 }
157 */
158
159                 [Test]
160                 public void GetXamlTypeAndAllXamlTypes ()
161                 {
162                         var ctx = new XamlSchemaContext (new Assembly [] {typeof (string).Assembly}); // build with corlib.
163                         Assert.AreEqual (0, ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).Count (), "#0"); // premise
164
165                         var xt = ctx.GetXamlType (typeof (string));
166                         Assert.IsNotNull (xt, "#1");
167                         Assert.AreEqual (typeof (string), xt.UnderlyingType, "#2");
168                         Assert.IsTrue (object.ReferenceEquals (xt, ctx.GetXamlType (typeof (string))), "#3");
169
170                         // non-primitive type example
171                         Assert.IsTrue (object.ReferenceEquals (ctx.GetXamlType (GetType ()), ctx.GetXamlType (GetType ())), "#4");
172
173                         // after getting these types, it still returns 0. So it's not all about caching.
174                         Assert.AreEqual (0, ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).Count (), "#5");
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (NotSupportedException))] // it is read-only
179                 public void AddGetAllXamlTypesToEmpty ()
180                 {
181                         var ctx = NewStandardContext ();
182                         ctx.GetAllXamlTypes ("urn:foo").Add (new XamlType (typeof (int), ctx));
183                 }
184
185                 [Test]
186                 public void GetAllXamlTypesInXaml2006Namespace ()
187                 {
188                         var ctx = NewStandardContext ();
189
190                         // There are some special types that have non-default name: MemberDefinition, PropertyDefinition
191
192                         var l = ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace);
193                         Assert.IsTrue (l.Count () > 40, "#1");
194                         Assert.IsTrue (l.Any (t => t.UnderlyingType == typeof (MemberDefinition)), "#2");
195                         Assert.IsTrue (l.Any (t => t.Name == "AmbientAttribute"), "#3");
196                         Assert.IsTrue (l.Any (t => t.Name == "XData"), "#4");
197                         Assert.IsTrue (l.Any (t => t.Name == "ArrayExtension"), "#5");
198                         Assert.IsTrue (l.Any (t => t.Name == "StaticExtension"), "#6");
199                         // FIXME: enable these tests when I sort out how these special names are filled.
200                         //Assert.IsTrue (l.Any (t => t.Name == "Member"), "#7");
201                         //Assert.IsTrue (l.Any (t => t.Name == "Property"), "#8");
202                         //Assert.IsFalse (l.Any (t => t.Name == "MemberDefinition"), "#9");
203                         //Assert.IsFalse (l.Any (t => t.Name == "PropertyDefinition"), "#10");
204                         //Assert.AreEqual ("MemberDefinition", new XamlType (typeof (MemberDefinition), new XamlSchemaContext (null, null)).Name);
205                         //Assert.AreEqual ("Member", l.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).First (t => t.UnderlyingType == typeof (MemberDefinition)));
206                         Assert.IsFalse (l.Any (t => t.Name == "Array"), "#11");
207                         Assert.IsFalse (l.Any (t => t.Name == "Null"), "#12");
208                         Assert.IsFalse (l.Any (t => t.Name == "Static"), "#13");
209                         Assert.IsFalse (l.Any (t => t.Name == "Type"), "#14");
210                         Assert.IsTrue (l.Contains (XamlLanguage.Type), "#15");
211                         Assert.IsFalse (l.Contains (XamlLanguage.String), "#16"); // huh?
212                         Assert.IsFalse (l.Contains (XamlLanguage.Object), "#17"); // huh?
213                         Assert.IsTrue (l.Contains (XamlLanguage.Array), "#18");
214                         Assert.IsFalse (l.Contains (XamlLanguage.Uri), "#19");
215                 }
216
217                 [Test]
218                 public void GetXamlTypeByName ()
219                 {
220                         var ns = XamlLanguage.Xaml2006Namespace;
221                         var ctx = NewThisAssemblyContext ();
222                         //var ctx = NewStandardContext ();
223                         XamlType xt;
224
225                         Assert.IsNull (ctx.GetXamlType (new XamlTypeName ("urn:foobarbaz", "bar")));
226
227                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Int32"));
228                         Assert.IsNotNull (xt, "#1");
229                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Int32", new XamlTypeName [] {new XamlTypeName (ns, "Int32")}));
230                         Assert.IsNull (xt, "#1-2");
231                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Uri"));
232                         Assert.IsNotNull (xt, "#2");
233
234                         // Compare those results to GetAllXamlTypesInXaml2006Namespace() results,
235                         // which asserts that types with those names are *not* included.
236                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Array"));
237                         Assert.IsNotNull (xt, "#3");
238                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Property"));
239                         Assert.IsNotNull (xt, "#4");
240                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Null"));
241                         Assert.IsNotNull (xt, "#5");
242                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Static"));
243                         Assert.IsNotNull (xt, "#6");
244                         xt = ctx.GetXamlType (new XamlTypeName (ns, "Type"));
245                         Assert.IsNotNull (xt, "#7");
246                 }
247
248                 [Test]
249                 public void GetTypeForRuntimeType ()
250                 {
251                         var ctx = NewStandardContext ();
252
253                         // There are some special types that have non-default name: MemberDefinition, PropertyDefinition
254
255                         var xt = ctx.GetXamlType (typeof (Type));
256                         Assert.AreEqual ("Type", xt.Name, "#1-1");
257                         Assert.AreEqual (typeof (Type), xt.UnderlyingType, "#1-2");
258
259                         xt = ctx.GetXamlType (new XamlTypeName (XamlLanguage.Xaml2006Namespace, "Type")); // becomes TypeExtension, not Type
260                         Assert.AreEqual ("TypeExtension", xt.Name, "#2-1");
261                         Assert.AreEqual (typeof (TypeExtension), xt.UnderlyingType, "#2-2");
262                 }
263
264                 [Test]
265                 public void GetTypeFromXamlTypeNameWithClrName ()
266                 {
267                         // ensure that this does *not* resolve clr type name.
268                         var xn = new XamlTypeName ("clr-namespace:System;assembly=mscorlib", "DateTime");
269                         var ctx = NewStandardContext ();
270                         var xt = ctx.GetXamlType (xn);
271                         Assert.IsNull (xt, "#1");
272
273                         ctx = new XamlSchemaContext ();
274                         xt = ctx.GetXamlType (xn);
275                         Assert.IsNotNull (xt, "#2");
276                 }
277         }
278 }