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

Side by Side 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, 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/gcm_driver/instance_id/instance_id_driver.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "components/gcm_driver/instance_id/instance_id.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace instance_id {
14
15 const char kTestAppID1[] = "TestApp1";
16 const char kTestAppID2[] = "TestApp2";
17
18 class InstanceIDDriverTest : public testing::Test {
19 public:
20 InstanceIDDriverTest();
21 ~InstanceIDDriverTest() override;
22
23 // testing::Test:
24 void SetUp() override;
25
26 void WaitForAsyncOperation();
27
28 void DeleteIDCompleted(InstanceID* instance_id, InstanceID::Result result);
29
30 InstanceIDDriver* driver() const { return driver_.get(); }
31 InstanceID::Result delete_id_result() const { return delete_id_result_; }
32
33 private:
34 base::MessageLoopForUI message_loop_;
35 scoped_ptr<InstanceIDDriver> driver_;
36 InstanceID::Result delete_id_result_;
37 base::Closure async_operation_completed_callback_;
38
39 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest);
40 };
41
42 InstanceIDDriverTest::InstanceIDDriverTest()
43 : delete_id_result_(InstanceID::UNKNOWN_ERROR) {
44 }
45
46 InstanceIDDriverTest::~InstanceIDDriverTest() {
47 }
48
49 void InstanceIDDriverTest::SetUp() {
50 driver_.reset(new InstanceIDDriver());
51 }
52
53 void InstanceIDDriverTest::WaitForAsyncOperation() {
54 base::RunLoop run_loop;
55 async_operation_completed_callback_ = run_loop.QuitClosure();
56 run_loop.Run();
57 }
58
59 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID* instance_id,
60 InstanceID::Result result) {
61 delete_id_result_ = result;
62 if (!async_operation_completed_callback_.is_null())
63 async_operation_completed_callback_.Run();
64 }
65
66 TEST_F(InstanceIDDriverTest, NewID) {
67 // New ID is generated for the first time.
68 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1);
69 std::string id1 = instance_id1->GetID();
70 EXPECT_FALSE(id1.empty());
71 base::Time creation_time1 = instance_id1->GetCreationTime();
72 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
73
74 // Same ID is returned for the same app.
75 EXPECT_EQ(id1, instance_id1->GetID());
76 EXPECT_EQ(creation_time1, instance_id1->GetCreationTime());
77
78 // New ID is generated for another app.
79 InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2);
80 std::string id2 = instance_id2->GetID();
81 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
82 EXPECT_NE(id1, id2);
83 }
84
85 TEST_F(InstanceIDDriverTest, DeleteID) {
86 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
87 std::string id1 = instance_id->GetID();
88 EXPECT_FALSE(id1.empty());
89
90 // New ID will be generated from GetID after calling DeleteID.
91 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted,
92 base::Unretained(this)));
93 WaitForAsyncOperation();
94 EXPECT_EQ(InstanceID::SUCCESS, delete_id_result());
95
96 std::string id2 = instance_id->GetID();
97 EXPECT_FALSE(id2.empty());
98 EXPECT_NE(id1, id2);
99 }
100
101 } // instance_id
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698