[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms.Layout / TableLayoutSettingsTypeConverterTest.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2006 Novell, Inc.
21 //
22
23
24 using System;
25 using System.ComponentModel;
26 using System.Globalization;
27 using System.Windows.Forms;
28 using System.Windows.Forms.Layout;
29
30 using NUnit.Framework;
31 using System.Collections.Generic;
32
33 namespace MonoTests.System.Windows.Forms.Layout {
34
35         [TestFixture]
36         public class TableLayoutSettingsTypeConverterTest : TestHelper {
37                 
38                 [Test]
39                 public void CanConvertFrom ()
40                 {
41                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter ();
42
43                         Assert.IsTrue (c.CanConvertFrom (null, typeof (string)), "1");
44                         Assert.IsFalse (c.CanConvertFrom (null, typeof (int)), "2");
45                         Assert.IsFalse (c.CanConvertFrom (null, typeof (float)), "3");
46                         Assert.IsFalse (c.CanConvertFrom (null, typeof (object)), "4");
47                 }
48
49                 [Test]
50                 public void CanConvertTo ()
51                 {
52                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter ();
53
54                         Assert.IsTrue (c.CanConvertTo (null, typeof (string)), "1");
55                         Assert.IsFalse (c.CanConvertTo (null, typeof (int)), "2");
56                         Assert.IsFalse (c.CanConvertTo (null, typeof (float)), "3");
57                         Assert.IsFalse (c.CanConvertTo (null, typeof (object)), "4");
58                 }
59
60                 [Test]
61                 public void Roundtrip ()
62                 {
63                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter ();
64                         object result;
65
66                         string sv = @"<?xml version=""1.0"" encoding=""utf-16""?><TableLayoutSettings>"
67                                 + @"<Controls>"
68                                 +   @"<Control Name=""userNameLabel"" Row=""0"" RowSpan=""1"" Column=""0"" ColumnSpan=""1"" />"
69                                 +   @"<Control Name=""savePassword"" Row=""2"" RowSpan=""1"" Column=""1"" ColumnSpan=""1"" />"
70                                 +   @"<Control Name=""userName"" Row=""0"" RowSpan=""1"" Column=""1"" ColumnSpan=""1"" />"
71                                 +   @"<Control Name=""password"" Row=""1"" RowSpan=""1"" Column=""1"" ColumnSpan=""1"" />"
72                                 +   @"<Control Name=""passwordLabel"" Row=""1"" RowSpan=""1"" Column=""0"" ColumnSpan=""1"" />"
73                                 + @"</Controls><Columns Styles=""AutoSize,0,Percent,100"" />"
74                                 + @"<Rows Styles=""AutoSize,0,AutoSize,0,AutoSize,0"" />"
75                                 + @"</TableLayoutSettings>";
76
77                         result = c.ConvertFrom (null, null, sv);
78
79                         Assert.AreEqual (typeof (TableLayoutSettings), result.GetType(), "1");
80
81                         TableLayoutSettings ts = (TableLayoutSettings)result;
82
83                         Assert.AreEqual (2, ts.ColumnStyles.Count, "2");
84                         Assert.AreEqual (SizeType.AutoSize, ts.ColumnStyles[0].SizeType, "3");
85                         Assert.AreEqual (0.0f, ts.ColumnStyles[0].Width, "4");
86                         Assert.AreEqual (SizeType.Percent, ts.ColumnStyles[1].SizeType, "5");
87                         Assert.AreEqual (100.0f, ts.ColumnStyles[1].Width, "6");
88
89                         Assert.AreEqual (3, ts.RowStyles.Count, "7");
90
91                         Assert.AreEqual (SizeType.AutoSize, ts.RowStyles[0].SizeType, "8");
92                         Assert.AreEqual (0.0f, ts.RowStyles[0].Height, "9");
93                         Assert.AreEqual (SizeType.AutoSize, ts.RowStyles[1].SizeType, "10");
94                         Assert.AreEqual (0.0f, ts.RowStyles[1].Height, "11");
95                         Assert.AreEqual (SizeType.AutoSize, ts.RowStyles[2].SizeType, "12");
96                         Assert.AreEqual (0.0f, ts.RowStyles[2].Height, "13");
97
98                         string rv = (string)c.ConvertTo (null, null, ts, typeof (string));
99
100                         // We do not guarantee the order of <Controls>, but the length should be the same
101                         Assert.AreEqual (sv.Length, rv.Length, "roundtrip");
102                 }
103
104                 //--------------------------------------------------------------
105                 private static TableLayoutSettings CreateSettingsEmpty()
106                 {
107                         return new TableLayoutPanel().LayoutSettings;
108                 }
109                 const String XmlSettingsEmpty
110                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
111                         + "<TableLayoutSettings>"
112                         + "<Controls />"
113                         + "<Columns Styles=\"\" /><Rows Styles=\"\" />"
114                         + "</TableLayoutSettings>";
115
116                 private static TableLayoutSettings CreateSettingsB()
117                 {
118                         TableLayoutPanel tlp = new TableLayoutPanel();
119                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
120                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F));
121                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 10F));
122                         tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 40F));
123                         tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 35F));
124                         return tlp.LayoutSettings;
125                 }
126                 const String XmlSettingsB
127                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
128                         + "<TableLayoutSettings>"
129                         + "<Controls />"
130                         + "<Columns Styles=\"Percent,50,Percent,40,Absolute,10\" />"
131                         + "<Rows Styles=\"Percent,40,Percent,35\" />"
132                         + "</TableLayoutSettings>";
133
134                 private static TableLayoutSettings CreateSettingsC()
135                 {
136                         TableLayoutPanel tlp = new TableLayoutPanel();
137                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
138                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 400));
139                         tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 40F));
140                         tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize, 35F));
141                         tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
142                         return tlp.LayoutSettings;
143                 }
144                 const String XmlSettingsC
145                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
146                         + "<TableLayoutSettings>"
147                         + "<Controls />"
148                         + "<Columns Styles=\"AutoSize,0,Absolute,400\" />"
149                         + "<Rows Styles=\"Percent,40,AutoSize,35,AutoSize,0\" />"
150                         + "</TableLayoutSettings>";
151
152                 private static TableLayoutSettings CreateSettingsD()
153                 {
154                         TableLayoutPanel tlp = new TableLayoutPanel();
155                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
156                         tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
157                         return tlp.LayoutSettings;
158                 }
159                 const String XmlSettingsD
160                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
161                         + "<TableLayoutSettings>"
162                         + "<Controls />"
163                         + "<Columns Styles=\"Percent,100\" />"
164                         + "<Rows Styles=\"Percent,100\" />"
165                         + "</TableLayoutSettings>";
166
167                 private static TableLayoutSettings CreateSettingsEDecimalOneItem()
168                 {
169                         TableLayoutPanel tlp = new TableLayoutPanel();
170                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 98.9F));
171                         tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 99.9F));
172                         return tlp.LayoutSettings;
173                 }
174                 const String XmlSettingsEDecimalOneItem
175                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
176                         + "<TableLayoutSettings>"
177                         + "<Controls />"
178                         + "<Columns Styles=\"Percent,98.9\" />"
179                         + "<Rows Styles=\"Percent,99.9\" />"
180                         + "</TableLayoutSettings>";
181                 // See https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=156278&wa=wsignin1.0
182                 // and e.g. http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.dotnet.internationalization&tid=f11516e2-33da-4047-8e2f-205df4ab09e5&cat=en_US_3fcb35c8-ccb3-4554-bd55-8038c0ecc923&lang=en&cr=US&sloc=&p=1
183                 // Uses comma as separator, but uses current cultures number formatting thus 99,9%
184                 const String XmlSettingsEDecimalOneItemCommaDecimalPoint
185                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
186                         + "<TableLayoutSettings>"
187                         + "<Controls />"
188                         + "<Columns Styles=\"Percent,98,9\" />"
189                         + "<Rows Styles=\"Percent,99,9\" />"
190                         + "</TableLayoutSettings>";
191                 const String XmlSettingsEDecimalOneItemCommaDecimalPoint_RowDataFirst
192                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
193                         + "<TableLayoutSettings>"
194                         + "<Controls />"
195                         + "<Rows Styles=\"Percent,99,9\" />"
196                         + "<Columns Styles=\"Percent,98,9\" />"
197                         + "</TableLayoutSettings>";
198
199                 private static TableLayoutSettings CreateSettingsF()
200                 {
201                         TableLayoutPanel tlp = new TableLayoutPanel();
202                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
203                         tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
204                         return tlp.LayoutSettings;
205                 }
206                 const String XmlSettingsF
207                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
208                         + "<TableLayoutSettings>"
209                         + "<Controls />"
210                         + "<Columns Styles=\"AutoSize,0\" />"
211                         + "<Rows Styles=\"AutoSize,0\" />"
212                         + "</TableLayoutSettings>";
213                 const String XmlSettingsF_EmptyNumericElement
214                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
215                         + "<TableLayoutSettings>"
216                         + "<Controls />"
217                         + "<Columns Styles=\"AutoSize,\" />"
218                         + "<Rows Styles=\"AutoSize,\" />"
219                         + "</TableLayoutSettings>";
220                 const String XmlSettingsF_NoNumericElement
221                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
222                         + "<TableLayoutSettings>"
223                         + "<Controls />"
224                         + "<Columns Styles=\"AutoSize\" />"
225                         + "<Rows Styles=\"AutoSize\" />"
226                         + "</TableLayoutSettings>";
227
228                 private static TableLayoutSettings CreateSettingsGDecimalTwoItems()
229                 {
230                         TableLayoutPanel tlp = new TableLayoutPanel();
231                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 12.3456F));
232                         tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.3333F));
233                         tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 98.7654F));
234                         tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 33.3333F));
235                         return tlp.LayoutSettings;
236                 }
237                 const String XmlSettingsGDecimalTwoItems
238                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
239                         + "<TableLayoutSettings>"
240                         + "<Controls />"
241                         + "<Columns Styles=\"Percent,12.3456,Percent,33.3333\" />"
242                         + "<Rows Styles=\"Percent,98.7654,Percent,33.3333\" />"
243                         + "</TableLayoutSettings>";
244                 // See https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=156278&wa=wsignin1.0
245                 // and e.g. http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.dotnet.internationalization&tid=f11516e2-33da-4047-8e2f-205df4ab09e5&cat=en_US_3fcb35c8-ccb3-4554-bd55-8038c0ecc923&lang=en&cr=US&sloc=&p=1
246                 // Uses comma as separator, but uses current cultures number formatting thus 99,9%
247                 // !!!! e.g. <Columns Styles="Percent,99,9" />
248                 const String XmlSettingsGDecimalTwoItems_CommaDecimalPoint
249                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
250                         + "<TableLayoutSettings>"
251                         + "<Controls />"
252                         + "<Columns Styles=\"Percent,12,3456,Percent,33,3333\" />"
253                         + "<Rows Styles=\"Percent,98,7654,Percent,33,3333\" />"
254                         + "</TableLayoutSettings>";
255                 const String XmlSettingsGDecimalTwoItems_CommaDecimalPoint_RowDataFirst
256                         = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"
257                         + "<TableLayoutSettings>"
258                         + "<Controls />"
259                         + "<Rows Styles=\"Percent,98,7654,Percent,33,3333\" />"
260                         + "<Columns Styles=\"Percent,12,3456,Percent,33,3333\" />"
261                         + "</TableLayoutSettings>";
262
263                 //--------------------------------------------------------------
264                 [Test]
265                 public void ConvertTo()
266                 {
267                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
268
269                         TableLayoutSettings tls;
270                         String result;
271                         //
272                         tls = CreateSettingsEmpty();
273                         result = c.ConvertToInvariantString(tls);
274                         Assert.AreEqual(XmlSettingsEmpty, result, "#1-empty");
275                         //
276                         tls = CreateSettingsB();
277                         result = c.ConvertToInvariantString(tls);
278                         Assert.AreEqual(XmlSettingsB, result, "#B");
279                         //
280                         tls = CreateSettingsC();
281                         result = c.ConvertToInvariantString(tls);
282                         Assert.AreEqual(XmlSettingsC, result, "#C");
283                         //
284                         tls = CreateSettingsD();
285                         result = c.ConvertToInvariantString(tls);
286                         Assert.AreEqual(XmlSettingsD, result, "#D");
287                         //
288                         tls = CreateSettingsEDecimalOneItem();
289                         result = c.ConvertToInvariantString(tls);
290                         Assert.AreEqual(XmlSettingsEDecimalOneItem, result, "#DDecimal");
291                         //
292                         tls = CreateSettingsF();
293                         result = c.ConvertToInvariantString(tls);
294                         Assert.AreEqual(XmlSettingsF, result, "#E");
295                 }
296
297                 [Test]
298                 public void ConvertTo_CurrentCultureHasDot_IsInvariantCulture()
299                 {
300                         CultureInfo culture = CultureInfo.InvariantCulture;
301                         ConvertTo_WithCurrentCulture(culture, XmlSettingsEDecimalOneItem);
302                 }
303
304                 [Test]
305                 public void ConvertTo_CurrentCultureHasDot()
306                 {
307                         CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
308                         ConvertTo_WithCurrentCulture(culture, XmlSettingsEDecimalOneItem);
309                 }
310
311                 [Test]
312                 public void ConvertTo_CurrentCultureHasComma()
313                 {
314                         if (!IsMonoRuntime) {
315                                 Assert.Ignore("Remember MSFT still uses the current culture decimal mark!");
316                         }
317                         //
318                         // Changed to always use dot-decimal-point float format
319                         //
320                         // This test is a test of current behaviour, and NOT *desired* behaviour.
321                         // It would likely be preferable for the float values (and everything) 
322                         // to be written using InvariantCulture, e.g. style.Width.ToString (CultureInfo.InvariantCulture)
323
324                         // The bug in MSFT's output mentioned above causes 99.9% to be written
325                         // culture sensitively as "99,9".
326                         // The bug depends on the *current* culture and NOT the culture passed-in 
327                         // to ConvertTo!!!
328                         //
329                         // de-DE or fr-FR would work here too!
330                         CultureInfo culture = CultureInfo.GetCultureInfo("ca-ES");
331                         ConvertTo_WithCurrentCulture(culture, XmlSettingsEDecimalOneItem);
332                         // was: XmlSettingsEDecimalOneItemCommaDecimalPoint
333                         //
334                         // Just check that this culture normal produces "99,9"
335                         Assert.AreEqual("99,9", String.Format(culture, "{0}", 99.9d), "ToString d");
336                         Assert.AreEqual("99,9", String.Format(culture, "{0}", 99.9f), "ToString f");
337                         Assert.AreEqual("99,9", String.Format(culture, "{0}", 99.9m), "ToString m");
338                 }
339
340                 private static void ConvertTo_WithCurrentCulture(CultureInfo culture, String expectedXml)
341                 {
342                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
343
344                         String result;
345                         //
346                         TableLayoutSettings tls = CreateSettingsEDecimalOneItem();
347                         CultureInfo previous = global::System.Threading.Thread.CurrentThread.CurrentCulture;
348                         global::System.Threading.Thread.CurrentThread.CurrentCulture = culture;
349                         try {
350                                 result = c.ConvertToString(null, culture, tls);
351                                 Assert.AreEqual(expectedXml, result, "Culture passed");
352                                 //
353                                 result = c.ConvertToString(null, null, tls);
354                                 Assert.AreEqual(expectedXml, result, "No culture passed");
355                                 //
356                                 result = c.ConvertToInvariantString(tls);
357                                 Assert.AreEqual(expectedXml, result, "Invariant");
358                         } finally {
359                                 global::System.Threading.Thread.CurrentThread.CurrentCulture = previous;
360                         }
361                 }
362
363                 //--------------------------------------------------------------
364                 void Assert_IsInstanceOfType(Type expected, Object value, String message)
365                 {
366                         if (expected == null)
367                                 throw new ArgumentNullException("expected");
368                         //Assert.AreEqual(expected, value == null ? null : value.GetType(), message);
369                         if (!expected.IsInstanceOfType(value))
370                                 throw new Exception("Booooo: " + message + Environment.NewLine
371                                         + "expected: '" + expected + "'" + Environment.NewLine
372                                         + "actual:   '" + (value == null ? "(null)" : value.GetType().ToString()) + "'");
373                 }
374
375                 [Test]
376                 public void ConvertFrom_NOT()
377                 {
378                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
379
380                         try {
381                                 c.ConvertFrom(9999);
382                                 Assert.Fail("should have thrown -- " + "#1");
383                         } catch (Exception ex) {
384                                 Assert_IsInstanceOfType(typeof(NotSupportedException), ex, "ExType -- " + "#1");
385                         }
386                         try {
387                                 c.ConvertFrom(null);
388                                 Assert.Fail("should have thrown -- " + "#2");
389                         } catch (Exception ex) {
390                                 Assert_IsInstanceOfType(typeof(NotSupportedException), ex, "ExType -- " + "#2");
391                         }
392                         try {
393                                 c.ConvertFrom(String.Empty);
394                                 Assert.Fail("should have thrown -- " + "#3");
395                         } catch (Exception ex) {
396                                 Assert_IsInstanceOfType(typeof(global::System.Xml.XmlException), ex, "ExType -- " + "#3");
397                         }
398                 }
399
400                 [Test]
401                 public void ConvertFrom_notCheckResults()
402                 {
403                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
404
405                         c.ConvertFromInvariantString(XmlSettingsB);
406                         c.ConvertFromInvariantString(XmlSettingsC);
407                         c.ConvertFromInvariantString(XmlSettingsD);
408                         c.ConvertFromInvariantString(XmlSettingsEDecimalOneItem);
409                         c.ConvertFromInvariantString(XmlSettingsF);
410                 }
411
412                 [Test]
413                 public void ConvertFrom_notCheckResults_Empty()
414                 {
415                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
416
417                         c.ConvertFromInvariantString(XmlSettingsEmpty);
418                         // Mono fails (reports problem with value in Enum.Parse).
419                         // This is valid content (as output both platforms) so it should work.
420                 }
421
422                 [Test]
423                 public void ConvertFrom_notCheckResults_Bad_EmptyNumericElement()
424                 {
425                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
426
427                         try {
428                                 // It's manually broken content, so it's ok to fail...
429                                 c.ConvertFromInvariantString(XmlSettingsF_EmptyNumericElement);
430                         } catch (IndexOutOfRangeException) {
431                                 // MSFT fails here.  Mono doesn't
432                         }
433                 }
434
435                 [Test]
436                 public void ConvertFrom_notCheckResults_Bad_NoNumericElement()
437                 {
438                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
439
440                         try {
441                                 // It's manually broken content, so it's ok to fail...
442                                 c.ConvertFromInvariantString(XmlSettingsF_NoNumericElement);
443                         } catch (IndexOutOfRangeException) {
444                                 // Both fail here.
445                         }
446                 }
447
448                 [Test]
449                 public void ConvertFrom_notCheckResults_CommaDecimalPoint()
450                 {
451                         // The bug in MSFT's output mentioned above causes 99.9% to be written
452                         // culture sensitively as "99,9".
453                         // Test whether ConvertFrom crashes on reading such bad content.
454                         //
455                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
456
457                         c.ConvertFromInvariantString(XmlSettingsEDecimalOneItemCommaDecimalPoint);
458                 }
459
460                 [Test]
461                 public void ConvertFrom_notCheckResults_CommaDecimalPoint_RowDataFirst()
462                 {
463                         // The bug in MSFT's output mentioned above causes 99.9% to be written
464                         // culture sensitively as "99,9".
465                         // Test whether ConvertFrom crashes on reading such bad content.
466                         //
467                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
468
469                         c.ConvertFromInvariantString(XmlSettingsEDecimalOneItemCommaDecimalPoint_RowDataFirst);
470                 }
471
472                 //------
473                 struct TestRow
474                 {
475                         public String Name;
476                         public TableLayoutSettings Settings;
477                         public String XmlContent;
478                         public Directions Directions;
479
480                         public TestRow(TableLayoutSettings settings, String xmlContent, String name, Directions directions)
481                         {
482                                 this.Name = name;
483                                 this.Settings = settings;
484                                 this.XmlContent = xmlContent;
485                                 this.Directions = directions;
486                         }
487                 }
488                 [Flags]
489                 enum Directions
490                 {
491                         None = 0,
492                         To = 1,
493                         From = 2, 
494                         Both
495                 }
496
497                 IList<TestRow> tests;
498
499                 public TableLayoutSettingsTypeConverterTest()
500                 {
501                         BuildLoopTestCaseList();
502                 }
503
504                 private void  BuildLoopTestCaseList()
505                 {
506                         tests = new List<TestRow>();
507                         //==================
508                         // **very** basic test of equality checking methods...
509                         //-tests.Add(new TestRow(CreateSettingsC(), XmlSettingsB, "XmlSettingsB vs C!!!!", Directions.From));
510                         //==================
511                         //--------
512                         tests.Add(new TestRow(CreateSettingsB(), XmlSettingsB, "XmlSettingsB", Directions.Both));
513                         //--------
514                         tests.Add(new TestRow(CreateSettingsC(), XmlSettingsC, "XmlSettingsC", Directions.Both));
515                         //--------
516                         tests.Add(new TestRow(CreateSettingsD(), XmlSettingsD, "XmlSettingsD", Directions.Both));
517                         //--------
518                         tests.Add(new TestRow(CreateSettingsEDecimalOneItem(), XmlSettingsEDecimalOneItem, 
519                                 "XmlSettingsEDecimalOneItem", 
520                                 Directions.Both));
521                         tests.Add(new TestRow(CreateSettingsEDecimalOneItem(), XmlSettingsEDecimalOneItemCommaDecimalPoint, 
522                                 "XmlSettingsEDecimalOneItem_CommaDecimalPoint", 
523                                 Directions.From));      // 'To' case is the one above.
524                         tests.Add(new TestRow(CreateSettingsEDecimalOneItem(), XmlSettingsEDecimalOneItemCommaDecimalPoint_RowDataFirst, 
525                                 "XmlSettingsEDecimalOneItem_CommaDecimalPoint_RowDataFirst", 
526                                 Directions.From));      // 'To' case is the one above.
527                         //--------
528                         tests.Add(new TestRow(CreateSettingsF(), XmlSettingsF, "XmlSettingsF", Directions.Both));
529                         tests.Add(new TestRow(CreateSettingsF(), XmlSettingsF_EmptyNumericElement, 
530                                 "XmlSettingsF_EmptyNumericElement", 
531                                 Directions.None));      // Fails on both platforms, see individual test methods.
532                         tests.Add(new TestRow(CreateSettingsF(), XmlSettingsF_NoNumericElement, 
533                                 "XmlSettingsF_NoNumericElement",
534                                 Directions.None));      // Fails on both platforms, see individual test methods.
535                         //--------
536                         tests.Add(new TestRow(CreateSettingsGDecimalTwoItems(), XmlSettingsGDecimalTwoItems, 
537                                 "XmlSettingsGDecimalTwoItems", 
538                                 Directions.Both));
539                         tests.Add(new TestRow(CreateSettingsGDecimalTwoItems(), XmlSettingsGDecimalTwoItems_CommaDecimalPoint,
540                                 "XmlSettingsGDecimalTwoItems_CommaDecimalPoint", 
541                                 Directions.From));      // 'To' case is the one above.
542                         tests.Add(new TestRow(CreateSettingsGDecimalTwoItems(), XmlSettingsGDecimalTwoItems_CommaDecimalPoint_RowDataFirst,
543                                 "XmlSettingsGDecimalTwoItems_CommaDecimalPoint_RowDataFirst", 
544                                 Directions.From));      // 'To' case is the one above.
545                         //--------
546                         tests.Add(new TestRow(CreateSettingsEmpty(), XmlSettingsEmpty, 
547                                 "XmlSettingsEmpty", Directions.Both));
548                 }
549
550
551                 [Test]
552                 public void ConvertTo_Loop_InvariantCulture()
553                 {
554                         ConvertTo_Loop(CultureInfo.InvariantCulture);
555                 }
556
557                 [Test]
558                 public void ConvertTo_Loop_CultureWithComma()
559                 {
560                         ConvertTo_Loop(CultureInfo.GetCultureInfo("de-DE"));
561                 }
562
563                 [Test]
564                 public void ConvertTo_Loop_CurrentCultureWithComma_CultureWithComma()
565                 {
566                         if (!IsMonoRuntime) {
567                                 Assert.Ignore("Remember MSFT still uses the current culture decimal mark!");
568                         }
569                         //
570                         CultureInfo previous = global::System.Threading.Thread.CurrentThread.CurrentCulture;
571                         global::System.Threading.Thread.CurrentThread.CurrentCulture
572                                 = CultureInfo.GetCultureInfo("de-DE");
573                         try {
574                                 ConvertTo_Loop(CultureInfo.GetCultureInfo("de-DE"));
575                         } finally {
576                                 global::System.Threading.Thread.CurrentThread.CurrentCulture = previous;
577                         }
578                 }
579
580                 private bool IsMonoRuntime
581                 {
582                         get { return Type.GetType("Mono.Runtime") != null; }
583                 }
584
585
586                 // Ohh for a more recent version on NUnit, (/and addin) to do row test cases!
587                 private void ConvertTo_Loop(CultureInfo culturePassedToConvertTo)
588                 {
589                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
590
591                         int i = 0;
592                         foreach (TestRow row in tests) {
593                                 if ((row.Directions & Directions.To) == 0) {
594                                         //Console.WriteLine("ConvertTo_Loop skipping: " + row.Name);
595                                         continue;
596                                 }
597                                 //
598                                 String title = String.Format("#{0}, {1}", i, row.Name);
599                                 try {
600                                         String result = (String)c.ConvertTo(null, culturePassedToConvertTo, row.Settings, typeof(String));
601                                         Assert.AreEqual(row.XmlContent, result, row.Name);
602                                         title = null;
603                                 } finally {
604                                         if (title != null)
605                                                 Console.WriteLine("ConvertTo_Loop row that failed ** : " + title);
606                                 }
607                                 ++i;
608                         }
609                 }
610
611                 //--------
612                 [Test]
613                 public void ConvertFrom_Loop_InvariantCulture()
614                 {
615                         ConvertFrom_Loop(CultureInfo.InvariantCulture);
616                 }
617
618                 [Test]
619                 public void ConvertFrom_Loop_CultureWithComma()
620                 {
621                         ConvertFrom_Loop(CultureInfo.GetCultureInfo("de-DE"));
622                 }
623                 [Test]
624                 public void ConvertFrom_Loop_CurrentCultureWithComma_CultureWithComma()
625                 {
626                         CultureInfo previous = global::System.Threading.Thread.CurrentThread.CurrentCulture;
627                         global::System.Threading.Thread.CurrentThread.CurrentCulture
628                                 = CultureInfo.GetCultureInfo("de-DE");
629                         try {
630                                 ConvertFrom_Loop(CultureInfo.GetCultureInfo("de-DE"));
631                         } finally {
632                                 global::System.Threading.Thread.CurrentThread.CurrentCulture = previous;
633                         }
634                 }
635                 [Test]
636                 public void ConvertFrom_Loop_CurrentCultureWithComma_InvariantCulture()
637                 {
638                         CultureInfo previous = global::System.Threading.Thread.CurrentThread.CurrentCulture;
639                         global::System.Threading.Thread.CurrentThread.CurrentCulture
640                                 = CultureInfo.GetCultureInfo("de-DE");
641                         try {
642                                 ConvertFrom_Loop(CultureInfo.InvariantCulture);
643                         } finally {
644                                 global::System.Threading.Thread.CurrentThread.CurrentCulture = previous;
645                         }
646                 }
647
648                 // Ohh for a more recent version on NUnit, (/and addin) to do row test cases!
649                 private void ConvertFrom_Loop(CultureInfo culturePassedToConvertFrom)
650                 {
651                         TableLayoutSettingsTypeConverter c = new TableLayoutSettingsTypeConverter();
652
653                         int i = 0;
654                         foreach (TestRow row in tests) {
655                                 if ((row.Directions & Directions.From) == 0) {
656                                         //Console.WriteLine("ConvertFrom_Loop skipping: " + row.Name);
657                                         continue;
658                                 }
659                                 //
660                                 String title = String.Format("#{0}, {1}", i, row.Name);
661                                 try {
662                                         TableLayoutSettings result = (TableLayoutSettings)
663                                                 c.ConvertFrom(null, culturePassedToConvertFrom, row.XmlContent);
664                                         Assert_AreEqual(row.Settings, result, row.Name);
665                                         title = null;
666                                 } finally {
667                                         if (title != null)
668                                                 Console.WriteLine("ConvertFrom_Loop row that failed ** : " + title);
669                                 }
670                                 ++i;
671                         }
672                 }
673
674                 //--------
675                 static void Assert_AreEqual(TableLayoutSettings expected, TableLayoutSettings actual, String message)
676                 {
677                         Assert_AreEqual(expected.ColumnStyles, actual.ColumnStyles, "ColumnStyles -- " + message);
678                         Assert_AreEqual(expected.RowStyles, actual.RowStyles, "RowStyles -- " + message);
679                 }
680
681                 static void Assert_AreEqual(TableLayoutColumnStyleCollection expected, TableLayoutColumnStyleCollection actual, String message)
682                 {
683                         for (int i = 0; i < Math.Min(expected.Count, actual.Count); ++i) {
684                                 ColumnStyle expectedCur = expected[i];
685                                 ColumnStyle actualCur = actual[i];
686                                 Assert_AreEqual(expectedCur, actualCur, "TableLayoutColumnStyleCollection[" + i + "] -- " + message);
687                         }
688                         // Check this *after*, so that if the initial values in the lists don't match 
689                         // that's reported instead -- it makes it more obvious what is being mis-parsed.
690                         Assert.AreEqual(expected.Count, actual.Count, "TableLayoutColumnStyleCollection.Count -- " + message);
691                 }
692                 static void Assert_AreEqual(TableLayoutRowStyleCollection expected, TableLayoutRowStyleCollection actual, String message)
693                 {
694                         for (int i = 0; i < Math.Min(expected.Count, actual.Count); ++i) {
695                                 RowStyle expectedCur = expected[i];
696                                 RowStyle actualCur = actual[i];
697                                 Assert_AreEqual(expectedCur, actualCur, "TableLayoutRowStyleCollection[" + i + "] -- " + message);
698                         }
699                         // Check this *after*, so that if the initial values in the lists don't match 
700                         // that's reported instead -- it makes it more obvious what is being mis-parsed.
701                         Assert.AreEqual(expected.Count, actual.Count, "TableLayoutRowStyleCollection.Count -- " + message);
702                 }
703
704                 static void Assert_AreEqual(ColumnStyle expected, ColumnStyle actual, String message)
705                 {
706                         Assert.AreEqual(expected.SizeType, actual.SizeType, "ColumnStyle.SizeType -- " + message);
707                         Assert.AreEqual(expected.Width, actual.Width, "ColumnStyle.Width -- " + message);
708                 }
709                 static void Assert_AreEqual(RowStyle expected, RowStyle actual, String message)
710                 {
711                         Assert.AreEqual(expected.SizeType, actual.SizeType, "RowStyle.SizeType -- " + message);
712                         Assert.AreEqual(expected.Height, actual.Height, "RowStyle.Height -- " + message);
713                 }
714
715         }
716 }
717