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

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: Peter's comments #3 Created 3 years, 5 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"
jkrcal 2017/07/06 17:01:48 "build/build_config.h" has been enforced by a pres
17 #include "components/history/core/browser/history_backend_client.h"
15 #include "components/history/core/browser/thumbnail_database.h" 18 #include "components/history/core/browser/thumbnail_database.h"
16 #include "components/history/core/test/database_test_utils.h" 19 #include "components/history/core/test/database_test_utils.h"
17 #include "sql/connection.h" 20 #include "sql/connection.h"
18 #include "sql/recovery.h" 21 #include "sql/recovery.h"
19 #include "sql/test/scoped_error_expecter.h" 22 #include "sql/test/scoped_error_expecter.h"
20 #include "sql/test/test_helpers.h" 23 #include "sql/test/test_helpers.h"
24 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/sqlite/sqlite3.h" 26 #include "third_party/sqlite/sqlite3.h"
23 #include "url/gurl.h" 27 #include "url/gurl.h"
24 28
29 using testing::StrictMock;
30 using testing::Return;
31
25 namespace history { 32 namespace history {
26 33
27 namespace { 34 namespace {
28 35
29 // Blobs for the bitmap tests. These aren't real bitmaps. Golden 36 // Blobs for the bitmap tests. These aren't real bitmaps. Golden
30 // database files store the same blobs (see VersionN tests). 37 // database files store the same blobs (see VersionN tests).
31 const unsigned char kBlob1[] = 38 const unsigned char kBlob1[] =
32 "12346102356120394751634516591348710478123649165419234519234512349134"; 39 "12346102356120394751634516591348710478123649165419234519234512349134";
33 const unsigned char kBlob2[] = 40 const unsigned char kBlob2[] =
34 "goiwuegrqrcomizqyzkjalitbahxfjytrqvpqeroicxmnlkhlzunacxaneviawrtxcywhgef"; 41 "goiwuegrqrcomizqyzkjalitbahxfjytrqvpqeroicxmnlkhlzunacxaneviawrtxcywhgef";
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 } 169 }
163 170
164 bool CompareIconMappingIconUrl(const IconMapping& a, const IconMapping& b) { 171 bool CompareIconMappingIconUrl(const IconMapping& a, const IconMapping& b) {
165 return a.icon_url < b.icon_url; 172 return a.icon_url < b.icon_url;
166 } 173 }
167 174
168 void SortMappingsByIconUrl(std::vector<IconMapping>* mappings) { 175 void SortMappingsByIconUrl(std::vector<IconMapping>* mappings) {
169 std::sort(mappings->begin(), mappings->end(), &CompareIconMappingIconUrl); 176 std::sort(mappings->begin(), mappings->end(), &CompareIconMappingIconUrl);
170 } 177 }
171 178
179 class MockHistoryBackendClient : public HistoryBackendClient {
180 public:
181 // MOCK_METHOD0(~HistoryBackendClient, void());
182 MOCK_METHOD1(IsBookmarked, bool(const GURL& url));
183 MOCK_METHOD1(GetBookmarks, void(std::vector<URLAndTitle>* bookmarks));
184 MOCK_METHOD0(ShouldReportDatabaseError, bool());
185 MOCK_METHOD1(IsWebSafe, bool(const GURL& url));
186
187 #if defined(OS_ANDROID)
188 MOCK_METHOD4(OnHistoryBackendInitialized,
189 void(HistoryBackend* history_backend,
190 HistoryDatabase* history_database,
191 ThumbnailDatabase* thumbnail_database,
192 const base::FilePath& history_dir));
193 MOCK_METHOD2(OnHistoryBackendDestroyed,
194 void(HistoryBackend* history_backend,
195 const base::FilePath& history_dir));
196 #endif // defined(OS_ANDROID)
197 };
198
172 } // namespace 199 } // namespace
173 200
174 class ThumbnailDatabaseTest : public testing::Test { 201 class ThumbnailDatabaseTest : public testing::Test {
175 public: 202 public:
176 ThumbnailDatabaseTest() {} 203 ThumbnailDatabaseTest() {}
177 ~ThumbnailDatabaseTest() override {} 204 ~ThumbnailDatabaseTest() override {}
178 205
179 // Initialize a thumbnail database instance from the SQL file at 206 // Initialize a thumbnail database instance from the SQL file at
180 // |golden_path| in the "History/" subdirectory of test data. 207 // |golden_path| in the "History/" subdirectory of test data.
181 std::unique_ptr<ThumbnailDatabase> LoadFromGolden(const char* golden_path) { 208 std::unique_ptr<ThumbnailDatabase> LoadFromGolden(const char* golden_path) {
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 start + base::TimeDelta::FromDays(kFaviconUpdateLastRequestedAfterDays); 386 start + base::TimeDelta::FromDays(kFaviconUpdateLastRequestedAfterDays);
360 db.TouchOnDemandFavicon(url, end); 387 db.TouchOnDemandFavicon(url, end);
361 388
362 base::Time last_updated; 389 base::Time last_updated;
363 base::Time last_requested; 390 base::Time last_requested;
364 EXPECT_TRUE(db.GetFaviconBitmap(bitmap, &last_updated, &last_requested, 391 EXPECT_TRUE(db.GetFaviconBitmap(bitmap, &last_updated, &last_requested,
365 nullptr, nullptr)); 392 nullptr, nullptr));
366 EXPECT_EQ(start, last_updated); // Does not mess with last_updated. 393 EXPECT_EQ(start, last_updated); // Does not mess with last_updated.
367 EXPECT_EQ(base::Time(), last_requested); // No update. 394 EXPECT_EQ(base::Time(), last_requested); // No update.
368 } 395 }
369 396
pkotwicz 2017/07/07 00:42:38 Please add a comment for each test case. For this
jkrcal 2017/07/07 08:45:16 Done.
397 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsReturnsOld) {
398 StrictMock<MockHistoryBackendClient> mock_client;
399 ThumbnailDatabase db(&mock_client);
pkotwicz 2017/07/07 00:42:39 Does the test work if you pass in nullptr?
jkrcal 2017/07/07 08:45:16 Ah, sure. It was there to mock being bookmarked. N
400 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
401 db.BeginTransaction();
402
403 base::Time start;
404 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
405 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
406 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
407
408 // Icon: old unused case.
409 GURL url("http://google.com");
410 favicon_base::FaviconID icon = db.AddFavicon(url, favicon_base::FAVICON);
411 ASSERT_NE(0, icon);
412 FaviconBitmapID bitmap = db.AddFaviconBitmap(
413 icon, favicon, FaviconBitmapType::ON_DEMAND, start, gfx::Size());
pkotwicz 2017/07/07 00:42:39 Nit: Can you use the 6 argument ThumbnailDatabase:
jkrcal 2017/07/07 08:45:16 Done.
414 ASSERT_NE(0, bitmap);
415 ASSERT_NE(0, db.AddIconMapping(url, icon));
416
417 base::Time delete_older_than = start + base::TimeDelta::FromDays(7);
pkotwicz 2017/07/07 00:42:39 Might as well do: "+ base::TimeDelta::FromSeconds
jkrcal 2017/07/07 08:45:17 Done.
418 auto list = db.GetOldOnDemandFavicons(delete_older_than);
419
420 // The icon is returned for deletion.
pkotwicz 2017/07/07 00:42:39 Nit: Don't mention deletion. The fact that the id
jkrcal 2017/07/07 08:45:16 Done.
421 EXPECT_EQ(1u, list.size());
422 EXPECT_EQ(icon, list[0].first);
423 EXPECT_EQ(url, list[0].second);
424 }
425
pkotwicz 2017/07/07 00:42:39 For this test's comment, how about: "Test that Th
jkrcal 2017/07/07 08:45:16 Done.
426 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsReturnsExpired) {
427 StrictMock<MockHistoryBackendClient> mock_client;
428 ThumbnailDatabase db(&mock_client);
429 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
430 db.BeginTransaction();
431
432 base::Time start;
433 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
434 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
435 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
436
437 // Icon: standard favicon (not on-demand) but expired.
438 GURL url("http://google.com");
439 favicon_base::FaviconID icon = db.AddFavicon(url, favicon_base::FAVICON);
440 ASSERT_NE(0, icon);
441 FaviconBitmapID bitmap = db.AddFaviconBitmap(
442 icon, favicon, FaviconBitmapType::ON_VISIT, start, gfx::Size());
443 ASSERT_NE(0, bitmap);
444 ASSERT_NE(0, db.AddIconMapping(url, icon));
445 ASSERT_NE(0, db.SetFaviconOutOfDate(icon));
446
447 // The threshold is ignored for expired icons.
448 auto list = db.GetOldOnDemandFavicons(/*threshold=*/base::Time());
pkotwicz 2017/07/07 00:42:39 Maybe use base::Time::Now() instead?
jkrcal 2017/07/07 08:45:16 Done.
449
450 // The icon is returned for deletion.
451 EXPECT_EQ(1u, list.size());
452 EXPECT_EQ(icon, list[0].first);
453 EXPECT_EQ(url, list[0].second);
454 }
455
456 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsDoesNotReturnFresh) {
pkotwicz 2017/07/07 00:42:39 For this test's comment how about: "Test that Thu
jkrcal 2017/07/07 08:45:16 Done.
457 StrictMock<MockHistoryBackendClient> mock_client;
458 ThumbnailDatabase db(&mock_client);
459 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
460 db.BeginTransaction();
461
462 base::Time start;
463 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
464 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
465 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
466
467 // Icon: freshly used case.
468 GURL url("http://google.com");
469 favicon_base::FaviconID icon = db.AddFavicon(url, favicon_base::FAVICON);
470 ASSERT_NE(0, icon);
471 FaviconBitmapID bitmap = db.AddFaviconBitmap(
472 icon, favicon, FaviconBitmapType::ON_DEMAND, start, gfx::Size());
473 ASSERT_NE(0, bitmap);
474 ASSERT_NE(0, db.AddIconMapping(url, icon));
475
476 // Touch the icon 3 weeks later.
477 base::Time now = start + base::TimeDelta::FromDays(21);
478 EXPECT_TRUE(db.TouchOnDemandFavicon(url, now));
479
480 base::Time delete_older_than = start + base::TimeDelta::FromDays(7);
481 auto list = db.GetOldOnDemandFavicons(delete_older_than);
482
483 // No icon is returned for deletion.
484 EXPECT_EQ(0u, list.size());
pkotwicz 2017/07/07 00:42:39 Nit: EXPECT_TRUE(list.empty());
jkrcal 2017/07/07 08:45:16 Done.
485 }
486
pkotwicz 2017/07/07 00:42:39 For this test's comment how about: "Test that Thu
jkrcal 2017/07/07 08:45:16 Done.
487 TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsDoesNotDeleteStandard) {
488 StrictMock<MockHistoryBackendClient> mock_client;
489 ThumbnailDatabase db(&mock_client);
490 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
491 db.BeginTransaction();
492
493 base::Time start;
494 ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start));
495 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
496 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
497
498 // Icon: standard favicon (not on-demand).
499 GURL url("http://google.com");
500 favicon_base::FaviconID icon = db.AddFavicon(url, favicon_base::FAVICON);
501 ASSERT_NE(0, icon);
502 FaviconBitmapID bitmap = db.AddFaviconBitmap(
503 icon, favicon, FaviconBitmapType::ON_VISIT, start, gfx::Size());
504 ASSERT_NE(0, bitmap);
505 ASSERT_NE(0, db.AddIconMapping(url, icon));
506
507 base::Time delete_older_than = start + base::TimeDelta::FromDays(7);
508 auto list = db.GetOldOnDemandFavicons(delete_older_than);
509
510 // No icon is returned for deletion.
511 EXPECT_EQ(0u, list.size());
pkotwicz 2017/07/07 00:42:39 Nit: EXPECT_TRUE(list.empty());
jkrcal 2017/07/07 08:45:16 Done.
512 }
513
370 TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) { 514 TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) {
371 ThumbnailDatabase db(NULL); 515 ThumbnailDatabase db(NULL);
372 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); 516 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
373 db.BeginTransaction(); 517 db.BeginTransaction();
374 518
375 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); 519 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
376 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 520 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
377 521
378 GURL url("http://google.com"); 522 GURL url("http://google.com");
379 favicon_base::FaviconID id = db.AddFavicon(url, favicon_base::TOUCH_ICON); 523 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); 1325 ThumbnailDatabase db(NULL);
1182 ASSERT_EQ(sql::INIT_OK, db.Init(db_path)); 1326 ASSERT_EQ(sql::INIT_OK, db.Init(db_path));
1183 1327
1184 // Verify that the resulting schema is correct, whether it 1328 // Verify that the resulting schema is correct, whether it
1185 // involved razing the file or fixing things in place. 1329 // involved razing the file or fixing things in place.
1186 VerifyTablesAndColumns(&db.db_); 1330 VerifyTablesAndColumns(&db.db_);
1187 } 1331 }
1188 } 1332 }
1189 1333
1190 } // namespace history 1334 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698