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

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

Issue 2275173002: Add unit test for BluetoothChooserController (Closed)
Patch Set: added unit test for BluetoothChooserController 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 <memory>
6 #include <string>
7
8 #include "base/bind.h"
9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h"
16
17 namespace {
18
19 class MockBluetoothChooserView : public ChooserController::View {
20 public:
21 MockBluetoothChooserView() {}
22 ~MockBluetoothChooserView() override = default;
23
24 MOCK_METHOD0(OnOptionsInitialized, void());
25 MOCK_METHOD1(OnOptionAdded, void(size_t index));
26 MOCK_METHOD1(OnOptionRemoved, void(size_t index));
27 MOCK_METHOD1(OnOptionUpdated, void(size_t index));
28 MOCK_METHOD1(OnAdapterEnabledChanged, void(bool enabled));
29 MOCK_METHOD1(OnRefreshStateChanged, void(bool enabled));
30
31 private:
32 DISALLOW_COPY_AND_ASSIGN(MockBluetoothChooserView);
33 };
34
35 } // namespace
36
37 class BluetoothChooserControllerTest : public testing::Test {
38 public:
39 BluetoothChooserControllerTest() {}
40 ~BluetoothChooserControllerTest() override = default;
Jeffrey Yasskin 2016/08/26 21:46:12 You don't need to mention a defaulted destructor.
juncai 2016/08/27 00:21:30 Done.
41
42 void SetUp() override {
43 content::BluetoothChooser::EventHandler event_handler =
44 base::Bind(&BluetoothChooserControllerTest::OnBluetoothChooserEvent,
45 base::Unretained(this));
46 bluetooth_chooser_controller_.reset(
Jeffrey Yasskin 2016/08/26 21:46:12 Make this a value member, not a unique_ptr, and in
juncai 2016/08/27 00:21:30 Done.
47 new BluetoothChooserController(nullptr, event_handler));
48 mock_bluetooth_chooser_view_.reset(new MockBluetoothChooserView());
Jeffrey Yasskin 2016/08/26 21:46:12 Also make mock_bluetooth_chooser_view_ a value mem
juncai 2016/08/27 00:21:30 Done.
49 bluetooth_chooser_controller_->set_view(mock_bluetooth_chooser_view_.get());
Jeffrey Yasskin 2016/08/26 21:46:12 Since this can't fail, it's also fine to move it t
juncai 2016/08/27 00:21:30 Done.
50 }
51
52 protected:
53 void OnBluetoothChooserEvent(content::BluetoothChooser::Event event,
54 const std::string& device_id) {
55 event_ = event;
56 device_id_ = device_id;
57 }
58
59 std::unique_ptr<BluetoothChooserController> bluetooth_chooser_controller_;
60 std::unique_ptr<MockBluetoothChooserView> mock_bluetooth_chooser_view_;
61 content::BluetoothChooser::Event event_;
Jeffrey Yasskin 2016/08/26 21:46:12 I'd call these last_event_ and last_device_id_.
juncai 2016/08/27 00:21:30 Done.
62 std::string device_id_;
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(BluetoothChooserControllerTest);
66 };
67
68 TEST_F(BluetoothChooserControllerTest, AddDevice) {
69 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionAdded(0)).Times(1);
70 bluetooth_chooser_controller_->AddOrUpdateDevice(
71 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
72 true /* is_gatt_connected */, true /* is_paired */, -1);
Jeffrey Yasskin 2016/08/26 21:46:12 Also comment the meaning of the -1: /* rssi */
juncai 2016/08/27 00:21:30 Done.
73 EXPECT_EQ(1u, bluetooth_chooser_controller_->NumOptions());
74 EXPECT_EQ(base::ASCIIToUTF16("a"),
75 bluetooth_chooser_controller_->GetOption(0));
76 EXPECT_EQ(-1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
77
78 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionAdded(1)).Times(1);
Jeffrey Yasskin 2016/08/26 21:46:12 Since you have three separate sets of expectations
juncai 2016/08/27 00:21:30 Done.
79 bluetooth_chooser_controller_->AddOrUpdateDevice(
80 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
81 true /* is_gatt_connected */, true /* is_paired */, 0);
82 EXPECT_EQ(2u, bluetooth_chooser_controller_->NumOptions());
83 EXPECT_EQ(base::ASCIIToUTF16("b"),
84 bluetooth_chooser_controller_->GetOption(1));
85 EXPECT_EQ(0, bluetooth_chooser_controller_->GetSignalStrengthLevel(1));
86
87 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionAdded(2)).Times(1);
88 bluetooth_chooser_controller_->AddOrUpdateDevice(
89 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
90 true /* is_gatt_connected */, true /* is_paired */, 1);
91 EXPECT_EQ(3u, bluetooth_chooser_controller_->NumOptions());
92 EXPECT_EQ(base::ASCIIToUTF16("c"),
93 bluetooth_chooser_controller_->GetOption(2));
94 EXPECT_EQ(1, bluetooth_chooser_controller_->GetSignalStrengthLevel(2));
95 }
96
97 TEST_F(BluetoothChooserControllerTest, RemoveDevice) {
98 bluetooth_chooser_controller_->AddOrUpdateDevice(
99 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
100 true /* is_gatt_connected */, true /* is_paired */, -1);
101 bluetooth_chooser_controller_->AddOrUpdateDevice(
102 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
103 true /* is_gatt_connected */, true /* is_paired */, 0);
104 bluetooth_chooser_controller_->AddOrUpdateDevice(
105 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
106 true /* is_gatt_connected */, true /* is_paired */, 1);
107
108 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionRemoved(1)).Times(1);
109 bluetooth_chooser_controller_->RemoveDevice("id_b");
110 EXPECT_EQ(2u, bluetooth_chooser_controller_->NumOptions());
111 EXPECT_EQ(base::ASCIIToUTF16("a"),
112 bluetooth_chooser_controller_->GetOption(0));
113 EXPECT_EQ(base::ASCIIToUTF16("c"),
114 bluetooth_chooser_controller_->GetOption(1));
115
116 // Remove a non-existent device, the number of devices should not change.
117 bluetooth_chooser_controller_->RemoveDevice("non-existent");
118 EXPECT_EQ(2u, bluetooth_chooser_controller_->NumOptions());
119 EXPECT_EQ(base::ASCIIToUTF16("a"),
120 bluetooth_chooser_controller_->GetOption(0));
121 EXPECT_EQ(base::ASCIIToUTF16("c"),
122 bluetooth_chooser_controller_->GetOption(1));
123
124 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
125 bluetooth_chooser_controller_->RemoveDevice("id_a");
126 EXPECT_EQ(1u, bluetooth_chooser_controller_->NumOptions());
127 EXPECT_EQ(base::ASCIIToUTF16("c"),
128 bluetooth_chooser_controller_->GetOption(0));
129
130 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionRemoved(0)).Times(1);
131 bluetooth_chooser_controller_->RemoveDevice("id_c");
132 EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
133 }
134
135 TEST_F(BluetoothChooserControllerTest, AddAndRemoveDeviceWithSameName) {
Jeffrey Yasskin 2016/08/26 21:46:12 Try to name tests with an overall description of w
juncai 2016/08/27 00:21:30 Done.
136 bluetooth_chooser_controller_->AddOrUpdateDevice(
137 "id_a_1", false /* should_update_name */, base::ASCIIToUTF16("a"),
138 true /* is_gatt_connected */, true /* is_paired */, -1);
139 EXPECT_EQ(base::ASCIIToUTF16("a"),
140 bluetooth_chooser_controller_->GetOption(0));
141
142 bluetooth_chooser_controller_->AddOrUpdateDevice(
143 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
144 true /* is_gatt_connected */, true /* is_paired */, 0);
145 bluetooth_chooser_controller_->AddOrUpdateDevice(
146 "id_a_2", false /* should_update_name */, base::ASCIIToUTF16("a"),
147 true /* is_gatt_connected */, true /* is_paired */, 1);
148 EXPECT_EQ(base::ASCIIToUTF16("a (id_a_1)"),
149 bluetooth_chooser_controller_->GetOption(0));
150 EXPECT_EQ(base::ASCIIToUTF16("b"),
151 bluetooth_chooser_controller_->GetOption(1));
152 EXPECT_EQ(base::ASCIIToUTF16("a (id_a_2)"),
153 bluetooth_chooser_controller_->GetOption(2));
154
155 bluetooth_chooser_controller_->RemoveDevice("id_a_1");
156 EXPECT_EQ(base::ASCIIToUTF16("b"),
157 bluetooth_chooser_controller_->GetOption(0));
158 EXPECT_EQ(base::ASCIIToUTF16("a"),
159 bluetooth_chooser_controller_->GetOption(1));
160 }
161
162 TEST_F(BluetoothChooserControllerTest, UpdateDeviceName) {
163 bluetooth_chooser_controller_->AddOrUpdateDevice(
164 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
165 true /* is_gatt_connected */, true /* is_paired */, -1);
166 EXPECT_EQ(base::ASCIIToUTF16("a"),
167 bluetooth_chooser_controller_->GetOption(0));
168
169 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
170 bluetooth_chooser_controller_->AddOrUpdateDevice(
171 "id_a", false /* should_update_name */, base::ASCIIToUTF16("aa"),
172 true /* is_gatt_connected */, true /* is_paired */, -1);
173 // The name is still "a" since |should_update_name| is false.
174 EXPECT_EQ(base::ASCIIToUTF16("a"),
175 bluetooth_chooser_controller_->GetOption(0));
176
177 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
178 bluetooth_chooser_controller_->AddOrUpdateDevice(
179 "id_a", true /* should_update_name */, base::ASCIIToUTF16("aa"),
180 true /* is_gatt_connected */, true /* is_paired */, -1);
181 EXPECT_EQ(1u, bluetooth_chooser_controller_->NumOptions());
182 EXPECT_EQ(base::ASCIIToUTF16("aa"),
183 bluetooth_chooser_controller_->GetOption(0));
184
185 bluetooth_chooser_controller_->RemoveDevice("id_a");
186 EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
187 }
188
189 TEST_F(BluetoothChooserControllerTest, UpdateDeviceSignalStrengthLevel) {
190 bluetooth_chooser_controller_->AddOrUpdateDevice(
191 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
192 true /* is_gatt_connected */, true /* is_paired */, -1);
193 EXPECT_EQ(-1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
194
195 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
196 bluetooth_chooser_controller_->AddOrUpdateDevice(
197 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
198 true /* is_gatt_connected */, true /* is_paired */, 1);
199 EXPECT_EQ(1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
200
201 EXPECT_CALL(*mock_bluetooth_chooser_view_, OnOptionUpdated(0)).Times(1);
202 // When Bluetooth device scanning stops, an update is sent and the signal
203 // strength level is -1, and in this case, should still use the previously
204 // stored signal strength level. So here the signal strength level is
205 // still 1.
206 bluetooth_chooser_controller_->AddOrUpdateDevice(
207 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
208 true /* is_gatt_connected */, true /* is_paired */, -1);
209 EXPECT_EQ(1, bluetooth_chooser_controller_->GetSignalStrengthLevel(0));
210 }
211
212 TEST_F(BluetoothChooserControllerTest, ChangeAdapterPresence) {
213 bluetooth_chooser_controller_->AddOrUpdateDevice(
Jeffrey Yasskin 2016/08/26 21:46:12 It looks like you have several tests that need to
juncai 2016/08/27 00:21:30 Done.
214 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
215 true /* is_gatt_connected */, true /* is_paired */, -1);
216 bluetooth_chooser_controller_->AddOrUpdateDevice(
217 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
218 true /* is_gatt_connected */, true /* is_paired */, 0);
219 bluetooth_chooser_controller_->AddOrUpdateDevice(
220 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
221 true /* is_gatt_connected */, true /* is_paired */, 1);
222
223 EXPECT_EQ(l10n_util::GetStringUTF16(
224 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
225 bluetooth_chooser_controller_->GetNoOptionsText());
226 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_->GetStatus());
227
228 EXPECT_CALL(
229 *mock_bluetooth_chooser_view_,
230 OnAdapterEnabledChanged(false /* Bluetooth adapter is turned off */))
231 .Times(1);
232 bluetooth_chooser_controller_->OnAdapterPresenceChanged(
233 content::BluetoothChooser::AdapterPresence::POWERED_OFF);
234 EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
235 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_ADAPTER_OFF),
236 bluetooth_chooser_controller_->GetNoOptionsText());
237 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_->GetStatus());
238
239 EXPECT_CALL(
240 *mock_bluetooth_chooser_view_,
241 OnAdapterEnabledChanged(true /* Bluetooth adapter is turned on */))
242 .Times(1);
243 bluetooth_chooser_controller_->OnAdapterPresenceChanged(
244 content::BluetoothChooser::AdapterPresence::POWERED_ON);
245 EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
246 EXPECT_EQ(l10n_util::GetStringUTF16(
247 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
248 bluetooth_chooser_controller_->GetNoOptionsText());
249 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
250 bluetooth_chooser_controller_->GetStatus());
251 }
252
253 TEST_F(BluetoothChooserControllerTest, ChangeDiscoveryState) {
254 bluetooth_chooser_controller_->AddOrUpdateDevice(
255 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
256 true /* is_gatt_connected */, true /* is_paired */, -1);
257 bluetooth_chooser_controller_->AddOrUpdateDevice(
258 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
259 true /* is_gatt_connected */, true /* is_paired */, 0);
260 bluetooth_chooser_controller_->AddOrUpdateDevice(
261 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
262 true /* is_gatt_connected */, true /* is_paired */, 1);
263
264 EXPECT_EQ(base::string16(), bluetooth_chooser_controller_->GetStatus());
265
266 EXPECT_CALL(
267 *mock_bluetooth_chooser_view_,
268 OnRefreshStateChanged(true /* Refreshing options is in progress */))
269 .Times(1);
270 bluetooth_chooser_controller_->OnDiscoveryStateChanged(
271 content::BluetoothChooser::DiscoveryState::DISCOVERING);
272 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING),
273 bluetooth_chooser_controller_->GetStatus());
274
275 EXPECT_CALL(*mock_bluetooth_chooser_view_,
276 OnRefreshStateChanged(false /* Refreshing options is complete */))
277 .Times(1);
278 bluetooth_chooser_controller_->OnDiscoveryStateChanged(
279 content::BluetoothChooser::DiscoveryState::IDLE);
280 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
281 bluetooth_chooser_controller_->GetStatus());
282
283 EXPECT_CALL(*mock_bluetooth_chooser_view_,
284 OnRefreshStateChanged(false /* Refreshing options is complete */))
285 .Times(1);
286 bluetooth_chooser_controller_->OnDiscoveryStateChanged(
287 content::BluetoothChooser::DiscoveryState::FAILED_TO_START);
288 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
289 bluetooth_chooser_controller_->GetStatus());
290 }
291
292 TEST_F(BluetoothChooserControllerTest, RefreshOptions) {
293 bluetooth_chooser_controller_->AddOrUpdateDevice(
294 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
295 true /* is_gatt_connected */, true /* is_paired */, -1);
296 bluetooth_chooser_controller_->AddOrUpdateDevice(
297 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
298 true /* is_gatt_connected */, true /* is_paired */, 0);
299 bluetooth_chooser_controller_->AddOrUpdateDevice(
300 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
301 true /* is_gatt_connected */, true /* is_paired */, 1);
302
303 bluetooth_chooser_controller_->RefreshOptions();
Jeffrey Yasskin 2016/08/26 21:46:12 Does this not call any view methods?
juncai 2016/08/27 00:21:30 It does call view method, but through: https://cs.
Jeffrey Yasskin 2016/08/27 00:35:45 Please test that.
juncai 2016/08/27 02:15:56 The above calling sequence is for non-testing code
Jeffrey Yasskin 2016/09/02 20:40:58 Argh, sorry, I mis-read the implementation of Refr
juncai 2016/09/02 22:41:45 Done.
304 EXPECT_EQ(0u, bluetooth_chooser_controller_->NumOptions());
305 EXPECT_EQ(content::BluetoothChooser::Event::RESCAN, event_);
306 EXPECT_EQ(std::string(), device_id_);
307 }
308
309 TEST_F(BluetoothChooserControllerTest, Select) {
310 bluetooth_chooser_controller_->AddOrUpdateDevice(
311 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
312 true /* is_gatt_connected */, true /* is_paired */, -1);
313 bluetooth_chooser_controller_->AddOrUpdateDevice(
314 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
315 true /* is_gatt_connected */, true /* is_paired */, 0);
316 bluetooth_chooser_controller_->AddOrUpdateDevice(
317 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
318 true /* is_gatt_connected */, true /* is_paired */, 1);
319
320 bluetooth_chooser_controller_->Select(0);
321 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, event_);
322 EXPECT_EQ("id_a", device_id_);
323
324 bluetooth_chooser_controller_->Select(1);
325 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, event_);
326 EXPECT_EQ("id_b", device_id_);
327
328 bluetooth_chooser_controller_->Select(2);
329 EXPECT_EQ(content::BluetoothChooser::Event::SELECTED, event_);
330 EXPECT_EQ("id_c", device_id_);
331 }
332
333 TEST_F(BluetoothChooserControllerTest, Cancel) {
334 bluetooth_chooser_controller_->AddOrUpdateDevice(
335 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
336 true /* is_gatt_connected */, true /* is_paired */, -1);
337 bluetooth_chooser_controller_->AddOrUpdateDevice(
338 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
339 true /* is_gatt_connected */, true /* is_paired */, 0);
340 bluetooth_chooser_controller_->AddOrUpdateDevice(
341 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
342 true /* is_gatt_connected */, true /* is_paired */, 1);
343
344 bluetooth_chooser_controller_->Cancel();
345 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, event_);
346 EXPECT_EQ(std::string(), device_id_);
347 }
348
349 TEST_F(BluetoothChooserControllerTest, Close) {
350 bluetooth_chooser_controller_->AddOrUpdateDevice(
351 "id_a", false /* should_update_name */, base::ASCIIToUTF16("a"),
352 true /* is_gatt_connected */, true /* is_paired */, -1);
353 bluetooth_chooser_controller_->AddOrUpdateDevice(
354 "id_b", false /* should_update_name */, base::ASCIIToUTF16("b"),
355 true /* is_gatt_connected */, true /* is_paired */, 0);
356 bluetooth_chooser_controller_->AddOrUpdateDevice(
357 "id_c", false /* should_update_name */, base::ASCIIToUTF16("c"),
358 true /* is_gatt_connected */, true /* is_paired */, 1);
359
360 bluetooth_chooser_controller_->Close();
361 EXPECT_EQ(content::BluetoothChooser::Event::CANCELLED, event_);
362 EXPECT_EQ(std::string(), device_id_);
363 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698