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