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

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

Issue 1113213002: Generate Instance ID locally. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address one more comment Created 5 years, 8 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
new file mode 100644
index 0000000000000000000000000000000000000000..2f2fab85521cddd89158bac26150ce36b2edb621
--- /dev/null
+++ b/components/gcm_driver/instance_id/instance_id_driver_unittest.cc
@@ -0,0 +1,130 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/gcm_driver/instance_id/instance_id_driver.h"
+
+#include <cmath>
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/strings/string_util.h"
+#include "components/gcm_driver/instance_id/instance_id.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace instance_id {
+
+namespace {
+
+const char kTestAppID1[] = "TestApp1";
+const char kTestAppID2[] = "TestApp2";
+
+bool VerifyInstanceID(const std::string& str) {
+ // Checks the length.
+ if (str.length() != static_cast<size_t>(
+ std::ceil(InstanceID::kInstanceIDByteLength * 8 / 6.0)))
+ return false;
+
+ // Checks if it is URL-safe base64 encoded.
+ for (auto ch : str) {
+ if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_' && ch != '-')
+ return false;
+ }
+ return true;
+}
+
+} // namespace
+
+class InstanceIDDriverTest : public testing::Test {
+ public:
+ InstanceIDDriverTest();
+ ~InstanceIDDriverTest() override;
+
+ // testing::Test:
+ void SetUp() override;
+
+ void WaitForAsyncOperation();
+
+ void DeleteIDCompleted(InstanceID* instance_id, InstanceID::Result result);
+
+ InstanceIDDriver* driver() const { return driver_.get(); }
+ InstanceID::Result delete_id_result() const { return delete_id_result_; }
+
+ private:
+ base::MessageLoopForUI message_loop_;
+ scoped_ptr<InstanceIDDriver> driver_;
+ InstanceID::Result delete_id_result_;
+ base::Closure async_operation_completed_callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest);
+};
+
+InstanceIDDriverTest::InstanceIDDriverTest()
+ : delete_id_result_(InstanceID::UNKNOWN_ERROR) {
+}
+
+InstanceIDDriverTest::~InstanceIDDriverTest() {
+}
+
+void InstanceIDDriverTest::SetUp() {
+ driver_.reset(new InstanceIDDriver());
+}
+
+void InstanceIDDriverTest::WaitForAsyncOperation() {
+ base::RunLoop run_loop;
+ async_operation_completed_callback_ = run_loop.QuitClosure();
+ run_loop.Run();
+}
+
+void InstanceIDDriverTest::DeleteIDCompleted(InstanceID* instance_id,
+ InstanceID::Result result) {
+ delete_id_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);
+ EXPECT_TRUE(instance_id1->GetCreationTime().is_null());
+
+ // New ID is generated for the first time.
+ std::string id1 = instance_id1->GetID();
+ EXPECT_FALSE(id1.empty());
+ EXPECT_TRUE(VerifyInstanceID(id1));
+ base::Time creation_time = instance_id1->GetCreationTime();
+ EXPECT_FALSE(creation_time.is_null());
+
+ // Same ID is returned for the same app.
+ EXPECT_EQ(id1, instance_id1->GetID());
+ EXPECT_EQ(creation_time, instance_id1->GetCreationTime());
+
+ // New ID is generated for another app.
+ InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2);
+ std::string id2 = instance_id2->GetID();
+ EXPECT_FALSE(id2.empty());
+ EXPECT_TRUE(VerifyInstanceID(id2));
+ EXPECT_NE(id1, id2);
+ EXPECT_FALSE(instance_id2->GetCreationTime().is_null());
+}
+
+TEST_F(InstanceIDDriverTest, DeleteID) {
+ InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
+ std::string id1 = instance_id->GetID();
+ EXPECT_FALSE(id1.empty());
+ EXPECT_FALSE(instance_id->GetCreationTime().is_null());
+
+ // New ID will be generated from GetID after calling DeleteID.
+ instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted,
+ base::Unretained(this)));
+ WaitForAsyncOperation();
+ EXPECT_EQ(InstanceID::SUCCESS, delete_id_result());
+ EXPECT_TRUE(instance_id->GetCreationTime().is_null());
+
+ std::string id2 = instance_id->GetID();
+ EXPECT_FALSE(id2.empty());
+ EXPECT_NE(id1, id2);
+ EXPECT_FALSE(instance_id->GetCreationTime().is_null());
+}
+
+} // instance_id

Powered by Google App Engine
This is Rietveld 408576698