6e20da75335b9320fe45509ad510dfd993367e40
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestStringFormat.cs
1 //
2 // StringFormat class testing unit
3 //
4 // Authors:
5 //       Jordi Mas i Hernàndez (jordi@ximian.com)
6 //       Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2004 Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2006 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.ComponentModel;
33 using System.Drawing;
34 using System.Drawing.Text;
35 using System.Security.Permissions;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Drawing{
39
40         [TestFixture]   
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class StringFormatTest {
43
44                 private void CheckDefaults (StringFormat sf)
45                 {
46                         Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
47                         Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
48                         Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
49                         Assert.AreEqual ((StringFormatFlags) 0, sf.FormatFlags, "FormatFlags");
50                         Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
51                         Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
52                         Assert.AreEqual (StringTrimming.Character, sf.Trimming, "Trimming");
53                 }
54
55                 [Test]
56                 public void Default ()
57                 {
58                         using (StringFormat sf = new StringFormat ()) {
59                                 CheckDefaults (sf);
60                                 Assert.AreEqual ("[StringFormat, FormatFlags=0]", sf.ToString (), "ToString");
61                                 // check setters validations
62                                 sf.FormatFlags = (StringFormatFlags) Int32.MinValue;
63                                 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "Min-FormatFlags");
64                                 Assert.AreEqual ("[StringFormat, FormatFlags=-2147483648]", sf.ToString (), "ToString-2");
65                         }
66                 }
67
68                 [Test]
69                 public void Default_Dispose ()
70                 {
71                         StringFormat sf = new StringFormat ();
72                         sf.Dispose ();
73                         Assert.Throws<ArgumentException> (() => sf.ToString ());
74                 }
75
76                 [Test]
77                 public void ctor_StringFormat_Null ()
78                 {
79                         Assert.Throws<ArgumentNullException> (() => new StringFormat (null));
80                 }
81
82                 [Test]
83                 public void ctor_StringFormat ()
84                 {
85                         using (StringFormat sf = new StringFormat (StringFormat.GenericTypographic)) {
86                                 CheckTypographic (sf);
87                         }
88                 }
89
90                 [Test]
91                 public void ctor_StringFormatFlags ()
92                 {
93                         using (StringFormat sf = new StringFormat ((StringFormatFlags)Int32.MinValue)) {
94                                 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
95                         }
96                 }
97
98                 [Test]
99                 public void ctor_StringFormatFlags_Int32 ()
100                 {
101                         using (StringFormat sf = new StringFormat ((StringFormatFlags) Int32.MinValue, Int32.MinValue)) {
102                                 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
103                                 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
104                         }
105                 }
106
107                 [Test]
108                 public void GenericDefault ()
109                 {
110                         CheckDefaults (StringFormat.GenericDefault);
111                 }
112
113                 [Test]
114                 public void GenericDefault_Dispose ()
115                 {
116                         StringFormat.GenericDefault.Dispose ();
117                         CheckDefaults (StringFormat.GenericDefault);
118                 }
119
120                 [Test]
121                 public void GenericDefault_Local_Dispose ()
122                 {
123                         StringFormat sf = StringFormat.GenericDefault;
124                         sf.Dispose (); // can't be cached
125                         Assert.Throws<ArgumentException> (() => CheckDefaults (sf));
126                 }
127
128                 private void CheckTypographic (StringFormat sf)
129                 {
130                         Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
131                         Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
132                         Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
133                         Assert.AreEqual (StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.NoClip, sf.FormatFlags, "FormatFlags");
134                         Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
135                         Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
136                         Assert.AreEqual (StringTrimming.None, sf.Trimming, "Trimming");
137                 }
138
139                 [Test]
140                 public void GenericTypographic ()
141                 {
142                         StringFormat sf = StringFormat.GenericTypographic;
143                         CheckTypographic (sf);
144                         Assert.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf.ToString (), "ToString");
145                 }
146
147                 [Test]
148                 public void GenericTypographic_Dispose ()
149                 {
150                         StringFormat.GenericTypographic.Dispose ();
151                         CheckTypographic (StringFormat.GenericTypographic);
152                 }
153
154                 [Test]
155                 public void GenericTypographic_Local_Dispose ()
156                 {
157                         StringFormat sf = StringFormat.GenericTypographic;
158                         sf.Dispose (); // can't be cached
159                         Assert.Throws<ArgumentException> (() => CheckTypographic (sf));
160                 }
161
162                 [Test]
163                 public void Alignment_All ()
164                 {
165                         using (StringFormat sf = new StringFormat ()) {
166                                 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
167                                         sf.Alignment = sa;
168                                         Assert.AreEqual (sa, sf.Alignment, sa.ToString ());
169                                 }
170                         }
171                 }
172
173                 [Test]
174                 public void Alignment_Invalid ()
175                 {
176                         using (StringFormat sf = new StringFormat ()) {
177                                 Assert.Throws<InvalidEnumArgumentException> (() => sf.Alignment = (StringAlignment) Int32.MinValue);
178                         }
179                 }
180
181                 [Test]
182                 public void HotkeyPrefix_All ()
183                 {
184                         using (StringFormat sf = new StringFormat ()) {
185                                 foreach (HotkeyPrefix hp in Enum.GetValues (typeof (HotkeyPrefix))) {
186                                         sf.HotkeyPrefix = hp;
187                                         Assert.AreEqual (hp, sf.HotkeyPrefix, hp.ToString ());
188                                 }
189                         }
190                 }
191
192                 [Test]
193                 public void HotkeyPrefix_Invalid ()
194                 {
195                         using (StringFormat sf = new StringFormat ()) {
196                                 Assert.Throws<InvalidEnumArgumentException> (() => sf.HotkeyPrefix = (HotkeyPrefix) Int32.MinValue);
197                         }
198                 }
199
200                 [Test]
201                 public void LineAlignment_All ()
202                 {
203                         using (StringFormat sf = new StringFormat ()) {
204                                 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
205                                         sf.LineAlignment = sa;
206                                         Assert.AreEqual (sa, sf.LineAlignment, sa.ToString ());
207                                 }
208                         }
209                 }
210
211                 [Test]
212                 public void LineAlignment_Invalid ()
213                 {
214                         using (StringFormat sf = new StringFormat ()) {
215                                 Assert.Throws<InvalidEnumArgumentException> (() => sf.LineAlignment = (StringAlignment) Int32.MinValue);
216                         }
217                 }
218
219                 [Test]
220                 public void Trimming_All ()
221                 {
222                         using (StringFormat sf = new StringFormat ()) {
223                                 foreach (StringTrimming st in Enum.GetValues (typeof (StringTrimming))) {
224                                         sf.Trimming = st;
225                                         Assert.AreEqual (st, sf.Trimming, st.ToString ());
226                                 }
227                         }
228                 }
229
230                 [Test]
231                 public void Trimming_Invalid ()
232                 {
233                         using (StringFormat sf = new StringFormat ()) {
234                                 Assert.Throws<InvalidEnumArgumentException> (() => sf.Trimming = (StringTrimming) Int32.MinValue);
235                         }
236                 }
237
238                 [Test]
239                 public void Clone() 
240                 {
241                         using (StringFormat sf = new StringFormat ()) {
242                                 using (StringFormat clone = (StringFormat) sf.Clone ()) {
243                                         CheckDefaults (clone);
244                                 }
245                         }
246                 }
247
248                 [Test]
249                 public void Clone_Complex ()
250                 {
251                         using (StringFormat sf = new StringFormat ()) {
252                                 CharacterRange[] ranges = new CharacterRange [2];
253                                 ranges[0].First = 1;
254                                 ranges[0].Length = 2;
255                                 ranges[1].First = 3;
256                                 ranges[1].Length = 4;
257                                 sf.SetMeasurableCharacterRanges (ranges);
258
259                                 float[] stops = new float [2];
260                                 stops [0] = 6.0f;
261                                 stops [1] = 7.0f;
262                                 sf.SetTabStops (5.0f, stops);
263
264                                 using (StringFormat clone = (StringFormat) sf.Clone ()) {
265                                         CheckDefaults (clone);
266
267                                         float first;
268                                         float[] cloned_stops = clone.GetTabStops (out first);
269                                         Assert.AreEqual (5.0f, first, "first");
270                                         Assert.AreEqual (6.0f, cloned_stops[0], "cloned_stops[0]");
271                                         Assert.AreEqual (7.0f, cloned_stops[1], "cloned_stops[1]");
272                                 }
273                         }
274                 }
275                         
276                 [Test]
277                 public void TestFormatFlags() 
278                 {
279                         using (StringFormat smf = new StringFormat ()) {
280                                 smf.FormatFlags = StringFormatFlags.DisplayFormatControl;
281                                 Assert.AreEqual (StringFormatFlags.DisplayFormatControl, smf.FormatFlags);
282                         }
283                 }               
284                 
285                 [Test]
286                 public void TabsStops() 
287                 {
288                         using (StringFormat smf = new StringFormat ()) {
289                                 float firstTabOffset;
290                                 float[] tabsSrc = { 100, 200, 300, 400 };
291                                 float[] tabStops;
292
293                                 smf.SetTabStops (200, tabsSrc);
294                                 tabStops = smf.GetTabStops (out firstTabOffset);
295
296                                 Assert.AreEqual (200, firstTabOffset);
297                                 Assert.AreEqual (tabsSrc.Length, tabStops.Length);
298                                 Assert.AreEqual (tabsSrc[0], tabStops[0]);
299                                 Assert.AreEqual (tabsSrc[1], tabStops[1]);
300                                 Assert.AreEqual (tabsSrc[2], tabStops[2]);
301                                 Assert.AreEqual (tabsSrc[3], tabStops[3]);
302                         }
303                 }
304
305                 [Test]
306                 public void SetTabStops_Null ()
307                 {
308                         using (StringFormat sf = new StringFormat ()) {
309                                 Assert.Throws<NullReferenceException> (() => sf.SetTabStops (Single.NaN, null));
310                         }
311                 }
312
313                 [Test]
314                 public void SetDigitSubstitution ()
315                 {
316                         using (StringFormat sf = new StringFormat ()) {
317                                 sf.SetDigitSubstitution (Int32.MinValue, (StringDigitSubstitute) Int32.MinValue);
318                                 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
319                                 Assert.AreEqual ((StringDigitSubstitute) Int32.MinValue, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
320                         }
321                 }
322
323                 [Test]
324                 public void SetMeasurableCharacterRanges_Null ()
325                 {
326                         using (StringFormat sf = new StringFormat ()) {
327                                 Assert.Throws<NullReferenceException> (() => sf.SetMeasurableCharacterRanges (null));
328                         }
329                 }
330
331                 [Test]
332                 public void SetMeasurableCharacterRanges_Empty ()
333                 {
334                         using (StringFormat sf = new StringFormat ()) {
335                                 CharacterRange[] range = new CharacterRange[0];
336                                 sf.SetMeasurableCharacterRanges (range);
337                         }
338                 }
339
340                 [Test]
341                 public void SetMeasurableCharacterRanges_Max ()
342                 {
343                         using (StringFormat sf = new StringFormat ()) {
344                                 CharacterRange[] range = new CharacterRange[32];
345                                 sf.SetMeasurableCharacterRanges (range);
346                         }
347                 }
348
349                 [Test]
350                 public void SetMeasurableCharacterRanges_TooBig ()
351                 {
352                         using (StringFormat sf = new StringFormat ()) {
353                                 CharacterRange[] range = new CharacterRange[33];
354                                 Assert.Throws<OverflowException> (() => sf.SetMeasurableCharacterRanges (range));
355                         }
356                 }
357         }
358 }