1 // 2 // Tests for System.Drawing.RectangleConverter.cs 3 // 4 // Author: 5 // Ravindra (rkumar@novell.com) 6 // 7 8 // 9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com) 10 // 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: 18 // 19 // The above copyright notice and this permission notice shall be 20 // included in all copies or substantial portions of the Software. 21 // 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. 29 // 30 31 using System; 32 using System.Collections; 33 using System.ComponentModel; 34 using System.ComponentModel.Design.Serialization; 35 using System.Drawing; 36 using System.Globalization; 37 using System.Security.Permissions; 38 using System.Threading; 39 40 using NUnit.Framework; 41 42 namespace MonoTests.System.Drawing 43 { 44 [TestFixture] 45 public class RectangleConverterTest 46 { 47 Rectangle rect; 48 Rectangle rectneg; 49 RectangleConverter rconv; 50 String rectStrInvariant; 51 String rectnegStrInvariant; 52 53 [SetUp] SetUp()54 public void SetUp () 55 { 56 rect = new Rectangle (10, 10, 20, 30); 57 rectStrInvariant = rect.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + 58 rect.Y + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + 59 rect.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + 60 rect.Height; 61 62 rectneg = new Rectangle (-10, -10, 20, 30); 63 rectnegStrInvariant = rectneg.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " 64 + rectneg.Y + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + 65 rectneg.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + rectneg.Height; 66 67 rconv = (RectangleConverter) TypeDescriptor.GetConverter (rect); 68 } 69 70 [Test] TestCanConvertFrom()71 public void TestCanConvertFrom () 72 { 73 Assert.IsTrue (rconv.CanConvertFrom (typeof (String)), "CCF#1"); 74 Assert.IsTrue (rconv.CanConvertFrom (null, typeof (String)), "CCF#2"); 75 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3"); 76 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4"); 77 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Point)), "CCF#5"); 78 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (PointF)), "CCF#6"); 79 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Size)), "CCF#7"); 80 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8"); 81 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Object)), "CCF#9"); 82 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (int)), "CCF#10"); 83 Assert.IsTrue (rconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11"); 84 } 85 86 [Test] TestCanConvertTo()87 public void TestCanConvertTo () 88 { 89 Assert.IsTrue (rconv.CanConvertTo (typeof (String)), "CCT#1"); 90 Assert.IsTrue (rconv.CanConvertTo (null, typeof (String)), "CCT#2"); 91 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3"); 92 Assert.IsFalse (rconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4"); 93 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Point)), "CCT#5"); 94 Assert.IsFalse (rconv.CanConvertTo (null, typeof (PointF)), "CCT#6"); 95 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Size)), "CCT#7"); 96 Assert.IsFalse (rconv.CanConvertTo (null, typeof (SizeF)), "CCT#8"); 97 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Object)), "CCT#9"); 98 Assert.IsFalse (rconv.CanConvertTo (null, typeof (int)), "CCT#10"); 99 } 100 101 [Test] TestConvertFrom()102 public void TestConvertFrom () 103 { 104 Assert.AreEqual (rect, (Rectangle) rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 105 "10, 10, 20, 30"), "CF#1"); 106 Assert.AreEqual (rectneg, (Rectangle) rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 107 "-10, -10, 20, 30"), "CF#2"); 108 109 try { 110 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 111 "10, 10"); 112 Assert.Fail ("CF#3: must throw ArgumentException"); 113 } catch (Exception e) { 114 Assert.IsTrue (e is ArgumentException, "CF#3"); 115 } 116 117 try { 118 rconv.ConvertFrom ("10"); 119 Assert.Fail ("CF#3a: must throw ArgumentException"); 120 } catch (Exception e) { 121 Assert.IsTrue (e is ArgumentException, "CF#3a"); 122 } 123 124 try { 125 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 126 "1, 1, 1, 1, 1"); 127 Assert.Fail ("CF#4: must throw ArgumentException"); 128 } catch (Exception e) { 129 Assert.IsTrue (e is ArgumentException, "CF#4"); 130 } 131 132 try { 133 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 134 "*1, 1, 1, 1"); 135 Assert.Fail ("CF#5: must throw Exception"); 136 } catch (Exception ex) { 137 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2"); 138 Assert.IsNotNull (ex.InnerException, "CF#5-3"); 139 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4"); 140 } 141 142 try { 143 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 144 new Rectangle (10, 10, 100, 100)); 145 Assert.Fail ("CF#6: must throw NotSupportedException"); 146 } catch (Exception e) { 147 Assert.IsTrue (e is NotSupportedException, "CF#6"); 148 } 149 150 try { 151 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 152 new RectangleF (10, 10, 100, 100)); 153 Assert.Fail ("CF#7: must throw NotSupportedException"); 154 } catch (Exception e) { 155 Assert.IsTrue (e is NotSupportedException, "CF#7"); 156 } 157 158 try { 159 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 160 new Point (10, 10)); 161 Assert.Fail ("CF#8: must throw NotSupportedException"); 162 } catch (Exception e) { 163 Assert.IsTrue (e is NotSupportedException, "CF#8"); 164 } 165 166 try { 167 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 168 new PointF (10, 10)); 169 Assert.Fail ("CF#9: must throw NotSupportedException"); 170 } catch (Exception e) { 171 Assert.IsTrue (e is NotSupportedException, "CF#9"); 172 } 173 174 try { 175 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 176 new Size (10, 10)); 177 Assert.Fail ("CF#10: must throw NotSupportedException"); 178 } catch (Exception e) { 179 Assert.IsTrue (e is NotSupportedException, "CF#10"); 180 } 181 182 try { 183 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 184 new SizeF (10, 10)); 185 Assert.Fail ("CF#11: must throw NotSupportedException"); 186 } catch (Exception e) { 187 Assert.IsTrue (e is NotSupportedException, "CF#11"); 188 } 189 190 try { 191 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 192 new Object ()); 193 Assert.Fail ("CF#12: must throw NotSupportedException"); 194 } catch (Exception e) { 195 Assert.IsTrue (e is NotSupportedException, "CF#12"); 196 } 197 198 try { 199 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 1001); 200 Assert.Fail ("CF#13: must throw NotSupportedException"); 201 } catch (Exception e) { 202 Assert.IsTrue (e is NotSupportedException, "CF#13"); 203 } 204 } 205 206 [Test] TestConvertTo()207 public void TestConvertTo () 208 { 209 Assert.AreEqual (rectStrInvariant, (String) rconv.ConvertTo (null, 210 CultureInfo.InvariantCulture, rect, typeof (String)), "CT#1"); 211 Assert.AreEqual (rectnegStrInvariant, (String) rconv.ConvertTo (null, 212 CultureInfo.InvariantCulture, rectneg, typeof (String)), "CT#2"); 213 214 try { 215 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 216 rect, typeof (Rectangle)); 217 Assert.Fail ("CT#3: must throw NotSupportedException"); 218 } catch (Exception e) { 219 Assert.IsTrue (e is NotSupportedException, "CT#3"); 220 } 221 222 try { 223 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 224 rect, typeof (RectangleF)); 225 Assert.Fail ("CT#4: must throw NotSupportedException"); 226 } catch (Exception e) { 227 Assert.IsTrue (e is NotSupportedException, "CT#4"); 228 } 229 230 try { 231 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 232 rect, typeof (Size)); 233 Assert.Fail ("CT#5: must throw NotSupportedException"); 234 } catch (Exception e) { 235 Assert.IsTrue (e is NotSupportedException, "CT#5"); 236 } 237 238 try { 239 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 240 rect, typeof (SizeF)); 241 Assert.Fail ("CT#6: must throw NotSupportedException"); 242 } catch (Exception e) { 243 Assert.IsTrue (e is NotSupportedException, "CT#6"); 244 } 245 246 try { 247 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 248 rect, typeof (Point)); 249 Assert.Fail ("CT#7: must throw NotSupportedException"); 250 } catch (Exception e) { 251 Assert.IsTrue (e is NotSupportedException, "CT#7"); 252 } 253 254 try { 255 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 256 rect, typeof (PointF)); 257 Assert.Fail ("CT#8: must throw NotSupportedException"); 258 } catch (Exception e) { 259 Assert.IsTrue (e is NotSupportedException, "CT#8"); 260 } 261 262 try { 263 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 264 rect, typeof (Object)); 265 Assert.Fail ("CT#9: must throw NotSupportedException"); 266 } catch (Exception e) { 267 Assert.IsTrue (e is NotSupportedException, "CT#9"); 268 } 269 270 try { 271 rconv.ConvertTo (null, CultureInfo.InvariantCulture, 272 rect, typeof (int)); 273 Assert.Fail ("CT#10: must throw NotSupportedException"); 274 } catch (Exception e) { 275 Assert.IsTrue (e is NotSupportedException, "CT#10"); 276 } 277 } 278 279 [Test] TestGetCreateInstanceSupported()280 public void TestGetCreateInstanceSupported () 281 { 282 Assert.IsTrue (rconv.GetCreateInstanceSupported (), "GCIS#1"); 283 Assert.IsTrue (rconv.GetCreateInstanceSupported (null), "GCIS#2"); 284 } 285 286 [Test] TestCreateInstance()287 public void TestCreateInstance () 288 { 289 Rectangle rectInstance; 290 291 Hashtable ht = new Hashtable (); 292 ht.Add ("X", 10); ht.Add ("Y", 10); 293 ht.Add ("Width", 20); ht.Add ("Height", 30); 294 295 rectInstance = (Rectangle) rconv.CreateInstance (ht); 296 Assert.AreEqual (rect, rectInstance, "CI#1"); 297 298 ht.Clear (); 299 ht.Add ("X", -10); ht.Add ("Y", -10); 300 ht.Add ("Width", 20); ht.Add ("Height", 30); 301 302 rectInstance = (Rectangle) rconv.CreateInstance (null, ht); 303 Assert.AreEqual (rectneg, rectInstance, "CI#2"); 304 } 305 306 [Test] TestCreateInstance_CaseSensitive()307 public void TestCreateInstance_CaseSensitive () 308 { 309 Hashtable ht = new Hashtable (); 310 ht.Add ("x", -10); 311 ht.Add ("Y", -10); 312 ht.Add ("Width", 20); 313 ht.Add ("Height", 30); 314 Assert.Throws<ArgumentException> (() => rconv.CreateInstance (null, ht)); 315 } 316 317 [Test] TestGetPropertiesSupported()318 public void TestGetPropertiesSupported () 319 { 320 Assert.IsTrue (rconv.GetPropertiesSupported (), "GPS#1"); 321 Assert.IsTrue (rconv.GetPropertiesSupported (null), "GPS#2"); 322 } 323 324 [Test] TestGetProperties()325 public void TestGetProperties () 326 { 327 Attribute [] attrs; 328 PropertyDescriptorCollection propsColl; 329 330 propsColl = rconv.GetProperties (rect); 331 Assert.AreEqual (4, propsColl.Count, "GP1#1"); 332 Assert.AreEqual (rect.X, propsColl["X"].GetValue (rect), "GP1#2"); 333 Assert.AreEqual (rect.Y, propsColl["Y"].GetValue (rect), "GP1#3"); 334 Assert.AreEqual (rect.Width, propsColl["Width"].GetValue (rect), "GP1#4"); 335 Assert.AreEqual (rect.Height, propsColl["Height"].GetValue (rect), "GP1#5"); 336 337 propsColl = rconv.GetProperties (null, rectneg); 338 Assert.AreEqual (4, propsColl.Count, "GP2#1"); 339 Assert.AreEqual (rectneg.X, propsColl["X"].GetValue (rectneg), "GP2#2"); 340 Assert.AreEqual (rectneg.Y, propsColl["Y"].GetValue (rectneg), "GP2#3"); 341 Assert.AreEqual (rectneg.Width, propsColl["Width"].GetValue (rectneg), "GP2#4"); 342 Assert.AreEqual (rectneg.Height, propsColl["Height"].GetValue (rectneg), "GP2#5"); 343 344 propsColl = rconv.GetProperties (null, rect, null); 345 Assert.AreEqual (11, propsColl.Count, "GP3#1"); 346 Assert.AreEqual (rect.X, propsColl["X"].GetValue (rect), "GP3#2"); 347 Assert.AreEqual (rect.Y, propsColl["Y"].GetValue (rect), "GP3#3"); 348 Assert.AreEqual (rect.Width, propsColl["Width"].GetValue (rect), "GP3#4"); 349 Assert.AreEqual (rect.Height, propsColl["Height"].GetValue (rect), "GP3#5"); 350 351 Assert.AreEqual (rect.Top, propsColl["Top"].GetValue (rect), "GP3#6"); 352 Assert.AreEqual (rect.Bottom, propsColl["Bottom"].GetValue (rect), "GP3#7"); 353 Assert.AreEqual (rect.Left, propsColl["Left"].GetValue (rect), "GP3#8"); 354 Assert.AreEqual (rect.Right, propsColl["Right"].GetValue (rect), "GP3#9"); 355 Assert.AreEqual (rect.Location, propsColl["Location"].GetValue (rect), "GP3#10"); 356 Assert.AreEqual (rect.Size, propsColl["Size"].GetValue (rect), "GP3#11"); 357 Assert.AreEqual (rect.IsEmpty, propsColl["IsEmpty"].GetValue (rect), "GP3#12"); 358 359 Type type = typeof (Rectangle); 360 attrs = Attribute.GetCustomAttributes (type, true); 361 propsColl = rconv.GetProperties (null, rect, attrs); 362 Assert.AreEqual (0, propsColl.Count, "GP3#13"); 363 } 364 365 [Test] ConvertFromInvariantString_string()366 public void ConvertFromInvariantString_string () 367 { 368 Assert.AreEqual (rect, rconv.ConvertFromInvariantString (rectStrInvariant), 369 "CFISS#1"); 370 Assert.AreEqual (rectneg, rconv.ConvertFromInvariantString (rectnegStrInvariant), 371 "CFISS#2"); 372 } 373 374 [Test] ConvertFromInvariantString_string_exc_1()375 public void ConvertFromInvariantString_string_exc_1 () 376 { 377 Assert.Throws<ArgumentException> (() => rconv.ConvertFromInvariantString ("1, 2, 3")); 378 } 379 380 [Test] ConvertFromInvariantString_string_exc_2()381 public void ConvertFromInvariantString_string_exc_2 () 382 { 383 try { 384 rconv.ConvertFromInvariantString ("hello"); 385 Assert.Fail ("#1"); 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"); 390 } 391 } 392 393 [Test] ConvertFromString_string()394 public void ConvertFromString_string () 395 { 396 // save current culture 397 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; 398 399 try { 400 PerformConvertFromStringTest (new CultureInfo ("en-US")); 401 PerformConvertFromStringTest (new CultureInfo ("nl-BE")); 402 PerformConvertFromStringTest (new MyCultureInfo ()); 403 } finally { 404 // restore original culture 405 Thread.CurrentThread.CurrentCulture = currentCulture; 406 } 407 } 408 409 [Test] ConvertFromString_string_exc_1()410 public void ConvertFromString_string_exc_1 () 411 { 412 CultureInfo culture = CultureInfo.CurrentCulture; 413 Assert.Throws<ArgumentException> (() => rconv.ConvertFromString (string.Format(culture, 414 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator))); 415 } 416 417 [Test] ConvertFromString_string_exc_2()418 public void ConvertFromString_string_exc_2 () 419 { 420 try { 421 rconv.ConvertFromString ("hello"); 422 Assert.Fail ("#1"); 423 } catch (Exception ex) { 424 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); 425 Assert.IsNotNull (ex.InnerException, "#3"); 426 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3"); 427 } 428 } 429 430 [Test] ConvertToInvariantString_string()431 public void ConvertToInvariantString_string () 432 { 433 Assert.AreEqual (rectStrInvariant, rconv.ConvertToInvariantString (rect), 434 "CFISS#1"); 435 Assert.AreEqual (rectnegStrInvariant, rconv.ConvertToInvariantString (rectneg), 436 "CFISS#2"); 437 } 438 439 [Test] ConvertToString_string()440 public void ConvertToString_string () { 441 // save current culture 442 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; 443 444 try { 445 PerformConvertToStringTest (new CultureInfo ("en-US")); 446 PerformConvertToStringTest (new CultureInfo ("nl-BE")); 447 PerformConvertToStringTest (new MyCultureInfo ()); 448 } finally { 449 // restore original culture 450 Thread.CurrentThread.CurrentCulture = currentCulture; 451 } 452 } 453 454 [Test] GetStandardValuesSupported()455 public void GetStandardValuesSupported () 456 { 457 Assert.IsFalse (rconv.GetStandardValuesSupported ()); 458 } 459 460 [Test] GetStandardValues()461 public void GetStandardValues () 462 { 463 Assert.IsNull (rconv.GetStandardValues ()); 464 } 465 466 [Test] GetStandardValuesExclusive()467 public void GetStandardValuesExclusive () 468 { 469 Assert.IsFalse (rconv.GetStandardValuesExclusive ()); 470 } 471 PerformConvertFromStringTest(CultureInfo culture)472 private void PerformConvertFromStringTest (CultureInfo culture) 473 { 474 // set current culture 475 Thread.CurrentThread.CurrentCulture = culture; 476 477 // perform tests 478 Assert.AreEqual (rect, rconv.ConvertFromString (CreateRectangleString (rect)), 479 "CFSS#1-" + culture.Name); 480 Assert.AreEqual (rectneg, rconv.ConvertFromString (CreateRectangleString (rectneg)), 481 "CFSS#2-" + culture.Name); 482 } 483 PerformConvertToStringTest(CultureInfo culture)484 private void PerformConvertToStringTest (CultureInfo culture) 485 { 486 // set current culture 487 Thread.CurrentThread.CurrentCulture = culture; 488 489 // perform tests 490 Assert.AreEqual (CreateRectangleString (rect), rconv.ConvertToString (rect), 491 "CFISS#1-" + culture.Name); 492 Assert.AreEqual (CreateRectangleString (rectneg), rconv.ConvertToString (rectneg), 493 "CFISS#2-" + culture.Name); 494 } 495 CreateRectangleString(Rectangle rectangle)496 private static string CreateRectangleString (Rectangle rectangle) 497 { 498 return CreateRectangleString (CultureInfo.CurrentCulture, rectangle); 499 } 500 CreateRectangleString(CultureInfo culture, Rectangle rectangle)501 private static string CreateRectangleString (CultureInfo culture, Rectangle rectangle) 502 { 503 return string.Format ("{0}{1} {2}{1} {3}{1} {4}", rectangle.X.ToString (culture), 504 culture.TextInfo.ListSeparator, rectangle.Y.ToString (culture), 505 rectangle.Width.ToString (culture), rectangle.Height.ToString (culture)); 506 } 507 508 [Serializable] 509 private sealed class MyCultureInfo : CultureInfo 510 { MyCultureInfo()511 internal MyCultureInfo () 512 : base ("en-US") 513 { 514 } 515 GetFormat(Type formatType)516 public override object GetFormat (Type formatType) 517 { 518 if (formatType == typeof (NumberFormatInfo)) { 519 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone (); 520 521 nfi.NegativeSign = "myNegativeSign"; 522 return NumberFormatInfo.ReadOnly (nfi); 523 } else { 524 return base.GetFormat (formatType); 525 } 526 } 527 } 528 } 529 } 530