Chromium Code Reviews| Index: chromeos/network/network_connection_handler_unittest.cc |
| diff --git a/chromeos/network/network_connection_handler_unittest.cc b/chromeos/network/network_connection_handler_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab5b04eb6fdb478cc21044582c43d41c7fb47fc3 |
| --- /dev/null |
| +++ b/chromeos/network/network_connection_handler_unittest.cc |
| @@ -0,0 +1,197 @@ |
| +// Copyright (c) 2012 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 "chromeos/network/network_connection_handler.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/shill_manager_client.h" |
| +#include "chromeos/dbus/shill_service_client.h" |
| +#include "chromeos/network/network_configuration_handler.h" |
| +#include "chromeos/network/network_state_handler.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/cros_system_api/dbus/service_constants.h" |
| + |
| +namespace { |
| + |
| +const char* kSuccessResult = "success"; |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| + |
| +class NetworkConnectionHandlerTest : public testing::Test { |
| + public: |
| + NetworkConnectionHandlerTest() { |
| + } |
| + virtual ~NetworkConnectionHandlerTest() { |
| + } |
| + |
| + virtual void SetUp() OVERRIDE { |
| + // Initialize DBusThreadManager with a stub implementation. |
| + DBusThreadManager::InitializeWithStub(); |
| + message_loop_.RunUntilIdle(); |
| + DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface() |
| + ->ClearServices(); |
| + message_loop_.RunUntilIdle(); |
| + NetworkStateHandler::Initialize(); |
| + NetworkConfigurationHandler::Initialize(); |
| + NetworkConnectionHandler::Initialize(); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + NetworkConnectionHandler::Shutdown(); |
| + NetworkConfigurationHandler::Shutdown(); |
| + NetworkStateHandler::Shutdown(); |
| + DBusThreadManager::Shutdown(); |
| + } |
| + |
| + protected: |
| + bool Configure(const std::string& json_string) { |
| + base::Value* json = base::JSONReader::Read(json_string); |
|
pneubeck (no reviews)
2013/05/07 08:46:39
network/onc/onc_utils.h: ReadDictionaryFromJson
wh
stevenjb
2013/05/08 01:57:22
Done.
|
| + if (!json) { |
| + LOG(ERROR) << "Error parsing json: " << json_string; |
| + return false; |
| + } |
| + base::DictionaryValue* json_dict; |
| + if (!json->GetAsDictionary(&json_dict)) { |
| + LOG(ERROR) << "json is not a dictionary: " << json; |
| + return false; |
| + } |
| + DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService( |
| + *json_dict, |
| + ObjectPathCallback(), ShillManagerClient::ErrorCallback()); |
| + message_loop_.RunUntilIdle(); |
| + return true; |
| + } |
| + |
| + void Connect(const std::string& service_path) { |
| + NetworkConnectionHandler::Get()->ConnectToNetwork( |
| + service_path, |
| + base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, |
| + base::Unretained(this)), |
| + base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, |
| + base::Unretained(this))); |
| + message_loop_.RunUntilIdle(); |
| + } |
| + |
| + void Disconnect(const std::string& service_path) { |
| + NetworkConnectionHandler::Get()->DisconnectNetwork( |
| + service_path, |
| + base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, |
| + base::Unretained(this)), |
| + base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, |
| + base::Unretained(this))); |
| + message_loop_.RunUntilIdle(); |
| + } |
| + |
| + void SuccessCallback() { |
| + result_ = kSuccessResult; |
| + } |
| + |
| + void ErrorCallback(const std::string& error_name, |
| + scoped_ptr<base::DictionaryValue> error_data) { |
| + result_ = error_name; |
| + } |
| + |
| + std::string GetResultAndReset() { |
| + std::string result = result_; |
|
pneubeck (no reviews)
2013/05/07 08:46:39
nit: swap maybe
stevenjb
2013/05/08 01:57:22
Done.
|
| + result_.clear(); |
| + return result; |
| + } |
| + |
| + MessageLoopForUI message_loop_; |
| + std::string result_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest); |
| +}; |
| + |
| +namespace { |
| + |
| +const char* kConfigConnectable = |
| + "{ \"GUID\": \"wifi0\", \"Type\": \"wifi\", \"State\": \"idle\" }"; |
| +const char* kConfigConnected = |
| + "{ \"GUID\": \"wifi1\", \"Type\": \"wifi\", \"State\": \"online\" }"; |
| +const char* kConfigConnecting = |
| + "{ \"GUID\": \"wifi2\", \"Type\": \"wifi\", \"State\": \"association\" }"; |
| +const char* kConfigRequiresPassphrase = |
| + "{ \"GUID\": \"wifi3\", \"Type\": \"wifi\", " |
| + "\"PassphraseRequired\": true }"; |
| +const char* kConfigRequiresActivation = |
| + "{ \"GUID\": \"cellular1\", \"Type\": \"cellular\"," |
| + " \"Cellular.ActivationState\": \"not-activated\" }"; |
| + |
| +} // namespace |
| + |
| +TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectSuccess) { |
|
pneubeck (no reviews)
2013/05/07 08:46:39
We should also test that at least a connect reques
stevenjb
2013/05/08 01:57:22
Done
|
| + EXPECT_TRUE(Configure(kConfigConnectable)); |
| + Connect("wifi0"); |
| + EXPECT_EQ(kSuccessResult, GetResultAndReset()); |
| +} |
| + |
| +// Handles basic failure cases. |
| +TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectFailure) { |
| + Connect("no-network"); |
| + EXPECT_EQ(NetworkConnectionHandler::kNetworkNotFound, GetResultAndReset()); |
| + |
| + EXPECT_TRUE(Configure(kConfigConnected)); |
| + Connect("wifi1"); |
| + EXPECT_EQ(NetworkConnectionHandler::kConnected, GetResultAndReset()); |
| + |
| + EXPECT_TRUE(Configure(kConfigConnecting)); |
| + Connect("wifi2"); |
| + EXPECT_EQ(NetworkConnectionHandler::kConnecting, GetResultAndReset()); |
| + |
| + EXPECT_TRUE(Configure(kConfigRequiresPassphrase)); |
| + Connect("wifi3"); |
| + EXPECT_EQ(NetworkConnectionHandler::kPassphraseRequired, GetResultAndReset()); |
| + |
| + EXPECT_TRUE(Configure(kConfigRequiresActivation)); |
| + Connect("cellular1"); |
| + EXPECT_EQ(NetworkConnectionHandler::kActivationRequired, GetResultAndReset()); |
| +} |
| + |
| +namespace { |
| + |
| +const char* kConfigRequiresCertificate = |
| + "{ \"GUID\": \"wifi4\", \"Type\": \"wifi\", \"Connectable\": false," |
| + " \"Security\": \"802_1x\"," |
| + " \"UIData\": \"{" |
| + " \\\"certificate_type\\\": \\\"pattern\\\"," |
| + " \\\"certificate_pattern\\\": \\\"foobar\\\" }\" }"; |
| + |
| +} // namespace |
| + |
| +// Handle certificates. TODO(stevenjb): Add certificate stubs to improve |
| +// test coverage. |
| +TEST_F(NetworkConnectionHandlerTest, |
| + NetworkConnectionHandlerConnectCertificate) { |
| + EXPECT_TRUE(Configure(kConfigRequiresCertificate)); |
| + Connect("wifi4"); |
| + EXPECT_EQ(NetworkConnectionHandler::kConfigurationRequired, |
| + GetResultAndReset()); |
| +} |
| + |
| +TEST_F(NetworkConnectionHandlerTest, |
| + NetworkConnectionHandlerDisconnectSuccess) { |
| + EXPECT_TRUE(Configure(kConfigConnected)); |
| + Disconnect("wifi1"); |
| + EXPECT_EQ(kSuccessResult, GetResultAndReset()); |
| +} |
| + |
| +TEST_F(NetworkConnectionHandlerTest, |
| + NetworkConnectionHandlerDisconnectFailure) { |
| + Connect("no-network"); |
| + EXPECT_EQ(NetworkConnectionHandler::kNetworkNotFound, GetResultAndReset()); |
| + |
| + EXPECT_TRUE(Configure(kConfigConnectable)); |
| + Disconnect("wifi0"); |
| + EXPECT_EQ(NetworkConnectionHandler::kNotConnected, GetResultAndReset()); |
| +} |
| + |
| +} // namespace chromeos |