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 AddOne(int* ptr) |
| 65 { |
| 66 *ptr += 1; |
| 67 } |
| 68 |
| 69 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) |
| 70 { |
| 71 int executionCount = 0; |
| 72 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 73 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 74 task->run(); |
| 75 |
| 76 EXPECT_EQ(1, executionCount); |
| 77 } |
| 78 |
| 79 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) |
| 80 { |
| 81 int executionCount = 0; |
| 82 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 83 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 84 task->run(); |
| 85 task->run(); |
| 86 task->run(); |
| 87 task->run(); |
| 88 |
| 89 EXPECT_EQ(1, executionCount); |
| 90 } |
| 91 |
| 92 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) |
| 93 { |
| 94 int executionCount = 0; |
| 95 OwnPtr<WebThread::Task> task; |
| 96 { |
| 97 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 98 task = adoptPtr(factory.task()); |
| 99 } |
| 100 task->run(); |
| 101 |
| 102 EXPECT_EQ(0, executionCount); |
| 103 } |
| 104 |
| 105 TEST_F(CancellableTaskFactoryTest, Cancel) |
| 106 { |
| 107 int executionCount = 0; |
| 108 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 109 OwnPtr<WebThread::Task> task = adoptPtr(factory.task()); |
| 110 factory.cancel(); |
| 111 task->run(); |
| 112 |
| 113 EXPECT_EQ(0, executionCount); |
| 114 } |
| 115 |
| 116 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) |
| 117 { |
| 118 int executionCount = 0; |
| 119 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
| 120 |
| 121 OwnPtr<WebThread::Task> taskA = adoptPtr(factory.task()); |
| 122 OwnPtr<WebThread::Task> taskB = adoptPtr(factory.task()); |
| 123 |
| 124 taskA->run(); |
| 125 EXPECT_EQ(0, executionCount); |
| 126 |
| 127 taskB->run(); |
| 128 EXPECT_EQ(1, executionCount); |
| 129 } |
| 130 |
| 131 } // namespace |
OLD | NEW |