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.
5 #include "chrome/browser/services/gcm/gcm_client_mock.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/sys_byteorder.h"
11 #include "base/time/time.h"
12 #include "content/public/browser/browser_thread.h"
16 GCMClientMock::GCMClientMock(Status status, ErrorSimulation error_simulation)
19 error_simulation_(error_simulation) {
22 GCMClientMock::~GCMClientMock() {
25 void GCMClientMock::Initialize(
26 const checkin_proto::ChromeBuildProto& chrome_build_proto,
27 const base::FilePath& store_path,
28 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
29 const scoped_refptr<net::URLRequestContextGetter>&
30 url_request_context_getter,
35 void GCMClientMock::CheckOut() {
36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
39 void GCMClientMock::Register(const std::string& app_id,
40 const std::string& cert,
41 const std::vector<std::string>& sender_ids) {
42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
44 std::string registration_id;
45 if (error_simulation_ == ALWAYS_SUCCEED)
46 registration_id = GetRegistrationIdFromSenderIds(sender_ids);
48 base::MessageLoop::current()->PostTask(
50 base::Bind(&GCMClientMock::RegisterFinished,
51 base::Unretained(this),
56 void GCMClientMock::Unregister(const std::string& app_id) {
59 void GCMClientMock::Send(const std::string& app_id,
60 const std::string& receiver_id,
61 const OutgoingMessage& message) {
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
64 base::MessageLoop::current()->PostTask(
66 base::Bind(&GCMClientMock::SendFinished,
67 base::Unretained(this),
72 bool GCMClientMock::IsReady() const {
73 return status_ == READY;
76 void GCMClientMock::ReceiveMessage(const std::string& app_id,
77 const IncomingMessage& message) {
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
80 content::BrowserThread::PostTask(
81 content::BrowserThread::IO,
83 base::Bind(&GCMClientMock::MessageReceived,
84 base::Unretained(this),
89 void GCMClientMock::DeleteMessages(const std::string& app_id) {
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
92 content::BrowserThread::PostTask(
93 content::BrowserThread::IO,
95 base::Bind(&GCMClientMock::MessagesDeleted,
96 base::Unretained(this),
100 void GCMClientMock::SetReady() {
101 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
102 DCHECK_EQ(status_, NOT_READY);
105 content::BrowserThread::PostTask(
106 content::BrowserThread::IO,
108 base::Bind(&GCMClientMock::SetReadyOnIO,
109 base::Unretained(this)));
113 std::string GCMClientMock::GetRegistrationIdFromSenderIds(
114 const std::vector<std::string>& sender_ids) {
115 // GCMProfileService normalizes the sender IDs by making them sorted.
116 std::vector<std::string> normalized_sender_ids = sender_ids;
117 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
119 // Simulate the registration_id by concaternating all sender IDs.
120 // Set registration_id to empty to denote an error if sender_ids contains a
122 std::string registration_id;
123 if (sender_ids.size() != 1 ||
124 sender_ids[0].find("error") == std::string::npos) {
125 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
127 registration_id += ",";
128 registration_id += normalized_sender_ids[i];
131 return registration_id;
134 void GCMClientMock::RegisterFinished(const std::string& app_id,
135 const std::string& registrion_id) {
136 delegate_->OnRegisterFinished(
137 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
140 void GCMClientMock::SendFinished(const std::string& app_id,
141 const std::string& message_id) {
142 delegate_->OnSendFinished(app_id, message_id, SUCCESS);
144 // Simulate send error if message id contains a hint.
145 if (message_id.find("error") != std::string::npos) {
146 base::MessageLoop::current()->PostDelayedTask(
148 base::Bind(&GCMClientMock::MessageSendError,
149 base::Unretained(this),
152 base::TimeDelta::FromMilliseconds(200));
156 void GCMClientMock::MessageReceived(const std::string& app_id,
157 const IncomingMessage& message) {
159 delegate_->OnMessageReceived(app_id, message);
162 void GCMClientMock::MessagesDeleted(const std::string& app_id) {
164 delegate_->OnMessagesDeleted(app_id);
167 void GCMClientMock::MessageSendError(const std::string& app_id,
168 const std::string& message_id) {
170 delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR);
173 void GCMClientMock::SetReadyOnIO() {
174 delegate_->OnGCMReady();