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

Side by Side Diff: extensions/browser/api/power/power_api_unittest.cc

Issue 958313002: [CleanUp] Move PowerApiManager to power_api and Rename (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rename to PowerAPI Created 5 years, 9 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
« no previous file with comments | « extensions/browser/api/power/power_api_manager.cc ('k') | extensions/extensions.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/browser/api/power/power_api.h" 5 #include "extensions/browser/api/power/power_api.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <string> 8 #include <string>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "content/public/browser/power_save_blocker.h" 14 #include "content/public/browser/power_save_blocker.h"
15 #include "extensions/browser/api/power/power_api_manager.h"
16 #include "extensions/browser/api_test_utils.h" 15 #include "extensions/browser/api_test_utils.h"
17 #include "extensions/browser/api_unittest.h" 16 #include "extensions/browser/api_unittest.h"
18 #include "extensions/common/extension.h" 17 #include "extensions/common/extension.h"
19 #include "extensions/common/test_util.h" 18 #include "extensions/common/test_util.h"
20 19
21 namespace extensions { 20 namespace extensions {
22 21
23 namespace { 22 namespace {
24 23
25 // Args commonly passed to PowerSaveBlockerStubManager::CallFunction(). 24 // Args commonly passed to PowerSaveBlockerStubManager::CallFunction().
(...skipping 23 matching lines...) Expand all
49 48
50 ~PowerSaveBlockerStub() override { unblock_callback_.Run(); } 49 ~PowerSaveBlockerStub() override { unblock_callback_.Run(); }
51 50
52 private: 51 private:
53 base::Closure unblock_callback_; 52 base::Closure unblock_callback_;
54 53
55 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStub); 54 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStub);
56 }; 55 };
57 56
58 // Manages PowerSaveBlockerStub objects. Tests can instantiate this class 57 // Manages PowerSaveBlockerStub objects. Tests can instantiate this class
59 // to make PowerApiManager's calls to create PowerSaveBlockers record the 58 // to make PowerAPI's calls to create PowerSaveBlockers record the
60 // actions that would've been performed instead of actually blocking and 59 // actions that would've been performed instead of actually blocking and
61 // unblocking power management. 60 // unblocking power management.
62 class PowerSaveBlockerStubManager { 61 class PowerSaveBlockerStubManager {
63 public: 62 public:
64 explicit PowerSaveBlockerStubManager(content::BrowserContext* context) 63 explicit PowerSaveBlockerStubManager(content::BrowserContext* context)
65 : browser_context_(context), 64 : browser_context_(context),
66 weak_ptr_factory_(this) { 65 weak_ptr_factory_(this) {
67 // Use base::Unretained since callbacks with return values can't use 66 // Use base::Unretained since callbacks with return values can't use
68 // weak pointers. 67 // weak pointers.
69 PowerApiManager::Get(browser_context_)->SetCreateBlockerFunctionForTesting( 68 PowerAPI::Get(browser_context_)
70 base::Bind(&PowerSaveBlockerStubManager::CreateStub, 69 ->SetCreateBlockerFunctionForTesting(base::Bind(
71 base::Unretained(this))); 70 &PowerSaveBlockerStubManager::CreateStub, base::Unretained(this)));
72 } 71 }
73 72
74 ~PowerSaveBlockerStubManager() { 73 ~PowerSaveBlockerStubManager() {
75 PowerApiManager::Get(browser_context_)->SetCreateBlockerFunctionForTesting( 74 PowerAPI::Get(browser_context_)
76 PowerApiManager::CreateBlockerFunction()); 75 ->SetCreateBlockerFunctionForTesting(PowerAPI::CreateBlockerFunction());
77 } 76 }
78 77
79 // Removes and returns the first item from |requests_|. Returns NONE if 78 // Removes and returns the first item from |requests_|. Returns NONE if
80 // |requests_| is empty. 79 // |requests_| is empty.
81 Request PopFirstRequest() { 80 Request PopFirstRequest() {
82 if (requests_.empty()) 81 if (requests_.empty())
83 return NONE; 82 return NONE;
84 83
85 Request request = requests_.front(); 84 Request request = requests_.front();
86 requests_.pop_front(); 85 requests_.pop_front();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 // Requests in chronological order. 119 // Requests in chronological order.
121 std::deque<Request> requests_; 120 std::deque<Request> requests_;
122 121
123 base::WeakPtrFactory<PowerSaveBlockerStubManager> weak_ptr_factory_; 122 base::WeakPtrFactory<PowerSaveBlockerStubManager> weak_ptr_factory_;
124 123
125 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStubManager); 124 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStubManager);
126 }; 125 };
127 126
128 } // namespace 127 } // namespace
129 128
130 class PowerApiTest : public ApiUnitTest { 129 class PowerAPITest : public ApiUnitTest {
131 public: 130 public:
132 void SetUp() override { 131 void SetUp() override {
133 ApiUnitTest::SetUp(); 132 ApiUnitTest::SetUp();
134 manager_.reset(new PowerSaveBlockerStubManager(browser_context())); 133 manager_.reset(new PowerSaveBlockerStubManager(browser_context()));
135 } 134 }
136 135
137 void TearDown() override { 136 void TearDown() override {
138 manager_.reset(); 137 manager_.reset();
139 ApiUnitTest::TearDown(); 138 ApiUnitTest::TearDown();
140 } 139 }
(...skipping 14 matching lines...) Expand all
155 scoped_refptr<UIThreadExtensionFunction> function( 154 scoped_refptr<UIThreadExtensionFunction> function(
156 type == REQUEST ? 155 type == REQUEST ?
157 static_cast<UIThreadExtensionFunction*>( 156 static_cast<UIThreadExtensionFunction*>(
158 new PowerRequestKeepAwakeFunction) : 157 new PowerRequestKeepAwakeFunction) :
159 static_cast<UIThreadExtensionFunction*>( 158 static_cast<UIThreadExtensionFunction*>(
160 new PowerReleaseKeepAwakeFunction)); 159 new PowerReleaseKeepAwakeFunction));
161 function->set_extension(extension); 160 function->set_extension(extension);
162 return api_test_utils::RunFunction(function.get(), args, browser_context()); 161 return api_test_utils::RunFunction(function.get(), args, browser_context());
163 } 162 }
164 163
165 // Send a notification to PowerApiManager saying that |extension| has 164 // Send a notification to PowerAPI saying that |extension| has
166 // been unloaded. 165 // been unloaded.
167 void UnloadExtension(const extensions::Extension* extension) { 166 void UnloadExtension(const extensions::Extension* extension) {
168 PowerApiManager::Get(browser_context())->OnExtensionUnloaded( 167 PowerAPI::Get(browser_context())
169 browser_context(), extension, UnloadedExtensionInfo::REASON_UNINSTALL); 168 ->OnExtensionUnloaded(browser_context(), extension,
169 UnloadedExtensionInfo::REASON_UNINSTALL);
170 } 170 }
171 171
172 scoped_ptr<PowerSaveBlockerStubManager> manager_; 172 scoped_ptr<PowerSaveBlockerStubManager> manager_;
173 }; 173 };
174 174
175 TEST_F(PowerApiTest, RequestAndRelease) { 175 TEST_F(PowerAPITest, RequestAndRelease) {
176 // Simulate an extension making and releasing a "display" request and a 176 // Simulate an extension making and releasing a "display" request and a
177 // "system" request. 177 // "system" request.
178 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); 178 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
179 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 179 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
180 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 180 EXPECT_EQ(NONE, manager_->PopFirstRequest());
181 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); 181 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
182 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 182 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
183 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 183 EXPECT_EQ(NONE, manager_->PopFirstRequest());
184 184
185 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension())); 185 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
186 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 186 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
187 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 187 EXPECT_EQ(NONE, manager_->PopFirstRequest());
188 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); 188 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
189 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 189 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
190 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 190 EXPECT_EQ(NONE, manager_->PopFirstRequest());
191 } 191 }
192 192
193 TEST_F(PowerApiTest, RequestWithoutRelease) { 193 TEST_F(PowerAPITest, RequestWithoutRelease) {
194 // Simulate an extension calling requestKeepAwake() without calling 194 // Simulate an extension calling requestKeepAwake() without calling
195 // releaseKeepAwake(). The override should be automatically removed when 195 // releaseKeepAwake(). The override should be automatically removed when
196 // the extension is unloaded. 196 // the extension is unloaded.
197 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); 197 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
198 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 198 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
199 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 199 EXPECT_EQ(NONE, manager_->PopFirstRequest());
200 200
201 UnloadExtension(extension()); 201 UnloadExtension(extension());
202 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 202 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
203 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 203 EXPECT_EQ(NONE, manager_->PopFirstRequest());
204 } 204 }
205 205
206 TEST_F(PowerApiTest, ReleaseWithoutRequest) { 206 TEST_F(PowerAPITest, ReleaseWithoutRequest) {
207 // Simulate an extension calling releaseKeepAwake() without having 207 // Simulate an extension calling releaseKeepAwake() without having
208 // calling requestKeepAwake() earlier. The call should be ignored. 208 // calling requestKeepAwake() earlier. The call should be ignored.
209 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); 209 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
210 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 210 EXPECT_EQ(NONE, manager_->PopFirstRequest());
211 } 211 }
212 212
213 TEST_F(PowerApiTest, UpgradeRequest) { 213 TEST_F(PowerAPITest, UpgradeRequest) {
214 // Simulate an extension calling requestKeepAwake("system") and then 214 // Simulate an extension calling requestKeepAwake("system") and then
215 // requestKeepAwake("display"). When the second call is made, a 215 // requestKeepAwake("display"). When the second call is made, a
216 // display-sleep-blocking request should be made before the initial 216 // display-sleep-blocking request should be made before the initial
217 // app-suspension-blocking request is released. 217 // app-suspension-blocking request is released.
218 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension())); 218 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
219 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 219 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
220 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 220 EXPECT_EQ(NONE, manager_->PopFirstRequest());
221 221
222 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); 222 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
223 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 223 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
224 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 224 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
225 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 225 EXPECT_EQ(NONE, manager_->PopFirstRequest());
226 226
227 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); 227 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
228 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 228 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
229 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 229 EXPECT_EQ(NONE, manager_->PopFirstRequest());
230 } 230 }
231 231
232 TEST_F(PowerApiTest, DowngradeRequest) { 232 TEST_F(PowerAPITest, DowngradeRequest) {
233 // Simulate an extension calling requestKeepAwake("display") and then 233 // Simulate an extension calling requestKeepAwake("display") and then
234 // requestKeepAwake("system"). When the second call is made, an 234 // requestKeepAwake("system"). When the second call is made, an
235 // app-suspension-blocking request should be made before the initial 235 // app-suspension-blocking request should be made before the initial
236 // display-sleep-blocking request is released. 236 // display-sleep-blocking request is released.
237 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); 237 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
238 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 238 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
239 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 239 EXPECT_EQ(NONE, manager_->PopFirstRequest());
240 240
241 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension())); 241 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
242 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 242 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
243 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 243 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
244 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 244 EXPECT_EQ(NONE, manager_->PopFirstRequest());
245 245
246 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); 246 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
247 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 247 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
248 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 248 EXPECT_EQ(NONE, manager_->PopFirstRequest());
249 } 249 }
250 250
251 TEST_F(PowerApiTest, MultipleExtensions) { 251 TEST_F(PowerAPITest, MultipleExtensions) {
252 // Simulate an extension blocking the display from sleeping. 252 // Simulate an extension blocking the display from sleeping.
253 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); 253 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
254 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 254 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
255 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 255 EXPECT_EQ(NONE, manager_->PopFirstRequest());
256 256
257 // Create a second extension that blocks system suspend. No additional 257 // Create a second extension that blocks system suspend. No additional
258 // PowerSaveBlocker is needed; the blocker from the first extension 258 // PowerSaveBlocker is needed; the blocker from the first extension
259 // already covers the behavior requested by the second extension. 259 // already covers the behavior requested by the second extension.
260 scoped_refptr<Extension> extension2(test_util::CreateEmptyExtension("id2")); 260 scoped_refptr<Extension> extension2(test_util::CreateEmptyExtension("id2"));
261 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension2.get())); 261 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension2.get()));
262 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 262 EXPECT_EQ(NONE, manager_->PopFirstRequest());
263 263
264 // When the first extension is unloaded, a new app-suspension blocker 264 // When the first extension is unloaded, a new app-suspension blocker
265 // should be created before the display-sleep blocker is destroyed. 265 // should be created before the display-sleep blocker is destroyed.
266 UnloadExtension(extension()); 266 UnloadExtension(extension());
267 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 267 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
268 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 268 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
269 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 269 EXPECT_EQ(NONE, manager_->PopFirstRequest());
270 270
271 // Make the first extension block display-sleep again. 271 // Make the first extension block display-sleep again.
272 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); 272 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
273 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 273 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
274 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 274 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
275 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 275 EXPECT_EQ(NONE, manager_->PopFirstRequest());
276 } 276 }
277 277
278 } // namespace extensions 278 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/power/power_api_manager.cc ('k') | extensions/extensions.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698