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
\r
27 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
29 [ExpectedException (typeof (ArgumentException))]
\r
31 public void IntervalException1 ()
\r
33 Timer timer = new Timer ();
\r
39 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
41 [ExpectedException (typeof (ArgumentException), "'-1' is not a valid value for Interval. Interval must be greater than 0.")]
\r
43 public void IntervalException2 ()
\r
45 Timer timer = new Timer ();
\r
46 timer.Interval = -1;
\r
50 public void IntervalException3 ()
\r
52 Timer timer = new Timer ();
\r
53 timer.Interval = int.MaxValue;
\r
58 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
60 [ExpectedException (typeof (ArgumentException), "'-2147483648' is not a valid value for Interval. Interval must be greater than 0.")]
\r
62 public void IntervalException4 ()
\r
64 Timer timer = new Timer ();
\r
65 timer.Interval = int.MinValue;
\r
69 [Category ("NotWorking")]
\r
70 public void StartTest ()
\r
72 // This test fails about 50% of the time on the buildbots.
\r
74 using (Timer timer = new Timer ()) {
\r
75 timer.Tick += new EventHandler (TickHandler);
\r
77 Sys_Threading.Thread.Sleep (500);
\r
78 Application.DoEvents ();
\r
79 Assert.AreEqual (true, timer.Enabled, "1");
\r
80 Assert.AreEqual (true, Ticked, "2");
\r
85 public void StopTest ()
\r
88 using (Timer timer = new Timer ()) {
\r
89 timer.Tick += new EventHandler (TickHandler);
\r
90 timer.Interval = 200;
\r
92 Assert.AreEqual (true, timer.Enabled, "1");
\r
93 Assert.AreEqual (false, Ticked, "2");
\r
95 Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
\r
96 Assert.AreEqual (false, timer.Enabled, "4");
\r
97 Sys_Threading.Thread.Sleep (500);
\r
98 Assert.AreEqual (false, Ticked, "5");
\r
104 public void TagTest ()
\r
106 Timer timer = new Timer ();
\r
108 Assert.AreEqual ("a", timer.Tag, "1");
\r
112 /* Application.DoEvents and Sleep are not guarenteed on Linux
\r
114 public void EnabledTest ()
\r
117 using (Timer timer = new Timer ()) {
\r
118 timer.Tick += new EventHandler (TickHandler);
\r
119 timer.Enabled = true;
\r
120 Sys_Threading.Thread.Sleep (150);
\r
121 Application.DoEvents ();
\r
122 Assert.AreEqual (true, timer.Enabled, "1");
\r
123 Assert.AreEqual (true, Ticked, "2");
\r
127 using (Timer timer = new Timer ()) {
\r
128 timer.Tick += new EventHandler (TickHandler);
\r
129 timer.Interval = 1000;
\r
130 timer.Enabled = true;
\r
131 Assert.AreEqual (true, timer.Enabled, "3");
\r
132 Assert.AreEqual (false, Ticked, "4");
\r
133 timer.Enabled = false;
\r
134 Assert.AreEqual (false, Ticked, "5"); // This may fail if we are running on a very slow machine...
\r
135 Assert.AreEqual (false, timer.Enabled, "6");
\r
140 void TickHandler (object sender, EventArgs e)
\r
146 public void DefaultProperties ()
\r
148 Timer timer = new Timer ();
\r
149 Assert.AreEqual (null, timer.Container, "C1");
\r
150 Assert.AreEqual (false, timer.Enabled, "E1");
\r
151 Assert.AreEqual (100, timer.Interval, "I1");
\r
152 Assert.AreEqual (null, timer.Site, "S1");
\r
154 Assert.AreEqual (null, timer.Tag, "T1");
\r
158 [Test] // bug #325033
\r
159 [Category ("NotWorking")]
\r
160 public void RunningThread ()
\r
162 Application.Run (new Bug325033Form ());
\r
163 Application.Run (new Bug325033Form2 ());
\r
166 class Bug325033Form : Form
\r
168 public Bug325033Form ()
\r
170 Load += new EventHandler (Form_Load);
\r
173 void Form_Load (object sender, EventArgs e)
\r
175 Thread t = new Thread (new ThreadStart (Run));
\r
176 t.IsBackground = true;
\r
184 Application.Run (new Bug325033Form2 ());
\r
188 class Bug325033Form2 : Form
\r
190 public Bug325033Form2 ()
\r
192 _label = new Label ();
\r
193 _label.AutoSize = true;
\r
194 _label.Dock = DockStyle.Fill;
\r
195 _label.Text = "It should close automatically.";
\r
196 Controls.Add (_label);
\r
197 _timer = new Timer ();
\r
198 _timer.Tick += new EventHandler (Timer_Tick);
\r
199 _timer.Interval = 500;
\r
203 void Timer_Tick (object sender, EventArgs e)
\r
209 private Label _label;
\r
210 private Timer _timer;
\r