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

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

Issue 554963002: Move extensions power API unit test to extensions_unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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
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 "chrome/browser/extensions/extension_function_test_utils.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "content/public/browser/power_save_blocker.h" 14 #include "content/public/browser/power_save_blocker.h"
17 #include "extensions/browser/api/power/power_api_manager.h" 15 #include "extensions/browser/api/power/power_api_manager.h"
16 #include "extensions/browser/api_test_utils.h"
17 #include "extensions/browser/api_unittest.h"
18 #include "extensions/common/extension.h" 18 #include "extensions/common/extension.h"
19 19 #include "extensions/common/test_util.h"
20 namespace utils = extension_function_test_utils;
21 20
22 namespace extensions { 21 namespace extensions {
23 22
24 namespace { 23 namespace {
25 24
26 // Args commonly passed to PowerSaveBlockerStubManager::CallFunction(). 25 // Args commonly passed to PowerSaveBlockerStubManager::CallFunction().
27 const char kDisplayArgs[] = "[\"display\"]"; 26 const char kDisplayArgs[] = "[\"display\"]";
28 const char kSystemArgs[] = "[\"system\"]"; 27 const char kSystemArgs[] = "[\"system\"]";
29 const char kEmptyArgs[] = "[]"; 28 const char kEmptyArgs[] = "[]";
30 29
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 // Requests in chronological order. 121 // Requests in chronological order.
123 std::deque<Request> requests_; 122 std::deque<Request> requests_;
124 123
125 base::WeakPtrFactory<PowerSaveBlockerStubManager> weak_ptr_factory_; 124 base::WeakPtrFactory<PowerSaveBlockerStubManager> weak_ptr_factory_;
126 125
127 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStubManager); 126 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStubManager);
128 }; 127 };
129 128
130 } // namespace 129 } // namespace
131 130
132 class PowerApiTest : public BrowserWithTestWindowTest { 131 class PowerApiTest : public ApiUnitTest {
133 public: 132 public:
134 virtual void SetUp() OVERRIDE { 133 virtual void SetUp() OVERRIDE {
135 BrowserWithTestWindowTest::SetUp(); 134 ApiUnitTest::SetUp();
136 manager_.reset(new PowerSaveBlockerStubManager(profile())); 135 manager_.reset(new PowerSaveBlockerStubManager(browser_context()));
137 extension_ = utils::CreateEmptyExtensionWithLocation(
138 extensions::Manifest::UNPACKED);
139 } 136 }
140 137
141 virtual void TearDown() OVERRIDE { 138 virtual void TearDown() OVERRIDE {
142 extension_ = NULL;
143 manager_.reset(); 139 manager_.reset();
144 BrowserWithTestWindowTest::TearDown(); 140 ApiUnitTest::TearDown();
145 } 141 }
146 142
147 protected: 143 protected:
148 // Shorthand for PowerRequestKeepAwakeFunction and 144 // Shorthand for PowerRequestKeepAwakeFunction and
149 // PowerReleaseKeepAwakeFunction. 145 // PowerReleaseKeepAwakeFunction.
150 enum FunctionType { 146 enum FunctionType {
151 REQUEST, 147 REQUEST,
152 RELEASE, 148 RELEASE,
153 }; 149 };
154 150
155 // Calls the function described by |type| with |args|, a JSON list of 151 // Calls the function described by |type| with |args|, a JSON list of
156 // arguments, on behalf of |extension|. 152 // arguments, on behalf of |extension|.
157 bool CallFunction(FunctionType type, 153 bool CallFunction(FunctionType type,
158 const std::string& args, 154 const std::string& args,
159 extensions::Extension* extension) { 155 const extensions::Extension* extension) {
160 scoped_refptr<UIThreadExtensionFunction> function( 156 scoped_refptr<UIThreadExtensionFunction> function(
161 type == REQUEST ? 157 type == REQUEST ?
162 static_cast<UIThreadExtensionFunction*>( 158 static_cast<UIThreadExtensionFunction*>(
163 new PowerRequestKeepAwakeFunction) : 159 new PowerRequestKeepAwakeFunction) :
164 static_cast<UIThreadExtensionFunction*>( 160 static_cast<UIThreadExtensionFunction*>(
165 new PowerReleaseKeepAwakeFunction)); 161 new PowerReleaseKeepAwakeFunction));
166 function->set_extension(extension); 162 function->set_extension(extension);
167 return utils::RunFunction(function.get(), args, browser(), utils::NONE); 163 return api_test_utils::RunFunction(function.get(), args, browser_context());
168 } 164 }
169 165
170 // Send a notification to PowerApiManager saying that |extension| has 166 // Send a notification to PowerApiManager saying that |extension| has
171 // been unloaded. 167 // been unloaded.
172 void UnloadExtension(extensions::Extension* extension) { 168 void UnloadExtension(const extensions::Extension* extension) {
173 PowerApiManager::Get(profile())->OnExtensionUnloaded( 169 PowerApiManager::Get(browser_context())->OnExtensionUnloaded(
174 profile(), extension, UnloadedExtensionInfo::REASON_UNINSTALL); 170 browser_context(), extension, UnloadedExtensionInfo::REASON_UNINSTALL);
175 } 171 }
176 172
177 scoped_ptr<PowerSaveBlockerStubManager> manager_; 173 scoped_ptr<PowerSaveBlockerStubManager> manager_;
178 scoped_refptr<extensions::Extension> extension_;
179 }; 174 };
180 175
181 TEST_F(PowerApiTest, RequestAndRelease) { 176 TEST_F(PowerApiTest, RequestAndRelease) {
182 // Simulate an extension making and releasing a "display" request and a 177 // Simulate an extension making and releasing a "display" request and a
183 // "system" request. 178 // "system" request.
184 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); 179 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
185 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 180 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
186 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 181 EXPECT_EQ(NONE, manager_->PopFirstRequest());
187 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); 182 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
188 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 183 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
189 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 184 EXPECT_EQ(NONE, manager_->PopFirstRequest());
190 185
191 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get())); 186 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
192 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 187 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
193 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 188 EXPECT_EQ(NONE, manager_->PopFirstRequest());
194 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); 189 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
195 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 190 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
196 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 191 EXPECT_EQ(NONE, manager_->PopFirstRequest());
197 } 192 }
198 193
199 TEST_F(PowerApiTest, RequestWithoutRelease) { 194 TEST_F(PowerApiTest, RequestWithoutRelease) {
200 // Simulate an extension calling requestKeepAwake() without calling 195 // Simulate an extension calling requestKeepAwake() without calling
201 // releaseKeepAwake(). The override should be automatically removed when 196 // releaseKeepAwake(). The override should be automatically removed when
202 // the extension is unloaded. 197 // the extension is unloaded.
203 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); 198 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
204 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 199 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
205 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 200 EXPECT_EQ(NONE, manager_->PopFirstRequest());
206 201
207 UnloadExtension(extension_.get()); 202 UnloadExtension(extension());
208 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 203 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
209 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 204 EXPECT_EQ(NONE, manager_->PopFirstRequest());
210 } 205 }
211 206
212 TEST_F(PowerApiTest, ReleaseWithoutRequest) { 207 TEST_F(PowerApiTest, ReleaseWithoutRequest) {
213 // Simulate an extension calling releaseKeepAwake() without having 208 // Simulate an extension calling releaseKeepAwake() without having
214 // calling requestKeepAwake() earlier. The call should be ignored. 209 // calling requestKeepAwake() earlier. The call should be ignored.
215 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); 210 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
216 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 211 EXPECT_EQ(NONE, manager_->PopFirstRequest());
217 } 212 }
218 213
219 TEST_F(PowerApiTest, UpgradeRequest) { 214 TEST_F(PowerApiTest, UpgradeRequest) {
220 // Simulate an extension calling requestKeepAwake("system") and then 215 // Simulate an extension calling requestKeepAwake("system") and then
221 // requestKeepAwake("display"). When the second call is made, a 216 // requestKeepAwake("display"). When the second call is made, a
222 // display-sleep-blocking request should be made before the initial 217 // display-sleep-blocking request should be made before the initial
223 // app-suspension-blocking request is released. 218 // app-suspension-blocking request is released.
224 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get())); 219 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
225 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 220 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
226 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 221 EXPECT_EQ(NONE, manager_->PopFirstRequest());
227 222
228 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); 223 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
229 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 224 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
230 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 225 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
231 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 226 EXPECT_EQ(NONE, manager_->PopFirstRequest());
232 227
233 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); 228 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
234 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 229 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
235 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 230 EXPECT_EQ(NONE, manager_->PopFirstRequest());
236 } 231 }
237 232
238 TEST_F(PowerApiTest, DowngradeRequest) { 233 TEST_F(PowerApiTest, DowngradeRequest) {
239 // Simulate an extension calling requestKeepAwake("display") and then 234 // Simulate an extension calling requestKeepAwake("display") and then
240 // requestKeepAwake("system"). When the second call is made, an 235 // requestKeepAwake("system"). When the second call is made, an
241 // app-suspension-blocking request should be made before the initial 236 // app-suspension-blocking request should be made before the initial
242 // display-sleep-blocking request is released. 237 // display-sleep-blocking request is released.
243 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); 238 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
244 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 239 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
245 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 240 EXPECT_EQ(NONE, manager_->PopFirstRequest());
246 241
247 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get())); 242 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
248 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 243 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
249 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 244 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
250 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 245 EXPECT_EQ(NONE, manager_->PopFirstRequest());
251 246
252 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); 247 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
253 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 248 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
254 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 249 EXPECT_EQ(NONE, manager_->PopFirstRequest());
255 } 250 }
256 251
257 TEST_F(PowerApiTest, MultipleExtensions) { 252 TEST_F(PowerApiTest, MultipleExtensions) {
258 // Simulate an extension blocking the display from sleeping. 253 // Simulate an extension blocking the display from sleeping.
259 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); 254 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
260 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 255 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
261 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 256 EXPECT_EQ(NONE, manager_->PopFirstRequest());
262 257
263 // Create a second extension that blocks system suspend. No additional 258 // Create a second extension that blocks system suspend. No additional
264 // PowerSaveBlocker is needed; the blocker from the first extension 259 // PowerSaveBlocker is needed; the blocker from the first extension
265 // already covers the behavior requested by the second extension. 260 // already covers the behavior requested by the second extension.
266 scoped_ptr<base::DictionaryValue> extension_value( 261 scoped_refptr<Extension> extension2(test_util::CreateExtensionWithID("id2"));
267 utils::ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
268 scoped_refptr<extensions::Extension> extension2(
269 utils::CreateExtension(extensions::Manifest::UNPACKED,
270 extension_value.get(), "second_extension"));
271 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension2.get())); 262 ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension2.get()));
272 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 263 EXPECT_EQ(NONE, manager_->PopFirstRequest());
273 264
274 // When the first extension is unloaded, a new app-suspension blocker 265 // When the first extension is unloaded, a new app-suspension blocker
275 // should be created before the display-sleep blocker is destroyed. 266 // should be created before the display-sleep blocker is destroyed.
276 UnloadExtension(extension_.get()); 267 UnloadExtension(extension());
277 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 268 EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
278 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 269 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
279 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 270 EXPECT_EQ(NONE, manager_->PopFirstRequest());
280 271
281 // Make the first extension block display-sleep again. 272 // Make the first extension block display-sleep again.
282 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); 273 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
283 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); 274 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
284 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); 275 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
285 EXPECT_EQ(NONE, manager_->PopFirstRequest()); 276 EXPECT_EQ(NONE, manager_->PopFirstRequest());
286 } 277 }
287 278
288 } // namespace extensions 279 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698