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