Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <cstdio> |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 9 | #include <memory> |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 13 | #include "base/files/file_util.h" |
Greg Thompson | 5310924e2 | 2019-11-04 20:22:02 | [diff] [blame] | 14 | #include "base/files/scoped_temp_dir.h" |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 15 | #include "base/functional/bind.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 16 | #include "base/location.h" |
Keishi Hattori | e175ac5 | 2022-06-07 06:24:57 | [diff] [blame] | 17 | #include "base/memory/raw_ptr.h" |
[email protected] | 2773016 | 2013-07-23 14:51:55 | [diff] [blame] | 18 | #include "base/process/process_handle.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 19 | #include "base/run_loop.h" |
[email protected] | c72674b | 2013-06-11 04:16:43 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
Lei Zhang | e02299a | 2021-04-26 23:12:24 | [diff] [blame] | 21 | #include "base/strings/stringprintf.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 22 | #include "base/task/single_thread_task_runner.h" |
[email protected] | bbd8da9 | 2013-06-28 02:12:20 | [diff] [blame] | 23 | #include "base/time/time.h" |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 24 | #include "base/timer/mock_timer.h" |
[email protected] | 1f371fa | 2013-01-23 00:35:14 | [diff] [blame] | 25 | #include "components/visitedlink/browser/visitedlink_delegate.h" |
| 26 | #include "components/visitedlink/browser/visitedlink_event_listener.h" |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 27 | #include "components/visitedlink/browser/visitedlink_writer.h" |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 28 | #include "components/visitedlink/common/visitedlink.mojom.h" |
Artin Lindqvist | d0647cca | 2023-02-27 19:23:02 | [diff] [blame] | 29 | #include "components/visitedlink/common/visitedlink_common.h" |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 30 | #include "components/visitedlink/renderer/visitedlink_reader.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 31 | #include "content/public/browser/browser_thread.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 32 | #include "content/public/test/browser_task_environment.h" |
[email protected] | 08a932d5 | 2012-06-03 21:42:12 | [diff] [blame] | 33 | #include "content/public/test/mock_render_process_host.h" |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 34 | #include "content/public/test/test_browser_context.h" |
[email protected] | b1e3f20 | 2012-06-04 14:45:50 | [diff] [blame] | 35 | #include "content/public/test/test_renderer_host.h" |
mtomasz | f7d99a5c | 2014-09-15 16:23:46 | [diff] [blame] | 36 | #include "content/public/test/test_utils.h" |
Julie Jeongeun Kim | 8ab180f7 | 2019-09-25 09:25:46 | [diff] [blame] | 37 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 38 | #include "mojo/public/cpp/bindings/receiver_set.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | #include "testing/gtest/include/gtest/gtest.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 40 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | e3b599e | 2013-07-05 07:15:17 | [diff] [blame] | 41 | #include "url/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 43 | using content::BrowserThread; |
[email protected] | eddd88670 | 2012-03-16 14:53:23 | [diff] [blame] | 44 | using content::MockRenderProcessHost; |
[email protected] | c025738 | 2012-03-12 20:15:34 | [diff] [blame] | 45 | using content::RenderViewHostTester; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 46 | |
Matt Falkenhagen | 24abf782 | 2017-12-15 23:55:11 | [diff] [blame] | 47 | namespace content { |
| 48 | class SiteInstance; |
| 49 | } |
| 50 | |
[email protected] | ab3eaeed | 2013-05-17 00:18:44 | [diff] [blame] | 51 | namespace visitedlink { |
[email protected] | 1f371fa | 2013-01-23 00:35:14 | [diff] [blame] | 52 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | namespace { |
| 54 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 55 | typedef std::vector<GURL> URLs; |
| 56 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | // a nice long URL that we can append numbers to to get new URLs |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 58 | const char kTestPrefix[] = |
| 59 | "http://www.google.com/products/foo/index.html?id=45028640526508376&seq="; |
| 60 | constexpr int kTestCount = 1000; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | |
| 62 | // Returns a test URL for index |i| |
| 63 | GURL TestURL(int i) { |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 64 | return GURL(base::StringPrintf("%s%d", kTestPrefix, i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | } |
| 66 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 67 | std::vector<VisitedLinkReader*> g_readers; |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 68 | |
| 69 | class TestVisitedLinkDelegate : public VisitedLinkDelegate { |
| 70 | public: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 71 | void RebuildTable(const scoped_refptr<URLEnumerator>& enumerator) override; |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 72 | |
| 73 | void AddURLForRebuild(const GURL& url); |
| 74 | |
| 75 | private: |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 76 | URLs rebuild_urls_; |
| 77 | }; |
| 78 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 79 | void TestVisitedLinkDelegate::RebuildTable( |
| 80 | const scoped_refptr<URLEnumerator>& enumerator) { |
| 81 | for (URLs::const_iterator itr = rebuild_urls_.begin(); |
Ken Rockot | 4a16adb | 2019-12-17 07:31:42 | [diff] [blame] | 82 | itr != rebuild_urls_.end(); ++itr) |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 83 | enumerator->OnURL(*itr); |
| 84 | enumerator->OnComplete(true); |
| 85 | } |
| 86 | |
| 87 | void TestVisitedLinkDelegate::AddURLForRebuild(const GURL& url) { |
| 88 | rebuild_urls_.push_back(url); |
| 89 | } |
| 90 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 91 | class TestURLIterator : public VisitedLinkWriter::URLIterator { |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 92 | public: |
| 93 | explicit TestURLIterator(const URLs& urls); |
| 94 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 95 | const GURL& NextURL() override; |
| 96 | bool HasNextURL() const override; |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 97 | |
| 98 | private: |
| 99 | URLs::const_iterator iterator_; |
| 100 | URLs::const_iterator end_; |
| 101 | }; |
| 102 | |
| 103 | TestURLIterator::TestURLIterator(const URLs& urls) |
Ken Rockot | 4a16adb | 2019-12-17 07:31:42 | [diff] [blame] | 104 | : iterator_(urls.begin()), end_(urls.end()) {} |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 105 | |
| 106 | const GURL& TestURLIterator::NextURL() { |
| 107 | return *(iterator_++); |
| 108 | } |
| 109 | |
| 110 | bool TestURLIterator::HasNextURL() const { |
| 111 | return iterator_ != end_; |
| 112 | } |
[email protected] | dba45e3 | 2013-01-04 20:27:15 | [diff] [blame] | 113 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 114 | } // namespace |
| 115 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 116 | class TrackingVisitedLinkEventListener : public VisitedLinkWriter::Listener { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 117 | public: |
| 118 | TrackingVisitedLinkEventListener() |
Ken Rockot | 4a16adb | 2019-12-17 07:31:42 | [diff] [blame] | 119 | : reset_count_(0), completely_reset_count_(0), add_count_(0) {} |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 120 | |
Ken Rockot | aeb7eca | 2018-03-30 16:40:09 | [diff] [blame] | 121 | void NewTable(base::ReadOnlySharedMemoryRegion* table_region) override { |
| 122 | if (table_region->IsValid()) { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 123 | for (std::vector<VisitedLinkReader>::size_type i = 0; |
| 124 | i < g_readers.size(); i++) { |
| 125 | g_readers[i]->UpdateVisitedLinks(table_region->Duplicate()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 126 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | } |
| 128 | } |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 129 | void Add(VisitedLinkCommon::Fingerprint) override { add_count_++; } |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 130 | void Reset(bool invalidate_hashes) override { |
| 131 | if (invalidate_hashes) |
| 132 | completely_reset_count_++; |
| 133 | else |
| 134 | reset_count_++; |
| 135 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 137 | void SetUp() { |
| 138 | reset_count_ = 0; |
| 139 | add_count_ = 0; |
| 140 | } |
| 141 | |
| 142 | int reset_count() const { return reset_count_; } |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 143 | int completely_reset_count() { return completely_reset_count_; } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 144 | int add_count() const { return add_count_; } |
| 145 | |
| 146 | private: |
| 147 | int reset_count_; |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 148 | int completely_reset_count_; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 149 | int add_count_; |
| 150 | }; |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 151 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 152 | class VisitedLinkTest : public testing::Test { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | protected: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | // Initializes the visited link objects. Pass in the size that you want a |
| 155 | // freshly created table to be. 0 means use the default. |
| 156 | // |
| 157 | // |suppress_rebuild| is set when we're not testing rebuilding, see |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 158 | // the VisitedLinkWriter constructor. |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 159 | // |
| 160 | // |wait_for_io_complete| wait for result of async loading. |
| 161 | bool InitVisited(int initial_size, |
| 162 | bool suppress_rebuild, |
| 163 | bool wait_for_io_complete) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | // Initialize the visited link system. |
Peter Boström | 665b49d | 2021-04-05 19:29:58 | [diff] [blame] | 165 | writer_ = std::make_unique<VisitedLinkWriter>( |
| 166 | new TrackingVisitedLinkEventListener(), &delegate_, true, |
| 167 | suppress_rebuild, visited_file_, initial_size); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 168 | bool result = writer_->Init(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 169 | if (result && wait_for_io_complete) { |
| 170 | // Wait for all pending file I/O to be completed. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 171 | content::RunAllTasksUntilIdle(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 172 | } |
| 173 | return result; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // May be called multiple times (some tests will do this to clear things, |
| 177 | // and TearDown will do this to make sure eveything is shiny before quitting. |
| 178 | void ClearDB() { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 179 | writer_.reset(nullptr); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | |
[email protected] | 3189013e | 2012-01-19 04:11:57 | [diff] [blame] | 181 | // Wait for all pending file I/O to be completed. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 182 | content::RunAllTasksUntilIdle(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Loads the database from disk and makes sure that the same URLs are present |
| 186 | // as were generated by TestIO_Create(). This also checks the URLs with a |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 187 | // reader to make sure it reads the data properly. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | void Reload() { |
| 189 | // Clean up after our caller, who may have left the database open. |
| 190 | ClearDB(); |
| 191 | |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 192 | ASSERT_TRUE(InitVisited(0, true, true)); |
| 193 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 194 | writer_->DebugValidate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | |
| 196 | // check that the table has the proper number of entries |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 197 | int used_count = writer_->GetUsedCount(); |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 198 | ASSERT_EQ(used_count, kTestCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 200 | // Create a reader database. |
| 201 | VisitedLinkReader reader; |
| 202 | reader.UpdateVisitedLinks( |
| 203 | writer_->mapped_table_memory().region.Duplicate()); |
| 204 | g_readers.push_back(&reader); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
| 206 | bool found; |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 207 | for (int i = 0; i < kTestCount; i++) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 208 | GURL cur = TestURL(i); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 209 | found = writer_->IsVisited(cur); |
| 210 | EXPECT_TRUE(found) << "URL " << i << "not found in writer."; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 212 | found = reader.IsVisited(cur); |
| 213 | EXPECT_TRUE(found) << "URL " << i << "not found in reader."; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // test some random URL so we know that it returns false sometimes too |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 217 | found = writer_->IsVisited(GURL("http://unfound.site/")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | ASSERT_FALSE(found); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 219 | found = reader.IsVisited(GURL("http://unfound.site/")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | ASSERT_FALSE(found); |
| 221 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 222 | writer_->DebugValidate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 224 | g_readers.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // testing::Test |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 228 | void SetUp() override { |
[email protected] | f708ed1 | 2010-09-23 12:18:34 | [diff] [blame] | 229 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 230 | |
vabr | 8c498ea4 | 2016-09-15 12:41:58 | [diff] [blame] | 231 | history_dir_ = temp_dir_.GetPath().AppendASCII("VisitedLinkTest"); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 232 | ASSERT_TRUE(base::CreateDirectory(history_dir_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 234 | visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | } |
| 236 | |
Greg Thompson | 5310924e2 | 2019-11-04 20:22:02 | [diff] [blame] | 237 | void TearDown() override { |
| 238 | ClearDB(); |
| 239 | ASSERT_TRUE(temp_dir_.Delete()); |
| 240 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 241 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 242 | base::ScopedTempDir temp_dir_; |
[email protected] | f708ed1 | 2010-09-23 12:18:34 | [diff] [blame] | 243 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | // Filenames for the services; |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 245 | base::FilePath history_dir_; |
| 246 | base::FilePath visited_file_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 248 | std::unique_ptr<VisitedLinkWriter> writer_; |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 249 | TestVisitedLinkDelegate delegate_; |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 250 | content::BrowserTaskEnvironment task_environment_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | }; |
| 252 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | // This test creates and reads some databases to make sure the data is |
| 254 | // preserved throughout those operations. |
| 255 | TEST_F(VisitedLinkTest, DatabaseIO) { |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 256 | ASSERT_TRUE(InitVisited(0, true, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 258 | for (int i = 0; i < kTestCount; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 259 | writer_->AddURL(TestURL(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | |
| 261 | // Test that the database was written properly |
| 262 | Reload(); |
| 263 | } |
| 264 | |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 265 | // This test fills a database using AddURLs() and verifies that it can be read |
| 266 | // back correctly. |
| 267 | TEST_F(VisitedLinkTest, DatabaseIOAddURLs) { |
| 268 | ASSERT_TRUE(InitVisited(0, true, true)); |
| 269 | |
| 270 | constexpr int kHalfCount = kTestCount / 2; |
| 271 | |
| 272 | // Add urls in pairs of two. Simulates calls to AddURLs() after navigations |
| 273 | // with redirects. |
| 274 | for (int i = 0; i < (kHalfCount - 1); i += 2) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 275 | writer_->AddURLs({TestURL(i), TestURL(i + 1)}); |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 276 | |
| 277 | // Add a big vector of URLs, exceeding kBulkOperationThreshold. |
| 278 | std::vector<GURL> urls; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 279 | static_assert(kHalfCount > VisitedLinkWriter::kBulkOperationThreshold, |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 280 | "kBulkOperationThreshold not exceeded"); |
| 281 | for (int i = kHalfCount; i < kTestCount; i++) |
| 282 | urls.push_back(TestURL(i)); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 283 | writer_->AddURLs(urls); |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 284 | |
| 285 | // Test that the database was written properly. |
| 286 | Reload(); |
| 287 | } |
| 288 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | // Checks that we can delete things properly when there are collisions. |
| 290 | TEST_F(VisitedLinkTest, Delete) { |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 291 | static const int32_t kInitialSize = 17; |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 292 | ASSERT_TRUE(InitVisited(kInitialSize, true, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | |
| 294 | // Add a cluster from 14-17 wrapping around to 0. These will all hash to the |
| 295 | // same value. |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 296 | const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14; |
| 297 | const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14; |
| 298 | const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14; |
| 299 | const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14; |
| 300 | const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 301 | writer_->AddFingerprint(kFingerprint0, false); // @14 |
| 302 | writer_->AddFingerprint(kFingerprint1, false); // @15 |
| 303 | writer_->AddFingerprint(kFingerprint2, false); // @16 |
| 304 | writer_->AddFingerprint(kFingerprint3, false); // @0 |
| 305 | writer_->AddFingerprint(kFingerprint4, false); // @1 |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | |
| 307 | // Deleting 14 should move the next value up one slot (we do not specify an |
| 308 | // order). |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 309 | EXPECT_EQ(kFingerprint3, writer_->hash_table_[0]); |
| 310 | writer_->DeleteFingerprint(kFingerprint3, false); |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 311 | VisitedLinkCommon::Fingerprint zero_fingerprint = 0; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 312 | EXPECT_EQ(zero_fingerprint, writer_->hash_table_[1]); |
| 313 | EXPECT_NE(zero_fingerprint, writer_->hash_table_[0]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | |
| 315 | // Deleting the other four should leave the table empty. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 316 | writer_->DeleteFingerprint(kFingerprint0, false); |
| 317 | writer_->DeleteFingerprint(kFingerprint1, false); |
| 318 | writer_->DeleteFingerprint(kFingerprint2, false); |
| 319 | writer_->DeleteFingerprint(kFingerprint4, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 320 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 321 | EXPECT_EQ(0, writer_->used_items_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 322 | for (int i = 0; i < kInitialSize; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 323 | EXPECT_EQ(zero_fingerprint, writer_->hash_table_[i]) |
| 324 | << "Hash table has values in it."; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 325 | } |
| 326 | |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 327 | // When we delete more than kBulkOperationThreshold we trigger different |
| 328 | // behavior where the entire file is rewritten. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 329 | TEST_F(VisitedLinkTest, BigDelete) { |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 330 | ASSERT_TRUE(InitVisited(16381, true, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | |
| 332 | // Add the base set of URLs that won't be deleted. |
| 333 | // Reload() will test for these. |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 334 | for (int32_t i = 0; i < kTestCount; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 335 | writer_->AddURL(TestURL(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 336 | |
| 337 | // Add more URLs than necessary to trigger this case. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 338 | const int kTestDeleteCount = VisitedLinkWriter::kBulkOperationThreshold + 2; |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 339 | URLs urls_to_delete; |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 340 | for (int32_t i = kTestCount; i < kTestCount + kTestDeleteCount; i++) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | GURL url(TestURL(i)); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 342 | writer_->AddURL(url); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 343 | urls_to_delete.push_back(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 346 | TestURLIterator iterator(urls_to_delete); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 347 | writer_->DeleteURLs(&iterator); |
| 348 | writer_->DebugValidate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 349 | |
| 350 | Reload(); |
| 351 | } |
| 352 | |
| 353 | TEST_F(VisitedLinkTest, DeleteAll) { |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 354 | ASSERT_TRUE(InitVisited(0, true, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 355 | |
| 356 | { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 357 | VisitedLinkReader reader; |
| 358 | reader.UpdateVisitedLinks( |
| 359 | writer_->mapped_table_memory().region.Duplicate()); |
| 360 | g_readers.push_back(&reader); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | |
| 362 | // Add the test URLs. |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 363 | for (int i = 0; i < kTestCount; i++) { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 364 | writer_->AddURL(TestURL(i)); |
| 365 | ASSERT_EQ(i + 1, writer_->GetUsedCount()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | } |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 367 | writer_->DebugValidate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 368 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 369 | // Make sure the reader picked up the adds. |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 370 | for (int i = 0; i < kTestCount; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 371 | EXPECT_TRUE(reader.IsVisited(TestURL(i))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 373 | // Clear the table and make sure the reader picked it up. |
| 374 | writer_->DeleteAllURLs(); |
| 375 | EXPECT_EQ(0, writer_->GetUsedCount()); |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 376 | for (int i = 0; i < kTestCount; i++) { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 377 | EXPECT_FALSE(writer_->IsVisited(TestURL(i))); |
| 378 | EXPECT_FALSE(reader.IsVisited(TestURL(i))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // Close the database. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 382 | g_readers.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 383 | ClearDB(); |
| 384 | } |
| 385 | |
| 386 | // Reopen and validate. |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 387 | ASSERT_TRUE(InitVisited(0, true, true)); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 388 | writer_->DebugValidate(); |
| 389 | EXPECT_EQ(0, writer_->GetUsedCount()); |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 390 | for (int i = 0; i < kTestCount; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 391 | EXPECT_FALSE(writer_->IsVisited(TestURL(i))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | } |
| 393 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 394 | // This tests that the writer correctly resizes its tables when it gets too |
| 395 | // full, notifies its readers of the change, and updates the disk. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | TEST_F(VisitedLinkTest, Resizing) { |
| 397 | // Create a very small database. |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 398 | const int32_t initial_size = 17; |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 399 | ASSERT_TRUE(InitVisited(initial_size, true, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 401 | // ...and a reader |
| 402 | VisitedLinkReader reader; |
| 403 | reader.UpdateVisitedLinks(writer_->mapped_table_memory().region.Duplicate()); |
| 404 | g_readers.push_back(&reader); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 406 | int32_t used_count = writer_->GetUsedCount(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | ASSERT_EQ(used_count, 0); |
| 408 | |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 409 | for (int i = 0; i < kTestCount; i++) { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 410 | writer_->AddURL(TestURL(i)); |
| 411 | used_count = writer_->GetUsedCount(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | ASSERT_EQ(i + 1, used_count); |
| 413 | } |
| 414 | |
| 415 | // Verify that the table got resized sufficiently. |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 416 | int32_t table_size; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 417 | VisitedLinkCommon::Fingerprint* table; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 418 | writer_->GetUsageStatistics(&table_size, &table); |
| 419 | used_count = writer_->GetUsedCount(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | ASSERT_GT(table_size, used_count); |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 421 | ASSERT_EQ(used_count, kTestCount) |
| 422 | << "table count doesn't match the # of things we added"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 424 | // Verify that the reader got the resize message and has the same |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 425 | // table information. |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 426 | int32_t child_table_size; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | VisitedLinkCommon::Fingerprint* child_table; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 428 | reader.GetUsageStatistics(&child_table_size, &child_table); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | ASSERT_EQ(table_size, child_table_size); |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 430 | for (int32_t i = 0; i < table_size; i++) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | ASSERT_EQ(table[i], child_table[i]); |
| 432 | } |
| 433 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 434 | writer_->DebugValidate(); |
| 435 | g_readers.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | |
| 437 | // This tests that the file is written correctly by reading it in using |
| 438 | // a new database. |
| 439 | Reload(); |
| 440 | } |
| 441 | |
| 442 | // Tests that if the database doesn't exist, it will be rebuilt from history. |
| 443 | TEST_F(VisitedLinkTest, Rebuild) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | // Add half of our URLs to history. This needs to be done before we |
| 445 | // initialize the visited link DB. |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 446 | int history_count = kTestCount / 2; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | for (int i = 0; i < history_count; i++) |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 448 | delegate_.AddURLForRebuild(TestURL(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | |
| 450 | // Initialize the visited link DB. Since the visited links file doesn't exist |
| 451 | // and we don't suppress history rebuilding, this will load from history. |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 452 | ASSERT_TRUE(InitVisited(0, false, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 453 | |
| 454 | // While the table is rebuilding, add the rest of the URLs to the visited |
| 455 | // link system. This isn't guaranteed to happen during the rebuild, so we |
| 456 | // can't be 100% sure we're testing the right thing, but in practice is. |
| 457 | // All the adds above will generally take some time queuing up on the |
| 458 | // history thread, and it will take a while to catch up to actually |
| 459 | // processing the rebuild that has queued behind it. We will generally |
| 460 | // finish adding all of the URLs before it has even found the first URL. |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 461 | for (int i = history_count; i < kTestCount; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 462 | writer_->AddURL(TestURL(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 463 | |
| 464 | // Add one more and then delete it. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 465 | writer_->AddURL(TestURL(kTestCount)); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 466 | URLs urls_to_delete; |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 467 | urls_to_delete.push_back(TestURL(kTestCount)); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 468 | TestURLIterator iterator(urls_to_delete); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 469 | writer_->DeleteURLs(&iterator); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 470 | |
| 471 | // Wait for the rebuild to complete. The task will terminate the message |
| 472 | // loop when the rebuild is done. There's no chance that the rebuild will |
| 473 | // complete before we set the task because the rebuild completion message |
| 474 | // is posted to the message loop; until we Run() it, rebuild can not |
| 475 | // complete. |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 476 | base::RunLoop run_loop; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 477 | writer_->set_rebuild_complete_task(run_loop.QuitClosure()); |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 478 | run_loop.Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | |
| 480 | // Test that all URLs were written to the database properly. |
| 481 | Reload(); |
| 482 | |
| 483 | // Make sure the extra one was *not* written (Reload won't test this). |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 484 | EXPECT_FALSE(writer_->IsVisited(TestURL(kTestCount))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 486 | |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 487 | // Test that importing a large number of URLs will work |
| 488 | TEST_F(VisitedLinkTest, BigImport) { |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 489 | ASSERT_TRUE(InitVisited(0, false, false)); |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 490 | |
| 491 | // Before the table rebuilds, add a large number of URLs |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 492 | int total_count = VisitedLinkWriter::kDefaultTableSize + 10; |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 493 | for (int i = 0; i < total_count; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 494 | writer_->AddURL(TestURL(i)); |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 495 | |
| 496 | // Wait for the rebuild to complete. |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 497 | base::RunLoop run_loop; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 498 | writer_->set_rebuild_complete_task(run_loop.QuitClosure()); |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 499 | run_loop.Run(); |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 500 | |
| 501 | // Ensure that the right number of URLs are present |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 502 | int used_count = writer_->GetUsedCount(); |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 503 | ASSERT_EQ(used_count, total_count); |
| 504 | } |
| 505 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 506 | TEST_F(VisitedLinkTest, Listener) { |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 507 | ASSERT_TRUE(InitVisited(0, true, true)); |
| 508 | |
| 509 | TrackingVisitedLinkEventListener* listener = |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 510 | static_cast<TrackingVisitedLinkEventListener*>(writer_->GetListener()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 511 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 512 | // Verify that VisitedLinkWriter::Listener::Reset(true) was never called when |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 513 | // the table was created. |
| 514 | EXPECT_EQ(0, listener->completely_reset_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 515 | |
| 516 | // Add test URLs. |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 517 | for (int i = 0; i < kTestCount; i++) { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 518 | writer_->AddURL(TestURL(i)); |
| 519 | ASSERT_EQ(i + 1, writer_->GetUsedCount()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 520 | } |
| 521 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 522 | // Delete an URL. |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 523 | URLs urls_to_delete; |
| 524 | urls_to_delete.push_back(TestURL(0)); |
| 525 | TestURLIterator iterator(urls_to_delete); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 526 | writer_->DeleteURLs(&iterator); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 527 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 528 | // ... and all of the remaining ones. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 529 | writer_->DeleteAllURLs(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 530 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 531 | // Verify that VisitedLinkWriter::Listener::Add was called for each added URL. |
Eric Seckler | 4364b05 | 2019-11-21 21:37:55 | [diff] [blame] | 532 | EXPECT_EQ(kTestCount, listener->add_count()); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 533 | // Verify that VisitedLinkWriter::Listener::Reset was called both when one and |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 534 | // all URLs are deleted. |
[email protected] | b42e6d8 | 2012-11-02 02:44:56 | [diff] [blame] | 535 | EXPECT_EQ(2, listener->reset_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 536 | |
| 537 | ClearDB(); |
| 538 | |
| 539 | ASSERT_TRUE(InitVisited(0, true, true)); |
| 540 | |
| 541 | listener = |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 542 | static_cast<TrackingVisitedLinkEventListener*>(writer_->GetListener()); |
| 543 | // Verify that VisitedLinkWriter::Listener::Reset(true) was called when the |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 544 | // table was loaded. |
| 545 | EXPECT_EQ(1, listener->completely_reset_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 546 | } |
| 547 | |
Artin Lindqvist | d0647cca | 2023-02-27 19:23:02 | [diff] [blame] | 548 | TEST_F(VisitedLinkTest, HashRangeWraparound) { |
| 549 | ASSERT_TRUE(InitVisited(0, true, true)); |
| 550 | |
| 551 | // Create two fingerprints that, when added, will create a wraparound hash |
| 552 | // range. |
| 553 | const VisitedLinkCommon::Fingerprint kFingerprint0 = |
| 554 | writer_->DefaultTableSize() - 1; |
| 555 | const VisitedLinkCommon::Fingerprint kFingerprint1 = kFingerprint0 + 1; |
| 556 | |
| 557 | // Add the two fingerprints. |
| 558 | const VisitedLinkCommon::Hash hash0 = |
| 559 | writer_->AddFingerprint(kFingerprint0, false); |
| 560 | const VisitedLinkCommon::Hash hash1 = |
| 561 | writer_->AddFingerprint(kFingerprint1, false); |
| 562 | |
| 563 | // Verify the hashes form a range that wraps around. |
| 564 | EXPECT_EQ(hash0, VisitedLinkCommon::Hash(writer_->DefaultTableSize() - 1)); |
| 565 | EXPECT_EQ(hash1, 0); |
| 566 | |
| 567 | // Write the database to file. |
| 568 | writer_->WriteUsedItemCountToFile(); |
| 569 | writer_->WriteHashRangeToFile(hash0, hash1); |
| 570 | |
| 571 | // Close and reopen the database. |
| 572 | ClearDB(); |
| 573 | ASSERT_TRUE(InitVisited(0, true, true)); |
| 574 | |
| 575 | // Verify database contents. |
| 576 | ASSERT_EQ(writer_->GetUsedCount(), 2); |
| 577 | ASSERT_TRUE(writer_->IsVisited(kFingerprint0)); |
| 578 | ASSERT_TRUE(writer_->IsVisited(kFingerprint1)); |
| 579 | } |
| 580 | |
Artin Lindqvist | 05c32ee | 2023-04-04 17:01:24 | [diff] [blame] | 581 | TEST_F(VisitedLinkTest, ResizeErrorHandling) { |
| 582 | // Create a small database. |
| 583 | const int32_t initial_size = 17; |
| 584 | ASSERT_TRUE(InitVisited(initial_size, true, true)); |
| 585 | |
| 586 | // Add test URL. |
| 587 | GURL url = TestURL(0); |
| 588 | writer_->AddURL(url); |
| 589 | |
| 590 | // Simulate shared memory allocation failure, causing CreateURLTable() to |
| 591 | // fail. |
| 592 | VisitedLinkWriter::fail_table_creation_for_testing_ = true; |
| 593 | |
| 594 | // Expect resize to fail silently. |
| 595 | const int32_t new_size = 23; |
| 596 | writer_->ResizeTable(new_size); |
| 597 | |
| 598 | // Restore global state for subsequent tests. |
| 599 | VisitedLinkWriter::fail_table_creation_for_testing_ = false; |
| 600 | |
| 601 | // Verify contents. |
| 602 | ASSERT_EQ(writer_->GetUsedCount(), 1); |
| 603 | ASSERT_TRUE(writer_->IsVisited(url)); |
| 604 | } |
| 605 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 606 | class VisitCountingContext : public mojom::VisitedLinkNotificationSink { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 607 | public: |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 608 | VisitCountingContext() |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 609 | : add_count_(0), |
| 610 | add_event_count_(0), |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 611 | reset_event_count_(0), |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 612 | completely_reset_event_count_(0), |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 613 | new_table_count_(0) {} |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 614 | |
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 615 | VisitCountingContext(const VisitCountingContext&) = delete; |
| 616 | VisitCountingContext& operator=(const VisitCountingContext&) = delete; |
| 617 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 618 | void Bind(mojo::ScopedMessagePipeHandle handle) { |
Julie Jeongeun Kim | 8ab180f7 | 2019-09-25 09:25:46 | [diff] [blame] | 619 | receiver_.Add(this, |
| 620 | mojo::PendingReceiver<mojom::VisitedLinkNotificationSink>( |
| 621 | std::move(handle))); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 622 | } |
| 623 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 624 | void WaitForUpdate() { |
| 625 | base::RunLoop run_loop; |
| 626 | quit_closure_ = run_loop.QuitClosure(); |
| 627 | run_loop.Run(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 628 | } |
| 629 | |
Julie Jeongeun Kim | 8ab180f7 | 2019-09-25 09:25:46 | [diff] [blame] | 630 | void WaitForNoUpdate() { receiver_.FlushForTesting(); } |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 631 | |
Julie Jeongeun Kim | 8ab180f7 | 2019-09-25 09:25:46 | [diff] [blame] | 632 | mojo::ReceiverSet<mojom::VisitedLinkNotificationSink>& binding() { |
| 633 | return receiver_; |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 634 | } |
| 635 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 636 | void NotifyUpdate() { |
| 637 | if (!quit_closure_.is_null()) |
Daniel Cheng | 83563bc | 2019-04-30 23:50:24 | [diff] [blame] | 638 | std::move(quit_closure_).Run(); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void UpdateVisitedLinks( |
Ken Rockot | aeb7eca | 2018-03-30 16:40:09 | [diff] [blame] | 642 | base::ReadOnlySharedMemoryRegion table_region) override { |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 643 | new_table_count_++; |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 644 | NotifyUpdate(); |
| 645 | } |
| 646 | |
| 647 | void AddVisitedLinks(const std::vector<uint64_t>& link_hashes) override { |
| 648 | add_count_ += link_hashes.size(); |
| 649 | add_event_count_++; |
| 650 | NotifyUpdate(); |
| 651 | } |
| 652 | |
| 653 | void ResetVisitedLinks(bool invalidate_cached_hashes) override { |
| 654 | if (invalidate_cached_hashes) |
| 655 | completely_reset_event_count_++; |
| 656 | else |
| 657 | reset_event_count_++; |
| 658 | NotifyUpdate(); |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 659 | } |
| 660 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 661 | int add_count() const { return add_count_; } |
| 662 | int add_event_count() const { return add_event_count_; } |
| 663 | int reset_event_count() const { return reset_event_count_; } |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 664 | int completely_reset_event_count() const { |
| 665 | return completely_reset_event_count_; |
| 666 | } |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 667 | int new_table_count() const { return new_table_count_; } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 668 | |
| 669 | private: |
| 670 | int add_count_; |
| 671 | int add_event_count_; |
| 672 | int reset_event_count_; |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 673 | int completely_reset_event_count_; |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 674 | int new_table_count_; |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 675 | |
Ken Rockot | 4a16adb | 2019-12-17 07:31:42 | [diff] [blame] | 676 | base::OnceClosure quit_closure_; |
Julie Jeongeun Kim | 8ab180f7 | 2019-09-25 09:25:46 | [diff] [blame] | 677 | mojo::ReceiverSet<mojom::VisitedLinkNotificationSink> receiver_; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 678 | }; |
| 679 | |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 680 | // Stub out as little as possible, borrowing from RenderProcessHost. |
[email protected] | b6a2f8de | 2012-01-31 17:28:49 | [diff] [blame] | 681 | class VisitRelayingRenderProcessHost : public MockRenderProcessHost { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 682 | public: |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 683 | explicit VisitRelayingRenderProcessHost( |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 684 | content::BrowserContext* browser_context, |
| 685 | VisitCountingContext* context) |
Bo Liu | eb218ee | 2018-03-21 20:24:35 | [diff] [blame] | 686 | : MockRenderProcessHost(browser_context) { |
Ken Rockot | 4a16adb | 2019-12-17 07:31:42 | [diff] [blame] | 687 | OverrideBinderForTesting(mojom::VisitedLinkNotificationSink::Name_, |
| 688 | base::BindRepeating(&VisitCountingContext::Bind, |
| 689 | base::Unretained(context))); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 690 | } |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 691 | |
| 692 | VisitRelayingRenderProcessHost(const VisitRelayingRenderProcessHost&) = |
| 693 | delete; |
| 694 | VisitRelayingRenderProcessHost& operator=( |
| 695 | const VisitRelayingRenderProcessHost&) = delete; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | class VisitedLinkRenderProcessHostFactory |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 699 | : public content::RenderProcessHostFactory { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 700 | public: |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 701 | VisitedLinkRenderProcessHostFactory() : context_(new VisitCountingContext) {} |
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 702 | |
| 703 | VisitedLinkRenderProcessHostFactory( |
| 704 | const VisitedLinkRenderProcessHostFactory&) = delete; |
| 705 | VisitedLinkRenderProcessHostFactory& operator=( |
| 706 | const VisitedLinkRenderProcessHostFactory&) = delete; |
| 707 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 708 | content::RenderProcessHost* CreateRenderProcessHost( |
Matt Falkenhagen | 24abf782 | 2017-12-15 23:55:11 | [diff] [blame] | 709 | content::BrowserContext* browser_context, |
Lucas Furukawa Gadani | 0a70be9 | 2019-06-28 17:31:26 | [diff] [blame] | 710 | content::SiteInstance* site_instance) override { |
Dave Tapuska | 65a0487 | 2020-11-12 18:12:34 | [diff] [blame] | 711 | auto rph = std::make_unique<VisitRelayingRenderProcessHost>(browser_context, |
| 712 | context_.get()); |
| 713 | content::RenderProcessHost* result = rph.get(); |
| 714 | processes_.push_back(std::move(rph)); |
| 715 | return result; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 716 | } |
| 717 | |
Jonathan Ross | 08ad289 | 2023-03-14 20:51:33 | [diff] [blame] | 718 | // RenderProcessHostImpl::OnProcessLaunched only notifies once the child |
| 719 | // process has launched. This is after RenderWidgetHost has been created. We |
| 720 | // will notify at the end of SetUp. |
| 721 | void NotifyProcessLaunced() { |
| 722 | for (auto& rph : processes_) { |
| 723 | creation_observer_->OnRenderProcessHostCreated(rph.get()); |
| 724 | } |
| 725 | } |
| 726 | |
dan sinclair | 9b5e027 | 2022-03-01 14:49:12 | [diff] [blame] | 727 | void SetRenderProcessHostCreationObserver( |
| 728 | content::RenderProcessHostCreationObserver* observer) { |
| 729 | creation_observer_ = observer; |
| 730 | } |
| 731 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 732 | VisitCountingContext* context() { return context_.get(); } |
| 733 | |
Dave Tapuska | 65a0487 | 2020-11-12 18:12:34 | [diff] [blame] | 734 | void DeleteRenderProcessHosts() { processes_.clear(); } |
| 735 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 736 | private: |
Arthur Sonzogni | e98d214 | 2023-06-01 15:02:25 | [diff] [blame] | 737 | raw_ptr<content::RenderProcessHostCreationObserver, DanglingUntriaged> |
| 738 | creation_observer_ = nullptr; |
dan sinclair | 9b5e027 | 2022-03-01 14:49:12 | [diff] [blame] | 739 | |
Dave Tapuska | 65a0487 | 2020-11-12 18:12:34 | [diff] [blame] | 740 | std::list<std::unique_ptr<VisitRelayingRenderProcessHost>> processes_; |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 741 | std::unique_ptr<VisitCountingContext> context_; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 742 | }; |
| 743 | |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 744 | class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 745 | public: |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 746 | void SetUp() override { |
[email protected] | 9a46725f | 2012-10-24 18:25:48 | [diff] [blame] | 747 | SetRenderProcessHostFactory(&vc_rph_factory_); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 748 | content::RenderViewHostTestHarness::SetUp(); |
Jonathan Ross | 08ad289 | 2023-03-14 20:51:33 | [diff] [blame] | 749 | vc_rph_factory_.NotifyProcessLaunced(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 750 | } |
| 751 | |
wkorman | 1c17345b | 2015-11-12 03:02:42 | [diff] [blame] | 752 | void TearDown() override { |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 753 | // Explicitly destroy the writer before proceeding with the rest |
wkorman | 1c17345b | 2015-11-12 03:02:42 | [diff] [blame] | 754 | // of teardown because it posts a task to close a file handle, and |
| 755 | // we need to make sure we've finished all file related work |
| 756 | // before our superclass sets about destroying the scoped temp |
| 757 | // directory. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 758 | writer_.reset(); |
Dave Tapuska | 65a0487 | 2020-11-12 18:12:34 | [diff] [blame] | 759 | DeleteContents(); |
| 760 | vc_rph_factory_.DeleteRenderProcessHosts(); |
wkorman | 1c17345b | 2015-11-12 03:02:42 | [diff] [blame] | 761 | RenderViewHostTestHarness::TearDown(); |
| 762 | } |
| 763 | |
Sylvain Defresne | 330d05c | 2019-08-07 12:36:37 | [diff] [blame] | 764 | std::unique_ptr<content::BrowserContext> CreateBrowserContext() override { |
| 765 | auto context = std::make_unique<content::TestBrowserContext>(); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 766 | CreateVisitedLinkWriter(context.get()); |
dan sinclair | 9b5e027 | 2022-03-01 14:49:12 | [diff] [blame] | 767 | vc_rph_factory_.SetRenderProcessHostCreationObserver( |
| 768 | static_cast<VisitedLinkEventListener*>(writer_->GetListener())); |
[email protected] | cc99c9f | 2013-07-15 12:26:09 | [diff] [blame] | 769 | return context; |
| 770 | } |
| 771 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 772 | VisitCountingContext* context() { return vc_rph_factory_.context(); } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 773 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 774 | VisitedLinkWriter* writer() const { return writer_.get(); } |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 775 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 776 | protected: |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 777 | void CreateVisitedLinkWriter(content::BrowserContext* browser_context) { |
Peter Boström | 665b49d | 2021-04-05 19:29:58 | [diff] [blame] | 778 | timer_ = std::make_unique<base::MockOneShotTimer>(); |
| 779 | writer_ = |
| 780 | std::make_unique<VisitedLinkWriter>(browser_context, &delegate_, true); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 781 | static_cast<VisitedLinkEventListener*>(writer_->GetListener()) |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 782 | ->SetCoalesceTimerForTest(timer_.get()); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 783 | writer_->Init(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 784 | } |
| 785 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 786 | VisitedLinkRenderProcessHostFactory vc_rph_factory_; |
| 787 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 788 | TestVisitedLinkDelegate delegate_; |
tzik | 57c66e3 | 2018-07-02 08:02:57 | [diff] [blame] | 789 | std::unique_ptr<base::MockOneShotTimer> timer_; |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 790 | std::unique_ptr<VisitedLinkWriter> writer_; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 791 | }; |
| 792 | |
wkorman | 1c17345b | 2015-11-12 03:02:42 | [diff] [blame] | 793 | TEST_F(VisitedLinkEventsTest, Coalescence) { |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 794 | // Waiting complete rebuild the table. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 795 | content::RunAllTasksUntilIdle(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 796 | |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 797 | // After rebuild table expect reset event. |
| 798 | EXPECT_EQ(1, context()->reset_event_count()); |
| 799 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 800 | // add some URLs to writer. |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 801 | // Add a few URLs. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 802 | writer()->AddURL(GURL("http://acidtests.org/")); |
| 803 | writer()->AddURL(GURL("http://google.com/")); |
| 804 | writer()->AddURL(GURL("http://chromium.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 805 | // Just for kicks, add a duplicate URL. This shouldn't increase the resulting |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 806 | writer()->AddURL(GURL("http://acidtests.org/")); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 807 | ASSERT_TRUE(timer_->IsRunning()); |
| 808 | timer_->Fire(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 809 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 810 | context()->WaitForUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 811 | |
| 812 | // We now should have 3 entries added in 1 event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 813 | EXPECT_EQ(3, context()->add_count()); |
| 814 | EXPECT_EQ(1, context()->add_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 815 | |
| 816 | // Test whether the coalescing continues by adding a few more URLs. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 817 | writer()->AddURL(GURL("http://google.com/chrome/")); |
| 818 | writer()->AddURL(GURL("http://webkit.org/")); |
| 819 | writer()->AddURL(GURL("http://acid3.acidtests.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 820 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 821 | ASSERT_TRUE(timer_->IsRunning()); |
| 822 | timer_->Fire(); |
| 823 | context()->WaitForUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 824 | |
| 825 | // We should have 6 entries added in 2 events. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 826 | EXPECT_EQ(6, context()->add_count()); |
| 827 | EXPECT_EQ(2, context()->add_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 828 | |
| 829 | // Test whether duplicate entries produce add events. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 830 | writer()->AddURL(GURL("http://acidtests.org/")); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 831 | EXPECT_FALSE(timer_->IsRunning()); |
| 832 | context()->WaitForNoUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 833 | |
| 834 | // We should have no change in results. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 835 | EXPECT_EQ(6, context()->add_count()); |
| 836 | EXPECT_EQ(2, context()->add_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 837 | |
| 838 | // Ensure that the coalescing does not resume after resetting. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 839 | writer()->AddURL(GURL("http://build.chromium.org/")); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 840 | EXPECT_TRUE(timer_->IsRunning()); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 841 | writer()->DeleteAllURLs(); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 842 | EXPECT_FALSE(timer_->IsRunning()); |
| 843 | context()->WaitForNoUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 844 | |
| 845 | // We should have no change in results except for one new reset event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 846 | EXPECT_EQ(6, context()->add_count()); |
| 847 | EXPECT_EQ(2, context()->add_event_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 848 | EXPECT_EQ(2, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 849 | } |
| 850 | |
[email protected] | 7fa7dd5 | 2011-04-24 01:09:42 | [diff] [blame] | 851 | TEST_F(VisitedLinkEventsTest, Basics) { |
Aaron Colwell | 4056352 | 2021-02-18 17:14:35 | [diff] [blame] | 852 | RenderViewHostTester::For(rvh())->CreateTestRenderView(); |
[email protected] | 6e8d6464 | 2009-08-10 15:27:19 | [diff] [blame] | 853 | |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 854 | // Waiting complete rebuild the table. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 855 | content::RunAllTasksUntilIdle(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 856 | |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 857 | // After rebuild table expect reset event. |
| 858 | EXPECT_EQ(1, context()->reset_event_count()); |
| 859 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 860 | // Add a few URLs. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 861 | writer()->AddURL(GURL("http://acidtests.org/")); |
| 862 | writer()->AddURL(GURL("http://google.com/")); |
| 863 | writer()->AddURL(GURL("http://chromium.org/")); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 864 | ASSERT_TRUE(timer_->IsRunning()); |
| 865 | timer_->Fire(); |
| 866 | context()->WaitForUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 867 | |
| 868 | // We now should have 1 add event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 869 | EXPECT_EQ(1, context()->add_event_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 870 | EXPECT_EQ(1, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 871 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 872 | writer()->DeleteAllURLs(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 873 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 874 | EXPECT_FALSE(timer_->IsRunning()); |
| 875 | context()->WaitForNoUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 876 | |
| 877 | // We should have no change in add results, plus one new reset event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 878 | EXPECT_EQ(1, context()->add_event_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 879 | EXPECT_EQ(2, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 880 | } |
| 881 | |
[email protected] | 7fa7dd5 | 2011-04-24 01:09:42 | [diff] [blame] | 882 | TEST_F(VisitedLinkEventsTest, TabVisibility) { |
Aaron Colwell | 4056352 | 2021-02-18 17:14:35 | [diff] [blame] | 883 | RenderViewHostTester::For(rvh())->CreateTestRenderView(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 884 | |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 885 | // Waiting complete rebuild the table. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 886 | content::RunAllTasksUntilIdle(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 887 | |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 888 | // After rebuild table expect reset event. |
| 889 | EXPECT_EQ(1, context()->reset_event_count()); |
| 890 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 891 | // Simulate tab becoming inactive. |
bsep | f3b00fa5 | 2017-02-11 00:06:38 | [diff] [blame] | 892 | RenderViewHostTester::For(rvh())->SimulateWasHidden(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 893 | |
| 894 | // Add a few URLs. |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 895 | writer()->AddURL(GURL("http://acidtests.org/")); |
| 896 | writer()->AddURL(GURL("http://google.com/")); |
| 897 | writer()->AddURL(GURL("http://chromium.org/")); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 898 | ASSERT_TRUE(timer_->IsRunning()); |
| 899 | timer_->Fire(); |
| 900 | context()->WaitForNoUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 901 | |
| 902 | // We shouldn't have any events. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 903 | EXPECT_EQ(0, context()->add_event_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 904 | EXPECT_EQ(1, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 905 | |
| 906 | // Simulate the tab becoming active. |
bsep | f3b00fa5 | 2017-02-11 00:06:38 | [diff] [blame] | 907 | RenderViewHostTester::For(rvh())->SimulateWasShown(); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 908 | context()->WaitForUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 909 | |
| 910 | // We should now have 3 add events, still no reset events. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 911 | EXPECT_EQ(1, context()->add_event_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 912 | EXPECT_EQ(1, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 913 | |
| 914 | // Deactivate the tab again. |
bsep | f3b00fa5 | 2017-02-11 00:06:38 | [diff] [blame] | 915 | RenderViewHostTester::For(rvh())->SimulateWasHidden(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 916 | |
| 917 | // Add a bunch of URLs (over 50) to exhaust the link event buffer. |
| 918 | for (int i = 0; i < 100; i++) |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 919 | writer()->AddURL(TestURL(i)); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 920 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 921 | ASSERT_TRUE(timer_->IsRunning()); |
| 922 | timer_->Fire(); |
| 923 | context()->WaitForNoUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 924 | |
| 925 | // Again, no change in events until tab is active. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 926 | EXPECT_EQ(1, context()->add_event_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 927 | EXPECT_EQ(1, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 928 | |
| 929 | // Activate the tab. |
bsep | f3b00fa5 | 2017-02-11 00:06:38 | [diff] [blame] | 930 | RenderViewHostTester::For(rvh())->SimulateWasShown(); |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 931 | EXPECT_FALSE(timer_->IsRunning()); |
| 932 | context()->WaitForUpdate(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 933 | |
| 934 | // We should have only one more reset event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 935 | EXPECT_EQ(1, context()->add_event_count()); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 936 | EXPECT_EQ(2, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 937 | } |
[email protected] | 1f371fa | 2013-01-23 00:35:14 | [diff] [blame] | 938 | |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 939 | // Tests that VisitedLink ignores renderer process creation notification for a |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 940 | // different context. |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 941 | TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) { |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 942 | content::TestBrowserContext different_context; |
| 943 | VisitCountingContext counting_context; |
dan sinclair | 9b5e027 | 2022-03-01 14:49:12 | [diff] [blame] | 944 | // There are two render process hosts in play with this test. The primary |
| 945 | // one is where the observer callback (done below) will be received |
| 946 | // and don't need an observer for the other process host as it isn't |
| 947 | // needed in the test. |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 948 | VisitRelayingRenderProcessHost different_process_host(&different_context, |
| 949 | &counting_context); |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 950 | |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 951 | size_t old_size = counting_context.binding().size(); |
dan sinclair | 9b5e027 | 2022-03-01 14:49:12 | [diff] [blame] | 952 | |
| 953 | static_cast<VisitedLinkEventListener*>(writer()->GetListener()) |
| 954 | ->OnRenderProcessHostCreated(&different_process_host); |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 955 | size_t new_size = counting_context.binding().size(); |
| 956 | EXPECT_EQ(old_size, new_size); |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 957 | } |
| 958 | |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 959 | class VisitedLinkCompletelyResetEventTest : public VisitedLinkEventsTest { |
| 960 | public: |
Sylvain Defresne | 330d05c | 2019-08-07 12:36:37 | [diff] [blame] | 961 | std::unique_ptr<content::BrowserContext> CreateBrowserContext() override { |
| 962 | auto context = std::make_unique<content::TestBrowserContext>(); |
| 963 | CreateVisitedLinkFile(context.get()); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 964 | CreateVisitedLinkWriter(context.get()); |
dan sinclair | 9b5e027 | 2022-03-01 14:49:12 | [diff] [blame] | 965 | vc_rph_factory_.SetRenderProcessHostCreationObserver( |
| 966 | static_cast<VisitedLinkEventListener*>(writer_->GetListener())); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 967 | return context; |
| 968 | } |
| 969 | |
| 970 | void CreateVisitedLinkFile(content::BrowserContext* browser_context) { |
| 971 | base::FilePath visited_file = |
| 972 | browser_context->GetPath().Append(FILE_PATH_LITERAL("Visited Links")); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 973 | std::unique_ptr<VisitedLinkWriter> writer( |
| 974 | new VisitedLinkWriter(new TrackingVisitedLinkEventListener(), |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 975 | &delegate_, true, true, visited_file, 0)); |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 976 | writer->Init(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 977 | // Waiting complete create the table. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 978 | content::RunAllTasksUntilIdle(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 979 | |
Robert Sesek | ffeae8e | 2019-11-26 17:10:15 | [diff] [blame] | 980 | writer.reset(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 981 | // Wait for all pending file I/O to be completed. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 982 | content::RunAllTasksUntilIdle(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 983 | } |
| 984 | }; |
| 985 | |
| 986 | TEST_F(VisitedLinkCompletelyResetEventTest, LoadTable) { |
| 987 | // Waiting complete loading the table. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 988 | content::RunAllTasksUntilIdle(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 989 | |
sammc | aa22276 | 2016-09-21 07:30:42 | [diff] [blame] | 990 | context()->binding().FlushForTesting(); |
sath | af37203d | 2015-12-15 15:10:42 | [diff] [blame] | 991 | |
| 992 | // After load table expect completely reset event. |
| 993 | EXPECT_EQ(1, context()->completely_reset_event_count()); |
| 994 | } |
| 995 | |
[email protected] | ab3eaeed | 2013-05-17 00:18:44 | [diff] [blame] | 996 | } // namespace visitedlink |