This source file includes following definitions.
- SaveTaskArg
- Observer
- SetUp
- Task1
- Task2
- FailingTask
- GetLastTask
- RunsTasksOnCurrentThread
- PostDelayedTask
- PostNonNestableDelayedTask
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "content/browser/startup_task_runner.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/task_runner.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
using base::Closure;
using testing::_;
using testing::Assign;
using testing::Invoke;
using testing::WithArg;
int observer_calls = 0;
int task_count = 0;
int observer_result;
base::Closure task;
bool SaveTaskArg(const Closure& arg) {
task = arg;
return true;
}
void Observer(int result) {
observer_calls++;
observer_result = result;
}
class StartupTaskRunnerTest : public testing::Test {
public:
virtual void SetUp() {
last_task_ = 0;
observer_calls = 0;
task_count = 0;
}
int Task1() {
last_task_ = 1;
task_count++;
return 0;
}
int Task2() {
last_task_ = 2;
task_count++;
return 0;
}
int FailingTask() {
last_task_ = 3;
task_count++;
return 1;
}
int GetLastTask() { return last_task_; }
private:
int last_task_;
};
class MockTaskRunner {
public:
MOCK_METHOD3(
PostDelayedTask,
bool(const tracked_objects::Location&, const Closure&, base::TimeDelta));
MOCK_METHOD3(
PostNonNestableDelayedTask,
bool(const tracked_objects::Location&, const Closure&, base::TimeDelta));
};
class TaskRunnerProxy : public base::SingleThreadTaskRunner {
public:
TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {}
virtual bool RunsTasksOnCurrentThread() const OVERRIDE { return true; }
virtual bool PostDelayedTask(const tracked_objects::Location& location,
const Closure& closure,
base::TimeDelta delta) OVERRIDE {
return mock_->PostDelayedTask(location, closure, delta);
}
virtual bool PostNonNestableDelayedTask(
const tracked_objects::Location& location,
const Closure& closure,
base::TimeDelta delta) OVERRIDE {
return mock_->PostNonNestableDelayedTask(location, closure, delta);
}
private:
MockTaskRunner* mock_;
virtual ~TaskRunnerProxy() {}
};
TEST_F(StartupTaskRunnerTest, SynchronousExecution) {
MockTaskRunner mock_runner;
scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0);
StartupTaskRunner runner(base::Bind(&Observer), proxy);
StartupTask task1 =
base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this));
runner.AddTask(task1);
EXPECT_EQ(GetLastTask(), 0);
StartupTask task2 =
base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
runner.AddTask(task2);
EXPECT_EQ(GetLastTask(), 0);
runner.RunAllTasksNow();
EXPECT_EQ(GetLastTask(), 2);
EXPECT_EQ(task_count, 2);
EXPECT_EQ(observer_calls, 1);
EXPECT_EQ(observer_result, 0);
runner.StartRunningTasksAsync();
EXPECT_EQ(task_count, 2);
EXPECT_EQ(observer_calls, 1);
}
TEST_F(StartupTaskRunnerTest, NullObserver) {
MockTaskRunner mock_runner;
scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0);
StartupTaskRunner runner(base::Callback<void(int)>(), proxy);
StartupTask task1 =
base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this));
runner.AddTask(task1);
EXPECT_EQ(GetLastTask(), 0);
StartupTask task2 =
base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
runner.AddTask(task2);
EXPECT_EQ(GetLastTask(), 0);
runner.RunAllTasksNow();
EXPECT_EQ(GetLastTask(), 2);
EXPECT_EQ(task_count, 2);
runner.StartRunningTasksAsync();
EXPECT_EQ(task_count, 2);
EXPECT_EQ(observer_calls, 0);
}
TEST_F(StartupTaskRunnerTest, SynchronousExecutionFailedTask) {
MockTaskRunner mock_runner;
scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
EXPECT_CALL(mock_runner, PostNonNestableDelayedTask(_, _, _)).Times(0);
StartupTaskRunner runner(base::Bind(&Observer), proxy);
StartupTask task3 =
base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this));
runner.AddTask(task3);
EXPECT_EQ(GetLastTask(), 0);
StartupTask task2 =
base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
runner.AddTask(task2);
EXPECT_EQ(GetLastTask(), 0);
runner.RunAllTasksNow();
EXPECT_EQ(GetLastTask(), 3);
EXPECT_EQ(task_count, 1);
EXPECT_EQ(observer_calls, 1);
EXPECT_EQ(observer_result, 1);
runner.StartRunningTasksAsync();
EXPECT_EQ(observer_calls, 1);
EXPECT_EQ(task_count, 1);
}
TEST_F(StartupTaskRunnerTest, AsynchronousExecution) {
MockTaskRunner mock_runner;
scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
EXPECT_CALL(
mock_runner,
PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0)))
.Times(testing::Between(2, 3))
.WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg)));
StartupTaskRunner runner(base::Bind(&Observer), proxy);
StartupTask task1 =
base::Bind(&StartupTaskRunnerTest::Task1, base::Unretained(this));
runner.AddTask(task1);
StartupTask task2 =
base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
runner.AddTask(task2);
EXPECT_EQ(GetLastTask(), 0);
runner.StartRunningTasksAsync();
EXPECT_EQ(GetLastTask(), 0);
for (int i = 0; i < 4 && observer_calls == 0; i++) {
task.Run();
EXPECT_EQ(i + 1, GetLastTask());
}
EXPECT_EQ(task_count, 2);
EXPECT_EQ(observer_calls, 1);
EXPECT_EQ(observer_result, 0);
runner.RunAllTasksNow();
EXPECT_EQ(task_count, 2);
EXPECT_EQ(observer_calls, 1);
}
TEST_F(StartupTaskRunnerTest, AsynchronousExecutionFailedTask) {
MockTaskRunner mock_runner;
scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
EXPECT_CALL(mock_runner, PostDelayedTask(_, _, _)).Times(0);
EXPECT_CALL(
mock_runner,
PostNonNestableDelayedTask(_, _, base::TimeDelta::FromMilliseconds(0)))
.Times(testing::Between(1, 2))
.WillRepeatedly(WithArg<1>(Invoke(SaveTaskArg)));
StartupTaskRunner runner(base::Bind(&Observer), proxy);
StartupTask task3 =
base::Bind(&StartupTaskRunnerTest::FailingTask, base::Unretained(this));
runner.AddTask(task3);
StartupTask task2 =
base::Bind(&StartupTaskRunnerTest::Task2, base::Unretained(this));
runner.AddTask(task2);
EXPECT_EQ(GetLastTask(), 0);
runner.StartRunningTasksAsync();
EXPECT_EQ(GetLastTask(), 0);
for (int i = 0; i < 4 && observer_calls == 0; i++) {
task.Run();
}
EXPECT_EQ(GetLastTask(), 3);
EXPECT_EQ(task_count, 1);
EXPECT_EQ(observer_calls, 1);
EXPECT_EQ(observer_result, 1);
runner.RunAllTasksNow();
EXPECT_EQ(observer_calls, 1);
EXPECT_EQ(task_count, 1);
}
}
}