In .:
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / WebColorConverterTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.WebColorConverter.cs 
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 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 NUnit.Framework;
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.IO;
36 using System.Drawing;
37 using System.Drawing.Drawing2D;
38 using System.Globalization;
39 using System.Web;
40 using System.Web.UI;
41 using System.Web.UI.WebControls;
42
43 namespace MonoTests.System.Web.UI.WebControls
44 {
45         [TestFixture]   
46         public class WebColorConverterTest 
47         {
48                 [Test]
49                 public void Basic () 
50                 {
51                         WebColorConverter       conv;
52                         Color                   c;
53
54                         conv = new WebColorConverter();
55
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");
60
61                         c = Color.Fuchsia;
62                         Assert.AreEqual("Fuchsia", conv.ConvertTo(null, null, c, typeof(string)), "B5");
63
64                         c = Color.FromArgb(1, 2, 3);
65                         Assert.AreEqual("#010203", conv.ConvertTo(null, null, c, typeof(string)), "B6");
66
67                         c = Color.FromArgb(5, 1, 2, 3);
68                         Assert.AreEqual("#010203", conv.ConvertTo(null, null, c, typeof(string)), "B7");
69
70                         c = Color.FromArgb(254, 253, 252);
71                         Assert.AreEqual("#FEFDFC", conv.ConvertTo(null, null, c, typeof(string)), "B8");
72
73                         c = Color.FromKnownColor(KnownColor.BlanchedAlmond);
74                         Assert.AreEqual("BlanchedAlmond", conv.ConvertTo(null, null, c, typeof(string)), "B9");
75
76                         c = Color.FromName("OingoBoingo");
77                         Assert.AreEqual("#000000", conv.ConvertTo(null, null, c, typeof(string)), "B10");
78
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");
88
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");
94
95                         Assert.AreEqual(Color.Empty, conv.ConvertFrom(null, null, ""), "B23");
96
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");
105                 }
106
107                 [Test]
108                 [ExpectedException(typeof(Exception))]
109                 public void MalformatTest1 () 
110                 {
111                         WebColorConverter       conv;
112
113                         conv = new WebColorConverter();
114                         Assert.AreEqual(Color.Fuchsia, conv.ConvertFrom(null, null, "\rFuchsi\na\n"), "M1");
115                 }
116
117                 [Test]
118                 [ExpectedException(typeof(Exception))]
119                 public void MalformatTest2 () 
120                 {
121                         WebColorConverter       conv;
122
123                         conv = new WebColorConverter();
124                         Assert.AreEqual(Color.FromArgb(255, 1, 2, 3), conv.ConvertFrom(null, null, "#010G03"), "M2");
125                 }
126
127
128                 [Test]
129                 [ExpectedException(typeof(Exception))]
130                 public void MalformatTest3 () 
131                 {
132                         WebColorConverter       conv;
133
134                         conv = new WebColorConverter();
135                         Assert.AreEqual(Color.FromArgb(255, 1, 2, 3), conv.ConvertFrom(null, null, "#010203Garbage"), "M3");
136                 }
137
138                 [Test]
139                 [ExpectedException(typeof(Exception))]
140                 public void MalformatTest4 () 
141                 {
142                         WebColorConverter       conv;
143
144                         conv = new WebColorConverter();
145                         Assert.AreEqual(Color.FromArgb(255, 1, 2, 3), conv.ConvertFrom(null, null, "#010203 Garbage"), "M4");
146                 }
147
148                 [Test]
149                 [ExpectedException(typeof(Exception))]
150                 public void MalformatTest5 () 
151                 {
152                         WebColorConverter       conv;
153
154                         conv = new WebColorConverter();
155                         // Overflow
156                         Assert.AreEqual(Color.FromArgb(255, 254, 254, 254), conv.ConvertFrom(null, null, "4294901502"), "M5");
157                 }
158
159                 [Test]
160                 [ExpectedException(typeof(Exception))]
161                 public void MalformatTest6 () 
162                 {
163                         WebColorConverter       conv;
164
165                         conv = new WebColorConverter();
166                         Assert.AreEqual(Color.Fuchsia, conv.ConvertFrom(null, null, "#Fuchsia"), "M6");
167                 }
168
169                 [Test]
170                 [ExpectedException(typeof(Exception))]
171                 public void MalformatTest7 () 
172                 {
173                         WebColorConverter       conv;
174
175                         conv = new WebColorConverter();
176                         Assert.AreEqual(Color.FromArgb(255, 254, 254, 254), conv.ConvertFrom(null, null, "garbage"), "M7");
177                 }
178
179         }
180 }