copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / TestImageIndexConverter.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) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Ravindra (rkumar@novell.com)
24 //
25 // $Revision: 1.1 $
26 // $Modtime: $
27 // $Log: TestImageIndexConverter.cs,v $
28 // Revision 1.1  2004/08/27 22:17:37  ravindra
29 // Adding test for ImageIndexConverter.cs
30 //
31 //
32 //
33
34 using NUnit.Framework;
35 using System;
36 using System.ComponentModel;
37 using System.Windows.Forms;
38 using System.Globalization;
39
40 namespace MonoTests.System.Windows.Forms
41 {
42         [TestFixture]   
43         public class ImageIndexConverterTest
44         {
45                 ToolBarButton button;
46                 PropertyDescriptorCollection pdc;
47                 ImageIndexConverter ic;
48
49                 public ImageIndexConverterTest ()
50                 {
51                         button = new ToolBarButton ();
52                         pdc = TypeDescriptor.GetProperties (button);
53                         ic = (ImageIndexConverter) pdc.Find ("ImageIndex", true).Converter;
54                 }
55
56                 [TearDown]
57                 public void TearDown () { }
58
59                 [SetUp]
60                 public void SetUp () { }
61
62                 [Test]
63                 public void TestCanConvertFrom ()
64                 {
65                         Assert.IsFalse (ic.CanConvertFrom (typeof (byte)), "CanConvertFromByte must be false.");
66                         Assert.IsFalse (ic.CanConvertFrom (typeof (char)), "CanConvertFromChar must be false.");
67                         Assert.IsFalse (ic.CanConvertFrom (typeof (int)), "CanConvertFromInt must be false.");
68                         Assert.IsFalse (ic.CanConvertFrom (typeof (float)), "CanConvertFromFloat must be false.");
69                         Assert.IsFalse (ic.CanConvertFrom (typeof (long)), "CanConvertFromLong must be false.");
70                         Assert.IsFalse (ic.CanConvertFrom (typeof (double)), "CanConvertFromDouble must be false.");
71                         Assert.IsTrue (ic.CanConvertFrom (typeof (string)), "CanConvertFromString must be true.");
72                         Assert.IsFalse (ic.CanConvertFrom (typeof (object)), "CanConvertFromObject must be false.");
73                 }
74
75                 [Test]
76                 public void TestCanConvertTo ()
77                 {
78                         Assert.IsTrue (ic.CanConvertTo (typeof (byte)), "CanConvertToByte must be true.");
79                         Assert.IsTrue (ic.CanConvertTo (typeof (char)), "CanConvertToChar must be true.");
80                         Assert.IsTrue (ic.CanConvertTo (typeof (int)), "CanConvertToInt must be true.");
81                         Assert.IsTrue (ic.CanConvertTo (typeof (float)), "CanConvertToFloat must be true.");
82                         Assert.IsTrue (ic.CanConvertTo (typeof (long)), "CanConvertToLong must be true.");
83                         Assert.IsTrue (ic.CanConvertTo (typeof (double)), "CanConvertToDouble must be true.");
84                         Assert.IsTrue (ic.CanConvertTo (typeof (string)), "CanConvertToString must be true.");
85                         Assert.IsFalse (ic.CanConvertTo (typeof (object)), "CanConvertToObject must be false.");
86                 }
87
88                 [Test]
89                 public void TestConvertFrom ()
90                 {
91                         Assert.AreEqual (-12, (int) ic.ConvertFrom (null, CultureInfo.InvariantCulture, "-12"), "ConvertFromStr -12");
92                         Assert.AreEqual (-1, (int) ic.ConvertFrom (null, CultureInfo.InvariantCulture, "-1"), "ConvertFromStr -1");
93                         Assert.AreEqual (1, (int) ic.ConvertFrom (null, CultureInfo.InvariantCulture, "1"), "ConvertFromStr 1");
94
95                         try {
96                                 ic.ConvertFrom (null, CultureInfo.InvariantCulture, 1.2f);
97                                 Assert.Fail ("ConvertFromFloat did not throw exception.");
98                         } catch (Exception e) {
99                                 Assert.IsTrue (e is NotSupportedException, "ConvertFromFloat did not throw NotSupportedException.");
100                         }
101
102                         try {
103                                 ic.ConvertFrom (null, CultureInfo.InvariantCulture, 1);
104                                 Assert.Fail ("ConvertFromInt did not throw exception.");
105                         } catch (Exception e) {
106                                 Assert.IsTrue (e is NotSupportedException, "ConvertFromInt did not throw NotSupportedException.");
107                         }
108                 }
109
110                 [Test]
111                 public void TestConvertTo ()
112                 {
113                         Assert.AreEqual ("(none)", ic.ConvertTo (null, CultureInfo.InvariantCulture, -1, typeof (string)), "ConvertInt_Minus1_ToStr");
114                         Assert.AreEqual ("0", ic.ConvertTo (null, CultureInfo.InvariantCulture, 0, typeof (string)), "ConvertInt_0_ToStr");
115                         Assert.AreEqual ("1", ic.ConvertTo (null, CultureInfo.InvariantCulture, 1, typeof (string)), "ConvertInt_1_ToStr");
116                         Assert.AreEqual (0, ic.ConvertTo (null, CultureInfo.InvariantCulture, 0, typeof (int)), "ConvertInt_0_ToInt");
117                         Assert.AreEqual ("(none)", ic.ConvertTo (null, CultureInfo.InvariantCulture, "(none)", typeof (string)), "ConvertStr_none_ToStr");
118                         Assert.AreEqual ("-1", ic.ConvertTo (null, CultureInfo.InvariantCulture, "-1", typeof (string)), "ConvertStr_Minus1_ToStr");
119                         Assert.AreEqual (-1, ic.ConvertTo (null, CultureInfo.InvariantCulture, -1, typeof (int)), "ConvertInt_Minus1_ToInt");
120                         Assert.AreEqual (1, ic.ConvertTo (null, CultureInfo.InvariantCulture, 1, typeof (int)), "ConvertInt_1_ToInt");
121                         Assert.AreEqual (-1, ic.ConvertTo (null, CultureInfo.InvariantCulture, "-1", typeof (int)), "ConvertStr_Minus1_ToInt");
122                         Assert.AreEqual (0, ic.ConvertTo (null, CultureInfo.InvariantCulture, "0", typeof (int)), "ConvertStr_0_ToInt");
123                         Assert.AreEqual (1, ic.ConvertTo (null, CultureInfo.InvariantCulture, "1", typeof (int)), "ConvertStr_1_ToInt");
124
125                         Assert.AreEqual (2, ic.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f, typeof (int)), "ConvertFloat_1_5_ToInt must return 2.");
126
127                         try {
128                                 ic.ConvertTo (null, CultureInfo.InvariantCulture, "-1.5f", typeof (int));
129                                 Assert.Fail("ConvertFloatStrToInt must throw exception.");
130                         } catch (Exception e) {
131                                 Assert.IsTrue (e is FormatException, "ConvertFloatStrToInt must throw FormatException.");
132                         }
133
134                         Assert.AreEqual (1.5, ic.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f, typeof (float)), "ConvertFloat_1_5_ToFloat");
135                 }
136
137                 [Test]
138                 public void TestGetCreateInstanceSupported ()
139                 {
140                         Assert.IsFalse (ic.GetCreateInstanceSupported (), "GetCreateInstance must return false.");
141                 }
142
143                 [Test]
144                 public void TestGetStandardValuesSupported ()
145                 {
146                         Assert.IsTrue (ic.GetStandardValuesSupported (), "GetStandardValuesSupported must return true.");
147                 }
148
149                 [Test]
150                 public void TestGetStandardValuesExclusive ()
151                 {
152                         Assert.IsFalse (ic.GetStandardValuesExclusive (), "GetStandardValuesExclusive must return false.");
153                 }
154
155                 [Test]
156                 public void TestGetStandardValues ()
157                 {
158                         TypeConverter.StandardValuesCollection stdVals = ic.GetStandardValues (null);
159                         Assert.AreEqual (1, stdVals.Count, "StandardValues count must be 1.");
160                         Assert.AreEqual (-1, stdVals [0], "Standard Value count must be -1.");
161                 }
162         }
163 }