OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "platform/scheduler/CancellableTaskFactory.h" |
| 7 |
| 8 #include <gtest/gtest.h> |
| 9 |
| 10 using blink::CancellableTaskFactory; |
| 11 using blink::WebThread; |
| 12 |
| 13 namespace { |
| 14 |
| 15 typedef testing::Test CancellableTaskFactoryTest; |
| 16 |
| 17 TEST_F(CancellableTaskFactoryTest, IsPending_TaskNotCreated) |
| 18 { |
| 19 CancellableTaskFactory factory(nullptr); |
| 20 |
| 21 EXPECT_FALSE(factory.isPending()); |
| 22 } |
| 23 |
| 24 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreated) |
| 25 { |
| 26 CancellableTaskFactory factory(nullptr); |
| 27 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 28 |
| 29 EXPECT_TRUE(factory.isPending()); |
| 30 } |
| 31 |
| 32 void EmptyFn() |
| 33 { |
| 34 } |
| 35 |
| 36 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndRun) |
| 37 { |
| 38 CancellableTaskFactory factory(WTF::bind(&EmptyFn)); |
| 39 { |
| 40 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 41 task->run(); |
| 42 } |
| 43 |
| 44 EXPECT_FALSE(factory.isPending()); |
| 45 } |
| 46 |
| 47 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndDestroyed) |
| 48 { |
| 49 CancellableTaskFactory factory(nullptr); |
| 50 delete factory.task(); |
| 51 |
| 52 EXPECT_FALSE(factory.isPending()); |
| 53 } |
| 54 |
| 55 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndCancelled) |
| 56 { |
| 57 CancellableTaskFactory factory(nullptr); |
| 58 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 59 factory.cancel(); |
| 60 |
| 61 EXPECT_FALSE(factory.isPending()); |
| 62 } |
| 63 |
| 64 void TestFn(CancellableTaskFactory* factory) |
| 65 { |
| 66 EXPECT_FALSE(factory->isPending()); |
| 67 } |
| 68 |
| 69 TEST_F(CancellableTaskFactoryTest, IsPending_InCallback) |
| 70 { |
| 71 CancellableTaskFactory factory(WTF::bind(&TestFn, &factory)); |
| 72 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 73 factory.cancel(); |
| 74 |
| 75 EXPECT_FALSE(factory.isPending()); |
| 76 } |
| 77 |
| 78 void AddOne(int* ptr) |
| 79 { |
| 80 *ptr += 1; |
| 81 } |
| 82 |
| 83 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) |
| 84 { |
| 85 int executionCount = 0; |
| 86 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 87 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 88 task->run(); |
| 89 |
| 90 EXPECT_EQ(1, executionCount); |
| 91 } |
| 92 |
| 93 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) |
| 94 { |
| 95 int executionCount = 0; |
| 96 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 97 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 98 task->run(); |
| 99 task->run(); |
| 100 task->run(); |
| 101 task->run(); |
| 102 |
| 103 EXPECT_EQ(1, executionCount); |
| 104 } |
| 105 |
| 106 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) |
| 107 { |
| 108 int executionCount = 0; |
| 109 OwnPtr<WebThread::Task> task; |
| 110 { |
| 111 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 112 task = adoptPtr(factory.task()); |
| 113 } |
| 114 task->run(); |
| 115 |
| 116 EXPECT_EQ(0, executionCount); |
| 117 } |
| 118 |
| 119 TEST_F(CancellableTaskFactoryTest, Cancel) |
| 120 { |
| 121 int executionCount = 0; |
| 122 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 123 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 124 factory.cancel(); |
| 125 task->run(); |
| 126 |
| 127 EXPECT_EQ(0, executionCount); |
| 128 } |
| 129 |
| 130 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) |
| 131 { |
| 132 int executionCount = 0; |
| 133 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 134 |
| 135 OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task()); |
| 136 OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task()); |
| 137 |
| 138 taskA->run(); |
| 139 EXPECT_EQ(0, executionCount); |
| 140 |
| 141 taskB->run(); |
| 142 EXPECT_EQ(1, executionCount); |
| 143 } |
| 144 |
| 145 } // namespace |
OLD | NEW |