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