| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/scoped_ptr.h" | |
| 6 #include "chrome/browser/chromeos/extensions/power/power_api.h" | |
| 7 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
| 8 #include "chrome/common/extensions/extension_test_util.h" | |
| 9 #include "chrome/common/extensions/manifest.h" | |
| 10 #include "chrome/test/base/in_process_browser_test.h" | |
| 11 #include "chrome/test/base/ui_test_utils.h" | |
| 12 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
| 13 #include "chromeos/dbus/mock_power_manager_client.h" | |
| 14 #include "chromeos/dbus/mock_update_engine_client.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using ::testing::_; | |
| 18 using ::testing::Return; | |
| 19 | |
| 20 namespace extensions { | |
| 21 | |
| 22 class PowerApiTest : public InProcessBrowserTest { | |
| 23 public: | |
| 24 PowerApiTest() {} | |
| 25 virtual ~PowerApiTest() {} | |
| 26 | |
| 27 virtual void SetUp() OVERRIDE { | |
| 28 chromeos::MockDBusThreadManager* mock_dbus_thread_manager = | |
| 29 new chromeos::MockDBusThreadManager; | |
| 30 | |
| 31 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) | |
| 32 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); | |
| 33 EXPECT_CALL(*mock_dbus_thread_manager->mock_update_engine_client(), | |
| 34 GetLastStatus()) | |
| 35 .Times(1) | |
| 36 .WillOnce(Return(chromeos::MockUpdateEngineClient::Status())); | |
| 37 chromeos::DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | |
| 38 power_client_ = mock_dbus_thread_manager->mock_power_manager_client(); | |
| 39 | |
| 40 InProcessBrowserTest::SetUp(); | |
| 41 } | |
| 42 | |
| 43 virtual void TearDown() OVERRIDE { | |
| 44 chromeos::DBusThreadManager::Shutdown(); | |
| 45 | |
| 46 InProcessBrowserTest::TearDown(); | |
| 47 } | |
| 48 | |
| 49 protected: | |
| 50 static const int kRequestId = 5; // arbitrary | |
| 51 | |
| 52 scoped_refptr<Extension> CreateExtensionWithId(const std::string& id) { | |
| 53 return extension_test_util::CreateExtensionWithID( | |
| 54 extension_test_util::MakeId(id)); | |
| 55 } | |
| 56 | |
| 57 void RunFunctionAndExpectPass(UIThreadExtensionFunction* function, | |
| 58 Extension* extension) { | |
| 59 function->set_extension(extension); | |
| 60 function->set_has_callback(true); | |
| 61 | |
| 62 scoped_ptr<base::Value> result( | |
| 63 extension_function_test_utils::RunFunctionAndReturnSingleResult( | |
| 64 function, "[]", browser())); | |
| 65 | |
| 66 ASSERT_TRUE(result.get() != NULL); | |
| 67 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 68 bool boolean_value; | |
| 69 result->GetAsBoolean(&boolean_value); | |
| 70 EXPECT_EQ(boolean_value, true); | |
| 71 | |
| 72 MessageLoop::current()->RunUntilIdle(); | |
| 73 } | |
| 74 | |
| 75 // Adds an expectation that RequestPowerStateOverrides() will be called once | |
| 76 // to create a new override. The callback is stored in | |
| 77 // |request_id_callback_|, which should be invoked with |kRequestId|. | |
| 78 void AddRequestPowerStateOverridesExpectation() { | |
| 79 EXPECT_CALL(*power_client_, RequestPowerStateOverrides(0, _, _, _)) | |
| 80 .WillOnce(testing::SaveArg<3>(&request_id_callback_)); | |
| 81 } | |
| 82 | |
| 83 // Adds an expectation that CancelPowerStateOverrides() will be called once to | |
| 84 // cancel an override previously created via | |
| 85 // AddRequestPowerStateOverridesExpectation(). | |
| 86 void AddCancelPowerStateOverridesExpectation() { | |
| 87 EXPECT_CALL(*power_client_, CancelPowerStateOverrides(kRequestId)).Times(1); | |
| 88 } | |
| 89 | |
| 90 void RequestKeepAwake(scoped_refptr<Extension> extension) { | |
| 91 scoped_refptr<PowerRequestKeepAwakeFunction> function( | |
| 92 new PowerRequestKeepAwakeFunction); | |
| 93 RunFunctionAndExpectPass(function.get(), extension); | |
| 94 } | |
| 95 | |
| 96 void ReleaseKeepAwake(scoped_refptr<Extension> extension) { | |
| 97 scoped_refptr<PowerReleaseKeepAwakeFunction> function( | |
| 98 new PowerReleaseKeepAwakeFunction); | |
| 99 RunFunctionAndExpectPass(function.get(), extension); | |
| 100 } | |
| 101 | |
| 102 chromeos::MockPowerManagerClient* power_client_; | |
| 103 | |
| 104 chromeos::PowerStateRequestIdCallback request_id_callback_; | |
| 105 | |
| 106 private: | |
| 107 DISALLOW_COPY_AND_ASSIGN(PowerApiTest); | |
| 108 }; | |
| 109 | |
| 110 IN_PROC_BROWSER_TEST_F(PowerApiTest, RequestAndRelease) { | |
| 111 scoped_refptr<Extension> extension(CreateExtensionWithId("0")); | |
| 112 AddRequestPowerStateOverridesExpectation(); | |
| 113 RequestKeepAwake(extension); | |
| 114 request_id_callback_.Run(kRequestId); | |
| 115 | |
| 116 AddCancelPowerStateOverridesExpectation(); | |
| 117 ReleaseKeepAwake(extension); | |
| 118 } | |
| 119 | |
| 120 IN_PROC_BROWSER_TEST_F(PowerApiTest, RequestMultipleAndReleaseOne) { | |
| 121 scoped_refptr<Extension> extension1(CreateExtensionWithId("1")); | |
| 122 scoped_refptr<Extension> extension2(CreateExtensionWithId("2")); | |
| 123 | |
| 124 AddRequestPowerStateOverridesExpectation(); | |
| 125 RequestKeepAwake(extension1); | |
| 126 RequestKeepAwake(extension2); | |
| 127 RequestKeepAwake(extension1); | |
| 128 request_id_callback_.Run(kRequestId); | |
| 129 | |
| 130 AddCancelPowerStateOverridesExpectation(); | |
| 131 ReleaseKeepAwake(extension1); | |
| 132 } | |
| 133 | |
| 134 IN_PROC_BROWSER_TEST_F(PowerApiTest, RequestOneAndReleaseMultiple) { | |
| 135 scoped_refptr<Extension> extension(CreateExtensionWithId("3")); | |
| 136 | |
| 137 AddRequestPowerStateOverridesExpectation(); | |
| 138 RequestKeepAwake(extension); | |
| 139 request_id_callback_.Run(kRequestId); | |
| 140 | |
| 141 AddCancelPowerStateOverridesExpectation(); | |
| 142 ReleaseKeepAwake(extension); | |
| 143 ReleaseKeepAwake(extension); | |
| 144 ReleaseKeepAwake(extension); | |
| 145 } | |
| 146 | |
| 147 IN_PROC_BROWSER_TEST_F(PowerApiTest, RequestMultipleAndReleaseAll) { | |
| 148 scoped_refptr<Extension> extension1(CreateExtensionWithId("4")); | |
| 149 scoped_refptr<Extension> extension2(CreateExtensionWithId("5")); | |
| 150 scoped_refptr<Extension> extension3(CreateExtensionWithId("6")); | |
| 151 | |
| 152 AddRequestPowerStateOverridesExpectation(); | |
| 153 RequestKeepAwake(extension1); | |
| 154 RequestKeepAwake(extension2); | |
| 155 RequestKeepAwake(extension3); | |
| 156 request_id_callback_.Run(kRequestId); | |
| 157 | |
| 158 AddCancelPowerStateOverridesExpectation(); | |
| 159 ReleaseKeepAwake(extension3); | |
| 160 ReleaseKeepAwake(extension1); | |
| 161 ReleaseKeepAwake(extension2); | |
| 162 } | |
| 163 | |
| 164 // TODO(rkc): Add another test to verify a Request->Release->Request scenario. | |
| 165 | |
| 166 } // namespace extensions | |
| OLD | NEW |