Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / dbus / fake_gsm_sms_client.cc
blobba1286bd9d3ec2fc39da2361652122f11cd914ed
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.
5 #include "base/bind.h"
6 #include "base/location.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "chromeos/dbus/fake_gsm_sms_client.h"
11 namespace chromeos {
13 FakeGsmSMSClient::FakeGsmSMSClient()
14 : test_index_(-1),
15 sms_test_message_switch_present_(false),
16 weak_ptr_factory_(this) {
17 test_messages_.push_back("Test Message 0");
18 test_messages_.push_back("Test Message 1");
19 test_messages_.push_back("Test a relatively long message 2");
20 test_messages_.push_back("Test a very, the quick brown fox jumped"
21 " over the lazy dog, long message 3");
22 test_messages_.push_back("Test Message 4");
23 test_messages_.push_back("Test Message 5");
24 test_messages_.push_back("Test Message 6");
27 FakeGsmSMSClient::~FakeGsmSMSClient() {
30 void FakeGsmSMSClient::Init(dbus::Bus* bus) {
33 void FakeGsmSMSClient::SetSmsReceivedHandler(
34 const std::string& service_name,
35 const dbus::ObjectPath& object_path,
36 const SmsReceivedHandler& handler) {
37 handler_ = handler;
40 void FakeGsmSMSClient::ResetSmsReceivedHandler(
41 const std::string& service_name,
42 const dbus::ObjectPath& object_path) {
43 handler_.Reset();
46 void FakeGsmSMSClient::Delete(const std::string& service_name,
47 const dbus::ObjectPath& object_path,
48 uint32 index,
49 const DeleteCallback& callback) {
50 message_list_.Remove(index, NULL);
51 callback.Run();
54 void FakeGsmSMSClient::Get(const std::string& service_name,
55 const dbus::ObjectPath& object_path,
56 uint32 index,
57 const GetCallback& callback) {
58 base::DictionaryValue* dictionary = NULL;
59 if (message_list_.GetDictionary(index, &dictionary)) {
60 callback.Run(*dictionary);
61 return;
63 base::DictionaryValue empty_dictionary;
64 callback.Run(empty_dictionary);
67 void FakeGsmSMSClient::List(const std::string& service_name,
68 const dbus::ObjectPath& object_path,
69 const ListCallback& callback) {
70 callback.Run(message_list_);
73 void FakeGsmSMSClient::RequestUpdate(const std::string& service_name,
74 const dbus::ObjectPath& object_path) {
75 if (!sms_test_message_switch_present_)
76 return;
78 if (test_index_ >= 0)
79 return;
80 test_index_ = 0;
81 // Call PushTestMessageChain asynchronously so that the handler_ callback
82 // does not get called from the update request.
83 base::ThreadTaskRunnerHandle::Get()->PostTask(
84 FROM_HERE, base::Bind(&FakeGsmSMSClient::PushTestMessageChain,
85 weak_ptr_factory_.GetWeakPtr()));
88 void FakeGsmSMSClient::PushTestMessageChain() {
89 if (PushTestMessage())
90 PushTestMessageDelayed();
93 void FakeGsmSMSClient::PushTestMessageDelayed() {
94 const int kSmsMessageDelaySeconds = 5;
95 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
96 FROM_HERE, 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()))
103 return false;
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);
112 ++test_index_;
113 return true;
116 } // namespace chromeos