2 // Copyright (C) 2010 Novell Inc. http://novell.com
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
24 using System.Collections;
25 using System.Collections.Generic;
27 using System.Reflection;
28 using System.Windows.Markup;
30 using System.Xaml.Schema;
31 using NUnit.Framework;
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")]
37 namespace MonoTests.System.Xaml
40 public class XamlSchemaContextTest
42 XamlSchemaContext NewStandardContext ()
44 return new XamlSchemaContext (new Assembly [] {typeof (XamlSchemaContext).Assembly });
47 XamlSchemaContext NewThisAssemblyContext ()
49 return new XamlSchemaContext (new Assembly [] {GetType ().Assembly });
53 public void ConstructorNullAssemblies ()
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");
63 public void ConstructorNullSettings ()
66 var ctx = new XamlSchemaContext ((XamlSchemaContextSettings) null);
70 public void ConstructorNoAssembly ()
72 var ctx = new XamlSchemaContext (new Assembly [0]);
76 public void Constructor ()
78 var ctx = new XamlSchemaContext (new Assembly [] {typeof (XamlSchemaContext).Assembly });
79 Assert.AreEqual (1, ctx.ReferenceAssemblies.Count, "#1");
83 public void GetAllXamlNamespaces ()
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");
92 ctx = NewStandardContext ();
93 arr = ctx.GetAllXamlNamespaces ().ToArray ();
94 Assert.AreEqual (1, arr.Length, "#2");
95 Assert.AreEqual (XamlLanguage.Xaml2006Namespace, arr [0], "#2-2");
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");
105 [ExpectedException (typeof (ArgumentNullException))]
106 public void GetPreferredPrefixNull ()
108 var ctx = new XamlSchemaContext (null, null);
109 ctx.GetPreferredPrefix (null);
113 public void GetPreferredPrefix ()
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" ?
122 [ExpectedException (typeof (ArgumentNullException))]
123 public void TryGetCompatibleXamlNamespaceNull ()
125 var ctx = new XamlSchemaContext (null, null);
127 ctx.TryGetCompatibleXamlNamespace (null, out dummy);
131 [Category ("NotDotNet")] // TryGetCompatibleXamlNamespace() never worked like documented.
132 public void TryGetCompatibleXamlNamespace ()
134 var ctx = new XamlSchemaContext (null, null);
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.
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");
148 var settings = new XamlSchemaContextSettings () { FullyQualifyAssemblyNamesInClrNamespaces = true };
149 ctx = new XamlSchemaContext (new Assembly [] {typeof (XamlSchemaContext).Assembly }, settings);
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");
160 public void GetXamlTypeAndAllXamlTypes ()
162 var ctx = new XamlSchemaContext (new Assembly [] {typeof (string).Assembly}); // build with corlib.
163 Assert.AreEqual (0, ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).Count (), "#0"); // premise
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");
170 // non-primitive type example
171 Assert.IsTrue (object.ReferenceEquals (ctx.GetXamlType (GetType ()), ctx.GetXamlType (GetType ())), "#4");
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");
178 [ExpectedException (typeof (NotSupportedException))] // it is read-only
179 public void AddGetAllXamlTypesToEmpty ()
181 var ctx = NewStandardContext ();
182 ctx.GetAllXamlTypes ("urn:foo").Add (new XamlType (typeof (int), ctx));
186 public void GetAllXamlTypesInXaml2006Namespace ()
188 var ctx = NewStandardContext ();
190 // There are some special types that have non-default name: MemberDefinition, PropertyDefinition
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");
218 public void GetXamlTypeByName ()
220 var ns = XamlLanguage.Xaml2006Namespace;
221 var ctx = NewThisAssemblyContext ();
222 //var ctx = NewStandardContext ();
225 Assert.IsNull (ctx.GetXamlType (new XamlTypeName ("urn:foobarbaz", "bar")));
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");
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");
249 public void GetTypeForRuntimeType ()
251 var ctx = NewStandardContext ();
253 // There are some special types that have non-default name: MemberDefinition, PropertyDefinition
255 var xt = ctx.GetXamlType (typeof (Type));
256 Assert.AreEqual ("Type", xt.Name, "#1-1");
257 Assert.AreEqual (typeof (Type), xt.UnderlyingType, "#1-2");
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");
265 public void GetTypeFromXamlTypeNameWithClrName ()
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");
273 ctx = new XamlSchemaContext ();
274 xt = ctx.GetXamlType (xn);
275 Assert.IsNotNull (xt, "#2");