Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / components / wifi / wifi_test.cc
blobd88232fab75366456f9a867802d9e7bb6394245f
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 <stdio.h>
6 #include <string>
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/cancelable_callback.h"
11 #include "base/command_line.h"
12 #include "base/file_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "components/wifi/wifi_service.h"
24 #if defined(OS_MACOSX)
25 #include "base/mac/scoped_nsautorelease_pool.h"
26 #endif
28 namespace wifi {
30 class WiFiTest {
31 public:
32 WiFiTest() {}
33 ~WiFiTest() {}
35 enum Result {
36 RESULT_ERROR = -2,
37 RESULT_WRONG_USAGE = -1,
38 RESULT_OK = 0,
39 RESULT_PENDING = 1,
42 Result Main(int argc, const char* argv[]);
44 private:
45 bool ParseCommandLine(int argc, const char* argv[]);
47 void Start() {}
48 void Finish(Result result) {
49 DCHECK_NE(RESULT_PENDING, result);
50 result_ = result;
51 if (base::MessageLoop::current())
52 base::MessageLoop::current()->Quit();
55 #if defined(OS_MACOSX)
56 // Without this there will be a mem leak on osx.
57 base::mac::ScopedNSAutoreleasePool scoped_pool_;
58 #endif
60 // Need AtExitManager to support AsWeakPtr (in NetLog).
61 base::AtExitManager exit_manager_;
63 Result result_;
66 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) {
67 if (!ParseCommandLine(argc, argv)) {
68 VLOG(0) << "Usage: " << argv[0] <<
69 " [--list]"
70 " [--get_key]"
71 " [--get_properties]"
72 " [--create]"
73 " [--connect]"
74 " [--disconnect]"
75 " [--network_guid=<network_guid>]"
76 " [--frequency=0|2400|5000]"
77 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
78 " [--password=<wifi_password>]"
79 " [<network_guid>]\n";
80 return RESULT_WRONG_USAGE;
83 base::MessageLoopForIO loop;
84 result_ = RESULT_PENDING;
86 return result_;
89 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) {
90 CommandLine::Init(argc, argv);
91 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
92 std::string network_guid =
93 parsed_command_line.GetSwitchValueASCII("network_guid");
94 std::string frequency =
95 parsed_command_line.GetSwitchValueASCII("frequency");
96 std::string password =
97 parsed_command_line.GetSwitchValueASCII("password");
98 std::string security =
99 parsed_command_line.GetSwitchValueASCII("security");
101 if (parsed_command_line.GetArgs().size() == 1) {
102 #if defined(OS_WIN)
103 network_guid = base::UTF16ToASCII(parsed_command_line.GetArgs()[0]);
104 #else
105 network_guid = parsed_command_line.GetArgs()[0];
106 #endif
109 #if defined(OS_WIN)
110 if (parsed_command_line.HasSwitch("debug"))
111 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK);
112 #endif
114 #if defined(OS_WIN) || defined(OS_MACOSX)
115 scoped_ptr<WiFiService> wifi_service(WiFiService::Create());
116 #else
117 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateForTest());
118 #endif
120 wifi_service->Initialize(NULL);
122 if (parsed_command_line.HasSwitch("list")) {
123 base::ListValue network_list;
124 wifi_service->GetVisibleNetworks(std::string(), &network_list);
125 VLOG(0) << network_list;
126 return true;
129 if (parsed_command_line.HasSwitch("get_properties")) {
130 if (network_guid.length() > 0) {
131 base::DictionaryValue properties;
132 std::string error;
133 wifi_service->GetProperties(network_guid, &properties, &error);
134 VLOG(0) << error << ":\n" << properties;
135 return true;
139 // Optional properties (frequency, password) to use for connect or create.
140 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue());
142 if (!frequency.empty()) {
143 int value = 0;
144 if (base::StringToInt(frequency, &value)) {
145 properties->SetInteger("WiFi.Frequency", value);
146 // fall through to connect.
150 if (!password.empty())
151 properties->SetString("WiFi.Passphrase", password);
153 if (!security.empty())
154 properties->SetString("WiFi.Security", security);
156 if (parsed_command_line.HasSwitch("create")) {
157 if (!network_guid.empty()) {
158 std::string error;
159 std::string new_network_guid;
160 properties->SetString("WiFi.SSID", network_guid);
161 VLOG(0) << "Creating Network: " << *properties;
162 wifi_service->CreateNetwork(false,
163 properties.Pass(),
164 &new_network_guid,
165 &error);
166 VLOG(0) << error << ":\n" << new_network_guid;
167 return true;
171 if (parsed_command_line.HasSwitch("connect")) {
172 if (!network_guid.empty()) {
173 std::string error;
174 if (!properties->empty()) {
175 VLOG(0) << "Using connect properties: " << *properties;
176 wifi_service->SetProperties(network_guid,
177 properties.Pass(),
178 &error);
180 wifi_service->StartConnect(network_guid, &error);
181 VLOG(0) << error;
182 return true;
186 if (parsed_command_line.HasSwitch("disconnect")) {
187 if (network_guid.length() > 0) {
188 std::string error;
189 wifi_service->StartDisconnect(network_guid, &error);
190 VLOG(0) << error;
191 return true;
195 if (parsed_command_line.HasSwitch("get_key")) {
196 if (network_guid.length() > 0) {
197 std::string error;
198 std::string key_data;
199 wifi_service->GetKeyFromSystem(network_guid,
200 &key_data,
201 &error);
202 VLOG(0) << key_data << error;
203 return true;
207 return false;
210 } // namespace wifi
212 int main(int argc, const char* argv[]) {
213 CommandLine::Init(argc, argv);
214 logging::LoggingSettings settings;
215 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
216 logging::InitLogging(settings);
218 wifi::WiFiTest wifi_test;
219 return wifi_test.Main(argc, argv);