OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // History unit tests come in two flavors: | 5 // History unit tests come in two flavors: |
6 // | 6 // |
7 // 1. The more complicated style is that the unit test creates a full history | 7 // 1. The more complicated style is that the unit test creates a full history |
8 // service. This spawns a background thread for the history backend, and | 8 // service. This spawns a background thread for the history backend, and |
9 // all communication is asynchronous. This is useful for testing more | 9 // all communication is asynchronous. This is useful for testing more |
10 // complicated things or end-to-end behavior. | 10 // complicated things or end-to-end behavior. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 virtual void TearDown() { | 130 virtual void TearDown() { |
131 DeleteBackend(); | 131 DeleteBackend(); |
132 | 132 |
133 // Make sure we don't have any event pending that could disrupt the next | 133 // Make sure we don't have any event pending that could disrupt the next |
134 // test. | 134 // test. |
135 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 135 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
136 MessageLoop::current()->Run(); | 136 MessageLoop::current()->Run(); |
137 } | 137 } |
138 | 138 |
139 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) { | 139 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) { |
| 140 std::vector<GURL> url_chain; |
| 141 url_chain.push_back(GURL("foo-url")); |
| 142 |
140 DownloadPersistentStoreInfo download( | 143 DownloadPersistentStoreInfo download( |
141 FilePath(FILE_PATH_LITERAL("foo-path")), | 144 FilePath(FILE_PATH_LITERAL("foo-path")), |
142 GURL("foo-url"), | 145 FilePath(FILE_PATH_LITERAL("foo-path")), |
| 146 url_chain, |
143 GURL(""), | 147 GURL(""), |
144 time, | 148 time, |
145 time, | 149 time, |
146 0, | 150 0, |
147 512, | 151 512, |
148 state, | 152 state, |
| 153 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
149 0, | 154 0, |
150 0); | 155 0); |
151 return db_->CreateDownload(download); | 156 return db_->CreateDownload(download); |
152 } | 157 } |
153 | 158 |
154 ScopedTempDir temp_dir_; | 159 ScopedTempDir temp_dir_; |
155 | 160 |
156 MessageLoopForUI message_loop_; | 161 MessageLoopForUI message_loop_; |
157 | 162 |
158 // names of the database files | 163 // names of the database files |
(...skipping 19 matching lines...) Expand all Loading... |
178 content::Details<HistoryDetails> det(details); | 183 content::Details<HistoryDetails> det(details); |
179 history_test_->in_mem_backend_->Observe(type, | 184 history_test_->in_mem_backend_->Observe(type, |
180 content::Source<HistoryBackendDBTest>(NULL), det); | 185 content::Source<HistoryBackendDBTest>(NULL), det); |
181 | 186 |
182 // The backend passes ownership of the details pointer to us. | 187 // The backend passes ownership of the details pointer to us. |
183 delete details; | 188 delete details; |
184 } | 189 } |
185 | 190 |
186 namespace { | 191 namespace { |
187 | 192 |
| 193 // Schema for the downloads database for verion 23 and earlier. |
| 194 const char* kVersion23DownloadsSchema = |
| 195 "CREATE TABLE downloads (" |
| 196 "id INTEGER PRIMARY KEY," |
| 197 "full_path LONGVARCHAR NOT NULL," |
| 198 "url LONGVARCHAR NOT NULL," |
| 199 "start_time INTEGER NOT NULL," |
| 200 "received_bytes INTEGER NOT NULL," |
| 201 "total_bytes INTEGER NOT NULL," |
| 202 "state INTEGER NOT NULL," |
| 203 "end_time INTEGER NOT NULL," |
| 204 "opened INTEGER NOT NULL)"; |
| 205 |
188 TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) { | 206 TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) { |
189 CreateBackendAndDatabase(); | 207 CreateBackendAndDatabase(); |
190 | 208 |
191 Time now = Time::Now(); | 209 Time now = Time::Now(); |
192 TimeDelta one_day = TimeDelta::FromDays(1); | 210 TimeDelta one_day = TimeDelta::FromDays(1); |
193 Time month_ago = now - TimeDelta::FromDays(30); | 211 Time month_ago = now - TimeDelta::FromDays(30); |
194 | 212 |
195 // Initially there should be nothing in the downloads database. | 213 // Initially there should be nothing in the downloads database. |
196 std::vector<DownloadPersistentStoreInfo> downloads; | 214 std::vector<DownloadPersistentStoreInfo> downloads; |
197 db_->QueryDownloads(&downloads); | 215 db_->QueryDownloads(&downloads); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 { | 297 { |
280 // Re-open the db for manual manipulation. | 298 // Re-open the db for manual manipulation. |
281 sql::Connection db; | 299 sql::Connection db; |
282 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); | 300 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
283 { | 301 { |
284 // Manually force the version to 22. | 302 // Manually force the version to 22. |
285 sql::Statement version22(db.GetUniqueStatement( | 303 sql::Statement version22(db.GetUniqueStatement( |
286 "UPDATE meta SET value=22 WHERE key='version'")); | 304 "UPDATE meta SET value=22 WHERE key='version'")); |
287 ASSERT_TRUE(version22.Run()); | 305 ASSERT_TRUE(version22.Run()); |
288 } | 306 } |
| 307 // Nuke the new tables and create an old one. |
| 308 ASSERT_TRUE(db.Execute("DROP TABLE downloads")); |
| 309 ASSERT_TRUE(db.Execute("DROP TABLE downloads_url_chains")); |
| 310 ASSERT_TRUE(db.Execute(kVersion23DownloadsSchema)); |
| 311 |
289 // Manually insert corrupted rows; there's infrastructure in place now to | 312 // Manually insert corrupted rows; there's infrastructure in place now to |
290 // make this impossible, at least according to the test above. | 313 // make this impossible, at least according to the test above. |
291 for (int state = 0; state < 5; ++state) { | 314 for (int state = 0; state < 5; ++state) { |
292 sql::Statement s(db.GetUniqueStatement( | 315 sql::Statement s(db.GetUniqueStatement( |
293 "INSERT INTO downloads (id, full_path, url, start_time, " | 316 "INSERT INTO downloads (id, full_path, url, start_time, " |
294 "received_bytes, total_bytes, state, end_time, opened) VALUES " | 317 "received_bytes, total_bytes, state, end_time, opened) VALUES " |
295 "(?, ?, ?, ?, ?, ?, ?, ?, ?)")); | 318 "(?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
296 s.BindInt64(0, 1 + state); | 319 s.BindInt64(0, 1 + state); |
297 s.BindString(1, "path"); | 320 s.BindString(1, "path"); |
298 s.BindString(2, "url"); | 321 s.BindString(2, "url"); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 // 4. | 359 // 4. |
337 EXPECT_EQ(((counter == 3) ? 4 : counter), statement.ColumnInt(1)); | 360 EXPECT_EQ(((counter == 3) ? 4 : counter), statement.ColumnInt(1)); |
338 EXPECT_EQ(counter % 2, statement.ColumnInt(2)); | 361 EXPECT_EQ(counter % 2, statement.ColumnInt(2)); |
339 ++counter; | 362 ++counter; |
340 } | 363 } |
341 EXPECT_EQ(5, counter); | 364 EXPECT_EQ(5, counter); |
342 } | 365 } |
343 } | 366 } |
344 } | 367 } |
345 | 368 |
| 369 TEST_F(HistoryBackendDBTest, MigrateReasonAndPaths) { |
| 370 // Create the db and close it so that we can reopen it directly. |
| 371 CreateBackendAndDatabase(); |
| 372 DeleteBackend(); |
| 373 { |
| 374 // Re-open the db for manual manipulation. |
| 375 sql::Connection db; |
| 376 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
| 377 { |
| 378 // Manually force the version to 23. |
| 379 sql::Statement version23(db.GetUniqueStatement( |
| 380 "UPDATE meta SET value=23 WHERE key='version'")); |
| 381 ASSERT_TRUE(version23.Run()); |
| 382 } |
| 383 |
| 384 // Nuke the new tables and create an old one with some hand crafted |
| 385 // values in it. |
| 386 ASSERT_TRUE(db.Execute("DROP TABLE downloads")); |
| 387 ASSERT_TRUE(db.Execute("DROP TABLE downloads_url_chains")); |
| 388 ASSERT_TRUE(db.Execute(kVersion23DownloadsSchema)); |
| 389 |
| 390 // Manually insert some rows. |
| 391 sql::Statement s(db.GetUniqueStatement( |
| 392 "INSERT INTO downloads (id, full_path, url, start_time, " |
| 393 "received_bytes, total_bytes, state, end_time, opened) VALUES " |
| 394 "(?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
| 395 |
| 396 int64 db_handle = 0; |
| 397 // Null path. |
| 398 s.BindInt64(0, ++db_handle); |
| 399 s.BindString(1, ""); |
| 400 s.BindString(2, "http://whatever.com/index.html"); |
| 401 s.BindInt64(3, base::Time::Now().ToTimeT()); |
| 402 s.BindInt64(4, 100); |
| 403 s.BindInt64(5, 100); |
| 404 s.BindInt(6, 1); |
| 405 s.BindInt64(7, base::Time::Now().ToTimeT()); |
| 406 s.BindInt(8, 1); |
| 407 ASSERT_TRUE(s.Run()); |
| 408 s.Reset(true); |
| 409 |
| 410 // Non-null path. |
| 411 s.BindInt64(0, ++db_handle); |
| 412 s.BindString(1, "/path/to/some/file"); |
| 413 s.BindString(2, "http://whatever.com/index1.html"); |
| 414 s.BindInt64(3, base::Time::Now().ToTimeT()); |
| 415 s.BindInt64(4, 100); |
| 416 s.BindInt64(5, 100); |
| 417 s.BindInt(6, 1); |
| 418 s.BindInt64(7, base::Time::Now().ToTimeT()); |
| 419 s.BindInt(8, 1); |
| 420 ASSERT_TRUE(s.Run()); |
| 421 } |
| 422 |
| 423 // Re-open the db using the HistoryDatabase, which should migrate from version |
| 424 // 23 to 24, creating the new tables and creating the new path and reason |
| 425 // columns. |
| 426 CreateBackendAndDatabase(); |
| 427 DeleteBackend(); |
| 428 { |
| 429 // Re-open the db for manual manipulation. |
| 430 sql::Connection db; |
| 431 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
| 432 { |
| 433 // The version should have been updated. |
| 434 int cur_version = HistoryDatabase::GetCurrentVersion(); |
| 435 ASSERT_LT(23, cur_version); |
| 436 sql::Statement s(db.GetUniqueStatement( |
| 437 "SELECT value FROM meta WHERE key = 'version'")); |
| 438 EXPECT_TRUE(s.Step()); |
| 439 EXPECT_EQ(cur_version, s.ColumnInt(0)); |
| 440 } |
| 441 { |
| 442 // Confirm downloads table is valid. |
| 443 sql::Statement statement(db.GetUniqueStatement( |
| 444 "SELECT id, interrupt_reason, target_path, current_path " |
| 445 "FROM downloads ORDER BY id")); |
| 446 EXPECT_TRUE(statement.Step()); |
| 447 EXPECT_EQ(1, statement.ColumnInt64(0)); |
| 448 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 449 statement.ColumnInt(1)); |
| 450 EXPECT_EQ("", statement.ColumnString(2)); |
| 451 EXPECT_EQ("", statement.ColumnString(3)); |
| 452 |
| 453 EXPECT_TRUE(statement.Step()); |
| 454 EXPECT_EQ(2, statement.ColumnInt64(0)); |
| 455 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 456 statement.ColumnInt(1)); |
| 457 EXPECT_EQ("/path/to/some/file", statement.ColumnString(2)); |
| 458 EXPECT_EQ("/path/to/some/file", statement.ColumnString(3)); |
| 459 |
| 460 EXPECT_FALSE(statement.Step()); |
| 461 } |
| 462 { |
| 463 // Confirm donwloads_url_chains table is valid. |
| 464 sql::Statement statement(db.GetUniqueStatement( |
| 465 "SELECT id, chain_index, url FROM downloads_url_chains " |
| 466 " ORDER BY id, chain_index")); |
| 467 EXPECT_TRUE(statement.Step()); |
| 468 EXPECT_EQ(1, statement.ColumnInt64(0)); |
| 469 EXPECT_EQ(0, statement.ColumnInt(1)); |
| 470 EXPECT_EQ("http://whatever.com/index.html", statement.ColumnString(2)); |
| 471 |
| 472 EXPECT_TRUE(statement.Step()); |
| 473 EXPECT_EQ(2, statement.ColumnInt64(0)); |
| 474 EXPECT_EQ(0, statement.ColumnInt(1)); |
| 475 EXPECT_EQ("http://whatever.com/index1.html", statement.ColumnString(2)); |
| 476 |
| 477 EXPECT_FALSE(statement.Step()); |
| 478 } |
| 479 } |
| 480 } |
| 481 |
346 // The tracker uses RenderProcessHost pointers for scoping but never | 482 // The tracker uses RenderProcessHost pointers for scoping but never |
347 // dereferences them. We use ints because it's easier. This function converts | 483 // dereferences them. We use ints because it's easier. This function converts |
348 // between the two. | 484 // between the two. |
349 static void* MakeFakeHost(int id) { | 485 static void* MakeFakeHost(int id) { |
350 void* host = 0; | 486 void* host = 0; |
351 memcpy(&host, &id, sizeof(id)); | 487 memcpy(&host, &id, sizeof(id)); |
352 return host; | 488 return host; |
353 } | 489 } |
354 | 490 |
355 class HistoryTest : public testing::Test { | 491 class HistoryTest : public testing::Test { |
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1032 request_consumer.CancelAllRequests(); | 1168 request_consumer.CancelAllRequests(); |
1033 CleanupHistoryService(); | 1169 CleanupHistoryService(); |
1034 // WARNING: history has now been deleted. | 1170 // WARNING: history has now been deleted. |
1035 history_service_.reset(); | 1171 history_service_.reset(); |
1036 ASSERT_FALSE(task->done_invoked); | 1172 ASSERT_FALSE(task->done_invoked); |
1037 } | 1173 } |
1038 | 1174 |
1039 } // namespace | 1175 } // namespace |
1040 | 1176 |
1041 } // namespace history | 1177 } // namespace history |
OLD | NEW |