[msbuild] Getting build error "Error initializing task XmlPeek: Not registered task...
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml / TestedTypes.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.Collections.ObjectModel;
27 using System.ComponentModel;
28 using System.Diagnostics;
29 using System.Globalization;
30 using System.IO;
31 using System.Linq;
32 using System.Reflection;
33 using System.Windows.Markup;
34 using System.Xaml;
35 using System.Xaml.Schema;
36 using System.Xml;
37 using System.Xml.Schema;
38 using System.Xml.Serialization;
39
40 [assembly: XmlnsDefinition ("http://www.domain.com/path", "XamlTest")] // bug #680385
41 [assembly: XmlnsDefinition ("http://www.domain.com/path", "SecondTest")] // bug #681045, same xmlns key for different clrns.
42
43 [assembly: XmlnsDefinition ("http://schemas.example.com/test", "XamarinBug3003")] // bug #3003
44 [assembly: XmlnsPrefix ("http://schemas.example.com/test", "test")] // bug #3003
45
46 namespace MonoTests.System.Xaml
47 {
48         public class ArgumentAttributed
49         {
50                 public ArgumentAttributed (string s1, string s2)
51                 {
52                         Arg1 = s1;
53                         Arg2 = s2;
54                 }
55
56                 [ConstructorArgument ("s1")]
57                 public string Arg1 { get; set; }
58
59                 [ConstructorArgument ("s2")]
60                 public string Arg2 { get; set; }
61         }
62
63         public class ComplexPositionalParameterWrapper
64         {
65                 public ComplexPositionalParameterWrapper ()
66                 {
67                 }
68                 
69                 public ComplexPositionalParameterClass Param { get; set; }
70         }
71         
72         [TypeConverter (typeof (ComplexPositionalParameterClassConverter))]
73         public class ComplexPositionalParameterClass : MarkupExtension
74         {
75                 public ComplexPositionalParameterClass (ComplexPositionalParameterValue value)
76                 {
77                         this.Value = value;
78                 }
79
80                 [ConstructorArgument ("value")]
81                 public ComplexPositionalParameterValue Value { get; private set; }
82                 
83                 public override object ProvideValue (IServiceProvider sp)
84                 {
85                         return Value.Foo;
86                 }
87         }
88         
89         public class ComplexPositionalParameterClassConverter : TypeConverter
90         {
91                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
92                 {
93                         return sourceType == typeof (string);
94                 }
95
96                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object valueToConvert)
97                 {
98                         return new ComplexPositionalParameterClass (new ComplexPositionalParameterValue () {Foo = (string) valueToConvert});
99                 }
100
101                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
102                 {
103                         // conversion to string is not supported.
104                         return destinationType == typeof (ComplexPositionalParameterClass);
105                 }
106         }
107         
108         public class ComplexPositionalParameterValue
109         {
110                 public string Foo { get; set; }
111         }
112         
113         //[MarkupExtensionReturnType (typeof (Array))]
114         //[ContentProperty ("Items")]  ... so, these attributes do not affect XamlObjectReader.
115         public class MyArrayExtension : MarkupExtension
116         {
117                 public MyArrayExtension ()
118                 {
119                         items = new ArrayList ();
120                 }
121
122                 public MyArrayExtension (Array array)
123                 {
124                         items = new ArrayList (array);
125                         this.Type = array.GetType ().GetElementType ();
126                 }
127                 
128                 public MyArrayExtension (Type type)
129                         : this ()
130                 {
131                         this.Type = type;
132                 }
133
134                 IList items;
135                 public IList Items {
136                         get { return items; }
137                         private set { items = value; }
138                 }
139                 
140                 [ConstructorArgument ("type")]
141                 public Type Type { get; set; }
142                 
143                 public override object ProvideValue (IServiceProvider serviceProvider)
144                 {
145                         if (Type == null)
146                                 throw new InvalidOperationException ("Type property must be set before calling ProvideValue method");
147
148                         Array a = Array.CreateInstance (Type, Items.Count);
149                         Items.CopyTo (a, 0);
150                         return a;
151                 }
152         }
153
154         // The trailing "A" gives significant difference in XML output!
155         public class MyArrayExtensionA : MarkupExtension
156         {
157                 public MyArrayExtensionA ()
158                 {
159                         items = new ArrayList ();
160                 }
161
162                 public MyArrayExtensionA (Array array)
163                 {
164                         items = new ArrayList (array);
165                         this.Type = array.GetType ().GetElementType ();
166                 }
167                 
168                 public MyArrayExtensionA (Type type)
169                         : this ()
170                 {
171                         this.Type = type;
172                 }
173
174                 IList items;
175                 public IList Items {
176                         get { return items; }
177                         private set { items = value; }
178                 }
179                 
180                 [ConstructorArgument ("type")]
181                 public Type Type { get; set; }
182                 
183                 public override object ProvideValue (IServiceProvider serviceProvider)
184                 {
185                         if (Type == null)
186                                 throw new InvalidOperationException ("Type property must be set before calling ProvideValue method");
187
188                         Array a = Array.CreateInstance (Type, Items.Count);
189                         Items.CopyTo (a, 0);
190                         return a;
191                 }
192         }
193
194         class TestClass1
195         {
196         }
197
198         public class TestClass3
199         {
200                 public TestClass3 Nested { get; set; }
201         }
202
203         public class TestClass4
204         {
205                 public string Foo { get; set; }
206                 public string Bar { get; set; }
207         }
208         
209         public class TestClass5
210         {
211                 public static string Foo { get; set; }
212                 public string Bar { get; set; }
213                 public string Baz { internal get; set; }
214                 public string ReadOnly {
215                         get { return Foo; }
216                 }
217         }
218
219         public class MyExtension : MarkupExtension
220         {
221                 public MyExtension ()
222                 {
223                 }
224
225                 public MyExtension (Type arg1, string arg2, string arg3)
226                 {
227                         Foo = arg1;
228                         Bar = arg2;
229                         Baz = arg3;
230                 }
231
232                 [ConstructorArgument ("arg1")]
233                 public Type Foo { get; set; }
234                 
235                 [ConstructorArgument ("arg2")]
236                 public string Bar { get; set; }
237                 
238                 [ConstructorArgument ("arg3")]
239                 public string Baz { get; set; }
240
241                 public override object ProvideValue (IServiceProvider provider)
242                 {
243                         return "provided_value";
244                 }
245         }
246
247         [TypeConverter (typeof (StringConverter))] // This attribute is the markable difference between MyExtension and this type.
248         public class MyExtension2 : MarkupExtension
249         {
250                 public MyExtension2 ()
251                 {
252                 }
253
254                 public MyExtension2 (Type arg1, string arg2)
255                 {
256                         Foo = arg1;
257                         Bar = arg2;
258                 }
259
260                 [ConstructorArgument ("arg1")]
261                 public Type Foo { get; set; }
262                 
263                 [ConstructorArgument ("arg2")]
264                 public string Bar { get; set; }
265
266                 public override object ProvideValue (IServiceProvider provider)
267                 {
268                         return "provided_value";
269                 }
270         }
271
272         [TypeConverter (typeof (StringConverter))] // same as MyExtension2 except that it is *not* MarkupExtension.
273         public class MyExtension3
274         {
275                 public MyExtension3 ()
276                 {
277                 }
278
279                 // cf. According to [MS-XAML-2009] 3.2.1.11, constructors are invalid unless the type is derived from TypeExtension. So, it is likely *ignored*.
280                 public MyExtension3 (Type arg1, string arg2)
281                 {
282                         Foo = arg1;
283                         Bar = arg2;
284                 }
285
286                 [ConstructorArgument ("arg1")]
287                 public Type Foo { get; set; }
288                 
289                 [ConstructorArgument ("arg2")]
290                 public string Bar { get; set; }
291         }
292
293         [TypeConverter (typeof (DateTimeConverter))] // same as MyExtension3 except for the type converter.
294         public class MyExtension4
295         {
296                 public MyExtension4 ()
297                 {
298                 }
299
300                 // cf. According to [MS-XAML-2009] 3.2.1.11, constructors are invalid unless the type is derived from TypeExtension. So, it is likely *ignored*.
301                 public MyExtension4 (Type arg1, string arg2)
302                 {
303                         Foo = arg1;
304                         Bar = arg2;
305                 }
306
307                 [ConstructorArgument ("arg1")]
308                 public Type Foo { get; set; }
309                 
310                 [ConstructorArgument ("arg2")]
311                 public string Bar { get; set; }
312         }
313
314         // no type converter, and there are only simple-type arguments == _PositionalParameters is applicable.
315         public class MyExtension5 : MarkupExtension
316         {
317                 public MyExtension5 (string arg1, string arg2)
318                 {
319                         Foo = arg1;
320                         Bar = arg2;
321                 }
322
323                 [ConstructorArgument ("arg1")]
324                 public string Foo { get; set; }
325                 
326                 [ConstructorArgument ("arg2")]
327                 public string Bar { get; set; }
328                 
329                 public override object ProvideValue (IServiceProvider sp)
330                 {
331                         return Foo;
332                 }
333         }
334
335         // Almost the same as MyExtension5, BUT there is default constructor which XamlObjectReader prefers.
336         public class MyExtension6 : MarkupExtension
337         {
338                 public MyExtension6 ()
339                 {
340                 }
341
342                 public MyExtension6 (string arg1)
343                 {
344                         Foo = arg1;
345                 }
346
347                 [ConstructorArgument ("arg1")]
348                 public string Foo { get; set; }
349                 
350                 public override object ProvideValue (IServiceProvider sp)
351                 {
352                         return Foo;
353                 }
354         }
355
356         public class PositionalParametersClass1 : MarkupExtension
357         {
358                 public PositionalParametersClass1 (string foo)
359                         : this (foo, -1)
360                 {
361                 }
362                 
363                 public PositionalParametersClass1 (string foo, int bar)
364                 {
365                         Foo = foo;
366                         Bar = bar;
367                 }
368                 
369                 [ConstructorArgument ("foo")]
370                 public string Foo { get; set; }
371
372                 [ConstructorArgument ("bar")]
373                 public int Bar { get; set; }
374
375                 public override object ProvideValue (IServiceProvider sp)
376                 {
377                         return Foo;
378                 }
379         }
380
381         public class PositionalParametersWrapper
382         {
383                 public PositionalParametersClass1 Body { get; set; }
384                 
385                 public PositionalParametersWrapper ()
386                 {
387                 }
388                 
389                 public PositionalParametersWrapper (string foo, int bar)
390                 {
391                         Body = new PositionalParametersClass1 (foo, bar);
392                 }
393         }
394         
395         public class ListWrapper
396         {
397                 public ListWrapper ()
398                 {
399                         Items = new List<int> ();
400                 }
401
402                 public ListWrapper (List<int> items)
403                 {
404                         Items = items;
405                 }
406
407                 public List<int> Items { get; private set; }
408         }
409         
410         public class ListWrapper2
411         {
412                 public ListWrapper2 ()
413                 {
414                         Items = new List<int> ();
415                 }
416
417                 public ListWrapper2 (List<int> items)
418                 {
419                         Items = items;
420                 }
421
422                 public List<int> Items { get; set; } // it is settable, which makes difference.
423         }
424
425         [ContentProperty ("Content")]
426         public class ContentIncludedClass
427         {
428                 public string Content { get; set; }
429         }
430
431         public class StaticClass1
432         {
433                 static StaticClass1 ()
434                 {
435                         FooBar = "test";
436                 }
437
438                 public static string FooBar { get; set; }
439         }
440
441         public class StaticExtensionWrapper
442         {
443                 public StaticExtensionWrapper ()
444                 {
445                 }
446                 
447                 public StaticExtension Param { get; set; }
448
449                 public static string Foo = "foo";
450         }
451         
452         public class TypeExtensionWrapper
453         {
454                 public TypeExtensionWrapper ()
455                 {
456                 }
457                 
458                 public TypeExtension Param { get; set; }
459         }
460         
461         public class XDataWrapper
462         {
463                 public XData Markup { get; set; }
464         }
465         
466         // FIXME: test it with XamlXmlReader (needs to create xml first)
467         public class EventContainer
468         {
469                 public event Action Run;
470         }
471         
472         public class NamedItem
473         {
474                 public NamedItem ()
475                 {
476                         References = new List<NamedItem> ();
477                 }
478                 
479                 public NamedItem (string name)
480                         : this ()
481                 {
482                         ItemName = name;
483                 }
484                 
485                 public string ItemName { get; set; }
486                 public IList<NamedItem> References { get; private set; }
487         }
488         
489         [RuntimeNameProperty ("ItemName")]
490         public class NamedItem2
491         {
492                 public NamedItem2 ()
493                 {
494                         References = new List<NamedItem2> ();
495                 }
496                 
497                 public NamedItem2 (string name)
498                         : this ()
499                 {
500                         ItemName = name;
501                 }
502                 
503                 public string ItemName { get; set; }
504                 public IList<NamedItem2> References { get; private set; }
505         }
506
507         [TypeConverter (typeof (TestValueConverter))]
508         public class TestValueSerialized
509         {
510                 public TestValueSerialized ()
511                 {
512                 }
513
514                 public string Foo { get; set; }
515         }
516
517         public class TestValueConverter : TypeConverter
518         {
519                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
520                 {
521                         //Console.Error.WriteLine ("### {0}:{1}", sourceType, context);
522                         ValueSerializerContextTest.Context = (IValueSerializerContext) context;
523                         return true;
524                 }
525                 
526                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object source)
527                 {
528                         //Console.Error.WriteLine ("##### {0}:{1}", source, context);
529                         ValueSerializerContextTest.Provider = (IServiceProvider) context;
530                         var sp = context as IServiceProvider;
531                         // ValueSerializerContextTest.Context = (IValueSerializerContext) context; -> causes InvalidCastException
532                         if ((source as string) == "v")
533                                 return new TestValueSerialized ();
534                         throw new Exception ("huh");
535                 }
536
537                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
538                 {
539                         //Console.Error.WriteLine ("$$$ {0}:{1}", destinationType, context);
540                         ValueSerializerContextTest.Context = (IValueSerializerContext) context;
541                         return destinationType != typeof (MarkupExtension);
542                 }
543         }
544
545         [ContentProperty ("Value")]
546         public class XmlSerializableWrapper
547         {
548                 public XmlSerializableWrapper () // mandatory
549                         : this (new XmlSerializable ())
550                 {
551                 }
552
553                 public XmlSerializableWrapper (XmlSerializable val)
554                 {
555                         this.val = val;
556                 }
557
558                 XmlSerializable val;
559
560                 public XmlSerializable Value {
561                         get { return val; }
562                         // To make it become XData, it cannot have a setter.
563                 }
564         }
565
566         public class XmlSerializable : IXmlSerializable
567         {
568                 public XmlSerializable ()
569                 {
570                 }
571
572                 public XmlSerializable (string raw)
573                 {
574                         this.raw = raw;
575                 }
576
577                 string raw;
578
579                 public string GetRaw ()
580                 {
581                         return raw;
582                 }
583
584                 public void ReadXml (XmlReader reader)
585                 {
586                         reader.MoveToContent ();
587                         raw = reader.ReadOuterXml ();
588                 }
589                 
590                 public void WriteXml (XmlWriter writer)
591                 {
592                         if (raw != null) {
593                                 var xr = XmlReader.Create (new StringReader (raw));
594                                 while (!xr.EOF)
595                                         writer.WriteNode (xr, false);
596                         }
597                 }
598                 
599                 public XmlSchema GetSchema ()
600                 {
601                         return null;
602                 }
603         }
604         
605         public class Attachable
606         {
607                 public static readonly AttachableMemberIdentifier FooIdentifier = new AttachableMemberIdentifier (typeof (Attachable), "Foo");
608                 public static readonly AttachableMemberIdentifier ProtectedIdentifier = new AttachableMemberIdentifier (typeof (Attachable), "Protected");
609                 
610                 public static string GetFoo (object target)
611                 {
612                         string v;
613                         return AttachablePropertyServices.TryGetProperty (target, FooIdentifier, out v) ? v : null;
614                 }
615                 
616                 public static void SetFoo (object target, string value)
617                 {
618                         AttachablePropertyServices.SetProperty (target, FooIdentifier, value);
619                 }
620
621                 public static string GetBar (object target, object signatureMismatch)
622                 {
623                         return null;
624                 }
625                 
626                 public static void SetBar (object signatureMismatch)
627                 {
628                 }
629
630                 public static void GetBaz (object noReturnType)
631                 {
632                 }
633                 
634                 public static string SetBaz (object target, object extraReturnType)
635                 {
636                         return null;
637                 }
638
639                 protected static string GetProtected (object target)
640                 {
641                         string v;
642                         return AttachablePropertyServices.TryGetProperty (target, ProtectedIdentifier, out v) ? v : null;
643                 }
644                 
645                 protected static void SetProtected (object target, string value)
646                 {
647                         AttachablePropertyServices.SetProperty (target, ProtectedIdentifier, value);
648                 }
649
650                 static Dictionary<object,List<EventHandler>> handlers = new Dictionary<object,List<EventHandler>> ();
651
652                 public static void AddXHandler (object target, EventHandler handler)
653                 {
654                         List<EventHandler> l;
655                         if (!handlers.TryGetValue (target, out l)) {
656                                 l = new List<EventHandler> ();
657                                 handlers [target] = l;
658                         }
659                         l.Add (handler);
660                 }
661
662                 public static void RemoveXHandler (object target, EventHandler handler)
663                 {
664                         handlers [target].Remove (handler);
665                 }
666         }
667         
668         public class AttachedPropertyStore : IAttachedPropertyStore
669         {
670                 public AttachedPropertyStore ()
671                 {
672                 }
673                 
674                 Dictionary<AttachableMemberIdentifier,object> props = new Dictionary<AttachableMemberIdentifier,object> ();
675
676                 public int PropertyCount {
677                         get { return props.Count; }
678                 }
679                 
680                 public void CopyPropertiesTo (KeyValuePair<AttachableMemberIdentifier, object> [] array, int index)
681                 {
682                         ((ICollection<KeyValuePair<AttachableMemberIdentifier, object>>) props).CopyTo (array, index);
683                 }
684                 
685                 public bool RemoveProperty (AttachableMemberIdentifier attachableMemberIdentifier)
686                 {
687                         return props.Remove (attachableMemberIdentifier);
688                 }
689                 
690                 public void SetProperty (AttachableMemberIdentifier attachableMemberIdentifier, object value)
691                 {
692                         props [attachableMemberIdentifier] = value;
693                 }
694                 
695                 public bool TryGetProperty (AttachableMemberIdentifier attachableMemberIdentifier, out object value)
696                 {
697                         return props.TryGetValue (attachableMemberIdentifier, out value);
698                 }
699         }
700
701         public class AttachedWrapper : AttachedPropertyStore
702         {
703                 public AttachedWrapper ()
704                 {
705                         Value = new Attached ();
706                 }
707
708                 public Attached Value { get; set; }
709         }
710
711         public class AttachedWrapper2
712         {
713                 public static readonly AttachableMemberIdentifier FooIdentifier = new AttachableMemberIdentifier (typeof (AttachedWrapper2), "Foo");
714
715                 static AttachedPropertyStore store = new AttachedPropertyStore ();
716
717                 public static string GetFoo (object target)
718                 {
719                         object v;
720                         return store.TryGetProperty (FooIdentifier, out v) ? (string) v : null;
721                 }
722                 
723                 public static void SetFoo (object target, string value)
724                 {
725                         store.SetProperty (FooIdentifier, value);
726                 }
727
728                 public static int PropertyCount {
729                         get { return store.PropertyCount; }
730                 }
731
732                 public AttachedWrapper2 ()
733                 {
734                         Value = new Attached ();
735                 }
736
737                 public Attached Value { get; set; }
738         }
739
740         public class Attached : Attachable
741         {
742         }
743
744         public class Attached2
745         {
746                 internal String Property { get; set; }
747         }
748         
749         public class AttachedWrapper3
750         {
751                 public static void SetProperty (Attached2 a, string value)
752                 {
753                         a.Property = value;
754                 }
755         }
756
757         public class EventStore
758         {
759                 public bool Method1Invoked;
760
761                 public event EventHandler<EventArgs> Event1;
762                 public event Func<object> Event2;
763
764                 public object Examine ()
765                 {
766                         if (Event1 != null)
767                                 Event1 (this, EventArgs.Empty);
768                         if (Event2 != null)
769                                 return Event2 ();
770                         else
771                                 return null;
772                 }
773
774                 public void Method1 ()
775                 {
776                         throw new Exception ();
777                 }
778
779                 public void Method1 (object o, EventArgs e)
780                 {
781                         Method1Invoked = true;
782                 }
783
784                 public object Method2 ()
785                 {
786                         return "foo";
787                 }
788         }
789
790         public class EventStore2<TEventArgs> where TEventArgs : EventArgs
791         {
792                 public bool Method1Invoked;
793
794                 public event EventHandler<TEventArgs> Event1;
795                 public event Func<object> Event2;
796
797                 public object Examine ()
798                 {
799                         if (Event1 != null)
800                                 Event1 (this, default (TEventArgs));
801                         if (Event2 != null)
802                                 return Event2 ();
803                         else
804                                 return null;
805                 }
806
807                 public void Method1 ()
808                 {
809                         throw new Exception ();
810                 }
811
812                 public void Method1 (object o, EventArgs e)
813                 {
814                         throw new Exception ();
815                 }
816
817                 public void Method1 (object o, TEventArgs e)
818                 {
819                         Method1Invoked = true;
820                 }
821
822                 public object Method2 ()
823                 {
824                         return "foo";
825                 }
826         }
827
828         public class AbstractContainer
829         {
830                 public AbstractObject Value1 { get; set; }
831                 public AbstractObject Value2 { get; set; }
832         }
833         
834         public abstract class AbstractObject
835         {
836                 public abstract string Foo { get; set; }
837         }
838
839         public class DerivedObject : AbstractObject
840         {
841                 public override string Foo { get; set; }
842         }
843
844         public class ReadOnlyPropertyContainer
845         {
846                 string foo;
847                 public string Foo {
848                         get { return foo; }
849                         set { foo = Bar = value; }
850                 }
851                 public string Bar { get; private set; }
852         }
853
854         public class EnumContainer
855         {
856                 public EnumValueType EnumProperty { get; set; }
857         }
858
859         public enum EnumValueType
860         {
861                 One,
862                 Two,
863                 Three,
864                 Four
865         }
866
867         [ContentProperty ("ListOfItems")]
868         public class CollectionContentProperty
869         {
870                 public IList<SimpleClass> ListOfItems { get; set; }
871
872                 public CollectionContentProperty ()
873                 {
874                         this.ListOfItems = new List<SimpleClass> ();
875                 }
876         }
877
878         [ContentProperty ("ListOfItems")]
879         public class CollectionContentPropertyX
880         {
881                 public IList ListOfItems { get; set; }
882
883                 public CollectionContentPropertyX ()
884                 {
885                         this.ListOfItems = new List<IEnumerable> ();
886                 }
887         }
888
889         public class SimpleClass
890         {
891         }
892
893         public class NullableContainer
894         {
895                 public int? TestProp { get; set; }
896         }
897
898         public class DirectListContainer // for such xml that directly contains items in <*.Items> element.
899         {
900                 public IList<DirectListContent> Items { get; set; }
901
902                 public DirectListContainer ()
903                 {
904                         this.Items = new List<DirectListContent> ();
905                 }
906         }
907
908         public class DirectListContent
909         {
910                 public string Value { get; set; }
911         }
912
913         public class DirectDictionaryContainer // for such xml that directly contains items in <*.Items> element.
914         {
915                 public IDictionary<EnumValueType,int> Items { get; set; }
916
917                 public DirectDictionaryContainer ()
918                 {
919                         this.Items = new Dictionary<EnumValueType,int> ();
920                 }
921         }
922 }
923
924 namespace XamlTest
925 {
926         public class Configurations : List<Configuration>
927         {
928                 private Configuration active;
929                 private bool isFrozen;
930
931                 public Configuration Active {
932                         get { return this.active; }
933                         set {
934                                 if (this.isFrozen) {
935                                 throw new InvalidOperationException ("The 'Active' configuration can only be changed via modifying the source file (" + this.Source + ").");
936                                 }
937
938                                 this.active = value;
939                         }
940                 }
941
942                 public string Source { get; private set; }
943         }
944
945         public class Configuration
946         {
947                 public string Version { get; set; }
948
949                 public string Path { get; set; }
950         }
951 }
952
953 // see bug #681480
954 namespace SecondTest
955 {
956         public class TypeOtherAssembly
957         {
958                 [TypeConverter (typeof (NullableUintListConverter))]
959                 public List<uint?> Values { get; set; }
960
961                 public TypeOtherAssembly ()
962                 {
963                         this.Values = new List<uint?> ();
964                 }
965         }
966
967         public class NullableUintListConverter : CustomTypeConverterBase
968         {
969                 public override object ConvertFrom (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
970                 {
971                         string configValue = value as string;
972                         if (string.IsNullOrWhiteSpace (configValue))
973                                 return null;
974
975                         string delimiterStr = ", ";
976                         char [] delimiters = delimiterStr.ToCharArray ();
977                         string [] tokens = configValue.Split (delimiters, StringSplitOptions.RemoveEmptyEntries);
978
979                         List<uint?> parsedList = new List<uint?> (tokens.Length);
980                         foreach (string token in tokens)
981                                 parsedList.Add(uint.Parse(token));
982
983                         return parsedList;
984                 }
985
986                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
987                 {
988                         var v = (List<uint?>) value;
989                         return String.Join (", ", (from i in v select i.ToString ()).ToArray ());
990                 }
991         }
992         
993         public class CustomTypeConverterBase : TypeConverter
994         {
995                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
996                 {
997                         if (sourceType == typeof (string))
998                         {
999                                 return true;
1000                         }
1001                         return base.CanConvertFrom (context, sourceType);
1002                 }
1003         }
1004
1005         #region bug #681202
1006
1007         [MarkupExtensionReturnType (typeof (object))]
1008         public class ResourceExtension : MarkupExtension
1009         {
1010                 [ConstructorArgument ("key")]
1011                 public object Key { get; set; }
1012
1013                 public ResourceExtension (object key)
1014                 {
1015                         this.Key = key;
1016                 }
1017
1018                 public override object ProvideValue (IServiceProvider serviceProvider)
1019                 {
1020                         IXamlSchemaContextProvider service = serviceProvider.GetService (typeof (IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
1021                         IAmbientProvider provider = serviceProvider.GetService (typeof (IAmbientProvider)) as IAmbientProvider;
1022                         Debug.Assert (provider != null, "The provider should not be null!");
1023
1024                         XamlSchemaContext schemaContext = service.SchemaContext;
1025                         XamlType[] types = new XamlType [] { schemaContext.GetXamlType (typeof (ResourcesDict)) };
1026
1027                         // ResourceDict is marked as Ambient, so the instance current being deserialized should be in this list.
1028                         List<AmbientPropertyValue> list = provider.GetAllAmbientValues (null, false, types) as List<AmbientPropertyValue>;
1029                         if (list.Count != 1)
1030                                 throw new Exception ("expected ambient property count == 1 but " + list.Count);
1031                         for (int i = 0; i < list.Count; i++) {
1032                                 AmbientPropertyValue value = list [i];
1033                                 ResourcesDict dict = value.Value as ResourcesDict;
1034
1035                                 // For this example, we know that dict should not be null and that it is the only value in list.
1036                                 object result = dict [this.Key];
1037                                 return result;
1038                         }
1039
1040                         return null;
1041                 }
1042         }
1043
1044         [UsableDuringInitialization (true), Ambient]
1045         public class ResourcesDict : Dictionary<object, object>
1046         {
1047         }
1048
1049         public class TestObject
1050         {
1051                 public TestObject TestProperty { get; set; }
1052         }
1053
1054         #endregion
1055
1056         public class ResourcesDict2 : Dictionary<object, object>
1057         {
1058         }
1059
1060         public class TestObject2
1061         {
1062                 public string TestProperty { get; set; }
1063         }
1064
1065         #region bug #683290
1066         [ContentProperty ("Items")]
1067         public class SimpleType
1068         {
1069                 public IList<SimpleType> Items { get; set; }
1070                 
1071                 public IList<SimpleType> NonContentItems { get; set; }
1072                 
1073                 public string TestProperty { get; set; }
1074                 
1075                 public SimpleType ()
1076                 {
1077                         this.Items = new List<SimpleType> ();
1078                         this.NonContentItems=new List<SimpleType> ();
1079                 }
1080         }
1081         
1082         public class ContentPropertyContainer : Dictionary<object, object>
1083         {
1084         }
1085         #endregion
1086 }
1087
1088 #region "xamarin bug #2927"
1089 namespace XamarinBug2927
1090 {
1091         public class RootClass
1092         {
1093                 public RootClass ()
1094                 {
1095                         Child = new MyChildClass ();
1096                 }
1097                 
1098                 public bool Invoked;
1099                 
1100                 public ChildClass Child { get; set; }
1101         }
1102
1103         public class MyRootClass : RootClass
1104         {
1105                 public void HandleMyEvent (object sender, EventArgs e)
1106                 {
1107                         Invoked = true;
1108                 }
1109         }
1110
1111         public class RootClass2
1112         {
1113                 public RootClass2 ()
1114                 {
1115                         Child = new MyChildClass ();
1116                 }
1117                 
1118                 public bool Invoked;
1119                 
1120                 public ChildClass Child { get; set; }
1121                 
1122                 public void HandleMyEvent (object sender, EventArgs e)
1123                 {
1124                         Invoked = true;
1125                 }
1126         }
1127
1128         public class MyRootClass2 : RootClass2
1129         {
1130         }
1131
1132         public class ChildClass
1133         {
1134                 public bool Invoked;
1135                 
1136                 public DescendantClass Descendant { get; set; }
1137         }
1138
1139         public class MyChildClass : ChildClass
1140         {
1141                 public MyChildClass ()
1142                 {
1143                         Descendant = new DescendantClass () { Value = "x" };
1144                 }
1145         
1146                 public void HandleMyEvent (object sender, EventArgs e)
1147                 {
1148                         Invoked = true;
1149                 }
1150         }
1151
1152         public class DescendantClass
1153         {
1154                 public bool Invoked;
1155                 public event EventHandler DoWork;
1156                 public string Value { get; set; }
1157         
1158                 public void Work ()
1159                 {
1160                         DoWork (this, EventArgs.Empty);
1161                 }
1162         
1163                 public void HandleMyEvent (object sender, EventArgs e)
1164                 {
1165                         Invoked = true;
1166                 }
1167         }
1168 }
1169 #endregion
1170
1171 #region "xamarin bug 3003"
1172
1173 namespace XamarinBug3003
1174 {
1175         public static class TestContext
1176         {
1177                 public static StringWriter Writer = new StringWriter ();
1178                 
1179                 public const string XmlInput = @"<Parent xmlns='http://schemas.example.com/test' Title='Parent Title'>
1180         <Child Parent.AssociatedProperty='child 1' Title='Child Title 1'></Child>       
1181         <Child Parent.AssociatedProperty='child 2' Title='Child Title 2'></Child>       
1182 </Parent>";
1183                 
1184                 // In bug #3003 repro, there is output "Item 'Child' inserted at index 'x'" , but I couldn't get it in the output either on .NET or Mono.
1185                 // On the other hand, in the standalone repro case they are in the output either in mono or in .NET. So I just stopped caring about that as it works as expected.
1186                 public const string ExpectedResult = @"
1187 Parent Constructed
1188 ISupportInitialize.BeginInit: Parent
1189 XamlObjectWriterSettings.AfterBeginInit: Parent
1190 XamlObjectWriterSettings.BeforeProperties: Parent
1191 XamlObjectWriterSettings.XamlSetValue: Parent Title, Member: Title
1192 Parent.Title_set: Parent
1193 Child Constructed
1194 ISupportInitialize.BeginInit: Child
1195 XamlObjectWriterSettings.AfterBeginInit: Child
1196 XamlObjectWriterSettings.BeforeProperties: Child
1197 XamlObjectWriterSettings.XamlSetValue: child 1, Member: AssociatedProperty
1198 Parent.SetAssociatedProperty: child 1
1199 XamlObjectWriterSettings.XamlSetValue: Child Title 1, Member: Title
1200 Child.Title_set: Child
1201 XamlObjectWriterSettings.AfterProperties: Child
1202 ISupportInitialize.EndInit: Child
1203 XamlObjectWriterSettings.AfterEndInit: Child
1204 Child Constructed
1205 ISupportInitialize.BeginInit: Child
1206 XamlObjectWriterSettings.AfterBeginInit: Child
1207 XamlObjectWriterSettings.BeforeProperties: Child
1208 XamlObjectWriterSettings.XamlSetValue: child 2, Member: AssociatedProperty
1209 Parent.SetAssociatedProperty: child 2
1210 XamlObjectWriterSettings.XamlSetValue: Child Title 2, Member: Title
1211 Child.Title_set: Child
1212 XamlObjectWriterSettings.AfterProperties: Child
1213 ISupportInitialize.EndInit: Child
1214 XamlObjectWriterSettings.AfterEndInit: Child
1215 XamlObjectWriterSettings.AfterProperties: Parent
1216 ISupportInitialize.EndInit: Parent
1217 XamlObjectWriterSettings.AfterEndInit: Parent
1218 Loaded Parent
1219 ";
1220         }
1221
1222         public class BaseItemCollection : Collection<BaseItem>
1223         {
1224                 protected override void InsertItem (int index, BaseItem item)
1225                 {
1226                         base.InsertItem (index, item);
1227                         Console.WriteLine ("Item '{0}' inserted at index '{1}'", item, index);
1228                 }
1229         }
1230
1231         public class BaseItem : ISupportInitialize
1232         {
1233                 Dictionary<string, object> properties = new Dictionary<string, object> ();
1234                 
1235                 public Dictionary<string, object> Properties
1236                 {
1237                         get { return properties; }
1238                 }
1239
1240                 string title;
1241
1242                 public string Title
1243                 {
1244                         get { return title; }
1245                         set
1246                         {
1247                                 title = value;
1248                                 TestContext.Writer.WriteLine ("{0}.Title_set: {0}", this.GetType ().Name, value);
1249                         }
1250                 }
1251
1252                 public BaseItem ()
1253                 {
1254                         TestContext.Writer.WriteLine ("{0} Constructed", this.GetType ().Name);
1255                 }
1256
1257
1258                 public void BeginInit ()
1259                 {
1260                         TestContext.Writer.WriteLine ("ISupportInitialize.BeginInit: {0}", this);
1261                 }
1262
1263                 public void EndInit ()
1264                 {
1265                         TestContext.Writer.WriteLine ("ISupportInitialize.EndInit: {0}", this);
1266                 }
1267
1268                 public override string ToString ()
1269                 {
1270                         return this.GetType ().Name.ToString ();
1271                 }
1272         }
1273
1274         public class Child : BaseItem
1275         {
1276         }
1277
1278         [ContentProperty ("Children")]
1279         public class Parent : BaseItem
1280         {
1281                 BaseItemCollection children = new BaseItemCollection ();
1282                 
1283                 public BaseItemCollection Children
1284                 {
1285                         get { return children; }
1286                 }
1287                 
1288                 
1289                 public static string GetAssociatedProperty (Child child)
1290                 {
1291                         object value;
1292                         if (child.Properties.TryGetValue ("myassociatedproperty", out value)) return value as string;
1293                         return null;
1294                 }
1295                 
1296                 public static void SetAssociatedProperty (Child child, string value)
1297                 {
1298                         TestContext.Writer.WriteLine ("Parent.SetAssociatedProperty: {0}", value);
1299                         child.Properties ["myassociatedproperty"] = value;
1300                 }
1301                 
1302         }
1303 }
1304
1305 #endregion