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(LoadingDelay loading_delay
)
18 status_(UNINITIALIZED
),
19 loading_delay_(loading_delay
),
20 weak_ptr_factory_(this) {
23 GCMClientMock::~GCMClientMock() {
26 void GCMClientMock::Initialize(
27 const checkin_proto::ChromeBuildProto
& chrome_build_proto
,
28 const base::FilePath
& store_path
,
29 const std::vector
<std::string
>& account_ids
,
30 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
,
31 const scoped_refptr
<net::URLRequestContextGetter
>&
32 url_request_context_getter
,
37 void GCMClientMock::Load() {
38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
39 DCHECK_NE(LOADED
, status_
);
41 if (loading_delay_
== DELAY_LOADING
)
46 void GCMClientMock::DoLoading() {
48 base::MessageLoop::current()->PostTask(
50 base::Bind(&GCMClientMock::CheckinFinished
,
51 weak_ptr_factory_
.GetWeakPtr()));
54 void GCMClientMock::Stop() {
55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
59 void GCMClientMock::CheckOut() {
60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
61 status_
= CHECKED_OUT
;
64 void GCMClientMock::Register(const std::string
& app_id
,
65 const std::vector
<std::string
>& sender_ids
) {
66 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
68 std::string registration_id
= GetRegistrationIdFromSenderIds(sender_ids
);
69 base::MessageLoop::current()->PostTask(
71 base::Bind(&GCMClientMock::RegisterFinished
,
72 weak_ptr_factory_
.GetWeakPtr(),
77 void GCMClientMock::Unregister(const std::string
& app_id
) {
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
80 base::MessageLoop::current()->PostTask(
82 base::Bind(&GCMClientMock::UnregisterFinished
,
83 weak_ptr_factory_
.GetWeakPtr(),
87 void GCMClientMock::Send(const std::string
& app_id
,
88 const std::string
& receiver_id
,
89 const OutgoingMessage
& message
) {
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
92 base::MessageLoop::current()->PostTask(
94 base::Bind(&GCMClientMock::SendFinished
,
95 weak_ptr_factory_
.GetWeakPtr(),
100 void GCMClientMock::SetRecording(bool recording
) {
103 void GCMClientMock::ClearActivityLogs() {
106 GCMClient::GCMStatistics
GCMClientMock::GetStatistics() const {
107 return GCMClient::GCMStatistics();
110 void GCMClientMock::PerformDelayedLoading() {
111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
113 content::BrowserThread::PostTask(
114 content::BrowserThread::IO
,
116 base::Bind(&GCMClientMock::DoLoading
, weak_ptr_factory_
.GetWeakPtr()));
119 void GCMClientMock::ReceiveMessage(const std::string
& app_id
,
120 const IncomingMessage
& message
) {
121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
123 content::BrowserThread::PostTask(
124 content::BrowserThread::IO
,
126 base::Bind(&GCMClientMock::MessageReceived
,
127 weak_ptr_factory_
.GetWeakPtr(),
132 void GCMClientMock::DeleteMessages(const std::string
& app_id
) {
133 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
135 content::BrowserThread::PostTask(
136 content::BrowserThread::IO
,
138 base::Bind(&GCMClientMock::MessagesDeleted
,
139 weak_ptr_factory_
.GetWeakPtr(),
144 std::string
GCMClientMock::GetRegistrationIdFromSenderIds(
145 const std::vector
<std::string
>& sender_ids
) {
146 // GCMService normalizes the sender IDs by making them sorted.
147 std::vector
<std::string
> normalized_sender_ids
= sender_ids
;
148 std::sort(normalized_sender_ids
.begin(), normalized_sender_ids
.end());
150 // Simulate the registration_id by concaternating all sender IDs.
151 // Set registration_id to empty to denote an error if sender_ids contains a
153 std::string registration_id
;
154 if (sender_ids
.size() != 1 ||
155 sender_ids
[0].find("error") == std::string::npos
) {
156 for (size_t i
= 0; i
< normalized_sender_ids
.size(); ++i
) {
158 registration_id
+= ",";
159 registration_id
+= normalized_sender_ids
[i
];
162 return registration_id
;
165 void GCMClientMock::CheckinFinished() {
166 delegate_
->OnGCMReady();
169 void GCMClientMock::RegisterFinished(const std::string
& app_id
,
170 const std::string
& registrion_id
) {
171 delegate_
->OnRegisterFinished(
172 app_id
, registrion_id
, registrion_id
.empty() ? SERVER_ERROR
: SUCCESS
);
175 void GCMClientMock::UnregisterFinished(const std::string
& app_id
) {
176 delegate_
->OnUnregisterFinished(app_id
, GCMClient::SUCCESS
);
179 void GCMClientMock::SendFinished(const std::string
& app_id
,
180 const OutgoingMessage
& message
) {
181 delegate_
->OnSendFinished(app_id
, message
.id
, SUCCESS
);
183 // Simulate send error if message id contains a hint.
184 if (message
.id
.find("error") != std::string::npos
) {
185 SendErrorDetails send_error_details
;
186 send_error_details
.message_id
= message
.id
;
187 send_error_details
.result
= NETWORK_ERROR
;
188 send_error_details
.additional_data
= message
.data
;
189 base::MessageLoop::current()->PostDelayedTask(
191 base::Bind(&GCMClientMock::MessageSendError
,
192 weak_ptr_factory_
.GetWeakPtr(),
195 base::TimeDelta::FromMilliseconds(200));
199 void GCMClientMock::MessageReceived(const std::string
& app_id
,
200 const IncomingMessage
& message
) {
202 delegate_
->OnMessageReceived(app_id
, message
);
205 void GCMClientMock::MessagesDeleted(const std::string
& app_id
) {
207 delegate_
->OnMessagesDeleted(app_id
);
210 void GCMClientMock::MessageSendError(
211 const std::string
& app_id
,
212 const GCMClient::SendErrorDetails
& send_error_details
) {
214 delegate_
->OnMessageSendError(app_id
, send_error_details
);