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