2005-08-23 Iain McCoy <iain@mccoy.id.au>
[mono.git] / mcs / class / PresentationFramework / Test / ObjectWriter.cs
1 // 
2 // ObjectWriter.cs - NUnit Test Cases for the xaml object builder
3 // 
4 // Author:
5 //   Iain McCoy (iain@mccoy.id.au)
6 //
7 // (C) 2005 Iain McCoy
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using NUnit.Framework;
31
32 using System;
33 using System.Diagnostics;
34 using System.IO;
35 using System.Xml;
36 using System.Reflection;
37 using System.Windows;
38 using System.CodeDom.Compiler;
39 using Mono.Windows.Serialization;
40 using Xaml.TestVocab.Console;
41
42 namespace MonoTests.System.Windows.Serialization
43 {
44
45 [TestFixture]
46 public class ObjectWriterTest {
47         string code;
48         
49         [SetUp]
50         public void GetReady()
51         {
52         }
53
54         [TearDown]
55         public void Clean()
56         {
57                 code = null;
58         }
59
60         [Test]
61         public void TestTopLevel()
62         {
63                 code = "<ConsoleApp xmlns=\"console\"></ConsoleApp>";
64                 ConsoleApp app = new ConsoleApp();
65                 compare(app);
66         }
67
68         [Test]
69         public void TestTopLevelWithClassName()
70         {
71                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Class=\"nnn\">\n"+
72                         "</ConsoleApp>";
73                 ConsoleApp app = new ConsoleApp();
74                 compare(app);
75         }
76         
77         [Test]
78         public void TestTopLevelWithClassNameAndNamespace()
79         {
80                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Class=\"Test.Thing.nnn\">\n"+
81                         "</ConsoleApp>";
82                 ConsoleApp app = new ConsoleApp();
83                 compare(app);
84         }
85         
86         [Test]
87         public void TestSimplestAddChild()
88         {
89
90                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
91                         "<ConsoleWriter></ConsoleWriter>" +
92                         "</ConsoleApp>";
93                 ConsoleApp app = new ConsoleApp();
94                 ConsoleWriter writer = new ConsoleWriter();
95                 app.AddChild(writer);
96
97                 compare(app);
98         }
99
100         [Test]
101         public void TestSimplestAddChildWithInstanceName()
102         {
103                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
104                         "<ConsoleWriter x:Name=\"XXX\"></ConsoleWriter>" +
105                         "</ConsoleApp>";
106
107                 ConsoleApp app = new ConsoleApp();
108                 ConsoleWriter writer = new ConsoleWriter();
109                 app.AddChild(writer);
110
111                 compare(app);
112         }
113
114         
115         [Test]
116         public void TestSimplestAddChildAndText()
117         {
118                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
119                         "<ConsoleWriter>Hello</ConsoleWriter>" +
120                         "</ConsoleApp>";
121
122                 ConsoleApp app = new ConsoleApp();
123                 ConsoleWriter writer = new ConsoleWriter();
124                 writer.AddText("Hello");
125                 app.AddChild(writer);
126
127                 compare(app);
128         }
129
130         [Test]
131         public void TestTextProperty()
132         {
133                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
134                         "<ConsoleWriter Text=\"Hello\" />" +
135                         "</ConsoleApp>";
136
137                 ConsoleApp app = new ConsoleApp();
138                 ConsoleWriter writer = new ConsoleWriter();
139                 writer.Text = new ConsoleValueString("Hello");
140                 app.AddChild(writer);
141
142                 compare(app);                                                   
143         }
144
145         [Test]
146         public void TestDependencyProperty()
147         {
148                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
149                         "<ConsoleWriter ConsoleApp.Repetitions=\"3\" />" +
150                         "</ConsoleApp>";
151
152                 ConsoleApp app = new ConsoleApp();
153                 ConsoleWriter writer = new ConsoleWriter();
154                 ConsoleApp.SetRepetitions(writer, 3);
155                 app.AddChild(writer);
156
157                 compare(app);
158         }
159
160         [Test]
161         public void TestObjectAsPropertyValue()
162         {
163                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
164                         "<ConsoleReader>\n" +
165                         "<ConsoleReader.Prompt><ConsoleWriter /></ConsoleReader.Prompt>\n" +
166                         "</ConsoleReader>\n" +
167                         "</ConsoleApp>";
168
169                 ConsoleApp app = new ConsoleApp();
170                 ConsoleReader reader = new ConsoleReader();
171                 ConsoleWriter writer = new ConsoleWriter();
172                 reader.Prompt = writer;
173                 app.AddChild(reader);
174                 
175                 compare(app);
176         }
177
178
179         private void compare(object expected)
180         {
181                 string mapping = "<?Mapping ClrNamespace=\"Xaml.TestVocab.Console\" Assembly=\"./TestVocab.dll\" XmlNamespace=\"console\" ?>\n";
182                 object o = ObjectWriter.Parse(new XmlTextReader(new StringReader(mapping + code)));
183                 Assert.AreEqual(expected, o);
184         }
185
186 }
187
188 }