2 // Tests for System.Drawing.SizeConverter.cs
5 // Ravindra (rkumar@novell.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.ComponentModel;
36 using System.ComponentModel.Design.Serialization;
38 using System.Globalization;
39 using System.Security.Permissions;
40 using System.Threading;
42 using NUnit.Framework;
44 namespace MonoTests.System.Drawing
47 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
48 public class SizeFConverterTest
52 SizeFConverter szconv;
53 String szStrInvariant;
54 String sznegStrInvariant;
59 sz = new SizeF (10, 20);
60 szStrInvariant = sz.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + sz.Height;
62 szneg = new SizeF (-20, -30);
63 sznegStrInvariant = szneg.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + szneg.Height;
65 szconv = (SizeFConverter) TypeDescriptor.GetConverter (sz);
70 [NUnit.Framework.Category ("NotWorking")]
72 public void TestCanConvertFrom ()
74 Assert.IsTrue (szconv.CanConvertFrom (typeof (String)), "CCF#1");
75 Assert.IsTrue (szconv.CanConvertFrom (null, typeof (String)), "CCF#2");
76 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
77 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
78 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
79 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
80 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
81 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
82 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
83 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (int)), "CCF#10");
84 Assert.IsTrue (szconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
88 public void TestCanConvertTo ()
90 Assert.IsTrue (szconv.CanConvertTo (typeof (String)), "CCT#1");
91 Assert.IsTrue (szconv.CanConvertTo (null, typeof (String)), "CCT#2");
92 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
93 Assert.IsFalse (szconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
94 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Point)), "CCT#5");
95 Assert.IsFalse (szconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
96 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Size)), "CCT#7");
97 Assert.IsFalse (szconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
98 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Object)), "CCT#9");
99 Assert.IsFalse (szconv.CanConvertTo (null, typeof (int)), "CCT#10");
104 [NUnit.Framework.Category ("NotWorking")]
106 public void TestConvertFrom ()
108 Assert.AreEqual (sz, (SizeF) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
110 Assert.AreEqual (szneg, (SizeF) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
111 "-20, -30"), "CF#2");
114 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10");
115 Assert.Fail ("CF#3: must throw ArgumentException");
116 } catch (Exception e) {
117 Assert.IsTrue (e is ArgumentException, "CF#3");
121 szconv.ConvertFrom ("10");
122 Assert.Fail ("CF#3a: must throw ArgumentException");
123 } catch (Exception e) {
124 Assert.IsTrue (e is ArgumentException, "CF#3a");
128 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
130 Assert.Fail ("CF#4: must throw ArgumentException");
131 } catch (Exception e) {
132 Assert.IsTrue (e is ArgumentException, "CF#4");
136 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
138 Assert.Fail ("CF#5-1: must throw Exception");
139 } catch (Exception ex) {
140 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2");
141 Assert.IsNotNull (ex.InnerException, "CF#5-3");
142 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4");
146 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
148 Assert.Fail ("CF#6: must throw NotSupportedException");
149 } catch (Exception e) {
150 Assert.IsTrue (e is NotSupportedException, "CF#6");
154 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
155 new PointF (10, 10));
156 Assert.Fail ("CF#7: must throw NotSupportedException");
157 } catch (Exception e) {
158 Assert.IsTrue (e is NotSupportedException, "CF#7");
162 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
164 Assert.Fail ("CF#8: must throw NotSupportedException");
165 } catch (Exception e) {
166 Assert.IsTrue (e is NotSupportedException, "CF#8");
170 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
172 Assert.Fail ("CF#9: must throw NotSupportedException");
173 } catch (Exception e) {
174 Assert.IsTrue (e is NotSupportedException, "CF#9");
178 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
179 Assert.Fail ("CF#10: must throw NotSupportedException");
180 } catch (Exception e) {
181 Assert.IsTrue (e is NotSupportedException, "CF#10");
186 public void TestConvertTo ()
188 Assert.AreEqual (szStrInvariant, (String) szconv.ConvertTo (null,
189 CultureInfo.InvariantCulture, sz, typeof (String)), "CT#1");
190 Assert.AreEqual (sznegStrInvariant, (String) szconv.ConvertTo (null,
191 CultureInfo.InvariantCulture, szneg, typeof (String)), "CT#2");
194 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
196 Assert.Fail ("CT#3: must throw NotSupportedException");
197 } catch (Exception e) {
198 Assert.IsTrue (e is NotSupportedException, "CT#3");
202 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
204 Assert.Fail ("CT#4: must throw NotSupportedException");
205 } catch (Exception e) {
206 Assert.IsTrue (e is NotSupportedException, "CT#4");
210 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
212 Assert.Fail ("CT#5: must throw NotSupportedException");
213 } catch (Exception e) {
214 Assert.IsTrue (e is NotSupportedException, "CT#5");
218 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
220 Assert.Fail ("CT#6: must throw NotSupportedException");
221 } catch (Exception e) {
222 Assert.IsTrue (e is NotSupportedException, "CT#6");
226 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
228 Assert.Fail ("CT#7: must throw NotSupportedException");
229 } catch (Exception e) {
230 Assert.IsTrue (e is NotSupportedException, "CT#7");
235 public void TestGetCreateInstanceSupported ()
237 Assert.IsTrue (szconv.GetCreateInstanceSupported (), "GCIS#1");
238 Assert.IsTrue (szconv.GetCreateInstanceSupported (null), "GCIS#2");
242 public void TestCreateInstance ()
246 Hashtable ht = new Hashtable ();
247 ht.Add ("Width", 10.0f); ht.Add ("Height", 20.0f);
249 szInstance = (SizeF) szconv.CreateInstance (ht);
250 Assert.AreEqual (sz, szInstance, "CI#1");
253 ht.Add ("Width", -20.0f); ht.Add ("Height", -30.0f);
255 szInstance = (SizeF) szconv.CreateInstance (null, ht);
256 Assert.AreEqual (szneg, szInstance, "CI#2");
260 [ExpectedException (typeof (InvalidCastException))]
261 public void TestCreateInstance_Int ()
263 Hashtable ht = new Hashtable ();
264 ht.Add ("Width", 10);
265 ht.Add ("Height", 20);
266 szconv.CreateInstance (null, ht);
270 [ExpectedException (typeof (NullReferenceException))]
271 public void TestCreateInstance_CaseSensitive ()
273 Hashtable ht = new Hashtable ();
274 ht.Add ("width", 20);
275 ht.Add ("Height", 30);
276 szconv.CreateInstance (null, ht);
280 public void TestGetPropertiesSupported ()
282 Assert.IsTrue (szconv.GetPropertiesSupported (), "GPS#1");
283 Assert.IsTrue (szconv.GetPropertiesSupported (null), "GPS#2");
288 [NUnit.Framework.Category ("NotWorking")]
290 public void TestGetProperties ()
293 PropertyDescriptorCollection propsColl;
295 propsColl = szconv.GetProperties (sz);
296 Assert.AreEqual (2, propsColl.Count, "GP1#1");
297 Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP1#2");
298 Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP1#3");
300 propsColl = szconv.GetProperties (null, szneg);
301 Assert.AreEqual (2, propsColl.Count, "GP2#1");
302 Assert.AreEqual (szneg.Width, propsColl["Width"].GetValue (szneg), "GP2#2");
303 Assert.AreEqual (szneg.Height, propsColl["Height"].GetValue (szneg), "GP2#3");
305 propsColl = szconv.GetProperties (null, sz, null);
306 Assert.AreEqual (3, propsColl.Count, "GP3#1");
307 Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP3#2");
308 Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP3#3");
309 Assert.AreEqual (sz.IsEmpty, propsColl["IsEmpty"].GetValue (sz), "GP3#4");
311 Type type = typeof (SizeF);
312 attrs = Attribute.GetCustomAttributes (type, true);
313 propsColl = szconv.GetProperties (null, sz, attrs);
314 Assert.AreEqual (0, propsColl.Count, "GP3#5");
318 public void ConvertFromInvariantString_string ()
320 Assert.AreEqual (sz, szconv.ConvertFromInvariantString (szStrInvariant),
322 Assert.AreEqual (szneg, szconv.ConvertFromInvariantString (sznegStrInvariant),
327 [ExpectedException (typeof (ArgumentException))]
328 public void ConvertFromInvariantString_string_exc_1 ()
330 szconv.ConvertFromInvariantString ("1, 2, 3");
335 [NUnit.Framework.Category ("NotWorking")]
337 public void ConvertFromInvariantString_string_exc_2 ()
340 szconv.ConvertFromInvariantString ("hello");
342 } catch (Exception ex) {
343 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
344 Assert.IsNotNull (ex.InnerException, "#3");
345 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
351 [NUnit.Framework.Category ("NotWorking")]
353 public void ConvertFromString_string ()
355 // save current culture
356 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
359 PerformConvertFromStringTest (new CultureInfo ("en-US"));
360 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
361 PerformConvertFromStringTest (new MyCultureInfo ());
363 // restore original culture
364 Thread.CurrentThread.CurrentCulture = currentCulture;
369 [ExpectedException (typeof (ArgumentException))]
370 public void ConvertFromString_string_exc_1 ()
372 CultureInfo culture = CultureInfo.CurrentCulture;
373 szconv.ConvertFromString (string.Format(culture,
374 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator));
379 [NUnit.Framework.Category ("NotWorking")]
381 public void ConvertFromString_string_exc_2 ()
384 szconv.ConvertFromString ("hello");
386 } catch (Exception ex) {
387 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
388 Assert.IsNotNull (ex.InnerException, "#3");
389 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
394 public void ConvertToInvariantString_string ()
396 Assert.AreEqual (szStrInvariant, szconv.ConvertToInvariantString (sz),
398 Assert.AreEqual (sznegStrInvariant, szconv.ConvertToInvariantString (szneg),
403 public void ConvertToString_string ()
405 // save current culture
406 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
409 PerformConvertToStringTest (new CultureInfo ("en-US"));
410 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
411 PerformConvertToStringTest (new MyCultureInfo ());
413 // restore original culture
414 Thread.CurrentThread.CurrentCulture = currentCulture;
419 public void GetStandardValuesSupported ()
421 Assert.IsFalse (szconv.GetStandardValuesSupported ());
425 public void GetStandardValues ()
427 Assert.IsNull (szconv.GetStandardValues ());
431 public void GetStandardValuesExclusive ()
433 Assert.IsFalse (szconv.GetStandardValuesExclusive ());
436 private void PerformConvertFromStringTest (CultureInfo culture)
438 // set current culture
439 Thread.CurrentThread.CurrentCulture = culture;
442 Assert.AreEqual (sz, szconv.ConvertFromString (CreateSizeString (culture, sz)),
443 "CFSS#1-" + culture.Name);
444 Assert.AreEqual (szneg, szconv.ConvertFromString (CreateSizeString (culture, szneg)),
445 "CFSS#2-" + culture.Name);
448 private void PerformConvertToStringTest (CultureInfo culture)
450 // set current culture
451 Thread.CurrentThread.CurrentCulture = culture;
454 Assert.AreEqual (CreateSizeString (culture, sz), szconv.ConvertToString (sz),
455 "CFISS#1-" + culture.Name);
456 Assert.AreEqual (CreateSizeString (culture, szneg), szconv.ConvertToString (szneg),
457 "CFISS#2-" + culture.Name);
460 private static string CreateSizeString (SizeF size)
462 return CreateSizeString (CultureInfo.CurrentCulture, size);
465 private static string CreateSizeString (CultureInfo culture, SizeF size)
467 return string.Format ("{0}{1} {2}", size.Width.ToString (culture),
468 culture.TextInfo.ListSeparator, size.Height.ToString (culture));
472 private sealed class MyCultureInfo : CultureInfo
474 internal MyCultureInfo () : base ("en-US")
478 public override object GetFormat (Type formatType)
480 if (formatType == typeof (NumberFormatInfo)) {
481 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
483 nfi.NegativeSign = "myNegativeSign";
484 return NumberFormatInfo.ReadOnly (nfi);
486 return base.GetFormat (formatType);