TestImageIndexConverter.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ([email protected])
  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. using NUnit.Framework;
  34. using System;
  35. using System.ComponentModel;
  36. using System.Windows.Forms;
  37. using System.Globalization;
  38. namespace MonoTests.System.Windows.Forms
  39. {
  40. [TestFixture]
  41. public class ImageIndexConverterTest
  42. {
  43. ToolBarButton button;
  44. PropertyDescriptorCollection pdc;
  45. ImageIndexConverter ic;
  46. public ImageIndexConverterTest ()
  47. {
  48. button = new ToolBarButton ();
  49. pdc = TypeDescriptor.GetProperties (button);
  50. ic = (ImageIndexConverter) pdc.Find ("ImageIndex", true).Converter;
  51. }
  52. [TearDown]
  53. public void TearDown () { }
  54. [SetUp]
  55. public void SetUp () { }
  56. [Test]
  57. public void TestCanConvertFrom ()
  58. {
  59. Assert.IsFalse (ic.CanConvertFrom (typeof (byte)), "CanConvertFromByte must be false.");
  60. Assert.IsFalse (ic.CanConvertFrom (typeof (char)), "CanConvertFromChar must be false.");
  61. Assert.IsFalse (ic.CanConvertFrom (typeof (int)), "CanConvertFromInt must be false.");
  62. Assert.IsFalse (ic.CanConvertFrom (typeof (float)), "CanConvertFromFloat must be false.");
  63. Assert.IsFalse (ic.CanConvertFrom (typeof (long)), "CanConvertFromLong must be false.");
  64. Assert.IsFalse (ic.CanConvertFrom (typeof (double)), "CanConvertFromDouble must be false.");
  65. Assert.IsTrue (ic.CanConvertFrom (typeof (string)), "CanConvertFromString must be true.");
  66. Assert.IsFalse (ic.CanConvertFrom (typeof (object)), "CanConvertFromObject must be false.");
  67. }
  68. [Test]
  69. public void TestCanConvertTo ()
  70. {
  71. Assert.IsTrue (ic.CanConvertTo (typeof (byte)), "CanConvertToByte must be true.");
  72. Assert.IsTrue (ic.CanConvertTo (typeof (char)), "CanConvertToChar must be true.");
  73. Assert.IsTrue (ic.CanConvertTo (typeof (int)), "CanConvertToInt must be true.");
  74. Assert.IsTrue (ic.CanConvertTo (typeof (float)), "CanConvertToFloat must be true.");
  75. Assert.IsTrue (ic.CanConvertTo (typeof (long)), "CanConvertToLong must be true.");
  76. Assert.IsTrue (ic.CanConvertTo (typeof (double)), "CanConvertToDouble must be true.");
  77. Assert.IsTrue (ic.CanConvertTo (typeof (string)), "CanConvertToString must be true.");
  78. Assert.IsFalse (ic.CanConvertTo (typeof (object)), "CanConvertToObject must be false.");
  79. }
  80. [Test]
  81. public void TestConvertFrom ()
  82. {
  83. Assert.AreEqual (-12, (int) ic.ConvertFrom (null, CultureInfo.InvariantCulture, "-12"), "ConvertFromStr -12");
  84. Assert.AreEqual (-1, (int) ic.ConvertFrom (null, CultureInfo.InvariantCulture, "-1"), "ConvertFromStr -1");
  85. Assert.AreEqual (1, (int) ic.ConvertFrom (null, CultureInfo.InvariantCulture, "1"), "ConvertFromStr 1");
  86. try {
  87. ic.ConvertFrom (null, CultureInfo.InvariantCulture, 1.2f);
  88. Assert.Fail ("ConvertFromFloat did not throw exception.");
  89. } catch (Exception e) {
  90. Assert.IsTrue (e is NotSupportedException, "ConvertFromFloat did not throw NotSupportedException.");
  91. }
  92. try {
  93. ic.ConvertFrom (null, CultureInfo.InvariantCulture, 1);
  94. Assert.Fail ("ConvertFromInt did not throw exception.");
  95. } catch (Exception e) {
  96. Assert.IsTrue (e is NotSupportedException, "ConvertFromInt did not throw NotSupportedException.");
  97. }
  98. }
  99. [Test]
  100. public void TestConvertTo ()
  101. {
  102. Assert.AreEqual ("(none)", ic.ConvertTo (null, CultureInfo.InvariantCulture, -1, typeof (string)), "ConvertInt_Minus1_ToStr");
  103. Assert.AreEqual ("0", ic.ConvertTo (null, CultureInfo.InvariantCulture, 0, typeof (string)), "ConvertInt_0_ToStr");
  104. Assert.AreEqual ("1", ic.ConvertTo (null, CultureInfo.InvariantCulture, 1, typeof (string)), "ConvertInt_1_ToStr");
  105. Assert.AreEqual (0, ic.ConvertTo (null, CultureInfo.InvariantCulture, 0, typeof (int)), "ConvertInt_0_ToInt");
  106. Assert.AreEqual ("(none)", ic.ConvertTo (null, CultureInfo.InvariantCulture, "(none)", typeof (string)), "ConvertStr_none_ToStr");
  107. Assert.AreEqual ("-1", ic.ConvertTo (null, CultureInfo.InvariantCulture, "-1", typeof (string)), "ConvertStr_Minus1_ToStr");
  108. Assert.AreEqual (-1, ic.ConvertTo (null, CultureInfo.InvariantCulture, -1, typeof (int)), "ConvertInt_Minus1_ToInt");
  109. Assert.AreEqual (1, ic.ConvertTo (null, CultureInfo.InvariantCulture, 1, typeof (int)), "ConvertInt_1_ToInt");
  110. Assert.AreEqual (-1, ic.ConvertTo (null, CultureInfo.InvariantCulture, "-1", typeof (int)), "ConvertStr_Minus1_ToInt");
  111. Assert.AreEqual (0, ic.ConvertTo (null, CultureInfo.InvariantCulture, "0", typeof (int)), "ConvertStr_0_ToInt");
  112. Assert.AreEqual (1, ic.ConvertTo (null, CultureInfo.InvariantCulture, "1", typeof (int)), "ConvertStr_1_ToInt");
  113. Assert.AreEqual (2, ic.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f, typeof (int)), "ConvertFloat_1_5_ToInt must return 2.");
  114. try {
  115. ic.ConvertTo (null, CultureInfo.InvariantCulture, "-1.5f", typeof (int));
  116. Assert.Fail("ConvertFloatStrToInt must throw exception.");
  117. } catch (Exception e) {
  118. Assert.IsTrue (e is FormatException, "ConvertFloatStrToInt must throw FormatException.");
  119. }
  120. Assert.AreEqual (1.5, ic.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f, typeof (float)), "ConvertFloat_1_5_ToFloat");
  121. }
  122. [Test]
  123. public void TestGetCreateInstanceSupported ()
  124. {
  125. Assert.IsFalse (ic.GetCreateInstanceSupported (), "GetCreateInstance must return false.");
  126. }
  127. [Test]
  128. public void TestGetStandardValuesSupported ()
  129. {
  130. Assert.IsTrue (ic.GetStandardValuesSupported (), "GetStandardValuesSupported must return true.");
  131. }
  132. [Test]
  133. public void TestGetStandardValuesExclusive ()
  134. {
  135. Assert.IsFalse (ic.GetStandardValuesExclusive (), "GetStandardValuesExclusive must return false.");
  136. }
  137. [Test]
  138. public void TestGetStandardValues ()
  139. {
  140. TypeConverter.StandardValuesCollection stdVals = ic.GetStandardValues (null);
  141. Assert.AreEqual (1, stdVals.Count, "StandardValues count must be 1.");
  142. Assert.AreEqual (-1, stdVals [0], "Standard Value count must be -1.");
  143. }
  144. }
  145. }