Chromium Code Reviews| 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() {} | |
|
Jeffrey Yasskin
2016/08/27 00:35:46
I believe you can also omit this line since it's i
juncai
2016/08/27 02:15:56
Tried removing this, but got compile error:
"error
Jeffrey Yasskin
2016/09/02 20:40:58
Ah, it's because DISALLOW_COPY_AND_ASSIGN provides
juncai
2016/09/02 22:41:45
Acknowledged.
| |
| 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, ChangeAdapterPresence) { | |
| 247 EXPECT_EQ(l10n_util::GetStringUTF16( | |
| 248 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), | |
| 249 bluetooth_chooser_controller_.GetNoOptionsText()); | |
| 250 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus()); | |
| 251 | |
| 252 EXPECT_CALL( | |
|
Jeffrey Yasskin
2016/08/27 00:35:45
Can you split this into 3 tests, with the division
juncai
2016/08/27 02:15:56
Done.
| |
| 253 mock_bluetooth_chooser_view_, | |
| 254 OnAdapterEnabledChanged(false /* Bluetooth adapter is turned off */)) | |
| 255 .Times(1); | |
| 256 bluetooth_chooser_controller_.OnAdapterPresenceChanged( | |
| 257 content::BluetoothChooser::AdapterPresence::POWERED_OFF); | |
| 258 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); | |
| 259 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_ADAPTER_OFF), | |
| 260 bluetooth_chooser_controller_.GetNoOptionsText()); | |
| 261 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus()); | |
| 262 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); | |
| 263 | |
| 264 EXPECT_CALL( | |
| 265 mock_bluetooth_chooser_view_, | |
| 266 OnAdapterEnabledChanged(true /* Bluetooth adapter is turned on */)) | |
| 267 .Times(1); | |
| 268 bluetooth_chooser_controller_.OnAdapterPresenceChanged( | |
| 269 content::BluetoothChooser::AdapterPresence::POWERED_ON); | |
| 270 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); | |
| 271 EXPECT_EQ(l10n_util::GetStringUTF16( | |
| 272 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), | |
| 273 bluetooth_chooser_controller_.GetNoOptionsText()); | |
| 274 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN), | |
| 275 bluetooth_chooser_controller_.GetStatus()); | |
| 276 } | |
| 277 | |
| 278 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, ChangeDiscoveryState) { | |
| 279 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus()); | |
| 280 | |
| 281 EXPECT_CALL( | |
| 282 mock_bluetooth_chooser_view_, | |
| 283 OnRefreshStateChanged(true /* Refreshing options is in progress */)) | |
| 284 .Times(1); | |
| 285 bluetooth_chooser_controller_.OnDiscoveryStateChanged( | |
| 286 content::BluetoothChooser::DiscoveryState::DISCOVERING); | |
| 287 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING), | |
| 288 bluetooth_chooser_controller_.GetStatus()); | |
| 289 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); | |
| 290 | |
| 291 EXPECT_CALL(mock_bluetooth_chooser_view_, | |
| 292 OnRefreshStateChanged(false /* Refreshing options is complete */)) | |
| 293 .Times(1); | |
| 294 bluetooth_chooser_controller_.OnDiscoveryStateChanged( | |
| 295 content::BluetoothChooser::DiscoveryState::IDLE); | |
| 296 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN), | |
| 297 bluetooth_chooser_controller_.GetStatus()); | |
| 298 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_); | |
| 299 | |
| 300 EXPECT_CALL(mock_bluetooth_chooser_view_, | |
| 301 OnRefreshStateChanged(false /* Refreshing options is complete */)) | |
| 302 .Times(1); | |
| 303 bluetooth_chooser_controller_.OnDiscoveryStateChanged( | |
| 304 content::BluetoothChooser::DiscoveryState::FAILED_TO_START); | |
| 305 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN), | |
| 306 bluetooth_chooser_controller_.GetStatus()); | |
| 307 } | |
| 308 | |
| 309 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, RefreshOptions) { | |
| 310 bluetooth_chooser_controller_.RefreshOptions(); | |
| 311 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions()); | |
| 312 EXPECT_EQ(content::BluetoothChooser::Event::RESCAN, last_event_); | |
| 313 EXPECT_EQ(std::string(), last_device_id_); | |
| 314 } | |
| 315 | |
| 316 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, | |
| 317 SelectOneDeviceAndEventHandlerCalled) { | |
|
Jeffrey Yasskin
2016/08/27 00:35:46
I'd name these more like a sentence: "SelectingOne
juncai
2016/08/27 02:15:56
Done.
| |
| 318 bluetooth_chooser_controller_.Select(0); | |
| 319 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, last_event_); | |
| 320 EXPECT_EQ("id_a", last_device_id_); | |
| 321 | |
| 322 bluetooth_chooser_controller_.Select(1); | |
| 323 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, last_event_); | |
| 324 EXPECT_EQ("id_b", last_device_id_); | |
| 325 | |
| 326 bluetooth_chooser_controller_.Select(2); | |
| 327 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, last_event_); | |
| 328 EXPECT_EQ("id_c", last_device_id_); | |
| 329 } | |
| 330 | |
| 331 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, | |
| 332 CancelAndEventHandlerCalled) { | |
| 333 bluetooth_chooser_controller_.Cancel(); | |
| 334 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, last_event_); | |
| 335 EXPECT_EQ(std::string(), last_device_id_); | |
| 336 } | |
| 337 | |
| 338 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, | |
| 339 CloseAndEventHandlerCalled) { | |
| 340 bluetooth_chooser_controller_.Close(); | |
| 341 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, last_event_); | |
| 342 EXPECT_EQ(std::string(), last_device_id_); | |
| 343 } | |
| OLD | NEW |