Index: chrome/utility/wifi/wifi_test.cc |
diff --git a/chrome/utility/wifi/wifi_test.cc b/chrome/utility/wifi/wifi_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..81e485551454d272a16871881f5221bf2b4ec574 |
--- /dev/null |
+++ b/chrome/utility/wifi/wifi_test.cc |
@@ -0,0 +1,186 @@ |
+// 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 <stdio.h> |
+#include <iostream> |
+#include <string> |
+ |
+#include "base/at_exit.h" |
+#include "base/bind.h" |
+#include "base/cancelable_callback.h" |
+#include "base/command_line.h" |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_split.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/time/time.h" |
+#include "chrome/utility/wifi/wifi_service.h" |
+ |
+#if defined(OS_MACOSX) |
+#include "base/mac/scoped_nsautorelease_pool.h" |
+#endif |
+ |
+namespace wifi { |
+ |
+class WiFiTest { |
+ public: |
+ WiFiTest() {} |
+ ~WiFiTest() {} |
+ |
+ enum Result { |
+ RESULT_ERROR = -2, |
+ RESULT_WRONG_USAGE = -1, |
+ RESULT_OK = 0, |
+ RESULT_PENDING = 1, |
+ }; |
+ |
+ Result Main(int argc, const char* argv[]); |
+ |
+ private: |
+ bool ParseCommandLine(int argc, const char* argv[]); |
+ |
+ void Start() {} |
+ void Finish(Result result) { |
+ DCHECK_NE(RESULT_PENDING, result); |
+ result_ = result; |
+ if (base::MessageLoop::current()) |
+ base::MessageLoop::current()->Quit(); |
+ } |
+ |
+ void OnError(const std::string& error_name, |
+ scoped_ptr<base::DictionaryValue> error_data) { |
+ std::cout << __FUNCTION__ << ":" << error_name; |
+ Finish(RESULT_ERROR); |
+ } |
+ |
+ void OnStringResult(const std::string& network_guid) { |
+ std::cout << __FUNCTION__ << ":\n" << network_guid; |
+ Finish(RESULT_OK); |
+ } |
+ |
+ void OnNetworkProperties(const std::string& network_guid, |
+ const WiFiService::NetworkProperties& properties) { |
+ std::cout << __FUNCTION__ << ":\n" << *properties.ToValue(false).release(); |
+ Finish(RESULT_OK); |
+ } |
+ |
+ void OnGetVisibleNetworks(const WiFiService::NetworkList& network_list) { |
+ std::cout << __FUNCTION__ << ":\n"; |
+ for (WiFiService::NetworkList::const_iterator it = network_list.begin(); |
+ it != network_list.end(); |
+ ++it) { |
+ std::cout << *it->ToValue(false).release() << "\n"; |
+ } |
+ Finish(RESULT_OK); |
+ } |
+ |
+#if defined(OS_MACOSX) |
+ // Without this there will be a mem leak on osx. |
+ base::mac::ScopedNSAutoreleasePool scoped_pool_; |
+#endif |
+ |
+ // Need AtExitManager to support AsWeakPtr (in NetLog). |
+ base::AtExitManager exit_manager_; |
+ |
+ Result result_; |
+}; |
+ |
+WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { |
+ if (!ParseCommandLine(argc, argv)) { |
+ fprintf(stderr, |
+ "usage: %s [--list]" |
+ " [--get_properties]" |
+ " [--connect]" |
+ " [--disconnect]" |
+ " [--network_guid=<network_guid>]" |
+ " [<network_guid>]\n", |
+ argv[0]); |
+ return RESULT_WRONG_USAGE; |
+ } |
+ |
+ base::MessageLoopForIO loop; |
+ result_ = RESULT_PENDING; |
+ Start(); |
+ |
+ return result_; |
+} |
+ |
+bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) { |
+ CommandLine::Init(argc, argv); |
+ const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
+ std::string network_guid = |
+ parsed_command_line.GetSwitchValueASCII("network_guid"); |
+ |
+ if (parsed_command_line.GetArgs().size() == 1) { |
+#if defined(OS_WIN) |
+ network_guid = WideToASCII(parsed_command_line.GetArgs()[0]); |
+#else |
+ network_guid = parsed_command_line.GetArgs()[0]; |
+#endif |
+ } |
+ |
+#if defined(OS_WIN) |
+ if (parsed_command_line.HasSwitch("debug")) |
+ MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); |
+#endif |
+ |
+#if defined(OS_WIN) |
+ scoped_ptr<WiFiService> wifi_service(WiFiService::CreateService()); |
+#else |
+ scoped_ptr<WiFiService> wifi_service(WiFiService::CreateServiceMock()); |
+#endif |
+ |
+ if (parsed_command_line.HasSwitch("list")) { |
+ wifi_service->GetVisibleNetworks( |
+ base::Bind(&WiFiTest::OnGetVisibleNetworks, base::Unretained(this)), |
+ base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
+ return true; |
+ } |
+ |
+ if (parsed_command_line.HasSwitch("get_properties")) { |
+ if (network_guid.length() > 0) { |
+ wifi_service->GetProperties( |
+ network_guid, |
+ base::Bind(&WiFiTest::OnNetworkProperties, base::Unretained(this)), |
+ base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
+ |
+ return true; |
+ } |
+ } |
+ |
+ if (parsed_command_line.HasSwitch("connect")) { |
+ if (network_guid.length() > 0) { |
+ wifi_service->StartConnect( |
+ network_guid, |
+ base::Bind(&WiFiTest::OnStringResult, base::Unretained(this)), |
+ base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
+ |
+ return true; |
+ } |
+ } |
+ |
+ if (parsed_command_line.HasSwitch("disconnect")) { |
+ if (network_guid.length() > 0) { |
+ wifi_service->StartDisconnect( |
+ network_guid, |
+ base::Bind(&WiFiTest::OnStringResult, base::Unretained(this)), |
+ base::Bind(&WiFiTest::OnError, base::Unretained(this))); |
+ |
+ return true; |
+ } |
+ } |
+ |
+ return false; |
+} |
+ |
+} // namespace wifi |
+ |
+int main(int argc, const char* argv[]) { |
+ wifi::WiFiTest wifi_test; |
+ return wifi_test.Main(argc, argv); |
+} |