This source file includes following definitions.
- Interval
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "cc/scheduler/delay_based_time_source.h"
#include "base/basictypes.h"
#include "base/test/test_simple_task_runner.h"
#include "cc/test/scheduler_test_common.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cc {
namespace {
base::TimeDelta Interval() {
return base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond /
60);
}
TEST(DelayBasedTimeSourceTest, TaskPostedAndTickCalled) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
EXPECT_TRUE(timer->Active());
EXPECT_TRUE(task_runner->HasPendingTask());
timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(16));
task_runner->RunPendingTasks();
EXPECT_TRUE(timer->Active());
EXPECT_TRUE(client.TickCalled());
}
TEST(DelayBasedTimeSource, TickNotCalledWithTaskPosted) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
EXPECT_TRUE(task_runner->HasPendingTask());
timer->SetActive(false);
task_runner->RunPendingTasks();
EXPECT_FALSE(client.TickCalled());
}
TEST(DelayBasedTimeSource, StartTwiceEnqueuesOneTask) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
EXPECT_TRUE(task_runner->HasPendingTask());
task_runner->ClearPendingTasks();
timer->SetActive(true);
EXPECT_FALSE(task_runner->HasPendingTask());
}
TEST(DelayBasedTimeSource, StartWhenRunningDoesntTick) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
EXPECT_TRUE(task_runner->HasPendingTask());
task_runner->RunPendingTasks();
task_runner->ClearPendingTasks();
timer->SetActive(true);
EXPECT_FALSE(task_runner->HasPendingTask());
}
TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyOnRequestedTime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval());
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterRequestedTime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval() +
base::TimeDelta::FromMicroseconds(1));
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, NextDelaySaneWhenExactlyTwiceAfterRequestedTime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + 2 * Interval());
task_runner->RunPendingTasks();
EXPECT_EQ(0, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, NextDelaySaneWhenSlightlyAfterTwiceRequestedTime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + 2 * Interval() +
base::TimeDelta::FromMicroseconds(1));
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, NextDelaySaneWhenHalfAfterRequestedTime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval() +
base::TimeDelta::FromMilliseconds(8));
task_runner->RunPendingTasks();
EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, SaneHandlingOfJitteryTimebase) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval());
timer->SetTimebaseAndInterval(
timer->Now() + base::TimeDelta::FromMilliseconds(1), Interval());
task_runner->RunPendingTasks();
EXPECT_EQ(17, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval());
timer->SetTimebaseAndInterval(
timer->Now() - base::TimeDelta::FromMilliseconds(1), Interval());
task_runner->RunPendingTasks();
EXPECT_EQ(15, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, HandlesSignificantTimebaseChangesImmediately) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval());
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
client.Reset();
task_runner->ClearPendingTasks();
task_runner->RunPendingTasks();
base::TimeDelta jitter = base::TimeDelta::FromMilliseconds(7) +
base::TimeDelta::FromMicroseconds(1);
timer->SetTimebaseAndInterval(timer->Now() + jitter, Interval());
EXPECT_FALSE(client.TickCalled());
EXPECT_EQ(16 + 7, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval() + jitter);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
client.Reset();
task_runner->ClearPendingTasks();
task_runner->RunPendingTasks();
timer->SetTimebaseAndInterval(base::TimeTicks() + Interval(), Interval());
EXPECT_FALSE(client.TickCalled());
EXPECT_EQ(16 - 7, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, HanldlesSignificantIntervalChangesImmediately) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval());
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
client.Reset();
task_runner->ClearPendingTasks();
task_runner->RunPendingTasks();
timer->SetTimebaseAndInterval(base::TimeTicks() + Interval(), Interval() * 2);
EXPECT_FALSE(client.TickCalled());
EXPECT_EQ(33, task_runner->NextPendingTaskDelay().InMilliseconds());
timer->SetNow(timer->Now() + Interval() * 2);
task_runner->RunPendingTasks();
EXPECT_EQ(33, task_runner->NextPendingTaskDelay().InMilliseconds());
client.Reset();
task_runner->ClearPendingTasks();
task_runner->RunPendingTasks();
timer->SetTimebaseAndInterval(base::TimeTicks() + Interval() * 3, Interval());
EXPECT_FALSE(client.TickCalled());
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, JitteryRuntimeWithFutureTimebases) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
base::TimeTicks future_timebase = timer->Now() + Interval() * 10;
base::TimeDelta jitter1 = base::TimeDelta::FromMilliseconds(1);
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() + jitter1);
task_runner->RunPendingTasks();
EXPECT_EQ(15, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() - jitter1);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() - jitter1);
task_runner->RunPendingTasks();
EXPECT_EQ(17, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() + jitter1);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
base::TimeDelta jitter8 = base::TimeDelta::FromMilliseconds(8);
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() + jitter8);
task_runner->RunPendingTasks();
EXPECT_EQ(8, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() - jitter8);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() - jitter8);
task_runner->RunPendingTasks();
EXPECT_EQ(24, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() + jitter8);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
base::TimeDelta jitter15 = base::TimeDelta::FromMilliseconds(15);
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() + jitter15);
task_runner->RunPendingTasks();
EXPECT_EQ(1, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() - jitter15);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() - jitter15);
task_runner->RunPendingTasks();
EXPECT_EQ(31, task_runner->NextPendingTaskDelay().InMilliseconds());
future_timebase += Interval();
timer->SetTimebaseAndInterval(future_timebase, Interval());
timer->SetNow(timer->Now() + Interval() + jitter15);
task_runner->RunPendingTasks();
EXPECT_EQ(16, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSourceTest, AchievesTargetRateWithNoNoise) {
int num_iterations = 10;
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
double total_frame_time = 0.0;
for (int i = 0; i < num_iterations; ++i) {
int64 delay_ms = task_runner->NextPendingTaskDelay().InMilliseconds();
total_frame_time += delay_ms / 1000.0;
timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(delay_ms));
task_runner->RunPendingTasks();
}
double average_interval =
total_frame_time / static_cast<double>(num_iterations);
EXPECT_NEAR(1.0 / 60.0, average_interval, 0.1);
}
TEST(DelayBasedTimeSource, TestDeactivateWhilePending) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
timer->SetActive(false);
timer = NULL;
EXPECT_TRUE(task_runner->HasPendingTask());
task_runner->RunPendingTasks();
}
TEST(DelayBasedTimeSource, TestDeactivateAndReactivateBeforeNextTickTime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
timer->SetActive(false);
task_runner->RunPendingTasks();
timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(4));
timer->SetActive(true);
EXPECT_EQ(12, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, TestDeactivateAndReactivateAfterNextTickTime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(Interval(), task_runner.get());
timer->SetClient(&client);
timer->SetActive(true);
task_runner->RunPendingTasks();
timer->SetActive(false);
task_runner->RunPendingTasks();
timer->SetNow(timer->Now() + base::TimeDelta::FromMilliseconds(20));
timer->SetActive(true);
EXPECT_EQ(13, task_runner->NextPendingTaskDelay().InMilliseconds());
}
TEST(DelayBasedTimeSource, TestOverflow) {
base::TimeDelta interval = base::TimeDelta::FromInternalValue(4000);
base::TimeTicks big_now = base::TimeTicks::FromInternalValue(8635916564000);
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
new base::TestSimpleTaskRunner;
FakeTimeSourceClient client;
scoped_refptr<FakeDelayBasedTimeSource> timer =
FakeDelayBasedTimeSource::Create(interval, task_runner.get());
timer->SetClient(&client);
timer->SetNow(big_now);
timer->SetActive(true);
EXPECT_EQ(0, task_runner->NextPendingTaskDelay().InMilliseconds());
}
}
}