1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/tether/network_connection_handler_tether_delegate.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/tether/fake_active_host.h"
#include "chromeos/ash/components/tether/fake_tether_connector.h"
#include "chromeos/ash/components/tether/fake_tether_disconnector.h"
#include "chromeos/ash/components/tether/tether_disconnector.h"
#include "chromeos/ash/components/tether/tether_session_completion_logger.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace tether {
namespace {
const char kSuccessResult[] = "success";
// Does nothing when a connection is requested.
class DummyTetherConnector : public FakeTetherConnector {
public:
// TetherConnector:
void ConnectToNetwork(const std::string& tether_network_guid,
base::OnceClosure success_callback,
StringErrorCallback error_callback) override {}
};
// Does nothing when a disconnection is requested.
class DummyTetherDisconnector : public FakeTetherDisconnector {
public:
// TetherDisconnector:
void DisconnectFromNetwork(
const std::string& tether_network_guid,
base::OnceClosure success_callback,
StringErrorCallback error_callback,
const TetherSessionCompletionLogger::SessionCompletionReason&
session_completion_reason) override {}
};
class TestNetworkConnectionHandler : public NetworkConnectionHandler {
public:
TestNetworkConnectionHandler() : NetworkConnectionHandler() {}
~TestNetworkConnectionHandler() override = default;
void CallTetherConnect(const std::string& tether_network_guid,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback) {
InitiateTetherNetworkConnection(tether_network_guid,
std::move(success_callback),
std::move(error_callback));
}
void CallTetherDisconnect(const std::string& tether_network_guid,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback) {
InitiateTetherNetworkDisconnection(tether_network_guid,
std::move(success_callback),
std::move(error_callback));
}
// NetworkConnectionHandler:
void ConnectToNetwork(const std::string& service_path,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback,
bool check_error_state,
ConnectCallbackMode mode) override {}
void DisconnectNetwork(
const std::string& service_path,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback) override {}
void Init(
NetworkStateHandler* network_state_handler,
NetworkConfigurationHandler* network_configuration_handler,
ManagedNetworkConfigurationHandler* managed_network_configuration_handler,
CellularConnectionHandler* cellular_connection_handler) override {}
void OnAutoConnectedInitiated(int auto_connect_reasons) override {}
};
} // namespace
class NetworkConnectionHandlerTetherDelegateTest : public testing::Test {
public:
NetworkConnectionHandlerTetherDelegateTest(
const NetworkConnectionHandlerTetherDelegateTest&) = delete;
NetworkConnectionHandlerTetherDelegateTest& operator=(
const NetworkConnectionHandlerTetherDelegateTest&) = delete;
protected:
NetworkConnectionHandlerTetherDelegateTest() = default;
void SetUp() override {
result_.clear();
test_network_connection_handler_ =
base::WrapUnique(new TestNetworkConnectionHandler());
fake_active_host_ = std::make_unique<FakeActiveHost>();
fake_tether_connector_ = std::make_unique<FakeTetherConnector>();
fake_tether_disconnector_ = std::make_unique<FakeTetherDisconnector>();
delegate_ = std::make_unique<NetworkConnectionHandlerTetherDelegate>(
test_network_connection_handler_.get(), fake_active_host_.get(),
fake_tether_connector_.get(), fake_tether_disconnector_.get());
}
void TearDown() override {
// No more callbacks should occur after deletion.
delegate_.reset();
EXPECT_EQ(std::string(), GetResultAndReset());
}
void CallTetherConnect(const std::string& guid) {
test_network_connection_handler_->CallTetherConnect(
guid,
base::BindOnce(&NetworkConnectionHandlerTetherDelegateTest::OnSuccess,
base::Unretained(this)),
base::BindOnce(&NetworkConnectionHandlerTetherDelegateTest::OnError,
base::Unretained(this)));
}
void CallTetherDisconnect(const std::string& guid) {
test_network_connection_handler_->CallTetherDisconnect(
guid,
base::BindOnce(&NetworkConnectionHandlerTetherDelegateTest::OnSuccess,
base::Unretained(this)),
base::BindOnce(&NetworkConnectionHandlerTetherDelegateTest::OnError,
base::Unretained(this)));
}
void OnSuccess() { result_ = kSuccessResult; }
void OnError(const std::string& error) { result_ = error; }
std::string GetResultAndReset() {
std::string result;
result.swap(result_);
return result;
}
std::unique_ptr<TestNetworkConnectionHandler>
test_network_connection_handler_;
std::unique_ptr<FakeActiveHost> fake_active_host_;
std::unique_ptr<FakeTetherConnector> fake_tether_connector_;
std::unique_ptr<FakeTetherDisconnector> fake_tether_disconnector_;
std::string result_;
std::unique_ptr<NetworkConnectionHandlerTetherDelegate> delegate_;
};
TEST_F(NetworkConnectionHandlerTetherDelegateTest,
TestConnect_NotAlreadyConnected) {
CallTetherConnect("tetherNetworkGuid");
EXPECT_EQ("tetherNetworkGuid",
fake_tether_connector_->last_connected_tether_network_guid());
EXPECT_EQ(kSuccessResult, GetResultAndReset());
}
TEST_F(NetworkConnectionHandlerTetherDelegateTest,
TestConnect_AlreadyConnectedToSameDevice) {
fake_active_host_->SetActiveHostConnected("activeHostId", "tetherNetworkGuid",
"wifiNetworkGuid");
CallTetherConnect("tetherNetworkGuid");
EXPECT_TRUE(
fake_tether_connector_->last_connected_tether_network_guid().empty());
EXPECT_TRUE(fake_tether_disconnector_->last_disconnected_tether_network_guid()
.empty());
EXPECT_EQ(NetworkConnectionHandler::kErrorConnected, GetResultAndReset());
}
TEST_F(NetworkConnectionHandlerTetherDelegateTest,
TestConnect_AlreadyConnectedToDifferentDevice) {
fake_active_host_->SetActiveHostConnected("activeHostId", "tetherNetworkGuid",
"wifiNetworkGuid");
CallTetherConnect("newTetherNetworkGuid");
EXPECT_EQ("tetherNetworkGuid",
fake_tether_disconnector_->last_disconnected_tether_network_guid());
EXPECT_EQ("newTetherNetworkGuid",
fake_tether_connector_->last_connected_tether_network_guid());
EXPECT_EQ(kSuccessResult, GetResultAndReset());
}
TEST_F(NetworkConnectionHandlerTetherDelegateTest, TestDisconnect) {
CallTetherDisconnect("tetherNetworkGuid");
EXPECT_EQ("tetherNetworkGuid",
fake_tether_disconnector_->last_disconnected_tether_network_guid());
EXPECT_EQ(
TetherSessionCompletionLogger::SessionCompletionReason::USER_DISCONNECTED,
*fake_tether_disconnector_->last_session_completion_reason());
EXPECT_EQ(kSuccessResult, GetResultAndReset());
}
TEST_F(NetworkConnectionHandlerTetherDelegateTest,
TestPendingCallbacksInvokedWhenDeleted) {
delegate_.reset();
// Use "dummy" connector/disconnector.
std::unique_ptr<DummyTetherConnector> dummy_connector =
base::WrapUnique(new DummyTetherConnector());
std::unique_ptr<DummyTetherDisconnector> dummy_disconnector =
base::WrapUnique(new DummyTetherDisconnector());
test_network_connection_handler_ =
base::WrapUnique(new TestNetworkConnectionHandler());
delegate_ = std::make_unique<NetworkConnectionHandlerTetherDelegate>(
test_network_connection_handler_.get(), fake_active_host_.get(),
dummy_connector.get(), dummy_disconnector.get());
CallTetherConnect("tetherNetworkGuid");
// No callbacks should have been invoked.
EXPECT_TRUE(result_.empty());
// Now, delete the delegate. It should fire the error callback.
delegate_.reset();
EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset());
}
} // namespace tether
} // namespace ash
|