| Index: chromeos/components/tether/disconnect_tethering_operation_unittest.cc
|
| diff --git a/chromeos/components/tether/disconnect_tethering_operation_unittest.cc b/chromeos/components/tether/disconnect_tethering_operation_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cc369484c1904560338cb88e1200a1e6e0a727a8
|
| --- /dev/null
|
| +++ b/chromeos/components/tether/disconnect_tethering_operation_unittest.cc
|
| @@ -0,0 +1,108 @@
|
| +// Copyright 2016 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/components/tether/disconnect_tethering_operation.h"
|
| +
|
| +#include <memory>
|
| +#include <vector>
|
| +
|
| +#include "chromeos/components/tether/fake_ble_connection_manager.h"
|
| +#include "chromeos/components/tether/message_wrapper.h"
|
| +#include "chromeos/components/tether/proto/tether.pb.h"
|
| +#include "components/cryptauth/remote_device_test_util.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace chromeos {
|
| +
|
| +namespace tether {
|
| +
|
| +namespace {
|
| +
|
| +class TestObserver : public DisconnectTetheringOperation::Observer {
|
| + public:
|
| + TestObserver() : has_run_callback_(false), success_(false) {}
|
| +
|
| + virtual ~TestObserver() {}
|
| +
|
| + bool WasLastOperationSuccessful() {
|
| + EXPECT_TRUE(has_run_callback_);
|
| + return success_;
|
| + }
|
| +
|
| + // DisconnectTetheringOperation::Observer:
|
| + void OnOperationFinished(bool success) override {
|
| + has_run_callback_ = true;
|
| + success_ = success;
|
| + }
|
| +
|
| + private:
|
| + bool has_run_callback_;
|
| + bool success_;
|
| +};
|
| +
|
| +std::string CreateDisconnectTetheringString() {
|
| + DisconnectTetheringRequest request;
|
| + return MessageWrapper(request).ToRawMessage();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class DisconnectTetheringOperationTest : public testing::Test {
|
| + protected:
|
| + DisconnectTetheringOperationTest()
|
| + : disconnect_tethering_request_string_(CreateDisconnectTetheringString()),
|
| + test_device_(cryptauth::GenerateTestRemoteDevices(1)[0]) {}
|
| +
|
| + void SetUp() override {
|
| + fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
|
| +
|
| + operation_ = base::WrapUnique(new DisconnectTetheringOperation(
|
| + test_device_, fake_ble_connection_manager_.get()));
|
| +
|
| + test_observer_ = base::WrapUnique(new TestObserver());
|
| + operation_->AddObserver(test_observer_.get());
|
| +
|
| + operation_->Initialize();
|
| + }
|
| +
|
| + void SimulateDeviceAuthenticationAndVerifyMessageSent() {
|
| + operation_->OnDeviceAuthenticated(test_device_);
|
| +
|
| + // Verify that the message was sent successfully.
|
| + std::vector<FakeBleConnectionManager::SentMessage>& sent_messages =
|
| + fake_ble_connection_manager_->sent_messages();
|
| + ASSERT_EQ(1u, sent_messages.size());
|
| + EXPECT_EQ(test_device_, sent_messages[0].remote_device);
|
| + EXPECT_EQ(disconnect_tethering_request_string_, sent_messages[0].message);
|
| + }
|
| +
|
| + void SimulateConnectionTimeout() {
|
| + operation_->UnregisterDevice(test_device_);
|
| + }
|
| +
|
| + const std::string disconnect_tethering_request_string_;
|
| + const cryptauth::RemoteDevice test_device_;
|
| +
|
| + std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
|
| + std::unique_ptr<TestObserver> test_observer_;
|
| +
|
| + std::unique_ptr<DisconnectTetheringOperation> operation_;
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(DisconnectTetheringOperationTest);
|
| +};
|
| +
|
| +TEST_F(DisconnectTetheringOperationTest, TestSuccess) {
|
| + SimulateDeviceAuthenticationAndVerifyMessageSent();
|
| + EXPECT_TRUE(test_observer_->WasLastOperationSuccessful());
|
| +}
|
| +
|
| +TEST_F(DisconnectTetheringOperationTest, TestFailure) {
|
| + SimulateConnectionTimeout();
|
| + EXPECT_FALSE(test_observer_->WasLastOperationSuccessful());
|
| +}
|
| +
|
| +} // namespace tether
|
| +
|
| +} // namespace cryptauth
|
|
|