| Index: chrome/utility/wifi/wifi_service_mock.cc
|
| diff --git a/chrome/utility/wifi/wifi_service_mock.cc b/chrome/utility/wifi/wifi_service_mock.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6aea62c425b27c9e167da0ec2d3e1882ddcefc86
|
| --- /dev/null
|
| +++ b/chrome/utility/wifi/wifi_service_mock.cc
|
| @@ -0,0 +1,219 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/utility/wifi/wifi_service.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "components/onc/onc_constants.h"
|
| +
|
| +namespace wifi {
|
| +
|
| +class WiFiServiceMock : public WiFiService {
|
| + public:
|
| + WiFiServiceMock() {
|
| + // Populate mock data expected by unit test.
|
| + {
|
| + WiFiService::NetworkProperties network_properties;
|
| + network_properties.connection_state = onc::connection_state::kConnected;
|
| + network_properties.guid = "stub_ethernet";
|
| + network_properties.name = "eth0";
|
| + network_properties.type = onc::network_type::kEthernet;
|
| + network_properties.json_extra =
|
| + " {"
|
| + " \"Authentication\": \"None\""
|
| + " }";
|
| + networks_.push_back(network_properties);
|
| + }
|
| + {
|
| + WiFiService::NetworkProperties network_properties;
|
| + network_properties.connection_state = onc::connection_state::kConnected;
|
| + network_properties.guid = "stub_wifi1";
|
| + network_properties.name = "wifi1";
|
| + network_properties.type = onc::network_type::kWiFi;
|
| + network_properties.frequency = 0;
|
| + network_properties.ssid = "stub_wifi1";
|
| + network_properties.security = onc::wifi::kWEP_PSK;
|
| + network_properties.signal_strength = 0;
|
| + networks_.push_back(network_properties);
|
| + }
|
| + {
|
| + WiFiService::NetworkProperties network_properties;
|
| + network_properties.connection_state = onc::connection_state::kConnected;
|
| + network_properties.guid = "stub_vpn1";
|
| + network_properties.name = "vpn1";
|
| + network_properties.type = onc::network_type::kVPN;
|
| + networks_.push_back(network_properties);
|
| + }
|
| + {
|
| + WiFiService::NetworkProperties network_properties;
|
| + network_properties.connection_state =
|
| + onc::connection_state::kNotConnected;
|
| + network_properties.guid = "stub_wifi2";
|
| + network_properties.name = "wifi2_PSK";
|
| + network_properties.type = onc::network_type::kWiFi;
|
| + network_properties.frequency = 5000;
|
| + network_properties.frequency_list.push_back(2400);
|
| + network_properties.frequency_list.push_back(5000);
|
| + network_properties.ssid = "stub_wifi2";
|
| + network_properties.security = onc::wifi::kWPA_PSK;
|
| + network_properties.signal_strength = 80;
|
| + networks_.push_back(network_properties);
|
| + }
|
| + {
|
| + WiFiService::NetworkProperties network_properties;
|
| + network_properties.connection_state =
|
| + onc::connection_state::kNotConnected;
|
| + network_properties.guid = "stub_cellular1";
|
| + network_properties.name = "cellular1";
|
| + network_properties.type = onc::network_type::kCellular;
|
| + network_properties.json_extra =
|
| + " {"
|
| + " \"ActivateOverNonCellularNetwork\": false,"
|
| + " \"ActivationState\": \"not-activated\","
|
| + " \"NetworkTechnology\": \"GSM\","
|
| + " \"RoamingState\": \"home\""
|
| + " }";
|
| + networks_.push_back(network_properties);
|
| + }
|
| + }
|
| +
|
| + virtual void GetProperties(const std::string& network_guid,
|
| + const NetworkPropertiesCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE {
|
| + NetworkList::iterator network_properties = FindNetwork(network_guid);
|
| + if (network_properties != networks_.end()) {
|
| + callback.Run(network_guid, *network_properties);
|
| + } else {
|
| + scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
|
| + error_callback.Run("Error.DBusFailed", error_data.Pass());
|
| + }
|
| + }
|
| +
|
| + virtual void GetState(const std::string& network_guid,
|
| + const NetworkPropertiesCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE {}
|
| +
|
| + virtual void GetManagedProperties(
|
| + const std::string& network_guid,
|
| + const DictionaryResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE {}
|
| +
|
| + virtual void SetProperties(const std::string& network_guid,
|
| + const base::DictionaryValue& properties,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE {
|
| + NetworkList::iterator network_properties = FindNetwork(network_guid);
|
| + if (network_properties != networks_.end() &&
|
| + network_properties->UpdateFromValue(properties)) {
|
| + callback.Run(network_guid);
|
| + } else {
|
| + scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
|
| + error_callback.Run("Error.DBusFailed", error_data.Pass());
|
| + }
|
| + }
|
| +
|
| + virtual void GetVisibleNetworks(
|
| + const NetworkListCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE {
|
| + callback.Run(networks_);
|
| + }
|
| +
|
| + virtual void RequestNetworkScan() OVERRIDE {}
|
| +
|
| + virtual void StartConnect(const std::string& network_guid,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE {
|
| + NetworkList::iterator network_properties = FindNetwork(network_guid);
|
| + if (network_properties != networks_.end()) {
|
| + DisconnectAllNetworksOfType(network_properties->type);
|
| + network_properties->connection_state = onc::connection_state::kConnected;
|
| + SortNetworks();
|
| + callback.Run(network_guid);
|
| + NotifyNetworkListChanged(networks_);
|
| + NotifyNetworkChanged(network_guid);
|
| + } else {
|
| + scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
|
| + error_callback.Run("configure-failed", error_data.Pass());
|
| + }
|
| + }
|
| +
|
| + virtual void StartDisconnect(const std::string& network_guid,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE {
|
| + NetworkList::iterator network_properties = FindNetwork(network_guid);
|
| + if (network_properties != networks_.end()) {
|
| + network_properties->connection_state =
|
| + onc::connection_state::kNotConnected;
|
| + SortNetworks();
|
| + callback.Run(network_guid);
|
| + NotifyNetworkListChanged(networks_);
|
| + NotifyNetworkChanged(network_guid);
|
| + } else {
|
| + scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
|
| + error_callback.Run("not-found", error_data.Pass());
|
| + }
|
| + }
|
| +
|
| + virtual void SetNetworksChangedObserver(
|
| + const NetworkGuidListCallback& observer) OVERRIDE {
|
| + networks_changed_observer_ = observer;
|
| + }
|
| +
|
| + virtual void SetNetworkListChangedObserver(
|
| + const NetworkGuidListCallback& observer) OVERRIDE {
|
| + network_list_changed_observer_ = observer;
|
| + }
|
| +
|
| + private:
|
| + NetworkList::iterator FindNetwork(const std::string& network_guid) {
|
| + for (NetworkList::iterator it = networks_.begin(); it != networks_.end();
|
| + ++it) {
|
| + if (it->guid == network_guid)
|
| + return it;
|
| + }
|
| + return networks_.end();
|
| + }
|
| +
|
| + void DisconnectAllNetworksOfType(const std::string& type) {
|
| + for (NetworkList::iterator it = networks_.begin(); it != networks_.end();
|
| + ++it) {
|
| + if (it->type == type)
|
| + it->connection_state = onc::connection_state::kNotConnected;
|
| + }
|
| + }
|
| +
|
| + void SortNetworks() {
|
| + // Sort networks, so connected/connecting is up front, then by type:
|
| + // Ethernet, WiFi, Cellular, VPN
|
| + networks_.sort(WiFiService::NetworkProperties::OrderByType);
|
| + }
|
| +
|
| + void NotifyNetworkListChanged(const NetworkList& networks) {
|
| + WiFiService::NetworkGuidList current_networks;
|
| + for (WiFiService::NetworkList::const_iterator it = networks.begin();
|
| + it != networks.end();
|
| + ++it) {
|
| + current_networks.push_back(it->guid);
|
| + }
|
| + network_list_changed_observer_.Run(current_networks);
|
| + }
|
| +
|
| + void NotifyNetworkChanged(const std::string& network_guid) {
|
| + WiFiService::NetworkGuidList changed_networks(1, network_guid);
|
| + networks_changed_observer_.Run(changed_networks);
|
| + }
|
| +
|
| + NetworkList networks_;
|
| + NetworkGuidListCallback networks_changed_observer_;
|
| + NetworkGuidListCallback network_list_changed_observer_;
|
| +};
|
| +
|
| +WiFiService* WiFiService::CreateServiceMock() { return new WiFiServiceMock(); }
|
| +
|
| +// TODO(mef): Figure out a better platform switching. For now all platform
|
| +// except Windows use Mock implementation.
|
| +#if !defined(OS_WIN)
|
| +WiFiService* WiFiService::CreateService() { return new WiFiServiceMock(); }
|
| +#endif
|
| +
|
| +} // namespace wifi
|
|
|