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