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