[bcl] Remove more NET_2_0 checks from class libs
[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                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
27                 public void IntervalException1 ()\r
28                 {\r
29                         Timer timer = new Timer ();\r
30                         timer.Interval = 0;\r
31                 }\r
32 \r
33                 [Test ()]\r
34                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
35                 public void IntervalException2 ()\r
36                 {\r
37                         Timer timer = new Timer ();\r
38                         timer.Interval = -1;\r
39                 }\r
40 \r
41                 [Test ()]\r
42                 public void IntervalException3 ()\r
43                 {\r
44                         Timer timer = new Timer ();\r
45                         timer.Interval = int.MaxValue;\r
46                 }\r
47 \r
48                 [Test ()]\r
49                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
50                 public void IntervalException4 ()\r
51                 {\r
52                         Timer timer = new Timer ();\r
53                         timer.Interval = int.MinValue;\r
54                 }\r
55                 \r
56                 [Test]\r
57                 [Category ("NotWorking")]\r
58                 public void StartTest ()\r
59                 {\r
60                         // This test fails about 50% of the time on the buildbots.\r
61                         Ticked = false;\r
62                         using (Timer timer = new Timer ()) {\r
63                                 timer.Tick += new EventHandler (TickHandler);\r
64                                 timer.Start ();\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
69                         }\r
70                 }\r
71 \r
72                 [Test]\r
73                 public void StopTest ()\r
74                 {\r
75                         Ticked = false;\r
76                         using (Timer timer = new Timer ()) {\r
77                                 timer.Tick += new EventHandler (TickHandler);\r
78                                 timer.Interval = 200;\r
79                                 timer.Start ();\r
80                                 Assert.AreEqual (true, timer.Enabled, "1");\r
81                                 Assert.AreEqual (false, Ticked, "2");\r
82                                 timer.Stop ();\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
87                         }\r
88                 }\r
89                 \r
90                 [Test]\r
91                 public void TagTest ()\r
92                 {\r
93                         Timer timer = new Timer ();\r
94                         timer.Tag = "a";\r
95                         Assert.AreEqual ("a", timer.Tag, "1");\r
96                 }\r
97 \r
98                 /* Application.DoEvents and Sleep are not guarenteed on Linux\r
99                 [Test]\r
100                 public void EnabledTest ()\r
101                 {\r
102                         Ticked = false;\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
110                         }\r
111                         \r
112                         Ticked = false;\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
122                         }\r
123                 }\r
124                 */\r
125 \r
126                 void TickHandler (object sender, EventArgs e)\r
127                 {\r
128                         Ticked = true;\r
129                 }\r
130                 \r
131                 [Test]\r
132                 public void DefaultProperties ()\r
133                 {\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
140                 }\r
141 \r
142                 [Test] // bug #325033\r
143                 public void RunningThread ()\r
144                 {\r
145                         Application.Run (new Bug325033Form ());\r
146                         Application.Run (new Bug325033Form2 ());\r
147                 }\r
148 \r
149                 class Bug325033Form : Form\r
150                 {\r
151                         public Bug325033Form ()\r
152                         {\r
153                                 Load += new EventHandler (Form_Load);\r
154                         }\r
155 \r
156                         void Form_Load (object sender, EventArgs e)\r
157                         {\r
158                                 Thread t = new Thread (new ThreadStart (Run));\r
159                                 t.IsBackground = true;\r
160                                 t.Start ();\r
161                                 t.Join ();\r
162                                 Close ();\r
163                         }\r
164 \r
165                         void Run ()\r
166                         {\r
167                                 Application.Run (new Bug325033Form2 ());\r
168                         }\r
169                 }\r
170 \r
171                 class Bug325033Form2 : Form\r
172                 {\r
173                         public Bug325033Form2 ()\r
174                         {\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
183                                 _timer.Start ();\r
184                         }\r
185 \r
186                         void Timer_Tick (object sender, EventArgs e)\r
187                         {\r
188                                 _timer.Stop ();\r
189                                 Close ();\r
190                         }\r
191 \r
192                         private Label _label;\r
193                         private Timer _timer;\r
194                 }\r
195         }\r
196 }\r