TimerTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. [ExpectedException (typeof (ArgumentException))]
  21. public void IntervalException1 ()
  22. {
  23. Timer timer = new Timer ();
  24. timer.Interval = 0;
  25. }
  26. [Test ()]
  27. [ExpectedException (typeof (ArgumentException), "'-1' is not a valid value for Interval. Interval must be greater than 0.")]
  28. public void IntervalException2 ()
  29. {
  30. Timer timer = new Timer ();
  31. timer.Interval = -1;
  32. }
  33. [Test ()]
  34. public void IntervalException3 ()
  35. {
  36. Timer timer = new Timer ();
  37. timer.Interval = int.MaxValue;
  38. }
  39. [Test ()]
  40. [ExpectedException (typeof (ArgumentException), "'-2147483648' is not a valid value for Interval. Interval must be greater than 0.")]
  41. public void IntervalException4 ()
  42. {
  43. Timer timer = new Timer ();
  44. timer.Interval = int.MinValue;
  45. }
  46. [Test]
  47. public void StartTest ()
  48. {
  49. Ticked = false;
  50. using (Timer timer = new Timer ()) {
  51. timer.Tick += new EventHandler (TickHandler);
  52. timer.Start ();
  53. Sys_Threading.Thread.Sleep (150);
  54. Application.DoEvents ();
  55. Assert.AreEqual (true, timer.Enabled, "1");
  56. Assert.AreEqual (true, Ticked, "2");
  57. }
  58. }
  59. [Test]
  60. public void StopTest ()
  61. {
  62. Ticked = false;
  63. using (Timer timer = new Timer ()) {
  64. timer.Tick += new EventHandler (TickHandler);
  65. timer.Interval = 200;
  66. timer.Start ();
  67. Assert.AreEqual (true, timer.Enabled, "1");
  68. Assert.AreEqual (false, Ticked, "2");
  69. timer.Stop ();
  70. Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
  71. Assert.AreEqual (false, timer.Enabled, "4");
  72. Sys_Threading.Thread.Sleep (500);
  73. Assert.AreEqual (false, Ticked, "5");
  74. }
  75. }
  76. #if NET_2_0
  77. [Test]
  78. public void TagTest ()
  79. {
  80. Timer timer = new Timer ();
  81. timer.Tag = "a";
  82. Assert.AreEqual ("a", timer.Tag, "1");
  83. }
  84. #endif
  85. [Test]
  86. public void EnabledTest ()
  87. {
  88. Ticked = false;
  89. using (Timer timer = new Timer ()) {
  90. timer.Tick += new EventHandler (TickHandler);
  91. timer.Enabled = true;
  92. Sys_Threading.Thread.Sleep (150);
  93. Application.DoEvents ();
  94. Assert.AreEqual (true, timer.Enabled, "1");
  95. Assert.AreEqual (true, Ticked, "2");
  96. }
  97. Ticked = false;
  98. using (Timer timer = new Timer ()) {
  99. timer.Tick += new EventHandler (TickHandler);
  100. timer.Interval = 1000;
  101. timer.Enabled = true;
  102. Assert.AreEqual (true, timer.Enabled, "1");
  103. Assert.AreEqual (false, Ticked, "2");
  104. timer.Enabled = false;
  105. Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
  106. Assert.AreEqual (false, timer.Enabled, "4");
  107. }
  108. }
  109. void TickHandler (object sender, EventArgs e)
  110. {
  111. Ticked = true;
  112. }
  113. [Test]
  114. public void DefaultProperties ()
  115. {
  116. Timer timer = new Timer ();
  117. Assert.AreEqual (null, timer.Container, "C1");
  118. Assert.AreEqual (false, timer.Enabled, "E1");
  119. Assert.AreEqual (100, timer.Interval, "I1");
  120. Assert.AreEqual (null, timer.Site, "S1");
  121. #if NET_2_0
  122. Assert.AreEqual (null, timer.Tag, "T1");
  123. #endif
  124. }
  125. }
  126. }