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