EventInfoTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. using System.Runtime.InteropServices;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Reflection
  33. {
  34. [TestFixture]
  35. public class EventInfoTest
  36. {
  37. [Test]
  38. public void IsDefined_AttributeType_Null ()
  39. {
  40. EventInfo priv = typeof (PrivateEvent).GetEvents (
  41. BindingFlags.Public | BindingFlags.NonPublic |
  42. BindingFlags.Static) [0];
  43. try {
  44. priv.IsDefined ((Type) null, false);
  45. Assert.Fail ("#1");
  46. } catch (ArgumentNullException ex) {
  47. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  48. Assert.IsNull (ex.InnerException, "#3");
  49. Assert.IsNotNull (ex.Message, "#4");
  50. Assert.IsNotNull (ex.ParamName, "#5");
  51. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  52. }
  53. }
  54. [Test]
  55. public void TestGetXXXMethod ()
  56. {
  57. EventInfo priv = typeof (PrivateEvent).GetEvents (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) [0];
  58. Assert.IsNull (priv.GetAddMethod (), "#A1");
  59. Assert.IsNull (priv.GetRaiseMethod (), "#A2");
  60. Assert.IsNull (priv.GetRemoveMethod (), "#A3");
  61. EventInfo pub = typeof (PublicEvent).GetEvents (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) [0];
  62. Assert.IsNotNull (pub.GetAddMethod (), "#B1");
  63. Assert.IsNull (pub.GetRaiseMethod (), "#B2");
  64. Assert.IsNotNull (pub.GetRemoveMethod (), "#B3");
  65. }
  66. [Test]
  67. public void AddHandlerToNullInstanceEventRaisesTargetException ()
  68. {
  69. EventInfo ev = typeof (TestClass).GetEvent ("pub");
  70. EventHandler dele = (a,b) => {};
  71. try {
  72. ev.AddEventHandler (null, dele);
  73. Assert.Fail ("#1");
  74. } catch (TargetException) {}
  75. }
  76. [Test]
  77. public void AddHandleToPrivateEventRaisesInvalidOperationException ()
  78. {
  79. EventInfo ev = typeof (TestClass).GetEvent ("priv", BindingFlags.NonPublic| BindingFlags.Instance);
  80. EventHandler dele = (a,b) => {};
  81. try {
  82. ev.AddEventHandler (new PrivateEvent (), dele);
  83. Assert.Fail ("#1");
  84. } catch (InvalidOperationException) {}
  85. }
  86. [Test]
  87. public void AddHandlerWithIncompatibleTargetShouldRaiseTargetException ()
  88. {
  89. EventInfo ev = typeof (TestClass).GetEvent ("pub");
  90. EventHandler dele = (a,b) => {};
  91. try {
  92. ev.AddEventHandler (new PublicEvent (), dele);
  93. Assert.Fail ("#1");
  94. } catch (TargetException) {}
  95. }
  96. [Test]
  97. public void RemoveHandleToPrivateEventRaisesInvalidOperationException ()
  98. {
  99. EventInfo ev = typeof (TestClass).GetEvent ("priv", BindingFlags.NonPublic| BindingFlags.Instance);
  100. EventHandler dele = (a,b) => {};
  101. try {
  102. ev.RemoveEventHandler (new PrivateEvent (), dele);
  103. Assert.Fail ("#1");
  104. } catch (InvalidOperationException) {}
  105. }
  106. [Test]
  107. public void EventInfoModule ()
  108. {
  109. Type type = typeof (TestClass);
  110. EventInfo ev = type.GetEvent ("pub");
  111. Assert.AreEqual (type.Module, ev.Module);
  112. }
  113. [Test]
  114. public void MetadataToken ()
  115. {
  116. EventInfo ev = typeof (TestClass).GetEvent ("pub");
  117. Assert.IsTrue ((int)ev.MetadataToken > 0);
  118. }
  119. #pragma warning disable 67
  120. public class PrivateEvent
  121. {
  122. private static event EventHandler x;
  123. }
  124. public class PublicEvent
  125. {
  126. public static event EventHandler x;
  127. }
  128. public class TestClass
  129. {
  130. public event EventHandler pub;
  131. private event EventHandler priv;
  132. }
  133. #pragma warning restore 67
  134. }
  135. }