Add new ValueSerializerContext and use it everywhere appropriate.
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml / XamlMemberTest.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.Text;
30 using System.Xaml;
31 using System.Xaml.Schema;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Xaml
35 {
36         [TestFixture]
37         // FIXME: uncomment TypeConverter tests
38         public class XamlMemberTest
39         {
40                 XamlSchemaContext sctx = new XamlSchemaContext (new XamlSchemaContextSettings ());
41                 EventInfo ass_load = typeof (AppDomain).GetEvent ("AssemblyLoad");
42                 PropertyInfo str_len = typeof (string).GetProperty ("Length");
43                 PropertyInfo sb_len = typeof (StringBuilder).GetProperty ("Length");
44                 MethodInfo dummy_add = typeof (XamlMemberTest).GetMethod ("DummyAddMethod");
45                 MethodInfo dummy_get = typeof (XamlMemberTest).GetMethod ("DummyGetMethod");
46                 MethodInfo dummy_set = typeof (XamlMemberTest).GetMethod ("DummySetMethod");
47                 MethodInfo dummy_set2 = typeof (Dummy).GetMethod ("DummySetMethod");
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentNullException))]
51                 public void ConstructorEventInfoNullEventInfo ()
52                 {
53                         new XamlMember ((EventInfo) null, sctx);
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentNullException))]
58                 public void ConstructorEventInfoNullSchemaContext ()
59                 {
60                         new XamlMember (ass_load, null);
61                 }
62
63                 [Test]
64                 [ExpectedException (typeof (ArgumentNullException))]
65                 public void ConstructorPropertyInfoNullPropertyInfo ()
66                 {
67                         new XamlMember ((PropertyInfo) null, sctx);
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (ArgumentNullException))]
72                 public void ConstructorPropertyInfoNullSchemaContext ()
73                 {
74                         new XamlMember (str_len, null);
75                 }
76
77                 [Test]
78                 [ExpectedException (typeof (ArgumentNullException))]
79                 public void ConstructorAddMethodNullName ()
80                 {
81                         new XamlMember (null, GetType ().GetMethod ("DummyAddMEthod"), sctx);
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentNullException))]
86                 public void ConstructorAddMethodNullMethod ()
87                 {
88                         new XamlMember ("DummyAddMethod", null, sctx);
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (ArgumentNullException))]
93                 public void ConstructorAddMethodNullSchemaContext ()
94                 {
95                         new XamlMember ("DummyAddMethod", dummy_add, null);
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void ConstructorGetSetMethodNullName ()
101                 {
102                         new XamlMember (null, dummy_get, dummy_set, sctx);
103                 }
104
105                 [Test]
106                 public void ConstructorGetSetMethodNullGetMethod ()
107                 {
108                         new XamlMember ("DummyProp", null, dummy_set, sctx);
109                 }
110
111                 [Test]
112                 public void ConstructorGetSetMethodNullSetMethod ()
113                 {
114                         new XamlMember ("DummyProp", dummy_get, null, sctx);
115                 }
116
117                 [Test]
118                 [ExpectedException (typeof (ArgumentNullException))]
119                 public void ConstructorGetSetMethodNullGetSetMethod ()
120                 {
121                         new XamlMember ("DummyProp", null, null, sctx);
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentNullException))]
126                 public void ConstructorGetSetMethodNullSchemaContext ()
127                 {
128                         new XamlMember ("DummyProp", dummy_get, dummy_set, null);
129                 }
130
131                 [Test]
132                 [ExpectedException (typeof (ArgumentNullException))]
133                 public void ConstructorNameTypeNullName ()
134                 {
135                         new XamlMember (null, new XamlType (typeof (string), sctx), false);
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (ArgumentNullException))]
140                 public void ConstructorNameTypeNullType ()
141                 {
142                         new XamlMember ("Length", null, false);
143                 }
144
145                 [Test]
146                 [ExpectedException (typeof (ArgumentException))]
147                 public void AddMethodInvalid ()
148                 {
149                         // It is not of expected kind of member here:
150                         // "Attached property setter and attached event adder methods must have two parameters."
151                         new XamlMember ("AssemblyLoad", ass_load.GetAddMethod (), sctx);
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (ArgumentException))]
156                 public void GetMethodInvlaid ()
157                 {
158                         // It is not of expected kind of member here:
159                         // "Attached property getter methods must have one parameter and a non-void return type."
160                         new XamlMember ("Length", sb_len.GetGetMethod (), null, sctx);
161                 }
162
163                 [Test]
164                 [ExpectedException (typeof (ArgumentException))]
165                 public void SetMethodInvalid ()
166                 {
167                         // It is not of expected kind of member here:
168                         // "Attached property setter and attached event adder methods must have two parameters."
169                         new XamlMember ("Length", null, sb_len.GetSetMethod (), sctx);
170                 }
171
172                 [Test]
173                 public void MethodsFromDifferentType ()
174                 {
175                         // allowed...
176                         var i = new XamlMember ("Length", dummy_get, dummy_set2, sctx);
177                         Assert.IsNotNull (i.DeclaringType, "#1");
178                         // hmm...
179                         Assert.AreEqual (GetType (), i.DeclaringType.UnderlyingType, "#2");
180                 }
181
182                 // default values.
183
184                 [Test]
185                 public void EventInfoDefaultValues ()
186                 {
187                         var m = new XamlMember (typeof (AppDomain).GetEvent ("AssemblyLoad"), sctx);
188
189                         Assert.IsNotNull (m.DeclaringType, "#2");
190                         Assert.AreEqual (typeof (AppDomain), m.DeclaringType.UnderlyingType, "#2-2");
191                         Assert.IsNotNull (m.Invoker, "#3");
192                         Assert.IsNull (m.Invoker.UnderlyingGetter, "#3-2");
193                         Assert.AreEqual (ass_load.GetAddMethod (), m.Invoker.UnderlyingSetter, "#3-3");
194                         Assert.IsFalse (m.IsUnknown, "#4");
195                         Assert.IsFalse (m.IsReadPublic, "#5");
196                         Assert.IsTrue (m.IsWritePublic, "#6");
197                         Assert.AreEqual ("AssemblyLoad", m.Name, "#7");
198                         Assert.IsTrue (m.IsNameValid, "#8");
199                         Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", m.PreferredXamlNamespace, "#9");
200                         Assert.AreEqual (new XamlType (typeof (AppDomain), sctx), m.TargetType, "#10");
201                         Assert.IsNotNull (m.Type, "#11");
202                         Assert.AreEqual (typeof (AssemblyLoadEventHandler), m.Type.UnderlyingType, "#11-2");
203 //                      Assert.IsNotNull (m.TypeConverter, "#12"); // EventConverter
204                         Assert.IsNull (m.ValueSerializer, "#13");
205                         Assert.IsNull (m.DeferringLoader, "#14");
206                         Assert.AreEqual (ass_load, m.UnderlyingMember, "#15");
207                         Assert.IsFalse (m.IsReadOnly, "#16");
208                         Assert.IsTrue (m.IsWriteOnly, "#17");
209                         Assert.IsFalse (m.IsAttachable, "#18");
210                         Assert.IsTrue (m.IsEvent, "#19");
211                         Assert.IsFalse (m.IsDirective, "#20");
212                         Assert.IsNotNull (m.DependsOn, "#21");
213                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
214                         Assert.IsFalse (m.IsAmbient, "#22");
215                 }
216
217                 [Test]
218                 public void PropertyInfoDefaultValues ()
219                 {
220                         var m = new XamlMember (typeof (string).GetProperty ("Length"), sctx);
221
222                         Assert.IsNotNull (m.DeclaringType, "#2");
223                         Assert.AreEqual (typeof (string), m.DeclaringType.UnderlyingType, "#2-2");
224                         Assert.IsNotNull (m.Invoker, "#3");
225                         Assert.AreEqual (str_len.GetGetMethod (), m.Invoker.UnderlyingGetter, "#3-2");
226                         Assert.AreEqual (str_len.GetSetMethod (), m.Invoker.UnderlyingSetter, "#3-3");
227                         Assert.IsFalse (m.IsUnknown, "#4");
228                         Assert.IsTrue (m.IsReadPublic, "#5");
229                         Assert.IsFalse (m.IsWritePublic, "#6");
230                         Assert.AreEqual ("Length", m.Name, "#7");
231                         Assert.IsTrue (m.IsNameValid, "#8");
232                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, m.PreferredXamlNamespace, "#9");
233                         Assert.AreEqual (new XamlType (typeof (string), sctx), m.TargetType, "#10");
234                         Assert.IsNotNull (m.Type, "#11");
235                         Assert.AreEqual (typeof (int), m.Type.UnderlyingType, "#11-2");
236                         Assert.IsNotNull (m.TypeConverter, "#12");
237                         Assert.IsNull (m.ValueSerializer, "#13");
238                         Assert.IsNull (m.DeferringLoader, "#14");
239                         Assert.AreEqual (str_len, m.UnderlyingMember, "#15");
240                         Assert.IsTrue (m.IsReadOnly, "#16");
241                         Assert.IsFalse (m.IsWriteOnly, "#17");
242                         Assert.IsFalse (m.IsAttachable, "#18");
243                         Assert.IsFalse (m.IsEvent, "#19");
244                         Assert.IsFalse (m.IsDirective, "#20");
245                         Assert.IsNotNull (m.DependsOn, "#21");
246                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
247                         Assert.IsFalse (m.IsAmbient, "#22");
248                 }
249
250                 public void DummyAddMethod (object o, AssemblyLoadEventHandler h)
251                 {
252                 }
253
254                 public int DummyGetMethod (object o)
255                 {
256                         return 5;
257                 }
258
259                 public void DummySetMethod (object o, int v)
260                 {
261                 }
262
263                 public class Dummy
264                 {
265                         public int DummyGetMethod (object o)
266                         {
267                                 return 5;
268                         }
269
270                         public void DummySetMethod (object o, int v)
271                         {
272                         }
273                 }
274
275                 [Test]
276                 public void AddMethodDefaultValues ()
277                 {
278                         var m = new XamlMember ("DummyAddMethod", dummy_add, sctx);
279
280                         Assert.IsNotNull (m.DeclaringType, "#2");
281                         Assert.AreEqual (GetType (), m.DeclaringType.UnderlyingType, "#2-2");
282                         Assert.IsNotNull (m.Invoker, "#3");
283                         Assert.IsNull (m.Invoker.UnderlyingGetter, "#3-2");
284                         Assert.AreEqual (dummy_add, m.Invoker.UnderlyingSetter, "#3-3");
285                         Assert.IsFalse (m.IsUnknown, "#4");
286                         Assert.IsFalse (m.IsReadPublic, "#5");
287                         Assert.IsTrue (m.IsWritePublic, "#6");
288                         Assert.AreEqual ("DummyAddMethod", m.Name, "#7");
289                         Assert.IsTrue (m.IsNameValid, "#8");
290                         var ns = "clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name;
291                         Assert.AreEqual (ns, m.PreferredXamlNamespace, "#9");
292                         // since it is unknown.
293                         Assert.AreEqual (new XamlType (typeof (object), sctx), m.TargetType, "#10");
294                         Assert.IsNotNull (m.Type, "#11");
295                         Assert.AreEqual (typeof (AssemblyLoadEventHandler), m.Type.UnderlyingType, "#11-2");
296 //                      Assert.IsNotNull (m.TypeConverter, "#12");
297                         Assert.IsNull (m.ValueSerializer, "#13");
298                         Assert.IsNull (m.DeferringLoader, "#14");
299                         Assert.AreEqual (dummy_add, m.UnderlyingMember, "#15");
300                         Assert.IsFalse (m.IsReadOnly, "#16");
301                         Assert.IsTrue (m.IsWriteOnly, "#17");
302                         Assert.IsTrue (m.IsAttachable, "#18");
303                         Assert.IsTrue (m.IsEvent, "#19");
304                         Assert.IsFalse (m.IsDirective, "#20");
305                         Assert.IsNotNull (m.DependsOn, "#21");
306                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
307                         Assert.IsFalse (m.IsAmbient, "#22");
308                 }
309
310                 [Test]
311                 public void GetSetMethodDefaultValues ()
312                 {
313                         var m = new XamlMember ("DummyProp", dummy_get, dummy_set, sctx);
314
315                         Assert.IsNotNull (m.DeclaringType, "#2");
316                         Assert.AreEqual (GetType (), m.DeclaringType.UnderlyingType, "#2-2");
317                         Assert.IsNotNull (m.Invoker, "#3");
318                         Assert.AreEqual (dummy_get, m.Invoker.UnderlyingGetter, "#3-2");
319                         Assert.AreEqual (dummy_set, m.Invoker.UnderlyingSetter, "#3-3");
320                         Assert.IsFalse (m.IsUnknown, "#4");
321                         Assert.IsTrue (m.IsReadPublic, "#5");
322                         Assert.IsTrue (m.IsWritePublic, "#6");
323                         Assert.AreEqual ("DummyProp", m.Name, "#7");
324                         Assert.IsTrue (m.IsNameValid, "#8");
325                         var ns = "clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name;
326                         Assert.AreEqual (ns, m.PreferredXamlNamespace, "#9");
327                         // since it is unknown.
328                         Assert.AreEqual (new XamlType (typeof (object), sctx), m.TargetType, "#10");
329                         Assert.IsNotNull (m.Type, "#11");
330                         Assert.AreEqual (typeof (int), m.Type.UnderlyingType, "#11-2");
331 //                      Assert.IsNotNull (m.TypeConverter, "#12");
332                         Assert.IsNull (m.ValueSerializer, "#13");
333                         Assert.IsNull (m.DeferringLoader, "#14");
334                         Assert.AreEqual (dummy_get, m.UnderlyingMember, "#15");
335                         Assert.IsFalse (m.IsReadOnly, "#16");
336                         Assert.IsFalse (m.IsWriteOnly, "#17");
337                         Assert.IsTrue (m.IsAttachable, "#18");
338                         Assert.IsFalse (m.IsEvent, "#19");
339                         Assert.IsFalse (m.IsDirective, "#20");
340                         Assert.IsNotNull (m.DependsOn, "#21");
341                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
342                         Assert.IsFalse (m.IsAmbient, "#22");
343                 }
344
345                 [Test]
346                 public void NameTypeDefaultValues ()
347                 {
348                         var m = new XamlMember ("Length", new XamlType (typeof (string), sctx), false);
349
350                         Assert.IsNotNull (m.DeclaringType, "#2");
351                         Assert.AreEqual (typeof (string), m.DeclaringType.UnderlyingType, "#2-2");
352                         Assert.IsNotNull (m.Invoker, "#3");
353                         Assert.IsNull (m.Invoker.UnderlyingGetter, "#3-2");
354                         Assert.IsNull (m.Invoker.UnderlyingSetter, "#3-3");
355                         Assert.IsTrue (m.IsUnknown, "#4");
356                         Assert.IsTrue (m.IsReadPublic, "#5");
357                         Assert.IsTrue (m.IsWritePublic, "#6");
358                         Assert.AreEqual ("Length", m.Name, "#7");
359                         Assert.IsTrue (m.IsNameValid, "#8");
360                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, m.PreferredXamlNamespace, "#9");
361                         Assert.AreEqual (new XamlType (typeof (string), sctx), m.TargetType, "#10");
362                         Assert.IsNotNull (m.Type, "#11");
363                         Assert.AreEqual (typeof (object), m.Type.UnderlyingType, "#11-2");
364                         Assert.IsNull (m.TypeConverter, "#12");
365                         Assert.IsNull (m.ValueSerializer, "#13");
366                         Assert.IsNull (m.DeferringLoader, "#14");
367                         Assert.IsNull (m.UnderlyingMember, "#15");
368                         Assert.IsFalse (m.IsReadOnly, "#16");
369                         Assert.IsFalse (m.IsWriteOnly, "#17");
370                         Assert.IsFalse (m.IsAttachable, "#18");
371                         Assert.IsFalse (m.IsEvent, "#19");
372                         Assert.IsFalse (m.IsDirective, "#20");
373                         Assert.IsNotNull (m.DependsOn, "#21");
374                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
375                         Assert.IsFalse (m.IsAmbient, "#22");
376                 }
377
378                 [Test]
379                 public void UnderlyingMember ()
380                 {
381                         Assert.IsTrue (new XamlMember (ass_load, sctx).UnderlyingMember is EventInfo, "#1");
382                         Assert.IsTrue (new XamlMember (str_len, sctx).UnderlyingMember is PropertyInfo, "#2");
383                         Assert.AreEqual (dummy_get, new XamlMember ("DummyProp", dummy_get, dummy_set, sctx).UnderlyingMember, "#3");
384                         Assert.AreEqual (dummy_add, new XamlMember ("DummyAddMethod", dummy_add, sctx).UnderlyingMember, "#4");
385                         Assert.IsNull (new XamlMember ("Length", new XamlType (typeof (string), sctx), false).UnderlyingMember, "#5");
386                 }
387
388                 [Test]
389                 public void EqualsTest ()
390                 {
391                         XamlMember m;
392                         var xt = XamlLanguage.Type;
393                         m = new XamlMember ("Type", xt, false);
394                         var type_type = xt.GetMember ("Type");
395                         Assert.AreNotEqual (m, xt.GetMember ("Type"), "#1"); // whoa!
396                         Assert.AreNotEqual (type_type, m, "#2"); // whoa!
397                         Assert.AreEqual (type_type, xt.GetMember ("Type"), "#3");
398                         Assert.AreEqual (type_type.ToString (), m.ToString (), "#4");
399
400                         Assert.AreEqual (xt.GetAllMembers ().FirstOrDefault (mm => mm.Name == "Type"), xt.GetAllMembers ().FirstOrDefault (mm => mm.Name == "Type"), "#5");
401                         Assert.AreEqual (xt.GetAllMembers ().FirstOrDefault (mm => mm.Name == "Type"), xt.GetMember ("Type"), "#6");
402                 }
403
404                 [Test]
405                 public void ToStringTest ()
406                 {
407                         Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}_Initialization", XamlLanguage.Initialization.ToString (), "#1");
408
409                         // Wow. Uncomment this, and it will show .NET returns the XamlMember.ToString() results *inconsistently*.
410                         //Assert.AreEqual ("System.Windows.Markup.XData", XamlLanguage.XData.ToString (), "#2pre");
411                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, XamlLanguage.XData.PreferredXamlNamespace, "#2pre2");
412
413                         Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}XData.Text", XamlLanguage.XData.GetMember ("Text").ToString (), "#2");
414
415                         var pi = typeof (string).GetProperty ("Length");
416                         Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}String.Length", new XamlMember (pi, sctx).ToString (), "#3");
417
418                         Assert.AreEqual ("System.Xaml.XamlSchemaContext.FooBar", new XamlMember ("FooBar", typeof (XamlSchemaContext).GetMethod ("GetPreferredPrefix"), null, sctx).ToString (), "#4");
419
420                         Assert.AreEqual ("{urn:foo}bar", new XamlDirective ("urn:foo", "bar").ToString (), "#5");
421                 }
422         }
423 }