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