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