Chromium Code Reviews| 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..bffeb639248135ec7c250170a3904e57638e4151 |
| --- /dev/null |
| +++ b/chrome/utility/wifi/wifi_test.cc |
| @@ -0,0 +1,192 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
Jói
2013/10/19 21:14:45
2013, no (c)
mef
2013/10/21 15:37:16
Done.
|
| +// 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" |
| +#if defined(OS_MACOSX) |
|
Jói
2013/10/19 21:14:45
It seems more typical to move platform-specific in
mef
2013/10/21 15:37:16
I started with that, but presubmit checks were com
Jói
2013/10/21 16:44:03
They won't complain if you leave a blank line befo
mef
2013/10/22 20:06:01
Done.
|
| +#include "base/mac/scoped_nsautorelease_pool.h" |
| +#endif |
| +#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" |
| + |
| +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_ERROR); |
|
Jói
2013/10/19 21:14:45
Why RESULT_ERROR? Here and next method.
mef
2013/10/21 15:37:16
Done.
|
| + } |
| + |
| + void OnDictionaryResult(const std::string& network_guid, |
| + const base::DictionaryValue& dictionary) { |
| + std::cout << __FUNCTION__ << ":\n" << network_guid << dictionary; |
| + Finish(RESULT_ERROR); |
| + } |
| + |
| + 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"; |
| + } |
|
Jói
2013/10/19 21:14:45
Need to call Finish() ?
mef
2013/10/21 15:37:16
Done.
|
| + } |
| + |
| +#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(); |
| + // if (result_ == RESULT_PENDING) |
|
Jói
2013/10/19 21:14:45
Should these two lines be removed?
mef
2013/10/21 15:37:16
Done.
|
| + // base::MessageLoop::current()->Run(); |
| + |
| + 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); |
| +} |