OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/gsm_sms_client.h" | 5 #include "chromeos/dbus/gsm_sms_client.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/location.h" | 10 #include "base/location.h" |
9 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
10 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
11 #include "base/values.h" | 13 #include "base/values.h" |
12 #include "dbus/message.h" | 14 #include "dbus/message.h" |
13 #include "dbus/mock_bus.h" | 15 #include "dbus/mock_bus.h" |
14 #include "dbus/mock_object_proxy.h" | 16 #include "dbus/mock_object_proxy.h" |
15 #include "dbus/object_path.h" | 17 #include "dbus/object_path.h" |
16 #include "dbus/values_util.h" | 18 #include "dbus/values_util.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/cros_system_api/dbus/service_constants.h" | 21 #include "third_party/cros_system_api/dbus/service_constants.h" |
20 | 22 |
21 using ::testing::_; | 23 using ::testing::_; |
22 using ::testing::Invoke; | 24 using ::testing::Invoke; |
23 using ::testing::Return; | 25 using ::testing::Return; |
24 | 26 |
25 namespace chromeos { | 27 namespace chromeos { |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 // A mock SmsReceivedHandler. | 31 // A mock SmsReceivedHandler. |
30 class MockSmsReceivedHandler { | 32 class MockSmsReceivedHandler { |
31 public: | 33 public: |
32 MOCK_METHOD2(Run, void(uint32 index, bool complete)); | 34 MOCK_METHOD2(Run, void(uint32_t index, bool complete)); |
33 }; | 35 }; |
34 | 36 |
35 // A mock DeleteCallback. | 37 // A mock DeleteCallback. |
36 class MockDeleteCallback { | 38 class MockDeleteCallback { |
37 public: | 39 public: |
38 MOCK_METHOD0(Run, void()); | 40 MOCK_METHOD0(Run, void()); |
39 }; | 41 }; |
40 | 42 |
41 // A mock GetCallback. | 43 // A mock GetCallback. |
42 class MockGetCallback { | 44 class MockGetCallback { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 109 |
108 void TearDown() override { mock_bus_->ShutdownAndBlock(); } | 110 void TearDown() override { mock_bus_->ShutdownAndBlock(); } |
109 | 111 |
110 // Handles Delete method call. | 112 // Handles Delete method call. |
111 void OnDelete(dbus::MethodCall* method_call, | 113 void OnDelete(dbus::MethodCall* method_call, |
112 int timeout_ms, | 114 int timeout_ms, |
113 const dbus::ObjectProxy::ResponseCallback& callback) { | 115 const dbus::ObjectProxy::ResponseCallback& callback) { |
114 EXPECT_EQ(modemmanager::kModemManagerSMSInterface, | 116 EXPECT_EQ(modemmanager::kModemManagerSMSInterface, |
115 method_call->GetInterface()); | 117 method_call->GetInterface()); |
116 EXPECT_EQ(modemmanager::kSMSDeleteFunction, method_call->GetMember()); | 118 EXPECT_EQ(modemmanager::kSMSDeleteFunction, method_call->GetMember()); |
117 uint32 index = 0; | 119 uint32_t index = 0; |
118 dbus::MessageReader reader(method_call); | 120 dbus::MessageReader reader(method_call); |
119 EXPECT_TRUE(reader.PopUint32(&index)); | 121 EXPECT_TRUE(reader.PopUint32(&index)); |
120 EXPECT_EQ(expected_index_, index); | 122 EXPECT_EQ(expected_index_, index); |
121 EXPECT_FALSE(reader.HasMoreData()); | 123 EXPECT_FALSE(reader.HasMoreData()); |
122 | 124 |
123 message_loop_.task_runner()->PostTask(FROM_HERE, | 125 message_loop_.task_runner()->PostTask(FROM_HERE, |
124 base::Bind(callback, response_)); | 126 base::Bind(callback, response_)); |
125 } | 127 } |
126 | 128 |
127 // Handles Get method call. | 129 // Handles Get method call. |
128 void OnGet(dbus::MethodCall* method_call, | 130 void OnGet(dbus::MethodCall* method_call, |
129 int timeout_ms, | 131 int timeout_ms, |
130 const dbus::ObjectProxy::ResponseCallback& callback) { | 132 const dbus::ObjectProxy::ResponseCallback& callback) { |
131 EXPECT_EQ(modemmanager::kModemManagerSMSInterface, | 133 EXPECT_EQ(modemmanager::kModemManagerSMSInterface, |
132 method_call->GetInterface()); | 134 method_call->GetInterface()); |
133 EXPECT_EQ(modemmanager::kSMSGetFunction, method_call->GetMember()); | 135 EXPECT_EQ(modemmanager::kSMSGetFunction, method_call->GetMember()); |
134 uint32 index = 0; | 136 uint32_t index = 0; |
135 dbus::MessageReader reader(method_call); | 137 dbus::MessageReader reader(method_call); |
136 EXPECT_TRUE(reader.PopUint32(&index)); | 138 EXPECT_TRUE(reader.PopUint32(&index)); |
137 EXPECT_EQ(expected_index_, index); | 139 EXPECT_EQ(expected_index_, index); |
138 EXPECT_FALSE(reader.HasMoreData()); | 140 EXPECT_FALSE(reader.HasMoreData()); |
139 | 141 |
140 message_loop_.task_runner()->PostTask(FROM_HERE, | 142 message_loop_.task_runner()->PostTask(FROM_HERE, |
141 base::Bind(callback, response_)); | 143 base::Bind(callback, response_)); |
142 } | 144 } |
143 | 145 |
144 // Handles List method call. | 146 // Handles List method call. |
(...skipping 20 matching lines...) Expand all Loading... |
165 scoped_ptr<GsmSMSClient> client_; | 167 scoped_ptr<GsmSMSClient> client_; |
166 // A message loop to emulate asynchronous behavior. | 168 // A message loop to emulate asynchronous behavior. |
167 base::MessageLoop message_loop_; | 169 base::MessageLoop message_loop_; |
168 // The mock bus. | 170 // The mock bus. |
169 scoped_refptr<dbus::MockBus> mock_bus_; | 171 scoped_refptr<dbus::MockBus> mock_bus_; |
170 // The mock object proxy. | 172 // The mock object proxy. |
171 scoped_refptr<dbus::MockObjectProxy> mock_proxy_; | 173 scoped_refptr<dbus::MockObjectProxy> mock_proxy_; |
172 // The SmsReceived signal handler given by the tested client. | 174 // The SmsReceived signal handler given by the tested client. |
173 dbus::ObjectProxy::SignalCallback sms_received_callback_; | 175 dbus::ObjectProxy::SignalCallback sms_received_callback_; |
174 // Expected argument for Delete and Get methods. | 176 // Expected argument for Delete and Get methods. |
175 uint32 expected_index_; | 177 uint32_t expected_index_; |
176 // Response returned by mock methods. | 178 // Response returned by mock methods. |
177 dbus::Response* response_; | 179 dbus::Response* response_; |
178 // Expected result of Get and List methods. | 180 // Expected result of Get and List methods. |
179 base::Value* expected_result_; | 181 base::Value* expected_result_; |
180 | 182 |
181 private: | 183 private: |
182 // Used to implement the mock proxy. | 184 // Used to implement the mock proxy. |
183 void OnConnectToSignal( | 185 void OnConnectToSignal( |
184 const std::string& interface_name, | 186 const std::string& interface_name, |
185 const std::string& signal_name, | 187 const std::string& signal_name, |
186 const dbus::ObjectProxy::SignalCallback& signal_callback, | 188 const dbus::ObjectProxy::SignalCallback& signal_callback, |
187 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { | 189 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
188 sms_received_callback_ = signal_callback; | 190 sms_received_callback_ = signal_callback; |
189 const bool success = true; | 191 const bool success = true; |
190 message_loop_.task_runner()->PostTask( | 192 message_loop_.task_runner()->PostTask( |
191 FROM_HERE, base::Bind(on_connected_callback, interface_name, | 193 FROM_HERE, base::Bind(on_connected_callback, interface_name, |
192 signal_name, success)); | 194 signal_name, success)); |
193 } | 195 } |
194 }; | 196 }; |
195 | 197 |
196 TEST_F(GsmSMSClientTest, SmsReceived) { | 198 TEST_F(GsmSMSClientTest, SmsReceived) { |
197 // Set expectations. | 199 // Set expectations. |
198 const uint32 kIndex = 42; | 200 const uint32_t kIndex = 42; |
199 const bool kComplete = true; | 201 const bool kComplete = true; |
200 MockSmsReceivedHandler handler; | 202 MockSmsReceivedHandler handler; |
201 EXPECT_CALL(handler, Run(kIndex, kComplete)).Times(1); | 203 EXPECT_CALL(handler, Run(kIndex, kComplete)).Times(1); |
202 // Set handler. | 204 // Set handler. |
203 client_->SetSmsReceivedHandler(kServiceName, dbus::ObjectPath(kObjectPath), | 205 client_->SetSmsReceivedHandler(kServiceName, dbus::ObjectPath(kObjectPath), |
204 base::Bind(&MockSmsReceivedHandler::Run, | 206 base::Bind(&MockSmsReceivedHandler::Run, |
205 base::Unretained(&handler))); | 207 base::Unretained(&handler))); |
206 | 208 |
207 // Run the message loop to run the signal connection result callback. | 209 // Run the message loop to run the signal connection result callback. |
208 message_loop_.RunUntilIdle(); | 210 message_loop_.RunUntilIdle(); |
209 | 211 |
210 // Send signal. | 212 // Send signal. |
211 dbus::Signal signal(modemmanager::kModemManagerSMSInterface, | 213 dbus::Signal signal(modemmanager::kModemManagerSMSInterface, |
212 modemmanager::kSMSReceivedSignal); | 214 modemmanager::kSMSReceivedSignal); |
213 dbus::MessageWriter writer(&signal); | 215 dbus::MessageWriter writer(&signal); |
214 writer.AppendUint32(kIndex); | 216 writer.AppendUint32(kIndex); |
215 writer.AppendBool(kComplete); | 217 writer.AppendBool(kComplete); |
216 ASSERT_FALSE(sms_received_callback_.is_null()); | 218 ASSERT_FALSE(sms_received_callback_.is_null()); |
217 sms_received_callback_.Run(&signal); | 219 sms_received_callback_.Run(&signal); |
218 // Reset handler. | 220 // Reset handler. |
219 client_->ResetSmsReceivedHandler(kServiceName, dbus::ObjectPath(kObjectPath)); | 221 client_->ResetSmsReceivedHandler(kServiceName, dbus::ObjectPath(kObjectPath)); |
220 // Send signal again. | 222 // Send signal again. |
221 sms_received_callback_.Run(&signal); | 223 sms_received_callback_.Run(&signal); |
222 } | 224 } |
223 | 225 |
224 TEST_F(GsmSMSClientTest, Delete) { | 226 TEST_F(GsmSMSClientTest, Delete) { |
225 // Set expectations. | 227 // Set expectations. |
226 const uint32 kIndex = 42; | 228 const uint32_t kIndex = 42; |
227 expected_index_ = kIndex; | 229 expected_index_ = kIndex; |
228 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) | 230 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) |
229 .WillOnce(Invoke(this, &GsmSMSClientTest::OnDelete)); | 231 .WillOnce(Invoke(this, &GsmSMSClientTest::OnDelete)); |
230 MockDeleteCallback callback; | 232 MockDeleteCallback callback; |
231 EXPECT_CALL(callback, Run()).Times(1); | 233 EXPECT_CALL(callback, Run()).Times(1); |
232 // Create response. | 234 // Create response. |
233 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 235 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
234 response_ = response.get(); | 236 response_ = response.get(); |
235 // Call Delete. | 237 // Call Delete. |
236 client_->Delete(kServiceName, dbus::ObjectPath(kObjectPath), kIndex, | 238 client_->Delete(kServiceName, dbus::ObjectPath(kObjectPath), kIndex, |
237 base::Bind(&MockDeleteCallback::Run, | 239 base::Bind(&MockDeleteCallback::Run, |
238 base::Unretained(&callback))); | 240 base::Unretained(&callback))); |
239 | 241 |
240 // Run the message loop. | 242 // Run the message loop. |
241 message_loop_.RunUntilIdle(); | 243 message_loop_.RunUntilIdle(); |
242 } | 244 } |
243 | 245 |
244 TEST_F(GsmSMSClientTest, Get) { | 246 TEST_F(GsmSMSClientTest, Get) { |
245 // Set expectations. | 247 // Set expectations. |
246 const uint32 kIndex = 42; | 248 const uint32_t kIndex = 42; |
247 expected_index_ = kIndex; | 249 expected_index_ = kIndex; |
248 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) | 250 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) |
249 .WillOnce(Invoke(this, &GsmSMSClientTest::OnGet)); | 251 .WillOnce(Invoke(this, &GsmSMSClientTest::OnGet)); |
250 MockGetCallback callback; | 252 MockGetCallback callback; |
251 EXPECT_CALL(callback, Run(_)) | 253 EXPECT_CALL(callback, Run(_)) |
252 .WillOnce(Invoke(this, &GsmSMSClientTest::CheckResult)); | 254 .WillOnce(Invoke(this, &GsmSMSClientTest::CheckResult)); |
253 // Create response. | 255 // Create response. |
254 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 256 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
255 dbus::MessageWriter writer(response.get()); | 257 dbus::MessageWriter writer(response.get()); |
256 dbus::MessageWriter array_writer(NULL); | 258 dbus::MessageWriter array_writer(NULL); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 // Call List. | 320 // Call List. |
319 client_->List(kServiceName, dbus::ObjectPath(kObjectPath), | 321 client_->List(kServiceName, dbus::ObjectPath(kObjectPath), |
320 base::Bind(&MockListCallback::Run, | 322 base::Bind(&MockListCallback::Run, |
321 base::Unretained(&callback))); | 323 base::Unretained(&callback))); |
322 | 324 |
323 // Run the message loop. | 325 // Run the message loop. |
324 message_loop_.RunUntilIdle(); | 326 message_loop_.RunUntilIdle(); |
325 } | 327 } |
326 | 328 |
327 } // namespace chromeos | 329 } // namespace chromeos |
OLD | NEW |