Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 "shill/power_manager.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 9 | #include <base/bind.h> |
| 10 | #include <base/memory/scoped_ptr.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 14 | #include "shill/mock_power_manager_proxy.h" |
| 15 | #include "shill/power_manager_proxy_interface.h" |
| 16 | #include "shill/proxy_factory.h" |
| 17 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 18 | using base::Bind; |
| 19 | using base::Unretained; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 20 | using std::string; |
| 21 | using testing::_; |
| 22 | using testing::Test; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | class FakeProxyFactory : public ProxyFactory { |
| 29 | public: |
| 30 | FakeProxyFactory() : delegate_(NULL) {} |
| 31 | |
| 32 | virtual PowerManagerProxyInterface *CreatePowerManagerProxy( |
| 33 | PowerManagerProxyDelegate *delegate) { |
| 34 | delegate_ = delegate; |
| 35 | return new MockPowerManagerProxy; |
| 36 | } |
| 37 | PowerManagerProxyDelegate *delegate() const { return delegate_; } |
| 38 | |
| 39 | private: |
| 40 | PowerManagerProxyDelegate *delegate_; |
| 41 | }; |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | class PowerManagerTest : public Test { |
| 46 | public: |
| 47 | PowerManagerTest() |
| 48 | : power_manager_(&factory_), |
| 49 | delegate_(factory_.delegate()) { } |
| 50 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 51 | MOCK_METHOD1(TestCallback1, void(PowerManager::SuspendState)); |
| 52 | MOCK_METHOD1(TestCallback2, void(PowerManager::SuspendState)); |
| 53 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 54 | protected: |
| 55 | FakeProxyFactory factory_; |
| 56 | PowerManager power_manager_; |
| 57 | PowerManagerProxyDelegate *const delegate_; |
| 58 | }; |
| 59 | |
| 60 | TEST_F(PowerManagerTest, Add) { |
| 61 | const string kKey = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 62 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
| 63 | power_manager_.AddStateChangeCallback( |
| 64 | kKey, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 65 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 66 | power_manager_.RemoveStateChangeCallback(kKey); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST_F(PowerManagerTest, AddMultipleRunMultiple) { |
| 70 | const string kKey1 = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 71 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
| 72 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kMem)); |
| 73 | power_manager_.AddStateChangeCallback( |
| 74 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 75 | |
| 76 | const string kKey2 = "Beeblebrox"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 77 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kOn)); |
| 78 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kMem)); |
| 79 | power_manager_.AddStateChangeCallback( |
| 80 | kKey2, Bind(&PowerManagerTest::TestCallback2, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 81 | |
| 82 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 83 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 84 | } |
| 85 | |
| 86 | TEST_F(PowerManagerTest, Remove) { |
| 87 | const string kKey1 = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 88 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
| 89 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kMem)); |
| 90 | power_manager_.AddStateChangeCallback( |
| 91 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 92 | |
| 93 | const string kKey2 = "Beeblebrox"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 94 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kOn)); |
| 95 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kMem)).Times(0); |
| 96 | power_manager_.AddStateChangeCallback( |
| 97 | kKey2, Bind(&PowerManagerTest::TestCallback2, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 98 | |
| 99 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 100 | |
| 101 | power_manager_.RemoveStateChangeCallback(kKey2); |
| 102 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 103 | |
| 104 | power_manager_.RemoveStateChangeCallback(kKey1); |
| 105 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 106 | } |
| 107 | |
| 108 | TEST_F(PowerManagerTest, OnSuspendDelayIgnored) { |
| 109 | const string kKey = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 110 | EXPECT_CALL(*this, TestCallback1(_)).Times(0); |
| 111 | power_manager_.AddStateChangeCallback( |
| 112 | kKey, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 113 | factory_.delegate()->OnSuspendDelay(99); |
| 114 | } |
| 115 | |
| 116 | typedef PowerManagerTest PowerManagerDeathTest; |
| 117 | |
| 118 | TEST_F(PowerManagerDeathTest, AddDuplicateKey) { |
| 119 | const string kKey1 = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 120 | power_manager_.AddStateChangeCallback( |
| 121 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 122 | |
| 123 | #ifndef NDEBUG |
| 124 | // Adding another callback with the same key is an error and causes a crash in |
| 125 | // debug mode. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 126 | EXPECT_DEATH(power_manager_.AddStateChangeCallback( |
| 127 | kKey1, Bind(&PowerManagerTest::TestCallback2, Unretained(this))), |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 128 | "Inserting duplicate key"); |
| 129 | #else // NDEBUG |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 130 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kOn)); |
| 131 | power_manager_.AddStateChangeCallback( |
| 132 | kKey1, Bind(&PowerManagerTest::TestCallback2, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 133 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 134 | #endif // NDEBUG |
| 135 | } |
| 136 | |
| 137 | TEST_F(PowerManagerDeathTest, RemoveUnknownKey) { |
| 138 | const string kKey1 = "Zaphod"; |
| 139 | const string kKey2 = "Beeblebrox"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 140 | power_manager_.AddStateChangeCallback( |
| 141 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 142 | |
| 143 | #ifndef NDEBUG |
| 144 | // Attempting to remove a callback key that was not added is an error and |
| 145 | // crashes in debug mode. |
| 146 | EXPECT_DEATH(power_manager_.RemoveStateChangeCallback(kKey2), |
| 147 | "Removing unknown key"); |
| 148 | #else // NDEBUG |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 149 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 150 | |
| 151 | // In non-debug mode, removing an unknown key does nothing. |
| 152 | power_manager_.RemoveStateChangeCallback(kKey2); |
| 153 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 154 | #endif // NDEBUG |
| 155 | } |
| 156 | |
| 157 | } // namespace shill |