Merge pull request #439 from mono-soc-2012/garyb/iconfix
[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 #if NET_4_0
92                 [NUnit.Framework.CategoryAttribute ("NotWorking")]
93                 // Since ValueSerializer has moved to System.Xaml.dll while the type
94                 // this test expects is in WindowsBase, there should be some additional
95                 // support code in this assembly. Until someone does that job, this
96                 // test won't pass.
97 #endif
98                 public void CanConvertFromString ()
99                 {
100                         Context context = new Context ();
101                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime), context);
102
103                         Assert.AreEqual ("DateTimeValueSerializer", serializer.GetType().Name, "1");
104
105                         Assert.IsTrue (serializer.CanConvertFromString (null, context), "2");
106                         Assert.IsTrue (serializer.CanConvertFromString ("2008-01-18T10:39:27.106426-08:00", context), "3");
107                         Assert.IsTrue (serializer.CanConvertFromString ("2008-01-.106426-08:00", context), "4");
108                 }
109
110 #if NET_4_0
111                 [NUnit.Framework.CategoryAttribute ("NotWorking")]
112                 // Since ValueSerializer has moved to System.Xaml.dll while the type
113                 // this test expects is in WindowsBase, there should be some additional
114                 // support code in this assembly. Until someone does that job, this
115                 // test won't pass.
116 #endif
117                 [Test]
118                 [ExpectedException (typeof (ArgumentException))] // Expected object of type 'DateTime'.
119                 public void CanConvertToString1 ()
120                 {
121                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
122                         Context context = new Context ();
123
124                         serializer.CanConvertToString (null, context);
125                 }
126                 [Test]
127                 public void CanConvertToString2 ()
128                 {
129                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
130                         Context context = new Context ();
131
132                         DateTime dt = DateTime.Parse ("2008-01-18T10:39:27.106426-08:00");
133
134                         Assert.IsTrue (serializer.CanConvertToString (dt, context), "6");
135                 }
136
137                 [Test]
138                 public void ConvertFromString1 ()
139                 {
140                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
141                         Context context = new Context ();
142
143                         DateTime dt = DateTime.Parse ("2008-01-18T10:39:27.106426-08:00");
144
145                         Assert.AreEqual (dt, serializer.ConvertFromString ("2008-01-18T10:39:27.106426-08:00", context), "2");
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (NotSupportedException))] // 'DateTimeValueSerializer' ValueSerializer cannot convert from '(null)'
150                 public void ConvertFromString2 ()
151                 {
152                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
153                         Context context = new Context ();
154
155                         serializer.ConvertFromString (null, context);
156                 }
157
158                 [Test]
159                 [ExpectedException (typeof (FormatException))] // String was not recognized as a valid DateTime.
160                 public void ConvertFromString3 ()
161                 {
162                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
163                         Context context = new Context ();
164
165                         serializer.ConvertFromString ("2008-01-.106426-08:00", context);
166                 }
167
168                 [Test]
169                 [NUnit.Framework.CategoryAttribute ("NotWorking")]
170                 public void ConvertToString1 ()
171                 {
172                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
173                         Context context = new Context ();
174
175                         DateTime dt = DateTime.Parse ("2008-01-18T10:39:27.106426-08:00");
176
177                         Assert.AreEqual ("2008-01-18T10:39:27.106426-08:00", serializer.ConvertToString (dt, context), "1");
178                 }
179
180                 [Test]
181                 [ExpectedException (typeof (NotSupportedException))]
182                 public void ConvertToString2 ()
183                 {
184                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
185                         Context context = new Context ();
186
187                         serializer.ConvertToString (null, context);
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (NotSupportedException))]
192                 public void ConvertToString3 ()
193                 {
194                         ValueSerializer serializer = (ValueSerializer)ValueSerializer.GetSerializerFor (typeof (DateTime));
195                         Context context = new Context ();
196
197                         serializer.ConvertToString (1, context);
198                 }
199         }
200
201 }