Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
Ryan Hansberry
2016/12/19 20:06:52
2016
Kyle Horimoto
2016/12/19 20:15:03
Done.
| |
| 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/ble_advertisement_device_queue.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "components/cryptauth/remote_device_test_util.h" | |
| 10 #include "testing/gmock/include/gmock/gmock.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 using testing::ElementsAre; | |
| 14 | |
| 15 namespace chromeos { | |
| 16 | |
| 17 namespace tether { | |
| 18 | |
| 19 class BleAdvertisementDeviceQueueTest : public testing::Test { | |
| 20 protected: | |
| 21 BleAdvertisementDeviceQueueTest() : device_queue_(nullptr) {} | |
| 22 | |
| 23 void SetUp() override { | |
| 24 device_queue_.reset(new BleAdvertisementDeviceQueue()); | |
| 25 } | |
| 26 | |
| 27 std::unique_ptr<BleAdvertisementDeviceQueue> device_queue_; | |
| 28 | |
| 29 private: | |
| 30 DISALLOW_COPY_AND_ASSIGN(BleAdvertisementDeviceQueueTest); | |
| 31 }; | |
| 32 | |
| 33 TEST_F(BleAdvertisementDeviceQueueTest, TestEmptyQueue) { | |
| 34 std::vector<cryptauth::RemoteDevice> to_advertise = | |
| 35 device_queue_->GetDevicesToWhichToAdvertise(); | |
| 36 EXPECT_EQ(static_cast<size_t>(0), device_queue_->GetSize()); | |
| 37 EXPECT_TRUE(to_advertise.empty()); | |
| 38 } | |
| 39 | |
| 40 TEST_F(BleAdvertisementDeviceQueueTest, TestSingleDevice) { | |
| 41 std::vector<cryptauth::RemoteDevice> devices = | |
| 42 cryptauth::GenerateTestRemoteDevices(1); | |
| 43 EXPECT_TRUE(device_queue_->SetDevices(devices)); | |
| 44 EXPECT_EQ(static_cast<size_t>(1), device_queue_->GetSize()); | |
| 45 | |
| 46 std::vector<cryptauth::RemoteDevice> to_advertise = | |
| 47 device_queue_->GetDevicesToWhichToAdvertise(); | |
| 48 EXPECT_THAT(to_advertise, ElementsAre(devices[0])); | |
| 49 } | |
| 50 | |
| 51 TEST_F(BleAdvertisementDeviceQueueTest, TestSingleDevice_MoveToEnd) { | |
| 52 std::vector<cryptauth::RemoteDevice> devices = | |
| 53 cryptauth::GenerateTestRemoteDevices(1); | |
| 54 EXPECT_TRUE(device_queue_->SetDevices(devices)); | |
| 55 EXPECT_EQ(static_cast<size_t>(1), device_queue_->GetSize()); | |
| 56 | |
| 57 std::vector<cryptauth::RemoteDevice> to_advertise = | |
| 58 device_queue_->GetDevicesToWhichToAdvertise(); | |
| 59 EXPECT_THAT(to_advertise, ElementsAre(devices[0])); | |
| 60 | |
| 61 device_queue_->MoveDeviceToEnd(devices[0].GetDeviceId()); | |
| 62 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 63 EXPECT_THAT(to_advertise, ElementsAre(devices[0])); | |
| 64 } | |
| 65 | |
| 66 TEST_F(BleAdvertisementDeviceQueueTest, TestTwoDevices) { | |
| 67 std::vector<cryptauth::RemoteDevice> devices = | |
| 68 cryptauth::GenerateTestRemoteDevices(2); | |
| 69 EXPECT_TRUE(device_queue_->SetDevices(devices)); | |
| 70 EXPECT_EQ(static_cast<size_t>(2), device_queue_->GetSize()); | |
| 71 | |
| 72 std::vector<cryptauth::RemoteDevice> to_advertise = | |
| 73 device_queue_->GetDevicesToWhichToAdvertise(); | |
| 74 EXPECT_THAT(to_advertise, ElementsAre(devices[0], devices[1])); | |
| 75 } | |
| 76 | |
| 77 TEST_F(BleAdvertisementDeviceQueueTest, TestTwoDevices_MoveToEnd) { | |
| 78 std::vector<cryptauth::RemoteDevice> devices = | |
| 79 cryptauth::GenerateTestRemoteDevices(2); | |
| 80 EXPECT_TRUE(device_queue_->SetDevices(devices)); | |
| 81 EXPECT_EQ(static_cast<size_t>(2), device_queue_->GetSize()); | |
| 82 | |
| 83 std::vector<cryptauth::RemoteDevice> to_advertise = | |
| 84 device_queue_->GetDevicesToWhichToAdvertise(); | |
| 85 EXPECT_THAT(to_advertise, ElementsAre(devices[0], devices[1])); | |
| 86 | |
| 87 device_queue_->MoveDeviceToEnd(devices[0].GetDeviceId()); | |
| 88 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 89 EXPECT_THAT(to_advertise, ElementsAre(devices[1], devices[0])); | |
| 90 | |
| 91 device_queue_->MoveDeviceToEnd(devices[1].GetDeviceId()); | |
| 92 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 93 EXPECT_THAT(to_advertise, ElementsAre(devices[0], devices[1])); | |
| 94 } | |
| 95 | |
| 96 TEST_F(BleAdvertisementDeviceQueueTest, TestThreeDevices) { | |
| 97 // Note: These tests need to be rewritten if MAX_CONCURRENT_ADVERTISEMENTS is | |
| 98 // ever changed. | |
| 99 ASSERT_GT(3, BleAdvertisementDeviceQueue::kMaxConcurrentAdvertisements); | |
| 100 | |
| 101 std::vector<cryptauth::RemoteDevice> devices = | |
| 102 cryptauth::GenerateTestRemoteDevices(3); | |
| 103 EXPECT_TRUE(device_queue_->SetDevices(devices)); | |
| 104 EXPECT_EQ(static_cast<size_t>(3), device_queue_->GetSize()); | |
| 105 | |
| 106 std::vector<cryptauth::RemoteDevice> to_advertise = | |
| 107 device_queue_->GetDevicesToWhichToAdvertise(); | |
| 108 EXPECT_THAT(to_advertise, ElementsAre(devices[0], devices[1])); | |
| 109 | |
| 110 device_queue_->MoveDeviceToEnd(devices[0].GetDeviceId()); | |
| 111 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 112 EXPECT_THAT(to_advertise, ElementsAre(devices[1], devices[2])); | |
| 113 | |
| 114 device_queue_->MoveDeviceToEnd(devices[1].GetDeviceId()); | |
| 115 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 116 EXPECT_THAT(to_advertise, ElementsAre(devices[2], devices[0])); | |
| 117 | |
| 118 device_queue_->MoveDeviceToEnd(devices[2].GetDeviceId()); | |
| 119 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 120 EXPECT_THAT(to_advertise, ElementsAre(devices[0], devices[1])); | |
| 121 } | |
| 122 | |
| 123 TEST_F(BleAdvertisementDeviceQueueTest, TestAddingDevices) { | |
| 124 // Note: These tests need to be rewritten if MAX_CONCURRENT_ADVERTISEMENTS is | |
| 125 // ever changed. | |
| 126 ASSERT_GT(3, BleAdvertisementDeviceQueue::kMaxConcurrentAdvertisements); | |
| 127 | |
| 128 std::vector<cryptauth::RemoteDevice> all_devices = | |
| 129 cryptauth::GenerateTestRemoteDevices(5); | |
| 130 std::vector<cryptauth::RemoteDevice> initial_devices = {all_devices[0], | |
| 131 all_devices[1]}; | |
| 132 std::vector<cryptauth::RemoteDevice> updated_devices_list = { | |
| 133 all_devices[1], all_devices[2], all_devices[3], all_devices[4]}; | |
| 134 | |
| 135 EXPECT_TRUE(device_queue_->SetDevices(initial_devices)); | |
| 136 EXPECT_EQ(static_cast<size_t>(2), device_queue_->GetSize()); | |
| 137 | |
| 138 std::vector<cryptauth::RemoteDevice> to_advertise = | |
| 139 device_queue_->GetDevicesToWhichToAdvertise(); | |
| 140 EXPECT_THAT(to_advertise, ElementsAre(all_devices[0], all_devices[1])); | |
| 141 | |
| 142 device_queue_->MoveDeviceToEnd(all_devices[0].GetDeviceId()); | |
| 143 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 144 EXPECT_THAT(to_advertise, ElementsAre(all_devices[1], all_devices[0])); | |
| 145 | |
| 146 // Device #1 has been unregistered; Devices #3 and #4 have been registered. | |
| 147 EXPECT_TRUE(device_queue_->SetDevices(updated_devices_list)); | |
| 148 EXPECT_EQ(static_cast<size_t>(4), device_queue_->GetSize()); | |
| 149 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 150 EXPECT_THAT(to_advertise, ElementsAre(all_devices[1], all_devices[2])); | |
|
Ryan Hansberry
2016/12/19 20:06:52
nit: Is there an expected consistency to having ex
Kyle Horimoto
2016/12/19 20:15:03
The EXPECT_EQ() always takes expected first and ac
| |
| 151 | |
| 152 device_queue_->MoveDeviceToEnd(all_devices[2].GetDeviceId()); | |
| 153 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 154 EXPECT_THAT(to_advertise, ElementsAre(all_devices[1], all_devices[3])); | |
| 155 | |
| 156 device_queue_->MoveDeviceToEnd(all_devices[1].GetDeviceId()); | |
| 157 to_advertise = device_queue_->GetDevicesToWhichToAdvertise(); | |
| 158 EXPECT_THAT(to_advertise, ElementsAre(all_devices[3], all_devices[4])); | |
| 159 } | |
| 160 | |
| 161 TEST_F(BleAdvertisementDeviceQueueTest, TestSettingSameDevices) { | |
| 162 std::vector<cryptauth::RemoteDevice> devices = | |
| 163 cryptauth::GenerateTestRemoteDevices(2); | |
| 164 EXPECT_TRUE(device_queue_->SetDevices(devices)); | |
| 165 EXPECT_FALSE(device_queue_->SetDevices(devices)); | |
| 166 EXPECT_FALSE(device_queue_->SetDevices(devices)); | |
|
Ryan Hansberry
2016/12/19 20:06:52
Any reason to have this call twice?
Kyle Horimoto
2016/12/19 20:15:03
Yes, this test is meant to test setting the same d
| |
| 167 EXPECT_TRUE( | |
| 168 device_queue_->SetDevices(cryptauth::GenerateTestRemoteDevices(3))); | |
| 169 } | |
| 170 | |
| 171 } // namespace tether | |
| 172 | |
| 173 } // namespace cryptauth | |
| OLD | NEW |