OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/service_worker/service_worker_database.h" | 5 #include "content/browser/service_worker/service_worker_database.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "content/browser/service_worker/service_worker_database.pb.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 | 11 |
10 namespace content { | 12 namespace content { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 struct AvailableIds { | 16 struct AvailableIds { |
15 int64 reg_id; | 17 int64 reg_id; |
16 int64 res_id; | 18 int64 res_id; |
17 int64 ver_id; | 19 int64 ver_id; |
18 | 20 |
19 AvailableIds() : reg_id(-1), res_id(-1), ver_id(-1) {} | 21 AvailableIds() : reg_id(-1), res_id(-1), ver_id(-1) {} |
20 ~AvailableIds() {} | 22 ~AvailableIds() {} |
21 }; | 23 }; |
22 | 24 |
23 ServiceWorkerDatabase* CreateDatabase(const base::FilePath& path) { | 25 ServiceWorkerDatabase* CreateDatabase(const base::FilePath& path) { |
24 return new ServiceWorkerDatabase(path); | 26 return new ServiceWorkerDatabase(path); |
25 } | 27 } |
26 | 28 |
27 ServiceWorkerDatabase* CreateDatabaseInMemory() { | 29 ServiceWorkerDatabase* CreateDatabaseInMemory() { |
28 return new ServiceWorkerDatabase(base::FilePath()); | 30 return new ServiceWorkerDatabase(base::FilePath()); |
29 } | 31 } |
30 | 32 |
| 33 void VerifyRegistrationData( |
| 34 const ServiceWorkerDatabase::RegistrationData& expected, |
| 35 const ServiceWorkerDatabase::RegistrationData& actual) { |
| 36 EXPECT_EQ(expected.registration_id, actual.registration_id); |
| 37 EXPECT_EQ(expected.scope, actual.scope); |
| 38 EXPECT_EQ(expected.script, actual.script); |
| 39 EXPECT_EQ(expected.version_id, actual.version_id); |
| 40 EXPECT_EQ(expected.is_active, actual.is_active); |
| 41 EXPECT_EQ(expected.has_fetch_handler, actual.has_fetch_handler); |
| 42 EXPECT_EQ(expected.last_update_check, actual.last_update_check); |
| 43 } |
| 44 |
31 } // namespace | 45 } // namespace |
32 | 46 |
33 TEST(ServiceWorkerDatabaseTest, OpenDatabase) { | 47 TEST(ServiceWorkerDatabaseTest, OpenDatabase) { |
34 base::ScopedTempDir database_dir; | 48 base::ScopedTempDir database_dir; |
35 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 49 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
36 scoped_ptr<ServiceWorkerDatabase> database( | 50 scoped_ptr<ServiceWorkerDatabase> database( |
37 CreateDatabase(database_dir.path())); | 51 CreateDatabase(database_dir.path())); |
38 | 52 |
39 // Should be false because the database does not exist at the path. | 53 // Should be false because the database does not exist at the path. |
40 EXPECT_FALSE(database->LazyOpen(false)); | 54 EXPECT_FALSE(database->LazyOpen(false)); |
(...skipping 11 matching lines...) Expand all Loading... |
52 EXPECT_FALSE(database->LazyOpen(false)); | 66 EXPECT_FALSE(database->LazyOpen(false)); |
53 | 67 |
54 EXPECT_TRUE(database->LazyOpen(true)); | 68 EXPECT_TRUE(database->LazyOpen(true)); |
55 database.reset(CreateDatabaseInMemory()); | 69 database.reset(CreateDatabaseInMemory()); |
56 | 70 |
57 // Should be false because the database is not persistent. | 71 // Should be false because the database is not persistent. |
58 EXPECT_FALSE(database->LazyOpen(false)); | 72 EXPECT_FALSE(database->LazyOpen(false)); |
59 } | 73 } |
60 | 74 |
61 TEST(ServiceWorkerDatabaseTest, GetNextAvailableIds) { | 75 TEST(ServiceWorkerDatabaseTest, GetNextAvailableIds) { |
62 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | 76 base::ScopedTempDir database_dir; |
| 77 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 78 scoped_ptr<ServiceWorkerDatabase> database( |
| 79 CreateDatabase(database_dir.path())); |
63 AvailableIds ids; | 80 AvailableIds ids; |
64 | 81 |
65 // Should be false because the database hasn't been opened. | 82 // Should be false because the database hasn't been opened. |
66 EXPECT_FALSE(database->GetNextAvailableIds( | 83 EXPECT_FALSE(database->GetNextAvailableIds( |
67 &ids.reg_id, &ids.ver_id, &ids.res_id)); | 84 &ids.reg_id, &ids.ver_id, &ids.res_id)); |
68 | 85 |
69 ASSERT_TRUE(database->LazyOpen(true)); | 86 ASSERT_TRUE(database->LazyOpen(true)); |
70 EXPECT_TRUE(database->GetNextAvailableIds( | 87 EXPECT_TRUE(database->GetNextAvailableIds( |
71 &ids.reg_id, &ids.ver_id, &ids.res_id)); | 88 &ids.reg_id, &ids.ver_id, &ids.res_id)); |
72 EXPECT_EQ(0, ids.reg_id); | 89 EXPECT_EQ(0, ids.reg_id); |
73 EXPECT_EQ(0, ids.ver_id); | 90 EXPECT_EQ(0, ids.ver_id); |
74 EXPECT_EQ(0, ids.res_id); | 91 EXPECT_EQ(0, ids.res_id); |
75 | 92 |
76 // TODO(nhiroki): Test GetNextAvailableIds() after update these ids. | 93 // Writing a registration bumps the next available ids. |
| 94 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 95 ServiceWorkerDatabase::RegistrationData data1; |
| 96 data1.registration_id = 100; |
| 97 data1.scope = GURL("http://example.com/foo"); |
| 98 data1.script = GURL("http://example.com/script1.js"); |
| 99 data1.version_id = 200; |
| 100 ASSERT_TRUE(database->WriteRegistration(data1, resources)); |
| 101 |
| 102 EXPECT_TRUE(database->GetNextAvailableIds( |
| 103 &ids.reg_id, &ids.ver_id, &ids.res_id)); |
| 104 EXPECT_EQ(101, ids.reg_id); |
| 105 EXPECT_EQ(201, ids.ver_id); |
| 106 EXPECT_EQ(0, ids.res_id); |
| 107 |
| 108 // Writing a registration whose ids are lower than the stored ones should not |
| 109 // bump the next available ids. |
| 110 ServiceWorkerDatabase::RegistrationData data2; |
| 111 data2.registration_id = 10; |
| 112 data2.scope = GURL("http://example.com/bar"); |
| 113 data2.script = GURL("http://example.com/script2.js"); |
| 114 data2.version_id = 20; |
| 115 ASSERT_TRUE(database->WriteRegistration(data2, resources)); |
| 116 |
| 117 // Close and reopen the database to verify the stored values. |
| 118 database.reset(CreateDatabase(database_dir.path())); |
| 119 |
| 120 EXPECT_TRUE(database->GetNextAvailableIds( |
| 121 &ids.reg_id, &ids.ver_id, &ids.res_id)); |
| 122 EXPECT_EQ(101, ids.reg_id); |
| 123 EXPECT_EQ(201, ids.ver_id); |
| 124 EXPECT_EQ(0, ids.res_id); |
| 125 } |
| 126 |
| 127 TEST(ServiceWorkerDatabaseTest, GetOriginsWithRegistrations) { |
| 128 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 129 |
| 130 std::set<GURL> origins; |
| 131 EXPECT_FALSE(database->GetOriginsWithRegistrations(&origins)); |
| 132 EXPECT_TRUE(origins.empty()); |
| 133 |
| 134 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 135 |
| 136 ServiceWorkerDatabase::RegistrationData data1; |
| 137 data1.registration_id = 123; |
| 138 data1.scope = GURL("http://example.com/foo"); |
| 139 data1.script = GURL("http://example.com/script1.js"); |
| 140 data1.version_id = 456; |
| 141 GURL origin1 = data1.script.GetOrigin(); |
| 142 ASSERT_TRUE(database->WriteRegistration(data1, resources)); |
| 143 |
| 144 ServiceWorkerDatabase::RegistrationData data2; |
| 145 data2.registration_id = 234; |
| 146 data2.scope = GURL("https://www.example.com/bar"); |
| 147 data2.script = GURL("https://www.example.com/script2.js"); |
| 148 data2.version_id = 567; |
| 149 GURL origin2 = data2.script.GetOrigin(); |
| 150 ASSERT_TRUE(database->WriteRegistration(data2, resources)); |
| 151 |
| 152 ServiceWorkerDatabase::RegistrationData data3; |
| 153 data3.registration_id = 345; |
| 154 data3.scope = GURL("https://example.org/hoge"); |
| 155 data3.script = GURL("https://example.org/script3.js"); |
| 156 data3.version_id = 678; |
| 157 GURL origin3 = data3.script.GetOrigin(); |
| 158 ASSERT_TRUE(database->WriteRegistration(data3, resources)); |
| 159 |
| 160 // |origin3| has two registrations. |
| 161 ServiceWorkerDatabase::RegistrationData data4; |
| 162 data4.registration_id = 456; |
| 163 data4.scope = GURL("https://example.org/fuga"); |
| 164 data4.script = GURL("https://example.org/script4.js"); |
| 165 data4.version_id = 789; |
| 166 ASSERT_EQ(origin3, data4.scope.GetOrigin()); |
| 167 ASSERT_TRUE(database->WriteRegistration(data4, resources)); |
| 168 |
| 169 origins.clear(); |
| 170 EXPECT_TRUE(database->GetOriginsWithRegistrations(&origins)); |
| 171 EXPECT_EQ(3U, origins.size()); |
| 172 EXPECT_TRUE(ContainsKey(origins, origin1)); |
| 173 EXPECT_TRUE(ContainsKey(origins, origin2)); |
| 174 EXPECT_TRUE(ContainsKey(origins, origin3)); |
| 175 |
| 176 // |origin3| has another registration, so should not remove it from the |
| 177 // unique origin list. |
| 178 ASSERT_TRUE(database->DeleteRegistration(data4.registration_id, origin3)); |
| 179 |
| 180 origins.clear(); |
| 181 EXPECT_TRUE(database->GetOriginsWithRegistrations(&origins)); |
| 182 EXPECT_EQ(3U, origins.size()); |
| 183 EXPECT_TRUE(ContainsKey(origins, origin1)); |
| 184 EXPECT_TRUE(ContainsKey(origins, origin2)); |
| 185 EXPECT_TRUE(ContainsKey(origins, origin3)); |
| 186 |
| 187 // |origin3| should be removed from the unique origin list. |
| 188 ASSERT_TRUE(database->DeleteRegistration(data3.registration_id, origin3)); |
| 189 |
| 190 origins.clear(); |
| 191 EXPECT_TRUE(database->GetOriginsWithRegistrations(&origins)); |
| 192 EXPECT_EQ(2U, origins.size()); |
| 193 EXPECT_TRUE(ContainsKey(origins, origin1)); |
| 194 EXPECT_TRUE(ContainsKey(origins, origin2)); |
| 195 } |
| 196 |
| 197 TEST(ServiceWorkerDatabaseTest, GetRegistrationsForOrigin) { |
| 198 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 199 |
| 200 GURL origin("https://example.org"); |
| 201 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; |
| 202 EXPECT_FALSE(database->GetRegistrationsForOrigin(origin, ®istrations)); |
| 203 |
| 204 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 205 |
| 206 ServiceWorkerDatabase::RegistrationData data1; |
| 207 data1.registration_id = 100; |
| 208 data1.scope = GURL("http://example.com/foo"); |
| 209 data1.script = GURL("http://example.com/script1.js"); |
| 210 data1.version_id = 1000; |
| 211 ASSERT_TRUE(database->WriteRegistration(data1, resources)); |
| 212 |
| 213 ServiceWorkerDatabase::RegistrationData data2; |
| 214 data2.registration_id = 200; |
| 215 data2.scope = GURL("https://www.example.com/bar"); |
| 216 data2.script = GURL("https://www.example.com/script2.js"); |
| 217 data2.version_id = 2000; |
| 218 ASSERT_TRUE(database->WriteRegistration(data2, resources)); |
| 219 |
| 220 ServiceWorkerDatabase::RegistrationData data3; |
| 221 data3.registration_id = 300; |
| 222 data3.scope = GURL("https://example.org/hoge"); |
| 223 data3.script = GURL("https://example.org/script3.js"); |
| 224 data3.version_id = 3000; |
| 225 ASSERT_TRUE(database->WriteRegistration(data3, resources)); |
| 226 |
| 227 // Same origin with |data3|. |
| 228 ServiceWorkerDatabase::RegistrationData data4; |
| 229 data4.registration_id = 400; |
| 230 data4.scope = GURL("https://example.org/fuga"); |
| 231 data4.script = GURL("https://example.org/script4.js"); |
| 232 data4.version_id = 4000; |
| 233 ASSERT_TRUE(database->WriteRegistration(data4, resources)); |
| 234 |
| 235 EXPECT_TRUE(database->GetRegistrationsForOrigin(origin, ®istrations)); |
| 236 EXPECT_EQ(2U, registrations.size()); |
| 237 VerifyRegistrationData(data3, registrations[0]); |
| 238 VerifyRegistrationData(data4, registrations[1]); |
| 239 } |
| 240 |
| 241 TEST(ServiceWorkerDatabaseTest, Registration) { |
| 242 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 243 |
| 244 ServiceWorkerDatabase::RegistrationData data; |
| 245 data.registration_id = 100; |
| 246 data.scope = GURL("http://example.com/foo"); |
| 247 data.script = GURL("http://example.com/script.js"); |
| 248 data.version_id = 200; |
| 249 GURL origin = data.scope.GetOrigin(); |
| 250 |
| 251 // TODO(nhiroki): Test ResourceRecord manipulation. |
| 252 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 253 |
| 254 EXPECT_TRUE(database->WriteRegistration(data, resources)); |
| 255 |
| 256 ServiceWorkerDatabase::RegistrationData data_out; |
| 257 std::vector<ServiceWorkerDatabase::ResourceRecord> resources_out; |
| 258 EXPECT_TRUE(database->ReadRegistration( |
| 259 data.registration_id, origin, &data_out, &resources_out)); |
| 260 VerifyRegistrationData(data, data_out); |
| 261 |
| 262 EXPECT_TRUE(database->DeleteRegistration(data.registration_id, |
| 263 data.scope.GetOrigin())); |
| 264 |
| 265 EXPECT_FALSE(database->ReadRegistration( |
| 266 data.registration_id, origin, &data_out, &resources_out)); |
77 } | 267 } |
78 | 268 |
79 } // namespace content | 269 } // namespace content |
OLD | NEW |