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