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

Side by Side Diff: components/history/core/browser/thumbnail_database_unittest.cc

Issue 2903573002: [Thumbnails DB] Add functionality to clear unused on-demand favicons. (Closed)
Patch Set: Compile error fixes #2 Created 3 years, 4 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 (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 #include <stddef.h> 5 #include <stddef.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/ref_counted_memory.h" 13 #include "base/memory/ref_counted_memory.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/strings/stringprintf.h"
16 #include "build/build_config.h"
15 #include "components/history/core/browser/thumbnail_database.h" 17 #include "components/history/core/browser/thumbnail_database.h"
16 #include "components/history/core/test/database_test_utils.h" 18 #include "components/history/core/test/database_test_utils.h"
17 #include "sql/connection.h" 19 #include "sql/connection.h"
18 #include "sql/recovery.h" 20 #include "sql/recovery.h"
19 #include "sql/test/scoped_error_expecter.h" 21 #include "sql/test/scoped_error_expecter.h"
20 #include "sql/test/test_helpers.h" 22 #include "sql/test/test_helpers.h"
23 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/sqlite/sqlite3.h" 25 #include "third_party/sqlite/sqlite3.h"
23 #include "url/gurl.h" 26 #include "url/gurl.h"
24 27
28 using testing::AllOf;
29 using testing::ElementsAre;
30 using testing::Field;
31 using testing::Pair;
32 using testing::Return;
33
25 namespace history { 34 namespace history {
26 35
27 namespace { 36 namespace {
28 37
29 // Blobs for the bitmap tests. These aren't real bitmaps. Golden 38 // Blobs for the bitmap tests. These aren't real bitmaps. Golden
30 // database files store the same blobs (see VersionN tests). 39 // database files store the same blobs (see VersionN tests).
31 const unsigned char kBlob1[] = 40 const unsigned char kBlob1[] =
32 "12346102356120394751634516591348710478123649165419234519234512349134"; 41 "12346102356120394751634516591348710478123649165419234519234512349134";
33 const unsigned char kBlob2[] = 42 const unsigned char kBlob2[] =
34 "goiwuegrqrcomizqyzkjalitbahxfjytrqvpqeroicxmnlkhlzunacxaneviawrtxcywhgef"; 43 "goiwuegrqrcomizqyzkjalitbahxfjytrqvpqeroicxmnlkhlzunacxaneviawrtxcywhgef";
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 db.TouchOnDemandFavicon(url, end); 369 db.TouchOnDemandFavicon(url, end);
361 370
362 base::Time last_updated; 371 base::Time last_updated;
363 base::Time last_requested; 372 base::Time last_requested;
364 EXPECT_TRUE(db.GetFaviconBitmap(bitmap, &last_updated, &last_requested, 373 EXPECT_TRUE(db.GetFaviconBitmap(bitmap, &last_updated, &last_requested,
365 nullptr, nullptr)); 374 nullptr, nullptr));
366 EXPECT_EQ(start, last_updated); // Does not mess with last_updated. 375 EXPECT_EQ(start, last_updated); // Does not mess with last_updated.
367 EXPECT_EQ(base::Time(), last_requested); // No update. 376 EXPECT_EQ(base::Time(), last_requested); // No update.
368 } 377 }
369 378
379 // Test that ThumbnailDatabase::GetOldOnDemandFavicons() returns on-demand icons
380 // which were requested prior to the passed in timestamp.
381 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsReturnsOld) {
382 ThumbnailDatabase db(nullptr);
383 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
384 db.BeginTransaction();
385
386 base::Time start;
387 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
388 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
389 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
390
391 GURL url("http://google.com/favicon.ico");
392 favicon_base::FaviconID icon =
393 db.AddFavicon(url, favicon_base::FAVICON, favicon,
394 FaviconBitmapType::ON_DEMAND, start, gfx::Size());
395 ASSERT_NE(0, icon);
396 // Associate two different URLs with the icon.
397 GURL page_url1("http://google.com/1");
398 ASSERT_NE(0, db.AddIconMapping(page_url1, icon));
399 GURL page_url2("http://google.com/2");
400 ASSERT_NE(0, db.AddIconMapping(page_url2, icon));
401
402 base::Time get_older_than = start + base::TimeDelta::FromSeconds(1);
403 auto map = db.GetOldOnDemandFavicons(get_older_than);
404
405 // The icon is returned.
406 EXPECT_THAT(map, ElementsAre(Pair(
407 icon, AllOf(Field(&IconMappingsForExpiry::icon_url, url),
408 Field(&IconMappingsForExpiry::page_urls,
409 ElementsAre(page_url1, page_url2))))));
410 }
411
412 // Test that ThumbnailDatabase::GetOldOnDemandFavicons() returns on-visit icons
413 // if the on-visit icons have expired. We need this behavior in order to delete
414 // icons stored via HistoryService::SetOnDemandFavicons() prior to on-demand
415 // icons setting the "last_requested" time.
416 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsReturnsExpired) {
417 ThumbnailDatabase db(nullptr);
418 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
419 db.BeginTransaction();
420
421 base::Time start;
422 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
423 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
424 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
425
426 GURL url("http://google.com/favicon.ico");
427 favicon_base::FaviconID icon =
428 db.AddFavicon(url, favicon_base::FAVICON, favicon,
429 FaviconBitmapType::ON_VISIT, start, gfx::Size());
430 ASSERT_NE(0, icon);
431 GURL page_url("http://google.com/");
432 ASSERT_NE(0, db.AddIconMapping(page_url, icon));
433 ASSERT_TRUE(db.SetFaviconOutOfDate(icon));
434
435 // The threshold is ignored for expired icons.
436 auto map = db.GetOldOnDemandFavicons(/*threshold=*/base::Time::Now());
437
438 // The icon is returned.
439 EXPECT_THAT(map, ElementsAre(Pair(
440 icon, AllOf(Field(&IconMappingsForExpiry::icon_url, url),
441 Field(&IconMappingsForExpiry::page_urls,
442 ElementsAre(page_url))))));
443 }
444
445 // Test that ThumbnailDatabase::GetOldOnDemandFavicons() does not return
446 // on-demand icons which were requested after the passed in timestamp.
447 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsDoesNotReturnFresh) {
448 ThumbnailDatabase db(nullptr);
449 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
450 db.BeginTransaction();
451
452 base::Time start;
453 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
454 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
455 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
456
457 GURL url("http://google.com/favicon.ico");
458 favicon_base::FaviconID icon =
459 db.AddFavicon(url, favicon_base::FAVICON, favicon,
460 FaviconBitmapType::ON_DEMAND, start, gfx::Size());
461 ASSERT_NE(0, icon);
462 ASSERT_NE(0, db.AddIconMapping(GURL("http://google.com/"), icon));
463
464 // Touch the icon 3 weeks later.
465 base::Time now = start + base::TimeDelta::FromDays(21);
466 EXPECT_TRUE(db.TouchOnDemandFavicon(url, now));
467
468 base::Time get_older_than = start + base::TimeDelta::FromSeconds(1);
469 auto map = db.GetOldOnDemandFavicons(get_older_than);
470
471 // No icon is returned.
472 EXPECT_TRUE(map.empty());
473 }
474
475 // Test that ThumbnailDatabase::GetOldOnDemandFavicons() does not return
476 // non-expired on-visit icons.
477 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsDoesNotDeleteStandard) {
478 ThumbnailDatabase db(nullptr);
479 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
480 db.BeginTransaction();
481
482 base::Time start;
483 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
484 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
485 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
486
487 favicon_base::FaviconID icon = db.AddFavicon(
488 GURL("http://google.com/favicon.ico"), favicon_base::FAVICON, favicon,
489 FaviconBitmapType::ON_VISIT, start, gfx::Size());
490 ASSERT_NE(0, icon);
491 ASSERT_NE(0, db.AddIconMapping(GURL("http://google.com/"), icon));
492
493 base::Time get_older_than = start + base::TimeDelta::FromSeconds(1);
494 auto map = db.GetOldOnDemandFavicons(get_older_than);
495
496 // No icon is returned.
497 EXPECT_TRUE(map.empty());
498 }
499
370 TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) { 500 TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) {
371 ThumbnailDatabase db(NULL); 501 ThumbnailDatabase db(NULL);
372 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); 502 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
373 db.BeginTransaction(); 503 db.BeginTransaction();
374 504
375 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); 505 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
376 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 506 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
377 507
378 GURL url("http://google.com"); 508 GURL url("http://google.com");
379 favicon_base::FaviconID id = db.AddFavicon(url, favicon_base::TOUCH_ICON); 509 favicon_base::FaviconID id = db.AddFavicon(url, favicon_base::TOUCH_ICON);
(...skipping 801 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 ThumbnailDatabase db(NULL); 1311 ThumbnailDatabase db(NULL);
1182 ASSERT_EQ(sql::INIT_OK, db.Init(db_path)); 1312 ASSERT_EQ(sql::INIT_OK, db.Init(db_path));
1183 1313
1184 // Verify that the resulting schema is correct, whether it 1314 // Verify that the resulting schema is correct, whether it
1185 // involved razing the file or fixing things in place. 1315 // involved razing the file or fixing things in place.
1186 VerifyTablesAndColumns(&db.db_); 1316 VerifyTablesAndColumns(&db.db_);
1187 } 1317 }
1188 } 1318 }
1189 1319
1190 } // namespace history 1320 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698