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

Side by Side Diff: chromeos/dbus/nfc_client_unittest.cc

Issue 2292703002: chromeos: Remove unused NFC D-Bus client library (Closed)
Patch Set: rebase 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
« no previous file with comments | « chromeos/dbus/nfc_client_helpers.cc ('k') | chromeos/dbus/nfc_device_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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 <stddef.h>
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "chromeos/dbus/nfc_adapter_client.h"
12 #include "chromeos/dbus/nfc_client_helpers.h"
13 #include "chromeos/dbus/nfc_device_client.h"
14 #include "chromeos/dbus/nfc_manager_client.h"
15 #include "chromeos/dbus/nfc_record_client.h"
16 #include "chromeos/dbus/nfc_tag_client.h"
17 #include "dbus/mock_bus.h"
18 #include "dbus/mock_object_proxy.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h"
22
23 using ::testing::_;
24 using ::testing::Invoke;
25 using ::testing::Mock;
26 using ::testing::Return;
27
28 using chromeos::nfc_client_helpers::ObjectPathVector;
29
30 namespace chromeos {
31
32 namespace {
33
34 // D-Bus service name used by the test.
35 const char kTestServiceName[] = "test.service.name";
36
37 // Object paths that are used for testing.
38 const char kTestManagerPath[] = "/test/nfc/manager";
39 const char kTestAdapterPath0[] = "/test/nfc/adapter0";
40 const char kTestAdapterPath1[] = "/test/nfc/adapter1";
41 const char kTestDevicePath0[] = "/test/nfc/device0";
42 const char kTestDevicePath1[] = "/test/nfc/device1";
43 const char kTestRecordPath0[] = "/test/nfc/record0";
44 const char kTestRecordPath1[] = "/test/nfc/record1";
45 const char kTestRecordPath2[] = "/test/nfc/record2";
46 const char kTestRecordPath3[] = "/test/nfc/record3";
47 const char kTestTagPath0[] = "/test/nfc/tag0";
48 const char kTestTagPath1[] = "/test/nfc/tag1";
49
50 class MockNfcManagerObserver : public NfcManagerClient::Observer {
51 public:
52 MOCK_METHOD1(AdapterAdded, void(const dbus::ObjectPath&));
53 MOCK_METHOD1(AdapterRemoved, void(const dbus::ObjectPath&));
54 MOCK_METHOD1(ManagerPropertyChanged, void(const std::string&));
55 };
56
57 class MockNfcAdapterObserver : public NfcAdapterClient::Observer {
58 public:
59 MOCK_METHOD1(AdapterAdded, void(const dbus::ObjectPath&));
60 MOCK_METHOD1(AdapterRemoved, void(const dbus::ObjectPath&));
61 MOCK_METHOD2(AdapterPropertyChanged, void(const dbus::ObjectPath&,
62 const std::string&));
63 };
64
65 class MockNfcDeviceObserver : public NfcDeviceClient::Observer {
66 public:
67 MOCK_METHOD1(DeviceAdded, void(const dbus::ObjectPath&));
68 MOCK_METHOD1(DeviceRemoved, void(const dbus::ObjectPath&));
69 MOCK_METHOD2(DevicePropertyChanged, void(const dbus::ObjectPath&,
70 const std::string&));
71 };
72
73 class MockNfcRecordObserver : public NfcRecordClient::Observer {
74 public:
75 MOCK_METHOD1(RecordAdded, void(const dbus::ObjectPath&));
76 MOCK_METHOD1(RecordRemoved, void(const dbus::ObjectPath&));
77 MOCK_METHOD2(RecordPropertyChanged, void(const dbus::ObjectPath&,
78 const std::string&));
79 MOCK_METHOD1(RecordPropertiesReceived, void(const dbus::ObjectPath&));
80 };
81
82 class MockNfcTagObserver : public NfcTagClient::Observer {
83 public:
84 MOCK_METHOD1(TagAdded, void(const dbus::ObjectPath&));
85 MOCK_METHOD1(TagRemoved, void(const dbus::ObjectPath&));
86 MOCK_METHOD2(TagPropertyChanged, void(const dbus::ObjectPath&,
87 const std::string&));
88 };
89
90 } // namespace
91
92 class NfcClientTest : public testing::Test {
93 public:
94 NfcClientTest() : response_(NULL) {}
95 ~NfcClientTest() override {}
96
97 void SetUp() override {
98 // Create the mock bus.
99 dbus::Bus::Options options;
100 options.bus_type = dbus::Bus::SYSTEM;
101 mock_bus_ = new dbus::MockBus(options);
102
103 // Create the mock proxies.
104 mock_manager_proxy_ = new dbus::MockObjectProxy(
105 mock_bus_.get(),
106 kTestServiceName,
107 dbus::ObjectPath(kTestManagerPath));
108 mock_adapter0_proxy_ = new dbus::MockObjectProxy(
109 mock_bus_.get(),
110 kTestServiceName,
111 dbus::ObjectPath(kTestAdapterPath0));
112 mock_adapter1_proxy_ = new dbus::MockObjectProxy(
113 mock_bus_.get(),
114 kTestServiceName,
115 dbus::ObjectPath(kTestAdapterPath1));
116 mock_device0_proxy_ = new dbus::MockObjectProxy(
117 mock_bus_.get(),
118 kTestServiceName,
119 dbus::ObjectPath(kTestDevicePath0));
120 mock_device1_proxy_ = new dbus::MockObjectProxy(
121 mock_bus_.get(),
122 kTestServiceName,
123 dbus::ObjectPath(kTestDevicePath1));
124 mock_record0_proxy_ = new dbus::MockObjectProxy(
125 mock_bus_.get(),
126 kTestServiceName,
127 dbus::ObjectPath(kTestRecordPath0));
128 mock_record1_proxy_ = new dbus::MockObjectProxy(
129 mock_bus_.get(),
130 kTestServiceName,
131 dbus::ObjectPath(kTestRecordPath1));
132 mock_record2_proxy_ = new dbus::MockObjectProxy(
133 mock_bus_.get(),
134 kTestServiceName,
135 dbus::ObjectPath(kTestRecordPath2));
136 mock_record3_proxy_ = new dbus::MockObjectProxy(
137 mock_bus_.get(),
138 kTestServiceName,
139 dbus::ObjectPath(kTestRecordPath3));
140 mock_tag0_proxy_ = new dbus::MockObjectProxy(
141 mock_bus_.get(),
142 kTestServiceName,
143 dbus::ObjectPath(kTestTagPath0));
144 mock_tag1_proxy_ = new dbus::MockObjectProxy(
145 mock_bus_.get(),
146 kTestServiceName,
147 dbus::ObjectPath(kTestTagPath1));
148
149 // Set expectations that use NfcClientTest::OnConnectToSignal when the
150 // client connect signals on the mock proxies.
151 EXPECT_CALL(*mock_manager_proxy_.get(), ConnectToSignal(_, _, _, _))
152 .WillRepeatedly(Invoke(this, &NfcClientTest::OnConnectToSignal));
153 EXPECT_CALL(*mock_adapter0_proxy_.get(), ConnectToSignal(_, _, _, _))
154 .WillRepeatedly(Invoke(this, &NfcClientTest::OnConnectToSignal));
155 EXPECT_CALL(*mock_adapter1_proxy_.get(), ConnectToSignal(_, _, _, _))
156 .WillRepeatedly(Invoke(this, &NfcClientTest::OnConnectToSignal));
157
158 // Set expectations that return our mock proxies on demand.
159 EXPECT_CALL(
160 *mock_bus_.get(),
161 GetObjectProxy(nfc_manager::kNfcManagerServiceName,
162 dbus::ObjectPath(nfc_manager::kNfcManagerServicePath)))
163 .WillRepeatedly(Return(mock_manager_proxy_.get()));
164 EXPECT_CALL(*mock_bus_.get(),
165 GetObjectProxy(nfc_adapter::kNfcAdapterServiceName,
166 dbus::ObjectPath(kTestAdapterPath0)))
167 .WillRepeatedly(Return(mock_adapter0_proxy_.get()));
168 EXPECT_CALL(*mock_bus_.get(),
169 GetObjectProxy(nfc_adapter::kNfcAdapterServiceName,
170 dbus::ObjectPath(kTestAdapterPath1)))
171 .WillRepeatedly(Return(mock_adapter1_proxy_.get()));
172 EXPECT_CALL(*mock_bus_.get(),
173 GetObjectProxy(nfc_device::kNfcDeviceServiceName,
174 dbus::ObjectPath(kTestDevicePath0)))
175 .WillRepeatedly(Return(mock_device0_proxy_.get()));
176 EXPECT_CALL(*mock_bus_.get(),
177 GetObjectProxy(nfc_device::kNfcDeviceServiceName,
178 dbus::ObjectPath(kTestDevicePath1)))
179 .WillRepeatedly(Return(mock_device1_proxy_.get()));
180 EXPECT_CALL(*mock_bus_.get(),
181 GetObjectProxy(nfc_record::kNfcRecordServiceName,
182 dbus::ObjectPath(kTestRecordPath0)))
183 .WillRepeatedly(Return(mock_record0_proxy_.get()));
184 EXPECT_CALL(*mock_bus_.get(),
185 GetObjectProxy(nfc_record::kNfcRecordServiceName,
186 dbus::ObjectPath(kTestRecordPath1)))
187 .WillRepeatedly(Return(mock_record1_proxy_.get()));
188 EXPECT_CALL(*mock_bus_.get(),
189 GetObjectProxy(nfc_record::kNfcRecordServiceName,
190 dbus::ObjectPath(kTestRecordPath2)))
191 .WillRepeatedly(Return(mock_record2_proxy_.get()));
192 EXPECT_CALL(*mock_bus_.get(),
193 GetObjectProxy(nfc_record::kNfcRecordServiceName,
194 dbus::ObjectPath(kTestRecordPath3)))
195 .WillRepeatedly(Return(mock_record3_proxy_.get()));
196 EXPECT_CALL(*mock_bus_.get(),
197 GetObjectProxy(nfc_tag::kNfcTagServiceName,
198 dbus::ObjectPath(kTestTagPath0)))
199 .WillRepeatedly(Return(mock_tag0_proxy_.get()));
200 EXPECT_CALL(*mock_bus_.get(),
201 GetObjectProxy(nfc_tag::kNfcTagServiceName,
202 dbus::ObjectPath(kTestTagPath1)))
203 .WillRepeatedly(Return(mock_tag1_proxy_.get()));
204
205 // Handle |manager_client_|'s request to register a callback
206 // for |mock_manager_proxy_|'s D-Bus service becoming available.
207 EXPECT_CALL(*mock_manager_proxy_.get(), WaitForServiceToBeAvailable(_))
208 .WillRepeatedly(
209 Invoke(this, &NfcClientTest::OnWaitForServiceToBeAvailable));
210
211 // ShutdownAndBlock will be called in TearDown.
212 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
213
214 // Create the clients.
215 manager_client_.reset(NfcManagerClient::Create());
216 adapter_client_.reset(NfcAdapterClient::Create(manager_client_.get()));
217 device_client_.reset(NfcDeviceClient::Create(adapter_client_.get()));
218 tag_client_.reset(NfcTagClient::Create(adapter_client_.get()));
219 record_client_.reset(
220 NfcRecordClient::Create(device_client_.get(), tag_client_.get()));
221 manager_client_->Init(mock_bus_.get());
222 adapter_client_->Init(mock_bus_.get());
223 device_client_->Init(mock_bus_.get());
224 tag_client_->Init(mock_bus_.get());
225 record_client_->Init(mock_bus_.get());
226 manager_client_->AddObserver(&mock_manager_observer_);
227 adapter_client_->AddObserver(&mock_adapter_observer_);
228 device_client_->AddObserver(&mock_device_observer_);
229 tag_client_->AddObserver(&mock_tag_observer_);
230 record_client_->AddObserver(&mock_record_observer_);
231
232 message_loop_.RunUntilIdle();
233 }
234
235 void TearDown() override {
236 tag_client_->RemoveObserver(&mock_tag_observer_);
237 device_client_->RemoveObserver(&mock_device_observer_);
238 adapter_client_->RemoveObserver(&mock_adapter_observer_);
239 manager_client_->RemoveObserver(&mock_manager_observer_);
240 mock_bus_->ShutdownAndBlock();
241 }
242
243 void SimulateAdaptersChanged(
244 const ObjectPathVector& adapter_paths) {
245 NfcManagerClient::Properties* properties =
246 manager_client_->GetProperties();
247 ASSERT_TRUE(properties);
248 EXPECT_CALL(mock_manager_observer_,
249 ManagerPropertyChanged(nfc_manager::kAdaptersProperty));
250 SendArrayPropertyChangedSignal(
251 properties,
252 nfc_manager::kNfcManagerInterface,
253 nfc_manager::kAdaptersProperty,
254 adapter_paths);
255 Mock::VerifyAndClearExpectations(&mock_manager_observer_);
256 }
257
258 void SimulateTagsChanged(const ObjectPathVector& tag_paths,
259 const dbus::ObjectPath& adapter_path) {
260 NfcAdapterClient::Properties* properties =
261 adapter_client_->GetProperties(adapter_path);
262 ASSERT_TRUE(properties);
263 EXPECT_CALL(mock_adapter_observer_,
264 AdapterPropertyChanged(adapter_path,
265 nfc_adapter::kTagsProperty));
266 SendArrayPropertyChangedSignal(
267 properties,
268 nfc_adapter::kNfcAdapterInterface,
269 nfc_adapter::kTagsProperty,
270 tag_paths);
271 Mock::VerifyAndClearExpectations(&mock_adapter_observer_);
272 }
273
274 void SimulateDevicesChanged(const ObjectPathVector& device_paths,
275 const dbus::ObjectPath& adapter_path) {
276 NfcAdapterClient::Properties* properties =
277 adapter_client_->GetProperties(adapter_path);
278 ASSERT_TRUE(properties);
279 EXPECT_CALL(mock_adapter_observer_,
280 AdapterPropertyChanged(adapter_path,
281 nfc_adapter::kDevicesProperty));
282 SendArrayPropertyChangedSignal(
283 properties,
284 nfc_adapter::kNfcAdapterInterface,
285 nfc_adapter::kDevicesProperty,
286 device_paths);
287 Mock::VerifyAndClearExpectations(&mock_adapter_observer_);
288 }
289
290 void SimulateDeviceRecordsChanged(
291 const ObjectPathVector& record_paths,
292 const dbus::ObjectPath& device_path) {
293 NfcDeviceClient::Properties* properties =
294 device_client_->GetProperties(device_path);
295 ASSERT_TRUE(properties);
296 EXPECT_CALL(mock_device_observer_,
297 DevicePropertyChanged(device_path,
298 nfc_device::kRecordsProperty));
299 SendArrayPropertyChangedSignal(
300 properties,
301 nfc_device::kNfcDeviceInterface,
302 nfc_device::kRecordsProperty,
303 record_paths);
304 Mock::VerifyAndClearExpectations(&mock_device_observer_);
305 }
306
307 void SimulateTagRecordsChanged(
308 const ObjectPathVector& record_paths,
309 const dbus::ObjectPath& tag_path) {
310 NfcTagClient::Properties* properties =
311 tag_client_->GetProperties(tag_path);
312 ASSERT_TRUE(properties);
313 EXPECT_CALL(mock_tag_observer_,
314 TagPropertyChanged(tag_path,
315 nfc_tag::kRecordsProperty));
316 SendArrayPropertyChangedSignal(
317 properties,
318 nfc_tag::kNfcTagInterface,
319 nfc_tag::kRecordsProperty,
320 record_paths);
321 Mock::VerifyAndClearExpectations(&mock_tag_observer_);
322 }
323
324 void SendArrayPropertyChangedSignal(
325 dbus::PropertySet* properties,
326 const std::string& interface,
327 const std::string& property_name,
328 ObjectPathVector object_paths) {
329 dbus::Signal signal(interface, nfc_common::kPropertyChangedSignal);
330 dbus::MessageWriter writer(&signal);
331 writer.AppendString(property_name);
332 dbus::MessageWriter variant_writer(NULL);
333 writer.OpenVariant("ao", &variant_writer);
334 variant_writer.AppendArrayOfObjectPaths(object_paths);
335 writer.CloseContainer(&variant_writer);
336 properties->ChangedReceived(&signal);
337 }
338
339 MOCK_METHOD0(SuccessCallback, void(void));
340 MOCK_METHOD2(ErrorCallback, void(const std::string& error_name,
341 const std::string& error_message));
342
343 protected:
344 // The mock object proxies.
345 scoped_refptr<dbus::MockObjectProxy> mock_manager_proxy_;
346 scoped_refptr<dbus::MockObjectProxy> mock_adapter0_proxy_;
347 scoped_refptr<dbus::MockObjectProxy> mock_adapter1_proxy_;
348 scoped_refptr<dbus::MockObjectProxy> mock_device0_proxy_;
349 scoped_refptr<dbus::MockObjectProxy> mock_device1_proxy_;
350 scoped_refptr<dbus::MockObjectProxy> mock_record0_proxy_;
351 scoped_refptr<dbus::MockObjectProxy> mock_record1_proxy_;
352 scoped_refptr<dbus::MockObjectProxy> mock_record2_proxy_;
353 scoped_refptr<dbus::MockObjectProxy> mock_record3_proxy_;
354 scoped_refptr<dbus::MockObjectProxy> mock_tag0_proxy_;
355 scoped_refptr<dbus::MockObjectProxy> mock_tag1_proxy_;
356 // The mock bus.
357 scoped_refptr<dbus::MockBus> mock_bus_;
358 // A message loop to emulate asynchronous behavior.
359 base::MessageLoop message_loop_;
360 // Response returned by mock methods.
361 dbus::Response* response_;
362 // The D-Bus client objects under test.
363 std::unique_ptr<NfcManagerClient> manager_client_;
364 std::unique_ptr<NfcAdapterClient> adapter_client_;
365 std::unique_ptr<NfcDeviceClient> device_client_;
366 std::unique_ptr<NfcTagClient> tag_client_;
367 std::unique_ptr<NfcRecordClient> record_client_;
368 // Mock observers.
369 MockNfcManagerObserver mock_manager_observer_;
370 MockNfcAdapterObserver mock_adapter_observer_;
371 MockNfcDeviceObserver mock_device_observer_;
372 MockNfcTagObserver mock_tag_observer_;
373 MockNfcRecordObserver mock_record_observer_;
374 // The signal callbacks used to simulate asychronous signals.
375 dbus::ObjectProxy::SignalCallback manager_adapter_added_signal_callback_;
376 dbus::ObjectProxy::SignalCallback manager_adapter_removed_signal_callback_;
377
378 private:
379 // Used to inform |manager_client_| that |mock_manager_proxy_| is ready.
380 void OnWaitForServiceToBeAvailable(
381 dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback) {
382 message_loop_.task_runner()->PostTask(FROM_HERE,
383 base::Bind(callback, true));
384 }
385
386 // Used to implement the mock proxy.
387 void OnConnectToSignal(
388 const std::string& interface_name,
389 const std::string& signal_name,
390 const dbus::ObjectProxy::SignalCallback& signal_callback,
391 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) {
392 if (interface_name == nfc_manager::kNfcManagerInterface) {
393 if (signal_name == nfc_manager::kAdapterAddedSignal)
394 manager_adapter_added_signal_callback_ = signal_callback;
395 else if (signal_name == nfc_manager::kAdapterRemovedSignal)
396 manager_adapter_removed_signal_callback_ = signal_callback;
397 }
398 message_loop_.task_runner()->PostTask(
399 FROM_HERE,
400 base::Bind(on_connected_callback, interface_name, signal_name, true));
401 }
402 };
403
404 // Tests that when adapters are added and removed through the manager, all
405 // observers are notified and the proxies are created and removed
406 // accordingly.
407 TEST_F(NfcClientTest, AdaptersAddedAndRemoved) {
408 // Invoking methods on adapters that haven't been added should fail.
409 EXPECT_CALL(*this,
410 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
411 adapter_client_->StartPollLoop(
412 dbus::ObjectPath(kTestAdapterPath0),
413 nfc_adapter::kModeInitiator,
414 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
415 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
416 Mock::VerifyAndClearExpectations(this);
417
418 // Add adapter 0.
419 ObjectPathVector adapter_paths;
420 adapter_paths.push_back(dbus::ObjectPath(kTestAdapterPath0));
421 EXPECT_CALL(mock_adapter_observer_,
422 AdapterAdded(dbus::ObjectPath(kTestAdapterPath0)));
423 SimulateAdaptersChanged(adapter_paths);
424
425 // Invoking methods should succeed on adapter 0 but fail on adapter 1.
426 EXPECT_CALL(*mock_adapter0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
427 adapter_client_->StartPollLoop(
428 dbus::ObjectPath(kTestAdapterPath0),
429 nfc_adapter::kModeInitiator,
430 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
431 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
432 Mock::VerifyAndClearExpectations(&mock_adapter0_proxy_);
433 EXPECT_CALL(*this,
434 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
435 EXPECT_CALL(*mock_adapter1_proxy_, CallMethodWithErrorCallback(_, _, _, _))
436 .Times(1);
437 adapter_client_->StartPollLoop(
438 dbus::ObjectPath(kTestAdapterPath1),
439 nfc_adapter::kModeInitiator,
440 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
441 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
442 Mock::VerifyAndClearExpectations(this);
443 Mock::VerifyAndClearExpectations(&mock_adapter1_proxy_);
444
445 // Add adapter 1.
446 adapter_paths.push_back(dbus::ObjectPath(kTestAdapterPath1));
447 EXPECT_CALL(mock_adapter_observer_,
448 AdapterAdded(dbus::ObjectPath(kTestAdapterPath1)));
449 SimulateAdaptersChanged(adapter_paths);
450
451 // Invoking methods should succeed on both adapters.
452 EXPECT_CALL(*mock_adapter0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
453 EXPECT_CALL(*mock_adapter1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
454 adapter_client_->StartPollLoop(
455 dbus::ObjectPath(kTestAdapterPath0),
456 nfc_adapter::kModeInitiator,
457 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
458 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
459 adapter_client_->StartPollLoop(
460 dbus::ObjectPath(kTestAdapterPath1),
461 nfc_adapter::kModeInitiator,
462 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
463 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
464 Mock::VerifyAndClearExpectations(&mock_adapter0_proxy_);
465 Mock::VerifyAndClearExpectations(&mock_adapter1_proxy_);
466
467 // Remove adapter 0.
468 adapter_paths.erase(adapter_paths.begin());
469 EXPECT_CALL(mock_adapter_observer_,
470 AdapterRemoved(dbus::ObjectPath(kTestAdapterPath0)));
471 SimulateAdaptersChanged(adapter_paths);
472
473 // Invoking methods should succeed on adapter 1 but fail on adapter 0.
474 EXPECT_CALL(*this,
475 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
476 EXPECT_CALL(*mock_adapter0_proxy_, CallMethodWithErrorCallback(_, _, _, _))
477 .Times(0);
478 adapter_client_->StartPollLoop(
479 dbus::ObjectPath(kTestAdapterPath0),
480 nfc_adapter::kModeInitiator,
481 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
482 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
483 Mock::VerifyAndClearExpectations(this);
484
485 EXPECT_CALL(*mock_adapter1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
486 adapter_client_->StartPollLoop(
487 dbus::ObjectPath(kTestAdapterPath1),
488 nfc_adapter::kModeInitiator,
489 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
490 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
491 Mock::VerifyAndClearExpectations(&mock_adapter0_proxy_);
492 Mock::VerifyAndClearExpectations(&mock_adapter1_proxy_);
493
494 // Remove adapter 1.
495 adapter_paths.clear();
496 EXPECT_CALL(mock_adapter_observer_,
497 AdapterRemoved(dbus::ObjectPath(kTestAdapterPath1)));
498 SimulateAdaptersChanged(adapter_paths);
499
500 // Invoking methods should fail on both adapters.
501 EXPECT_CALL(*this,
502 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _))
503 .Times(2);
504 EXPECT_CALL(*mock_adapter0_proxy_, CallMethodWithErrorCallback(_, _, _, _))
505 .Times(0);
506 EXPECT_CALL(*mock_adapter1_proxy_, CallMethodWithErrorCallback(_, _, _, _))
507 .Times(0);
508 adapter_client_->StartPollLoop(
509 dbus::ObjectPath(kTestAdapterPath0),
510 nfc_adapter::kModeInitiator,
511 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
512 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
513 adapter_client_->StartPollLoop(
514 dbus::ObjectPath(kTestAdapterPath1),
515 nfc_adapter::kModeInitiator,
516 base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
517 base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
518 }
519
520 // Tests that when tags are added and removed through an adapter, all
521 // observers are notified and the proxies are created and removed
522 // accordingly.
523 TEST_F(NfcClientTest, TagsAddedAndRemoved) {
524 // Invoking methods on tags that haven't been added should fail.
525 EXPECT_CALL(*this,
526 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
527 base::DictionaryValue write_data;
528 write_data.SetString(nfc_record::kTypeProperty, nfc_record::kTypeText);
529 tag_client_->Write(dbus::ObjectPath(kTestTagPath0), write_data,
530 base::Bind(&NfcClientTest::SuccessCallback,
531 base::Unretained(this)),
532 base::Bind(&NfcClientTest::ErrorCallback,
533 base::Unretained(this)));
534 Mock::VerifyAndClearExpectations(this);
535
536 // Add adapter 0.
537 ObjectPathVector adapter_paths;
538 adapter_paths.push_back(dbus::ObjectPath(kTestAdapterPath0));
539 EXPECT_CALL(mock_adapter_observer_,
540 AdapterAdded(dbus::ObjectPath(kTestAdapterPath0)));
541 SimulateAdaptersChanged(adapter_paths);
542 Mock::VerifyAndClearExpectations(&mock_adapter_observer_);
543
544 // Add tag 0.
545 ObjectPathVector tag_paths;
546 tag_paths.push_back(dbus::ObjectPath(kTestTagPath0));
547 EXPECT_CALL(mock_tag_observer_,
548 TagAdded(dbus::ObjectPath(kTestTagPath0)));
549 SimulateTagsChanged(tag_paths, dbus::ObjectPath(kTestAdapterPath0));
550 Mock::VerifyAndClearExpectations(&mock_tag_observer_);
551
552 // Invoking methods should succeed on tag 0 but fail on tag 1.
553 EXPECT_CALL(*mock_tag0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
554 tag_client_->Write(dbus::ObjectPath(kTestTagPath0), write_data,
555 base::Bind(&NfcClientTest::SuccessCallback,
556 base::Unretained(this)),
557 base::Bind(&NfcClientTest::ErrorCallback,
558 base::Unretained(this)));
559 Mock::VerifyAndClearExpectations(&mock_tag0_proxy_);
560 EXPECT_CALL(*this,
561 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
562 EXPECT_CALL(*mock_tag1_proxy_, CallMethodWithErrorCallback(_, _, _, _))
563 .Times(1);
564 tag_client_->Write(dbus::ObjectPath(kTestTagPath1), write_data,
565 base::Bind(&NfcClientTest::SuccessCallback,
566 base::Unretained(this)),
567 base::Bind(&NfcClientTest::ErrorCallback,
568 base::Unretained(this)));
569 Mock::VerifyAndClearExpectations(this);
570 Mock::VerifyAndClearExpectations(&mock_tag1_proxy_);
571
572 // Add tag 1.
573 tag_paths.push_back(dbus::ObjectPath(kTestTagPath1));
574 EXPECT_CALL(mock_tag_observer_,
575 TagAdded(dbus::ObjectPath(kTestTagPath1)));
576 SimulateTagsChanged(tag_paths, dbus::ObjectPath(kTestAdapterPath0));
577 Mock::VerifyAndClearExpectations(&mock_tag_observer_);
578
579 // Invoking methods should succeed on both tags.
580 EXPECT_CALL(*mock_tag0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
581 EXPECT_CALL(*mock_tag1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
582 tag_client_->Write(dbus::ObjectPath(kTestTagPath0), write_data,
583 base::Bind(&NfcClientTest::SuccessCallback,
584 base::Unretained(this)),
585 base::Bind(&NfcClientTest::ErrorCallback,
586 base::Unretained(this)));
587 tag_client_->Write(dbus::ObjectPath(kTestTagPath1), write_data,
588 base::Bind(&NfcClientTest::SuccessCallback,
589 base::Unretained(this)),
590 base::Bind(&NfcClientTest::ErrorCallback,
591 base::Unretained(this)));
592 Mock::VerifyAndClearExpectations(&mock_tag0_proxy_);
593 Mock::VerifyAndClearExpectations(&mock_tag1_proxy_);
594
595 // Remove tag 0.
596 tag_paths.erase(tag_paths.begin());
597 EXPECT_CALL(mock_tag_observer_,
598 TagRemoved(dbus::ObjectPath(kTestTagPath0)));
599 SimulateTagsChanged(tag_paths, dbus::ObjectPath(kTestAdapterPath0));
600 Mock::VerifyAndClearExpectations(&mock_tag_observer_);
601
602 // Invoking methods should succeed on tag 1 but fail on tag 0.
603 EXPECT_CALL(*this,
604 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
605 EXPECT_CALL(*mock_tag0_proxy_, CallMethodWithErrorCallback(_, _, _, _))
606 .Times(0);
607 tag_client_->Write(dbus::ObjectPath(kTestTagPath0), write_data,
608 base::Bind(&NfcClientTest::SuccessCallback,
609 base::Unretained(this)),
610 base::Bind(&NfcClientTest::ErrorCallback,
611 base::Unretained(this)));
612 Mock::VerifyAndClearExpectations(this);
613 Mock::VerifyAndClearExpectations(&mock_tag0_proxy_);
614 EXPECT_CALL(*mock_tag1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
615 tag_client_->Write(dbus::ObjectPath(kTestTagPath1), write_data,
616 base::Bind(&NfcClientTest::SuccessCallback,
617 base::Unretained(this)),
618 base::Bind(&NfcClientTest::ErrorCallback,
619 base::Unretained(this)));
620 Mock::VerifyAndClearExpectations(&mock_tag1_proxy_);
621
622 // Remove tag 1.
623 tag_paths.clear();
624 EXPECT_CALL(mock_tag_observer_,
625 TagRemoved(dbus::ObjectPath(kTestTagPath1)));
626 SimulateTagsChanged(tag_paths, dbus::ObjectPath(kTestAdapterPath0));
627 Mock::VerifyAndClearExpectations(&mock_tag_observer_);
628
629 // Invoking methods should fail on both tags.
630 EXPECT_CALL(*this,
631 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _))
632 .Times(2);
633 EXPECT_CALL(*mock_tag0_proxy_, CallMethodWithErrorCallback(_, _, _, _))
634 .Times(0);
635 EXPECT_CALL(*mock_tag1_proxy_, CallMethodWithErrorCallback(_, _, _, _))
636 .Times(0);
637 tag_client_->Write(dbus::ObjectPath(kTestTagPath0), write_data,
638 base::Bind(&NfcClientTest::SuccessCallback,
639 base::Unretained(this)),
640 base::Bind(&NfcClientTest::ErrorCallback,
641 base::Unretained(this)));
642 tag_client_->Write(dbus::ObjectPath(kTestTagPath1), write_data,
643 base::Bind(&NfcClientTest::SuccessCallback,
644 base::Unretained(this)),
645 base::Bind(&NfcClientTest::ErrorCallback,
646 base::Unretained(this)));
647 }
648
649 // Tests that when devices are added and removed through an adapter, all
650 // observers are notified and the proxies are created and removed
651 // accordingly.
652 TEST_F(NfcClientTest, DevicesAddedAndRemoved) {
653 // Invoking methods on devices that haven't been added should fail.
654 EXPECT_CALL(*this,
655 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
656 base::DictionaryValue write_data;
657 write_data.SetString(nfc_record::kTypeProperty, nfc_record::kTypeText);
658 device_client_->Push(dbus::ObjectPath(kTestDevicePath0), write_data,
659 base::Bind(&NfcClientTest::SuccessCallback,
660 base::Unretained(this)),
661 base::Bind(&NfcClientTest::ErrorCallback,
662 base::Unretained(this)));
663 Mock::VerifyAndClearExpectations(this);
664
665 // Add adapter 0.
666 ObjectPathVector adapter_paths;
667 adapter_paths.push_back(dbus::ObjectPath(kTestAdapterPath0));
668 EXPECT_CALL(mock_adapter_observer_,
669 AdapterAdded(dbus::ObjectPath(kTestAdapterPath0)));
670 SimulateAdaptersChanged(adapter_paths);
671
672 // Add device 0.
673 ObjectPathVector device_paths;
674 device_paths.push_back(dbus::ObjectPath(kTestDevicePath0));
675 EXPECT_CALL(mock_device_observer_,
676 DeviceAdded(dbus::ObjectPath(kTestDevicePath0)));
677 SimulateDevicesChanged(device_paths, dbus::ObjectPath(kTestAdapterPath0));
678 Mock::VerifyAndClearExpectations(&mock_device_observer_);
679
680 // Invoking methods should succeed on device 0 but fail on device 1.
681 EXPECT_CALL(*mock_device0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
682 device_client_->Push(dbus::ObjectPath(kTestDevicePath0), write_data,
683 base::Bind(&NfcClientTest::SuccessCallback,
684 base::Unretained(this)),
685 base::Bind(&NfcClientTest::ErrorCallback,
686 base::Unretained(this)));
687 Mock::VerifyAndClearExpectations(&mock_device0_proxy_);
688 EXPECT_CALL(*this,
689 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
690 EXPECT_CALL(*mock_device1_proxy_, CallMethodWithErrorCallback(_, _, _, _))
691 .Times(1);
692 device_client_->Push(dbus::ObjectPath(kTestDevicePath1), write_data,
693 base::Bind(&NfcClientTest::SuccessCallback,
694 base::Unretained(this)),
695 base::Bind(&NfcClientTest::ErrorCallback,
696 base::Unretained(this)));
697 Mock::VerifyAndClearExpectations(this);
698 Mock::VerifyAndClearExpectations(&mock_device1_proxy_);
699
700 // Add device 1.
701 device_paths.push_back(dbus::ObjectPath(kTestDevicePath1));
702 EXPECT_CALL(mock_device_observer_,
703 DeviceAdded(dbus::ObjectPath(kTestDevicePath1)));
704 SimulateDevicesChanged(device_paths, dbus::ObjectPath(kTestAdapterPath0));
705 Mock::VerifyAndClearExpectations(&mock_device_observer_);
706
707 // Invoking methods should succeed on both devices.
708 EXPECT_CALL(*mock_device0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
709 EXPECT_CALL(*mock_device1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
710 device_client_->Push(dbus::ObjectPath(kTestDevicePath0), write_data,
711 base::Bind(&NfcClientTest::SuccessCallback,
712 base::Unretained(this)),
713 base::Bind(&NfcClientTest::ErrorCallback,
714 base::Unretained(this)));
715 device_client_->Push(dbus::ObjectPath(kTestDevicePath1), write_data,
716 base::Bind(&NfcClientTest::SuccessCallback,
717 base::Unretained(this)),
718 base::Bind(&NfcClientTest::ErrorCallback,
719 base::Unretained(this)));
720 Mock::VerifyAndClearExpectations(&mock_device0_proxy_);
721 Mock::VerifyAndClearExpectations(&mock_device1_proxy_);
722
723 // Remove device 0.
724 device_paths.erase(device_paths.begin());
725 EXPECT_CALL(mock_device_observer_,
726 DeviceRemoved(dbus::ObjectPath(kTestDevicePath0)));
727 SimulateDevicesChanged(device_paths, dbus::ObjectPath(kTestAdapterPath0));
728 Mock::VerifyAndClearExpectations(&mock_device_observer_);
729
730 // Invoking methods should succeed on device 1 but fail on device 0.
731 EXPECT_CALL(*this,
732 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
733 EXPECT_CALL(*mock_device0_proxy_, CallMethodWithErrorCallback(_, _, _, _))
734 .Times(0);
735 device_client_->Push(dbus::ObjectPath(kTestDevicePath0), write_data,
736 base::Bind(&NfcClientTest::SuccessCallback,
737 base::Unretained(this)),
738 base::Bind(&NfcClientTest::ErrorCallback,
739 base::Unretained(this)));
740 Mock::VerifyAndClearExpectations(this);
741 Mock::VerifyAndClearExpectations(&mock_device0_proxy_);
742 EXPECT_CALL(*mock_device1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
743 device_client_->Push(dbus::ObjectPath(kTestDevicePath1), write_data,
744 base::Bind(&NfcClientTest::SuccessCallback,
745 base::Unretained(this)),
746 base::Bind(&NfcClientTest::ErrorCallback,
747 base::Unretained(this)));
748 Mock::VerifyAndClearExpectations(&mock_device1_proxy_);
749
750 // Remove device 1.
751 device_paths.clear();
752 EXPECT_CALL(mock_device_observer_,
753 DeviceRemoved(dbus::ObjectPath(kTestDevicePath1)));
754 SimulateDevicesChanged(device_paths, dbus::ObjectPath(kTestAdapterPath0));
755 Mock::VerifyAndClearExpectations(&mock_device_observer_);
756
757 // Invoking methods should fail on both devices.
758 EXPECT_CALL(*this,
759 ErrorCallback(nfc_client_helpers::kUnknownObjectError, _))
760 .Times(2);
761 EXPECT_CALL(*mock_device0_proxy_, CallMethodWithErrorCallback(_, _, _, _))
762 .Times(0);
763 EXPECT_CALL(*mock_device1_proxy_, CallMethodWithErrorCallback(_, _, _, _))
764 .Times(0);
765 device_client_->Push(dbus::ObjectPath(kTestDevicePath0), write_data,
766 base::Bind(&NfcClientTest::SuccessCallback,
767 base::Unretained(this)),
768 base::Bind(&NfcClientTest::ErrorCallback,
769 base::Unretained(this)));
770 device_client_->Push(dbus::ObjectPath(kTestDevicePath1), write_data,
771 base::Bind(&NfcClientTest::SuccessCallback,
772 base::Unretained(this)),
773 base::Bind(&NfcClientTest::ErrorCallback,
774 base::Unretained(this)));
775 }
776
777 TEST_F(NfcClientTest, ObjectCleanup) {
778 // Tests that when an adapter gets removed, proxies that belong to the
779 // adapter, device, tag, and record hierarchy get cleaned up properly.
780 ObjectPathVector object_paths;
781
782 // Add adapters.
783 EXPECT_CALL(mock_adapter_observer_,
784 AdapterAdded(dbus::ObjectPath(kTestAdapterPath0)));
785 EXPECT_CALL(mock_adapter_observer_,
786 AdapterAdded(dbus::ObjectPath(kTestAdapterPath1)));
787 object_paths.push_back(dbus::ObjectPath(kTestAdapterPath0));
788 object_paths.push_back(dbus::ObjectPath(kTestAdapterPath1));
789 SimulateAdaptersChanged(object_paths);
790 Mock::VerifyAndClearExpectations(&mock_adapter_observer_);
791
792 // Add devices and a tags. Assign them like the following:
793 // - device 0 -> adapter 0
794 // - tag 0 -> adapter 0
795 // - device 1 -> adapter 1
796 // - tag 1 -> adapter 1
797 EXPECT_CALL(mock_device_observer_,
798 DeviceAdded(dbus::ObjectPath(kTestDevicePath0)));
799 EXPECT_CALL(mock_device_observer_,
800 DeviceAdded(dbus::ObjectPath(kTestDevicePath1)));
801 EXPECT_CALL(mock_tag_observer_,
802 TagAdded(dbus::ObjectPath(kTestTagPath0)));
803 EXPECT_CALL(mock_tag_observer_,
804 TagAdded(dbus::ObjectPath(kTestTagPath1)));
805 object_paths.clear();
806 object_paths.push_back(dbus::ObjectPath(kTestDevicePath0));
807 SimulateDevicesChanged(object_paths, dbus::ObjectPath(kTestAdapterPath0));
808 object_paths.clear();
809 object_paths.push_back(dbus::ObjectPath(kTestTagPath0));
810 SimulateTagsChanged(object_paths, dbus::ObjectPath(kTestAdapterPath0));
811 object_paths.clear();
812 object_paths.push_back(dbus::ObjectPath(kTestDevicePath1));
813 SimulateDevicesChanged(object_paths, dbus::ObjectPath(kTestAdapterPath1));
814 object_paths.clear();
815 object_paths.push_back(dbus::ObjectPath(kTestTagPath1));
816 SimulateTagsChanged(object_paths, dbus::ObjectPath(kTestAdapterPath1));
817 Mock::VerifyAndClearExpectations(&mock_device_observer_);
818 Mock::VerifyAndClearExpectations(&mock_tag_observer_);
819
820 // Add records. Assign them like the following:
821 // - record 0 -> device 0
822 // - record 1 -> tag 0
823 // - record 2 -> device 1
824 // - record 3 -> tag 1
825 EXPECT_CALL(mock_record_observer_,
826 RecordAdded(dbus::ObjectPath(kTestRecordPath0)));
827 EXPECT_CALL(mock_record_observer_,
828 RecordAdded(dbus::ObjectPath(kTestRecordPath1)));
829 EXPECT_CALL(mock_record_observer_,
830 RecordAdded(dbus::ObjectPath(kTestRecordPath2)));
831 EXPECT_CALL(mock_record_observer_,
832 RecordAdded(dbus::ObjectPath(kTestRecordPath3)));
833 object_paths.clear();
834 object_paths.push_back(dbus::ObjectPath(kTestRecordPath0));
835 SimulateDeviceRecordsChanged(object_paths,
836 dbus::ObjectPath(kTestDevicePath0));
837 object_paths.clear();
838 object_paths.push_back(dbus::ObjectPath(kTestRecordPath1));
839 SimulateTagRecordsChanged(object_paths,
840 dbus::ObjectPath(kTestTagPath0));
841 object_paths.clear();
842 object_paths.push_back(dbus::ObjectPath(kTestRecordPath2));
843 SimulateDeviceRecordsChanged(object_paths,
844 dbus::ObjectPath(kTestDevicePath1));
845 object_paths.clear();
846 object_paths.push_back(dbus::ObjectPath(kTestRecordPath3));
847 SimulateTagRecordsChanged(object_paths,
848 dbus::ObjectPath(kTestTagPath1));
849 Mock::VerifyAndClearExpectations(&mock_record_observer_);
850
851 // Check that the records have been assigned to the correct device or tag.
852 NfcTagClient::Properties* tag_properties =
853 tag_client_->GetProperties(dbus::ObjectPath(kTestTagPath0));
854 EXPECT_EQ((size_t)1, tag_properties->records.value().size());
855 EXPECT_EQ(dbus::ObjectPath(kTestRecordPath1),
856 tag_properties->records.value()[0]);
857 NfcDeviceClient::Properties* device_properties =
858 device_client_->GetProperties(dbus::ObjectPath(kTestDevicePath0));
859 EXPECT_EQ((size_t)1, device_properties->records.value().size());
860 EXPECT_EQ(dbus::ObjectPath(kTestRecordPath0),
861 device_properties->records.value()[0]);
862
863 // Remove adapter 0. Make sure that all of the tag, device, and records that
864 // are in the adapter 0 hierarchy are removed.
865 object_paths.clear();
866 object_paths.push_back(dbus::ObjectPath(kTestAdapterPath1));
867 EXPECT_CALL(mock_adapter_observer_,
868 AdapterRemoved(dbus::ObjectPath(kTestAdapterPath0)));
869 EXPECT_CALL(mock_device_observer_,
870 DeviceRemoved(dbus::ObjectPath(kTestDevicePath0)));
871 EXPECT_CALL(mock_tag_observer_,
872 TagRemoved(dbus::ObjectPath(kTestTagPath0)));
873 EXPECT_CALL(mock_record_observer_,
874 RecordRemoved(dbus::ObjectPath(kTestRecordPath0)));
875 EXPECT_CALL(mock_record_observer_,
876 RecordRemoved(dbus::ObjectPath(kTestRecordPath1)));
877 SimulateAdaptersChanged(object_paths);
878 Mock::VerifyAndClearExpectations(&mock_adapter_observer_);
879 Mock::VerifyAndClearExpectations(&mock_device_observer_);
880 Mock::VerifyAndClearExpectations(&mock_tag_observer_);
881 Mock::VerifyAndClearExpectations(&mock_record_observer_);
882
883 // Remove adapter 1.
884 object_paths.clear();
885 EXPECT_CALL(mock_adapter_observer_,
886 AdapterRemoved(dbus::ObjectPath(kTestAdapterPath1)));
887 EXPECT_CALL(mock_device_observer_,
888 DeviceRemoved(dbus::ObjectPath(kTestDevicePath1)));
889 EXPECT_CALL(mock_tag_observer_,
890 TagRemoved(dbus::ObjectPath(kTestTagPath1)));
891 EXPECT_CALL(mock_record_observer_,
892 RecordRemoved(dbus::ObjectPath(kTestRecordPath2)));
893 EXPECT_CALL(mock_record_observer_,
894 RecordRemoved(dbus::ObjectPath(kTestRecordPath3)));
895 SimulateAdaptersChanged(object_paths);
896 }
897
898 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/nfc_client_helpers.cc ('k') | chromeos/dbus/nfc_device_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698