This source file includes following definitions.
- next_persistent_id_
- BuildGCMStore
- GetNextPersistentId
- PumpLoop
- LoadCallback
- UpdateCallback
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "google_apis/gcm/engine/gcm_store_impl.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "components/os_crypt/os_crypt.h"
#include "google_apis/gcm/base/mcs_message.h"
#include "google_apis/gcm/base/mcs_util.h"
#include "google_apis/gcm/protocol/mcs.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gcm {
namespace {
const int kNumPersistentIds = 10;
const int kNumMessagesPerApp = 20;
const char kAppName[] = "my_app";
const char kCategoryName[] = "my_category";
const uint64 kDeviceId = 22;
const uint64 kDeviceToken = 55;
class GCMStoreImplTest : public testing::Test {
public:
GCMStoreImplTest();
virtual ~GCMStoreImplTest();
scoped_ptr<GCMStore> BuildGCMStore();
std::string GetNextPersistentId();
void PumpLoop();
void LoadCallback(scoped_ptr<GCMStore::LoadResult>* result_dst,
scoped_ptr<GCMStore::LoadResult> result);
void UpdateCallback(bool success);
protected:
base::MessageLoop message_loop_;
base::ScopedTempDir temp_directory_;
bool expected_success_;
uint64 next_persistent_id_;
scoped_ptr<base::RunLoop> run_loop_;
};
GCMStoreImplTest::GCMStoreImplTest()
: expected_success_(true),
next_persistent_id_(base::Time::Now().ToInternalValue()) {
EXPECT_TRUE(temp_directory_.CreateUniqueTempDir());
run_loop_.reset(new base::RunLoop());
}
GCMStoreImplTest::~GCMStoreImplTest() {}
scoped_ptr<GCMStore> GCMStoreImplTest::BuildGCMStore() {
#if defined(OS_MACOSX)
OSCrypt::UseMockKeychain(true);
#endif
return scoped_ptr<GCMStore>(new GCMStoreImpl(
temp_directory_.path(),
message_loop_.message_loop_proxy()));
}
std::string GCMStoreImplTest::GetNextPersistentId() {
return base::Uint64ToString(next_persistent_id_++);
}
void GCMStoreImplTest::PumpLoop() { message_loop_.RunUntilIdle(); }
void GCMStoreImplTest::LoadCallback(
scoped_ptr<GCMStore::LoadResult>* result_dst,
scoped_ptr<GCMStore::LoadResult> result) {
ASSERT_TRUE(result->success);
*result_dst = result.Pass();
run_loop_->Quit();
run_loop_.reset(new base::RunLoop());
}
void GCMStoreImplTest::UpdateCallback(bool success) {
ASSERT_EQ(expected_success_, success);
}
TEST_F(GCMStoreImplTest, LoadNew) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
EXPECT_EQ(0U, load_result->device_android_id);
EXPECT_EQ(0U, load_result->device_security_token);
EXPECT_TRUE(load_result->incoming_messages.empty());
EXPECT_TRUE(load_result->outgoing_messages.empty());
EXPECT_EQ(base::Time::FromInternalValue(0LL), load_result->last_checkin_time);
}
TEST_F(GCMStoreImplTest, DeviceCredentials) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
gcm_store->SetDeviceCredentials(
kDeviceId,
kDeviceToken,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
gcm_store = BuildGCMStore().Pass();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_EQ(kDeviceId, load_result->device_android_id);
ASSERT_EQ(kDeviceToken, load_result->device_security_token);
}
TEST_F(GCMStoreImplTest, LastCheckinTime) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
base::Time last_checkin_time = base::Time::Now();
gcm_store->SetLastCheckinTime(
last_checkin_time,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
gcm_store = BuildGCMStore().Pass();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_EQ(last_checkin_time, load_result->last_checkin_time);
}
TEST_F(GCMStoreImplTest, Registrations) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
linked_ptr<RegistrationInfo> registration1(new RegistrationInfo);
registration1->sender_ids.push_back("sender1");
registration1->registration_id = "registration1";
gcm_store->AddRegistration(
"app1",
registration1,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
linked_ptr<RegistrationInfo> registration2(new RegistrationInfo);
registration2->sender_ids.push_back("sender2_1");
registration2->sender_ids.push_back("sender2_2");
registration2->registration_id = "registration2";
gcm_store->AddRegistration(
"app2",
registration2,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
gcm_store = BuildGCMStore().Pass();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_EQ(2u, load_result->registrations.size());
ASSERT_TRUE(load_result->registrations.find("app1") !=
load_result->registrations.end());
EXPECT_EQ(registration1->registration_id,
load_result->registrations["app1"]->registration_id);
ASSERT_EQ(1u, load_result->registrations["app1"]->sender_ids.size());
EXPECT_EQ(registration1->sender_ids[0],
load_result->registrations["app1"]->sender_ids[0]);
ASSERT_TRUE(load_result->registrations.find("app2") !=
load_result->registrations.end());
EXPECT_EQ(registration2->registration_id,
load_result->registrations["app2"]->registration_id);
ASSERT_EQ(2u, load_result->registrations["app2"]->sender_ids.size());
EXPECT_EQ(registration2->sender_ids[0],
load_result->registrations["app2"]->sender_ids[0]);
EXPECT_EQ(registration2->sender_ids[1],
load_result->registrations["app2"]->sender_ids[1]);
}
TEST_F(GCMStoreImplTest, IncomingMessages) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
std::vector<std::string> persistent_ids;
for (int i = 0; i < kNumPersistentIds; ++i) {
persistent_ids.push_back(GetNextPersistentId());
gcm_store->AddIncomingMessage(
persistent_ids.back(),
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
}
gcm_store = BuildGCMStore().Pass();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_EQ(persistent_ids, load_result->incoming_messages);
ASSERT_TRUE(load_result->outgoing_messages.empty());
gcm_store->RemoveIncomingMessages(
persistent_ids,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
gcm_store = BuildGCMStore().Pass();
load_result->incoming_messages.clear();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_TRUE(load_result->incoming_messages.empty());
ASSERT_TRUE(load_result->outgoing_messages.empty());
}
TEST_F(GCMStoreImplTest, OutgoingMessages) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
std::vector<std::string> persistent_ids;
const int kNumPersistentIds = 10;
for (int i = 0; i < kNumPersistentIds; ++i) {
persistent_ids.push_back(GetNextPersistentId());
mcs_proto::DataMessageStanza message;
message.set_from(kAppName + persistent_ids.back());
message.set_category(kCategoryName + persistent_ids.back());
gcm_store->AddOutgoingMessage(
persistent_ids.back(),
MCSMessage(message),
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
}
gcm_store = BuildGCMStore().Pass();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_TRUE(load_result->incoming_messages.empty());
ASSERT_EQ(load_result->outgoing_messages.size(), persistent_ids.size());
for (int i = 0; i < kNumPersistentIds; ++i) {
std::string id = persistent_ids[i];
ASSERT_TRUE(load_result->outgoing_messages[id].get());
const mcs_proto::DataMessageStanza* message =
reinterpret_cast<mcs_proto::DataMessageStanza*>(
load_result->outgoing_messages[id].get());
ASSERT_EQ(message->from(), kAppName + id);
ASSERT_EQ(message->category(), kCategoryName + id);
}
gcm_store->RemoveOutgoingMessages(
persistent_ids,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
gcm_store = BuildGCMStore().Pass();
load_result->outgoing_messages.clear();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_TRUE(load_result->incoming_messages.empty());
ASSERT_TRUE(load_result->outgoing_messages.empty());
}
TEST_F(GCMStoreImplTest, IncomingAndOutgoingMessages) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
std::vector<std::string> persistent_ids;
const int kNumPersistentIds = 10;
for (int i = 0; i < kNumPersistentIds; ++i) {
persistent_ids.push_back(GetNextPersistentId());
gcm_store->AddIncomingMessage(
persistent_ids.back(),
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
mcs_proto::DataMessageStanza message;
message.set_from(kAppName + persistent_ids.back());
message.set_category(kCategoryName + persistent_ids.back());
gcm_store->AddOutgoingMessage(
persistent_ids.back(),
MCSMessage(message),
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
}
gcm_store = BuildGCMStore().Pass();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_EQ(persistent_ids, load_result->incoming_messages);
ASSERT_EQ(load_result->outgoing_messages.size(), persistent_ids.size());
for (int i = 0; i < kNumPersistentIds; ++i) {
std::string id = persistent_ids[i];
ASSERT_TRUE(load_result->outgoing_messages[id].get());
const mcs_proto::DataMessageStanza* message =
reinterpret_cast<mcs_proto::DataMessageStanza*>(
load_result->outgoing_messages[id].get());
ASSERT_EQ(message->from(), kAppName + id);
ASSERT_EQ(message->category(), kCategoryName + id);
}
gcm_store->RemoveIncomingMessages(
persistent_ids,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
gcm_store->RemoveOutgoingMessages(
persistent_ids,
base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
PumpLoop();
gcm_store = BuildGCMStore().Pass();
load_result->incoming_messages.clear();
load_result->outgoing_messages.clear();
gcm_store->Load(base::Bind(
&GCMStoreImplTest::LoadCallback, base::Unretained(this), &load_result));
PumpLoop();
ASSERT_TRUE(load_result->incoming_messages.empty());
ASSERT_TRUE(load_result->outgoing_messages.empty());
}
TEST_F(GCMStoreImplTest, PerAppMessageLimits) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(&GCMStoreImplTest::LoadCallback,
base::Unretained(this),
&load_result));
for (int i = 0; i < kNumMessagesPerApp; ++i) {
mcs_proto::DataMessageStanza message;
message.set_from(kAppName);
message.set_category(kCategoryName);
EXPECT_TRUE(gcm_store->AddOutgoingMessage(
base::IntToString(i),
MCSMessage(message),
base::Bind(&GCMStoreImplTest::UpdateCallback,
base::Unretained(this))));
PumpLoop();
}
for (int i = 0; i < kNumMessagesPerApp; ++i) {
mcs_proto::DataMessageStanza message;
message.set_from(kAppName);
message.set_category(kCategoryName);
EXPECT_FALSE(gcm_store->AddOutgoingMessage(
base::IntToString(i + kNumMessagesPerApp),
MCSMessage(message),
base::Bind(&GCMStoreImplTest::UpdateCallback,
base::Unretained(this))));
PumpLoop();
}
gcm_store = BuildGCMStore().Pass();
gcm_store->Load(base::Bind(&GCMStoreImplTest::LoadCallback,
base::Unretained(this),
&load_result));
PumpLoop();
for (int i = 0; i < kNumMessagesPerApp; ++i) {
mcs_proto::DataMessageStanza message;
message.set_from(kAppName);
message.set_category(kCategoryName);
EXPECT_FALSE(gcm_store->AddOutgoingMessage(
base::IntToString(i + kNumMessagesPerApp),
MCSMessage(message),
base::Bind(&GCMStoreImplTest::UpdateCallback,
base::Unretained(this))));
PumpLoop();
}
for (int i = 0; i < kNumMessagesPerApp; ++i) {
gcm_store->RemoveOutgoingMessage(
base::IntToString(i),
base::Bind(&GCMStoreImplTest::UpdateCallback,
base::Unretained(this)));
PumpLoop();
}
for (int i = 0; i < kNumMessagesPerApp; ++i) {
mcs_proto::DataMessageStanza message;
message.set_from(kAppName);
message.set_category(kCategoryName);
EXPECT_TRUE(gcm_store->AddOutgoingMessage(
base::IntToString(i + kNumMessagesPerApp),
MCSMessage(message),
base::Bind(&GCMStoreImplTest::UpdateCallback,
base::Unretained(this))));
PumpLoop();
}
}
TEST_F(GCMStoreImplTest, AddMessageAfterDestroy) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(&GCMStoreImplTest::LoadCallback,
base::Unretained(this),
&load_result));
PumpLoop();
gcm_store->Destroy(base::Bind(&GCMStoreImplTest::UpdateCallback,
base::Unretained(this)));
PumpLoop();
expected_success_ = false;
for (int i = 0; i < kNumMessagesPerApp * 2; ++i) {
mcs_proto::DataMessageStanza message;
message.set_from(kAppName);
message.set_category(kCategoryName);
EXPECT_TRUE(gcm_store->AddOutgoingMessage(
base::IntToString(i),
MCSMessage(message),
base::Bind(&GCMStoreImplTest::UpdateCallback,
base::Unretained(this))));
PumpLoop();
}
}
TEST_F(GCMStoreImplTest, ReloadAfterClose) {
scoped_ptr<GCMStore> gcm_store(BuildGCMStore());
scoped_ptr<GCMStore::LoadResult> load_result;
gcm_store->Load(base::Bind(&GCMStoreImplTest::LoadCallback,
base::Unretained(this),
&load_result));
PumpLoop();
gcm_store->Close();
PumpLoop();
gcm_store->Load(base::Bind(&GCMStoreImplTest::LoadCallback,
base::Unretained(this),
&load_result));
PumpLoop();
}
}
}