Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include "device/include/controller.h" |
| 23 | #include "stack/btm/ble_advertiser_hci_interface.h" |
| 24 | #include "stack/include/ble_advertiser.h" |
| 25 | |
| 26 | using ::testing::_; |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 27 | using ::testing::Args; |
| 28 | using ::testing::ElementsAreArray; |
Jakub Pawlowski | 1dd7751 | 2016-10-10 14:25:52 -0700 | [diff] [blame] | 29 | using ::testing::Exactly; |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 30 | using ::testing::IsEmpty; |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 31 | using ::testing::SaveArg; |
| 32 | using status_cb = BleAdvertiserHciInterface::status_cb; |
| 33 | |
| 34 | const int num_adv_instances = 16; |
| 35 | |
| 36 | /* Below are methods that must be implemented if we don't want to compile the |
| 37 | * whole stack. They will be removed, or changed into mocks one by one in the |
| 38 | * future, as the refactoring progresses */ |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 39 | bool BTM_BleLocalPrivacyEnabled() { return true; } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 40 | uint16_t BTM_ReadDiscoverability(uint16_t* p_window, uint16_t* p_interval) { |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 41 | return true; |
| 42 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 43 | bool SMP_Encrypt(uint8_t* key, uint8_t key_len, uint8_t* plain_text, |
| 44 | uint8_t pt_len, tSMP_ENC* p_out) { |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 45 | return true; |
| 46 | } |
| 47 | void BTM_GetDeviceIDRoot(BT_OCTET16 irk) {} |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 48 | void btm_ble_update_dmt_flag_bits(uint8_t* flag_value, |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 49 | const uint16_t connect_mode, |
| 50 | const uint16_t disc_mode) {} |
| 51 | void btm_acl_update_conn_addr(uint8_t conn_handle, BD_ADDR address) {} |
| 52 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 53 | void btm_gen_resolvable_private_addr(void* p_cmd_cplt_cback) { |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 54 | // TODO(jpawlowski): should call p_cmd_cplt_cback(); |
| 55 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 56 | void alarm_set_on_queue(alarm_t* alarm, period_ms_t interval_ms, |
| 57 | alarm_callback_t cb, void* data, fixed_queue_t* queue) { |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 58 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 59 | void alarm_cancel(alarm_t* alarm) {} |
| 60 | alarm_t* alarm_new_periodic(const char* name) { return nullptr; } |
| 61 | alarm_t* alarm_new(const char* name) { return nullptr; } |
| 62 | void alarm_free(alarm_t* alarm) {} |
| 63 | const controller_t* controller_get_interface() { return nullptr; } |
| 64 | fixed_queue_t* btu_general_alarm_queue = nullptr; |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 65 | |
| 66 | namespace { |
| 67 | |
| 68 | class AdvertiserHciMock : public BleAdvertiserHciInterface { |
| 69 | public: |
| 70 | AdvertiserHciMock() = default; |
| 71 | ~AdvertiserHciMock() override = default; |
| 72 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 73 | MOCK_METHOD1(ReadInstanceCount, |
| 74 | void(base::Callback<void(uint8_t /* inst_cnt*/)>)); |
| 75 | MOCK_METHOD1(SetAdvertisingEventObserver, |
| 76 | void(AdvertisingEventObserver* observer)); |
| 77 | MOCK_METHOD4(SetAdvertisingData, void(uint8_t, uint8_t*, uint8_t, status_cb)); |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 78 | MOCK_METHOD4(SetScanResponseData, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 79 | void(uint8_t, uint8_t*, uint8_t, status_cb)); |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 80 | MOCK_METHOD3(SetRandomAddress, void(BD_ADDR, uint8_t, status_cb)); |
| 81 | MOCK_METHOD3(Enable, void(uint8_t, uint8_t, status_cb)); |
| 82 | |
| 83 | MOCK_METHOD6(SetParameters1, |
| 84 | void(uint8_t, uint8_t, uint8_t, uint8_t, BD_ADDR, uint8_t)); |
| 85 | MOCK_METHOD6(SetParameters2, |
| 86 | void(BD_ADDR, uint8_t, uint8_t, uint8_t, uint8_t, status_cb)); |
| 87 | |
| 88 | void SetParameters(uint8_t adv_int_min, uint8_t adv_int_max, |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 89 | uint8_t advertising_type, uint8_t own_address_type, |
| 90 | BD_ADDR own_address, uint8_t direct_address_type, |
| 91 | BD_ADDR direct_address, uint8_t channel_map, |
| 92 | uint8_t filter_policy, uint8_t instance, uint8_t tx_power, |
| 93 | status_cb cmd_complete) override { |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 94 | SetParameters1(adv_int_min, adv_int_max, advertising_type, own_address_type, |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 95 | own_address, direct_address_type); |
| 96 | SetParameters2(direct_address, channel_map, filter_policy, instance, |
| 97 | tx_power, cmd_complete); |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | private: |
| 101 | DISALLOW_COPY_AND_ASSIGN(AdvertiserHciMock); |
| 102 | }; |
| 103 | |
| 104 | } // namespace |
| 105 | |
| 106 | class BleAdvertisingManagerTest : public testing::Test { |
| 107 | protected: |
| 108 | int reg_inst_id = -1; |
| 109 | int reg_status = -1; |
| 110 | int set_params_status = -1; |
| 111 | int set_data_status = -1; |
| 112 | int enable_status = -1; |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 113 | int start_advertising_status = -1; |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 114 | std::unique_ptr<AdvertiserHciMock> hci_mock; |
| 115 | |
| 116 | virtual void SetUp() { |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 117 | hci_mock.reset(new AdvertiserHciMock()); |
Jakub Pawlowski | 1dd7751 | 2016-10-10 14:25:52 -0700 | [diff] [blame] | 118 | |
| 119 | base::Callback<void(uint8_t)> inst_cnt_Cb; |
| 120 | EXPECT_CALL(*hci_mock, ReadInstanceCount(_)) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 121 | .Times(Exactly(1)) |
| 122 | .WillOnce(SaveArg<0>(&inst_cnt_Cb)); |
Jakub Pawlowski | 1dd7751 | 2016-10-10 14:25:52 -0700 | [diff] [blame] | 123 | |
| 124 | BleAdvertisingManager::Initialize(hci_mock.get()); |
| 125 | |
| 126 | // we are a truly gracious fake controller, let the command succeed! |
| 127 | inst_cnt_Cb.Run(num_adv_instances); |
| 128 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | virtual void TearDown() { |
| 132 | BleAdvertisingManager::CleanUp(); |
| 133 | hci_mock.reset(); |
| 134 | } |
| 135 | |
| 136 | public: |
| 137 | void RegistrationCb(uint8_t inst_id, uint8_t status) { |
| 138 | reg_inst_id = inst_id; |
| 139 | reg_status = status; |
| 140 | } |
| 141 | |
| 142 | void SetParametersCb(uint8_t status) { set_params_status = status; } |
| 143 | void SetDataCb(uint8_t status) { set_data_status = status; } |
| 144 | void EnableCb(uint8_t status) { enable_status = status; } |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 145 | void StartAdvertisingCb(uint8_t status) { start_advertising_status = status; } |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | TEST_F(BleAdvertisingManagerTest, test_registration) { |
Jakub Pawlowski | 06325e3 | 2016-11-14 11:20:52 -0800 | [diff] [blame] | 149 | for (int i = 0; i < num_adv_instances; i++) { |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 150 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 151 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 152 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 153 | EXPECT_EQ(i, reg_inst_id); |
| 154 | } |
| 155 | |
| 156 | // This call should return an error - no more advertisers left. |
| 157 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 158 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
Jakub Pawlowski | 318e310 | 2016-11-22 12:51:20 -0800 | [diff] [blame] | 159 | EXPECT_EQ(ADVERTISE_FAILED_TOO_MANY_ADVERTISERS, reg_status); |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 160 | // Don't bother checking inst_id, it doesn't matter |
| 161 | |
| 162 | // This will currently trigger a mock message about a call to Enable(). This |
| 163 | // should be fixed in the future- we shouldn't disable non-enabled scan. |
| 164 | BleAdvertisingManager::Get()->Unregister(5); |
| 165 | |
| 166 | // One advertiser was freed, so should be able to register one now |
| 167 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 168 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 169 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 170 | EXPECT_EQ(5, reg_inst_id); |
| 171 | } |
| 172 | |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 173 | /* This test verifies that the following flow is working correctly: register, |
| 174 | * set parameters, set data, enable, ... (advertise) ..., unregister*/ |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 175 | TEST_F(BleAdvertisingManagerTest, test_android_flow) { |
| 176 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 177 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 178 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 179 | int advertiser_id = reg_inst_id; |
| 180 | |
| 181 | status_cb set_params_cb; |
| 182 | tBTM_BLE_ADV_PARAMS params; |
| 183 | EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _)).Times(1); |
| 184 | EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, advertiser_id, _, _)) |
| 185 | .Times(1) |
| 186 | .WillOnce(SaveArg<5>(&set_params_cb)); |
| 187 | BleAdvertisingManager::Get()->SetParameters( |
| 188 | advertiser_id, ¶ms, |
| 189 | base::Bind(&BleAdvertisingManagerTest::SetParametersCb, |
| 190 | base::Unretained(this))); |
| 191 | |
Jakub Pawlowski | 1dd7751 | 2016-10-10 14:25:52 -0700 | [diff] [blame] | 192 | // we are a truly gracious fake controller, let the command succeed! |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 193 | set_params_cb.Run(0); |
| 194 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status); |
| 195 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 196 | |
| 197 | status_cb set_data_cb; |
| 198 | EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, advertiser_id, _)) |
| 199 | .Times(1) |
| 200 | .WillOnce(SaveArg<3>(&set_data_cb)); |
| 201 | BleAdvertisingManager::Get()->SetData( |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 202 | advertiser_id, false, std::vector<uint8_t>(), |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 203 | base::Bind(&BleAdvertisingManagerTest::SetDataCb, |
| 204 | base::Unretained(this))); |
| 205 | set_data_cb.Run(0); |
| 206 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status); |
| 207 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 208 | |
| 209 | status_cb enable_cb; |
| 210 | EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, advertiser_id, _)) |
| 211 | .Times(1) |
| 212 | .WillOnce(SaveArg<2>(&enable_cb)); |
| 213 | BleAdvertisingManager::Get()->Enable( |
| 214 | advertiser_id, true, |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 215 | base::Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), |
| 216 | 0, base::Callback<void(uint8_t)>()); |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 217 | enable_cb.Run(0); |
| 218 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, enable_status); |
| 219 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 220 | |
| 221 | /* fake controller should be advertising */ |
| 222 | |
| 223 | EXPECT_CALL(*hci_mock, Enable(0x00 /* disable */, advertiser_id, _)) |
| 224 | .Times(1) |
| 225 | .WillOnce(SaveArg<2>(&enable_cb)); |
| 226 | BleAdvertisingManager::Get()->Unregister(advertiser_id); |
| 227 | enable_cb.Run(0); |
Jakub Pawlowski | e47b769 | 2016-09-28 07:36:54 -0700 | [diff] [blame] | 228 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 229 | } |
Jakub Pawlowski | 9eaf776 | 2016-10-04 19:30:09 -0700 | [diff] [blame] | 230 | |
| 231 | /* This test verifies that when advertising data is set, tx power and flags will |
| 232 | * be properly filled. */ |
| 233 | TEST_F(BleAdvertisingManagerTest, test_adv_data_filling) { |
| 234 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 235 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 236 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 237 | int advertiser_id = reg_inst_id; |
| 238 | |
| 239 | status_cb set_params_cb; |
| 240 | tBTM_BLE_ADV_PARAMS params; |
| 241 | params.adv_type = BTM_BLE_CONNECT_EVT; |
| 242 | params.tx_power = -15; |
| 243 | EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _)).Times(1); |
| 244 | EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, advertiser_id, |
| 245 | (uint8_t)params.tx_power, _)) |
| 246 | .Times(1) |
| 247 | .WillOnce(SaveArg<5>(&set_params_cb)); |
| 248 | BleAdvertisingManager::Get()->SetParameters( |
| 249 | advertiser_id, ¶ms, |
| 250 | base::Bind(&BleAdvertisingManagerTest::SetParametersCb, |
| 251 | base::Unretained(this))); |
| 252 | |
| 253 | // let the set parameters command succeed! |
| 254 | set_params_cb.Run(0); |
| 255 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status); |
| 256 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 257 | |
| 258 | status_cb set_data_cb; |
| 259 | /* verify that flags will be added, and tx power filled, if call to SetData |
| 260 | * contained only tx power, and the advertisement is connectable */ |
| 261 | uint8_t expected_adv_data[] = {0x02 /* len */, 0x01 /* flags */, |
| 262 | 0x02 /* flags value */, 0x02 /* len */, |
| 263 | 0x0A /* tx_power */, params.tx_power}; |
| 264 | EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, advertiser_id, _)) |
| 265 | .With(Args<1, 0>(ElementsAreArray(expected_adv_data))) |
| 266 | .Times(1) |
| 267 | .WillOnce(SaveArg<3>(&set_data_cb)); |
| 268 | BleAdvertisingManager::Get()->SetData( |
| 269 | advertiser_id, false, |
| 270 | std::vector<uint8_t>({0x02 /* len */, 0x0A /* tx_power */, 0x00}), |
| 271 | base::Bind(&BleAdvertisingManagerTest::SetDataCb, |
| 272 | base::Unretained(this))); |
| 273 | set_data_cb.Run(0); |
| 274 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status); |
| 275 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 276 | } |
| 277 | |
| 278 | /* This test verifies that when advertising is non-connectable, flags will not |
| 279 | * be added. */ |
| 280 | TEST_F(BleAdvertisingManagerTest, test_adv_data_not_filling) { |
| 281 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 282 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 283 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 284 | int advertiser_id = reg_inst_id; |
| 285 | |
| 286 | status_cb set_params_cb; |
| 287 | tBTM_BLE_ADV_PARAMS params; |
| 288 | params.adv_type = BTM_BLE_NON_CONNECT_EVT; |
| 289 | params.tx_power = -15; |
| 290 | EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _)).Times(1); |
| 291 | EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, advertiser_id, |
| 292 | (uint8_t)params.tx_power, _)) |
| 293 | .Times(1) |
| 294 | .WillOnce(SaveArg<5>(&set_params_cb)); |
| 295 | BleAdvertisingManager::Get()->SetParameters( |
| 296 | advertiser_id, ¶ms, |
| 297 | base::Bind(&BleAdvertisingManagerTest::SetParametersCb, |
| 298 | base::Unretained(this))); |
| 299 | |
| 300 | // let the set parameters command succeed! |
| 301 | set_params_cb.Run(0); |
| 302 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status); |
| 303 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 304 | |
| 305 | status_cb set_data_cb; |
| 306 | /* verify that flags will not be added */ |
| 307 | uint8_t expected_adv_data[] = { |
| 308 | 0x02 /* len */, 0xFF /* manufacturer specific */, 0x01 /* data */}; |
| 309 | EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, advertiser_id, _)) |
| 310 | .With(Args<1, 0>(ElementsAreArray(expected_adv_data))) |
| 311 | .Times(1) |
| 312 | .WillOnce(SaveArg<3>(&set_data_cb)); |
| 313 | BleAdvertisingManager::Get()->SetData( |
| 314 | advertiser_id, false, std::vector<uint8_t>({0x02 /* len */, 0xFF, 0x01}), |
| 315 | base::Bind(&BleAdvertisingManagerTest::SetDataCb, |
| 316 | base::Unretained(this))); |
| 317 | set_data_cb.Run(0); |
| 318 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status); |
| 319 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 320 | } |
Jakub Pawlowski | 06325e3 | 2016-11-14 11:20:52 -0800 | [diff] [blame] | 321 | |
| 322 | TEST_F(BleAdvertisingManagerTest, test_reenabling) { |
| 323 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 324 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 325 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 326 | EXPECT_EQ(0, reg_inst_id); |
| 327 | |
| 328 | uint8_t advertiser_id = reg_inst_id; |
| 329 | |
| 330 | status_cb enable_cb; |
| 331 | EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, advertiser_id, _)) |
| 332 | .Times(1) |
| 333 | .WillOnce(SaveArg<2>(&enable_cb)); |
| 334 | BleAdvertisingManager::Get()->OnAdvertisingStateChanged(advertiser_id, 0, 5); |
| 335 | enable_cb.Run(0); |
| 336 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 337 | } |
| 338 | |
| 339 | /* Make sure that instance is not reenabled if it's already disabled */ |
| 340 | TEST_F(BleAdvertisingManagerTest, test_reenabling_disabled_instance) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 341 | uint8_t advertiser_id = 1; // any unregistered value |
Jakub Pawlowski | 06325e3 | 2016-11-14 11:20:52 -0800 | [diff] [blame] | 342 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame^] | 343 | EXPECT_CALL(*hci_mock, Enable(_, _, _)).Times(Exactly(0)); |
Jakub Pawlowski | 06325e3 | 2016-11-14 11:20:52 -0800 | [diff] [blame] | 344 | BleAdvertisingManager::Get()->OnAdvertisingStateChanged(advertiser_id, 0, 5); |
| 345 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
Jakub Pawlowski | a75087e | 2016-11-17 13:27:28 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* This test verifies that the only flow that is currently used on Android, is |
| 349 | * working correctly in happy case scenario. */ |
| 350 | TEST_F(BleAdvertisingManagerTest, test_start_advertising) { |
| 351 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 352 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 353 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 354 | int advertiser_id = reg_inst_id; |
| 355 | |
| 356 | std::vector<uint8_t> adv_data; |
| 357 | std::vector<uint8_t> scan_resp; |
| 358 | tBTM_BLE_ADV_PARAMS params; |
| 359 | |
| 360 | status_cb set_params_cb; |
| 361 | status_cb set_data_cb; |
| 362 | status_cb set_scan_resp_data_cb; |
| 363 | status_cb enable_cb; |
| 364 | EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _)).Times(1); |
| 365 | EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, advertiser_id, _, _)) |
| 366 | .Times(1) |
| 367 | .WillOnce(SaveArg<5>(&set_params_cb)); |
| 368 | EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, advertiser_id, _)) |
| 369 | .Times(1) |
| 370 | .WillOnce(SaveArg<3>(&set_data_cb)); |
| 371 | EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, advertiser_id, _)) |
| 372 | .Times(1) |
| 373 | .WillOnce(SaveArg<3>(&set_scan_resp_data_cb)); |
| 374 | EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, advertiser_id, _)) |
| 375 | .Times(1) |
| 376 | .WillOnce(SaveArg<2>(&enable_cb)); |
| 377 | |
| 378 | BleAdvertisingManager::Get()->StartAdvertising( |
| 379 | advertiser_id, base::Bind(&BleAdvertisingManagerTest::StartAdvertisingCb, |
| 380 | base::Unretained(this)), |
| 381 | ¶ms, adv_data, scan_resp, 0, base::Callback<void(uint8_t)>()); |
| 382 | |
| 383 | // we are a truly gracious fake controller, let the commands succeed! |
| 384 | set_params_cb.Run(0); |
| 385 | set_data_cb.Run(0); |
| 386 | set_scan_resp_data_cb.Run(0); |
| 387 | enable_cb.Run(0); |
| 388 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_status); |
| 389 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 390 | |
| 391 | // ... advertising ... |
| 392 | |
| 393 | // Disable advertiser |
| 394 | status_cb disable_cb; |
| 395 | EXPECT_CALL(*hci_mock, Enable(0x00 /* disable */, advertiser_id, _)) |
| 396 | .Times(1) |
| 397 | .WillOnce(SaveArg<2>(&disable_cb)); |
| 398 | BleAdvertisingManager::Get()->Unregister(advertiser_id); |
| 399 | disable_cb.Run(0); |
| 400 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 401 | } |
| 402 | |
| 403 | TEST_F(BleAdvertisingManagerTest, test_start_advertising_set_params_failed) { |
| 404 | BleAdvertisingManager::Get()->RegisterAdvertiser(base::Bind( |
| 405 | &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this))); |
| 406 | EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status); |
| 407 | int advertiser_id = reg_inst_id; |
| 408 | |
| 409 | std::vector<uint8_t> adv_data; |
| 410 | std::vector<uint8_t> scan_resp; |
| 411 | tBTM_BLE_ADV_PARAMS params; |
| 412 | |
| 413 | status_cb set_params_cb; |
| 414 | EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _)).Times(1); |
| 415 | EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, advertiser_id, _, _)) |
| 416 | .Times(1) |
| 417 | .WillOnce(SaveArg<5>(&set_params_cb)); |
| 418 | |
| 419 | EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, advertiser_id, _)) |
| 420 | .Times(Exactly(0)); |
| 421 | |
| 422 | BleAdvertisingManager::Get()->StartAdvertising( |
| 423 | advertiser_id, base::Bind(&BleAdvertisingManagerTest::StartAdvertisingCb, |
| 424 | base::Unretained(this)), |
| 425 | ¶ms, adv_data, scan_resp, 0, base::Callback<void(uint8_t)>()); |
| 426 | |
| 427 | // set params failed |
| 428 | set_params_cb.Run(0x01); |
| 429 | |
| 430 | // Expect the whole flow to fail right away |
| 431 | EXPECT_EQ(BTM_BLE_MULTI_ADV_FAILURE, start_advertising_status); |
| 432 | ::testing::Mock::VerifyAndClearExpectations(hci_mock.get()); |
| 433 | } |