Merge pull request #1899 from saper/resgencond
[mono.git] / mcs / class / WindowsBase / Test / System.Windows.Markup / DateTimeValueSerializerTest.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok (toshok@ximian.com)
24 //
25
26 using System;
27 using System.ComponentModel;
28 using System.Windows;
29 using System.Windows.Converters;
30 using System.Windows.Markup;
31 using NUnit.Framework;
32
33 namespace MonoTests.System.Windows.Markup {
34
35         [TestFixture]
36         public class DateTimeValueSerializerTest
37         {
38                 class Context : IValueSerializerContext {
39                         public ValueSerializer GetValueSerializerFor (PropertyDescriptor descriptor)
40                         {
41                                 return null;
42                         }
43
44                         public ValueSerializer GetValueSerializerFor (Type type)
45                         {
46                                 return null;
47                         }
48
49                         public void OnComponentChanged ()
50                         {
51                         }
52
53                         public bool OnComponentChanging ()
54                         {
55                                 return false;
56                         }
57
58                         public IContainer Container {
59                                 get { return null; }
60                         }
61
62                         public object Instance {
63                                 get { return null; }
64                         }
65
66                         public PropertyDescriptor PropertyDescriptor {
67                                 get { return null; }
68                         }
69
70                         public object GetService (Type serviceType)
71                         {
72                                 return null;
73                         }
74                 }
75
76                 [Test]
77                 public void CanConvertFromStringNullContext ()
78                 {
79                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
80                         Assert.IsTrue (serializer.CanConvertFromString (null, null));
81                 }
82
83                 [Test]
84                 public void CanConvertToStringNullContext ()
85                 {
86                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
87                         Assert.IsTrue (serializer.CanConvertToString (DateTime.Now, null));
88                 }
89
90                 [Test]
91                 [NUnit.Framework.CategoryAttribute ("NotWorking")]
92                 // Since ValueSerializer has moved to System.Xaml.dll while the type
93                 // this test expects is in WindowsBase, there should be some additional
94                 // support code in this assembly. Until someone does that job, this
95                 // test won't pass.
96                 public void CanConvertFromString ()
97                 {
98                         Context context = new Context ();
99                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime), context);
100
101                         Assert.AreEqual ("DateTimeValueSerializer", serializer.GetType().Name, "1");
102
103                         Assert.IsTrue (serializer.CanConvertFromString (null, context), "2");
104                         Assert.IsTrue (serializer.CanConvertFromString ("2008-01-18T10:39:27.106426-08:00", context), "3");
105                         Assert.IsTrue (serializer.CanConvertFromString ("2008-01-.106426-08:00", context), "4");
106                 }
107
108                 [NUnit.Framework.CategoryAttribute ("NotWorking")]
109                 // Since ValueSerializer has moved to System.Xaml.dll while the type
110                 // this test expects is in WindowsBase, there should be some additional
111                 // support code in this assembly. Until someone does that job, this
112                 // test won't pass.
113                 [Test]
114                 [ExpectedException (typeof (ArgumentException))] // Expected object of type 'DateTime'.
115                 public void CanConvertToString1 ()
116                 {
117                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
118                         Context context = new Context ();
119
120                         serializer.CanConvertToString (null, context);
121                 }
122                 [Test]
123                 public void CanConvertToString2 ()
124                 {
125                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
126                         Context context = new Context ();
127
128                         DateTime dt = DateTime.Parse ("2008-01-18T10:39:27.106426-08:00");
129
130                         Assert.IsTrue (serializer.CanConvertToString (dt, context), "6");
131                 }
132
133                 [Test]
134                 public void ConvertFromString1 ()
135                 {
136                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
137                         Context context = new Context ();
138
139                         DateTime dt = DateTime.Parse ("2008-01-18T10:39:27.106426-08:00");
140
141                         Assert.AreEqual (dt, serializer.ConvertFromString ("2008-01-18T10:39:27.106426-08:00", context), "2");
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (NotSupportedException))] // 'DateTimeValueSerializer' ValueSerializer cannot convert from '(null)'
146                 public void ConvertFromString2 ()
147                 {
148                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
149                         Context context = new Context ();
150
151                         serializer.ConvertFromString (null, context);
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (FormatException))] // String was not recognized as a valid DateTime.
156                 public void ConvertFromString3 ()
157                 {
158                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
159                         Context context = new Context ();
160
161                         serializer.ConvertFromString ("2008-01-.106426-08:00", context);
162                 }
163
164                 [Test]
165                 [NUnit.Framework.CategoryAttribute ("NotWorking")]
166                 public void ConvertToString1 ()
167                 {
168                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
169                         Context context = new Context ();
170
171                         DateTime dt = DateTime.Parse ("2008-01-18T10:39:27.106426-08:00");
172
173                         Assert.AreEqual ("2008-01-18T10:39:27.106426-08:00", serializer.ConvertToString (dt, context), "1");
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (NotSupportedException))]
178                 public void ConvertToString2 ()
179                 {
180                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
181                         Context context = new Context ();
182
183                         serializer.ConvertToString (null, context);
184                 }
185
186                 [Test]
187                 [ExpectedException (typeof (NotSupportedException))]
188                 public void ConvertToString3 ()
189                 {
190                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
191                         Context context = new Context ();
192
193                         serializer.ConvertToString (1, context);
194                 }
195         }
196
197 }