EventInfoTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // EventInfoTest
  3. //
  4. // Ben Maurer ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Threading;
  29. using System.Reflection;
  30. #if !TARGET_JVM
  31. using System.Reflection.Emit;
  32. #endif // TARGET_JVM
  33. using System.Runtime.InteropServices;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Reflection
  36. {
  37. [TestFixture]
  38. public class EventInfoTest
  39. {
  40. [Test]
  41. public void IsDefined_AttributeType_Null ()
  42. {
  43. EventInfo priv = typeof (PrivateEvent).GetEvents (
  44. BindingFlags.Public | BindingFlags.NonPublic |
  45. BindingFlags.Static) [0];
  46. try {
  47. priv.IsDefined ((Type) null, false);
  48. Assert.Fail ("#1");
  49. } catch (ArgumentNullException ex) {
  50. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  51. Assert.IsNull (ex.InnerException, "#3");
  52. Assert.IsNotNull (ex.Message, "#4");
  53. Assert.IsNotNull (ex.ParamName, "#5");
  54. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  55. }
  56. }
  57. [Test]
  58. public void TestGetXXXMethod ()
  59. {
  60. EventInfo priv = typeof (PrivateEvent).GetEvents (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) [0];
  61. Assert.IsNull (priv.GetAddMethod (), "#A1");
  62. Assert.IsNull (priv.GetRaiseMethod (), "#A2");
  63. Assert.IsNull (priv.GetRemoveMethod (), "#A3");
  64. EventInfo pub = typeof (PublicEvent).GetEvents (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) [0];
  65. Assert.IsNotNull (pub.GetAddMethod (), "#B1");
  66. Assert.IsNull (pub.GetRaiseMethod (), "#B2");
  67. Assert.IsNotNull (pub.GetRemoveMethod (), "#B3");
  68. }
  69. #if NET_2_0
  70. [Test]
  71. public void AddHandlerToNullInstanceEventRaisesTargetException ()
  72. {
  73. EventInfo ev = typeof (TestClass).GetEvent ("pub");
  74. EventHandler dele = (a,b) => {};
  75. try {
  76. ev.AddEventHandler (null, dele);
  77. Assert.Fail ("#1");
  78. } catch (TargetException) {}
  79. }
  80. [Test]
  81. public void AddHandleToPrivateEventRaisesInvalidOperationException ()
  82. {
  83. EventInfo ev = typeof (TestClass).GetEvent ("priv", BindingFlags.NonPublic| BindingFlags.Instance);
  84. EventHandler dele = (a,b) => {};
  85. try {
  86. ev.AddEventHandler (new PrivateEvent (), dele);
  87. Assert.Fail ("#1");
  88. } catch (InvalidOperationException) {}
  89. }
  90. [Test]
  91. public void AddHandlerWithIncompatibleTargetShouldRaiseTargetException ()
  92. {
  93. EventInfo ev = typeof (TestClass).GetEvent ("pub");
  94. EventHandler dele = (a,b) => {};
  95. try {
  96. ev.AddEventHandler (new PublicEvent (), dele);
  97. Assert.Fail ("#1");
  98. } catch (TargetException) {}
  99. }
  100. [Test]
  101. public void RemoveHandleToPrivateEventRaisesInvalidOperationException ()
  102. {
  103. EventInfo ev = typeof (TestClass).GetEvent ("priv", BindingFlags.NonPublic| BindingFlags.Instance);
  104. EventHandler dele = (a,b) => {};
  105. try {
  106. ev.RemoveEventHandler (new PrivateEvent (), dele);
  107. Assert.Fail ("#1");
  108. } catch (InvalidOperationException) {}
  109. }
  110. #endif
  111. #pragma warning disable 67
  112. public class PrivateEvent
  113. {
  114. private static event EventHandler x;
  115. }
  116. public class PublicEvent
  117. {
  118. public static event EventHandler x;
  119. }
  120. public class TestClass
  121. {
  122. public event EventHandler pub;
  123. private event EventHandler priv;
  124. }
  125. #pragma warning restore 67
  126. }
  127. }