summaryrefslogtreecommitdiffstats
path: root/chrome/browser/services/gcm/gcm_client_mock.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/services/gcm/gcm_client_mock.cc')
-rw-r--r--chrome/browser/services/gcm/gcm_client_mock.cc144
1 files changed, 32 insertions, 112 deletions
diff --git a/chrome/browser/services/gcm/gcm_client_mock.cc b/chrome/browser/services/gcm/gcm_client_mock.cc
index 4945a27..45f4349 100644
--- a/chrome/browser/services/gcm/gcm_client_mock.cc
+++ b/chrome/browser/services/gcm/gcm_client_mock.cc
@@ -13,21 +13,10 @@
namespace gcm {
-namespace {
-
-// Converts the 8-byte prefix of a string into a uint64 value.
-uint64 HashToUInt64(const std::string& hash) {
- uint64 value;
- DCHECK_GE(hash.size(), sizeof(value));
- memcpy(&value, hash.data(), sizeof(value));
- return base::HostToNet64(value);
-}
-
-} // namespace
-
-GCMClientMock::GCMClientMock(Status status)
- : status_(status),
- simulate_server_error_(false) {
+GCMClientMock::GCMClientMock(Status status, ErrorSimulation error_simulation)
+ : delegate_(NULL),
+ status_(status),
+ error_simulation_(error_simulation) {
}
GCMClientMock::~GCMClientMock() {
@@ -38,60 +27,36 @@ void GCMClientMock::Initialize(
const base::FilePath& store_path,
const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
const scoped_refptr<net::URLRequestContextGetter>&
- url_request_context_getter) {
+ url_request_context_getter,
+ Delegate* delegate) {
+ delegate_ = delegate;
}
-void GCMClientMock::SetUserDelegate(const std::string& username,
- Delegate* delegate) {
+void GCMClientMock::CheckOut() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
-
- if (delegate)
- delegates_[username] = delegate;
- else
- delegates_.erase(username);
}
-void GCMClientMock::CheckIn(const std::string& username) {
- DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
-
- // Simulate the android_id and secret by some sort of hashing.
- CheckinInfo checkin_info;
- if (!simulate_server_error_)
- checkin_info = GetCheckinInfoFromUsername(username);
-
- base::MessageLoop::current()->PostTask(
- FROM_HERE,
- base::Bind(&GCMClientMock::CheckInFinished,
- base::Unretained(this),
- username,
- checkin_info));
-}
-
-void GCMClientMock::Register(const std::string& username,
- const std::string& app_id,
+void GCMClientMock::Register(const std::string& app_id,
const std::string& cert,
const std::vector<std::string>& sender_ids) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
std::string registration_id;
- if (!simulate_server_error_)
+ if (error_simulation_ == ALWAYS_SUCCEED)
registration_id = GetRegistrationIdFromSenderIds(sender_ids);
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&GCMClientMock::RegisterFinished,
base::Unretained(this),
- username,
app_id,
registration_id));
}
-void GCMClientMock::Unregister(const std::string& username,
- const std::string& app_id) {
+void GCMClientMock::Unregister(const std::string& app_id) {
}
-void GCMClientMock::Send(const std::string& username,
- const std::string& app_id,
+void GCMClientMock::Send(const std::string& app_id,
const std::string& receiver_id,
const OutgoingMessage& message) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
@@ -100,7 +65,6 @@ void GCMClientMock::Send(const std::string& username,
FROM_HERE,
base::Bind(&GCMClientMock::SendFinished,
base::Unretained(this),
- username,
app_id,
message.id));
}
@@ -109,8 +73,7 @@ bool GCMClientMock::IsReady() const {
return status_ == READY;
}
-void GCMClientMock::ReceiveMessage(const std::string& username,
- const std::string& app_id,
+void GCMClientMock::ReceiveMessage(const std::string& app_id,
const IncomingMessage& message) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
@@ -119,13 +82,11 @@ void GCMClientMock::ReceiveMessage(const std::string& username,
FROM_HERE,
base::Bind(&GCMClientMock::MessageReceived,
base::Unretained(this),
- username,
app_id,
message));
}
-void GCMClientMock::DeleteMessages(const std::string& username,
- const std::string& app_id) {
+void GCMClientMock::DeleteMessages(const std::string& app_id) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
content::BrowserThread::PostTask(
@@ -133,7 +94,6 @@ void GCMClientMock::DeleteMessages(const std::string& username,
FROM_HERE,
base::Bind(&GCMClientMock::MessagesDeleted,
base::Unretained(this),
- username,
app_id));
}
@@ -150,15 +110,6 @@ void GCMClientMock::SetReady() {
}
// static
-GCMClient::CheckinInfo GCMClientMock::GetCheckinInfoFromUsername(
- const std::string& username) {
- CheckinInfo checkin_info;
- checkin_info.android_id = HashToUInt64(username);
- checkin_info.secret = checkin_info.android_id / 10;
- return checkin_info;
-}
-
-// static
std::string GCMClientMock::GetRegistrationIdFromSenderIds(
const std::vector<std::string>& sender_ids) {
// GCMProfileService normalizes the sender IDs by making them sorted.
@@ -180,36 +131,15 @@ std::string GCMClientMock::GetRegistrationIdFromSenderIds(
return registration_id;
}
-GCMClient::Delegate* GCMClientMock::GetDelegate(
- const std::string& username) const {
- std::map<std::string, Delegate*>::const_iterator iter =
- delegates_.find(username);
- return iter == delegates_.end() ? NULL : iter->second;
-}
-
-void GCMClientMock::CheckInFinished(std::string username,
- CheckinInfo checkin_info) {
- GCMClient::Delegate* delegate = GetDelegate(username);
- DCHECK(delegate);
- delegate->OnCheckInFinished(
- checkin_info, checkin_info.IsValid() ? SUCCESS : SERVER_ERROR);
-}
-
-void GCMClientMock::RegisterFinished(std::string username,
- std::string app_id,
- std::string registrion_id) {
- GCMClient::Delegate* delegate = GetDelegate(username);
- DCHECK(delegate);
- delegate->OnRegisterFinished(
+void GCMClientMock::RegisterFinished(const std::string& app_id,
+ const std::string& registrion_id) {
+ delegate_->OnRegisterFinished(
app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
}
-void GCMClientMock::SendFinished(std::string username,
- std::string app_id,
- std::string message_id) {
- GCMClient::Delegate* delegate = GetDelegate(username);
- DCHECK(delegate);
- delegate->OnSendFinished(app_id, message_id, SUCCESS);
+void GCMClientMock::SendFinished(const std::string& app_id,
+ const std::string& message_id) {
+ delegate_->OnSendFinished(app_id, message_id, SUCCESS);
// Simulate send error if message id contains a hint.
if (message_id.find("error") != std::string::npos) {
@@ -217,41 +147,31 @@ void GCMClientMock::SendFinished(std::string username,
FROM_HERE,
base::Bind(&GCMClientMock::MessageSendError,
base::Unretained(this),
- username,
app_id,
message_id),
base::TimeDelta::FromMilliseconds(200));
}
}
-void GCMClientMock::MessageReceived(std::string username,
- std::string app_id,
- IncomingMessage message) {
- GCMClient::Delegate* delegate = GetDelegate(username);
- if (delegate)
- delegate->OnMessageReceived(app_id, message);
+void GCMClientMock::MessageReceived(const std::string& app_id,
+ const IncomingMessage& message) {
+ if (delegate_)
+ delegate_->OnMessageReceived(app_id, message);
}
-void GCMClientMock::MessagesDeleted(std::string username, std::string app_id) {
- GCMClient::Delegate* delegate = GetDelegate(username);
- if (delegate)
- delegate->OnMessagesDeleted(app_id);
+void GCMClientMock::MessagesDeleted(const std::string& app_id) {
+ if (delegate_)
+ delegate_->OnMessagesDeleted(app_id);
}
-void GCMClientMock::MessageSendError(std::string username,
- std::string app_id,
- std::string message_id) {
- GCMClient::Delegate* delegate = GetDelegate(username);
- if (delegate)
- delegate->OnMessageSendError(app_id, message_id, NETWORK_ERROR);
+void GCMClientMock::MessageSendError(const std::string& app_id,
+ const std::string& message_id) {
+ if (delegate_)
+ delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR);
}
void GCMClientMock::SetReadyOnIO() {
- for (std::map<std::string, Delegate*>::const_iterator iter =
- delegates_.begin();
- iter != delegates_.end(); ++iter) {
- iter->second->OnGCMReady();
- }
+ delegate_->OnGCMReady();
}
} // namespace gcm