2005-08-26 Iain McCoy <iain@mccoy.id.au>
[mono.git] / mcs / class / PresentationFramework / Test / ObjectWriter.cs
1 // 
2 // Parser.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 System.Windows.Serialization;
41 using Xaml.TestVocab.Console;
42
43 namespace MonoTests.System.Windows.Serialization
44 {
45
46 [TestFixture]
47 public class ParserTest {
48         string code;
49         
50         [SetUp]
51         public void GetReady()
52         {
53         }
54
55         [TearDown]
56         public void Clean()
57         {
58                 code = null;
59         }
60
61         [Test]
62         public void TestTopLevel()
63         {
64                 code = "<ConsoleApp xmlns=\"console\"></ConsoleApp>";
65                 ConsoleApp app = new ConsoleApp();
66                 compare(app);
67         }
68
69         [Test]
70         public void TestTopLevelWithClassName()
71         {
72                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Class=\"nnn\">\n"+
73                         "</ConsoleApp>";
74                 ConsoleApp app = new ConsoleApp();
75                 compare(app);
76         }
77         
78         [Test]
79         public void TestTopLevelWithClassNameAndNamespace()
80         {
81                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\" x:Class=\"Test.Thing.nnn\">\n"+
82                         "</ConsoleApp>";
83                 ConsoleApp app = new ConsoleApp();
84                 compare(app);
85         }
86         
87         [Test]
88         public void TestSimplestAddChild()
89         {
90
91                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
92                         "<ConsoleWriter></ConsoleWriter>" +
93                         "</ConsoleApp>";
94                 ConsoleApp app = new ConsoleApp();
95                 ConsoleWriter writer = new ConsoleWriter();
96                 app.AddChild(writer);
97
98                 compare(app);
99         }
100
101         [Test]
102         public void TestSimplestAddChildWithInstanceName()
103         {
104                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
105                         "<ConsoleWriter x:Name=\"XXX\"></ConsoleWriter>" +
106                         "</ConsoleApp>";
107
108                 ConsoleApp app = new ConsoleApp();
109                 ConsoleWriter writer = new ConsoleWriter();
110                 app.AddChild(writer);
111
112                 compare(app);
113         }
114
115         
116         [Test]
117         public void TestSimplestAddChildAndText()
118         {
119                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
120                         "<ConsoleWriter>Hello</ConsoleWriter>" +
121                         "</ConsoleApp>";
122
123                 ConsoleApp app = new ConsoleApp();
124                 ConsoleWriter writer = new ConsoleWriter();
125                 writer.AddText("Hello");
126                 app.AddChild(writer);
127
128                 compare(app);
129         }
130
131         [Test]
132         public void TestTextProperty()
133         {
134                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
135                         "<ConsoleWriter Text=\"Hello\" />" +
136                         "</ConsoleApp>";
137
138                 ConsoleApp app = new ConsoleApp();
139                 ConsoleWriter writer = new ConsoleWriter();
140                 writer.Text = new ConsoleValueString("Hello");
141                 app.AddChild(writer);
142
143                 compare(app);                                                   
144         }
145
146         [Test]
147         public void TestDependencyProperty()
148         {
149                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
150                         "<ConsoleWriter ConsoleApp.Repetitions=\"3\" />" +
151                         "</ConsoleApp>";
152
153                 ConsoleApp app = new ConsoleApp();
154                 ConsoleWriter writer = new ConsoleWriter();
155                 ConsoleApp.SetRepetitions(writer, 3);
156                 app.AddChild(writer);
157
158                 compare(app);
159         }
160
161         [Test]
162         public void TestObjectAsPropertyValue()
163         {
164                 code = "<ConsoleApp xmlns=\"console\" xmlns:x=\"http://schemas.microsoft.com/winfx/xaml/2005\">\n"+
165                         "<ConsoleReader>\n" +
166                         "<ConsoleReader.Prompt><ConsoleWriter /></ConsoleReader.Prompt>\n" +
167                         "</ConsoleReader>\n" +
168                         "</ConsoleApp>";
169
170                 ConsoleApp app = new ConsoleApp();
171                 ConsoleReader reader = new ConsoleReader();
172                 ConsoleWriter writer = new ConsoleWriter();
173                 reader.Prompt = writer;
174                 app.AddChild(reader);
175                 
176                 compare(app);
177         }
178
179
180         private void compare(object expected)
181         {
182                 string mapping = "<?Mapping ClrNamespace=\"Xaml.TestVocab.Console\" Assembly=\"./TestVocab.dll\" XmlNamespace=\"console\" ?>\n";
183                 object o = Parser.LoadXml(new XmlTextReader(new StringReader(mapping + code)));
184                 Assert.AreEqual(expected, o);
185         }
186
187 }
188
189 }