Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Unified Diff: components/gcm_driver/instance_id/instance_id_driver_unittest.cc

Issue 1137463003: Support getting and deleting token for Instance ID. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add new files Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/gcm_driver/instance_id/instance_id_driver_unittest.cc
diff --git a/components/gcm_driver/instance_id/instance_id_driver_unittest.cc b/components/gcm_driver/instance_id/instance_id_driver_unittest.cc
index 41305b524d07f76601e45f35d7b082be06f1fd58..12acb0b3eb1278dd2a88e89a893d9084b06e9104 100644
--- a/components/gcm_driver/instance_id/instance_id_driver_unittest.cc
+++ b/components/gcm_driver/instance_id/instance_id_driver_unittest.cc
@@ -19,6 +19,10 @@ namespace {
const char kTestAppID1[] = "TestApp1";
const char kTestAppID2[] = "TestApp2";
+const char kAuthorizedEntity1[] = "Sender 1";
+const char kAuthorizedEntity2[] = "Sender 2";
+const char kScope1[] = "GCM1";
+const char kScope2[] = "FooBar";
bool VerifyInstanceID(const std::string& str) {
// Checks the length.
@@ -53,6 +57,15 @@ class InstanceIDDriverTest : public testing::Test {
std::string GetID(InstanceID* instance_id);
base::Time GetCreationTime(InstanceID* instance_id);
InstanceID::Result DeleteID(InstanceID* instance_id);
+ std::string GetToken(
+ InstanceID* instance_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ const std::map<std::string, std::string>& options);
+ InstanceID::Result DeleteToken(
+ InstanceID* instance_id,
+ const std::string& authorized_entity,
+ const std::string& scope);
InstanceIDDriver* driver() const { return driver_.get(); }
@@ -60,6 +73,8 @@ class InstanceIDDriverTest : public testing::Test {
void GetIDCompleted(const std::string& id);
void GetCreationTimeCompleted(const base::Time& creation_time);
void DeleteIDCompleted(InstanceID::Result result);
+ void GetTokenCompleted(const std::string& token, InstanceID::Result result);
+ void DeleteTokenCompleted(InstanceID::Result result);
base::MessageLoopForUI message_loop_;
scoped_ptr<FakeGCMDriverForInstanceID> gcm_driver_;
@@ -67,6 +82,7 @@ class InstanceIDDriverTest : public testing::Test {
std::string id_;
base::Time creation_time_;
+ std::string token_;
InstanceID::Result result_;
bool async_operation_completed_;
@@ -129,6 +145,39 @@ InstanceID::Result InstanceIDDriverTest::DeleteID(InstanceID* instance_id) {
return result_;
}
+std::string InstanceIDDriverTest::GetToken(
+ InstanceID* instance_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ const std::map<std::string, std::string>& options) {
+ async_operation_completed_ = false;
+ token_.clear();
+ result_ = InstanceID::UNKNOWN_ERROR;;
+ instance_id->GetToken(
+ authorized_entity,
+ scope,
+ options,
+ base::Bind(&InstanceIDDriverTest::GetTokenCompleted,
+ base::Unretained(this)));
+ WaitForAsyncOperation();
+ return token_;
+}
+
+InstanceID::Result InstanceIDDriverTest::DeleteToken(
+ InstanceID* instance_id,
+ const std::string& authorized_entity,
+ const std::string& scope) {
+ async_operation_completed_ = false;
+ result_ = InstanceID::UNKNOWN_ERROR;;
+ instance_id->DeleteToken(
+ authorized_entity,
+ scope,
+ base::Bind(&InstanceIDDriverTest::DeleteTokenCompleted,
+ base::Unretained(this)));
+ WaitForAsyncOperation();
+ return result_;
+}
+
void InstanceIDDriverTest::GetIDCompleted(const std::string& id) {
async_operation_completed_ = true;
id_ = id;
@@ -151,6 +200,22 @@ void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) {
async_operation_completed_callback_.Run();
}
+void InstanceIDDriverTest::GetTokenCompleted(
+ const std::string& token, InstanceID::Result result){
+ async_operation_completed_ = true;
+ token_ = token;
+ result_ = result;
+ if (!async_operation_completed_callback_.is_null())
+ async_operation_completed_callback_.Run();
+}
+
+void InstanceIDDriverTest::DeleteTokenCompleted(InstanceID::Result result) {
+ async_operation_completed_ = true;
+ result_ = result;
+ if (!async_operation_completed_callback_.is_null())
+ async_operation_completed_callback_.Run();
+}
+
TEST_F(InstanceIDDriverTest, NewID) {
// Creation time should not be set when the ID is not created.
InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1);
@@ -220,4 +285,44 @@ TEST_F(InstanceIDDriverTest, DeleteID) {
EXPECT_FALSE(GetCreationTime(instance_id).is_null());
}
+TEST_F(InstanceIDDriverTest, GetToken) {
+ InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
+ std::map<std::string, std::string> options;
+ std::string token1 =
+ GetToken(instance_id, kAuthorizedEntity1, kScope1, options);
+ EXPECT_FALSE(token1.empty());
+
+ // Same token is returned for same authorized entity and scope.
+ EXPECT_EQ(token1,
+ GetToken(instance_id, kAuthorizedEntity1, kScope1, options));
+
+ // Different token is returned for different authorized entity or scope.
+ std::string token2 =
+ GetToken(instance_id, kAuthorizedEntity1, kScope2, options);
+ EXPECT_FALSE(token2.empty());
+ EXPECT_NE(token1, token2);
+
+ std::string token3 =
+ GetToken(instance_id, kAuthorizedEntity2, kScope1, options);
+ EXPECT_FALSE(token3.empty());
+ EXPECT_NE(token1, token3);
+ EXPECT_NE(token2, token3);
+}
+
+TEST_F(InstanceIDDriverTest, DeleteToken) {
+ InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
+ std::map<std::string, std::string> options;
+ std::string token1 =
+ GetToken(instance_id, kAuthorizedEntity1, kScope1, options);
+ EXPECT_FALSE(token1.empty());
+
fgorski 2015/05/13 18:32:39 add second token and verify that it is still there
jianli 2015/05/13 22:42:56 Done.
+ // Same token is returned for same authorized entity and scope after deletion.
+ EXPECT_EQ(InstanceID::SUCCESS,
+ DeleteToken(instance_id, kAuthorizedEntity1, kScope1));
+ std::string token2 =
+ GetToken(instance_id, kAuthorizedEntity1, kScope2, options);
+ EXPECT_FALSE(token2.empty());
+ EXPECT_NE(token1, token2);
+}
+
} // instance_id

Powered by Google App Engine
This is Rietveld 408576698