1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
//
// Tests for System.Web.UI.WebControls.FontUnit.cs
//
// Author:
// Miguel de Icaza (miguel@novell.com)
//
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls;
using NUnit.Framework;
namespace MonoTests.System.Web.UI.WebControls
{
[TestFixture]
public class FontUnitTest {
[Test]
public void FontUnitConstructors1 ()
{
FontUnit f1 = new FontUnit (FontSize.Large);
Assert.AreEqual (f1.Type, FontSize.Large, "A1");
Assert.AreEqual (f1.Unit, Unit.Empty, "A1.1");
}
[Test]
public void FontUnitConstructors2 ()
{
// Test the AsUnit values
FontUnit f1 = new FontUnit (FontSize.AsUnit);
Assert.AreEqual (f1.Type, FontSize.AsUnit, "A2");
Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A3");
Assert.AreEqual (f1.Unit.Value, 10, "A4");
}
[Test]
public void FontUnitConstructors3 ()
{
FontUnit f1 = new FontUnit (15);
Assert.AreEqual (f1.Type, FontSize.AsUnit, "A5");
Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A6");
Assert.AreEqual (f1.Unit.Value, 15, "A7");
}
[Test]
public void FontUnitConstructors4 ()
{
// Test the string constructor: null and empty
FontUnit f1 = new FontUnit (null);
Assert.AreEqual (f1.Type, FontSize.NotSet, "A8");
Assert.AreEqual (f1.Unit.IsEmpty, true, "A9");
}
[Test]
public void FontUnitConstructors5 ()
{
FontUnit f1 = new FontUnit ("");
Assert.AreEqual (f1.Type, FontSize.NotSet, "A10");
Assert.AreEqual (f1.Unit.IsEmpty, true, "A11");
}
#if NET_2_0
[Test]
public void FontUnitConstructors6 ()
{
FontUnit f1 = new FontUnit (2.5);
Assert.AreEqual (f1.Type, FontSize.AsUnit, "A12");
Assert.AreEqual (f1.Unit.Type, UnitType.Point, "A13");
Assert.AreEqual (f1.Unit.Value, 2.5, "A14");
}
[Test]
public void FontUnitConstructors7 ()
{
FontUnit f1 = new FontUnit (5.0, UnitType.Percentage);
Assert.AreEqual (f1.Type, FontSize.AsUnit, "A15");
Assert.AreEqual (f1.Unit.Type, UnitType.Percentage, "A17");
Assert.AreEqual (f1.Unit.Value, 5.0, "A18");
}
#endif
[Test]
public void FontUnitConstructors_Em ()
{
CultureInfo originalCulture = CultureInfo.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
try {
FontUnit fu = new FontUnit ("4,5em");
Assert.AreEqual (FontSize.AsUnit, fu.Type, "#1");
Assert.AreEqual (UnitType.Em, fu.Unit.Type, "#2");
Assert.AreEqual (4.5, fu.Unit.Value, "#3");
Assert.AreEqual ("4,5em", fu.ToString (), "#4");
} finally {
// restore original culture
Thread.CurrentThread.CurrentCulture = originalCulture;
}
}
[Test]
public void FontUnitConstructors_Pixel ()
{
FontUnit f1 = new FontUnit ("10px");
Assert.AreEqual (FontSize.AsUnit, f1.Type, "#1");
Assert.AreEqual (UnitType.Pixel, f1.Unit.Type, "#2");
Assert.AreEqual (10, f1.Unit.Value, "#3");
Assert.AreEqual ("10px", f1.ToString (), "#4");
}
[Test]
public void FontUnitConstructors_Point ()
{
CultureInfo originalCulture = CultureInfo.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
try {
FontUnit f1 = new FontUnit ("12,5pt");
Assert.AreEqual (FontSize.AsUnit, f1.Type, "Type");
Assert.AreEqual (UnitType.Point, f1.Unit.Type, "Unit.Type");
Assert.AreEqual (12.5, f1.Unit.Value, "Unit.Value");
Assert.AreEqual ("12,5pt", f1.ToString (), "ToString");
} finally {
// restore original culture
Thread.CurrentThread.CurrentCulture = originalCulture;
}
}
[Test]
public void FontUnitConstructors_Enum1 ()
{
FontUnit fu = new FontUnit ("Large");
Assert.AreEqual (FontSize.Large, fu.Type, "Large");
Assert.IsTrue (fu.Unit.IsEmpty, "Large.IsEmpty");
Assert.AreEqual ("Large", fu.ToString (), "Large.ToString");
}
[Test]
public void FontUnitConstructors_Enum2 ()
{
FontUnit fu = new FontUnit ("Larger");
Assert.AreEqual (FontSize.Larger, fu.Type, "Larger");
Assert.IsTrue (fu.Unit.IsEmpty, "Larger.IsEmpty");
Assert.AreEqual ("Larger", fu.ToString (), "Larger.ToString");
}
[Test]
public void FontUnitConstructors_Enum3 ()
{
FontUnit fu = new FontUnit ("Medium");
Assert.AreEqual (FontSize.Medium, fu.Type, "Medium");
Assert.IsTrue (fu.Unit.IsEmpty, "Medium.IsEmpty");
Assert.AreEqual ("Medium", fu.ToString (), "Medium.ToString");
}
[Test]
public void FontUnitConstructors_Enum4 ()
{
FontUnit fu = new FontUnit ("Small");
Assert.AreEqual (FontSize.Small, fu.Type, "Small");
Assert.IsTrue (fu.Unit.IsEmpty, "Small.IsEmpty");
Assert.AreEqual ("Small", fu.ToString (), "Small.ToString");
}
[Test]
public void FontUnitConstructors_Enum5 ()
{
FontUnit fu = new FontUnit ("Smaller");
Assert.AreEqual (FontSize.Smaller, fu.Type, "Smaller");
Assert.IsTrue (fu.Unit.IsEmpty, "Smaller.IsEmpty");
Assert.AreEqual ("Smaller", fu.ToString (), "Smaller.ToString");
}
[Test]
public void FontUnitConstructors_Enum6 ()
{
FontUnit fu = new FontUnit ("XLarge");
Assert.AreEqual (FontSize.XLarge, fu.Type, "XLarge");
Assert.IsTrue (fu.Unit.IsEmpty, "XLarge.IsEmpty");
Assert.AreEqual ("X-Large", fu.ToString (), "XLarge.ToString");
}
[Test]
public void FontUnitConstructors_Enum7 ()
{
FontUnit fu = new FontUnit ("X-Large");
Assert.AreEqual (FontSize.XLarge, fu.Type, "X-Large");
Assert.IsTrue (fu.Unit.IsEmpty, "X-Large.IsEmpty");
Assert.AreEqual ("X-Large", fu.ToString (), "X-Large.ToString");
}
[Test]
public void FontUnitConstructors_Enum9 ()
{
FontUnit fu = new FontUnit ("XSmall");
Assert.AreEqual (FontSize.XSmall, fu.Type, "XSmall");
Assert.IsTrue (fu.Unit.IsEmpty, "XSmall.IsEmpty");
Assert.AreEqual ("X-Small", fu.ToString (), "XSmall.ToString");
}
[Test]
public void FontUnitConstructors_Enum10 ()
{
FontUnit fu = new FontUnit ("X-Small");
Assert.AreEqual (FontSize.XSmall, fu.Type, "X-Small");
Assert.IsTrue (fu.Unit.IsEmpty, "X-Small.IsEmpty");
Assert.AreEqual ("X-Small", fu.ToString (), "X-Small.ToString");
}
[Test]
public void FontUnitConstructors_Enum11 ()
{
FontUnit fu = new FontUnit ("XXLarge");
Assert.AreEqual (FontSize.XXLarge, fu.Type, "XXLarge");
Assert.IsTrue (fu.Unit.IsEmpty, "XXLarge.IsEmpty");
Assert.AreEqual ("XX-Large", fu.ToString (), "XXLarge.ToString");
}
[Test]
public void FontUnitConstructors_Enum12 ()
{
FontUnit fu = new FontUnit ("XX-Large");
Assert.AreEqual (FontSize.XXLarge, fu.Type, "XX-Large");
Assert.IsTrue (fu.Unit.IsEmpty, "XX-Large.IsEmpty");
Assert.AreEqual ("XX-Large", fu.ToString (), "XX-Large.ToString");
}
[Test]
public void FontUnitConstructors_Enum13 ()
{
FontUnit fu = new FontUnit ("XXSmall");
Assert.AreEqual (FontSize.XXSmall, fu.Type, "XXSmall");
Assert.IsTrue (fu.Unit.IsEmpty, "XXSmall.IsEmpty");
Assert.AreEqual ("XX-Small", fu.ToString (), "XXSmall.ToString");
}
[Test]
public void FontUnitConstructors_Enum14 ()
{
FontUnit fu = new FontUnit ("XX-Small");
Assert.AreEqual (FontSize.XXSmall, fu.Type, "XX-Small");
Assert.IsTrue (fu.Unit.IsEmpty, "XX-Small.IsEmpty");
Assert.AreEqual ("XX-Small", fu.ToString (), "XX-Small.ToString");
}
[Test]
public void UnitEquality ()
{
FontUnit u1 = new FontUnit ("1px");
FontUnit u2 = new FontUnit ("2px");
FontUnit t1 = new FontUnit ("1px");
FontUnit c2 = new FontUnit ("2cm");
Assert.AreEqual (u1 == t1, true, "U1");
Assert.AreEqual (u1 != u2, true, "U2");
Assert.AreEqual (u1 == u2, false, "U3");
Assert.AreEqual (u1 != t1, false, "U4");
// Test that its comparing the units and value
Assert.AreEqual (u2 != c2, true, "U5");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IncorrectConstructor ()
{
FontUnit a = new FontUnit ((FontSize) (-1));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IncorrectConstructor2 ()
{
FontUnit a = new FontUnit ((FontSize) (FontSize.XXLarge + 1));
}
[Test]
[ExpectedException (typeof (FormatException))]
public void FontUnitConstructors_Enum_AsUnit ()
{
new FontUnit ("AsUnit");
}
[Test]
[ExpectedException (typeof (FormatException))]
public void FontUnitConstructors_Enum_NotSet ()
{
new FontUnit ("NotSet");
}
#if NET_2_0
class MyFormatProvider : IFormatProvider
{
public object GetFormat (Type format_type)
{
if (format_type.IsAssignableFrom (this.GetType ())) {
return this;
}
return null;
}
}
[Test]
public void FontUnit_IFormatProviderToString ()
{
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
try {
CultureInfo ci = CultureInfo.GetCultureInfo ("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
RunFontUnit_IFormatProviderToString_Tests ();
} finally {
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentUICulture;
}
}
void RunFontUnit_IFormatProviderToString_Tests ()
{
MyFormatProvider mfp = new MyFormatProvider ();
FontUnit f1 = new FontUnit (FontSize.Large);
Assert.AreEqual ("Large", f1.ToString (mfp), "T1");
f1 = new FontUnit (FontSize.AsUnit);
Assert.AreEqual ("10pt", f1.ToString (mfp), "T2");
f1 = new FontUnit (15);
Assert.AreEqual ("15pt", f1.ToString (mfp), "T3");
f1 = new FontUnit (null);
Assert.AreEqual ("", f1.ToString (mfp), "T4");
f1 = new FontUnit ("");
Assert.AreEqual ("", f1.ToString (mfp), "T5");
f1 = new FontUnit (2.5);
Assert.AreEqual ("2.5pt", f1.ToString (mfp), "T6");
f1 = new FontUnit (5.0, UnitType.Percentage);
Assert.AreEqual ("5%", f1.ToString (mfp), "T7");
}
#endif
}
}
|