TimerTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 System.Threading;
  12. using Timer = System.Windows.Forms.Timer;
  13. using Sys_Threading=System.Threading;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Windows.Forms
  16. {
  17. [TestFixture]
  18. public class TimerTest : TestHelper
  19. {
  20. bool Ticked;
  21. [Test ()]
  22. #if NET_2_0
  23. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  24. #else
  25. [ExpectedException (typeof (ArgumentException))]
  26. #endif
  27. public void IntervalException1 ()
  28. {
  29. Timer timer = new Timer ();
  30. timer.Interval = 0;
  31. }
  32. [Test ()]
  33. #if NET_2_0
  34. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  35. #else
  36. [ExpectedException (typeof (ArgumentException), "'-1' is not a valid value for Interval. Interval must be greater than 0.")]
  37. #endif
  38. public void IntervalException2 ()
  39. {
  40. Timer timer = new Timer ();
  41. timer.Interval = -1;
  42. }
  43. [Test ()]
  44. public void IntervalException3 ()
  45. {
  46. Timer timer = new Timer ();
  47. timer.Interval = int.MaxValue;
  48. }
  49. [Test ()]
  50. #if NET_2_0
  51. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  52. #else
  53. [ExpectedException (typeof (ArgumentException), "'-2147483648' is not a valid value for Interval. Interval must be greater than 0.")]
  54. #endif
  55. public void IntervalException4 ()
  56. {
  57. Timer timer = new Timer ();
  58. timer.Interval = int.MinValue;
  59. }
  60. [Test]
  61. [Category ("NotWorking")]
  62. public void StartTest ()
  63. {
  64. // This test fails about 50% of the time on the buildbots.
  65. Ticked = false;
  66. using (Timer timer = new Timer ()) {
  67. timer.Tick += new EventHandler (TickHandler);
  68. timer.Start ();
  69. Sys_Threading.Thread.Sleep (500);
  70. Application.DoEvents ();
  71. Assert.AreEqual (true, timer.Enabled, "1");
  72. Assert.AreEqual (true, Ticked, "2");
  73. }
  74. }
  75. [Test]
  76. public void StopTest ()
  77. {
  78. Ticked = false;
  79. using (Timer timer = new Timer ()) {
  80. timer.Tick += new EventHandler (TickHandler);
  81. timer.Interval = 200;
  82. timer.Start ();
  83. Assert.AreEqual (true, timer.Enabled, "1");
  84. Assert.AreEqual (false, Ticked, "2");
  85. timer.Stop ();
  86. Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
  87. Assert.AreEqual (false, timer.Enabled, "4");
  88. Sys_Threading.Thread.Sleep (500);
  89. Assert.AreEqual (false, Ticked, "5");
  90. }
  91. }
  92. #if NET_2_0
  93. [Test]
  94. public void TagTest ()
  95. {
  96. Timer timer = new Timer ();
  97. timer.Tag = "a";
  98. Assert.AreEqual ("a", timer.Tag, "1");
  99. }
  100. #endif
  101. /* Application.DoEvents and Sleep are not guarenteed on Linux
  102. [Test]
  103. public void EnabledTest ()
  104. {
  105. Ticked = false;
  106. using (Timer timer = new Timer ()) {
  107. timer.Tick += new EventHandler (TickHandler);
  108. timer.Enabled = true;
  109. Sys_Threading.Thread.Sleep (150);
  110. Application.DoEvents ();
  111. Assert.AreEqual (true, timer.Enabled, "1");
  112. Assert.AreEqual (true, Ticked, "2");
  113. }
  114. Ticked = false;
  115. using (Timer timer = new Timer ()) {
  116. timer.Tick += new EventHandler (TickHandler);
  117. timer.Interval = 1000;
  118. timer.Enabled = true;
  119. Assert.AreEqual (true, timer.Enabled, "3");
  120. Assert.AreEqual (false, Ticked, "4");
  121. timer.Enabled = false;
  122. Assert.AreEqual (false, Ticked, "5"); // This may fail if we are running on a very slow machine...
  123. Assert.AreEqual (false, timer.Enabled, "6");
  124. }
  125. }
  126. */
  127. void TickHandler (object sender, EventArgs e)
  128. {
  129. Ticked = true;
  130. }
  131. [Test]
  132. public void DefaultProperties ()
  133. {
  134. Timer timer = new Timer ();
  135. Assert.AreEqual (null, timer.Container, "C1");
  136. Assert.AreEqual (false, timer.Enabled, "E1");
  137. Assert.AreEqual (100, timer.Interval, "I1");
  138. Assert.AreEqual (null, timer.Site, "S1");
  139. #if NET_2_0
  140. Assert.AreEqual (null, timer.Tag, "T1");
  141. #endif
  142. }
  143. [Test] // bug #325033
  144. public void RunningThread ()
  145. {
  146. Application.Run (new Bug325033Form ());
  147. Application.Run (new Bug325033Form2 ());
  148. }
  149. class Bug325033Form : Form
  150. {
  151. public Bug325033Form ()
  152. {
  153. Load += new EventHandler (Form_Load);
  154. }
  155. void Form_Load (object sender, EventArgs e)
  156. {
  157. Thread t = new Thread (new ThreadStart (Run));
  158. t.IsBackground = true;
  159. t.Start ();
  160. t.Join ();
  161. Close ();
  162. }
  163. void Run ()
  164. {
  165. Application.Run (new Bug325033Form2 ());
  166. }
  167. }
  168. class Bug325033Form2 : Form
  169. {
  170. public Bug325033Form2 ()
  171. {
  172. _label = new Label ();
  173. _label.AutoSize = true;
  174. _label.Dock = DockStyle.Fill;
  175. _label.Text = "It should close automatically.";
  176. Controls.Add (_label);
  177. _timer = new Timer ();
  178. _timer.Tick += new EventHandler (Timer_Tick);
  179. _timer.Interval = 500;
  180. _timer.Start ();
  181. }
  182. void Timer_Tick (object sender, EventArgs e)
  183. {
  184. _timer.Stop ();
  185. Close ();
  186. }
  187. private Label _label;
  188. private Timer _timer;
  189. }
  190. }
  191. }