Gary Morain | 7225361 | 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 | |
| 9 | #include <base/scoped_ptr.h> |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #include "shill/mock_callback.h" |
| 14 | #include "shill/mock_power_manager_proxy.h" |
| 15 | #include "shill/power_manager_proxy_interface.h" |
| 16 | #include "shill/proxy_factory.h" |
| 17 | |
| 18 | using std::string; |
| 19 | using testing::_; |
| 20 | using testing::Test; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | typedef MockCallback<void(PowerManager::SuspendState)> MyMockCallback; |
| 25 | |
| 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 | |
| 51 | protected: |
| 52 | FakeProxyFactory factory_; |
| 53 | PowerManager power_manager_; |
| 54 | PowerManagerProxyDelegate *const delegate_; |
| 55 | }; |
| 56 | |
| 57 | TEST_F(PowerManagerTest, Add) { |
| 58 | const string kKey = "Zaphod"; |
| 59 | MyMockCallback *mock_callback = NewMockCallback(); |
| 60 | EXPECT_CALL(*mock_callback, OnRun(PowerManagerProxyDelegate::kOn)); |
| 61 | power_manager_.AddStateChangeCallback(kKey, mock_callback); |
| 62 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 63 | } |
| 64 | |
| 65 | TEST_F(PowerManagerTest, AddMultipleRunMultiple) { |
| 66 | const string kKey1 = "Zaphod"; |
| 67 | MyMockCallback *mock_callback1 = NewMockCallback(); |
| 68 | EXPECT_CALL(*mock_callback1, OnRun(PowerManagerProxyDelegate::kOn)); |
| 69 | EXPECT_CALL(*mock_callback1, OnRun(PowerManagerProxyDelegate::kMem)); |
| 70 | power_manager_.AddStateChangeCallback(kKey1, mock_callback1); |
| 71 | |
| 72 | const string kKey2 = "Beeblebrox"; |
| 73 | MyMockCallback *mock_callback2 = NewMockCallback(); |
| 74 | EXPECT_CALL(*mock_callback2, OnRun(PowerManagerProxyDelegate::kOn)); |
| 75 | EXPECT_CALL(*mock_callback2, OnRun(PowerManagerProxyDelegate::kMem)); |
| 76 | power_manager_.AddStateChangeCallback(kKey2, mock_callback2); |
| 77 | |
| 78 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 79 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 80 | } |
| 81 | |
| 82 | TEST_F(PowerManagerTest, Remove) { |
| 83 | const string kKey1 = "Zaphod"; |
| 84 | MyMockCallback *mock_callback1 = NewMockCallback(); |
| 85 | EXPECT_CALL(*mock_callback1, OnRun(PowerManagerProxyDelegate::kOn)); |
| 86 | EXPECT_CALL(*mock_callback1, OnRun(PowerManagerProxyDelegate::kMem)); |
| 87 | power_manager_.AddStateChangeCallback(kKey1, mock_callback1); |
| 88 | |
| 89 | const string kKey2 = "Beeblebrox"; |
| 90 | MyMockCallback *mock_callback2 = NewMockCallback(); |
| 91 | EXPECT_CALL(*mock_callback2, OnRun(PowerManagerProxyDelegate::kOn)); |
| 92 | EXPECT_CALL(*mock_callback2, OnRun(PowerManagerProxyDelegate::kMem)).Times(0); |
| 93 | power_manager_.AddStateChangeCallback(kKey2, mock_callback2); |
| 94 | |
| 95 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 96 | |
| 97 | power_manager_.RemoveStateChangeCallback(kKey2); |
| 98 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kMem); |
| 99 | |
| 100 | power_manager_.RemoveStateChangeCallback(kKey1); |
| 101 | factory_.delegate()->OnPowerStateChanged(PowerManagerProxyDelegate::kOn); |
| 102 | } |
| 103 | |
| 104 | TEST_F(PowerManagerTest, OnSuspendDelayIgnored) { |
| 105 | const string kKey = "Zaphod"; |
| 106 | MyMockCallback *mock_callback = NewMockCallback(); |
| 107 | EXPECT_CALL(*mock_callback, OnRun(_)).Times(0); |
| 108 | power_manager_.AddStateChangeCallback(kKey, mock_callback); |
| 109 | factory_.delegate()->OnSuspendDelay(99); |
| 110 | } |
| 111 | |
| 112 | typedef PowerManagerTest PowerManagerDeathTest; |
| 113 | |
| 114 | TEST_F(PowerManagerDeathTest, AddDuplicateKey) { |
| 115 | const string kKey1 = "Zaphod"; |
| 116 | MyMockCallback *mock_callback1 = NewMockCallback(); |
| 117 | MyMockCallback *mock_callback2 = NewMockCallback(); |
| 118 | power_manager_.AddStateChangeCallback(kKey1, mock_callback1); |
| 119 | |
| 120 | // Adding another callback with the same key is an error. |
| 121 | EXPECT_DEATH(power_manager_.AddStateChangeCallback(kKey1, mock_callback2), |
| 122 | "Inserting duplicate key"); |
| 123 | } |
| 124 | |
| 125 | TEST_F(PowerManagerDeathTest, RemoveUnknownKey) { |
| 126 | const string kKey1 = "Zaphod"; |
| 127 | const string kKey2 = "Beeblebrox"; |
| 128 | MyMockCallback *mock_callback1 = NewMockCallback(); |
| 129 | power_manager_.AddStateChangeCallback(kKey1, mock_callback1); |
| 130 | |
| 131 | // Attempting to remove a callback key that was not added is an error. |
| 132 | EXPECT_DEATH(power_manager_.RemoveStateChangeCallback(kKey2), |
| 133 | "Removing unknown key"); |
| 134 | } |
| 135 | |
| 136 | } // namespace shill |