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

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: Remove leftover code 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..559e3fc4488c4e95cdc0c35380a232bad5aeccbd
--- /dev/null
+++ b/components/gcm_driver/instance_id/instance_id_driver_unittest.cc
@@ -0,0 +1,101 @@
+// 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 "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "components/gcm_driver/instance_id/instance_id.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace instance_id {
+
+const char kTestAppID1[] = "TestApp1";
+const char kTestAppID2[] = "TestApp2";
+
+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) {
+ // New ID is generated for the first time.
+ InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1);
+ std::string id1 = instance_id1->GetID();
+ EXPECT_FALSE(id1.empty());
+ base::Time creation_time1 = instance_id1->GetCreationTime();
+ EXPECT_FALSE(creation_time1.is_null());
fgorski 2015/04/30 22:13:11 you can take before and after time and check that
jianli 2015/05/02 00:30:17 I don't think this is safe since it might be likel
+
+ // Same ID is returned for the same app.
+ EXPECT_EQ(id1, instance_id1->GetID());
+ EXPECT_EQ(creation_time1, 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());
fgorski 2015/04/30 22:13:12 can we check that created ids: * are of certain le
jianli 2015/05/02 00:30:17 We don't have a strict guideline for length. Added
+ EXPECT_NE(id1, id2);
+}
+
+TEST_F(InstanceIDDriverTest, DeleteID) {
+ InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
+ std::string id1 = instance_id->GetID();
+ EXPECT_FALSE(id1.empty());
+
+ // 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());
+
+ std::string id2 = instance_id->GetID();
+ EXPECT_FALSE(id2.empty());
+ EXPECT_NE(id1, id2);
+}
+
+} // instance_id

Powered by Google App Engine
This is Rietveld 408576698