Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TimerTest.cs « System.Windows.Forms « Test « Managed.Windows.Forms « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e5781a198b60edc59a07b702eb0f55d29165fcc (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// 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 ()]
#if NET_2_0
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
#else
		[ExpectedException (typeof (ArgumentException))]
#endif
		public void IntervalException1 ()
		{
			Timer timer = new Timer ();
			timer.Interval = 0;
		}

		[Test ()]
#if NET_2_0
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
#else
		[ExpectedException (typeof (ArgumentException), "'-1' is not a valid value for Interval. Interval must be greater than 0.")]
#endif
		public void IntervalException2 ()
		{
			Timer timer = new Timer ();
			timer.Interval = -1;
		}

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

		[Test ()]
#if NET_2_0
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
#else
		[ExpectedException (typeof (ArgumentException), "'-2147483648' is not a valid value for Interval. Interval must be greater than 0.")]
#endif
		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");
			}
		}
		
#if NET_2_0
		[Test]
		public void TagTest ()
		{
			Timer timer = new Timer ();
			timer.Tag = "a";
			Assert.AreEqual ("a", timer.Tag, "1");
		}
#endif

		/* 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");
#if NET_2_0
			Assert.AreEqual (null, timer.Tag, "T1");
#endif
		}

		[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;
		}
	}
}