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 class TestClass { |
| 65 public: |
| 66 CancellableTaskFactory m_factory; |
| 67 |
| 68 TestClass() |
| 69 : m_factory(WTF::bind(&TestClass::TestFn, this)) |
| 70 { |
| 71 } |
| 72 |
| 73 void TestFn() |
| 74 { |
| 75 EXPECT_FALSE(m_factory.isPending()); |
| 76 } |
| 77 }; |
| 78 |
| 79 TEST_F(CancellableTaskFactoryTest, IsPending_InCallback) |
| 80 { |
| 81 TestClass testClass; |
| 82 OwnPtr<WebThread::Task> task = adoptPtr(testClass.m_factory.task()); |
| 83 task->run(); |
| 84 } |
| 85 |
| 86 void AddOne(int* ptr) |
| 87 { |
| 88 *ptr += 1; |
| 89 } |
| 90 |
| 91 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) |
| 92 { |
| 93 int executionCount = 0; |
| 94 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 95 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 96 task->run(); |
| 97 |
| 98 EXPECT_EQ(1, executionCount); |
| 99 } |
| 100 |
| 101 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) |
| 102 { |
| 103 int executionCount = 0; |
| 104 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 105 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 106 task->run(); |
| 107 task->run(); |
| 108 task->run(); |
| 109 task->run(); |
| 110 |
| 111 EXPECT_EQ(1, executionCount); |
| 112 } |
| 113 |
| 114 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) |
| 115 { |
| 116 int executionCount = 0; |
| 117 OwnPtr<WebThread::Task> task; |
| 118 { |
| 119 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 120 task = adoptPtr(factory.task()); |
| 121 } |
| 122 task->run(); |
| 123 |
| 124 EXPECT_EQ(0, executionCount); |
| 125 } |
| 126 |
| 127 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence) |
| 128 { |
| 129 int executionCount = 0; |
| 130 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 131 |
| 132 OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task()); |
| 133 taskA->run(); |
| 134 EXPECT_EQ(1, executionCount); |
| 135 |
| 136 OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task()); |
| 137 taskB->run(); |
| 138 EXPECT_EQ(2, executionCount); |
| 139 |
| 140 OwnPtr<WebThread::Task> taskC = adoptPtr(factory.task()); |
| 141 taskC->run(); |
| 142 EXPECT_EQ(3, executionCount); |
| 143 } |
| 144 |
| 145 TEST_F(CancellableTaskFactoryTest, Cancel) |
| 146 { |
| 147 int executionCount = 0; |
| 148 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 149 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 150 factory.cancel(); |
| 151 task->run(); |
| 152 |
| 153 EXPECT_EQ(0, executionCount); |
| 154 } |
| 155 |
| 156 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) |
| 157 { |
| 158 int executionCount = 0; |
| 159 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 160 |
| 161 OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task()); |
| 162 OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task()); |
| 163 |
| 164 taskA->run(); |
| 165 EXPECT_EQ(0, executionCount); |
| 166 |
| 167 taskB->run(); |
| 168 EXPECT_EQ(1, executionCount); |
| 169 } |
| 170 |
| 171 } // namespace |
OLD | NEW |