summaryrefslogtreecommitdiffstats
path: root/cc/delay_based_time_source_unittest.cc
blob: 60c9d45ce4c5198b709169a83bb172f556676f56 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"

#include "cc/delay_based_time_source.h"

#include "cc/test/scheduler_test_common.h"
#include "cc/thread.h"
#include "testing/gtest/include/gtest/gtest.h"

using namespace cc;
using namespace WebKitTests;

namespace {

base::TimeDelta interval()
{
    return base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond / 60);
}

TEST(DelayBasedTimeSourceTest, TaskPostedAndTickCalled)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);

    timer->setActive(true);
    EXPECT_TRUE(timer->active());
    EXPECT_TRUE(thread.hasPendingTask());

    timer->setNow(timer->now() + base::TimeDelta::FromMilliseconds(16));
    thread.runPendingTask();
    EXPECT_TRUE(timer->active());
    EXPECT_TRUE(client.tickCalled());
}

TEST(DelayBasedTimeSource, TickNotCalledWithTaskPosted)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    EXPECT_TRUE(thread.hasPendingTask());
    timer->setActive(false);
    thread.runPendingTask();
    EXPECT_FALSE(client.tickCalled());
}

TEST(DelayBasedTimeSource, StartTwiceEnqueuesOneTask)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    EXPECT_TRUE(thread.hasPendingTask());
    thread.reset();
    timer->setActive(true);
    EXPECT_FALSE(thread.hasPendingTask());
}

TEST(DelayBasedTimeSource, StartWhenRunningDoesntTick)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    thread.runPendingTask();
    thread.reset();
    timer->setActive(true);
    EXPECT_FALSE(thread.hasPendingTask());
}

// At 60Hz, when the tick returns at exactly the requested next time, make sure
// a 16ms next delay is posted.
TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyOnRequestedTime)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    timer->setNow(timer->now() + interval());
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());
}

// At 60Hz, when the tick returns at slightly after the requested next time, make sure
// a 16ms next delay is posted.
TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterRequestedTime)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    timer->setNow(timer->now() + interval() + base::TimeDelta::FromMicroseconds(1));
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());
}

// At 60Hz, when the tick returns at exactly 2*interval after the requested next time, make sure
// a 16ms next delay is posted.
TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyTwiceAfterRequestedTime)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    timer->setNow(timer->now() + 2 * interval());
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());
}

// At 60Hz, when the tick returns at 2*interval and a bit after the requested next time, make sure
// a 16ms next delay is posted.
TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterTwiceRequestedTime)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    timer->setNow(timer->now() + 2 * interval() + base::TimeDelta::FromMicroseconds(1));
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());
}

// At 60Hz, when the tick returns halfway to the next frame time, make sure
// a correct next delay value is posted.
TEST(DelayBasedTimeSource, NextDelaySaneWhenHalfAfterRequestedTime)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    timer->setNow(timer->now() + interval() + base::TimeDelta::FromMilliseconds(8));
    thread.runPendingTask();

    EXPECT_EQ(8, thread.pendingDelayMs());
}

// If the timebase and interval are updated with a jittery source, we want to
// make sure we do not double tick.
TEST(DelayBasedTimeSource, SaneHandlingOfJitteryTimebase)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    // Jitter timebase ~1ms late
    timer->setNow(timer->now() + interval());
    timer->setTimebaseAndInterval(timer->now() + base::TimeDelta::FromMilliseconds(1), interval());
    thread.runPendingTask();

    // Without double tick prevention, pendingDelayMs would be 1.
    EXPECT_EQ(17, thread.pendingDelayMs());

    // Jitter timebase ~1ms early
    timer->setNow(timer->now() + interval());
    timer->setTimebaseAndInterval(timer->now() - base::TimeDelta::FromMilliseconds(1), interval());
    thread.runPendingTask();

    EXPECT_EQ(15, thread.pendingDelayMs());
}

TEST(DelayBasedTimeSource, HandlesSignificantTimebaseChangesImmediately)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    // Tick, then shift timebase by +7ms.
    timer->setNow(timer->now() + interval());
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    client.reset();
    thread.runPendingTaskOnOverwrite(true);
    base::TimeDelta jitter = base::TimeDelta::FromMilliseconds(7) + base::TimeDelta::FromMicroseconds(1);
    timer->setTimebaseAndInterval(timer->now() + jitter, interval());
    thread.runPendingTaskOnOverwrite(false);

    EXPECT_FALSE(client.tickCalled()); // Make sure pending tasks were canceled.
    EXPECT_EQ(7, thread.pendingDelayMs());

    // Tick, then shift timebase by -7ms.
    timer->setNow(timer->now() + jitter);
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    client.reset();
    thread.runPendingTaskOnOverwrite(true);
    timer->setTimebaseAndInterval(base::TimeTicks() + interval(), interval());
    thread.runPendingTaskOnOverwrite(false);

    EXPECT_FALSE(client.tickCalled()); // Make sure pending tasks were canceled.
    EXPECT_EQ(16-7, thread.pendingDelayMs());
}

TEST(DelayBasedTimeSource, HanldlesSignificantIntervalChangesImmediately)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);
    // Run the first task, as that activates the timer and picks up a timebase.
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    // Tick, then double the interval.
    timer->setNow(timer->now() + interval());
    thread.runPendingTask();

    EXPECT_EQ(16, thread.pendingDelayMs());

    client.reset();
    thread.runPendingTaskOnOverwrite(true);
    timer->setTimebaseAndInterval(base::TimeTicks() + interval(), interval() * 2);
    thread.runPendingTaskOnOverwrite(false);

    EXPECT_FALSE(client.tickCalled()); // Make sure pending tasks were canceled.
    EXPECT_EQ(33, thread.pendingDelayMs());

    // Tick, then halve the interval.
    timer->setNow(timer->now() + interval() * 2);
    thread.runPendingTask();

    EXPECT_EQ(33, thread.pendingDelayMs());

    client.reset();
    thread.runPendingTaskOnOverwrite(true);
    timer->setTimebaseAndInterval(base::TimeTicks() + interval() * 3, interval());
    thread.runPendingTaskOnOverwrite(false);

    EXPECT_FALSE(client.tickCalled()); // Make sure pending tasks were canceled.
    EXPECT_EQ(16, thread.pendingDelayMs());
}

TEST(DelayBasedTimeSourceTest, AchievesTargetRateWithNoNoise)
{
    int numIterations = 10;

    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true);

    double totalFrameTime = 0;
    for (int i = 0; i < numIterations; ++i) {
        long long delayMs = thread.pendingDelayMs();

        // accumulate the "delay"
        totalFrameTime += delayMs / 1000.0;

        // Run the callback exactly when asked
        timer->setNow(timer->now() + base::TimeDelta::FromMilliseconds(delayMs));
        thread.runPendingTask();
    }
    double averageInterval = totalFrameTime / static_cast<double>(numIterations);
    EXPECT_NEAR(1.0 / 60.0, averageInterval, 0.1);
}

TEST(DelayBasedTimeSource, TestDeactivateWhilePending)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);
    timer->setActive(true); // Should post a task.
    timer->setActive(false);
    timer = NULL;
    thread.runPendingTask(); // Should run the posted task without crashing.
}

TEST(DelayBasedTimeSource, TestDeactivateAndReactivateBeforeNextTickTime)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);

    // Should run the activate task, and pick up a new timebase.
    timer->setActive(true);
    thread.runPendingTask();

    // Stop the timer
    timer->setActive(false);

    // Task will be pending anyway, run it
    thread.runPendingTask();

    // Start the timer again, but before the next tick time the timer previously
    // planned on using. That same tick time should still be targeted.
    timer->setNow(timer->now() + base::TimeDelta::FromMilliseconds(4));
    timer->setActive(true);
    EXPECT_EQ(12, thread.pendingDelayMs());
}

TEST(DelayBasedTimeSource, TestDeactivateAndReactivateAfterNextTickTime)
{
    FakeThread thread;
    FakeTimeSourceClient client;
    scoped_refptr<FakeDelayBasedTimeSource> timer = FakeDelayBasedTimeSource::create(interval(), &thread);
    timer->setClient(&client);

    // Should run the activate task, and pick up a new timebase.
    timer->setActive(true);
    thread.runPendingTask();

    // Stop the timer
    timer->setActive(false);

    // Task will be pending anyway, run it
    thread.runPendingTask();

    // Start the timer again, but before the next tick time the timer previously
    // planned on using. That same tick time should still be targeted.
    timer->setNow(timer->now() + base::TimeDelta::FromMilliseconds(20));
    timer->setActive(true);
    EXPECT_EQ(13, thread.pendingDelayMs());
}

}