Chromium Code Reviews| 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..d803c988cee868f5fe5d4dcd65111fcb4609cfba |
| --- /dev/null |
| +++ b/Source/platform/scheduler/CancellableTaskFactoryTest.cpp |
| @@ -0,0 +1,163 @@ |
| +// 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 TestFn(CancellableTaskFactory* factory) |
| +{ |
| + EXPECT_FALSE(factory->isPending()); |
| +} |
| + |
| +TEST_F(CancellableTaskFactoryTest, IsPending_InCallback) |
| +{ |
| + CancellableTaskFactory factory(WTF::bind(&TestFn, &factory)); |
| + OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| + factory.cancel(); |
|
Sami
2015/01/27 11:54:28
Did you mean to run the task here instead?
alex clarke (OOO till 29th)
2015/01/27 13:02:15
Done.
|
| + |
| + 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, Run_TasksInSequence) |
| +{ |
| + int executionCount = 0; |
| + CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| + |
| + OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task()); |
| + taskA->run(); |
| + EXPECT_EQ(1, executionCount); |
| + |
| + OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task()); |
| + taskB->run(); |
| + EXPECT_EQ(2, executionCount); |
| + |
| + OwnPtr<WebThread::Task> taskC = adoptPtr(factory.task()); |
| + taskC->run(); |
| + EXPECT_EQ(3, 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 |