2009-01-16 Carlos Alberto Cortez <calberto.cortez@gmail.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 : TestHelper\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                 public void RunningThread ()\r
160                 {\r
161                         Application.Run (new Bug325033Form ());\r
162                         Application.Run (new Bug325033Form2 ());\r
163                 }\r
164 \r
165                 class Bug325033Form : Form\r
166                 {\r
167                         public Bug325033Form ()\r
168                         {\r
169                                 Load += new EventHandler (Form_Load);\r
170                         }\r
171 \r
172                         void Form_Load (object sender, EventArgs e)\r
173                         {\r
174                                 Thread t = new Thread (new ThreadStart (Run));\r
175                                 t.IsBackground = true;\r
176                                 t.Start ();\r
177                                 t.Join ();\r
178                                 Close ();\r
179                         }\r
180 \r
181                         void Run ()\r
182                         {\r
183                                 Application.Run (new Bug325033Form2 ());\r
184                         }\r
185                 }\r
186 \r
187                 class Bug325033Form2 : Form\r
188                 {\r
189                         public Bug325033Form2 ()\r
190                         {\r
191                                 _label = new Label ();\r
192                                 _label.AutoSize = true;\r
193                                 _label.Dock = DockStyle.Fill;\r
194                                 _label.Text = "It should close automatically.";\r
195                                 Controls.Add (_label);\r
196                                 _timer = new Timer ();\r
197                                 _timer.Tick += new EventHandler (Timer_Tick);\r
198                                 _timer.Interval = 500;\r
199                                 _timer.Start ();\r
200                         }\r
201 \r
202                         void Timer_Tick (object sender, EventArgs e)\r
203                         {\r
204                                 _timer.Stop ();\r
205                                 Close ();\r
206                         }\r
207 \r
208                         private Label _label;\r
209                         private Timer _timer;\r
210                 }\r
211         }\r
212 }\r