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

Side by Side Diff: components/gcm_driver/instance_id/instance_id_driver_unittest.cc

Issue 1126233004: Persist Instance ID data to GCM store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix mac 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/gcm_driver/instance_id/instance_id_driver.h" 5 #include "components/gcm_driver/instance_id/instance_id_driver.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "components/gcm_driver/fake_gcm_driver.h" 12 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
13 #include "components/gcm_driver/instance_id/instance_id.h" 13 #include "components/gcm_driver/instance_id/instance_id.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace instance_id { 16 namespace instance_id {
17 17
18 namespace { 18 namespace {
19 19
20 const char kTestAppID1[] = "TestApp1"; 20 const char kTestAppID1[] = "TestApp1";
21 const char kTestAppID2[] = "TestApp2"; 21 const char kTestAppID2[] = "TestApp2";
22 22
(...skipping 16 matching lines...) Expand all
39 class InstanceIDDriverTest : public testing::Test { 39 class InstanceIDDriverTest : public testing::Test {
40 public: 40 public:
41 InstanceIDDriverTest(); 41 InstanceIDDriverTest();
42 ~InstanceIDDriverTest() override; 42 ~InstanceIDDriverTest() override;
43 43
44 // testing::Test: 44 // testing::Test:
45 void SetUp() override; 45 void SetUp() override;
46 46
47 void WaitForAsyncOperation(); 47 void WaitForAsyncOperation();
48 48
49 // Recreates InstanceIDDriver to simulate restart.
50 void RecreateInstanceIDDriver();
51
52 // Sync wrappers for async version.
53 std::string GetID(InstanceID* instance_id);
54 base::Time GetCreationTime(InstanceID* instance_id);
55 InstanceID::Result DeleteID(InstanceID* instance_id);
56
57 InstanceIDDriver* driver() const { return driver_.get(); }
58
59 private:
60 void GetIDCompleted(const std::string& id);
61 void GetCreationTimeCompleted(const base::Time& creation_time);
49 void DeleteIDCompleted(InstanceID::Result result); 62 void DeleteIDCompleted(InstanceID::Result result);
50 63
51 InstanceIDDriver* driver() const { return driver_.get(); } 64 base::MessageLoopForUI message_loop_;
52 InstanceID::Result delete_id_result() const { return delete_id_result_; } 65 scoped_ptr<FakeGCMDriverForInstanceID> gcm_driver_;
66 scoped_ptr<InstanceIDDriver> driver_;
53 67
54 private: 68 std::string id_;
55 base::MessageLoopForUI message_loop_; 69 base::Time creation_time_;
56 scoped_ptr<gcm::FakeGCMDriver> gcm_driver_; 70 InstanceID::Result result_;
57 scoped_ptr<InstanceIDDriver> driver_; 71
58 InstanceID::Result delete_id_result_; 72 bool async_operation_completed_;
59 base::Closure async_operation_completed_callback_; 73 base::Closure async_operation_completed_callback_;
60 74
61 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); 75 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest);
62 }; 76 };
63 77
64 InstanceIDDriverTest::InstanceIDDriverTest() 78 InstanceIDDriverTest::InstanceIDDriverTest()
65 : delete_id_result_(InstanceID::UNKNOWN_ERROR) { 79 : result_(InstanceID::UNKNOWN_ERROR),
80 async_operation_completed_(false) {
66 } 81 }
67 82
68 InstanceIDDriverTest::~InstanceIDDriverTest() { 83 InstanceIDDriverTest::~InstanceIDDriverTest() {
69 } 84 }
70 85
71 void InstanceIDDriverTest::SetUp() { 86 void InstanceIDDriverTest::SetUp() {
72 gcm_driver_.reset(new gcm::FakeGCMDriver); 87 gcm_driver_.reset(new FakeGCMDriverForInstanceID);
88 RecreateInstanceIDDriver();
89 }
90
91 void InstanceIDDriverTest::RecreateInstanceIDDriver() {
73 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); 92 driver_.reset(new InstanceIDDriver(gcm_driver_.get()));
74 } 93 }
75 94
76 void InstanceIDDriverTest::WaitForAsyncOperation() { 95 void InstanceIDDriverTest::WaitForAsyncOperation() {
96 // No need to wait if async operation is not needed.
97 if (async_operation_completed_)
98 return;
77 base::RunLoop run_loop; 99 base::RunLoop run_loop;
78 async_operation_completed_callback_ = run_loop.QuitClosure(); 100 async_operation_completed_callback_ = run_loop.QuitClosure();
79 run_loop.Run(); 101 run_loop.Run();
80 } 102 }
81 103
82 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) { 104 std::string InstanceIDDriverTest::GetID(InstanceID* instance_id) {
83 delete_id_result_ = result; 105 async_operation_completed_ = false;
106 id_.clear();
107 instance_id->GetID(base::Bind(&InstanceIDDriverTest::GetIDCompleted,
108 base::Unretained(this)));
109 WaitForAsyncOperation();
110 return id_;
111 }
112
113 base::Time InstanceIDDriverTest::GetCreationTime(InstanceID* instance_id) {
114 async_operation_completed_ = false;
115 creation_time_ = base::Time();
116 instance_id->GetCreationTime(
117 base::Bind(&InstanceIDDriverTest::GetCreationTimeCompleted,
118 base::Unretained(this)));
119 WaitForAsyncOperation();
120 return creation_time_;
121 }
122
123 InstanceID::Result InstanceIDDriverTest::DeleteID(InstanceID* instance_id) {
124 async_operation_completed_ = false;
125 result_ = InstanceID::UNKNOWN_ERROR;;
126 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted,
127 base::Unretained(this)));
128 WaitForAsyncOperation();
129 return result_;
130 }
131
132 void InstanceIDDriverTest::GetIDCompleted(const std::string& id) {
133 async_operation_completed_ = true;
134 id_ = id;
84 if (!async_operation_completed_callback_.is_null()) 135 if (!async_operation_completed_callback_.is_null())
85 async_operation_completed_callback_.Run(); 136 async_operation_completed_callback_.Run();
86 } 137 }
138
139 void InstanceIDDriverTest::GetCreationTimeCompleted(
140 const base::Time& creation_time) {
141 async_operation_completed_ = true;
142 creation_time_ = creation_time;
143 if (!async_operation_completed_callback_.is_null())
144 async_operation_completed_callback_.Run();
145 }
146
147 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) {
148 async_operation_completed_ = true;
149 result_ = result;
150 if (!async_operation_completed_callback_.is_null())
151 async_operation_completed_callback_.Run();
152 }
87 153
88 TEST_F(InstanceIDDriverTest, NewID) { 154 TEST_F(InstanceIDDriverTest, NewID) {
89 // Creation time should not be set when the ID is not created. 155 // Creation time should not be set when the ID is not created.
90 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); 156 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1);
91 EXPECT_TRUE(instance_id1->GetCreationTime().is_null()); 157 EXPECT_TRUE(GetCreationTime(instance_id1).is_null());
92 158
93 // New ID is generated for the first time. 159 // New ID is generated for the first time.
94 std::string id1 = instance_id1->GetID(); 160 std::string id1 = GetID(instance_id1);
95 EXPECT_FALSE(id1.empty());
96 EXPECT_TRUE(VerifyInstanceID(id1)); 161 EXPECT_TRUE(VerifyInstanceID(id1));
97 base::Time creation_time = instance_id1->GetCreationTime(); 162 base::Time creation_time = GetCreationTime(instance_id1);
98 EXPECT_FALSE(creation_time.is_null()); 163 EXPECT_FALSE(creation_time.is_null());
99 164
100 // Same ID is returned for the same app. 165 // Same ID is returned for the same app.
101 EXPECT_EQ(id1, instance_id1->GetID()); 166 EXPECT_EQ(id1, GetID(instance_id1));
102 EXPECT_EQ(creation_time, instance_id1->GetCreationTime()); 167 EXPECT_EQ(creation_time, GetCreationTime(instance_id1));
103 168
104 // New ID is generated for another app. 169 // New ID is generated for another app.
105 InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2); 170 InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2);
106 std::string id2 = instance_id2->GetID(); 171 std::string id2 = GetID(instance_id2);
107 EXPECT_FALSE(id2.empty());
108 EXPECT_TRUE(VerifyInstanceID(id2)); 172 EXPECT_TRUE(VerifyInstanceID(id2));
109 EXPECT_NE(id1, id2); 173 EXPECT_NE(id1, id2);
110 EXPECT_FALSE(instance_id2->GetCreationTime().is_null()); 174 EXPECT_FALSE(GetCreationTime(instance_id2).is_null());
175 }
176
177 TEST_F(InstanceIDDriverTest, PersistID) {
178 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
179
180 // Create the ID for the first time. The ID and creation time should be saved
181 // to the store.
182 std::string id = GetID(instance_id);
183 EXPECT_FALSE(id.empty());
184 base::Time creation_time = GetCreationTime(instance_id);
185 EXPECT_FALSE(creation_time.is_null());
186
187 // Simulate restart by recreating InstanceIDDriver. Same ID and creation time
188 // should be expected.
189 RecreateInstanceIDDriver();
190 instance_id = driver()->GetInstanceID(kTestAppID1);
191 EXPECT_EQ(creation_time, GetCreationTime(instance_id));
192 EXPECT_EQ(id, GetID(instance_id));
193
194 // Delete the ID. The ID and creation time should be removed from the store.
195 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id));
196 EXPECT_TRUE(GetCreationTime(instance_id).is_null());
197
198 // Simulate restart by recreating InstanceIDDriver. Different ID should be
199 // expected.
200 // Note that we do not check for different creation time since the test might
201 // be run at a very fast server.
202 RecreateInstanceIDDriver();
203 instance_id = driver()->GetInstanceID(kTestAppID1);
204 EXPECT_NE(id, GetID(instance_id));
111 } 205 }
112 206
113 TEST_F(InstanceIDDriverTest, DeleteID) { 207 TEST_F(InstanceIDDriverTest, DeleteID) {
114 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); 208 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
115 std::string id1 = instance_id->GetID(); 209 std::string id1 = GetID(instance_id);
116 EXPECT_FALSE(id1.empty()); 210 EXPECT_FALSE(id1.empty());
117 EXPECT_FALSE(instance_id->GetCreationTime().is_null()); 211 EXPECT_FALSE(GetCreationTime(instance_id).is_null());
118 212
119 // New ID will be generated from GetID after calling DeleteID. 213 // New ID will be generated from GetID after calling DeleteID.
120 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted, 214 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id));
121 base::Unretained(this))); 215 EXPECT_TRUE(GetCreationTime(instance_id).is_null());
122 WaitForAsyncOperation();
123 EXPECT_EQ(InstanceID::SUCCESS, delete_id_result());
124 EXPECT_TRUE(instance_id->GetCreationTime().is_null());
125 216
126 std::string id2 = instance_id->GetID(); 217 std::string id2 = GetID(instance_id);
127 EXPECT_FALSE(id2.empty()); 218 EXPECT_FALSE(id2.empty());
128 EXPECT_NE(id1, id2); 219 EXPECT_NE(id1, id2);
129 EXPECT_FALSE(instance_id->GetCreationTime().is_null()); 220 EXPECT_FALSE(GetCreationTime(instance_id).is_null());
130 } 221 }
131 222
132 } // instance_id 223 } // instance_id
OLDNEW
« no previous file with comments | « components/gcm_driver/instance_id/instance_id_android.cc ('k') | components/gcm_driver/instance_id/instance_id_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698