| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chromeos/dbus/power_manager_client.h" | 5 #include "chromeos/dbus/power_manager_client.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "chromeos/dbus/power_manager/suspend.pb.h" | 16 #include "chromeos/dbus/power_manager/suspend.pb.h" |
| 17 #include "dbus/mock_bus.h" | 17 #include "dbus/mock_bus.h" |
| 18 #include "dbus/mock_object_proxy.h" | 18 #include "dbus/mock_object_proxy.h" |
| 19 #include "dbus/object_path.h" | 19 #include "dbus/object_path.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/cros_system_api/dbus/service_constants.h" | 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 23 | 23 |
| 24 using ::testing::_; | 24 using ::testing::_; |
| 25 using ::testing::Return; | 25 using ::testing::Return; |
| 26 using ::testing::SaveArg; |
| 26 | 27 |
| 27 namespace chromeos { | 28 namespace chromeos { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 // Shorthand for a few commonly-used constants. | 32 // Shorthand for a few commonly-used constants. |
| 32 const char* kInterface = power_manager::kPowerManagerInterface; | 33 const char* kInterface = power_manager::kPowerManagerInterface; |
| 33 const char* kSuspendImminent = power_manager::kSuspendImminentSignal; | 34 const char* kSuspendImminent = power_manager::kSuspendImminentSignal; |
| 34 const char* kHandleSuspendReadiness = | 35 const char* kHandleSuspendReadiness = |
| 35 power_manager::kHandleSuspendReadinessMethod; | 36 power_manager::kHandleSuspendReadinessMethod; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 dbus::ObjectPath(power_manager::kPowerManagerServicePath)); | 170 dbus::ObjectPath(power_manager::kPowerManagerServicePath)); |
| 170 | 171 |
| 171 // |client_|'s Init() method should request a proxy for communicating with | 172 // |client_|'s Init() method should request a proxy for communicating with |
| 172 // powerd. | 173 // powerd. |
| 173 EXPECT_CALL(*bus_.get(), | 174 EXPECT_CALL(*bus_.get(), |
| 174 GetObjectProxy( | 175 GetObjectProxy( |
| 175 power_manager::kPowerManagerServiceName, | 176 power_manager::kPowerManagerServiceName, |
| 176 dbus::ObjectPath(power_manager::kPowerManagerServicePath))) | 177 dbus::ObjectPath(power_manager::kPowerManagerServicePath))) |
| 177 .WillRepeatedly(Return(proxy_.get())); | 178 .WillRepeatedly(Return(proxy_.get())); |
| 178 | 179 |
| 179 // Save |client_|'s signal callbacks. | 180 // Save |client_|'s signal and name-owner-changed callbacks. |
| 180 EXPECT_CALL(*proxy_.get(), ConnectToSignal(kInterface, _, _, _)) | 181 EXPECT_CALL(*proxy_.get(), ConnectToSignal(kInterface, _, _, _)) |
| 181 .WillRepeatedly(Invoke(this, &PowerManagerClientTest::ConnectToSignal)); | 182 .WillRepeatedly(Invoke(this, &PowerManagerClientTest::ConnectToSignal)); |
| 183 EXPECT_CALL(*proxy_.get(), SetNameOwnerChangedCallback(_)) |
| 184 .WillRepeatedly(SaveArg<0>(&name_owner_changed_callback_)); |
| 182 | 185 |
| 183 // |client_|'s Init() method should register regular and dark suspend | 186 // |client_|'s Init() method should register regular and dark suspend |
| 184 // delays. | 187 // delays. |
| 185 EXPECT_CALL( | 188 EXPECT_CALL( |
| 186 *proxy_.get(), | 189 *proxy_.get(), |
| 187 CallMethod(HasMember(power_manager::kRegisterSuspendDelayMethod), _, _)) | 190 CallMethod(HasMember(power_manager::kRegisterSuspendDelayMethod), _, _)) |
| 188 .WillRepeatedly( | 191 .WillRepeatedly( |
| 189 Invoke(this, &PowerManagerClientTest::RegisterSuspendDelay)); | 192 Invoke(this, &PowerManagerClientTest::RegisterSuspendDelay)); |
| 190 EXPECT_CALL( | 193 EXPECT_CALL( |
| 191 *proxy_.get(), | 194 *proxy_.get(), |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 // Mock bus and proxy for simulating calls to powerd. | 256 // Mock bus and proxy for simulating calls to powerd. |
| 254 scoped_refptr<dbus::MockBus> bus_; | 257 scoped_refptr<dbus::MockBus> bus_; |
| 255 scoped_refptr<dbus::MockObjectProxy> proxy_; | 258 scoped_refptr<dbus::MockObjectProxy> proxy_; |
| 256 | 259 |
| 257 std::unique_ptr<PowerManagerClient> client_; | 260 std::unique_ptr<PowerManagerClient> client_; |
| 258 | 261 |
| 259 // Maps from powerd signal name to the corresponding callback provided by | 262 // Maps from powerd signal name to the corresponding callback provided by |
| 260 // |client_|. | 263 // |client_|. |
| 261 std::map<std::string, dbus::ObjectProxy::SignalCallback> signal_callbacks_; | 264 std::map<std::string, dbus::ObjectProxy::SignalCallback> signal_callbacks_; |
| 262 | 265 |
| 266 // Callback passed to |proxy_|'s SetNameOwnerChangedCallback() method. |
| 267 // TODO(derat): Test that |client_| handles powerd restarts. |
| 268 dbus::ObjectProxy::NameOwnerChangedCallback name_owner_changed_callback_; |
| 269 |
| 263 private: | 270 private: |
| 264 // Handles calls to |proxy_|'s ConnectToSignal() method. | 271 // Handles calls to |proxy_|'s ConnectToSignal() method. |
| 265 void ConnectToSignal( | 272 void ConnectToSignal( |
| 266 const std::string& interface_name, | 273 const std::string& interface_name, |
| 267 const std::string& signal_name, | 274 const std::string& signal_name, |
| 268 dbus::ObjectProxy::SignalCallback signal_callback, | 275 dbus::ObjectProxy::SignalCallback signal_callback, |
| 269 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { | 276 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { |
| 270 CHECK_EQ(interface_name, power_manager::kPowerManagerInterface); | 277 CHECK_EQ(interface_name, power_manager::kPowerManagerInterface); |
| 271 signal_callbacks_[signal_name] = signal_callback; | 278 signal_callbacks_[signal_name] = signal_callback; |
| 272 | 279 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 EmitSuspendDoneSignal(kSuspendId); | 373 EmitSuspendDoneSignal(kSuspendId); |
| 367 EXPECT_EQ(1, delegate.num_suspend_imminent()); | 374 EXPECT_EQ(1, delegate.num_suspend_imminent()); |
| 368 EXPECT_EQ(1, delegate.num_suspend_done()); | 375 EXPECT_EQ(1, delegate.num_suspend_done()); |
| 369 } | 376 } |
| 370 | 377 |
| 371 // TODO(derat): Add more tests, e.g. for SuspendDone being received while | 378 // TODO(derat): Add more tests, e.g. for SuspendDone being received while |
| 372 // readiness callbacks are still outstanding (http://crbug.com/646912) and for | 379 // readiness callbacks are still outstanding (http://crbug.com/646912) and for |
| 373 // the handling of DarkSuspendImminent signals. | 380 // the handling of DarkSuspendImminent signals. |
| 374 | 381 |
| 375 } // namespace chromeos | 382 } // namespace chromeos |
| OLD | NEW |