OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "chrome/utility/wifi/wifi_service.h" |
| 6 #include "base/message_loop/message_loop.h" |
| 7 |
| 8 namespace wifi { |
| 9 |
| 10 class WiFiServiceMock : public WiFiService { |
| 11 public: |
| 12 WiFiServiceMock() { |
| 13 // Populate mock data expected by unit test. |
| 14 { |
| 15 WiFiService::NetworkProperties network_properties; |
| 16 network_properties.connection_state = |
| 17 WiFiService::kConnectionStateConnected; |
| 18 network_properties.guid = "stub_ethernet"; |
| 19 network_properties.name = "eth0"; |
| 20 network_properties.type = WiFiService::kNetworkTypeEthernet; |
| 21 networks_.push_back(network_properties); |
| 22 } |
| 23 { |
| 24 WiFiService::NetworkProperties network_properties; |
| 25 network_properties.connection_state = |
| 26 WiFiService::kConnectionStateConnected; |
| 27 network_properties.guid = "stub_wifi1"; |
| 28 network_properties.name = "wifi1"; |
| 29 network_properties.type = WiFiService::kNetworkTypeWiFi; |
| 30 network_properties.frequency = 0; |
| 31 network_properties.ssid = "stub_wifi1"; |
| 32 network_properties.security = WiFiService::kSecurityWEP_PSK; |
| 33 network_properties.signal_strength = 0; |
| 34 networks_.push_back(network_properties); |
| 35 } |
| 36 { |
| 37 WiFiService::NetworkProperties network_properties; |
| 38 network_properties.connection_state = |
| 39 WiFiService::kConnectionStateConnected; |
| 40 network_properties.guid = "stub_vpn1"; |
| 41 network_properties.name = "vpn1"; |
| 42 network_properties.type = WiFiService::kNetworkTypeVPN; |
| 43 networks_.push_back(network_properties); |
| 44 } |
| 45 { |
| 46 WiFiService::NetworkProperties network_properties; |
| 47 network_properties.connection_state = |
| 48 WiFiService::kConnectionStateNotConnected; |
| 49 network_properties.guid = "stub_wifi2"; |
| 50 network_properties.name = "wifi2_PSK"; |
| 51 network_properties.type = WiFiService::kNetworkTypeWiFi; |
| 52 network_properties.frequency = 5000; |
| 53 network_properties.frequency_list.push_back(2400); |
| 54 network_properties.frequency_list.push_back(5000); |
| 55 network_properties.ssid = "stub_wifi2"; |
| 56 network_properties.security = WiFiService::kSecurityWPA_PSK; |
| 57 network_properties.signal_strength = 80; |
| 58 networks_.push_back(network_properties); |
| 59 } |
| 60 { |
| 61 WiFiService::NetworkProperties network_properties; |
| 62 network_properties.connection_state = |
| 63 WiFiService::kConnectionStateNotConnected; |
| 64 network_properties.guid = "stub_cellular1"; |
| 65 network_properties.name = "cellular1"; |
| 66 network_properties.type = WiFiService::kNetworkTypeCellular; |
| 67 network_properties.json_extra = |
| 68 " {" |
| 69 " \"ActivateOverNonCellularNetwork\": false," |
| 70 " \"ActivationState\": \"not-activated\"," |
| 71 " \"NetworkTechnology\": \"GSM\"," |
| 72 " \"RoamingState\": \"home\"" |
| 73 " }"; |
| 74 networks_.push_back(network_properties); |
| 75 } |
| 76 } |
| 77 |
| 78 virtual void GetProperties(const std::string& network_guid, |
| 79 const NetworkPropertiesCallback& callback, |
| 80 const ErrorCallback& error_callback) OVERRIDE { |
| 81 NetworkList::iterator network_properties = FindNetwork(network_guid); |
| 82 if (network_properties != networks_.end()) { |
| 83 callback.Run(network_guid, *network_properties); |
| 84 } else { |
| 85 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
| 86 error_callback.Run("Error.DBusFailed", error_data.Pass()); |
| 87 } |
| 88 } |
| 89 |
| 90 virtual void GetState(const std::string& network_guid, |
| 91 const NetworkPropertiesCallback& callback, |
| 92 const ErrorCallback& error_callback) OVERRIDE {} |
| 93 |
| 94 virtual void GetManagedProperties( |
| 95 const std::string& network_guid, |
| 96 const DictionaryResultCallback& callback, |
| 97 const ErrorCallback& error_callback) OVERRIDE {} |
| 98 |
| 99 virtual void SetProperties(const std::string& network_guid, |
| 100 const base::DictionaryValue& properties, |
| 101 const StringResultCallback& callback, |
| 102 const ErrorCallback& error_callback) OVERRIDE { |
| 103 NetworkList::iterator network_properties = FindNetwork(network_guid); |
| 104 if (network_properties != networks_.end() && |
| 105 network_properties->UpdateFromValue(properties)) { |
| 106 callback.Run(network_guid); |
| 107 } else { |
| 108 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
| 109 error_callback.Run("Error.DBusFailed", error_data.Pass()); |
| 110 } |
| 111 } |
| 112 |
| 113 virtual void GetVisibleNetworks( |
| 114 const NetworkListCallback& callback, |
| 115 const ErrorCallback& error_callback) OVERRIDE { |
| 116 callback.Run(networks_); |
| 117 } |
| 118 |
| 119 virtual void RequestNetworkScan() OVERRIDE {} |
| 120 |
| 121 virtual void StartConnect(const std::string& network_guid, |
| 122 const StringResultCallback& callback, |
| 123 const ErrorCallback& error_callback) OVERRIDE { |
| 124 NetworkList::iterator network_properties = FindNetwork(network_guid); |
| 125 if (network_properties != networks_.end()) { |
| 126 DisconnectAllNetworksOfType(network_properties->type); |
| 127 network_properties->connection_state = kConnectionStateConnected; |
| 128 SortNetworks(); |
| 129 callback.Run(network_guid); |
| 130 NotifyNetworkListChanged(networks_); |
| 131 NotifyNetworkChanged(network_guid); |
| 132 } else { |
| 133 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
| 134 error_callback.Run("configure-failed", error_data.Pass()); |
| 135 } |
| 136 } |
| 137 |
| 138 virtual void StartDisconnect(const std::string& network_guid, |
| 139 const StringResultCallback& callback, |
| 140 const ErrorCallback& error_callback) OVERRIDE { |
| 141 NetworkList::iterator network_properties = FindNetwork(network_guid); |
| 142 if (network_properties != networks_.end()) { |
| 143 network_properties->connection_state = kConnectionStateNotConnected; |
| 144 SortNetworks(); |
| 145 callback.Run(network_guid); |
| 146 NotifyNetworkListChanged(networks_); |
| 147 NotifyNetworkChanged(network_guid); |
| 148 } else { |
| 149 scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
| 150 error_callback.Run("not-found", error_data.Pass()); |
| 151 } |
| 152 } |
| 153 |
| 154 virtual void SetNetworksChangedObserver( |
| 155 const NetworkGuidListCallback& observer) OVERRIDE { |
| 156 networks_changed_observer_ = observer; |
| 157 } |
| 158 |
| 159 virtual void SetNetworkListChangedObserver( |
| 160 const NetworkGuidListCallback& observer) OVERRIDE { |
| 161 network_list_changed_observer_ = observer; |
| 162 } |
| 163 |
| 164 private: |
| 165 NetworkList::iterator FindNetwork(const std::string& network_guid) { |
| 166 for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); |
| 167 ++it) { |
| 168 if (it->guid == network_guid) |
| 169 return it; |
| 170 } |
| 171 return networks_.end(); |
| 172 } |
| 173 |
| 174 void DisconnectAllNetworksOfType(WiFiService::NetworkType type) { |
| 175 for (NetworkList::iterator it = networks_.begin(); it != networks_.end(); |
| 176 ++it) { |
| 177 if (it->type == type) |
| 178 it->connection_state = kConnectionStateNotConnected; |
| 179 } |
| 180 } |
| 181 |
| 182 void SortNetworks() { |
| 183 // Sort networks, so connected/connecting is up front, then by type: |
| 184 // Ethernet, WiFi, Cellular, VPN |
| 185 networks_.sort(WiFiService::NetworkProperties::OrderByType); |
| 186 } |
| 187 |
| 188 void NotifyNetworkListChanged(const NetworkList& networks) { |
| 189 WiFiService::NetworkGuidList current_networks; |
| 190 for (WiFiService::NetworkList::const_iterator it = networks.begin(); |
| 191 it != networks.end(); |
| 192 ++it) { |
| 193 current_networks.push_back(it->guid); |
| 194 } |
| 195 network_list_changed_observer_.Run(current_networks); |
| 196 } |
| 197 |
| 198 void NotifyNetworkChanged(const std::string& network_guid) { |
| 199 WiFiService::NetworkGuidList changed_networks(1, network_guid); |
| 200 networks_changed_observer_.Run(changed_networks); |
| 201 } |
| 202 |
| 203 NetworkList networks_; |
| 204 NetworkGuidListCallback networks_changed_observer_; |
| 205 NetworkGuidListCallback network_list_changed_observer_; |
| 206 }; |
| 207 |
| 208 WiFiService* WiFiService::CreateServiceMock() { return new WiFiServiceMock(); } |
| 209 |
| 210 // TODO(mef): Figure out a better platform switching. For now all platform |
| 211 // except Windows use Mock implementation. |
| 212 #if !defined(OS_WIN) |
| 213 WiFiService* WiFiService::CreateService() { return new WiFiServiceMock(); } |
| 214 #endif |
| 215 |
| 216 } // namespace wifi |
OLD | NEW |