TestImageIndexConverter.cs 6.8 KB

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