TimerTest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // Copyright (c) 2007 Novell, Inc.
  3. //
  4. // Authors:
  5. // Rolf Bjarne Kvinge ([email protected])
  6. //
  7. using System;
  8. using System.Drawing;
  9. using System.Reflection;
  10. using System.Windows.Forms;
  11. using Sys_Threading=System.Threading;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Windows.Forms
  14. {
  15. [TestFixture]
  16. public class TimerTest
  17. {
  18. bool Ticked;
  19. [Test ()]
  20. #if NET_2_0
  21. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  22. #else
  23. [ExpectedException (typeof (ArgumentException))]
  24. #endif
  25. public void IntervalException1 ()
  26. {
  27. Timer timer = new Timer ();
  28. timer.Interval = 0;
  29. }
  30. [Test ()]
  31. #if NET_2_0
  32. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  33. #else
  34. [ExpectedException (typeof (ArgumentException), "'-1' is not a valid value for Interval. Interval must be greater than 0.")]
  35. #endif
  36. public void IntervalException2 ()
  37. {
  38. Timer timer = new Timer ();
  39. timer.Interval = -1;
  40. }
  41. [Test ()]
  42. public void IntervalException3 ()
  43. {
  44. Timer timer = new Timer ();
  45. timer.Interval = int.MaxValue;
  46. }
  47. [Test ()]
  48. #if NET_2_0
  49. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  50. #else
  51. [ExpectedException (typeof (ArgumentException), "'-2147483648' is not a valid value for Interval. Interval must be greater than 0.")]
  52. #endif
  53. public void IntervalException4 ()
  54. {
  55. Timer timer = new Timer ();
  56. timer.Interval = int.MinValue;
  57. }
  58. [Test]
  59. public void StartTest ()
  60. {
  61. Ticked = false;
  62. using (Timer timer = new Timer ()) {
  63. timer.Tick += new EventHandler (TickHandler);
  64. timer.Start ();
  65. Sys_Threading.Thread.Sleep (150);
  66. Application.DoEvents ();
  67. Assert.AreEqual (true, timer.Enabled, "1");
  68. Assert.AreEqual (true, Ticked, "2");
  69. }
  70. }
  71. [Test]
  72. public void StopTest ()
  73. {
  74. Ticked = false;
  75. using (Timer timer = new Timer ()) {
  76. timer.Tick += new EventHandler (TickHandler);
  77. timer.Interval = 200;
  78. timer.Start ();
  79. Assert.AreEqual (true, timer.Enabled, "1");
  80. Assert.AreEqual (false, Ticked, "2");
  81. timer.Stop ();
  82. Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
  83. Assert.AreEqual (false, timer.Enabled, "4");
  84. Sys_Threading.Thread.Sleep (500);
  85. Assert.AreEqual (false, Ticked, "5");
  86. }
  87. }
  88. #if NET_2_0
  89. [Test]
  90. public void TagTest ()
  91. {
  92. Timer timer = new Timer ();
  93. timer.Tag = "a";
  94. Assert.AreEqual ("a", timer.Tag, "1");
  95. }
  96. #endif
  97. [Test]
  98. public void EnabledTest ()
  99. {
  100. Ticked = false;
  101. using (Timer timer = new Timer ()) {
  102. timer.Tick += new EventHandler (TickHandler);
  103. timer.Enabled = true;
  104. Sys_Threading.Thread.Sleep (150);
  105. Application.DoEvents ();
  106. Assert.AreEqual (true, timer.Enabled, "1");
  107. Assert.AreEqual (true, Ticked, "2");
  108. }
  109. Ticked = false;
  110. using (Timer timer = new Timer ()) {
  111. timer.Tick += new EventHandler (TickHandler);
  112. timer.Interval = 1000;
  113. timer.Enabled = true;
  114. Assert.AreEqual (true, timer.Enabled, "1");
  115. Assert.AreEqual (false, Ticked, "2");
  116. timer.Enabled = false;
  117. Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
  118. Assert.AreEqual (false, timer.Enabled, "4");
  119. }
  120. }
  121. void TickHandler (object sender, EventArgs e)
  122. {
  123. Ticked = true;
  124. }
  125. [Test]
  126. public void DefaultProperties ()
  127. {
  128. Timer timer = new Timer ();
  129. Assert.AreEqual (null, timer.Container, "C1");
  130. Assert.AreEqual (false, timer.Enabled, "E1");
  131. Assert.AreEqual (100, timer.Interval, "I1");
  132. Assert.AreEqual (null, timer.Site, "S1");
  133. #if NET_2_0
  134. Assert.AreEqual (null, timer.Tag, "T1");
  135. #endif
  136. }
  137. }
  138. }