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 | |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 60 | TEST_F(PowerManagerTest, OnPowerStateChanged) { |
| 61 | EXPECT_EQ(PowerManagerProxyDelegate::kUnknown, power_manager_.power_state()); |
| 62 | power_manager_.OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 63 | EXPECT_EQ(PowerManagerProxyDelegate::kOn, power_manager_.power_state()); |
| 64 | } |
| 65 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 66 | TEST_F(PowerManagerTest, Add) { |
| 67 | const string kKey = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 68 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
| 69 | power_manager_.AddStateChangeCallback( |
| 70 | kKey, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 71 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 72 | power_manager_.RemoveStateChangeCallback(kKey); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST_F(PowerManagerTest, AddMultipleRunMultiple) { |
| 76 | const string kKey1 = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 77 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
| 78 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kMem)); |
| 79 | power_manager_.AddStateChangeCallback( |
| 80 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 81 | |
| 82 | const string kKey2 = "Beeblebrox"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 83 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kOn)); |
| 84 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kMem)); |
| 85 | power_manager_.AddStateChangeCallback( |
| 86 | kKey2, Bind(&PowerManagerTest::TestCallback2, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 87 | |
| 88 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 89 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 90 | } |
| 91 | |
| 92 | TEST_F(PowerManagerTest, Remove) { |
| 93 | const string kKey1 = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 94 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
| 95 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kMem)); |
| 96 | power_manager_.AddStateChangeCallback( |
| 97 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 98 | |
| 99 | const string kKey2 = "Beeblebrox"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 100 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kOn)); |
| 101 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kMem)).Times(0); |
| 102 | power_manager_.AddStateChangeCallback( |
| 103 | kKey2, Bind(&PowerManagerTest::TestCallback2, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 104 | |
| 105 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 106 | |
| 107 | power_manager_.RemoveStateChangeCallback(kKey2); |
| 108 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 109 | |
| 110 | power_manager_.RemoveStateChangeCallback(kKey1); |
| 111 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 112 | } |
| 113 | |
| 114 | TEST_F(PowerManagerTest, OnSuspendDelayIgnored) { |
| 115 | const string kKey = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 116 | EXPECT_CALL(*this, TestCallback1(_)).Times(0); |
| 117 | power_manager_.AddStateChangeCallback( |
| 118 | kKey, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 119 | factory_.delegate()->OnSuspendDelay(99); |
| 120 | } |
| 121 | |
| 122 | typedef PowerManagerTest PowerManagerDeathTest; |
| 123 | |
| 124 | TEST_F(PowerManagerDeathTest, AddDuplicateKey) { |
| 125 | const string kKey1 = "Zaphod"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 126 | power_manager_.AddStateChangeCallback( |
| 127 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 128 | |
| 129 | #ifndef NDEBUG |
| 130 | // Adding another callback with the same key is an error and causes a crash in |
| 131 | // debug mode. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 132 | EXPECT_DEATH(power_manager_.AddStateChangeCallback( |
| 133 | kKey1, Bind(&PowerManagerTest::TestCallback2, Unretained(this))), |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 134 | "Inserting duplicate key"); |
| 135 | #else // NDEBUG |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 136 | EXPECT_CALL(*this, TestCallback2(PowerManagerProxyDelegate::kOn)); |
| 137 | power_manager_.AddStateChangeCallback( |
| 138 | kKey1, Bind(&PowerManagerTest::TestCallback2, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 139 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 140 | #endif // NDEBUG |
| 141 | } |
| 142 | |
| 143 | TEST_F(PowerManagerDeathTest, RemoveUnknownKey) { |
| 144 | const string kKey1 = "Zaphod"; |
| 145 | const string kKey2 = "Beeblebrox"; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 146 | power_manager_.AddStateChangeCallback( |
| 147 | kKey1, Bind(&PowerManagerTest::TestCallback1, Unretained(this))); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 148 | |
| 149 | #ifndef NDEBUG |
| 150 | // Attempting to remove a callback key that was not added is an error and |
| 151 | // crashes in debug mode. |
| 152 | EXPECT_DEATH(power_manager_.RemoveStateChangeCallback(kKey2), |
| 153 | "Removing unknown key"); |
| 154 | #else // NDEBUG |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 155 | EXPECT_CALL(*this, TestCallback1(PowerManagerProxyDelegate::kOn)); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 156 | |
| 157 | // In non-debug mode, removing an unknown key does nothing. |
| 158 | power_manager_.RemoveStateChangeCallback(kKey2); |
| 159 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 160 | #endif // NDEBUG |
| 161 | } |
| 162 | |
| 163 | } // namespace shill |