summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/fake_gsm_sms_client.cc
blob: 4d4452f651e05f476b03fb8c50c40af08a6f3e03 (plain)
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
// Copyright (c) 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/fake_gsm_sms_client.h"

namespace chromeos {

FakeGsmSMSClient::FakeGsmSMSClient()
    : test_index_(-1),
      sms_test_message_switch_present_(false),
      weak_ptr_factory_(this) {
  test_messages_.push_back("Test Message 0");
  test_messages_.push_back("Test Message 1");
  test_messages_.push_back("Test a relatively long message 2");
  test_messages_.push_back("Test a very, the quick brown fox jumped"
                           " over the lazy dog, long message 3");
  test_messages_.push_back("Test Message 4");
  test_messages_.push_back("Test Message 5");
  test_messages_.push_back("Test Message 6");
}

FakeGsmSMSClient::~FakeGsmSMSClient() {
}

void FakeGsmSMSClient::Init(dbus::Bus* bus) {
}

void FakeGsmSMSClient::SetSmsReceivedHandler(
    const std::string& service_name,
    const dbus::ObjectPath& object_path,
    const SmsReceivedHandler& handler) {
  handler_ = handler;
}

void FakeGsmSMSClient::ResetSmsReceivedHandler(
    const std::string& service_name,
    const dbus::ObjectPath& object_path) {
  handler_.Reset();
}

void FakeGsmSMSClient::Delete(const std::string& service_name,
                              const dbus::ObjectPath& object_path,
                              uint32 index,
                              const DeleteCallback& callback) {
  message_list_.Remove(index, NULL);
  callback.Run();
}

void FakeGsmSMSClient::Get(const std::string& service_name,
                           const dbus::ObjectPath& object_path,
                           uint32 index,
                           const GetCallback& callback) {
  base::DictionaryValue* dictionary = NULL;
  if (message_list_.GetDictionary(index, &dictionary)) {
    callback.Run(*dictionary);
    return;
  }
  base::DictionaryValue empty_dictionary;
  callback.Run(empty_dictionary);
}

void FakeGsmSMSClient::List(const std::string& service_name,
                            const dbus::ObjectPath& object_path,
                            const ListCallback& callback) {
  callback.Run(message_list_);
}

void FakeGsmSMSClient::RequestUpdate(const std::string& service_name,
                                     const dbus::ObjectPath& object_path) {
  if (!sms_test_message_switch_present_)
    return;

  if (test_index_ >= 0)
    return;
  test_index_ = 0;
  // Call PushTestMessageChain asynchronously so that the handler_ callback
  // does not get called from the update request.
  base::MessageLoop::current()->PostTask(
      FROM_HERE,
      base::Bind(&FakeGsmSMSClient::PushTestMessageChain,
                 weak_ptr_factory_.GetWeakPtr()));
}

void FakeGsmSMSClient::PushTestMessageChain() {
  if (PushTestMessage())
    PushTestMessageDelayed();
}

void FakeGsmSMSClient::PushTestMessageDelayed() {
  const int kSmsMessageDelaySeconds = 5;
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&FakeGsmSMSClient::PushTestMessageChain,
                 weak_ptr_factory_.GetWeakPtr()),
      base::TimeDelta::FromSeconds(kSmsMessageDelaySeconds));
}

bool FakeGsmSMSClient::PushTestMessage() {
  if (test_index_ >= static_cast<int>(test_messages_.size()))
    return false;
  base::DictionaryValue* message = new base::DictionaryValue;
  message->SetString("number", "000-000-0000");
  message->SetString("text", test_messages_[test_index_]);
  message->SetInteger("index", test_index_);
  int msg_index = message_list_.GetSize();
  message_list_.Append(message);
  if (!handler_.is_null())
    handler_.Run(msg_index, true);
  ++test_index_;
  return true;
}

}  // namespace chromeos