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