1 // Copyright (c) 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.
6 #include "base/message_loop/message_loop.h"
7 #include "chromeos/dbus/fake_gsm_sms_client.h"
11 FakeGsmSMSClient::FakeGsmSMSClient()
13 sms_test_message_switch_present_(false),
14 weak_ptr_factory_(this) {
15 test_messages_
.push_back("Test Message 0");
16 test_messages_
.push_back("Test Message 1");
17 test_messages_
.push_back("Test a relatively long message 2");
18 test_messages_
.push_back("Test a very, the quick brown fox jumped"
19 " over the lazy dog, long message 3");
20 test_messages_
.push_back("Test Message 4");
21 test_messages_
.push_back("Test Message 5");
22 test_messages_
.push_back("Test Message 6");
25 FakeGsmSMSClient::~FakeGsmSMSClient() {
28 void FakeGsmSMSClient::Init(dbus::Bus
* bus
) {
31 void FakeGsmSMSClient::SetSmsReceivedHandler(
32 const std::string
& service_name
,
33 const dbus::ObjectPath
& object_path
,
34 const SmsReceivedHandler
& handler
) {
38 void FakeGsmSMSClient::ResetSmsReceivedHandler(
39 const std::string
& service_name
,
40 const dbus::ObjectPath
& object_path
) {
44 void FakeGsmSMSClient::Delete(const std::string
& service_name
,
45 const dbus::ObjectPath
& object_path
,
47 const DeleteCallback
& callback
) {
48 message_list_
.Remove(index
, NULL
);
52 void FakeGsmSMSClient::Get(const std::string
& service_name
,
53 const dbus::ObjectPath
& object_path
,
55 const GetCallback
& callback
) {
56 base::DictionaryValue
* dictionary
= NULL
;
57 if (message_list_
.GetDictionary(index
, &dictionary
)) {
58 callback
.Run(*dictionary
);
61 base::DictionaryValue empty_dictionary
;
62 callback
.Run(empty_dictionary
);
65 void FakeGsmSMSClient::List(const std::string
& service_name
,
66 const dbus::ObjectPath
& object_path
,
67 const ListCallback
& callback
) {
68 callback
.Run(message_list_
);
71 void FakeGsmSMSClient::RequestUpdate(const std::string
& service_name
,
72 const dbus::ObjectPath
& object_path
) {
73 if (!sms_test_message_switch_present_
)
79 // Call PushTestMessageChain asynchronously so that the handler_ callback
80 // does not get called from the update request.
81 base::MessageLoop::current()->PostTask(
83 base::Bind(&FakeGsmSMSClient::PushTestMessageChain
,
84 weak_ptr_factory_
.GetWeakPtr()));
87 void FakeGsmSMSClient::PushTestMessageChain() {
88 if (PushTestMessage())
89 PushTestMessageDelayed();
92 void FakeGsmSMSClient::PushTestMessageDelayed() {
93 const int kSmsMessageDelaySeconds
= 5;
94 base::MessageLoop::current()->PostDelayedTask(
96 base::Bind(&FakeGsmSMSClient::PushTestMessageChain
,
97 weak_ptr_factory_
.GetWeakPtr()),
98 base::TimeDelta::FromSeconds(kSmsMessageDelaySeconds
));
101 bool FakeGsmSMSClient::PushTestMessage() {
102 if (test_index_
>= static_cast<int>(test_messages_
.size()))
104 base::DictionaryValue
* message
= new base::DictionaryValue
;
105 message
->SetString("number", "000-000-0000");
106 message
->SetString("text", test_messages_
[test_index_
]);
107 message
->SetInteger("index", test_index_
);
108 int msg_index
= message_list_
.GetSize();
109 message_list_
.Append(message
);
110 if (!handler_
.is_null())
111 handler_
.Run(msg_index
, true);
116 } // namespace chromeos