File: TimerTest.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (196 lines) | stat: -rw-r--r-- 4,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Copyright (c) 2007 Novell, Inc.
//
// Authors:
//	Rolf Bjarne Kvinge  (RKvinge@novell.com)
//

using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Threading;
using Timer = System.Windows.Forms.Timer;
using Sys_Threading=System.Threading;

using NUnit.Framework;

namespace MonoTests.System.Windows.Forms
{
	[TestFixture]
	public class TimerTest : TestHelper
	{
		bool Ticked;
		
		[Test ()]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void IntervalException1 ()
		{
			Timer timer = new Timer ();
			timer.Interval = 0;
		}

		[Test ()]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void IntervalException2 ()
		{
			Timer timer = new Timer ();
			timer.Interval = -1;
		}

		[Test ()]
		public void IntervalException3 ()
		{
			Timer timer = new Timer ();
			timer.Interval = int.MaxValue;
		}

		[Test ()]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void IntervalException4 ()
		{
			Timer timer = new Timer ();
			timer.Interval = int.MinValue;
		}
		
		[Test]
		[Category ("NotWorking")]
		public void StartTest ()
		{
			// This test fails about 50% of the time on the buildbots.
			Ticked = false;
			using (Timer timer = new Timer ()) {
				timer.Tick += new EventHandler (TickHandler);
				timer.Start ();
				Sys_Threading.Thread.Sleep (500);
				Application.DoEvents ();
				Assert.AreEqual (true, timer.Enabled, "1");
				Assert.AreEqual (true, Ticked, "2");
			}
		}

		[Test]
		public void StopTest ()
		{
			Ticked = false;
			using (Timer timer = new Timer ()) {
				timer.Tick += new EventHandler (TickHandler);
				timer.Interval = 200;
				timer.Start ();
				Assert.AreEqual (true, timer.Enabled, "1");
				Assert.AreEqual (false, Ticked, "2");
				timer.Stop ();
				Assert.AreEqual (false, Ticked, "3"); // This may fail if we are running on a very slow machine...
				Assert.AreEqual (false, timer.Enabled, "4");
				Sys_Threading.Thread.Sleep (500);
				Assert.AreEqual (false, Ticked, "5");
			}
		}
		
		[Test]
		public void TagTest ()
		{
			Timer timer = new Timer ();
			timer.Tag = "a";
			Assert.AreEqual ("a", timer.Tag, "1");
		}

		/* Application.DoEvents and Sleep are not guarenteed on Linux
		[Test]
		public void EnabledTest ()
		{
			Ticked = false;
			using (Timer timer = new Timer ()) {
				timer.Tick += new EventHandler (TickHandler);
				timer.Enabled = true;
				Sys_Threading.Thread.Sleep (150);
				Application.DoEvents ();
				Assert.AreEqual (true, timer.Enabled, "1");
				Assert.AreEqual (true, Ticked, "2");
			}
			
			Ticked = false;
			using (Timer timer = new Timer ()) {
				timer.Tick += new EventHandler (TickHandler);
				timer.Interval = 1000;
				timer.Enabled = true;
				Assert.AreEqual (true, timer.Enabled, "3");
				Assert.AreEqual (false, Ticked, "4");
				timer.Enabled = false;
				Assert.AreEqual (false, Ticked, "5"); // This may fail if we are running on a very slow machine...
				Assert.AreEqual (false, timer.Enabled, "6");
			}
		}
		*/

		void TickHandler (object sender, EventArgs e)
		{
			Ticked = true;
		}
		
		[Test]
		public void DefaultProperties ()
		{
			Timer timer = new Timer ();
			Assert.AreEqual (null, timer.Container, "C1");
			Assert.AreEqual (false, timer.Enabled, "E1");
			Assert.AreEqual (100, timer.Interval, "I1");
			Assert.AreEqual (null, timer.Site, "S1");
			Assert.AreEqual (null, timer.Tag, "T1");
		}

		[Test] // bug #325033
		public void RunningThread ()
		{
			Application.Run (new Bug325033Form ());
			Application.Run (new Bug325033Form2 ());
		}

		class Bug325033Form : Form
		{
			public Bug325033Form ()
			{
				Load += new EventHandler (Form_Load);
			}

			void Form_Load (object sender, EventArgs e)
			{
				Thread t = new Thread (new ThreadStart (Run));
				t.IsBackground = true;
				t.Start ();
				t.Join ();
				Close ();
			}

			void Run ()
			{
				Application.Run (new Bug325033Form2 ());
			}
		}

		class Bug325033Form2 : Form
		{
			public Bug325033Form2 ()
			{
				_label = new Label ();
				_label.AutoSize = true;
				_label.Dock = DockStyle.Fill;
				_label.Text = "It should close automatically.";
				Controls.Add (_label);
				_timer = new Timer ();
				_timer.Tick += new EventHandler (Timer_Tick);
				_timer.Interval = 500;
				_timer.Start ();
			}

			void Timer_Tick (object sender, EventArgs e)
			{
				_timer.Stop ();
				Close ();
			}

			private Label _label;
			private Timer _timer;
		}
	}
}