Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: Source/platform/scheduler/CancellableTaskFactoryTest.cpp

Issue 1320023009: Limit stack-allocated uses of CancellableTaskFactory(PassOwnPtr<Closure>). (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/platform/scheduler/CancellableTaskFactory.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 namespace {
13
14 class TestCancellableTaskFactory : public CancellableTaskFactory {
15 public:
16 explicit TestCancellableTaskFactory(PassOwnPtr<Closure> closure)
17 : CancellableTaskFactory(closure)
18 {
19 }
20 };
21
22 } // namespace
23
12 using CancellableTaskFactoryTest = testing::Test; 24 using CancellableTaskFactoryTest = testing::Test;
13 25
14 TEST_F(CancellableTaskFactoryTest, IsPending_TaskNotCreated) 26 TEST_F(CancellableTaskFactoryTest, IsPending_TaskNotCreated)
15 { 27 {
16 CancellableTaskFactory factory(nullptr); 28 TestCancellableTaskFactory factory(nullptr);
17 29
18 EXPECT_FALSE(factory.isPending()); 30 EXPECT_FALSE(factory.isPending());
19 } 31 }
20 32
21 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreated) 33 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreated)
22 { 34 {
23 CancellableTaskFactory factory(nullptr); 35 TestCancellableTaskFactory factory(nullptr);
24 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); 36 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate());
25 37
26 EXPECT_TRUE(factory.isPending()); 38 EXPECT_TRUE(factory.isPending());
27 } 39 }
28 40
29 void EmptyFn() 41 void EmptyFn()
30 { 42 {
31 } 43 }
32 44
33 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndRun) 45 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndRun)
34 { 46 {
35 CancellableTaskFactory factory(WTF::bind(&EmptyFn)); 47 TestCancellableTaskFactory factory(WTF::bind(&EmptyFn));
36 { 48 {
37 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); 49 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate());
38 task->run(); 50 task->run();
39 } 51 }
40 52
41 EXPECT_FALSE(factory.isPending()); 53 EXPECT_FALSE(factory.isPending());
42 } 54 }
43 55
44 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndDestroyed) 56 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndDestroyed)
45 { 57 {
46 CancellableTaskFactory factory(nullptr); 58 TestCancellableTaskFactory factory(nullptr);
47 delete factory.cancelAndCreate(); 59 delete factory.cancelAndCreate();
48 60
49 EXPECT_FALSE(factory.isPending()); 61 EXPECT_FALSE(factory.isPending());
50 } 62 }
51 63
52 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndCancelled) 64 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndCancelled)
53 { 65 {
54 CancellableTaskFactory factory(nullptr); 66 TestCancellableTaskFactory factory(nullptr);
55 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); 67 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate());
56 factory.cancel(); 68 factory.cancel();
57 69
58 EXPECT_FALSE(factory.isPending()); 70 EXPECT_FALSE(factory.isPending());
59 } 71 }
60 72
61 class TestClass { 73 class TestClass {
62 public: 74 public:
63 CancellableTaskFactory m_factory; 75 OwnPtr<CancellableTaskFactory> m_factory;
64 76
65 TestClass() 77 TestClass()
66 : m_factory(WTF::bind(&TestClass::TestFn, this)) 78 : m_factory(CancellableTaskFactory::create(this, &TestClass::TestFn))
67 { 79 {
68 } 80 }
69 81
70 void TestFn() 82 void TestFn()
71 { 83 {
72 EXPECT_FALSE(m_factory.isPending()); 84 EXPECT_FALSE(m_factory->isPending());
73 } 85 }
74 }; 86 };
75 87
76 TEST_F(CancellableTaskFactoryTest, IsPending_InCallback) 88 TEST_F(CancellableTaskFactoryTest, IsPending_InCallback)
77 { 89 {
78 TestClass testClass; 90 TestClass testClass;
79 OwnPtr<WebTaskRunner::Task> task = adoptPtr(testClass.m_factory.cancelAndCre ate()); 91 OwnPtr<WebTaskRunner::Task> task = adoptPtr(testClass.m_factory->cancelAndCr eate());
80 task->run(); 92 task->run();
81 } 93 }
82 94
83 void AddOne(int* ptr) 95 void AddOne(int* ptr)
84 { 96 {
85 *ptr += 1; 97 *ptr += 1;
86 } 98 }
87 99
88 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) 100 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted)
89 { 101 {
90 int executionCount = 0; 102 int executionCount = 0;
91 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 103 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
92 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); 104 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate());
93 task->run(); 105 task->run();
94 106
95 EXPECT_EQ(1, executionCount); 107 EXPECT_EQ(1, executionCount);
96 } 108 }
97 109
98 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) 110 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce)
99 { 111 {
100 int executionCount = 0; 112 int executionCount = 0;
101 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 113 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
102 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); 114 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate());
103 task->run(); 115 task->run();
104 task->run(); 116 task->run();
105 task->run(); 117 task->run();
106 task->run(); 118 task->run();
107 119
108 EXPECT_EQ(1, executionCount); 120 EXPECT_EQ(1, executionCount);
109 } 121 }
110 122
111 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) 123 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution)
112 { 124 {
113 int executionCount = 0; 125 int executionCount = 0;
114 OwnPtr<WebTaskRunner::Task> task; 126 OwnPtr<WebTaskRunner::Task> task;
115 { 127 {
116 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 128 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
117 task = adoptPtr(factory.cancelAndCreate()); 129 task = adoptPtr(factory.cancelAndCreate());
118 } 130 }
119 task->run(); 131 task->run();
120 132
121 EXPECT_EQ(0, executionCount); 133 EXPECT_EQ(0, executionCount);
122 } 134 }
123 135
124 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence) 136 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence)
125 { 137 {
126 int executionCount = 0; 138 int executionCount = 0;
127 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 139 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
128 140
129 OwnPtr<WebTaskRunner::Task> taskA = adoptPtr(factory.cancelAndCreate()); 141 OwnPtr<WebTaskRunner::Task> taskA = adoptPtr(factory.cancelAndCreate());
130 taskA->run(); 142 taskA->run();
131 EXPECT_EQ(1, executionCount); 143 EXPECT_EQ(1, executionCount);
132 144
133 OwnPtr<WebTaskRunner::Task> taskB = adoptPtr(factory.cancelAndCreate()); 145 OwnPtr<WebTaskRunner::Task> taskB = adoptPtr(factory.cancelAndCreate());
134 taskB->run(); 146 taskB->run();
135 EXPECT_EQ(2, executionCount); 147 EXPECT_EQ(2, executionCount);
136 148
137 OwnPtr<WebTaskRunner::Task> taskC = adoptPtr(factory.cancelAndCreate()); 149 OwnPtr<WebTaskRunner::Task> taskC = adoptPtr(factory.cancelAndCreate());
138 taskC->run(); 150 taskC->run();
139 EXPECT_EQ(3, executionCount); 151 EXPECT_EQ(3, executionCount);
140 } 152 }
141 153
142 TEST_F(CancellableTaskFactoryTest, Cancel) 154 TEST_F(CancellableTaskFactoryTest, Cancel)
143 { 155 {
144 int executionCount = 0; 156 int executionCount = 0;
145 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 157 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
146 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); 158 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate());
147 factory.cancel(); 159 factory.cancel();
148 task->run(); 160 task->run();
149 161
150 EXPECT_EQ(0, executionCount); 162 EXPECT_EQ(0, executionCount);
151 } 163 }
152 164
153 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) 165 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes)
154 { 166 {
155 int executionCount = 0; 167 int executionCount = 0;
156 CancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 168 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount));
157 169
158 OwnPtr<WebTaskRunner::Task> taskA = adoptPtr(factory.cancelAndCreate()); 170 OwnPtr<WebTaskRunner::Task> taskA = adoptPtr(factory.cancelAndCreate());
159 OwnPtr<WebTaskRunner::Task> taskB = adoptPtr(factory.cancelAndCreate()); 171 OwnPtr<WebTaskRunner::Task> taskB = adoptPtr(factory.cancelAndCreate());
160 172
161 taskA->run(); 173 taskA->run();
162 EXPECT_EQ(0, executionCount); 174 EXPECT_EQ(0, executionCount);
163 175
164 taskB->run(); 176 taskB->run();
165 EXPECT_EQ(1, executionCount); 177 EXPECT_EQ(1, executionCount);
166 } 178 }
167 179
168 } // namespace blink 180 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/scheduler/CancellableTaskFactory.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698