Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/ui/bluetooth/bluetooth_chooser_controller_unittest.cc

Issue 2275173002: Add unit test for BluetoothChooserController (Closed)
Patch Set: address more comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 if (event == content::BluetoothChooser::Event::RESCAN) {
50 bluetooth_chooser_controller_.OnDiscoveryStateChanged(
51 content::BluetoothChooser::DiscoveryState::DISCOVERING);
52 }
53
54 last_event_ = event;
55 last_device_id_ = device_id;
56 }
57
58 BluetoothChooserController bluetooth_chooser_controller_;
59 MockBluetoothChooserView mock_bluetooth_chooser_view_;
60 content::BluetoothChooser::Event last_event_;
61 std::string last_device_id_;
62
63 private:
64 DISALLOW_COPY_AND_ASSIGN(BluetoothChooserControllerTest);
65 };
66
67 class BluetoothChooserControllerWithDevicesAddedTest
68 : public BluetoothChooserControllerTest {
69 public:
70 BluetoothChooserControllerWithDevicesAddedTest() {
71 bluetooth_chooser_controller_.AddOrUpdateDevice(
72 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
73 true /* is_gatt_connected */, true /* is_paired */,
74 -1 /* signal_strength_level */);
75 bluetooth_chooser_controller_.AddOrUpdateDevice(
76 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
77 true /* is_gatt_connected */, true /* is_paired */,
78 0 /* signal_strength_level */);
79 bluetooth_chooser_controller_.AddOrUpdateDevice(
80 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
81 true /* is_gatt_connected */, true /* is_paired */,
82 1 /* signal_strength_level */);
83 }
84 };
85
86 TEST_F(BluetoothChooserControllerTest, AddDevice) {
87 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(0)).Times(1);
88 bluetooth_chooser_controller_.AddOrUpdateDevice(
89 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
90 true /* is_gatt_connected */, true /* is_paired */,
91 -1 /* signal_strength_level */);
92 EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions());
93 EXPECT_EQ(base::ASCIIToUTF16("a"),
94 bluetooth_chooser_controller_.GetOption(0));
95 EXPECT_EQ(-1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
96 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
97
98 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(1)).Times(1);
99 bluetooth_chooser_controller_.AddOrUpdateDevice(
100 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
101 true /* is_gatt_connected */, true /* is_paired */,
102 0 /* signal_strength_level */);
103 EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions());
104 EXPECT_EQ(base::ASCIIToUTF16("b"),
105 bluetooth_chooser_controller_.GetOption(1));
106 EXPECT_EQ(0, bluetooth_chooser_controller_.GetSignalStrengthLevel(1));
107 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
108
109 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionAdded(2)).Times(1);
110 bluetooth_chooser_controller_.AddOrUpdateDevice(
111 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
112 true /* is_gatt_connected */, true /* is_paired */,
113 1 /* signal_strength_level */);
114 EXPECT_EQ(3u, bluetooth_chooser_controller_.NumOptions());
115 EXPECT_EQ(base::ASCIIToUTF16("c"),
116 bluetooth_chooser_controller_.GetOption(2));
117 EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(2));
118 }
119
120 TEST_F(BluetoothChooserControllerTest, RemoveDevice) {
121 bluetooth_chooser_controller_.AddOrUpdateDevice(
122 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
123 true /* is_gatt_connected */, true /* is_paired */,
124 -1 /* signal_strength_level */);
125 bluetooth_chooser_controller_.AddOrUpdateDevice(
126 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
127 true /* is_gatt_connected */, true /* is_paired */,
128 0 /* signal_strength_level */);
129 bluetooth_chooser_controller_.AddOrUpdateDevice(
130 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
131 true /* is_gatt_connected */, true /* is_paired */,
132 1 /* signal_strength_level */);
133
134 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(1)).Times(1);
135 bluetooth_chooser_controller_.RemoveDevice("id_b");
136 EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions());
137 EXPECT_EQ(base::ASCIIToUTF16("a"),
138 bluetooth_chooser_controller_.GetOption(0));
139 EXPECT_EQ(base::ASCIIToUTF16("c"),
140 bluetooth_chooser_controller_.GetOption(1));
141 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
142
143 // Remove a non-existent device, the number of devices should not change.
144 bluetooth_chooser_controller_.RemoveDevice("non-existent");
145 EXPECT_EQ(2u, bluetooth_chooser_controller_.NumOptions());
146 EXPECT_EQ(base::ASCIIToUTF16("a"),
147 bluetooth_chooser_controller_.GetOption(0));
148 EXPECT_EQ(base::ASCIIToUTF16("c"),
149 bluetooth_chooser_controller_.GetOption(1));
150
151 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
152 bluetooth_chooser_controller_.RemoveDevice("id_a");
153 EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions());
154 EXPECT_EQ(base::ASCIIToUTF16("c"),
155 bluetooth_chooser_controller_.GetOption(0));
156 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
157
158 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
159 bluetooth_chooser_controller_.RemoveDevice("id_c");
160 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
161 }
162
163 TEST_F(BluetoothChooserControllerTest, MultipleDevicesWithSameNameShowIds) {
164 bluetooth_chooser_controller_.AddOrUpdateDevice(
165 "id_a_1", false /* should_update_name */, base::ASCIIToUTF16("a"),
166 true /* is_gatt_connected */, true /* is_paired */,
167 -1 /* signal_strength_level */);
168 EXPECT_EQ(base::ASCIIToUTF16("a"),
169 bluetooth_chooser_controller_.GetOption(0));
170
171 bluetooth_chooser_controller_.AddOrUpdateDevice(
172 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
173 true /* is_gatt_connected */, true /* is_paired */,
174 0 /* signal_strength_level */);
175 bluetooth_chooser_controller_.AddOrUpdateDevice(
176 "id_a_2", false /* should_update_name */, base::ASCIIToUTF16("a"),
177 true /* is_gatt_connected */, true /* is_paired */,
178 1 /* signal_strength_level */);
179 EXPECT_EQ(base::ASCIIToUTF16("a (id_a_1)"),
180 bluetooth_chooser_controller_.GetOption(0));
181 EXPECT_EQ(base::ASCIIToUTF16("b"),
182 bluetooth_chooser_controller_.GetOption(1));
183 EXPECT_EQ(base::ASCIIToUTF16("a (id_a_2)"),
184 bluetooth_chooser_controller_.GetOption(2));
185
186 bluetooth_chooser_controller_.RemoveDevice("id_a_1");
187 EXPECT_EQ(base::ASCIIToUTF16("b"),
188 bluetooth_chooser_controller_.GetOption(0));
189 EXPECT_EQ(base::ASCIIToUTF16("a"),
190 bluetooth_chooser_controller_.GetOption(1));
191 }
192
193 TEST_F(BluetoothChooserControllerTest, UpdateDeviceName) {
194 bluetooth_chooser_controller_.AddOrUpdateDevice(
195 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
196 true /* is_gatt_connected */, true /* is_paired */,
197 -1 /* signal_strength_level */);
198 EXPECT_EQ(base::ASCIIToUTF16("a"),
199 bluetooth_chooser_controller_.GetOption(0));
200
201 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
202 bluetooth_chooser_controller_.AddOrUpdateDevice(
203 "id_a", false /* should_update_name */, base::ASCIIToUTF16("aa"),
204 true /* is_gatt_connected */, true /* is_paired */,
205 -1 /* signal_strength_level */);
206 // The name is still "a" since |should_update_name| is false.
207 EXPECT_EQ(base::ASCIIToUTF16("a"),
208 bluetooth_chooser_controller_.GetOption(0));
209 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
210
211 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
212 bluetooth_chooser_controller_.AddOrUpdateDevice(
213 "id_a", true /* should_update_name */, base::ASCIIToUTF16("aa"),
214 true /* is_gatt_connected */, true /* is_paired */,
215 -1 /* signal_strength_level */);
216 EXPECT_EQ(1u, bluetooth_chooser_controller_.NumOptions());
217 EXPECT_EQ(base::ASCIIToUTF16("aa"),
218 bluetooth_chooser_controller_.GetOption(0));
219
220 bluetooth_chooser_controller_.RemoveDevice("id_a");
221 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
222 }
223
224 TEST_F(BluetoothChooserControllerTest, UpdateDeviceSignalStrengthLevel) {
225 bluetooth_chooser_controller_.AddOrUpdateDevice(
226 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
227 true /* is_gatt_connected */, true /* is_paired */,
228 -1 /* signal_strength_level */);
229 EXPECT_EQ(-1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
230
231 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
232 bluetooth_chooser_controller_.AddOrUpdateDevice(
233 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
234 true /* is_gatt_connected */, true /* is_paired */,
235 1 /* signal_strength_level */);
236 EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
237 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
238
239 EXPECT_CALL(mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
240 // When Bluetooth device scanning stops, an update is sent and the signal
241 // strength level is -1, and in this case, should still use the previously
242 // stored signal strength level. So here the signal strength level is
243 // still 1.
244 bluetooth_chooser_controller_.AddOrUpdateDevice(
245 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
246 true /* is_gatt_connected */, true /* is_paired */,
247 -1 /* signal_strength_level */);
248 EXPECT_EQ(1, bluetooth_chooser_controller_.GetSignalStrengthLevel(0));
249 }
250
251 TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
252 InitialNoOptionsTextAndStatusText) {
253 EXPECT_EQ(l10n_util::GetStringUTF16(
254 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
255 bluetooth_chooser_controller_.GetNoOptionsText());
256 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus());
257 }
258
259 TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
260 BluetoothAdapterTurnedOff) {
261 EXPECT_CALL(
262 mock_bluetooth_chooser_view_,
263 OnAdapterEnabledChanged(false /* Bluetooth adapter is turned off */))
264 .Times(1);
265 bluetooth_chooser_controller_.OnAdapterPresenceChanged(
266 content::BluetoothChooser::AdapterPresence::POWERED_OFF);
267 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
268 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_ADAPTER_OFF),
269 bluetooth_chooser_controller_.GetNoOptionsText());
270 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus());
271 }
272
273 TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
274 BluetoothAdapterTurnedOn) {
275 EXPECT_CALL(
276 mock_bluetooth_chooser_view_,
277 OnAdapterEnabledChanged(true /* Bluetooth adapter is turned on */))
278 .Times(1);
279 bluetooth_chooser_controller_.OnAdapterPresenceChanged(
280 content::BluetoothChooser::AdapterPresence::POWERED_ON);
281 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
282 EXPECT_EQ(l10n_util::GetStringUTF16(
283 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
284 bluetooth_chooser_controller_.GetNoOptionsText());
285 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
286 bluetooth_chooser_controller_.GetStatus());
287 }
288
289 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, ChangeDiscoveryState) {
290 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_.GetStatus());
291
292 EXPECT_CALL(
293 mock_bluetooth_chooser_view_,
294 OnRefreshStateChanged(true /* Refreshing options is in progress */))
295 .Times(1);
296 bluetooth_chooser_controller_.OnDiscoveryStateChanged(
297 content::BluetoothChooser::DiscoveryState::DISCOVERING);
298 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING),
299 bluetooth_chooser_controller_.GetStatus());
300 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
301
302 EXPECT_CALL(mock_bluetooth_chooser_view_,
Jeffrey Yasskin 2016/09/02 20:40:58 Can you split this test into 3 with clear names al
juncai 2016/09/02 22:41:45 Done.
303 OnRefreshStateChanged(false /* Refreshing options is complete */))
304 .Times(1);
305 bluetooth_chooser_controller_.OnDiscoveryStateChanged(
306 content::BluetoothChooser::DiscoveryState::IDLE);
307 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
308 bluetooth_chooser_controller_.GetStatus());
309 testing::Mock::VerifyAndClearExpectations(&mock_bluetooth_chooser_view_);
310
311 EXPECT_CALL(mock_bluetooth_chooser_view_,
312 OnRefreshStateChanged(false /* Refreshing options is complete */))
313 .Times(1);
314 bluetooth_chooser_controller_.OnDiscoveryStateChanged(
315 content::BluetoothChooser::DiscoveryState::FAILED_TO_START);
316 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
317 bluetooth_chooser_controller_.GetStatus());
318 }
319
320 TEST_F(BluetoothChooserControllerWithDevicesAddedTest, RefreshOptions) {
321 EXPECT_CALL(
322 mock_bluetooth_chooser_view_,
323 OnRefreshStateChanged(true /* Refreshing options is in progress */))
324 .Times(1);
325 bluetooth_chooser_controller_.RefreshOptions();
326 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING),
327 bluetooth_chooser_controller_.GetStatus());
328 EXPECT_EQ(0u, bluetooth_chooser_controller_.NumOptions());
329 EXPECT_EQ(content::BluetoothChooser::Event::RESCAN, last_event_);
330 EXPECT_EQ(std::string(), last_device_id_);
331 }
332
333 TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
334 SelectingOneDeviceShouldCallEventHandler) {
335 bluetooth_chooser_controller_.Select(0);
336 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, last_event_);
337 EXPECT_EQ("id_a", last_device_id_);
338 }
339
340 TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
341 CancelShouldCallEventHandler) {
342 bluetooth_chooser_controller_.Cancel();
343 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, last_event_);
344 EXPECT_EQ(std::string(), last_device_id_);
345 }
346
347 TEST_F(BluetoothChooserControllerWithDevicesAddedTest,
348 CloseShouldCallEventHandler) {
349 bluetooth_chooser_controller_.Close();
350 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, last_event_);
351 EXPECT_EQ(std::string(), last_device_id_);
352 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698