OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/components/tether/disconnect_tethering_operation.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <vector> |
| 9 |
| 10 #include "chromeos/components/tether/fake_ble_connection_manager.h" |
| 11 #include "chromeos/components/tether/message_wrapper.h" |
| 12 #include "chromeos/components/tether/proto/tether.pb.h" |
| 13 #include "components/cryptauth/remote_device_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace chromeos { |
| 17 |
| 18 namespace tether { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class TestObserver : public DisconnectTetheringOperation::Observer { |
| 23 public: |
| 24 TestObserver() : has_run_callback_(false), success_(false) {} |
| 25 |
| 26 virtual ~TestObserver() {} |
| 27 |
| 28 bool WasLastOperationSuccessful() { |
| 29 EXPECT_TRUE(has_run_callback_); |
| 30 return success_; |
| 31 } |
| 32 |
| 33 // DisconnectTetheringOperation::Observer: |
| 34 void OnOperationFinished(bool success) override { |
| 35 has_run_callback_ = true; |
| 36 success_ = success; |
| 37 } |
| 38 |
| 39 private: |
| 40 bool has_run_callback_; |
| 41 bool success_; |
| 42 }; |
| 43 |
| 44 std::string CreateDisconnectTetheringString() { |
| 45 DisconnectTetheringRequest request; |
| 46 return MessageWrapper(request).ToRawMessage(); |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 class DisconnectTetheringOperationTest : public testing::Test { |
| 52 protected: |
| 53 DisconnectTetheringOperationTest() |
| 54 : disconnect_tethering_request_string_(CreateDisconnectTetheringString()), |
| 55 test_device_(cryptauth::GenerateTestRemoteDevices(1)[0]) {} |
| 56 |
| 57 void SetUp() override { |
| 58 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
| 59 |
| 60 operation_ = base::WrapUnique(new DisconnectTetheringOperation( |
| 61 test_device_, fake_ble_connection_manager_.get())); |
| 62 |
| 63 test_observer_ = base::WrapUnique(new TestObserver()); |
| 64 operation_->AddObserver(test_observer_.get()); |
| 65 |
| 66 operation_->Initialize(); |
| 67 } |
| 68 |
| 69 void SimulateDeviceAuthenticationAndVerifyMessageSent() { |
| 70 operation_->OnDeviceAuthenticated(test_device_); |
| 71 |
| 72 // Verify that the message was sent successfully. |
| 73 std::vector<FakeBleConnectionManager::SentMessage>& sent_messages = |
| 74 fake_ble_connection_manager_->sent_messages(); |
| 75 ASSERT_EQ(1u, sent_messages.size()); |
| 76 EXPECT_EQ(test_device_, sent_messages[0].remote_device); |
| 77 EXPECT_EQ(disconnect_tethering_request_string_, sent_messages[0].message); |
| 78 } |
| 79 |
| 80 void SimulateConnectionTimeout() { |
| 81 operation_->UnregisterDevice(test_device_); |
| 82 } |
| 83 |
| 84 const std::string disconnect_tethering_request_string_; |
| 85 const cryptauth::RemoteDevice test_device_; |
| 86 |
| 87 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
| 88 std::unique_ptr<TestObserver> test_observer_; |
| 89 |
| 90 std::unique_ptr<DisconnectTetheringOperation> operation_; |
| 91 |
| 92 private: |
| 93 DISALLOW_COPY_AND_ASSIGN(DisconnectTetheringOperationTest); |
| 94 }; |
| 95 |
| 96 TEST_F(DisconnectTetheringOperationTest, TestSuccess) { |
| 97 SimulateDeviceAuthenticationAndVerifyMessageSent(); |
| 98 EXPECT_TRUE(test_observer_->WasLastOperationSuccessful()); |
| 99 } |
| 100 |
| 101 TEST_F(DisconnectTetheringOperationTest, TestFailure) { |
| 102 SimulateConnectionTimeout(); |
| 103 EXPECT_FALSE(test_observer_->WasLastOperationSuccessful()); |
| 104 } |
| 105 |
| 106 } // namespace tether |
| 107 |
| 108 } // namespace cryptauth |
OLD | NEW |