| 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 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | |
| 20 #include "chrome/browser/history/expire_history_backend.h" | 19 #include "chrome/browser/history/expire_history_backend.h" |
| 21 #include "chrome/browser/history/history_database.h" | 20 #include "chrome/browser/history/history_database.h" |
| 22 #include "chrome/browser/history/history_notifications.h" | |
| 23 #include "chrome/browser/history/thumbnail_database.h" | 21 #include "chrome/browser/history/thumbnail_database.h" |
| 24 #include "chrome/browser/history/top_sites.h" | 22 #include "chrome/browser/history/top_sites.h" |
| 25 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 26 #include "chrome/tools/profiles/thumbnail-inl.h" | 24 #include "chrome/tools/profiles/thumbnail-inl.h" |
| 27 #include "components/history/core/common/thumbnail_score.h" | 25 #include "components/history/core/common/thumbnail_score.h" |
| 28 #include "components/history/core/test/history_client_fake_bookmarks.h" | 26 #include "components/history/core/test/history_client_fake_bookmarks.h" |
| 29 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | 29 #include "third_party/skia/include/core/SkBitmap.h" |
| 32 #include "ui/gfx/codec/jpeg_codec.h" | 30 #include "ui/gfx/codec/jpeg_codec.h" |
| 33 | 31 |
| 34 using base::Time; | 32 using base::Time; |
| 35 using base::TimeDelta; | 33 using base::TimeDelta; |
| 36 using base::TimeTicks; | 34 using base::TimeTicks; |
| 37 using content::BrowserThread; | 35 using content::BrowserThread; |
| 38 | 36 |
| 39 // Filename constants. | 37 // Filename constants. |
| 40 static const base::FilePath::CharType kHistoryFile[] = | 38 static const base::FilePath::CharType kHistoryFile[] = |
| 41 FILE_PATH_LITERAL("History"); | 39 FILE_PATH_LITERAL("History"); |
| 42 static const base::FilePath::CharType kThumbnailFile[] = | 40 static const base::FilePath::CharType kThumbnailFile[] = |
| 43 FILE_PATH_LITERAL("Thumbnails"); | 41 FILE_PATH_LITERAL("Thumbnails"); |
| 44 | 42 |
| 45 // The test must be in the history namespace for the gtest forward declarations | 43 // The test must be in the history namespace for the gtest forward declarations |
| 46 // to work. It also eliminates a bunch of ugly "history::". | 44 // to work. It also eliminates a bunch of ugly "history::". |
| 47 namespace history { | 45 namespace history { |
| 48 | 46 |
| 49 // ExpireHistoryTest ----------------------------------------------------------- | 47 // ExpireHistoryTest ----------------------------------------------------------- |
| 50 | 48 |
| 51 class ExpireHistoryTest : public testing::Test, | 49 class ExpireHistoryTest : public testing::Test, |
| 52 public BroadcastNotificationDelegate { | 50 public ExpireHistoryBackendDelegate { |
| 53 public: | 51 public: |
| 54 ExpireHistoryTest() | 52 ExpireHistoryTest() |
| 55 : ui_thread_(BrowserThread::UI, &message_loop_), | 53 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 56 db_thread_(BrowserThread::DB, &message_loop_), | 54 db_thread_(BrowserThread::DB, &message_loop_), |
| 57 expirer_(this, &history_client_), | 55 expirer_(this, &history_client_), |
| 58 now_(Time::Now()) {} | 56 now_(Time::Now()) {} |
| 59 | 57 |
| 60 protected: | 58 protected: |
| 61 // Called by individual tests when they want data populated. | 59 // Called by individual tests when they want data populated. |
| 62 void AddExampleData(URLID url_ids[3], Time visit_times[4]); | 60 void AddExampleData(URLID url_ids[3], Time visit_times[4]); |
| 63 // Add visits with source information. | 61 // Add visits with source information. |
| 64 void AddExampleSourceData(const GURL& url, URLID* id); | 62 void AddExampleSourceData(const GURL& url, URLID* id); |
| 65 | 63 |
| 66 // Returns true if the given favicon/thumanil has an entry in the DB. | 64 // Returns true if the given favicon/thumanil has an entry in the DB. |
| 67 bool HasFavicon(favicon_base::FaviconID favicon_id); | 65 bool HasFavicon(favicon_base::FaviconID favicon_id); |
| 68 bool HasThumbnail(URLID url_id); | 66 bool HasThumbnail(URLID url_id); |
| 69 | 67 |
| 70 favicon_base::FaviconID GetFavicon(const GURL& page_url, | 68 favicon_base::FaviconID GetFavicon(const GURL& page_url, |
| 71 favicon_base::IconType icon_type); | 69 favicon_base::IconType icon_type); |
| 72 | 70 |
| 73 // EXPECTs that each URL-specific history thing (basically, everything but | 71 // EXPECTs that each URL-specific history thing (basically, everything but |
| 74 // favicons) is gone, the reason being either that it was automatically | 72 // favicons) is gone, the reason being either that it was automatically |
| 75 // |expired|, or manually deleted. | 73 // |expired|, or manually deleted. |
| 76 void EnsureURLInfoGone(const URLRow& row, bool expired); | 74 void EnsureURLInfoGone(const URLRow& row, bool expired); |
| 77 | 75 |
| 78 // Returns whether a NOTIFICATION_HISTORY_URLS_MODIFIED was sent for |url|. | 76 // Returns whether ExpireHistoryBackendDelegate::NotifyURLsModified was |
| 77 // called for |url|. |
| 79 bool ModifiedNotificationSent(const GURL& url); | 78 bool ModifiedNotificationSent(const GURL& url); |
| 80 | 79 |
| 81 // Clears the list of notifications received. | 80 // Clears the list of notifications received. |
| 82 void ClearLastNotifications() { | 81 void ClearLastNotifications() { |
| 83 STLDeleteValues(¬ifications_); | 82 STLDeleteValues(¬ifications_); |
| 83 urls_modified_notifications_.clear(); |
| 84 urls_deleted_notifications_.clear(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void StarURL(const GURL& url) { history_client_.AddBookmark(url); } | 87 void StarURL(const GURL& url) { history_client_.AddBookmark(url); } |
| 87 | 88 |
| 88 static bool IsStringInFile(const base::FilePath& filename, const char* str); | 89 static bool IsStringInFile(const base::FilePath& filename, const char* str); |
| 89 | 90 |
| 90 // Returns the path the db files are created in. | 91 // Returns the path the db files are created in. |
| 91 const base::FilePath& path() const { return tmp_dir_.path(); } | 92 const base::FilePath& path() const { return tmp_dir_.path(); } |
| 92 | 93 |
| 93 // This must be destroyed last. | 94 // This must be destroyed last. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 109 // Time at the beginning of the test, so everybody agrees what "now" is. | 110 // Time at the beginning of the test, so everybody agrees what "now" is. |
| 110 const Time now_; | 111 const Time now_; |
| 111 | 112 |
| 112 // Notifications intended to be broadcast, we can check these values to make | 113 // Notifications intended to be broadcast, we can check these values to make |
| 113 // sure that the deletor is doing the correct broadcasts. We own the details | 114 // sure that the deletor is doing the correct broadcasts. We own the details |
| 114 // pointers. | 115 // pointers. |
| 115 typedef std::vector< std::pair<int, HistoryDetails*> > | 116 typedef std::vector< std::pair<int, HistoryDetails*> > |
| 116 NotificationList; | 117 NotificationList; |
| 117 NotificationList notifications_; | 118 NotificationList notifications_; |
| 118 | 119 |
| 120 typedef std::vector<URLRows> URLsModifiedNotificationList; |
| 121 URLsModifiedNotificationList urls_modified_notifications_; |
| 122 |
| 123 typedef std::vector<std::pair<bool, URLRows>> URLsDeletedNotificationList; |
| 124 URLsDeletedNotificationList urls_deleted_notifications_; |
| 125 |
| 119 private: | 126 private: |
| 120 void SetUp() override { | 127 void SetUp() override { |
| 121 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | 128 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); |
| 122 | 129 |
| 123 base::FilePath history_name = path().Append(kHistoryFile); | 130 base::FilePath history_name = path().Append(kHistoryFile); |
| 124 main_db_.reset(new HistoryDatabase); | 131 main_db_.reset(new HistoryDatabase); |
| 125 if (main_db_->Init(history_name) != sql::INIT_OK) | 132 if (main_db_->Init(history_name) != sql::INIT_OK) |
| 126 main_db_.reset(); | 133 main_db_.reset(); |
| 127 | 134 |
| 128 base::FilePath thumb_name = path().Append(kThumbnailFile); | 135 base::FilePath thumb_name = path().Append(kThumbnailFile); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 140 top_sites_ = NULL; | 147 top_sites_ = NULL; |
| 141 | 148 |
| 142 ClearLastNotifications(); | 149 ClearLastNotifications(); |
| 143 | 150 |
| 144 expirer_.SetDatabases(NULL, NULL); | 151 expirer_.SetDatabases(NULL, NULL); |
| 145 | 152 |
| 146 main_db_.reset(); | 153 main_db_.reset(); |
| 147 thumb_db_.reset(); | 154 thumb_db_.reset(); |
| 148 } | 155 } |
| 149 | 156 |
| 150 // BroadcastNotificationDelegate: | 157 // ExpireHistoryBackendDelegate: |
| 151 void BroadcastNotifications(int type, | 158 void NotifyURLsModified(const URLRows& rows) override { |
| 152 scoped_ptr<HistoryDetails> details) override { | 159 urls_modified_notifications_.push_back(rows); |
| 153 // This gets called when there are notifications to broadcast. Instead, we | |
| 154 // store them so we can tell that the correct notifications were sent. | |
| 155 notifications_.push_back(std::make_pair(type, details.release())); | |
| 156 } | 160 } |
| 157 void NotifySyncURLsModified(URLRows* rows) override {} | 161 |
| 158 void NotifySyncURLsDeleted(bool all_history, | 162 void NotifyURLsDeleted(bool all_history, |
| 159 bool expired, | 163 bool expired, |
| 160 URLRows* rows) override {} | 164 const URLRows& rows, |
| 165 const std::set<GURL>& favicon_urls) override { |
| 166 urls_deleted_notifications_.push_back(std::make_pair(expired, rows)); |
| 167 } |
| 161 }; | 168 }; |
| 162 | 169 |
| 163 // The example data consists of 4 visits. The middle two visits are to the | 170 // The example data consists of 4 visits. The middle two visits are to the |
| 164 // same URL, while the first and last are for unique ones. This allows a test | 171 // same URL, while the first and last are for unique ones. This allows a test |
| 165 // for the oldest or newest to include both a URL that should get totally | 172 // for the oldest or newest to include both a URL that should get totally |
| 166 // deleted (the one on the end) with one that should only get a visit deleted | 173 // deleted (the one on the end) with one that should only get a visit deleted |
| 167 // (with the one in the middle) when it picks the proper threshold time. | 174 // (with the one in the middle) when it picks the proper threshold time. |
| 168 // | 175 // |
| 169 // Each visit has indexed data, each URL has thumbnail. The first two URLs will | 176 // Each visit has indexed data, each URL has thumbnail. The first two URLs will |
| 170 // share the same avicon, while the last one will have a unique favicon. The | 177 // share the same avicon, while the last one will have a unique favicon. The |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 // There should be no visits. | 321 // There should be no visits. |
| 315 VisitVector visits; | 322 VisitVector visits; |
| 316 main_db_->GetVisitsForURL(row.id(), &visits); | 323 main_db_->GetVisitsForURL(row.id(), &visits); |
| 317 EXPECT_EQ(0U, visits.size()); | 324 EXPECT_EQ(0U, visits.size()); |
| 318 | 325 |
| 319 // Thumbnail should be gone. | 326 // Thumbnail should be gone. |
| 320 // TODO(sky): fix this, see comment in HasThumbnail. | 327 // TODO(sky): fix this, see comment in HasThumbnail. |
| 321 // EXPECT_FALSE(HasThumbnail(row.id())); | 328 // EXPECT_FALSE(HasThumbnail(row.id())); |
| 322 | 329 |
| 323 bool found_delete_notification = false; | 330 bool found_delete_notification = false; |
| 324 for (size_t i = 0; i < notifications_.size(); i++) { | 331 for (const std::pair<bool, URLRows>& pair : urls_deleted_notifications_) { |
| 325 if (notifications_[i].first == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { | 332 EXPECT_EQ(expired, pair.first); |
| 326 URLsDeletedDetails* details = reinterpret_cast<URLsDeletedDetails*>( | 333 const history::URLRows& rows(pair.second); |
| 327 notifications_[i].second); | 334 history::URLRows::const_iterator it_row = std::find_if( |
| 328 EXPECT_EQ(expired, details->expired); | 335 rows.begin(), rows.end(), history::URLRow::URLRowHasURL(row.url())); |
| 329 const history::URLRows& rows(details->rows); | 336 if (it_row != rows.end()) { |
| 330 history::URLRows::const_iterator it_row = std::find_if( | 337 // Further verify that the ID is set to what had been in effect in the |
| 331 rows.begin(), rows.end(), history::URLRow::URLRowHasURL(row.url())); | 338 // main database before the deletion. The InMemoryHistoryBackend relies |
| 332 if (it_row != rows.end()) { | 339 // on this to delete its cached copy of the row. |
| 333 // Further verify that the ID is set to what had been in effect in the | 340 EXPECT_EQ(row.id(), it_row->id()); |
| 334 // main database before the deletion. The InMemoryHistoryBackend relies | 341 found_delete_notification = true; |
| 335 // on this to delete its cached copy of the row. | |
| 336 EXPECT_EQ(row.id(), it_row->id()); | |
| 337 found_delete_notification = true; | |
| 338 } | |
| 339 } else if (notifications_[i].first == | |
| 340 chrome::NOTIFICATION_HISTORY_URLS_MODIFIED) { | |
| 341 const history::URLRows& rows = | |
| 342 static_cast<URLsModifiedDetails*>(notifications_[i].second)-> | |
| 343 changed_urls; | |
| 344 EXPECT_TRUE( | |
| 345 std::find_if(rows.begin(), rows.end(), | |
| 346 history::URLRow::URLRowHasURL(row.url())) == | |
| 347 rows.end()); | |
| 348 } | 342 } |
| 349 } | 343 } |
| 344 for (const auto& rows : urls_modified_notifications_) { |
| 345 EXPECT_TRUE(std::find_if(rows.begin(), |
| 346 rows.end(), |
| 347 history::URLRow::URLRowHasURL(row.url())) == |
| 348 rows.end()); |
| 349 } |
| 350 EXPECT_TRUE(found_delete_notification); | 350 EXPECT_TRUE(found_delete_notification); |
| 351 } | 351 } |
| 352 | 352 |
| 353 bool ExpireHistoryTest::ModifiedNotificationSent(const GURL& url) { | 353 bool ExpireHistoryTest::ModifiedNotificationSent(const GURL& url) { |
| 354 for (size_t i = 0; i < notifications_.size(); i++) { | 354 for (const auto& rows : urls_modified_notifications_) { |
| 355 if (notifications_[i].first == chrome::NOTIFICATION_HISTORY_URLS_MODIFIED) { | 355 if (std::find_if(rows.begin(), |
| 356 const history::URLRows& rows = | 356 rows.end(), |
| 357 static_cast<URLsModifiedDetails*>(notifications_[i].second)-> | 357 history::URLRow::URLRowHasURL(url)) != rows.end()) |
| 358 changed_urls; | 358 return true; |
| 359 if (std::find_if(rows.begin(), rows.end(), | |
| 360 history::URLRow::URLRowHasURL(url)) != rows.end()) | |
| 361 return true; | |
| 362 } | |
| 363 } | 359 } |
| 364 return false; | 360 return false; |
| 365 } | 361 } |
| 366 | 362 |
| 367 TEST_F(ExpireHistoryTest, DeleteFaviconsIfPossible) { | 363 TEST_F(ExpireHistoryTest, DeleteFaviconsIfPossible) { |
| 368 // Add a favicon record. | 364 // Add a favicon record. |
| 369 const GURL favicon_url("http://www.google.com/favicon.ico"); | 365 const GURL favicon_url("http://www.google.com/favicon.ico"); |
| 370 favicon_base::FaviconID icon_id = | 366 favicon_base::FaviconID icon_id = |
| 371 thumb_db_->AddFavicon(favicon_url, favicon_base::FAVICON); | 367 thumb_db_->AddFavicon(favicon_url, favicon_base::FAVICON); |
| 372 EXPECT_TRUE(icon_id); | 368 EXPECT_TRUE(icon_id); |
| (...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 873 EXPECT_TRUE(all->Read(now, main_db_.get(), &visits, 1)); | 869 EXPECT_TRUE(all->Read(now, main_db_.get(), &visits, 1)); |
| 874 EXPECT_EQ(1U, visits.size()); | 870 EXPECT_EQ(1U, visits.size()); |
| 875 } | 871 } |
| 876 | 872 |
| 877 // TODO(brettw) add some visits with no URL to make sure everything is updated | 873 // TODO(brettw) add some visits with no URL to make sure everything is updated |
| 878 // properly. Have the visits also refer to nonexistent FTS rows. | 874 // properly. Have the visits also refer to nonexistent FTS rows. |
| 879 // | 875 // |
| 880 // Maybe also refer to invalid favicons. | 876 // Maybe also refer to invalid favicons. |
| 881 | 877 |
| 882 } // namespace history | 878 } // namespace history |
| OLD | NEW |