e8b68c56f4d3eaf7702952dde3af77ea941fe6c2
[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                 [ExpectedException (typeof (ArgumentException))]
70                 public void Default_Dispose ()
71                 {
72                         StringFormat sf = new StringFormat ();
73                         sf.Dispose ();
74                         sf.ToString ();
75                 }
76
77                 [Test]
78                 [ExpectedException (typeof (ArgumentNullException))]
79                 public void ctor_StringFormat_Null ()
80                 {
81                         new StringFormat (null);
82                 }
83
84                 [Test]
85                 public void ctor_StringFormat ()
86                 {
87                         using (StringFormat sf = new StringFormat (StringFormat.GenericTypographic)) {
88                                 CheckTypographic (sf);
89                         }
90                 }
91
92                 [Test]
93                 public void ctor_StringFormatFlags ()
94                 {
95                         using (StringFormat sf = new StringFormat ((StringFormatFlags)Int32.MinValue)) {
96                                 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
97                         }
98                 }
99
100                 [Test]
101                 public void ctor_StringFormatFlags_Int32 ()
102                 {
103                         using (StringFormat sf = new StringFormat ((StringFormatFlags) Int32.MinValue, Int32.MinValue)) {
104                                 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
105                                 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
106                         }
107                 }
108
109                 [Test]
110                 public void GenericDefault ()
111                 {
112                         CheckDefaults (StringFormat.GenericDefault);
113                 }
114
115                 [Test]
116                 public void GenericDefault_Dispose ()
117                 {
118                         StringFormat.GenericDefault.Dispose ();
119                         CheckDefaults (StringFormat.GenericDefault);
120                 }
121
122                 [Test]
123                 [ExpectedException (typeof (ArgumentException))]
124                 public void GenericDefault_Local_Dispose ()
125                 {
126                         StringFormat sf = StringFormat.GenericDefault;
127                         sf.Dispose (); // can't be cached
128                         CheckDefaults (sf);
129                 }
130
131                 private void CheckTypographic (StringFormat sf)
132                 {
133                         Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
134                         Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
135                         Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
136                         Assert.AreEqual (StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.NoClip, sf.FormatFlags, "FormatFlags");
137                         Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
138                         Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
139                         Assert.AreEqual (StringTrimming.None, sf.Trimming, "Trimming");
140                 }
141
142                 [Test]
143                 public void GenericTypographic ()
144                 {
145                         StringFormat sf = StringFormat.GenericTypographic;
146                         CheckTypographic (sf);
147                         Assert.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf.ToString (), "ToString");
148                 }
149
150                 [Test]
151                 public void GenericTypographic_Dispose ()
152                 {
153                         StringFormat.GenericTypographic.Dispose ();
154                         CheckTypographic (StringFormat.GenericTypographic);
155                 }
156
157                 [Test]
158                 [ExpectedException (typeof (ArgumentException))]
159                 public void GenericTypographic_Local_Dispose ()
160                 {
161                         StringFormat sf = StringFormat.GenericTypographic;
162                         sf.Dispose (); // can't be cached
163                         CheckTypographic (sf);
164                 }
165
166                 [Test]
167                 public void Alignment_All ()
168                 {
169                         using (StringFormat sf = new StringFormat ()) {
170                                 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
171                                         sf.Alignment = sa;
172                                         Assert.AreEqual (sa, sf.Alignment, sa.ToString ());
173                                 }
174                         }
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (InvalidEnumArgumentException))]
179                 public void Alignment_Invalid ()
180                 {
181                         using (StringFormat sf = new StringFormat ()) {
182                                 sf.Alignment = (StringAlignment) Int32.MinValue;
183                         }
184                 }
185
186                 [Test]
187                 public void HotkeyPrefix_All ()
188                 {
189                         using (StringFormat sf = new StringFormat ()) {
190                                 foreach (HotkeyPrefix hp in Enum.GetValues (typeof (HotkeyPrefix))) {
191                                         sf.HotkeyPrefix = hp;
192                                         Assert.AreEqual (hp, sf.HotkeyPrefix, hp.ToString ());
193                                 }
194                         }
195                 }
196
197                 [Test]
198                 [ExpectedException (typeof (InvalidEnumArgumentException))]
199                 public void HotkeyPrefix_Invalid ()
200                 {
201                         using (StringFormat sf = new StringFormat ()) {
202                                 sf.HotkeyPrefix = (HotkeyPrefix) Int32.MinValue;
203                         }
204                 }
205
206                 [Test]
207                 public void LineAlignment_All ()
208                 {
209                         using (StringFormat sf = new StringFormat ()) {
210                                 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
211                                         sf.LineAlignment = sa;
212                                         Assert.AreEqual (sa, sf.LineAlignment, sa.ToString ());
213                                 }
214                         }
215                 }
216
217                 [Test]
218                 [ExpectedException (typeof (InvalidEnumArgumentException))]
219                 public void LineAlignment_Invalid ()
220                 {
221                         using (StringFormat sf = new StringFormat ()) {
222                                 sf.LineAlignment = (StringAlignment) Int32.MinValue;
223                         }
224                 }
225
226                 [Test]
227                 public void Trimming_All ()
228                 {
229                         using (StringFormat sf = new StringFormat ()) {
230                                 foreach (StringTrimming st in Enum.GetValues (typeof (StringTrimming))) {
231                                         sf.Trimming = st;
232                                         Assert.AreEqual (st, sf.Trimming, st.ToString ());
233                                 }
234                         }
235                 }
236
237                 [Test]
238                 [ExpectedException (typeof (InvalidEnumArgumentException))]
239                 public void Trimming_Invalid ()
240                 {
241                         using (StringFormat sf = new StringFormat ()) {
242                                 sf.Trimming = (StringTrimming) Int32.MinValue;
243                         }
244                 }
245
246                 [Test]
247                 public void Clone() 
248                 {
249                         using (StringFormat sf = new StringFormat ()) {
250                                 using (StringFormat clone = (StringFormat) sf.Clone ()) {
251                                         CheckDefaults (clone);
252                                 }
253                         }
254                 }
255
256                 [Test]
257                 public void Clone_Complex ()
258                 {
259                         using (StringFormat sf = new StringFormat ()) {
260                                 CharacterRange[] ranges = new CharacterRange [2];
261                                 ranges[0].First = 1;
262                                 ranges[0].Length = 2;
263                                 ranges[1].First = 3;
264                                 ranges[1].Length = 4;
265                                 sf.SetMeasurableCharacterRanges (ranges);
266
267                                 float[] stops = new float [2];
268                                 stops [0] = 6.0f;
269                                 stops [1] = 7.0f;
270                                 sf.SetTabStops (5.0f, stops);
271
272                                 using (StringFormat clone = (StringFormat) sf.Clone ()) {
273                                         CheckDefaults (clone);
274
275                                         float first;
276                                         float[] cloned_stops = clone.GetTabStops (out first);
277                                         Assert.AreEqual (5.0f, first, "first");
278                                         Assert.AreEqual (6.0f, cloned_stops[0], "cloned_stops[0]");
279                                         Assert.AreEqual (7.0f, cloned_stops[1], "cloned_stops[1]");
280                                 }
281                         }
282                 }
283                         
284                 [Test]
285                 public void TestFormatFlags() 
286                 {
287                         using (StringFormat smf = new StringFormat ()) {
288                                 smf.FormatFlags = StringFormatFlags.DisplayFormatControl;
289                                 Assert.AreEqual (StringFormatFlags.DisplayFormatControl, smf.FormatFlags);
290                         }
291                 }               
292                 
293                 [Test]
294                 public void TabsStops() 
295                 {
296                         using (StringFormat smf = new StringFormat ()) {
297                                 float firstTabOffset;
298                                 float[] tabsSrc = { 100, 200, 300, 400 };
299                                 float[] tabStops;
300
301                                 smf.SetTabStops (200, tabsSrc);
302                                 tabStops = smf.GetTabStops (out firstTabOffset);
303
304                                 Assert.AreEqual (200, firstTabOffset);
305                                 Assert.AreEqual (tabsSrc.Length, tabStops.Length);
306                                 Assert.AreEqual (tabsSrc[0], tabStops[0]);
307                                 Assert.AreEqual (tabsSrc[1], tabStops[1]);
308                                 Assert.AreEqual (tabsSrc[2], tabStops[2]);
309                                 Assert.AreEqual (tabsSrc[3], tabStops[3]);
310                         }
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (NullReferenceException))]
315                 public void SetTabStops_Null ()
316                 {
317                         using (StringFormat sf = new StringFormat ()) {
318                                 sf.SetTabStops (Single.NaN, null);
319                         }
320                 }
321
322                 [Test]
323                 public void SetDigitSubstitution ()
324                 {
325                         using (StringFormat sf = new StringFormat ()) {
326                                 sf.SetDigitSubstitution (Int32.MinValue, (StringDigitSubstitute) Int32.MinValue);
327                                 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
328                                 Assert.AreEqual ((StringDigitSubstitute) Int32.MinValue, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
329                         }
330                 }
331
332                 [Test]
333                 [ExpectedException (typeof (NullReferenceException))]
334                 public void SetMeasurableCharacterRanges_Null ()
335                 {
336                         using (StringFormat sf = new StringFormat ()) {
337                                 sf.SetMeasurableCharacterRanges (null);
338                         }
339                 }
340
341                 [Test]
342                 public void SetMeasurableCharacterRanges_Empty ()
343                 {
344                         using (StringFormat sf = new StringFormat ()) {
345                                 CharacterRange[] range = new CharacterRange[0];
346                                 sf.SetMeasurableCharacterRanges (range);
347                         }
348                 }
349
350                 [Test]
351                 public void SetMeasurableCharacterRanges_Max ()
352                 {
353                         using (StringFormat sf = new StringFormat ()) {
354                                 CharacterRange[] range = new CharacterRange[32];
355                                 sf.SetMeasurableCharacterRanges (range);
356                         }
357                 }
358
359                 [Test]
360                 [ExpectedException (typeof (OverflowException))]
361                 public void SetMeasurableCharacterRanges_TooBig ()
362                 {
363                         using (StringFormat sf = new StringFormat ()) {
364                                 CharacterRange[] range = new CharacterRange[33];
365                                 sf.SetMeasurableCharacterRanges (range);
366                         }
367                 }
368         }
369 }