Merge branch 'cecil-light'
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml.Schema / XamlMemberInvokerTest.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.Reflection;
27 using System.Text;
28 using System.Windows.Markup;
29 using System.Xaml;
30 using System.Xaml.Schema;
31 using System.Xml;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Xaml.Schema
35 {
36         [TestFixture]
37         public class XamlMemberInvokerTest
38         {
39                 XamlSchemaContext sctx = new XamlSchemaContext (new XamlSchemaContextSettings ());
40                 PropertyInfo str_len = typeof (string).GetProperty ("Length");
41                 PropertyInfo sb_len = typeof (StringBuilder).GetProperty ("Length");
42                 PropertyInfo xr_resolver = typeof (XmlResolver).GetProperty ("Credentials");
43                 EventInfo ass_load = typeof (AppDomain).GetEvent ("AssemblyLoad");
44
45                 [Test]
46                 [ExpectedException (typeof (ArgumentNullException))]
47                 public void ConstructorNull ()
48                 {
49                         new XamlMemberInvoker (null);
50                 }
51
52                 // Property
53
54                 [Test]
55                 public void FromProperty ()
56                 {
57                         var pi = str_len;
58                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
59                         Assert.AreEqual (pi.GetGetMethod (), i.UnderlyingGetter, "#1");
60                         Assert.IsNull (i.UnderlyingSetter, "#2");
61                         Assert.AreEqual (5, i.GetValue ("hello"), "#3");
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentNullException))]
66                 public void GetValueNullObject ()
67                 {
68                         var pi = str_len;
69                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
70                         i.GetValue (null);
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentNullException))]
75                 public void SetValueNullObject ()
76                 {
77                         var pi = sb_len;
78                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
79                         i.SetValue (null, 5);
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (NotSupportedException))]
84                 public void GetValueOnWriteOnlyProperty ()
85                 {
86                         var pi = xr_resolver;
87                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
88                         i.GetValue (new XmlUrlResolver ());
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (NotSupportedException))]
93                 public void SetValueOnReadOnlyProperty ()
94                 {
95                         var pi = str_len;
96                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
97                         i.SetValue ("hello", 5);
98                 }
99
100                 [Test]
101                 public void SetValueOnReadWriteProperty ()
102                 {
103                         var pi = sb_len;
104                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
105                         var sb = new StringBuilder ();
106                         i.SetValue (sb, 5);
107                         Assert.AreEqual (5, sb.Length, "#1");
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (TargetException))]
112                 public void GetValueOnIrrelevantObject ()
113                 {
114                         var pi = str_len;
115                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
116                         i.GetValue (new StringBuilder ());
117                 }
118
119                 [Test]
120                 public void GetValueOnTypeValue ()
121                 {
122                         var xm = XamlLanguage.Type.GetMember ("Type");
123                         var i = new XamlMemberInvoker (xm);
124                         var o = i.GetValue (new TypeExtension (typeof (int)));
125                         Assert.AreEqual (typeof (int), o, "#1");
126                 }
127
128                 [Test]
129                 public void GetValueArrayExtension ()
130                 {
131                         var xt = sctx.GetXamlType (typeof (TestClass));
132                         var xm = xt.GetMember ("ArrayMember");
133                         Assert.IsNotNull (xm, "#-1");
134                         Assert.AreEqual (XamlLanguage.Array, xm.Type, "#0");
135                         var o = xm.Invoker.GetValue (new TestClass ());
136                         Assert.AreEqual (typeof (ArrayExtension), o.GetType (), "#1");
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (NotSupportedException))]
141                 public void GetValueInitialization ()
142                 {
143                         var xm = XamlLanguage.Initialization;
144                         var i = xm.Invoker;
145                         i.GetValue ("foo");
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (NotSupportedException))]
150                 public void GetValuePositionalParameter ()
151                 {
152                         var xm = XamlLanguage.PositionalParameters;
153                         var i = xm.Invoker;
154                         i.GetValue (new TypeExtension (typeof (int)));
155                 }
156
157                 [Test]
158                 [ExpectedException (typeof (TargetException))]
159                 public void SetValueOnIrrelevantObject ()
160                 {
161                         var pi = sb_len;
162                         var i = new XamlMemberInvoker (new XamlMember (pi, sctx));
163                         i.SetValue ("hello", 5);
164                 }
165
166                 // Event
167
168                 [Test]
169                 public void FromEvent ()
170                 {
171                         var ei = ass_load;
172                         var i = new XamlMemberInvoker (new XamlMember (ei, sctx));
173                         Assert.IsNull (i.UnderlyingGetter, "#1");
174                         Assert.AreEqual (ei.GetAddMethod (), i.UnderlyingSetter, "#2");
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (NotSupportedException))]
179                 public void GetValueOnEvent ()
180                 {
181                         var ei = ass_load;
182                         var i = new XamlMemberInvoker (new XamlMember (ei, sctx));
183                         i.GetValue (AppDomain.CurrentDomain);
184                 }
185
186                 [Test]
187                 public void SetValueOnEventNull ()
188                 {
189                         var ei = ass_load;
190                         var i = new XamlMemberInvoker (new XamlMember (ei, sctx));
191                         i.SetValue (AppDomain.CurrentDomain, null);
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof (ArgumentException))]
196                 public void SetValueOnEventValueMismatch ()
197                 {
198                         var ei = ass_load;
199                         var i = new XamlMemberInvoker (new XamlMember (ei, sctx));
200                         i.SetValue (AppDomain.CurrentDomain, 5);
201                 }
202
203                 void DummyAssemblyLoad (object o, AssemblyLoadEventArgs e)
204                 {
205                 }
206
207                 [Test]
208                 public void SetValueOnEvent ()
209                 {
210                         var ei = ass_load;
211                         var i = new XamlMemberInvoker (new XamlMember (ei, sctx));
212                         i.SetValue (AppDomain.CurrentDomain, new AssemblyLoadEventHandler (DummyAssemblyLoad));
213                 }
214
215                 [Test]
216                 public void CustomTypeDefaultValues ()
217                 {
218                         var i = new MyXamlMemberInvoker ();
219                         Assert.IsNull (i.UnderlyingGetter, "#1");
220                         Assert.IsNull (i.UnderlyingSetter, "#2");
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof (MyException))]
225                 public void UnderlyingGetter ()
226                 {
227                         var i = new XamlMemberInvoker (new MyXamlMember (str_len, sctx));
228                         // call XamlMember's UnderlyingGetter.
229                         Assert.IsNotNull (i.UnderlyingGetter, "#1");
230                 }
231
232                 [Test]
233                 [ExpectedException (typeof (MyException))]
234                 public void UnderlyingSetter ()
235                 {
236                         var i = new XamlMemberInvoker (new MyXamlMember (str_len, sctx));
237                         // call XamlMember's UnderlyingSetter.
238                         Assert.IsNull (i.UnderlyingSetter, "#1");
239                 }
240
241                 class MyXamlMember : XamlMember
242                 {
243                         public MyXamlMember (PropertyInfo pi, XamlSchemaContext context)
244                                 : base (pi, context)
245                         {
246                         }
247                         
248                         protected override MethodInfo LookupUnderlyingGetter ()
249                         {
250                                 throw new MyException ();
251                         }
252
253                         protected override MethodInfo LookupUnderlyingSetter ()
254                         {
255                                 throw new MyException ();
256                         }
257                 }
258
259                 class MyException : Exception
260                 {
261                 }
262
263                 class MyXamlMemberInvoker : XamlMemberInvoker
264                 {
265                 }
266
267                 class TestClass
268                 {
269                         public TestClass ()
270                         {
271                                 ArrayMember = new ArrayExtension (typeof (int));
272                                 ArrayMember.AddChild (5);
273                                 ArrayMember.AddChild (3);
274                                 ArrayMember.AddChild (-1);
275                         }
276
277                         public ArrayExtension ArrayMember { get; set; }
278                 }
279         }
280 }