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 <string> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h" |
| 11 #include "chrome/grit/generated_resources.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 class MockBluetoothChooserView : public ChooserController::View { |
| 19 public: |
| 20 MockBluetoothChooserView() {} |
| 21 |
| 22 // ChooserController::View: |
| 23 MOCK_METHOD0(OnOptionsInitialized, void()); |
| 24 MOCK_METHOD1(OnOptionAdded, void(size_t index)); |
| 25 MOCK_METHOD1(OnOptionRemoved, void(size_t index)); |
| 26 MOCK_METHOD1(OnOptionUpdated, void(size_t index)); |
| 27 MOCK_METHOD1(OnAdapterEnabledChanged, void(bool enabled)); |
| 28 MOCK_METHOD1(OnRefreshStateChanged, void(bool enabled)); |
| 29 |
| 30 private: |
| 31 DISALLOW_COPY_AND_ASSIGN(MockBluetoothChooserView); |
| 32 }; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 class BluetoothChooserControllerTest : public testing::Test { |
| 37 public: |
| 38 BluetoothChooserControllerTest() |
| 39 : bluetooth_chooser_controller_( |
| 40 nullptr, |
| 41 base::Bind(&BluetoothChooserControllerTest::OnBluetoothChooserEvent, |
| 42 base::Unretained(this))) { |
| 43 bluetooth_chooser_controller_.set_view(&mock_bluetooth_chooser_view_); |
| 44 } |
| 45 |
| 46 protected: |
| 47 void OnBluetoothChooserEvent(content::BluetoothChooser::Event event, |
| 48 const std::string& device_id) { |
| 49 last_event_ = event; |
| 50 last_device_id_ = device_id; |
| 51 } |
| 52 |
| 53 BluetoothChooserController bluetooth_chooser_controller_; |
| 54 MockBluetoothChooserView mock_bluetooth_chooser_view_; |
| 55 content::BluetoothChooser::Event last_event_; |
| 56 std::string last_device_id_; |
| 57 |
| 58 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(BluetoothChooserControllerTest); |
| 60 }; |
| 61 |
| 62 class BluetoothChooserControllerWithDevicesAddedTest |
| 63 : public BluetoothChooserControllerTest { |
| 64 public: |
| 65 BluetoothChooserControllerWithDevicesAddedTest() { |
| 66 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 67 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 68 true /* is_gatt_connected */, true /* is_paired */, |
| 69 -1 /* signal_strength_level */); |
| 70 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 71 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"), |
| 72 true /* is_gatt_connected */, true /* is_paired */, |
| 73 0 /* signal_strength_level */); |
| 74 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 75 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"), |
| 76 true /* is_gatt_connected */, true /* is_paired */, |
| 77 1 /* signal_strength_level */); |
| 78 } |
| 79 }; |
| 80 |
| 81 TEST_F(BluetoothChooserControllerTest, AddDevice) { |
| 82 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(0)).Times(1); |
| 83 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 84 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 85 true /* is_gatt_connected */, true /* is_paired */, |
| 86 -1 /* signal_strength_level */); |
| 87 EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions()); |
| 88 EXPECT_EQ(base::ASCIIToUTF16("a"), |
| 89 bluetooth_chooser_controller_.GetOption(0)); |
| 90 EXPECT_EQ(-1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0)); |
| 91 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); |
| 92 |
| 93 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(1)).Times(1); |
| 94 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 95 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"), |
| 96 true /* is_gatt_connected */, true /* is_paired */, |
| 97 0 /* signal_strength_level */); |
| 98 EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions()); |
| 99 EXPECT_EQ(base::ASCIIToUTF16("b"), |
| 100 bluetooth_chooser_controller_.GetOption(1)); |
| 101 EXPECT_EQ(0, bluetooth_chooser_controller_.GetSignalStrengthLevel(1)); |
| 102 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); |
| 103 |
| 104 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(2)).Times(1); |
| 105 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 106 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"), |
| 107 true /* is_gatt_connected */, true /* is_paired */, |
| 108 1 /* signal_strength_level */); |
| 109 EXPECT_EQ(3u, bluetooth_chooser_controller_.NumOptions()); |
| 110 EXPECT_EQ(base::ASCIIToUTF16("c"), |
| 111 bluetooth_chooser_controller_.GetOption(2)); |
| 112 EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(2)); |
| 113 } |
| 114 |
| 115 TEST_F(BluetoothChooserControllerTest, RemoveDevice) { |
| 116 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 117 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 118 true /* is_gatt_connected */, true /* is_paired */, |
| 119 -1 /* signal_strength_level */); |
| 120 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 121 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"), |
| 122 true /* is_gatt_connected */, true /* is_paired */, |
| 123 0 /* signal_strength_level */); |
| 124 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 125 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"), |
| 126 true /* is_gatt_connected */, true /* is_paired */, |
| 127 1 /* signal_strength_level */); |
| 128 |
| 129 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(1)).Times(1); |
| 130 bluetooth_chooser_controller_.RemoveDevice("id_b"); |
| 131 EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions()); |
| 132 EXPECT_EQ(base::ASCIIToUTF16("a"), |
| 133 bluetooth_chooser_controller_.GetOption(0)); |
| 134 EXPECT_EQ(base::ASCIIToUTF16("c"), |
| 135 bluetooth_chooser_controller_.GetOption(1)); |
| 136 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); |
| 137 |
| 138 // Remove a non-existent device, the number of devices should not change. |
| 139 bluetooth_chooser_controller_.RemoveDevice("non-existent"); |
| 140 EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions()); |
| 141 EXPECT_EQ(base::ASCIIToUTF16("a"), |
| 142 bluetooth_chooser_controller_.GetOption(0)); |
| 143 EXPECT_EQ(base::ASCIIToUTF16("c"), |
| 144 bluetooth_chooser_controller_.GetOption(1)); |
| 145 |
| 146 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1); |
| 147 bluetooth_chooser_controller_.RemoveDevice("id_a"); |
| 148 EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions()); |
| 149 EXPECT_EQ(base::ASCIIToUTF16("c"), |
| 150 bluetooth_chooser_controller_.GetOption(0)); |
| 151 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); |
| 152 |
| 153 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1); |
| 154 bluetooth_chooser_controller_.RemoveDevice("id_c"); |
| 155 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); |
| 156 } |
| 157 |
| 158 TEST_F(BluetoothChooserControllerTest, MultipleDevicesWithSameNameShowIds) { |
| 159 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 160 "id_a_1", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 161 true /* is_gatt_connected */, true /* is_paired */, |
| 162 -1 /* signal_strength_level */); |
| 163 EXPECT_EQ(base::ASCIIToUTF16("a"), |
| 164 bluetooth_chooser_controller_.GetOption(0)); |
| 165 |
| 166 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 167 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"), |
| 168 true /* is_gatt_connected */, true /* is_paired */, |
| 169 0 /* signal_strength_level */); |
| 170 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 171 "id_a_2", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 172 true /* is_gatt_connected */, true /* is_paired */, |
| 173 1 /* signal_strength_level */); |
| 174 EXPECT_EQ(base::ASCIIToUTF16("a (id_a_1)"), |
| 175 bluetooth_chooser_controller_.GetOption(0)); |
| 176 EXPECT_EQ(base::ASCIIToUTF16("b"), |
| 177 bluetooth_chooser_controller_.GetOption(1)); |
| 178 EXPECT_EQ(base::ASCIIToUTF16("a (id_a_2)"), |
| 179 bluetooth_chooser_controller_.GetOption(2)); |
| 180 |
| 181 bluetooth_chooser_controller_.RemoveDevice("id_a_1"); |
| 182 EXPECT_EQ(base::ASCIIToUTF16("b"), |
| 183 bluetooth_chooser_controller_.GetOption(0)); |
| 184 EXPECT_EQ(base::ASCIIToUTF16("a"), |
| 185 bluetooth_chooser_controller_.GetOption(1)); |
| 186 } |
| 187 |
| 188 TEST_F(BluetoothChooserControllerTest, UpdateDeviceName) { |
| 189 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 190 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 191 true /* is_gatt_connected */, true /* is_paired */, |
| 192 -1 /* signal_strength_level */); |
| 193 EXPECT_EQ(base::ASCIIToUTF16("a"), |
| 194 bluetooth_chooser_controller_.GetOption(0)); |
| 195 |
| 196 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1); |
| 197 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 198 "id_a", false /* should_update_name */, base::ASCIIToUTF16("aa"), |
| 199 true /* is_gatt_connected */, true /* is_paired */, |
| 200 -1 /* signal_strength_level */); |
| 201 // The name is still "a" since |should_update_name| is false. |
| 202 EXPECT_EQ(base::ASCIIToUTF16("a"), |
| 203 bluetooth_chooser_controller_.GetOption(0)); |
| 204 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); |
| 205 |
| 206 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1); |
| 207 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 208 "id_a", true /* should_update_name */, base::ASCIIToUTF16("aa"), |
| 209 true /* is_gatt_connected */, true /* is_paired */, |
| 210 -1 /* signal_strength_level */); |
| 211 EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions()); |
| 212 EXPECT_EQ(base::ASCIIToUTF16("aa"), |
| 213 bluetooth_chooser_controller_.GetOption(0)); |
| 214 |
| 215 bluetooth_chooser_controller_.RemoveDevice("id_a"); |
| 216 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); |
| 217 } |
| 218 |
| 219 TEST_F(BluetoothChooserControllerTest, UpdateDeviceSignalStrengthLevel) { |
| 220 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 221 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 222 true /* is_gatt_connected */, true /* is_paired */, |
| 223 -1 /* signal_strength_level */); |
| 224 EXPECT_EQ(-1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0)); |
| 225 |
| 226 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1); |
| 227 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 228 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 229 true /* is_gatt_connected */, true /* is_paired */, |
| 230 1 /* signal_strength_level */); |
| 231 EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0)); |
| 232 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); |
| 233 |
| 234 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1); |
| 235 // When Bluetooth device scanning stops, an update is sent and the signal |
| 236 // strength level is -1, and in this case, should still use the previously |
| 237 // stored signal strength level. So here the signal strength level is |
| 238 // still 1. |
| 239 bluetooth_chooser_controller_.AddOrUpdateDevice( |
| 240 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"), |
| 241 true /* is_gatt_connected */, true /* is_paired */, |
| 242 -1 /* signal_strength_level */); |
| 243 EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0)); |
| 244 } |
| 245 |
| 246 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, |
| 247 InitialNoOptionsTextAndStatusText) { |
| 248 EXPECT_EQ(l10n_util::GetStringUTF16( |
| 249 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), |
| 250 bluetooth_chooser_controller_.GetNoOptionsText()); |
| 251 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus()); |
| 252 } |
| 253 |
| 254 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, |
| 255 BluetoothAdapterTurnedOff) { |
| 256 EXPECT_CALL( |
| 257 mock_bluetooth_chooser_view_, |
| 258 OnAdapterEnabledChanged(false /* Bluetooth adapter is turned off */)) |
| 259 .Times(1); |
| 260 bluetooth_chooser_controller_.OnAdapterPresenceChanged( |
| 261 content::BluetoothChooser::AdapterPresence::POWERED_OFF); |
| 262 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); |
| 263 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_ADAPTER_OFF), |
| 264 bluetooth_chooser_controller_.GetNoOptionsText()); |
| 265 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus()); |
| 266 } |
| 267 |
| 268 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, |
| 269 BluetoothAdapterTurnedOn) { |
| 270 EXPECT_CALL( |
| 271 mock_bluetooth_chooser_view_, |
| 272 OnAdapterEnabledChanged(true /* Bluetooth adapter is turned on */)) |
| 273 .Times(1); |
| 274 bluetooth_chooser_controller_.OnAdapterPresenceChanged( |
| 275 content::BluetoothChooser::AdapterPresence::POWERED_ON); |
| 276 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); |
| 277 EXPECT_EQ(l10n_util::GetStringUTF16( |
| 278 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), |
| 279 bluetooth_chooser_controller_.GetNoOptionsText()); |
| 280 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN), |
| 281 bluetooth_chooser_controller_.GetStatus()); |
| 282 } |
| 283 |
| 284 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, DiscoveringState) { |
| 285 EXPECT_CALL( |
| 286 mock_bluetooth_chooser_view_, |
| 287 OnRefreshStateChanged(true /* Refreshing options is in progress */)) |
| 288 .Times(1); |
| 289 bluetooth_chooser_controller_.OnDiscoveryStateChanged( |
| 290 content::BluetoothChooser::DiscoveryState::DISCOVERING); |
| 291 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING), |
| 292 bluetooth_chooser_controller_.GetStatus()); |
| 293 } |
| 294 |
| 295 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, IdleState) { |
| 296 EXPECT_CALL(mock_bluetooth_chooser_view_, |
| 297 OnRefreshStateChanged(false /* Refreshing options is complete */)) |
| 298 .Times(1); |
| 299 bluetooth_chooser_controller_.OnDiscoveryStateChanged( |
| 300 content::BluetoothChooser::DiscoveryState::IDLE); |
| 301 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN), |
| 302 bluetooth_chooser_controller_.GetStatus()); |
| 303 } |
| 304 |
| 305 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, FailedToStartState) { |
| 306 EXPECT_CALL(mock_bluetooth_chooser_view_, |
| 307 OnRefreshStateChanged(false /* Refreshing options is complete */)) |
| 308 .Times(1); |
| 309 bluetooth_chooser_controller_.OnDiscoveryStateChanged( |
| 310 content::BluetoothChooser::DiscoveryState::FAILED_TO_START); |
| 311 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN), |
| 312 bluetooth_chooser_controller_.GetStatus()); |
| 313 } |
| 314 |
| 315 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, RefreshOptions) { |
| 316 bluetooth_chooser_controller_.RefreshOptions(); |
| 317 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); |
| 318 EXPECT_EQ(content::BluetoothChooser::Event::RESCAN, last_event_); |
| 319 EXPECT_EQ(std::string(), last_device_id_); |
| 320 } |
| 321 |
| 322 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, |
| 323 SelectingOneDeviceShouldCallEventHandler) { |
| 324 bluetooth_chooser_controller_.Select(0); |
| 325 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, last_event_); |
| 326 EXPECT_EQ("id_a", last_device_id_); |
| 327 } |
| 328 |
| 329 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, |
| 330 CancelShouldCallEventHandler) { |
| 331 bluetooth_chooser_controller_.Cancel(); |
| 332 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, last_event_); |
| 333 EXPECT_EQ(std::string(), last_device_id_); |
| 334 } |
| 335 |
| 336 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, |
| 337 CloseShouldCallEventHandler) { |
| 338 bluetooth_chooser_controller_.Close(); |
| 339 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, last_event_); |
| 340 EXPECT_EQ(std::string(), last_device_id_); |
| 341 } |
OLD | NEW |