2 // Copyright (c) 2007 Novell, Inc.
\r
5 // Rolf Bjarne Kvinge (RKvinge@novell.com)
\r
9 using System.Drawing;
\r
10 using System.Reflection;
\r
11 using System.Windows.Forms;
\r
12 using System.Threading;
\r
13 using Timer = System.Windows.Forms.Timer;
\r
14 using Sys_Threading=System.Threading;
\r
16 using NUnit.Framework;
\r
18 namespace MonoTests.System.Windows.Forms
\r
21 public class TimerTest : TestHelper
\r
26 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
27 public void IntervalException1 ()
\r
29 Timer timer = new Timer ();
\r
34 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
35 public void IntervalException2 ()
\r
37 Timer timer = new Timer ();
\r
38 timer.Interval = -1;
\r
42 public void IntervalException3 ()
\r
44 Timer timer = new Timer ();
\r
45 timer.Interval = int.MaxValue;
\r
49 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
50 public void IntervalException4 ()
\r
52 Timer timer = new Timer ();
\r
53 timer.Interval = int.MinValue;
\r
57 [Category ("NotWorking")]
\r
58 public void StartTest ()
\r
60 // This test fails about 50% of the time on the buildbots.
\r
62 using (Timer timer = new Timer ()) {
\r
63 timer.Tick += new EventHandler (TickHandler);
\r
65 Sys_Threading.Thread.Sleep (500);
\r
66 Application.DoEvents ();
\r
67 Assert.AreEqual (true, timer.Enabled, "1");
\r
68 Assert.AreEqual (true, Ticked, "2");
\r
73 public void StopTest ()
\r
76 using (Timer timer = new Timer ()) {
\r
77 timer.Tick += new EventHandler (TickHandler);
\r
78 timer.Interval = 200;
\r
80 Assert.AreEqual (true, timer.Enabled, "1");
\r
81 Assert.AreEqual (false, Ticked, "2");
\r
83 Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
\r
84 Assert.AreEqual (false, timer.Enabled, "4");
\r
85 Sys_Threading.Thread.Sleep (500);
\r
86 Assert.AreEqual (false, Ticked, "5");
\r
91 public void TagTest ()
\r
93 Timer timer = new Timer ();
\r
95 Assert.AreEqual ("a", timer.Tag, "1");
\r
98 /* Application.DoEvents and Sleep are not guarenteed on Linux
\r
100 public void EnabledTest ()
\r
103 using (Timer timer = new Timer ()) {
\r
104 timer.Tick += new EventHandler (TickHandler);
\r
105 timer.Enabled = true;
\r
106 Sys_Threading.Thread.Sleep (150);
\r
107 Application.DoEvents ();
\r
108 Assert.AreEqual (true, timer.Enabled, "1");
\r
109 Assert.AreEqual (true, Ticked, "2");
\r
113 using (Timer timer = new Timer ()) {
\r
114 timer.Tick += new EventHandler (TickHandler);
\r
115 timer.Interval = 1000;
\r
116 timer.Enabled = true;
\r
117 Assert.AreEqual (true, timer.Enabled, "3");
\r
118 Assert.AreEqual (false, Ticked, "4");
\r
119 timer.Enabled = false;
\r
120 Assert.AreEqual (false, Ticked, "5"); // This may fail if we are running on a very slow machine...
\r
121 Assert.AreEqual (false, timer.Enabled, "6");
\r
126 void TickHandler (object sender, EventArgs e)
\r
132 public void DefaultProperties ()
\r
134 Timer timer = new Timer ();
\r
135 Assert.AreEqual (null, timer.Container, "C1");
\r
136 Assert.AreEqual (false, timer.Enabled, "E1");
\r
137 Assert.AreEqual (100, timer.Interval, "I1");
\r
138 Assert.AreEqual (null, timer.Site, "S1");
\r
139 Assert.AreEqual (null, timer.Tag, "T1");
\r
142 [Test] // bug #325033
\r
143 public void RunningThread ()
\r
145 Application.Run (new Bug325033Form ());
\r
146 Application.Run (new Bug325033Form2 ());
\r
149 class Bug325033Form : Form
\r
151 public Bug325033Form ()
\r
153 Load += new EventHandler (Form_Load);
\r
156 void Form_Load (object sender, EventArgs e)
\r
158 Thread t = new Thread (new ThreadStart (Run));
\r
159 t.IsBackground = true;
\r
167 Application.Run (new Bug325033Form2 ());
\r
171 class Bug325033Form2 : Form
\r
173 public Bug325033Form2 ()
\r
175 _label = new Label ();
\r
176 _label.AutoSize = true;
\r
177 _label.Dock = DockStyle.Fill;
\r
178 _label.Text = "It should close automatically.";
\r
179 Controls.Add (_label);
\r
180 _timer = new Timer ();
\r
181 _timer.Tick += new EventHandler (Timer_Tick);
\r
182 _timer.Interval = 500;
\r
186 void Timer_Tick (object sender, EventArgs e)
\r
192 private Label _label;
\r
193 private Timer _timer;
\r