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.
5 #include "components/wifi/fake_wifi_service.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/onc/onc_constants.h"
13 FakeWiFiService::FakeWiFiService() {
14 // Populate data expected by unit test.
16 NetworkProperties network_properties
;
17 network_properties
.connection_state
= onc::connection_state::kConnected
;
18 network_properties
.guid
= "stub_wifi1_guid";
19 network_properties
.name
= "wifi1";
20 network_properties
.type
= onc::network_type::kWiFi
;
21 network_properties
.frequency
= 0;
22 network_properties
.ssid
= "wifi1";
23 network_properties
.security
= onc::wifi::kWEP_PSK
;
24 network_properties
.signal_strength
= 40;
25 networks_
.push_back(network_properties
);
28 NetworkProperties network_properties
;
29 network_properties
.connection_state
= onc::connection_state::kNotConnected
;
30 network_properties
.guid
= "stub_wifi2_guid";
31 network_properties
.name
= "wifi2_PSK";
32 network_properties
.type
= onc::network_type::kWiFi
;
33 network_properties
.frequency
= 5000;
34 network_properties
.frequency_set
.insert(2400);
35 network_properties
.frequency_set
.insert(5000);
36 network_properties
.ssid
= "wifi2_PSK";
37 network_properties
.security
= onc::wifi::kWPA_PSK
;
38 network_properties
.signal_strength
= 80;
39 networks_
.push_back(network_properties
);
43 FakeWiFiService::~FakeWiFiService() {
46 void FakeWiFiService::Initialize(
47 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
50 void FakeWiFiService::UnInitialize() {
53 void FakeWiFiService::GetProperties(const std::string
& network_guid
,
54 base::DictionaryValue
* properties
,
56 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
57 if (network_properties
== networks_
.end()) {
58 *error
= "Error.InvalidNetworkGuid";
61 properties
->Swap(network_properties
->ToValue(false).get());
64 void FakeWiFiService::GetManagedProperties(
65 const std::string
& network_guid
,
66 base::DictionaryValue
* managed_properties
,
69 *error
= kErrorWiFiService
;
72 void FakeWiFiService::GetState(const std::string
& network_guid
,
73 base::DictionaryValue
* properties
,
75 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
76 if (network_properties
== networks_
.end()) {
77 *error
= "Error.InvalidNetworkGuid";
80 properties
->Swap(network_properties
->ToValue(true).get());
83 void FakeWiFiService::SetProperties(
84 const std::string
& network_guid
,
85 scoped_ptr
<base::DictionaryValue
> properties
,
87 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
88 if (network_properties
== networks_
.end() ||
89 !network_properties
->UpdateFromValue(*properties
)) {
90 *error
= "Error.DBusFailed";
94 void FakeWiFiService::CreateNetwork(
96 scoped_ptr
<base::DictionaryValue
> properties
,
97 std::string
* network_guid
,
99 NetworkProperties network_properties
;
100 if (network_properties
.UpdateFromValue(*properties
)) {
101 network_properties
.guid
= network_properties
.ssid
;
102 networks_
.push_back(network_properties
);
103 *network_guid
= network_properties
.guid
;
105 *error
= "Error.DBusFailed";
109 void FakeWiFiService::GetVisibleNetworks(const std::string
& network_type
,
110 base::ListValue
* network_list
,
111 bool include_details
) {
112 for (NetworkList::const_iterator it
= networks_
.begin();
113 it
!= networks_
.end();
115 if (network_type
.empty() || network_type
== onc::network_type::kAllTypes
||
116 it
->type
== network_type
) {
117 scoped_ptr
<base::DictionaryValue
> network(it
->ToValue(!include_details
));
118 network_list
->Append(network
.release());
123 void FakeWiFiService::RequestNetworkScan() {
124 NotifyNetworkListChanged(networks_
);
127 void FakeWiFiService::StartConnect(const std::string
& network_guid
,
128 std::string
* error
) {
129 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
130 if (network_properties
== networks_
.end()) {
131 *error
= "Error.InvalidNetworkGuid";
134 DisconnectAllNetworksOfType(network_properties
->type
);
135 network_properties
->connection_state
= onc::connection_state::kConnected
;
137 NotifyNetworkListChanged(networks_
);
138 NotifyNetworkChanged(network_guid
);
141 void FakeWiFiService::StartDisconnect(const std::string
& network_guid
,
142 std::string
* error
) {
143 NetworkList::iterator network_properties
= FindNetwork(network_guid
);
144 if (network_properties
== networks_
.end()) {
145 *error
= "Error.InvalidNetworkGuid";
148 network_properties
->connection_state
= onc::connection_state::kNotConnected
;
150 NotifyNetworkListChanged(networks_
);
151 NotifyNetworkChanged(network_guid
);
154 void FakeWiFiService::GetKeyFromSystem(const std::string
& network_guid
,
155 std::string
* key_data
,
156 std::string
* error
) {
157 *error
= "not-found";
160 void FakeWiFiService::SetEventObservers(
161 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
162 const NetworkGuidListCallback
& networks_changed_observer
,
163 const NetworkGuidListCallback
& network_list_changed_observer
) {
164 task_runner_
.swap(task_runner
);
165 networks_changed_observer_
= networks_changed_observer
;
166 network_list_changed_observer_
= network_list_changed_observer
;
169 void FakeWiFiService::RequestConnectedNetworkUpdate() {
172 void FakeWiFiService::GetConnectedNetworkSSID(std::string
* ssid
,
173 std::string
* error
) {
178 NetworkList::iterator
FakeWiFiService::FindNetwork(
179 const std::string
& network_guid
) {
180 for (NetworkList::iterator it
= networks_
.begin(); it
!= networks_
.end();
182 if (it
->guid
== network_guid
)
185 return networks_
.end();
188 void FakeWiFiService::DisconnectAllNetworksOfType(const std::string
& type
) {
189 for (NetworkList::iterator it
= networks_
.begin(); it
!= networks_
.end();
191 if (it
->type
== type
)
192 it
->connection_state
= onc::connection_state::kNotConnected
;
196 void FakeWiFiService::SortNetworks() {
197 // Sort networks, so connected/connecting is up front, then by type:
198 // Ethernet, WiFi, Cellular, VPN
199 networks_
.sort(NetworkProperties::OrderByType
);
202 void FakeWiFiService::NotifyNetworkListChanged(const NetworkList
& networks
) {
203 WiFiService::NetworkGuidList current_networks
;
204 for (NetworkList::const_iterator it
= networks
.begin(); it
!= networks
.end();
206 current_networks
.push_back(it
->guid
);
209 task_runner_
->PostTask(
210 FROM_HERE
, base::Bind(network_list_changed_observer_
, current_networks
));
213 void FakeWiFiService::NotifyNetworkChanged(const std::string
& network_guid
) {
214 WiFiService::NetworkGuidList
changed_networks(1, network_guid
);
215 task_runner_
->PostTask(
216 FROM_HERE
, base::Bind(networks_changed_observer_
, changed_networks
));