-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
TimerTest.java
213 lines (193 loc) · 5.43 KB
/
TimerTest.java
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
213
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.tests;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
public class TimerTest extends GdxTest {
static final int expectedTests = 10;
@Override
public void create () {
Gdx.app.log("TimerTest", "Starting tests...");
testOnce();
testRepeat();
testCancelPosted();
testCancelPostedTwice();
testCancelPostedReschedule();
testStop();
testStopStart();
testDelay();
testClear();
}
float time;
int testCount;
Task repeatTask;
public void render () {
if (time < 2) {
time += Gdx.graphics.getDeltaTime();
if (time >= 2) {
if (testCount != expectedTests)
throw new RuntimeException("Some tests did not run: " + testCount + " != " + expectedTests);
Gdx.app.log("TimerTest", "SUCCESS! " + testCount + " tests passed.");
repeatTask.cancel();
}
}
}
void testOnce () {
Task task = Timer.schedule(new Task() {
@Override
public void run () {
Gdx.app.log("TimerTest", "testOnce");
testCount++;
}
}, 1);
assertScheduled(task);
}
void testRepeat () {
repeatTask = Timer.schedule(new Task() {
int count;
@Override
public void run () {
Gdx.app.log("TimerTest", "testRepeat");
if (++count <= 2) testCount++;
}
}, 1, 1);
assertScheduled(repeatTask);
}
void testCancelPosted () {
Task task = Timer.schedule(new Task() {
@Override
public void run () {
throw new RuntimeException("Cancelled task should not run.");
}
}, 0.01f);
assertScheduled(task);
sleep(200); // Sleep so the task is posted.
assertNotScheduled(task);
task.cancel(); // The posted task should not execute.
assertNotScheduled(task);
Gdx.app.log("TimerTest", "testCancelPosted");
testCount++;
}
void testCancelPostedTwice () {
Task task = new Task() {
@Override
public void run () {
throw new RuntimeException("Cancelled task should not run.");
}
};
Timer.schedule(task, 0.01f);
assertScheduled(task);
sleep(200); // Sleep so the task is posted.
assertNotScheduled(task);
Timer.schedule(task, 0.01f);
assertScheduled(task);
sleep(200); // Sleep so the task is posted.
assertNotScheduled(task);
task.cancel(); // The twice posted task should not execute.
assertNotScheduled(task);
Gdx.app.log("TimerTest", "testCancelPostedTwice");
testCount++;
}
void testCancelPostedReschedule () {
Task task = Timer.schedule(new Task() {
int count;
@Override
public void run () {
if (++count != 1) throw new RuntimeException("Rescheduled task should only run once.");
Gdx.app.log("TimerTest", "testCancelPostedReschedule");
testCount++;
}
}, 0.01f);
assertScheduled(task);
sleep(200); // Sleep so the task is posted.
assertNotScheduled(task);
task.cancel(); // The posted task should not execute.
assertNotScheduled(task);
Timer.schedule(task, 0.01f); // Schedule it again.
assertScheduled(task);
}
void testStop () {
Timer timer = new Timer();
Task task = timer.scheduleTask(new Task() {
@Override
public void run () {
throw new RuntimeException("Stopped timer should not run tasks.");
}
}, 1);
assertScheduled(task);
timer.stop();
Gdx.app.log("TimerTest", "testStop");
testCount++;
}
void testStopStart () {
Timer timer = new Timer();
Task task = timer.scheduleTask(new Task() {
@Override
public void run () {
Gdx.app.log("TimerTest", "testStopStart");
testCount++;
}
}, 0.200f);
assertScheduled(task);
timer.stop();
sleep(300); // Sleep longer than task delay while stopped.
timer.start();
sleep(100);
assertScheduled(task); // Shouldn't have happened yet.
}
void testDelay () {
Timer timer = new Timer();
Task task = timer.scheduleTask(new Task() {
@Override
public void run () {
Gdx.app.log("TimerTest", "testDelay");
testCount++;
}
}, 0.200f);
assertScheduled(task);
timer.delay(200);
sleep(300); // Sleep longer than task delay.
assertScheduled(task); // Shouldn't have happened yet.
}
void testClear () {
Timer timer = new Timer();
Task task = timer.scheduleTask(new Task() {
@Override
public void run () {
throw new RuntimeException("Cleared timer should not run tasks.");
}
}, 0.200f);
assertScheduled(task);
timer.clear();
assertNotScheduled(task);
Gdx.app.log("TimerTest", "testClear");
testCount++;
}
void assertScheduled (Task task) {
if (!task.isScheduled()) throw new RuntimeException("Should be scheduled.");
}
void assertNotScheduled (Task task) {
if (task.isScheduled()) throw new RuntimeException("Should not be scheduled.");
}
void sleep (long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
}
}
}