| 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 <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 Loading... |
| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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()); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| OLD | NEW |