2 // Tests for System.Web.UI.WebControls.WebColorConverter.cs
5 // Peter Dennis Bartok (pbartok@novell.com)
9 // Copyright (C) 2005 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.
31 using NUnit.Framework;
33 using System.Collections;
34 using System.ComponentModel;
37 using System.Drawing.Drawing2D;
38 using System.Globalization;
41 using System.Web.UI.WebControls;
43 namespace MonoTests.System.Web.UI.WebControls
46 public class WebColorConverterTest
51 WebColorConverter conv;
54 conv = new WebColorConverter();
56 Assert.AreEqual(true, conv.CanConvertFrom(null, typeof(string)), "B1");
57 Assert.AreEqual(true, conv.CanConvertTo(null, typeof(string)), "B2");
58 Assert.AreEqual(false, conv.CanConvertFrom(null, typeof(int)), "B3");
59 Assert.AreEqual(false, conv.CanConvertTo(null, typeof(int)), "B4");
62 Assert.AreEqual("Fuchsia", conv.ConvertTo(null, null, c, typeof(string)), "B5");
64 c = Color.FromArgb(1, 2, 3);
65 Assert.AreEqual("#010203", conv.ConvertTo(null, null, c, typeof(string)), "B6");
67 c = Color.FromArgb(5, 1, 2, 3);
68 Assert.AreEqual("#010203", conv.ConvertTo(null, null, c, typeof(string)), "B7");
70 c = Color.FromArgb(254, 253, 252);
71 Assert.AreEqual("#FEFDFC", conv.ConvertTo(null, null, c, typeof(string)), "B8");
73 c = Color.FromKnownColor(KnownColor.BlanchedAlmond);
74 Assert.AreEqual("BlanchedAlmond", conv.ConvertTo(null, null, c, typeof(string)), "B9");
76 c = Color.FromName("OingoBoingo");
77 Assert.AreEqual("#000000", conv.ConvertTo(null, null, c, typeof(string)), "B10");
79 Assert.AreEqual(Color.FromArgb(0, 0, 79, 80), conv.ConvertFrom(null, null, "020304"), "B11");
80 Assert.AreEqual(Color.FromArgb(0, 0, 79, 80), conv.ConvertFrom(null, null, "20304"), "B12");
81 Assert.AreEqual(Color.FromArgb(127, 255, 255, 255), conv.ConvertFrom(null, null, "2147483647"), "B13");
82 Assert.AreEqual(Color.FromArgb(128, 0, 0, 1), conv.ConvertFrom(null, null, "-2147483647"), "B14");
83 Assert.AreEqual(Color.FromArgb(255, 2, 3, 4), conv.ConvertFrom(null, null, "#FF020304"), "B15");
84 Assert.AreEqual(Color.FromArgb(2, 3, 4), conv.ConvertFrom(null, null, "#020304"), "B16");
85 Assert.AreEqual(Color.FromArgb(0, 2, 3, 4), conv.ConvertFrom(null, null, "#20304"), "B17");
86 Assert.AreEqual(Color.Fuchsia, conv.ConvertFrom(null, null, "Fuchsia"), "B18");
87 Assert.AreEqual(Color.FromArgb(0, 0, 2, 52), conv.ConvertFrom(null, null, "#234"), "B19");
89 // Garbage/whitespace tests
90 c = Color.FromName("\rFuchsia\n");
91 Assert.AreEqual("#000000", conv.ConvertTo(null, null, c, typeof(string)), "B20");
92 Assert.AreEqual(Color.Fuchsia, conv.ConvertFrom(null, null, "\rFuchsia\n"), "B21");
93 Assert.AreEqual(Color.FromArgb(255, 1, 2, 3), conv.ConvertFrom(null, null, "#010203"), "B22");
95 Assert.AreEqual(Color.Empty, conv.ConvertFrom(null, null, ""), "B23");
97 Assert.AreEqual(Color.FromArgb(0, 0, 0, 1), conv.ConvertFrom(null, null, "#1"), "B24");
98 Assert.AreEqual(Color.FromArgb(0, 0, 0, 0x12), conv.ConvertFrom(null, null, "#12"), "B25");
99 Assert.AreEqual(Color.FromArgb(0, 0, 1, 0x23), conv.ConvertFrom(null, null, "#123"), "B26");
100 Assert.AreEqual(Color.FromArgb(0, 0, 0x12, 0x34), conv.ConvertFrom(null, null, "#1234"), "B27");
101 Assert.AreEqual(Color.FromArgb(0, 1, 0x23, 0x45), conv.ConvertFrom(null, null, "#12345"), "B28");
102 Assert.AreEqual(Color.FromArgb(0xff, 0x12, 0x34, 0x56), conv.ConvertFrom(null, null, "#123456"), "B29");
103 Assert.AreEqual(Color.FromArgb(0x1, 0x23, 0x45, 0x67), conv.ConvertFrom(null, null, "#1234567"), "B30");
104 Assert.AreEqual(Color.FromArgb(0x12, 0x34, 0x56, 0x78), conv.ConvertFrom(null, null, "#12345678"), "B31");
108 [ExpectedException(typeof(Exception))]
109 public void MalformatTest1 ()
111 WebColorConverter conv;
113 conv = new WebColorConverter();
114 Assert.AreEqual(Color.Fuchsia, conv.ConvertFrom(null, null, "\rFuchsi\na\n"), "M1");
118 [ExpectedException(typeof(Exception))]
119 public void MalformatTest2 ()
121 WebColorConverter conv;
123 conv = new WebColorConverter();
124 Assert.AreEqual(Color.FromArgb(255, 1, 2, 3), conv.ConvertFrom(null, null, "#010G03"), "M2");
129 [ExpectedException(typeof(Exception))]
130 public void MalformatTest3 ()
132 WebColorConverter conv;
134 conv = new WebColorConverter();
135 Assert.AreEqual(Color.FromArgb(255, 1, 2, 3), conv.ConvertFrom(null, null, "#010203Garbage"), "M3");
139 [ExpectedException(typeof(Exception))]
140 public void MalformatTest4 ()
142 WebColorConverter conv;
144 conv = new WebColorConverter();
145 Assert.AreEqual(Color.FromArgb(255, 1, 2, 3), conv.ConvertFrom(null, null, "#010203 Garbage"), "M4");
149 [ExpectedException(typeof(Exception))]
150 public void MalformatTest5 ()
152 WebColorConverter conv;
154 conv = new WebColorConverter();
156 Assert.AreEqual(Color.FromArgb(255, 254, 254, 254), conv.ConvertFrom(null, null, "4294901502"), "M5");
160 [ExpectedException(typeof(Exception))]
161 public void MalformatTest6 ()
163 WebColorConverter conv;
165 conv = new WebColorConverter();
166 Assert.AreEqual(Color.Fuchsia, conv.ConvertFrom(null, null, "#Fuchsia"), "M6");
170 [ExpectedException(typeof(Exception))]
171 public void MalformatTest7 ()
173 WebColorConverter conv;
175 conv = new WebColorConverter();
176 Assert.AreEqual(Color.FromArgb(255, 254, 254, 254), conv.ConvertFrom(null, null, "garbage"), "M7");