2008-06-02 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / TimerTest.cs
1 //\r
2 // Copyright (c) 2007 Novell, Inc.\r
3 //\r
4 // Authors:\r
5 //      Rolf Bjarne Kvinge  (RKvinge@novell.com)\r
6 //\r
7 \r
8 using System;\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
15 \r
16 using NUnit.Framework;\r
17 \r
18 namespace MonoTests.System.Windows.Forms\r
19 {\r
20         [TestFixture]\r
21         public class TimerTest\r
22         {\r
23                 bool Ticked;\r
24                 \r
25                 [Test ()]\r
26 #if NET_2_0\r
27                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
28 #else\r
29                 [ExpectedException (typeof (ArgumentException))]\r
30 #endif\r
31                 public void IntervalException1 ()\r
32                 {\r
33                         Timer timer = new Timer ();\r
34                         timer.Interval = 0;\r
35                 }\r
36 \r
37                 [Test ()]\r
38 #if NET_2_0\r
39                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
40 #else\r
41                 [ExpectedException (typeof (ArgumentException), "'-1' is not a valid value for Interval. Interval must be greater than 0.")]\r
42 #endif\r
43                 public void IntervalException2 ()\r
44                 {\r
45                         Timer timer = new Timer ();\r
46                         timer.Interval = -1;\r
47                 }\r
48 \r
49                 [Test ()]\r
50                 public void IntervalException3 ()\r
51                 {\r
52                         Timer timer = new Timer ();\r
53                         timer.Interval = int.MaxValue;\r
54                 }\r
55 \r
56                 [Test ()]\r
57 #if NET_2_0\r
58                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
59 #else\r
60                 [ExpectedException (typeof (ArgumentException), "'-2147483648' is not a valid value for Interval. Interval must be greater than 0.")]\r
61 #endif\r
62                 public void IntervalException4 ()\r
63                 {\r
64                         Timer timer = new Timer ();\r
65                         timer.Interval = int.MinValue;\r
66                 }\r
67                 \r
68                 [Test]\r
69                 [Category ("NotWorking")]\r
70                 public void StartTest ()\r
71                 {\r
72                         // This test fails about 50% of the time on the buildbots.\r
73                         Ticked = false;\r
74                         using (Timer timer = new Timer ()) {\r
75                                 timer.Tick += new EventHandler (TickHandler);\r
76                                 timer.Start ();\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
81                         }\r
82                 }\r
83 \r
84                 [Test]\r
85                 public void StopTest ()\r
86                 {\r
87                         Ticked = false;\r
88                         using (Timer timer = new Timer ()) {\r
89                                 timer.Tick += new EventHandler (TickHandler);\r
90                                 timer.Interval = 200;\r
91                                 timer.Start ();\r
92                                 Assert.AreEqual (true, timer.Enabled, "1");\r
93                                 Assert.AreEqual (false, Ticked, "2");\r
94                                 timer.Stop ();\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
99                         }\r
100                 }\r
101                 \r
102 #if NET_2_0\r
103                 [Test]\r
104                 public void TagTest ()\r
105                 {\r
106                         Timer timer = new Timer ();\r
107                         timer.Tag = "a";\r
108                         Assert.AreEqual ("a", timer.Tag, "1");\r
109                 }\r
110 #endif\r
111 \r
112                 /* Application.DoEvents and Sleep are not guarenteed on Linux\r
113                 [Test]\r
114                 public void EnabledTest ()\r
115                 {\r
116                         Ticked = false;\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
124                         }\r
125                         \r
126                         Ticked = false;\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
136                         }\r
137                 }\r
138                 */\r
139 \r
140                 void TickHandler (object sender, EventArgs e)\r
141                 {\r
142                         Ticked = true;\r
143                 }\r
144                 \r
145                 [Test]\r
146                 public void DefaultProperties ()\r
147                 {\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
153 #if NET_2_0\r
154                         Assert.AreEqual (null, timer.Tag, "T1");\r
155 #endif\r
156                 }\r
157 \r
158                 [Test] // bug #325033\r
159                 [Category ("NotWorking")]\r
160                 public void RunningThread ()\r
161                 {\r
162                         Application.Run (new Bug325033Form ());\r
163                         Application.Run (new Bug325033Form2 ());\r
164                 }\r
165 \r
166                 class Bug325033Form : Form\r
167                 {\r
168                         public Bug325033Form ()\r
169                         {\r
170                                 Load += new EventHandler (Form_Load);\r
171                         }\r
172 \r
173                         void Form_Load (object sender, EventArgs e)\r
174                         {\r
175                                 Thread t = new Thread (new ThreadStart (Run));\r
176                                 t.IsBackground = true;\r
177                                 t.Start ();\r
178                                 t.Join ();\r
179                                 Close ();\r
180                         }\r
181 \r
182                         void Run ()\r
183                         {\r
184                                 Application.Run (new Bug325033Form2 ());\r
185                         }\r
186                 }\r
187 \r
188                 class Bug325033Form2 : Form\r
189                 {\r
190                         public Bug325033Form2 ()\r
191                         {\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
200                                 _timer.Start ();\r
201                         }\r
202 \r
203                         void Timer_Tick (object sender, EventArgs e)\r
204                         {\r
205                                 _timer.Stop ();\r
206                                 Close ();\r
207                         }\r
208 \r
209                         private Label _label;\r
210                         private Timer _timer;\r
211                 }\r
212         }\r
213 }\r