| OLD | NEW |
| 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 <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.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_test_utils.h" | 15 #include "extensions/browser/api_test_utils.h" |
| 16 #include "extensions/browser/api_unittest.h" | 16 #include "extensions/browser/api_unittest.h" |
| 17 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 18 #include "extensions/common/test_util.h" | 18 #include "extensions/common/test_util.h" |
| 19 | 19 |
| 20 namespace extensions { | 20 namespace extensions { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 if (requests_.empty()) | 81 if (requests_.empty()) |
| 82 return NONE; | 82 return NONE; |
| 83 | 83 |
| 84 Request request = requests_.front(); | 84 Request request = requests_.front(); |
| 85 requests_.pop_front(); | 85 requests_.pop_front(); |
| 86 return request; | 86 return request; |
| 87 } | 87 } |
| 88 | 88 |
| 89 private: | 89 private: |
| 90 // Creates a new PowerSaveBlockerStub of type |type|. | 90 // Creates a new PowerSaveBlockerStub of type |type|. |
| 91 scoped_ptr<content::PowerSaveBlocker> CreateStub( | 91 std::unique_ptr<content::PowerSaveBlocker> CreateStub( |
| 92 content::PowerSaveBlocker::PowerSaveBlockerType type, | 92 content::PowerSaveBlocker::PowerSaveBlockerType type, |
| 93 content::PowerSaveBlocker::Reason reason, | 93 content::PowerSaveBlocker::Reason reason, |
| 94 const std::string& description) { | 94 const std::string& description) { |
| 95 Request unblock_request = NONE; | 95 Request unblock_request = NONE; |
| 96 switch (type) { | 96 switch (type) { |
| 97 case content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension: | 97 case content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension: |
| 98 requests_.push_back(BLOCK_APP_SUSPENSION); | 98 requests_.push_back(BLOCK_APP_SUSPENSION); |
| 99 unblock_request = UNBLOCK_APP_SUSPENSION; | 99 unblock_request = UNBLOCK_APP_SUSPENSION; |
| 100 break; | 100 break; |
| 101 case content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep: | 101 case content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep: |
| 102 requests_.push_back(BLOCK_DISPLAY_SLEEP); | 102 requests_.push_back(BLOCK_DISPLAY_SLEEP); |
| 103 unblock_request = UNBLOCK_DISPLAY_SLEEP; | 103 unblock_request = UNBLOCK_DISPLAY_SLEEP; |
| 104 break; | 104 break; |
| 105 } | 105 } |
| 106 return scoped_ptr<content::PowerSaveBlocker>( | 106 return std::unique_ptr<content::PowerSaveBlocker>(new PowerSaveBlockerStub( |
| 107 new PowerSaveBlockerStub( | 107 base::Bind(&PowerSaveBlockerStubManager::AppendRequest, |
| 108 base::Bind(&PowerSaveBlockerStubManager::AppendRequest, | 108 weak_ptr_factory_.GetWeakPtr(), unblock_request))); |
| 109 weak_ptr_factory_.GetWeakPtr(), | |
| 110 unblock_request))); | |
| 111 } | 109 } |
| 112 | 110 |
| 113 void AppendRequest(Request request) { | 111 void AppendRequest(Request request) { |
| 114 requests_.push_back(request); | 112 requests_.push_back(request); |
| 115 } | 113 } |
| 116 | 114 |
| 117 content::BrowserContext* browser_context_; | 115 content::BrowserContext* browser_context_; |
| 118 | 116 |
| 119 // Requests in chronological order. | 117 // Requests in chronological order. |
| 120 std::deque<Request> requests_; | 118 std::deque<Request> requests_; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 } | 160 } |
| 163 | 161 |
| 164 // Send a notification to PowerAPI saying that |extension| has | 162 // Send a notification to PowerAPI saying that |extension| has |
| 165 // been unloaded. | 163 // been unloaded. |
| 166 void UnloadExtension(const extensions::Extension* extension) { | 164 void UnloadExtension(const extensions::Extension* extension) { |
| 167 PowerAPI::Get(browser_context()) | 165 PowerAPI::Get(browser_context()) |
| 168 ->OnExtensionUnloaded(browser_context(), extension, | 166 ->OnExtensionUnloaded(browser_context(), extension, |
| 169 UnloadedExtensionInfo::REASON_UNINSTALL); | 167 UnloadedExtensionInfo::REASON_UNINSTALL); |
| 170 } | 168 } |
| 171 | 169 |
| 172 scoped_ptr<PowerSaveBlockerStubManager> manager_; | 170 std::unique_ptr<PowerSaveBlockerStubManager> manager_; |
| 173 }; | 171 }; |
| 174 | 172 |
| 175 TEST_F(PowerAPITest, RequestAndRelease) { | 173 TEST_F(PowerAPITest, RequestAndRelease) { |
| 176 // Simulate an extension making and releasing a "display" request and a | 174 // Simulate an extension making and releasing a "display" request and a |
| 177 // "system" request. | 175 // "system" request. |
| 178 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); | 176 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); |
| 179 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); | 177 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); |
| 180 EXPECT_EQ(NONE, manager_->PopFirstRequest()); | 178 EXPECT_EQ(NONE, manager_->PopFirstRequest()); |
| 181 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); | 179 ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension())); |
| 182 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); | 180 EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 EXPECT_EQ(NONE, manager_->PopFirstRequest()); | 267 EXPECT_EQ(NONE, manager_->PopFirstRequest()); |
| 270 | 268 |
| 271 // Make the first extension block display-sleep again. | 269 // Make the first extension block display-sleep again. |
| 272 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); | 270 ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension())); |
| 273 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); | 271 EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); |
| 274 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); | 272 EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); |
| 275 EXPECT_EQ(NONE, manager_->PopFirstRequest()); | 273 EXPECT_EQ(NONE, manager_->PopFirstRequest()); |
| 276 } | 274 } |
| 277 | 275 |
| 278 } // namespace extensions | 276 } // namespace extensions |
| OLD | NEW |