Chromium Code Reviews| 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 // Same origin with |data3|. | |
| 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 GURL origin4 = data4.script.GetOrigin(); | |
| 167 ASSERT_EQ(origin3, origin4); | |
| 168 ASSERT_TRUE(database->WriteRegistration(data4, resources)); | |
| 169 | |
| 170 origins.clear(); | |
| 171 EXPECT_TRUE(database->GetOriginsWithRegistrations(&origins)); | |
| 172 EXPECT_EQ(3U, origins.size()); | |
| 173 EXPECT_TRUE(ContainsKey(origins, origin1)); | |
| 174 EXPECT_TRUE(ContainsKey(origins, origin2)); | |
| 175 EXPECT_TRUE(ContainsKey(origins, origin3)); | |
| 176 | |
| 177 // |origin4| has another registration, so should not remove it from the | |
|
michaeln
2014/04/26 00:29:02
nit: this confused me i thought origin4 would be a
nhiroki
2014/04/28 04:15:59
Done.
| |
| 178 // unique origin list. | |
| 179 ASSERT_TRUE(database->DeleteRegistration(data4.registration_id, origin4)); | |
| 180 | |
| 181 origins.clear(); | |
| 182 EXPECT_TRUE(database->GetOriginsWithRegistrations(&origins)); | |
| 183 EXPECT_EQ(3U, origins.size()); | |
| 184 EXPECT_TRUE(ContainsKey(origins, origin1)); | |
| 185 EXPECT_TRUE(ContainsKey(origins, origin2)); | |
| 186 EXPECT_TRUE(ContainsKey(origins, origin3)); | |
| 187 | |
| 188 // |origin3| should be removed from the unique origin list. | |
| 189 ASSERT_TRUE(database->DeleteRegistration(data3.registration_id, origin3)); | |
| 190 | |
| 191 origins.clear(); | |
| 192 EXPECT_TRUE(database->GetOriginsWithRegistrations(&origins)); | |
| 193 EXPECT_EQ(2U, origins.size()); | |
| 194 EXPECT_TRUE(ContainsKey(origins, origin1)); | |
| 195 EXPECT_TRUE(ContainsKey(origins, origin2)); | |
| 196 } | |
| 197 | |
| 198 TEST(ServiceWorkerDatabaseTest, GetRegistrationsForOrigin) { | |
| 199 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | |
| 200 | |
| 201 GURL origin("https://example.org"); | |
| 202 std::vector<ServiceWorkerDatabase::RegistrationData> registrations; | |
| 203 EXPECT_FALSE(database->GetRegistrationsForOrigin(origin, ®istrations)); | |
| 204 | |
| 205 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | |
| 206 | |
| 207 ServiceWorkerDatabase::RegistrationData data1; | |
| 208 data1.registration_id = 100; | |
| 209 data1.scope = GURL("http://example.com/foo"); | |
| 210 data1.script = GURL("http://example.com/script1.js"); | |
| 211 data1.version_id = 1000; | |
| 212 ASSERT_TRUE(database->WriteRegistration(data1, resources)); | |
| 213 | |
| 214 ServiceWorkerDatabase::RegistrationData data2; | |
| 215 data2.registration_id = 200; | |
| 216 data2.scope = GURL("https://www.example.com/bar"); | |
| 217 data2.script = GURL("https://www.example.com/script2.js"); | |
| 218 data2.version_id = 2000; | |
| 219 ASSERT_TRUE(database->WriteRegistration(data2, resources)); | |
| 220 | |
| 221 ServiceWorkerDatabase::RegistrationData data3; | |
| 222 data3.registration_id = 300; | |
| 223 data3.scope = GURL("https://example.org/hoge"); | |
| 224 data3.script = GURL("https://example.org/script3.js"); | |
| 225 data3.version_id = 3000; | |
| 226 ASSERT_TRUE(database->WriteRegistration(data3, resources)); | |
| 227 | |
| 228 // Same origin with |data3|. | |
| 229 ServiceWorkerDatabase::RegistrationData data4; | |
| 230 data4.registration_id = 400; | |
| 231 data4.scope = GURL("https://example.org/fuga"); | |
| 232 data4.script = GURL("https://example.org/script4.js"); | |
| 233 data4.version_id = 4000; | |
| 234 ASSERT_TRUE(database->WriteRegistration(data4, resources)); | |
| 235 | |
| 236 EXPECT_TRUE(database->GetRegistrationsForOrigin(origin, ®istrations)); | |
| 237 EXPECT_EQ(2U, registrations.size()); | |
| 238 VerifyRegistrationData(data3, registrations[0]); | |
| 239 VerifyRegistrationData(data4, registrations[1]); | |
| 240 } | |
| 241 | |
| 242 TEST(ServiceWorkerDatabaseTest, Registration) { | |
| 243 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | |
| 244 | |
| 245 ServiceWorkerDatabase::RegistrationData data; | |
| 246 data.registration_id = 100; | |
| 247 data.scope = GURL("http://example.com/foo"); | |
| 248 data.script = GURL("http://example.com/script.js"); | |
| 249 data.version_id = 200; | |
| 250 GURL origin = data.scope.GetOrigin(); | |
| 251 | |
| 252 // TODO(nhiroki): Test ResourceRecord manipulation. | |
| 253 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | |
| 254 | |
| 255 EXPECT_TRUE(database->WriteRegistration(data, resources)); | |
| 256 | |
| 257 ServiceWorkerDatabase::RegistrationData data_out; | |
| 258 std::vector<ServiceWorkerDatabase::ResourceRecord> resources_out; | |
| 259 EXPECT_TRUE(database->ReadRegistration( | |
| 260 data.registration_id, origin, &data_out, &resources_out)); | |
| 261 VerifyRegistrationData(data, data_out); | |
| 262 | |
| 263 EXPECT_TRUE(database->DeleteRegistration(data.registration_id, | |
| 264 data.scope.GetOrigin())); | |
| 265 | |
| 266 EXPECT_FALSE(database->ReadRegistration( | |
| 267 data.registration_id, origin, &data_out, &resources_out)); | |
| 77 } | 268 } |
| 78 | 269 |
| 79 } // namespace content | 270 } // namespace content |
| OLD | NEW |