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 "base/message_loop/message_loop.h"
6 #include "chromeos/dbus/dbus_thread_manager.h"
7 #include "chromeos/dbus/fake_nfc_adapter_client.h"
8 #include "device/nfc/nfc_adapter_chromeos.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 using device::NfcAdapter;
12
13 namespace chromeos {
14
15 namespace {
16
17 class TestObserver : public NfcAdapter::Observer {
18 public:
TestObserver(scoped_refptr<NfcAdapter> adapter)19 TestObserver(scoped_refptr<NfcAdapter> adapter)
20 : present_changed_count_(0),
21 powered_changed_count_(0),
22 adapter_(adapter) {
23 }
24
~TestObserver()25 virtual ~TestObserver() {}
26
27 // NfcAdapter::Observer override.
AdapterPresentChanged(NfcAdapter * adapter,bool present)28 virtual void AdapterPresentChanged(NfcAdapter* adapter,
29 bool present) OVERRIDE {
30 EXPECT_EQ(adapter_, adapter);
31 present_changed_count_++;
32 }
33
34 // NfcAdapter::Observer override.
AdapterPoweredChanged(NfcAdapter * adapter,bool powered)35 virtual void AdapterPoweredChanged(NfcAdapter* adapter,
36 bool powered) OVERRIDE {
37 EXPECT_EQ(adapter_, adapter);
38 powered_changed_count_++;
39 }
40
41 int present_changed_count_;
42 int powered_changed_count_;
43 scoped_refptr<NfcAdapter> adapter_;
44 };
45
46 } // namespace
47
48 class NfcChromeOSTest : public testing::Test {
49 public:
SetUp()50 virtual void SetUp() {
51 DBusThreadManager::InitializeWithStub();
52 fake_nfc_adapter_client_ = static_cast<FakeNfcAdapterClient*>(
53 DBusThreadManager::Get()->GetNfcAdapterClient());
54 SetAdapter();
55 message_loop_.RunUntilIdle();
56
57 success_callback_count_ = 0;
58 error_callback_count_ = 0;
59 }
60
TearDown()61 virtual void TearDown() {
62 adapter_ = NULL;
63 DBusThreadManager::Shutdown();
64 }
65
66 // Assigns a new instance of NfcAdapterChromeOS to |adapter_|.
SetAdapter()67 void SetAdapter() {
68 adapter_ = new NfcAdapterChromeOS();
69 ASSERT_TRUE(adapter_.get() != NULL);
70 ASSERT_TRUE(adapter_->IsInitialized());
71 }
72
73 // Generic callbacks for success and error.
SuccessCallback()74 void SuccessCallback() {
75 success_callback_count_++;
76 }
77
ErrorCallback()78 void ErrorCallback() {
79 error_callback_count_++;
80 }
81
82 protected:
83 // Fields for storing the number of times SuccessCallback and ErrorCallback
84 // have been called.
85 int success_callback_count_;
86 int error_callback_count_;
87
88 // A message loop to emulate asynchronous behavior.
89 base::MessageLoop message_loop_;
90
91 // The NfcAdapter instance under test.
92 scoped_refptr<NfcAdapter> adapter_;
93
94 // The fake D-Bus client instances used for testing.
95 FakeNfcAdapterClient* fake_nfc_adapter_client_;
96 };
97
TEST_F(NfcChromeOSTest,PresentChanged)98 TEST_F(NfcChromeOSTest, PresentChanged) {
99 EXPECT_TRUE(adapter_->IsPresent());
100
101 TestObserver observer(adapter_);
102 adapter_->AddObserver(&observer);
103
104 // Remove all adapters.
105 fake_nfc_adapter_client_->SetAdapterPresent(false);
106 EXPECT_EQ(1, observer.present_changed_count_);
107 EXPECT_FALSE(adapter_->IsPresent());
108
109 // Add two adapters.
110 fake_nfc_adapter_client_->SetAdapterPresent(true);
111 fake_nfc_adapter_client_->SetSecondAdapterPresent(true);
112 EXPECT_EQ(2, observer.present_changed_count_);
113 EXPECT_TRUE(adapter_->IsPresent());
114
115 // Remove the first adapter. Adapter should update to the second one.
116 fake_nfc_adapter_client_->SetAdapterPresent(false);
117 EXPECT_EQ(4, observer.present_changed_count_);
118 EXPECT_TRUE(adapter_->IsPresent());
119
120 fake_nfc_adapter_client_->SetSecondAdapterPresent(false);
121 EXPECT_EQ(5, observer.present_changed_count_);
122 EXPECT_FALSE(adapter_->IsPresent());
123 }
124
TEST_F(NfcChromeOSTest,SetPowered)125 TEST_F(NfcChromeOSTest, SetPowered) {
126 TestObserver observer(adapter_);
127 adapter_->AddObserver(&observer);
128
129 EXPECT_FALSE(adapter_->IsPowered());
130
131 // SetPowered(false), while not powered.
132 adapter_->SetPowered(
133 false,
134 base::Bind(&NfcChromeOSTest::SuccessCallback,
135 base::Unretained(this)),
136 base::Bind(&NfcChromeOSTest::ErrorCallback,
137 base::Unretained(this)));
138 EXPECT_FALSE(adapter_->IsPowered());
139 EXPECT_EQ(0, observer.powered_changed_count_);
140 EXPECT_EQ(0, success_callback_count_);
141 EXPECT_EQ(1, error_callback_count_);
142
143 // SetPowered(true).
144 adapter_->SetPowered(
145 true,
146 base::Bind(&NfcChromeOSTest::SuccessCallback,
147 base::Unretained(this)),
148 base::Bind(&NfcChromeOSTest::ErrorCallback,
149 base::Unretained(this)));
150 EXPECT_TRUE(adapter_->IsPowered());
151 EXPECT_EQ(1, observer.powered_changed_count_);
152 EXPECT_EQ(1, success_callback_count_);
153 EXPECT_EQ(1, error_callback_count_);
154
155 // SetPowered(true), while powered.
156 adapter_->SetPowered(
157 true,
158 base::Bind(&NfcChromeOSTest::SuccessCallback,
159 base::Unretained(this)),
160 base::Bind(&NfcChromeOSTest::ErrorCallback,
161 base::Unretained(this)));
162 EXPECT_TRUE(adapter_->IsPowered());
163 EXPECT_EQ(1, observer.powered_changed_count_);
164 EXPECT_EQ(1, success_callback_count_);
165 EXPECT_EQ(2, error_callback_count_);
166
167 // SetPowered(false).
168 adapter_->SetPowered(
169 false,
170 base::Bind(&NfcChromeOSTest::SuccessCallback,
171 base::Unretained(this)),
172 base::Bind(&NfcChromeOSTest::ErrorCallback,
173 base::Unretained(this)));
174 EXPECT_FALSE(adapter_->IsPowered());
175 EXPECT_EQ(2, observer.powered_changed_count_);
176 EXPECT_EQ(2, success_callback_count_);
177 EXPECT_EQ(2, error_callback_count_);
178 }
179
TEST_F(NfcChromeOSTest,PresentChangedWhilePowered)180 TEST_F(NfcChromeOSTest, PresentChangedWhilePowered) {
181 TestObserver observer(adapter_);
182 adapter_->AddObserver(&observer);
183
184 EXPECT_FALSE(adapter_->IsPowered());
185 EXPECT_TRUE(adapter_->IsPresent());
186
187 adapter_->SetPowered(
188 true,
189 base::Bind(&NfcChromeOSTest::SuccessCallback,
190 base::Unretained(this)),
191 base::Bind(&NfcChromeOSTest::ErrorCallback,
192 base::Unretained(this)));
193 EXPECT_TRUE(adapter_->IsPowered());
194
195 fake_nfc_adapter_client_->SetAdapterPresent(false);
196 EXPECT_EQ(1, observer.present_changed_count_);
197 EXPECT_EQ(2, observer.powered_changed_count_);
198 EXPECT_FALSE(adapter_->IsPowered());
199 EXPECT_FALSE(adapter_->IsPresent());
200 }
201
202 } // namespace chromeos
203