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 "chromeos/network/network_state_manager.h" |
| 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 #include <string> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" |
| 14 #include "base/values.h" |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 16 #include "chromeos/dbus/shill_service_client.h" |
| 17 #include "chromeos/network/network_state.h" |
| 18 #include "dbus/object_path.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace chromeos { |
| 22 |
| 23 namespace { |
| 24 |
| 25 void ErrorCallbackFunction(const std::string& error_name, |
| 26 const std::string& error_message) { |
| 27 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; |
| 28 } |
| 29 |
| 30 class TestObserver : public NetworkStateManager::Observer { |
| 31 public: |
| 32 TestObserver() |
| 33 : manager_changed_count_(0), |
| 34 network_count_(0) { |
| 35 } |
| 36 |
| 37 virtual ~TestObserver() {} |
| 38 |
| 39 virtual void NetworkManagerChanged(const std::string& property) { |
| 40 ++manager_changed_count_; |
| 41 } |
| 42 |
| 43 virtual void NetworkListChanged( |
| 44 const NetworkStateManager::NetworkStateList& networks) { |
| 45 network_count_ = networks.size(); |
| 46 } |
| 47 |
| 48 virtual void ActiveNetworkChanged(const NetworkState* network) { |
| 49 active_network_ = network ? network->path() : ""; |
| 50 active_network_state_ = network ? network->state() : ""; |
| 51 } |
| 52 |
| 53 virtual void ActiveNetworkStateChanged(const NetworkState* network) { |
| 54 active_network_state_ = network ? network->state() : ""; |
| 55 } |
| 56 |
| 57 virtual void NetworkServicePropertyChanged(const NetworkState* network, |
| 58 const std::string& property) { |
| 59 if (!network) |
| 60 return; |
| 61 changed_properties_[network->path()].insert(property); |
| 62 } |
| 63 |
| 64 int manager_changed_count() { return manager_changed_count_; } |
| 65 int network_count() { return network_count_; } |
| 66 std::string active_network() { return active_network_; } |
| 67 std::string active_network_state() { return active_network_state_; } |
| 68 |
| 69 bool PropertyChangedForService(const std::string& service_path, |
| 70 const std::string& property) { |
| 71 std::map<std::string, std::set<std::string> >::iterator iter = |
| 72 changed_properties_.find(service_path); |
| 73 if (iter == changed_properties_.end()) |
| 74 return false; |
| 75 if (iter->second.find(property) == iter->second.end()) |
| 76 return false; |
| 77 return true; |
| 78 } |
| 79 |
| 80 private: |
| 81 int manager_changed_count_; |
| 82 int network_count_; |
| 83 std::string active_network_; |
| 84 std::string active_network_state_; |
| 85 std::map<std::string, std::set<std::string> > changed_properties_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 88 }; |
| 89 |
| 90 } // namespace |
| 91 |
| 92 class NetworkStateManagerTest : public testing::Test { |
| 93 public: |
| 94 NetworkStateManagerTest() {} |
| 95 virtual ~NetworkStateManagerTest() {} |
| 96 |
| 97 virtual void SetUp() OVERRIDE { |
| 98 // Initialize DBusThreadManager with a stub implementation. |
| 99 DBusThreadManager::InitializeWithStub(); |
| 100 } |
| 101 |
| 102 virtual void TearDown() OVERRIDE { |
| 103 DBusThreadManager::Shutdown(); |
| 104 } |
| 105 |
| 106 protected: |
| 107 MessageLoopForUI message_loop_; |
| 108 |
| 109 private: |
| 110 DISALLOW_COPY_AND_ASSIGN(NetworkStateManagerTest); |
| 111 }; |
| 112 |
| 113 TEST_F(NetworkStateManagerTest, NetworkStateManagerStub) { |
| 114 // This relies on the stub dbus implementations for ShillManagerClient, |
| 115 scoped_ptr<NetworkStateManager> state_manager(new NetworkStateManager()); |
| 116 scoped_ptr<TestObserver> test_observer(new TestObserver()); |
| 117 state_manager->AddObserver(test_observer.get()); |
| 118 state_manager->Init(); |
| 119 message_loop_.RunAllPending(); |
| 120 EXPECT_EQ(test_observer->manager_changed_count(), 1); |
| 121 // ShillManagerClient stub entries are in shill_manager_client.cc. |
| 122 const int kNumShillManagerClientStubImplServices = 4; |
| 123 EXPECT_EQ(kNumShillManagerClientStubImplServices, |
| 124 test_observer->network_count()); |
| 125 const int kNumShillManagerClientStubImplOnlineServices = 2; |
| 126 EXPECT_EQ(kNumShillManagerClientStubImplOnlineServices, |
| 127 state_manager->NumObservedNetworksForTest()); |
| 128 const std::string kShillManagerClientStubActiveService = "stub_ethernet"; |
| 129 EXPECT_EQ(kShillManagerClientStubActiveService, |
| 130 test_observer->active_network()); |
| 131 EXPECT_EQ(kShillManagerClientStubActiveService, |
| 132 state_manager->ActiveNetwork()->path()); |
| 133 EXPECT_EQ(kShillManagerClientStubActiveService, |
| 134 state_manager->ConnectedNetworkByType( |
| 135 flimflam::kTypeEthernet)->path()); |
| 136 // ShillServiceClient stub entries are in shill_service_client.cc. |
| 137 const std::string kShillManagerClientStubActiveServiceState = |
| 138 flimflam::kStateOnline; |
| 139 EXPECT_EQ(kShillManagerClientStubActiveServiceState, |
| 140 test_observer->active_network_state()); |
| 141 } |
| 142 |
| 143 TEST_F(NetworkStateManagerTest, NetworkStateManagerTechnologyChanged) { |
| 144 // This relies on the stub dbus implementations for ShillManagerClient, |
| 145 scoped_ptr<NetworkStateManager> state_manager(new NetworkStateManager()); |
| 146 scoped_ptr<TestObserver> test_observer(new TestObserver()); |
| 147 state_manager->AddObserver(test_observer.get()); |
| 148 state_manager->Init(); |
| 149 message_loop_.RunAllPending(); |
| 150 EXPECT_EQ(1, test_observer->manager_changed_count()); |
| 151 // Enable a technology. |
| 152 EXPECT_FALSE(state_manager->TechnologyEnabled(flimflam::kTypeWimax)); |
| 153 state_manager->SetTechnologyEnabled(flimflam::kTypeWimax, true); |
| 154 message_loop_.RunAllPending(); |
| 155 // Ensure we get a manager changed callback when we change a property. |
| 156 EXPECT_EQ(2, test_observer->manager_changed_count()); |
| 157 EXPECT_TRUE(state_manager->TechnologyEnabled(flimflam::kTypeWimax)); |
| 158 } |
| 159 |
| 160 TEST_F(NetworkStateManagerTest, NetworkStateManagerServicePropertyChanged) { |
| 161 // This relies on the stub dbus implementations for ShillManagerClient, |
| 162 scoped_ptr<NetworkStateManager> state_manager(new NetworkStateManager()); |
| 163 scoped_ptr<TestObserver> test_observer(new TestObserver()); |
| 164 state_manager->AddObserver(test_observer.get()); |
| 165 state_manager->Init(); |
| 166 message_loop_.RunAllPending(); |
| 167 // Set a service property. |
| 168 const std::string eth0 = "stub_ethernet"; |
| 169 EXPECT_EQ("none", state_manager->GetNetworkState(eth0)->security()); |
| 170 EXPECT_FALSE(test_observer->PropertyChangedForService( |
| 171 eth0, flimflam::kSecurityProperty)); |
| 172 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( |
| 173 dbus::ObjectPath(eth0), |
| 174 flimflam::kSecurityProperty, base::StringValue("TestSecurity"), |
| 175 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); |
| 176 message_loop_.RunAllPending(); |
| 177 EXPECT_EQ("TestSecurity", state_manager->GetNetworkState(eth0)->security()); |
| 178 EXPECT_TRUE(test_observer->PropertyChangedForService( |
| 179 eth0, flimflam::kSecurityProperty)); |
| 180 } |
| 181 |
| 182 } // namespace chromeos |
OLD | NEW |