Chromium Code Reviews| 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(); | |
|
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.
| |
| 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, Run_TasksInSequence) | |
| 120 { | |
| 121 int executionCount = 0; | |
| 122 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | |
| 123 | |
| 124 OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task()); | |
| 125 taskA->run(); | |
| 126 EXPECT_EQ(1, executionCount); | |
| 127 | |
| 128 OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task()); | |
| 129 taskB->run(); | |
| 130 EXPECT_EQ(2, executionCount); | |
| 131 | |
| 132 OwnPtr<WebThread::Task> taskC = adoptPtr(factory.task()); | |
| 133 taskC->run(); | |
| 134 EXPECT_EQ(3, executionCount); | |
| 135 } | |
| 136 | |
| 137 TEST_F(CancellableTaskFactoryTest, Cancel) | |
| 138 { | |
| 139 int executionCount = 0; | |
| 140 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | |
| 141 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); | |
| 142 factory.cancel(); | |
| 143 task->run(); | |
| 144 | |
| 145 EXPECT_EQ(0, executionCount); | |
| 146 } | |
| 147 | |
| 148 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) | |
| 149 { | |
| 150 int executionCount = 0; | |
| 151 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | |
| 152 | |
| 153 OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task()); | |
| 154 OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task()); | |
| 155 | |
| 156 taskA->run(); | |
| 157 EXPECT_EQ(0, executionCount); | |
| 158 | |
| 159 taskB->run(); | |
| 160 EXPECT_EQ(1, executionCount); | |
| 161 } | |
| 162 | |
| 163 } // namespace | |
| OLD | NEW |