blob: 9a51eff4bf0a58c7bc6cd32a127d3c91e26afc02 [file] [log] [blame]
[email protected]02798a982012-01-27 00:45:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
avi5dd91f82015-12-25 22:30:465#include <stddef.h>
6#include <stdint.h>
7
initial.commit09911bf2008-07-26 23:55:298#include <cstdio>
dcheng3f767dc32016-04-25 22:54:229#include <memory>
[email protected]c38831a12011-10-28 12:44:4910#include <string>
11#include <vector>
initial.commit09911bf2008-07-26 23:55:2912
Sebastien Marchand53801a32019-01-25 16:26:1113#include "base/bind.h"
thestig819adcc82014-09-10 22:24:5314#include "base/files/file_util.h"
skyostilb0daa012015-06-02 19:03:4815#include "base/location.h"
avi5dd91f82015-12-25 22:30:4616#include "base/macros.h"
[email protected]27730162013-07-23 14:51:5517#include "base/process/process_handle.h"
[email protected]ec04d3f2013-06-06 21:31:3918#include "base/run_loop.h"
skyostilb0daa012015-06-02 19:03:4819#include "base/single_thread_task_runner.h"
[email protected]c72674b2013-06-11 04:16:4320#include "base/strings/string_util.h"
gab7966d312016-05-11 20:35:0121#include "base/threading/thread_task_runner_handle.h"
[email protected]bbd8da92013-06-28 02:12:2022#include "base/time/time.h"
sammcaa222762016-09-21 07:30:4223#include "base/timer/mock_timer.h"
[email protected]1f371fa2013-01-23 00:35:1424#include "components/visitedlink/browser/visitedlink_delegate.h"
25#include "components/visitedlink/browser/visitedlink_event_listener.h"
26#include "components/visitedlink/browser/visitedlink_master.h"
sammcaa222762016-09-21 07:30:4227#include "components/visitedlink/common/visitedlink.mojom.h"
[email protected]1f371fa2013-01-23 00:35:1428#include "components/visitedlink/renderer/visitedlink_slave.h"
[email protected]ec04d3f2013-06-06 21:31:3929#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0730#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1631#include "content/public/browser/notification_types.h"
Gabriel Charettec7108742019-08-23 03:31:4032#include "content/public/test/browser_task_environment.h"
[email protected]08a932d52012-06-03 21:42:1233#include "content/public/test/mock_render_process_host.h"
[email protected]4c3d9d62013-01-09 22:37:2034#include "content/public/test/test_browser_context.h"
[email protected]b1e3f202012-06-04 14:45:5035#include "content/public/test/test_renderer_host.h"
mtomaszf7d99a5c2014-09-15 16:23:4636#include "content/public/test/test_utils.h"
Julie Jeongeun Kim8ab180f72019-09-25 09:25:4637#include "mojo/public/cpp/bindings/pending_receiver.h"
38#include "mojo/public/cpp/bindings/receiver_set.h"
initial.commit09911bf2008-07-26 23:55:2939#include "testing/gtest/include/gtest/gtest.h"
[email protected]e3b599e2013-07-05 07:15:1740#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2941
[email protected]631bb742011-11-02 11:29:3942using content::BrowserThread;
[email protected]eddd886702012-03-16 14:53:2343using content::MockRenderProcessHost;
[email protected]c0257382012-03-12 20:15:3444using content::RenderViewHostTester;
[email protected]631bb742011-11-02 11:29:3945
Matt Falkenhagen24abf7822017-12-15 23:55:1146namespace content {
47class SiteInstance;
48}
49
[email protected]ab3eaeed2013-05-17 00:18:4450namespace visitedlink {
[email protected]1f371fa2013-01-23 00:35:1451
initial.commit09911bf2008-07-26 23:55:2952namespace {
53
[email protected]4c3d9d62013-01-09 22:37:2054typedef std::vector<GURL> URLs;
55
initial.commit09911bf2008-07-26 23:55:2956// a nice long URL that we can append numbers to to get new URLs
57const char g_test_prefix[] =
58 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq=";
59const int g_test_count = 1000;
60
61// Returns a test URL for index |i|
62GURL TestURL(int i) {
[email protected]7d3cbc92013-03-18 22:33:0463 return GURL(base::StringPrintf("%s%d", g_test_prefix, i));
initial.commit09911bf2008-07-26 23:55:2964}
65
[email protected]4c3d9d62013-01-09 22:37:2066std::vector<VisitedLinkSlave*> g_slaves;
67
68class TestVisitedLinkDelegate : public VisitedLinkDelegate {
69 public:
dcheng00ea022b2014-10-21 11:24:5670 void RebuildTable(const scoped_refptr<URLEnumerator>& enumerator) override;
[email protected]4c3d9d62013-01-09 22:37:2071
72 void AddURLForRebuild(const GURL& url);
73
74 private:
[email protected]4c3d9d62013-01-09 22:37:2075 URLs rebuild_urls_;
76};
77
[email protected]4c3d9d62013-01-09 22:37:2078void TestVisitedLinkDelegate::RebuildTable(
79 const scoped_refptr<URLEnumerator>& enumerator) {
80 for (URLs::const_iterator itr = rebuild_urls_.begin();
81 itr != rebuild_urls_.end();
82 ++itr)
83 enumerator->OnURL(*itr);
84 enumerator->OnComplete(true);
85}
86
87void TestVisitedLinkDelegate::AddURLForRebuild(const GURL& url) {
88 rebuild_urls_.push_back(url);
89}
90
91class TestURLIterator : public VisitedLinkMaster::URLIterator {
92 public:
93 explicit TestURLIterator(const URLs& urls);
94
dcheng00ea022b2014-10-21 11:24:5695 const GURL& NextURL() override;
96 bool HasNextURL() const override;
[email protected]4c3d9d62013-01-09 22:37:2097
98 private:
99 URLs::const_iterator iterator_;
100 URLs::const_iterator end_;
101};
102
103TestURLIterator::TestURLIterator(const URLs& urls)
104 : iterator_(urls.begin()),
105 end_(urls.end()) {
106}
107
108const GURL& TestURLIterator::NextURL() {
109 return *(iterator_++);
110}
111
112bool TestURLIterator::HasNextURL() const {
113 return iterator_ != end_;
114}
[email protected]dba45e32013-01-04 20:27:15115
[email protected]3e90d4a2009-07-03 17:38:39116} // namespace
117
118class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener {
119 public:
120 TrackingVisitedLinkEventListener()
121 : reset_count_(0),
sathaf37203d2015-12-15 15:10:42122 completely_reset_count_(0),
[email protected]3e90d4a2009-07-03 17:38:39123 add_count_(0) {}
124
Ken Rockotaeb7eca2018-03-30 16:40:09125 void NewTable(base::ReadOnlySharedMemoryRegion* table_region) override {
126 if (table_region->IsValid()) {
[email protected]3e90d4a2009-07-03 17:38:39127 for (std::vector<VisitedLinkSlave>::size_type i = 0;
128 i < g_slaves.size(); i++) {
Ken Rockotaeb7eca2018-03-30 16:40:09129 g_slaves[i]->UpdateVisitedLinks(table_region->Duplicate());
[email protected]3e90d4a2009-07-03 17:38:39130 }
initial.commit09911bf2008-07-26 23:55:29131 }
132 }
dcheng00ea022b2014-10-21 11:24:56133 void Add(VisitedLinkCommon::Fingerprint) override { add_count_++; }
sathaf37203d2015-12-15 15:10:42134 void Reset(bool invalidate_hashes) override {
135 if (invalidate_hashes)
136 completely_reset_count_++;
137 else
138 reset_count_++;
139 }
initial.commit09911bf2008-07-26 23:55:29140
[email protected]3e90d4a2009-07-03 17:38:39141 void SetUp() {
142 reset_count_ = 0;
143 add_count_ = 0;
144 }
145
146 int reset_count() const { return reset_count_; }
sathaf37203d2015-12-15 15:10:42147 int completely_reset_count() { return completely_reset_count_; }
[email protected]3e90d4a2009-07-03 17:38:39148 int add_count() const { return add_count_; }
149
150 private:
151 int reset_count_;
sathaf37203d2015-12-15 15:10:42152 int completely_reset_count_;
[email protected]3e90d4a2009-07-03 17:38:39153 int add_count_;
154};
[email protected]c2c998c2009-01-27 19:08:39155
[email protected]583844c2011-08-27 00:38:35156class VisitedLinkTest : public testing::Test {
initial.commit09911bf2008-07-26 23:55:29157 protected:
initial.commit09911bf2008-07-26 23:55:29158 // Initializes the visited link objects. Pass in the size that you want a
159 // freshly created table to be. 0 means use the default.
160 //
161 // |suppress_rebuild| is set when we're not testing rebuilding, see
162 // the VisitedLinkMaster constructor.
sathaf37203d2015-12-15 15:10:42163 //
164 // |wait_for_io_complete| wait for result of async loading.
165 bool InitVisited(int initial_size,
166 bool suppress_rebuild,
167 bool wait_for_io_complete) {
initial.commit09911bf2008-07-26 23:55:29168 // Initialize the visited link system.
[email protected]b42e6d82012-11-02 02:44:56169 master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(),
[email protected]4c3d9d62013-01-09 22:37:20170 &delegate_,
[email protected]bff706b2013-01-25 00:15:01171 true,
[email protected]3e90d4a2009-07-03 17:38:39172 suppress_rebuild, visited_file_,
173 initial_size));
sathaf37203d2015-12-15 15:10:42174 bool result = master_->Init();
175 if (result && wait_for_io_complete) {
176 // Wait for all pending file I/O to be completed.
Gabriel Charette01507a22017-09-27 21:30:08177 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42178 }
179 return result;
initial.commit09911bf2008-07-26 23:55:29180 }
181
182 // May be called multiple times (some tests will do this to clear things,
183 // and TearDown will do this to make sure eveything is shiny before quitting.
184 void ClearDB() {
Zinovy Nise8482f52018-05-29 06:08:44185 master_.reset(nullptr);
initial.commit09911bf2008-07-26 23:55:29186
[email protected]3189013e2012-01-19 04:11:57187 // Wait for all pending file I/O to be completed.
Gabriel Charette01507a22017-09-27 21:30:08188 content::RunAllTasksUntilIdle();
initial.commit09911bf2008-07-26 23:55:29189 }
190
191 // Loads the database from disk and makes sure that the same URLs are present
192 // as were generated by TestIO_Create(). This also checks the URLs with a
193 // slave to make sure it reads the data properly.
194 void Reload() {
195 // Clean up after our caller, who may have left the database open.
196 ClearDB();
197
sathaf37203d2015-12-15 15:10:42198 ASSERT_TRUE(InitVisited(0, true, true));
199
initial.commit09911bf2008-07-26 23:55:29200 master_->DebugValidate();
201
202 // check that the table has the proper number of entries
203 int used_count = master_->GetUsedCount();
204 ASSERT_EQ(used_count, g_test_count);
205
206 // Create a slave database.
207 VisitedLinkSlave slave;
Ken Rockotaeb7eca2018-03-30 16:40:09208 slave.UpdateVisitedLinks(master_->mapped_table_memory().region.Duplicate());
initial.commit09911bf2008-07-26 23:55:29209 g_slaves.push_back(&slave);
210
211 bool found;
212 for (int i = 0; i < g_test_count; i++) {
213 GURL cur = TestURL(i);
214 found = master_->IsVisited(cur);
215 EXPECT_TRUE(found) << "URL " << i << "not found in master.";
216
217 found = slave.IsVisited(cur);
218 EXPECT_TRUE(found) << "URL " << i << "not found in slave.";
219 }
220
221 // test some random URL so we know that it returns false sometimes too
222 found = master_->IsVisited(GURL("http://unfound.site/"));
223 ASSERT_FALSE(found);
224 found = slave.IsVisited(GURL("http://unfound.site/"));
225 ASSERT_FALSE(found);
226
227 master_->DebugValidate();
228
229 g_slaves.clear();
230 }
231
232 // testing::Test
dcheng30a1b1542014-10-29 21:27:50233 void SetUp() override {
[email protected]f708ed12010-09-23 12:18:34234 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
235
vabr8c498ea42016-09-15 12:41:58236 history_dir_ = temp_dir_.GetPath().AppendASCII("VisitedLinkTest");
[email protected]426d1c92013-12-03 20:08:54237 ASSERT_TRUE(base::CreateDirectory(history_dir_));
initial.commit09911bf2008-07-26 23:55:29238
[email protected]c2c998c2009-01-27 19:08:39239 visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks"));
initial.commit09911bf2008-07-26 23:55:29240 }
241
dcheng30a1b1542014-10-29 21:27:50242 void TearDown() override { ClearDB(); }
[email protected]f0a51fb52009-03-05 12:46:38243
[email protected]ea1a3f62012-11-16 20:34:23244 base::ScopedTempDir temp_dir_;
[email protected]f708ed12010-09-23 12:18:34245
initial.commit09911bf2008-07-26 23:55:29246 // Filenames for the services;
[email protected]9e275712013-02-10 19:20:14247 base::FilePath history_dir_;
248 base::FilePath visited_file_;
initial.commit09911bf2008-07-26 23:55:29249
dcheng3f767dc32016-04-25 22:54:22250 std::unique_ptr<VisitedLinkMaster> master_;
[email protected]4c3d9d62013-01-09 22:37:20251 TestVisitedLinkDelegate delegate_;
Gabriel Charette798fde72019-08-20 22:24:04252 content::BrowserTaskEnvironment task_environment_;
initial.commit09911bf2008-07-26 23:55:29253};
254
initial.commit09911bf2008-07-26 23:55:29255// This test creates and reads some databases to make sure the data is
256// preserved throughout those operations.
257TEST_F(VisitedLinkTest, DatabaseIO) {
sathaf37203d2015-12-15 15:10:42258 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29259
260 for (int i = 0; i < g_test_count; i++)
261 master_->AddURL(TestURL(i));
262
263 // Test that the database was written properly
264 Reload();
265}
266
267// Checks that we can delete things properly when there are collisions.
268TEST_F(VisitedLinkTest, Delete) {
avi5dd91f82015-12-25 22:30:46269 static const int32_t kInitialSize = 17;
sathaf37203d2015-12-15 15:10:42270 ASSERT_TRUE(InitVisited(kInitialSize, true, true));
initial.commit09911bf2008-07-26 23:55:29271
272 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the
273 // same value.
[email protected]c2c998c2009-01-27 19:08:39274 const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14;
275 const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14;
276 const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14;
277 const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14;
278 const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14;
[email protected]f8c42c92009-07-04 17:22:08279 master_->AddFingerprint(kFingerprint0, false); // @14
280 master_->AddFingerprint(kFingerprint1, false); // @15
281 master_->AddFingerprint(kFingerprint2, false); // @16
282 master_->AddFingerprint(kFingerprint3, false); // @0
283 master_->AddFingerprint(kFingerprint4, false); // @1
initial.commit09911bf2008-07-26 23:55:29284
285 // Deleting 14 should move the next value up one slot (we do not specify an
286 // order).
287 EXPECT_EQ(kFingerprint3, master_->hash_table_[0]);
288 master_->DeleteFingerprint(kFingerprint3, false);
[email protected]c2c998c2009-01-27 19:08:39289 VisitedLinkCommon::Fingerprint zero_fingerprint = 0;
290 EXPECT_EQ(zero_fingerprint, master_->hash_table_[1]);
291 EXPECT_NE(zero_fingerprint, master_->hash_table_[0]);
initial.commit09911bf2008-07-26 23:55:29292
293 // Deleting the other four should leave the table empty.
294 master_->DeleteFingerprint(kFingerprint0, false);
295 master_->DeleteFingerprint(kFingerprint1, false);
296 master_->DeleteFingerprint(kFingerprint2, false);
297 master_->DeleteFingerprint(kFingerprint4, false);
298
299 EXPECT_EQ(0, master_->used_items_);
300 for (int i = 0; i < kInitialSize; i++)
[email protected]c2c998c2009-01-27 19:08:39301 EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) <<
302 "Hash table has values in it.";
initial.commit09911bf2008-07-26 23:55:29303}
304
305// When we delete more than kBigDeleteThreshold we trigger different behavior
306// where the entire file is rewritten.
307TEST_F(VisitedLinkTest, BigDelete) {
sathaf37203d2015-12-15 15:10:42308 ASSERT_TRUE(InitVisited(16381, true, true));
initial.commit09911bf2008-07-26 23:55:29309
310 // Add the base set of URLs that won't be deleted.
311 // Reload() will test for these.
avi5dd91f82015-12-25 22:30:46312 for (int32_t i = 0; i < g_test_count; i++)
initial.commit09911bf2008-07-26 23:55:29313 master_->AddURL(TestURL(i));
314
315 // Add more URLs than necessary to trigger this case.
316 const int kTestDeleteCount = VisitedLinkMaster::kBigDeleteThreshold + 2;
[email protected]4c3d9d62013-01-09 22:37:20317 URLs urls_to_delete;
avi5dd91f82015-12-25 22:30:46318 for (int32_t i = g_test_count; i < g_test_count + kTestDeleteCount; i++) {
initial.commit09911bf2008-07-26 23:55:29319 GURL url(TestURL(i));
320 master_->AddURL(url);
[email protected]4c3d9d62013-01-09 22:37:20321 urls_to_delete.push_back(url);
initial.commit09911bf2008-07-26 23:55:29322 }
323
[email protected]4c3d9d62013-01-09 22:37:20324 TestURLIterator iterator(urls_to_delete);
325 master_->DeleteURLs(&iterator);
initial.commit09911bf2008-07-26 23:55:29326 master_->DebugValidate();
327
328 Reload();
329}
330
331TEST_F(VisitedLinkTest, DeleteAll) {
sathaf37203d2015-12-15 15:10:42332 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29333
334 {
335 VisitedLinkSlave slave;
Ken Rockotaeb7eca2018-03-30 16:40:09336 slave.UpdateVisitedLinks(master_->mapped_table_memory().region.Duplicate());
initial.commit09911bf2008-07-26 23:55:29337 g_slaves.push_back(&slave);
338
339 // Add the test URLs.
340 for (int i = 0; i < g_test_count; i++) {
341 master_->AddURL(TestURL(i));
342 ASSERT_EQ(i + 1, master_->GetUsedCount());
343 }
344 master_->DebugValidate();
345
346 // Make sure the slave picked up the adds.
347 for (int i = 0; i < g_test_count; i++)
348 EXPECT_TRUE(slave.IsVisited(TestURL(i)));
349
350 // Clear the table and make sure the slave picked it up.
351 master_->DeleteAllURLs();
352 EXPECT_EQ(0, master_->GetUsedCount());
353 for (int i = 0; i < g_test_count; i++) {
354 EXPECT_FALSE(master_->IsVisited(TestURL(i)));
355 EXPECT_FALSE(slave.IsVisited(TestURL(i)));
356 }
357
358 // Close the database.
359 g_slaves.clear();
360 ClearDB();
361 }
362
363 // Reopen and validate.
sathaf37203d2015-12-15 15:10:42364 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29365 master_->DebugValidate();
366 EXPECT_EQ(0, master_->GetUsedCount());
367 for (int i = 0; i < g_test_count; i++)
368 EXPECT_FALSE(master_->IsVisited(TestURL(i)));
369}
370
371// This tests that the master correctly resizes its tables when it gets too
372// full, notifies its slaves of the change, and updates the disk.
373TEST_F(VisitedLinkTest, Resizing) {
374 // Create a very small database.
avi5dd91f82015-12-25 22:30:46375 const int32_t initial_size = 17;
sathaf37203d2015-12-15 15:10:42376 ASSERT_TRUE(InitVisited(initial_size, true, true));
initial.commit09911bf2008-07-26 23:55:29377
378 // ...and a slave
379 VisitedLinkSlave slave;
Ken Rockotaeb7eca2018-03-30 16:40:09380 slave.UpdateVisitedLinks(master_->mapped_table_memory().region.Duplicate());
initial.commit09911bf2008-07-26 23:55:29381 g_slaves.push_back(&slave);
382
avi5dd91f82015-12-25 22:30:46383 int32_t used_count = master_->GetUsedCount();
initial.commit09911bf2008-07-26 23:55:29384 ASSERT_EQ(used_count, 0);
385
386 for (int i = 0; i < g_test_count; i++) {
387 master_->AddURL(TestURL(i));
388 used_count = master_->GetUsedCount();
389 ASSERT_EQ(i + 1, used_count);
390 }
391
392 // Verify that the table got resized sufficiently.
avi5dd91f82015-12-25 22:30:46393 int32_t table_size;
initial.commit09911bf2008-07-26 23:55:29394 VisitedLinkCommon::Fingerprint* table;
395 master_->GetUsageStatistics(&table_size, &table);
396 used_count = master_->GetUsedCount();
397 ASSERT_GT(table_size, used_count);
398 ASSERT_EQ(used_count, g_test_count) <<
399 "table count doesn't match the # of things we added";
400
401 // Verify that the slave got the resize message and has the same
402 // table information.
avi5dd91f82015-12-25 22:30:46403 int32_t child_table_size;
initial.commit09911bf2008-07-26 23:55:29404 VisitedLinkCommon::Fingerprint* child_table;
405 slave.GetUsageStatistics(&child_table_size, &child_table);
406 ASSERT_EQ(table_size, child_table_size);
avi5dd91f82015-12-25 22:30:46407 for (int32_t i = 0; i < table_size; i++) {
initial.commit09911bf2008-07-26 23:55:29408 ASSERT_EQ(table[i], child_table[i]);
409 }
410
411 master_->DebugValidate();
412 g_slaves.clear();
413
414 // This tests that the file is written correctly by reading it in using
415 // a new database.
416 Reload();
417}
418
419// Tests that if the database doesn't exist, it will be rebuilt from history.
420TEST_F(VisitedLinkTest, Rebuild) {
initial.commit09911bf2008-07-26 23:55:29421 // Add half of our URLs to history. This needs to be done before we
422 // initialize the visited link DB.
423 int history_count = g_test_count / 2;
424 for (int i = 0; i < history_count; i++)
[email protected]4c3d9d62013-01-09 22:37:20425 delegate_.AddURLForRebuild(TestURL(i));
initial.commit09911bf2008-07-26 23:55:29426
427 // Initialize the visited link DB. Since the visited links file doesn't exist
428 // and we don't suppress history rebuilding, this will load from history.
sathaf37203d2015-12-15 15:10:42429 ASSERT_TRUE(InitVisited(0, false, false));
initial.commit09911bf2008-07-26 23:55:29430
431 // While the table is rebuilding, add the rest of the URLs to the visited
432 // link system. This isn't guaranteed to happen during the rebuild, so we
433 // can't be 100% sure we're testing the right thing, but in practice is.
434 // All the adds above will generally take some time queuing up on the
435 // history thread, and it will take a while to catch up to actually
436 // processing the rebuild that has queued behind it. We will generally
437 // finish adding all of the URLs before it has even found the first URL.
438 for (int i = history_count; i < g_test_count; i++)
439 master_->AddURL(TestURL(i));
440
441 // Add one more and then delete it.
442 master_->AddURL(TestURL(g_test_count));
[email protected]4c3d9d62013-01-09 22:37:20443 URLs urls_to_delete;
444 urls_to_delete.push_back(TestURL(g_test_count));
445 TestURLIterator iterator(urls_to_delete);
446 master_->DeleteURLs(&iterator);
initial.commit09911bf2008-07-26 23:55:29447
448 // Wait for the rebuild to complete. The task will terminate the message
449 // loop when the rebuild is done. There's no chance that the rebuild will
450 // complete before we set the task because the rebuild completion message
451 // is posted to the message loop; until we Run() it, rebuild can not
452 // complete.
[email protected]ec04d3f2013-06-06 21:31:39453 base::RunLoop run_loop;
454 master_->set_rebuild_complete_task(run_loop.QuitClosure());
455 run_loop.Run();
initial.commit09911bf2008-07-26 23:55:29456
457 // Test that all URLs were written to the database properly.
458 Reload();
459
460 // Make sure the extra one was *not* written (Reload won't test this).
461 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count)));
462}
[email protected]3e90d4a2009-07-03 17:38:39463
[email protected]aed132ed2009-08-19 22:44:12464// Test that importing a large number of URLs will work
465TEST_F(VisitedLinkTest, BigImport) {
sathaf37203d2015-12-15 15:10:42466 ASSERT_TRUE(InitVisited(0, false, false));
[email protected]aed132ed2009-08-19 22:44:12467
468 // Before the table rebuilds, add a large number of URLs
469 int total_count = VisitedLinkMaster::kDefaultTableSize + 10;
470 for (int i = 0; i < total_count; i++)
471 master_->AddURL(TestURL(i));
472
473 // Wait for the rebuild to complete.
[email protected]ec04d3f2013-06-06 21:31:39474 base::RunLoop run_loop;
475 master_->set_rebuild_complete_task(run_loop.QuitClosure());
476 run_loop.Run();
[email protected]aed132ed2009-08-19 22:44:12477
478 // Ensure that the right number of URLs are present
479 int used_count = master_->GetUsedCount();
480 ASSERT_EQ(used_count, total_count);
481}
482
[email protected]3e90d4a2009-07-03 17:38:39483TEST_F(VisitedLinkTest, Listener) {
sathaf37203d2015-12-15 15:10:42484 ASSERT_TRUE(InitVisited(0, true, true));
485
486 TrackingVisitedLinkEventListener* listener =
487 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener());
488
489 // Verify that VisitedLinkMaster::Listener::Reset(true) was never called when
490 // the table was created.
491 EXPECT_EQ(0, listener->completely_reset_count());
[email protected]3e90d4a2009-07-03 17:38:39492
493 // Add test URLs.
494 for (int i = 0; i < g_test_count; i++) {
495 master_->AddURL(TestURL(i));
496 ASSERT_EQ(i + 1, master_->GetUsedCount());
497 }
498
[email protected]3e90d4a2009-07-03 17:38:39499 // Delete an URL.
[email protected]4c3d9d62013-01-09 22:37:20500 URLs urls_to_delete;
501 urls_to_delete.push_back(TestURL(0));
502 TestURLIterator iterator(urls_to_delete);
503 master_->DeleteURLs(&iterator);
504
[email protected]3e90d4a2009-07-03 17:38:39505 // ... and all of the remaining ones.
506 master_->DeleteAllURLs();
507
[email protected]3e90d4a2009-07-03 17:38:39508 // Verify that VisitedLinkMaster::Listener::Add was called for each added URL.
[email protected]b42e6d82012-11-02 02:44:56509 EXPECT_EQ(g_test_count, listener->add_count());
[email protected]3e90d4a2009-07-03 17:38:39510 // Verify that VisitedLinkMaster::Listener::Reset was called both when one and
511 // all URLs are deleted.
[email protected]b42e6d82012-11-02 02:44:56512 EXPECT_EQ(2, listener->reset_count());
sathaf37203d2015-12-15 15:10:42513
514 ClearDB();
515
516 ASSERT_TRUE(InitVisited(0, true, true));
517
518 listener =
519 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener());
520 // Verify that VisitedLinkMaster::Listener::Reset(true) was called when the
521 // table was loaded.
522 EXPECT_EQ(1, listener->completely_reset_count());
[email protected]3e90d4a2009-07-03 17:38:39523}
524
sammcaa222762016-09-21 07:30:42525class VisitCountingContext : public mojom::VisitedLinkNotificationSink {
[email protected]3e90d4a2009-07-03 17:38:39526 public:
[email protected]28a66e252013-01-25 07:54:02527 VisitCountingContext()
[email protected]3e90d4a2009-07-03 17:38:39528 : add_count_(0),
529 add_event_count_(0),
[email protected]c2134fb2013-01-23 04:28:52530 reset_event_count_(0),
sathaf37203d2015-12-15 15:10:42531 completely_reset_event_count_(0),
Lukasz Anforowicz2b2699732018-04-12 18:49:10532 new_table_count_(0) {}
[email protected]3e90d4a2009-07-03 17:38:39533
sammcaa222762016-09-21 07:30:42534 void Bind(mojo::ScopedMessagePipeHandle handle) {
Julie Jeongeun Kim8ab180f72019-09-25 09:25:46535 receiver_.Add(this,
536 mojo::PendingReceiver<mojom::VisitedLinkNotificationSink>(
537 std::move(handle)));
[email protected]3e90d4a2009-07-03 17:38:39538 }
539
sammcaa222762016-09-21 07:30:42540 void WaitForUpdate() {
541 base::RunLoop run_loop;
542 quit_closure_ = run_loop.QuitClosure();
543 run_loop.Run();
[email protected]3e90d4a2009-07-03 17:38:39544 }
545
Julie Jeongeun Kim8ab180f72019-09-25 09:25:46546 void WaitForNoUpdate() { receiver_.FlushForTesting(); }
sammcaa222762016-09-21 07:30:42547
Julie Jeongeun Kim8ab180f72019-09-25 09:25:46548 mojo::ReceiverSet<mojom::VisitedLinkNotificationSink>& binding() {
549 return receiver_;
sathaf37203d2015-12-15 15:10:42550 }
551
sammcaa222762016-09-21 07:30:42552 void NotifyUpdate() {
553 if (!quit_closure_.is_null())
Daniel Cheng83563bc2019-04-30 23:50:24554 std::move(quit_closure_).Run();
sammcaa222762016-09-21 07:30:42555 }
556
557 void UpdateVisitedLinks(
Ken Rockotaeb7eca2018-03-30 16:40:09558 base::ReadOnlySharedMemoryRegion table_region) override {
[email protected]c2134fb2013-01-23 04:28:52559 new_table_count_++;
sammcaa222762016-09-21 07:30:42560 NotifyUpdate();
561 }
562
563 void AddVisitedLinks(const std::vector<uint64_t>& link_hashes) override {
564 add_count_ += link_hashes.size();
565 add_event_count_++;
566 NotifyUpdate();
567 }
568
569 void ResetVisitedLinks(bool invalidate_cached_hashes) override {
570 if (invalidate_cached_hashes)
571 completely_reset_event_count_++;
572 else
573 reset_event_count_++;
574 NotifyUpdate();
[email protected]c2134fb2013-01-23 04:28:52575 }
576
[email protected]3e90d4a2009-07-03 17:38:39577 int add_count() const { return add_count_; }
578 int add_event_count() const { return add_event_count_; }
579 int reset_event_count() const { return reset_event_count_; }
sathaf37203d2015-12-15 15:10:42580 int completely_reset_event_count() const {
581 return completely_reset_event_count_;
582 }
[email protected]c2134fb2013-01-23 04:28:52583 int new_table_count() const { return new_table_count_; }
[email protected]3e90d4a2009-07-03 17:38:39584
585 private:
586 int add_count_;
587 int add_event_count_;
588 int reset_event_count_;
sathaf37203d2015-12-15 15:10:42589 int completely_reset_event_count_;
[email protected]c2134fb2013-01-23 04:28:52590 int new_table_count_;
sammcaa222762016-09-21 07:30:42591
592 base::Closure quit_closure_;
Julie Jeongeun Kim8ab180f72019-09-25 09:25:46593 mojo::ReceiverSet<mojom::VisitedLinkNotificationSink> receiver_;
Lukasz Anforowicz2b2699732018-04-12 18:49:10594
595 DISALLOW_COPY_AND_ASSIGN(VisitCountingContext);
[email protected]3e90d4a2009-07-03 17:38:39596};
597
[email protected]f3b1a082011-11-18 00:34:30598// Stub out as little as possible, borrowing from RenderProcessHost.
[email protected]b6a2f8de2012-01-31 17:28:49599class VisitRelayingRenderProcessHost : public MockRenderProcessHost {
[email protected]3e90d4a2009-07-03 17:38:39600 public:
[email protected]3d7474ff2011-07-27 17:47:37601 explicit VisitRelayingRenderProcessHost(
sammcaa222762016-09-21 07:30:42602 content::BrowserContext* browser_context,
603 VisitCountingContext* context)
Bo Liueb218ee2018-03-21 20:24:35604 : MockRenderProcessHost(browser_context) {
bena06f7d62017-04-04 20:48:52605 OverrideBinderForTesting(
sammcaa222762016-09-21 07:30:42606 mojom::VisitedLinkNotificationSink::Name_,
607 base::Bind(&VisitCountingContext::Bind, base::Unretained(context)));
[email protected]ad50def52011-10-19 23:17:07608 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27609 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]6c2381d2011-10-19 02:52:53610 content::Source<RenderProcessHost>(this),
[email protected]ad50def52011-10-19 23:17:07611 content::NotificationService::NoDetails());
[email protected]3e90d4a2009-07-03 17:38:39612 }
dcheng00ea022b2014-10-21 11:24:56613 ~VisitRelayingRenderProcessHost() override {
[email protected]ad50def52011-10-19 23:17:07614 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27615 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]f3b1a082011-11-18 00:34:30616 content::Source<content::RenderProcessHost>(this),
[email protected]ad50def52011-10-19 23:17:07617 content::NotificationService::NoDetails());
[email protected]3e90d4a2009-07-03 17:38:39618 }
619
[email protected]3e90d4a2009-07-03 17:38:39620 private:
[email protected]3e90d4a2009-07-03 17:38:39621 DISALLOW_COPY_AND_ASSIGN(VisitRelayingRenderProcessHost);
622};
623
624class VisitedLinkRenderProcessHostFactory
[email protected]f3b1a082011-11-18 00:34:30625 : public content::RenderProcessHostFactory {
[email protected]3e90d4a2009-07-03 17:38:39626 public:
sammcaa222762016-09-21 07:30:42627 VisitedLinkRenderProcessHostFactory() : context_(new VisitCountingContext) {}
dcheng00ea022b2014-10-21 11:24:56628 content::RenderProcessHost* CreateRenderProcessHost(
Matt Falkenhagen24abf7822017-12-15 23:55:11629 content::BrowserContext* browser_context,
Lucas Furukawa Gadani0a70be92019-06-28 17:31:26630 content::SiteInstance* site_instance) override {
sammcaa222762016-09-21 07:30:42631 return new VisitRelayingRenderProcessHost(browser_context, context_.get());
[email protected]3e90d4a2009-07-03 17:38:39632 }
633
sammcaa222762016-09-21 07:30:42634 VisitCountingContext* context() { return context_.get(); }
635
[email protected]3e90d4a2009-07-03 17:38:39636 private:
sammcaa222762016-09-21 07:30:42637 std::unique_ptr<VisitCountingContext> context_;
[email protected]3e90d4a2009-07-03 17:38:39638 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory);
639};
640
[email protected]28a66e252013-01-25 07:54:02641class VisitedLinkEventsTest : public content::RenderViewHostTestHarness {
[email protected]3e90d4a2009-07-03 17:38:39642 public:
dcheng30a1b1542014-10-29 21:27:50643 void SetUp() override {
[email protected]9a46725f2012-10-24 18:25:48644 SetRenderProcessHostFactory(&vc_rph_factory_);
[email protected]4c3d9d62013-01-09 22:37:20645 content::RenderViewHostTestHarness::SetUp();
[email protected]3e90d4a2009-07-03 17:38:39646 }
647
wkorman1c17345b2015-11-12 03:02:42648 void TearDown() override {
649 // Explicitly destroy the master before proceeding with the rest
650 // of teardown because it posts a task to close a file handle, and
651 // we need to make sure we've finished all file related work
652 // before our superclass sets about destroying the scoped temp
653 // directory.
654 master_.reset();
655 RenderViewHostTestHarness::TearDown();
656 }
657
Sylvain Defresne330d05c2019-08-07 12:36:37658 std::unique_ptr<content::BrowserContext> CreateBrowserContext() override {
659 auto context = std::make_unique<content::TestBrowserContext>();
660 CreateVisitedLinkMaster(context.get());
[email protected]cc99c9f2013-07-15 12:26:09661 return context;
662 }
663
sammcaa222762016-09-21 07:30:42664 VisitCountingContext* context() { return vc_rph_factory_.context(); }
[email protected]3e90d4a2009-07-03 17:38:39665
[email protected]5016ee12012-10-26 23:56:31666 VisitedLinkMaster* master() const {
[email protected]4c3d9d62013-01-09 22:37:20667 return master_.get();
[email protected]5016ee12012-10-26 23:56:31668 }
669
[email protected]3e90d4a2009-07-03 17:38:39670 protected:
sathaf37203d2015-12-15 15:10:42671 void CreateVisitedLinkMaster(content::BrowserContext* browser_context) {
tzik57c66e32018-07-02 08:02:57672 timer_.reset(new base::MockOneShotTimer());
sathaf37203d2015-12-15 15:10:42673 master_.reset(new VisitedLinkMaster(browser_context, &delegate_, true));
sammcaa222762016-09-21 07:30:42674 static_cast<VisitedLinkEventListener*>(master_->GetListener())
675 ->SetCoalesceTimerForTest(timer_.get());
sathaf37203d2015-12-15 15:10:42676 master_->Init();
677 }
678
[email protected]3e90d4a2009-07-03 17:38:39679 VisitedLinkRenderProcessHostFactory vc_rph_factory_;
680
[email protected]4c3d9d62013-01-09 22:37:20681 TestVisitedLinkDelegate delegate_;
tzik57c66e32018-07-02 08:02:57682 std::unique_ptr<base::MockOneShotTimer> timer_;
dcheng3f767dc32016-04-25 22:54:22683 std::unique_ptr<VisitedLinkMaster> master_;
[email protected]3e90d4a2009-07-03 17:38:39684};
685
wkorman1c17345b2015-11-12 03:02:42686TEST_F(VisitedLinkEventsTest, Coalescence) {
sathaf37203d2015-12-15 15:10:42687 // Waiting complete rebuild the table.
Gabriel Charette01507a22017-09-27 21:30:08688 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42689
sathaf37203d2015-12-15 15:10:42690 // After rebuild table expect reset event.
691 EXPECT_EQ(1, context()->reset_event_count());
692
[email protected]3e90d4a2009-07-03 17:38:39693 // add some URLs to master.
[email protected]3e90d4a2009-07-03 17:38:39694 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31695 master()->AddURL(GURL("http://acidtests.org/"));
696 master()->AddURL(GURL("http://google.com/"));
697 master()->AddURL(GURL("http://chromium.org/"));
[email protected]3e90d4a2009-07-03 17:38:39698 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting
[email protected]5016ee12012-10-26 23:56:31699 master()->AddURL(GURL("http://acidtests.org/"));
sammcaa222762016-09-21 07:30:42700 ASSERT_TRUE(timer_->IsRunning());
701 timer_->Fire();
[email protected]3e90d4a2009-07-03 17:38:39702
sammcaa222762016-09-21 07:30:42703 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39704
705 // We now should have 3 entries added in 1 event.
[email protected]28a66e252013-01-25 07:54:02706 EXPECT_EQ(3, context()->add_count());
707 EXPECT_EQ(1, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39708
709 // Test whether the coalescing continues by adding a few more URLs.
[email protected]5016ee12012-10-26 23:56:31710 master()->AddURL(GURL("http://google.com/chrome/"));
711 master()->AddURL(GURL("http://webkit.org/"));
712 master()->AddURL(GURL("http://acid3.acidtests.org/"));
[email protected]3e90d4a2009-07-03 17:38:39713
sammcaa222762016-09-21 07:30:42714 ASSERT_TRUE(timer_->IsRunning());
715 timer_->Fire();
716 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39717
718 // We should have 6 entries added in 2 events.
[email protected]28a66e252013-01-25 07:54:02719 EXPECT_EQ(6, context()->add_count());
720 EXPECT_EQ(2, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39721
722 // Test whether duplicate entries produce add events.
[email protected]5016ee12012-10-26 23:56:31723 master()->AddURL(GURL("http://acidtests.org/"));
sammcaa222762016-09-21 07:30:42724 EXPECT_FALSE(timer_->IsRunning());
725 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39726
727 // We should have no change in results.
[email protected]28a66e252013-01-25 07:54:02728 EXPECT_EQ(6, context()->add_count());
729 EXPECT_EQ(2, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39730
731 // Ensure that the coalescing does not resume after resetting.
[email protected]5016ee12012-10-26 23:56:31732 master()->AddURL(GURL("http://build.chromium.org/"));
sammcaa222762016-09-21 07:30:42733 EXPECT_TRUE(timer_->IsRunning());
[email protected]5016ee12012-10-26 23:56:31734 master()->DeleteAllURLs();
sammcaa222762016-09-21 07:30:42735 EXPECT_FALSE(timer_->IsRunning());
736 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39737
738 // We should have no change in results except for one new reset event.
[email protected]28a66e252013-01-25 07:54:02739 EXPECT_EQ(6, context()->add_count());
740 EXPECT_EQ(2, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42741 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39742}
743
[email protected]7fa7dd52011-04-24 01:09:42744TEST_F(VisitedLinkEventsTest, Basics) {
nasko4c0feb62015-06-05 18:37:06745 RenderViewHostTester::For(rvh())->CreateTestRenderView(
avif9a94c7e2016-10-14 15:54:11746 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false);
[email protected]6e8d64642009-08-10 15:27:19747
sathaf37203d2015-12-15 15:10:42748 // Waiting complete rebuild the table.
Gabriel Charette01507a22017-09-27 21:30:08749 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42750
sathaf37203d2015-12-15 15:10:42751 // After rebuild table expect reset event.
752 EXPECT_EQ(1, context()->reset_event_count());
753
[email protected]3e90d4a2009-07-03 17:38:39754 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31755 master()->AddURL(GURL("http://acidtests.org/"));
756 master()->AddURL(GURL("http://google.com/"));
757 master()->AddURL(GURL("http://chromium.org/"));
sammcaa222762016-09-21 07:30:42758 ASSERT_TRUE(timer_->IsRunning());
759 timer_->Fire();
760 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39761
762 // We now should have 1 add event.
[email protected]28a66e252013-01-25 07:54:02763 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42764 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39765
[email protected]5016ee12012-10-26 23:56:31766 master()->DeleteAllURLs();
[email protected]3e90d4a2009-07-03 17:38:39767
sammcaa222762016-09-21 07:30:42768 EXPECT_FALSE(timer_->IsRunning());
769 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39770
771 // We should have no change in add results, plus one new reset event.
[email protected]28a66e252013-01-25 07:54:02772 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42773 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39774}
775
[email protected]7fa7dd52011-04-24 01:09:42776TEST_F(VisitedLinkEventsTest, TabVisibility) {
nasko4c0feb62015-06-05 18:37:06777 RenderViewHostTester::For(rvh())->CreateTestRenderView(
avif9a94c7e2016-10-14 15:54:11778 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false);
[email protected]3e90d4a2009-07-03 17:38:39779
sathaf37203d2015-12-15 15:10:42780 // Waiting complete rebuild the table.
Gabriel Charette01507a22017-09-27 21:30:08781 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42782
sathaf37203d2015-12-15 15:10:42783 // After rebuild table expect reset event.
784 EXPECT_EQ(1, context()->reset_event_count());
785
[email protected]3e90d4a2009-07-03 17:38:39786 // Simulate tab becoming inactive.
bsepf3b00fa52017-02-11 00:06:38787 RenderViewHostTester::For(rvh())->SimulateWasHidden();
[email protected]3e90d4a2009-07-03 17:38:39788
789 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31790 master()->AddURL(GURL("http://acidtests.org/"));
791 master()->AddURL(GURL("http://google.com/"));
792 master()->AddURL(GURL("http://chromium.org/"));
sammcaa222762016-09-21 07:30:42793 ASSERT_TRUE(timer_->IsRunning());
794 timer_->Fire();
795 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39796
797 // We shouldn't have any events.
[email protected]28a66e252013-01-25 07:54:02798 EXPECT_EQ(0, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42799 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39800
801 // Simulate the tab becoming active.
bsepf3b00fa52017-02-11 00:06:38802 RenderViewHostTester::For(rvh())->SimulateWasShown();
sammcaa222762016-09-21 07:30:42803 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39804
805 // We should now have 3 add events, still no reset events.
[email protected]28a66e252013-01-25 07:54:02806 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42807 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39808
809 // Deactivate the tab again.
bsepf3b00fa52017-02-11 00:06:38810 RenderViewHostTester::For(rvh())->SimulateWasHidden();
[email protected]3e90d4a2009-07-03 17:38:39811
812 // Add a bunch of URLs (over 50) to exhaust the link event buffer.
813 for (int i = 0; i < 100; i++)
[email protected]5016ee12012-10-26 23:56:31814 master()->AddURL(TestURL(i));
[email protected]3e90d4a2009-07-03 17:38:39815
sammcaa222762016-09-21 07:30:42816 ASSERT_TRUE(timer_->IsRunning());
817 timer_->Fire();
818 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39819
820 // Again, no change in events until tab is active.
[email protected]28a66e252013-01-25 07:54:02821 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42822 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39823
824 // Activate the tab.
bsepf3b00fa52017-02-11 00:06:38825 RenderViewHostTester::For(rvh())->SimulateWasShown();
sammcaa222762016-09-21 07:30:42826 EXPECT_FALSE(timer_->IsRunning());
827 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39828
829 // We should have only one more reset event.
[email protected]28a66e252013-01-25 07:54:02830 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42831 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39832}
[email protected]1f371fa2013-01-23 00:35:14833
[email protected]c2134fb2013-01-23 04:28:52834// Tests that VisitedLink ignores renderer process creation notification for a
[email protected]28a66e252013-01-25 07:54:02835// different context.
[email protected]c2134fb2013-01-23 04:28:52836TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) {
sammcaa222762016-09-21 07:30:42837 content::TestBrowserContext different_context;
838 VisitCountingContext counting_context;
839 VisitRelayingRenderProcessHost different_process_host(&different_context,
840 &counting_context);
[email protected]c2134fb2013-01-23 04:28:52841
Lukasz Anforowicz2b2699732018-04-12 18:49:10842 size_t old_size = counting_context.binding().size();
[email protected]c2134fb2013-01-23 04:28:52843 content::NotificationService::current()->Notify(
844 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
845 content::Source<content::RenderProcessHost>(&different_process_host),
846 content::NotificationService::NoDetails());
Lukasz Anforowicz2b2699732018-04-12 18:49:10847 size_t new_size = counting_context.binding().size();
848 EXPECT_EQ(old_size, new_size);
[email protected]c2134fb2013-01-23 04:28:52849}
850
sathaf37203d2015-12-15 15:10:42851class VisitedLinkCompletelyResetEventTest : public VisitedLinkEventsTest {
852 public:
Sylvain Defresne330d05c2019-08-07 12:36:37853 std::unique_ptr<content::BrowserContext> CreateBrowserContext() override {
854 auto context = std::make_unique<content::TestBrowserContext>();
855 CreateVisitedLinkFile(context.get());
856 CreateVisitedLinkMaster(context.get());
sathaf37203d2015-12-15 15:10:42857 return context;
858 }
859
860 void CreateVisitedLinkFile(content::BrowserContext* browser_context) {
861 base::FilePath visited_file =
862 browser_context->GetPath().Append(FILE_PATH_LITERAL("Visited Links"));
dcheng3f767dc32016-04-25 22:54:22863 std::unique_ptr<VisitedLinkMaster> master(
sathaf37203d2015-12-15 15:10:42864 new VisitedLinkMaster(new TrackingVisitedLinkEventListener(),
865 &delegate_, true, true, visited_file, 0));
866 master->Init();
867 // Waiting complete create the table.
Gabriel Charette01507a22017-09-27 21:30:08868 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42869
870 master.reset();
871 // Wait for all pending file I/O to be completed.
Gabriel Charette01507a22017-09-27 21:30:08872 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42873 }
874};
875
876TEST_F(VisitedLinkCompletelyResetEventTest, LoadTable) {
877 // Waiting complete loading the table.
Gabriel Charette01507a22017-09-27 21:30:08878 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42879
sammcaa222762016-09-21 07:30:42880 context()->binding().FlushForTesting();
sathaf37203d2015-12-15 15:10:42881
882 // After load table expect completely reset event.
883 EXPECT_EQ(1, context()->completely_reset_event_count());
884}
885
[email protected]ab3eaeed2013-05-17 00:18:44886} // namespace visitedlink