| Index: Source/platform/scheduler/CancellableTaskFactoryTest.cpp
|
| diff --git a/Source/platform/scheduler/CancellableTaskFactoryTest.cpp b/Source/platform/scheduler/CancellableTaskFactoryTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c6c25a4b1446e9ff9563f63cb5394b4841973342
|
| --- /dev/null
|
| +++ b/Source/platform/scheduler/CancellableTaskFactoryTest.cpp
|
| @@ -0,0 +1,131 @@
|
| +// Copyright 2015 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 "platform/scheduler/CancellableTaskFactory.h"
|
| +
|
| +#include <gtest/gtest.h>
|
| +
|
| +using blink::CancellableTaskFactory;
|
| +using blink::WebThread;
|
| +
|
| +namespace {
|
| +
|
| +typedef testing::Test CancellableTaskFactoryTest;
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, IsPending_TaskNotCreated)
|
| +{
|
| + CancellableTaskFactory factory(nullptr);
|
| +
|
| + EXPECT_FALSE(factory.isPending());
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreated)
|
| +{
|
| + CancellableTaskFactory factory(nullptr);
|
| + OwnPtr<WebThread::Task> task = adoptPtr(factory.task());
|
| +
|
| + EXPECT_TRUE(factory.isPending());
|
| +}
|
| +
|
| +void EmptyFn()
|
| +{
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndRun)
|
| +{
|
| + CancellableTaskFactory factory(WTF::bind(&EmptyFn));
|
| + {
|
| + OwnPtr<WebThread::Task> task = adoptPtr(factory.task());
|
| + task->run();
|
| + }
|
| +
|
| + EXPECT_FALSE(factory.isPending());
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndDestroyed)
|
| +{
|
| + CancellableTaskFactory factory(nullptr);
|
| + delete factory.task();
|
| +
|
| + EXPECT_FALSE(factory.isPending());
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndCancelled)
|
| +{
|
| + CancellableTaskFactory factory(nullptr);
|
| + OwnPtr<WebThread::Task> task = adoptPtr(factory.task());
|
| + factory.cancel();
|
| +
|
| + EXPECT_FALSE(factory.isPending());
|
| +}
|
| +
|
| +void AddOne(int* ptr)
|
| +{
|
| + *ptr += 1;
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted)
|
| +{
|
| + int executionCount = 0;
|
| + CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
|
| + OwnPtr<WebThread::Task> task = adoptPtr(factory.task());
|
| + task->run();
|
| +
|
| + EXPECT_EQ(1, executionCount);
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce)
|
| +{
|
| + int executionCount = 0;
|
| + CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
|
| + OwnPtr<WebThread::Task> task = adoptPtr(factory.task());
|
| + task->run();
|
| + task->run();
|
| + task->run();
|
| + task->run();
|
| +
|
| + EXPECT_EQ(1, executionCount);
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution)
|
| +{
|
| + int executionCount = 0;
|
| + OwnPtr<WebThread::Task> task;
|
| + {
|
| + CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
|
| + task = adoptPtr(factory.task());
|
| + }
|
| + task->run();
|
| +
|
| + EXPECT_EQ(0, executionCount);
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, Cancel)
|
| +{
|
| + int executionCount = 0;
|
| + CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
|
| + OwnPtr<WebThread::Task> task = adoptPtr(factory.task());
|
| + factory.cancel();
|
| + task->run();
|
| +
|
| + EXPECT_EQ(0, executionCount);
|
| +}
|
| +
|
| +TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes)
|
| +{
|
| + int executionCount = 0;
|
| + CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
|
| +
|
| + OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task());
|
| + OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task());
|
| +
|
| + taskA->run();
|
| + EXPECT_EQ(0, executionCount);
|
| +
|
| + taskB->run();
|
| + EXPECT_EQ(1, executionCount);
|
| +}
|
| +
|
| +} // namespace
|
|
|