blob: c52317b2d7ea62e3bdd157f48c957a622dbc2c2c [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
sammcaa222762016-09-21 07:30:4213#include "base/callback_helpers.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"
[email protected]08a932d52012-06-03 21:42:1232#include "content/public/test/mock_render_process_host.h"
[email protected]4c3d9d62013-01-09 22:37:2033#include "content/public/test/test_browser_context.h"
[email protected]ec04d3f2013-06-06 21:31:3934#include "content/public/test/test_browser_thread_bundle.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"
initial.commit09911bf2008-07-26 23:55:2937#include "testing/gtest/include/gtest/gtest.h"
[email protected]e3b599e2013-07-05 07:15:1738#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2939
[email protected]631bb742011-11-02 11:29:3940using content::BrowserThread;
[email protected]eddd886702012-03-16 14:53:2341using content::MockRenderProcessHost;
[email protected]c0257382012-03-12 20:15:3442using content::RenderViewHostTester;
[email protected]631bb742011-11-02 11:29:3943
Matt Falkenhagen24abf7822017-12-15 23:55:1144namespace content {
45class SiteInstance;
46}
47
[email protected]ab3eaeed2013-05-17 00:18:4448namespace visitedlink {
[email protected]1f371fa2013-01-23 00:35:1449
initial.commit09911bf2008-07-26 23:55:2950namespace {
51
[email protected]4c3d9d62013-01-09 22:37:2052typedef std::vector<GURL> URLs;
53
initial.commit09911bf2008-07-26 23:55:2954// a nice long URL that we can append numbers to to get new URLs
55const char g_test_prefix[] =
56 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq=";
57const int g_test_count = 1000;
58
59// Returns a test URL for index |i|
60GURL TestURL(int i) {
[email protected]7d3cbc92013-03-18 22:33:0461 return GURL(base::StringPrintf("%s%d", g_test_prefix, i));
initial.commit09911bf2008-07-26 23:55:2962}
63
[email protected]4c3d9d62013-01-09 22:37:2064std::vector<VisitedLinkSlave*> g_slaves;
65
66class TestVisitedLinkDelegate : public VisitedLinkDelegate {
67 public:
dcheng00ea022b2014-10-21 11:24:5668 void RebuildTable(const scoped_refptr<URLEnumerator>& enumerator) override;
[email protected]4c3d9d62013-01-09 22:37:2069
70 void AddURLForRebuild(const GURL& url);
71
72 private:
[email protected]4c3d9d62013-01-09 22:37:2073 URLs rebuild_urls_;
74};
75
[email protected]4c3d9d62013-01-09 22:37:2076void TestVisitedLinkDelegate::RebuildTable(
77 const scoped_refptr<URLEnumerator>& enumerator) {
78 for (URLs::const_iterator itr = rebuild_urls_.begin();
79 itr != rebuild_urls_.end();
80 ++itr)
81 enumerator->OnURL(*itr);
82 enumerator->OnComplete(true);
83}
84
85void TestVisitedLinkDelegate::AddURLForRebuild(const GURL& url) {
86 rebuild_urls_.push_back(url);
87}
88
89class TestURLIterator : public VisitedLinkMaster::URLIterator {
90 public:
91 explicit TestURLIterator(const URLs& urls);
92
dcheng00ea022b2014-10-21 11:24:5693 const GURL& NextURL() override;
94 bool HasNextURL() const override;
[email protected]4c3d9d62013-01-09 22:37:2095
96 private:
97 URLs::const_iterator iterator_;
98 URLs::const_iterator end_;
99};
100
101TestURLIterator::TestURLIterator(const URLs& urls)
102 : iterator_(urls.begin()),
103 end_(urls.end()) {
104}
105
106const GURL& TestURLIterator::NextURL() {
107 return *(iterator_++);
108}
109
110bool TestURLIterator::HasNextURL() const {
111 return iterator_ != end_;
112}
[email protected]dba45e32013-01-04 20:27:15113
[email protected]3e90d4a2009-07-03 17:38:39114} // namespace
115
116class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener {
117 public:
118 TrackingVisitedLinkEventListener()
119 : reset_count_(0),
sathaf37203d2015-12-15 15:10:42120 completely_reset_count_(0),
[email protected]3e90d4a2009-07-03 17:38:39121 add_count_(0) {}
122
Ken Rockotaeb7eca2018-03-30 16:40:09123 void NewTable(base::ReadOnlySharedMemoryRegion* table_region) override {
124 if (table_region->IsValid()) {
[email protected]3e90d4a2009-07-03 17:38:39125 for (std::vector<VisitedLinkSlave>::size_type i = 0;
126 i < g_slaves.size(); i++) {
Ken Rockotaeb7eca2018-03-30 16:40:09127 g_slaves[i]->UpdateVisitedLinks(table_region->Duplicate());
[email protected]3e90d4a2009-07-03 17:38:39128 }
initial.commit09911bf2008-07-26 23:55:29129 }
130 }
dcheng00ea022b2014-10-21 11:24:56131 void Add(VisitedLinkCommon::Fingerprint) override { add_count_++; }
sathaf37203d2015-12-15 15:10:42132 void Reset(bool invalidate_hashes) override {
133 if (invalidate_hashes)
134 completely_reset_count_++;
135 else
136 reset_count_++;
137 }
initial.commit09911bf2008-07-26 23:55:29138
[email protected]3e90d4a2009-07-03 17:38:39139 void SetUp() {
140 reset_count_ = 0;
141 add_count_ = 0;
142 }
143
144 int reset_count() const { return reset_count_; }
sathaf37203d2015-12-15 15:10:42145 int completely_reset_count() { return completely_reset_count_; }
[email protected]3e90d4a2009-07-03 17:38:39146 int add_count() const { return add_count_; }
147
148 private:
149 int reset_count_;
sathaf37203d2015-12-15 15:10:42150 int completely_reset_count_;
[email protected]3e90d4a2009-07-03 17:38:39151 int add_count_;
152};
[email protected]c2c998c2009-01-27 19:08:39153
[email protected]583844c2011-08-27 00:38:35154class VisitedLinkTest : public testing::Test {
initial.commit09911bf2008-07-26 23:55:29155 protected:
initial.commit09911bf2008-07-26 23:55:29156 // Initializes the visited link objects. Pass in the size that you want a
157 // freshly created table to be. 0 means use the default.
158 //
159 // |suppress_rebuild| is set when we're not testing rebuilding, see
160 // the VisitedLinkMaster constructor.
sathaf37203d2015-12-15 15:10:42161 //
162 // |wait_for_io_complete| wait for result of async loading.
163 bool InitVisited(int initial_size,
164 bool suppress_rebuild,
165 bool wait_for_io_complete) {
initial.commit09911bf2008-07-26 23:55:29166 // Initialize the visited link system.
[email protected]b42e6d82012-11-02 02:44:56167 master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(),
[email protected]4c3d9d62013-01-09 22:37:20168 &delegate_,
[email protected]bff706b2013-01-25 00:15:01169 true,
[email protected]3e90d4a2009-07-03 17:38:39170 suppress_rebuild, visited_file_,
171 initial_size));
sathaf37203d2015-12-15 15:10:42172 bool result = master_->Init();
173 if (result && wait_for_io_complete) {
174 // Wait for all pending file I/O to be completed.
Gabriel Charette01507a22017-09-27 21:30:08175 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42176 }
177 return result;
initial.commit09911bf2008-07-26 23:55:29178 }
179
180 // May be called multiple times (some tests will do this to clear things,
181 // and TearDown will do this to make sure eveything is shiny before quitting.
182 void ClearDB() {
183 if (master_.get())
Ivan Kotenkov75b1c3a2017-10-24 14:47:24184 master_.reset(nullptr);
initial.commit09911bf2008-07-26 23:55:29185
[email protected]3189013e2012-01-19 04:11:57186 // Wait for all pending file I/O to be completed.
Gabriel Charette01507a22017-09-27 21:30:08187 content::RunAllTasksUntilIdle();
initial.commit09911bf2008-07-26 23:55:29188 }
189
190 // Loads the database from disk and makes sure that the same URLs are present
191 // as were generated by TestIO_Create(). This also checks the URLs with a
192 // slave to make sure it reads the data properly.
193 void Reload() {
194 // Clean up after our caller, who may have left the database open.
195 ClearDB();
196
sathaf37203d2015-12-15 15:10:42197 ASSERT_TRUE(InitVisited(0, true, true));
198
initial.commit09911bf2008-07-26 23:55:29199 master_->DebugValidate();
200
201 // check that the table has the proper number of entries
202 int used_count = master_->GetUsedCount();
203 ASSERT_EQ(used_count, g_test_count);
204
205 // Create a slave database.
206 VisitedLinkSlave slave;
Ken Rockotaeb7eca2018-03-30 16:40:09207 slave.UpdateVisitedLinks(master_->mapped_table_memory().region.Duplicate());
initial.commit09911bf2008-07-26 23:55:29208 g_slaves.push_back(&slave);
209
210 bool found;
211 for (int i = 0; i < g_test_count; i++) {
212 GURL cur = TestURL(i);
213 found = master_->IsVisited(cur);
214 EXPECT_TRUE(found) << "URL " << i << "not found in master.";
215
216 found = slave.IsVisited(cur);
217 EXPECT_TRUE(found) << "URL " << i << "not found in slave.";
218 }
219
220 // test some random URL so we know that it returns false sometimes too
221 found = master_->IsVisited(GURL("http://unfound.site/"));
222 ASSERT_FALSE(found);
223 found = slave.IsVisited(GURL("http://unfound.site/"));
224 ASSERT_FALSE(found);
225
226 master_->DebugValidate();
227
228 g_slaves.clear();
229 }
230
231 // testing::Test
dcheng30a1b1542014-10-29 21:27:50232 void SetUp() override {
[email protected]f708ed12010-09-23 12:18:34233 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
234
vabr8c498ea42016-09-15 12:41:58235 history_dir_ = temp_dir_.GetPath().AppendASCII("VisitedLinkTest");
[email protected]426d1c92013-12-03 20:08:54236 ASSERT_TRUE(base::CreateDirectory(history_dir_));
initial.commit09911bf2008-07-26 23:55:29237
[email protected]c2c998c2009-01-27 19:08:39238 visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks"));
initial.commit09911bf2008-07-26 23:55:29239 }
240
dcheng30a1b1542014-10-29 21:27:50241 void TearDown() override { ClearDB(); }
[email protected]f0a51fb52009-03-05 12:46:38242
[email protected]ea1a3f62012-11-16 20:34:23243 base::ScopedTempDir temp_dir_;
[email protected]f708ed12010-09-23 12:18:34244
initial.commit09911bf2008-07-26 23:55:29245 // Filenames for the services;
[email protected]9e275712013-02-10 19:20:14246 base::FilePath history_dir_;
247 base::FilePath visited_file_;
initial.commit09911bf2008-07-26 23:55:29248
dcheng3f767dc32016-04-25 22:54:22249 std::unique_ptr<VisitedLinkMaster> master_;
[email protected]4c3d9d62013-01-09 22:37:20250 TestVisitedLinkDelegate delegate_;
[email protected]ec04d3f2013-06-06 21:31:39251 content::TestBrowserThreadBundle thread_bundle_;
initial.commit09911bf2008-07-26 23:55:29252};
253
initial.commit09911bf2008-07-26 23:55:29254// This test creates and reads some databases to make sure the data is
255// preserved throughout those operations.
256TEST_F(VisitedLinkTest, DatabaseIO) {
sathaf37203d2015-12-15 15:10:42257 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29258
259 for (int i = 0; i < g_test_count; i++)
260 master_->AddURL(TestURL(i));
261
262 // Test that the database was written properly
263 Reload();
264}
265
266// Checks that we can delete things properly when there are collisions.
267TEST_F(VisitedLinkTest, Delete) {
avi5dd91f82015-12-25 22:30:46268 static const int32_t kInitialSize = 17;
sathaf37203d2015-12-15 15:10:42269 ASSERT_TRUE(InitVisited(kInitialSize, true, true));
initial.commit09911bf2008-07-26 23:55:29270
271 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the
272 // same value.
[email protected]c2c998c2009-01-27 19:08:39273 const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14;
274 const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14;
275 const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14;
276 const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14;
277 const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14;
[email protected]f8c42c92009-07-04 17:22:08278 master_->AddFingerprint(kFingerprint0, false); // @14
279 master_->AddFingerprint(kFingerprint1, false); // @15
280 master_->AddFingerprint(kFingerprint2, false); // @16
281 master_->AddFingerprint(kFingerprint3, false); // @0
282 master_->AddFingerprint(kFingerprint4, false); // @1
initial.commit09911bf2008-07-26 23:55:29283
284 // Deleting 14 should move the next value up one slot (we do not specify an
285 // order).
286 EXPECT_EQ(kFingerprint3, master_->hash_table_[0]);
287 master_->DeleteFingerprint(kFingerprint3, false);
[email protected]c2c998c2009-01-27 19:08:39288 VisitedLinkCommon::Fingerprint zero_fingerprint = 0;
289 EXPECT_EQ(zero_fingerprint, master_->hash_table_[1]);
290 EXPECT_NE(zero_fingerprint, master_->hash_table_[0]);
initial.commit09911bf2008-07-26 23:55:29291
292 // Deleting the other four should leave the table empty.
293 master_->DeleteFingerprint(kFingerprint0, false);
294 master_->DeleteFingerprint(kFingerprint1, false);
295 master_->DeleteFingerprint(kFingerprint2, false);
296 master_->DeleteFingerprint(kFingerprint4, false);
297
298 EXPECT_EQ(0, master_->used_items_);
299 for (int i = 0; i < kInitialSize; i++)
[email protected]c2c998c2009-01-27 19:08:39300 EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) <<
301 "Hash table has values in it.";
initial.commit09911bf2008-07-26 23:55:29302}
303
304// When we delete more than kBigDeleteThreshold we trigger different behavior
305// where the entire file is rewritten.
306TEST_F(VisitedLinkTest, BigDelete) {
sathaf37203d2015-12-15 15:10:42307 ASSERT_TRUE(InitVisited(16381, true, true));
initial.commit09911bf2008-07-26 23:55:29308
309 // Add the base set of URLs that won't be deleted.
310 // Reload() will test for these.
avi5dd91f82015-12-25 22:30:46311 for (int32_t i = 0; i < g_test_count; i++)
initial.commit09911bf2008-07-26 23:55:29312 master_->AddURL(TestURL(i));
313
314 // Add more URLs than necessary to trigger this case.
315 const int kTestDeleteCount = VisitedLinkMaster::kBigDeleteThreshold + 2;
[email protected]4c3d9d62013-01-09 22:37:20316 URLs urls_to_delete;
avi5dd91f82015-12-25 22:30:46317 for (int32_t i = g_test_count; i < g_test_count + kTestDeleteCount; i++) {
initial.commit09911bf2008-07-26 23:55:29318 GURL url(TestURL(i));
319 master_->AddURL(url);
[email protected]4c3d9d62013-01-09 22:37:20320 urls_to_delete.push_back(url);
initial.commit09911bf2008-07-26 23:55:29321 }
322
[email protected]4c3d9d62013-01-09 22:37:20323 TestURLIterator iterator(urls_to_delete);
324 master_->DeleteURLs(&iterator);
initial.commit09911bf2008-07-26 23:55:29325 master_->DebugValidate();
326
327 Reload();
328}
329
330TEST_F(VisitedLinkTest, DeleteAll) {
sathaf37203d2015-12-15 15:10:42331 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29332
333 {
334 VisitedLinkSlave slave;
Ken Rockotaeb7eca2018-03-30 16:40:09335 slave.UpdateVisitedLinks(master_->mapped_table_memory().region.Duplicate());
initial.commit09911bf2008-07-26 23:55:29336 g_slaves.push_back(&slave);
337
338 // Add the test URLs.
339 for (int i = 0; i < g_test_count; i++) {
340 master_->AddURL(TestURL(i));
341 ASSERT_EQ(i + 1, master_->GetUsedCount());
342 }
343 master_->DebugValidate();
344
345 // Make sure the slave picked up the adds.
346 for (int i = 0; i < g_test_count; i++)
347 EXPECT_TRUE(slave.IsVisited(TestURL(i)));
348
349 // Clear the table and make sure the slave picked it up.
350 master_->DeleteAllURLs();
351 EXPECT_EQ(0, master_->GetUsedCount());
352 for (int i = 0; i < g_test_count; i++) {
353 EXPECT_FALSE(master_->IsVisited(TestURL(i)));
354 EXPECT_FALSE(slave.IsVisited(TestURL(i)));
355 }
356
357 // Close the database.
358 g_slaves.clear();
359 ClearDB();
360 }
361
362 // Reopen and validate.
sathaf37203d2015-12-15 15:10:42363 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29364 master_->DebugValidate();
365 EXPECT_EQ(0, master_->GetUsedCount());
366 for (int i = 0; i < g_test_count; i++)
367 EXPECT_FALSE(master_->IsVisited(TestURL(i)));
368}
369
370// This tests that the master correctly resizes its tables when it gets too
371// full, notifies its slaves of the change, and updates the disk.
372TEST_F(VisitedLinkTest, Resizing) {
373 // Create a very small database.
avi5dd91f82015-12-25 22:30:46374 const int32_t initial_size = 17;
sathaf37203d2015-12-15 15:10:42375 ASSERT_TRUE(InitVisited(initial_size, true, true));
initial.commit09911bf2008-07-26 23:55:29376
377 // ...and a slave
378 VisitedLinkSlave slave;
Ken Rockotaeb7eca2018-03-30 16:40:09379 slave.UpdateVisitedLinks(master_->mapped_table_memory().region.Duplicate());
initial.commit09911bf2008-07-26 23:55:29380 g_slaves.push_back(&slave);
381
avi5dd91f82015-12-25 22:30:46382 int32_t used_count = master_->GetUsedCount();
initial.commit09911bf2008-07-26 23:55:29383 ASSERT_EQ(used_count, 0);
384
385 for (int i = 0; i < g_test_count; i++) {
386 master_->AddURL(TestURL(i));
387 used_count = master_->GetUsedCount();
388 ASSERT_EQ(i + 1, used_count);
389 }
390
391 // Verify that the table got resized sufficiently.
avi5dd91f82015-12-25 22:30:46392 int32_t table_size;
initial.commit09911bf2008-07-26 23:55:29393 VisitedLinkCommon::Fingerprint* table;
394 master_->GetUsageStatistics(&table_size, &table);
395 used_count = master_->GetUsedCount();
396 ASSERT_GT(table_size, used_count);
397 ASSERT_EQ(used_count, g_test_count) <<
398 "table count doesn't match the # of things we added";
399
400 // Verify that the slave got the resize message and has the same
401 // table information.
avi5dd91f82015-12-25 22:30:46402 int32_t child_table_size;
initial.commit09911bf2008-07-26 23:55:29403 VisitedLinkCommon::Fingerprint* child_table;
404 slave.GetUsageStatistics(&child_table_size, &child_table);
405 ASSERT_EQ(table_size, child_table_size);
avi5dd91f82015-12-25 22:30:46406 for (int32_t i = 0; i < table_size; i++) {
initial.commit09911bf2008-07-26 23:55:29407 ASSERT_EQ(table[i], child_table[i]);
408 }
409
410 master_->DebugValidate();
411 g_slaves.clear();
412
413 // This tests that the file is written correctly by reading it in using
414 // a new database.
415 Reload();
416}
417
418// Tests that if the database doesn't exist, it will be rebuilt from history.
419TEST_F(VisitedLinkTest, Rebuild) {
initial.commit09911bf2008-07-26 23:55:29420 // Add half of our URLs to history. This needs to be done before we
421 // initialize the visited link DB.
422 int history_count = g_test_count / 2;
423 for (int i = 0; i < history_count; i++)
[email protected]4c3d9d62013-01-09 22:37:20424 delegate_.AddURLForRebuild(TestURL(i));
initial.commit09911bf2008-07-26 23:55:29425
426 // Initialize the visited link DB. Since the visited links file doesn't exist
427 // and we don't suppress history rebuilding, this will load from history.
sathaf37203d2015-12-15 15:10:42428 ASSERT_TRUE(InitVisited(0, false, false));
initial.commit09911bf2008-07-26 23:55:29429
430 // While the table is rebuilding, add the rest of the URLs to the visited
431 // link system. This isn't guaranteed to happen during the rebuild, so we
432 // can't be 100% sure we're testing the right thing, but in practice is.
433 // All the adds above will generally take some time queuing up on the
434 // history thread, and it will take a while to catch up to actually
435 // processing the rebuild that has queued behind it. We will generally
436 // finish adding all of the URLs before it has even found the first URL.
437 for (int i = history_count; i < g_test_count; i++)
438 master_->AddURL(TestURL(i));
439
440 // Add one more and then delete it.
441 master_->AddURL(TestURL(g_test_count));
[email protected]4c3d9d62013-01-09 22:37:20442 URLs urls_to_delete;
443 urls_to_delete.push_back(TestURL(g_test_count));
444 TestURLIterator iterator(urls_to_delete);
445 master_->DeleteURLs(&iterator);
initial.commit09911bf2008-07-26 23:55:29446
447 // Wait for the rebuild to complete. The task will terminate the message
448 // loop when the rebuild is done. There's no chance that the rebuild will
449 // complete before we set the task because the rebuild completion message
450 // is posted to the message loop; until we Run() it, rebuild can not
451 // complete.
[email protected]ec04d3f2013-06-06 21:31:39452 base::RunLoop run_loop;
453 master_->set_rebuild_complete_task(run_loop.QuitClosure());
454 run_loop.Run();
initial.commit09911bf2008-07-26 23:55:29455
456 // Test that all URLs were written to the database properly.
457 Reload();
458
459 // Make sure the extra one was *not* written (Reload won't test this).
460 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count)));
461}
[email protected]3e90d4a2009-07-03 17:38:39462
[email protected]aed132ed2009-08-19 22:44:12463// Test that importing a large number of URLs will work
464TEST_F(VisitedLinkTest, BigImport) {
sathaf37203d2015-12-15 15:10:42465 ASSERT_TRUE(InitVisited(0, false, false));
[email protected]aed132ed2009-08-19 22:44:12466
467 // Before the table rebuilds, add a large number of URLs
468 int total_count = VisitedLinkMaster::kDefaultTableSize + 10;
469 for (int i = 0; i < total_count; i++)
470 master_->AddURL(TestURL(i));
471
472 // Wait for the rebuild to complete.
[email protected]ec04d3f2013-06-06 21:31:39473 base::RunLoop run_loop;
474 master_->set_rebuild_complete_task(run_loop.QuitClosure());
475 run_loop.Run();
[email protected]aed132ed2009-08-19 22:44:12476
477 // Ensure that the right number of URLs are present
478 int used_count = master_->GetUsedCount();
479 ASSERT_EQ(used_count, total_count);
480}
481
[email protected]3e90d4a2009-07-03 17:38:39482TEST_F(VisitedLinkTest, Listener) {
sathaf37203d2015-12-15 15:10:42483 ASSERT_TRUE(InitVisited(0, true, true));
484
485 TrackingVisitedLinkEventListener* listener =
486 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener());
487
488 // Verify that VisitedLinkMaster::Listener::Reset(true) was never called when
489 // the table was created.
490 EXPECT_EQ(0, listener->completely_reset_count());
[email protected]3e90d4a2009-07-03 17:38:39491
492 // Add test URLs.
493 for (int i = 0; i < g_test_count; i++) {
494 master_->AddURL(TestURL(i));
495 ASSERT_EQ(i + 1, master_->GetUsedCount());
496 }
497
[email protected]3e90d4a2009-07-03 17:38:39498 // Delete an URL.
[email protected]4c3d9d62013-01-09 22:37:20499 URLs urls_to_delete;
500 urls_to_delete.push_back(TestURL(0));
501 TestURLIterator iterator(urls_to_delete);
502 master_->DeleteURLs(&iterator);
503
[email protected]3e90d4a2009-07-03 17:38:39504 // ... and all of the remaining ones.
505 master_->DeleteAllURLs();
506
[email protected]3e90d4a2009-07-03 17:38:39507 // Verify that VisitedLinkMaster::Listener::Add was called for each added URL.
[email protected]b42e6d82012-11-02 02:44:56508 EXPECT_EQ(g_test_count, listener->add_count());
[email protected]3e90d4a2009-07-03 17:38:39509 // Verify that VisitedLinkMaster::Listener::Reset was called both when one and
510 // all URLs are deleted.
[email protected]b42e6d82012-11-02 02:44:56511 EXPECT_EQ(2, listener->reset_count());
sathaf37203d2015-12-15 15:10:42512
513 ClearDB();
514
515 ASSERT_TRUE(InitVisited(0, true, true));
516
517 listener =
518 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener());
519 // Verify that VisitedLinkMaster::Listener::Reset(true) was called when the
520 // table was loaded.
521 EXPECT_EQ(1, listener->completely_reset_count());
[email protected]3e90d4a2009-07-03 17:38:39522}
523
sammcaa222762016-09-21 07:30:42524class VisitCountingContext : public mojom::VisitedLinkNotificationSink {
[email protected]3e90d4a2009-07-03 17:38:39525 public:
[email protected]28a66e252013-01-25 07:54:02526 VisitCountingContext()
[email protected]3e90d4a2009-07-03 17:38:39527 : add_count_(0),
528 add_event_count_(0),
[email protected]c2134fb2013-01-23 04:28:52529 reset_event_count_(0),
sathaf37203d2015-12-15 15:10:42530 completely_reset_event_count_(0),
sammcaa222762016-09-21 07:30:42531 new_table_count_(0),
532 binding_(this) {}
[email protected]3e90d4a2009-07-03 17:38:39533
sammcaa222762016-09-21 07:30:42534 void Bind(mojo::ScopedMessagePipeHandle handle) {
Ken Rockotf4d8a942017-05-13 00:10:37535 binding_.Bind(mojom::VisitedLinkNotificationSinkRequest(std::move(handle)));
[email protected]3e90d4a2009-07-03 17:38:39536 }
537
sammcaa222762016-09-21 07:30:42538 void WaitForUpdate() {
539 base::RunLoop run_loop;
540 quit_closure_ = run_loop.QuitClosure();
541 run_loop.Run();
[email protected]3e90d4a2009-07-03 17:38:39542 }
543
sammcaa222762016-09-21 07:30:42544 void WaitForNoUpdate() { binding_.FlushForTesting(); }
545
546 mojo::Binding<mojom::VisitedLinkNotificationSink>& binding() {
547 return binding_;
sathaf37203d2015-12-15 15:10:42548 }
549
sammcaa222762016-09-21 07:30:42550 void NotifyUpdate() {
551 if (!quit_closure_.is_null())
552 base::ResetAndReturn(&quit_closure_).Run();
553 }
554
555 void UpdateVisitedLinks(
Ken Rockotaeb7eca2018-03-30 16:40:09556 base::ReadOnlySharedMemoryRegion table_region) override {
[email protected]c2134fb2013-01-23 04:28:52557 new_table_count_++;
sammcaa222762016-09-21 07:30:42558 NotifyUpdate();
559 }
560
561 void AddVisitedLinks(const std::vector<uint64_t>& link_hashes) override {
562 add_count_ += link_hashes.size();
563 add_event_count_++;
564 NotifyUpdate();
565 }
566
567 void ResetVisitedLinks(bool invalidate_cached_hashes) override {
568 if (invalidate_cached_hashes)
569 completely_reset_event_count_++;
570 else
571 reset_event_count_++;
572 NotifyUpdate();
[email protected]c2134fb2013-01-23 04:28:52573 }
574
[email protected]3e90d4a2009-07-03 17:38:39575 int add_count() const { return add_count_; }
576 int add_event_count() const { return add_event_count_; }
577 int reset_event_count() const { return reset_event_count_; }
sathaf37203d2015-12-15 15:10:42578 int completely_reset_event_count() const {
579 return completely_reset_event_count_;
580 }
[email protected]c2134fb2013-01-23 04:28:52581 int new_table_count() const { return new_table_count_; }
[email protected]3e90d4a2009-07-03 17:38:39582
583 private:
584 int add_count_;
585 int add_event_count_;
586 int reset_event_count_;
sathaf37203d2015-12-15 15:10:42587 int completely_reset_event_count_;
[email protected]c2134fb2013-01-23 04:28:52588 int new_table_count_;
sammcaa222762016-09-21 07:30:42589
590 base::Closure quit_closure_;
591 mojo::Binding<mojom::VisitedLinkNotificationSink> binding_;
[email protected]3e90d4a2009-07-03 17:38:39592};
593
[email protected]f3b1a082011-11-18 00:34:30594// Stub out as little as possible, borrowing from RenderProcessHost.
[email protected]b6a2f8de2012-01-31 17:28:49595class VisitRelayingRenderProcessHost : public MockRenderProcessHost {
[email protected]3e90d4a2009-07-03 17:38:39596 public:
[email protected]3d7474ff2011-07-27 17:47:37597 explicit VisitRelayingRenderProcessHost(
sammcaa222762016-09-21 07:30:42598 content::BrowserContext* browser_context,
599 VisitCountingContext* context)
Bo Liueb218ee2018-03-21 20:24:35600 : MockRenderProcessHost(browser_context) {
bena06f7d62017-04-04 20:48:52601 OverrideBinderForTesting(
sammcaa222762016-09-21 07:30:42602 mojom::VisitedLinkNotificationSink::Name_,
603 base::Bind(&VisitCountingContext::Bind, base::Unretained(context)));
[email protected]ad50def52011-10-19 23:17:07604 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27605 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]6c2381d2011-10-19 02:52:53606 content::Source<RenderProcessHost>(this),
[email protected]ad50def52011-10-19 23:17:07607 content::NotificationService::NoDetails());
[email protected]3e90d4a2009-07-03 17:38:39608 }
dcheng00ea022b2014-10-21 11:24:56609 ~VisitRelayingRenderProcessHost() override {
[email protected]ad50def52011-10-19 23:17:07610 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27611 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]f3b1a082011-11-18 00:34:30612 content::Source<content::RenderProcessHost>(this),
[email protected]ad50def52011-10-19 23:17:07613 content::NotificationService::NoDetails());
[email protected]3e90d4a2009-07-03 17:38:39614 }
615
[email protected]3e90d4a2009-07-03 17:38:39616 private:
[email protected]3e90d4a2009-07-03 17:38:39617 DISALLOW_COPY_AND_ASSIGN(VisitRelayingRenderProcessHost);
618};
619
620class VisitedLinkRenderProcessHostFactory
[email protected]f3b1a082011-11-18 00:34:30621 : public content::RenderProcessHostFactory {
[email protected]3e90d4a2009-07-03 17:38:39622 public:
sammcaa222762016-09-21 07:30:42623 VisitedLinkRenderProcessHostFactory() : context_(new VisitCountingContext) {}
dcheng00ea022b2014-10-21 11:24:56624 content::RenderProcessHost* CreateRenderProcessHost(
Matt Falkenhagen24abf7822017-12-15 23:55:11625 content::BrowserContext* browser_context,
626 content::SiteInstance* site_instance) const override {
sammcaa222762016-09-21 07:30:42627 return new VisitRelayingRenderProcessHost(browser_context, context_.get());
[email protected]3e90d4a2009-07-03 17:38:39628 }
629
sammcaa222762016-09-21 07:30:42630 VisitCountingContext* context() { return context_.get(); }
631
[email protected]3e90d4a2009-07-03 17:38:39632 private:
sammcaa222762016-09-21 07:30:42633 std::unique_ptr<VisitCountingContext> context_;
[email protected]3e90d4a2009-07-03 17:38:39634 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory);
635};
636
[email protected]28a66e252013-01-25 07:54:02637class VisitedLinkEventsTest : public content::RenderViewHostTestHarness {
[email protected]3e90d4a2009-07-03 17:38:39638 public:
dcheng30a1b1542014-10-29 21:27:50639 void SetUp() override {
[email protected]9a46725f2012-10-24 18:25:48640 SetRenderProcessHostFactory(&vc_rph_factory_);
[email protected]4c3d9d62013-01-09 22:37:20641 content::RenderViewHostTestHarness::SetUp();
[email protected]3e90d4a2009-07-03 17:38:39642 }
643
wkorman1c17345b2015-11-12 03:02:42644 void TearDown() override {
645 // Explicitly destroy the master before proceeding with the rest
646 // of teardown because it posts a task to close a file handle, and
647 // we need to make sure we've finished all file related work
648 // before our superclass sets about destroying the scoped temp
649 // directory.
650 master_.reset();
651 RenderViewHostTestHarness::TearDown();
652 }
653
dcheng00ea022b2014-10-21 11:24:56654 content::BrowserContext* CreateBrowserContext() override {
sammcaa222762016-09-21 07:30:42655 content::BrowserContext* context = new content::TestBrowserContext();
sathaf37203d2015-12-15 15:10:42656 CreateVisitedLinkMaster(context);
[email protected]cc99c9f2013-07-15 12:26:09657 return context;
658 }
659
sammcaa222762016-09-21 07:30:42660 VisitCountingContext* context() { return vc_rph_factory_.context(); }
[email protected]3e90d4a2009-07-03 17:38:39661
[email protected]5016ee12012-10-26 23:56:31662 VisitedLinkMaster* master() const {
[email protected]4c3d9d62013-01-09 22:37:20663 return master_.get();
[email protected]5016ee12012-10-26 23:56:31664 }
665
[email protected]3e90d4a2009-07-03 17:38:39666 protected:
sathaf37203d2015-12-15 15:10:42667 void CreateVisitedLinkMaster(content::BrowserContext* browser_context) {
sammcaa222762016-09-21 07:30:42668 timer_.reset(new base::MockTimer(false, false));
sathaf37203d2015-12-15 15:10:42669 master_.reset(new VisitedLinkMaster(browser_context, &delegate_, true));
sammcaa222762016-09-21 07:30:42670 static_cast<VisitedLinkEventListener*>(master_->GetListener())
671 ->SetCoalesceTimerForTest(timer_.get());
sathaf37203d2015-12-15 15:10:42672 master_->Init();
673 }
674
[email protected]3e90d4a2009-07-03 17:38:39675 VisitedLinkRenderProcessHostFactory vc_rph_factory_;
676
[email protected]4c3d9d62013-01-09 22:37:20677 TestVisitedLinkDelegate delegate_;
sammcaa222762016-09-21 07:30:42678 std::unique_ptr<base::MockTimer> timer_;
dcheng3f767dc32016-04-25 22:54:22679 std::unique_ptr<VisitedLinkMaster> master_;
[email protected]3e90d4a2009-07-03 17:38:39680};
681
wkorman1c17345b2015-11-12 03:02:42682TEST_F(VisitedLinkEventsTest, Coalescence) {
sathaf37203d2015-12-15 15:10:42683 // Waiting complete rebuild the table.
Gabriel Charette01507a22017-09-27 21:30:08684 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42685
sathaf37203d2015-12-15 15:10:42686 // After rebuild table expect reset event.
687 EXPECT_EQ(1, context()->reset_event_count());
688
[email protected]3e90d4a2009-07-03 17:38:39689 // add some URLs to master.
[email protected]3e90d4a2009-07-03 17:38:39690 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31691 master()->AddURL(GURL("http://acidtests.org/"));
692 master()->AddURL(GURL("http://google.com/"));
693 master()->AddURL(GURL("http://chromium.org/"));
[email protected]3e90d4a2009-07-03 17:38:39694 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting
[email protected]5016ee12012-10-26 23:56:31695 master()->AddURL(GURL("http://acidtests.org/"));
sammcaa222762016-09-21 07:30:42696 ASSERT_TRUE(timer_->IsRunning());
697 timer_->Fire();
[email protected]3e90d4a2009-07-03 17:38:39698
sammcaa222762016-09-21 07:30:42699 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39700
701 // We now should have 3 entries added in 1 event.
[email protected]28a66e252013-01-25 07:54:02702 EXPECT_EQ(3, context()->add_count());
703 EXPECT_EQ(1, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39704
705 // Test whether the coalescing continues by adding a few more URLs.
[email protected]5016ee12012-10-26 23:56:31706 master()->AddURL(GURL("http://google.com/chrome/"));
707 master()->AddURL(GURL("http://webkit.org/"));
708 master()->AddURL(GURL("http://acid3.acidtests.org/"));
[email protected]3e90d4a2009-07-03 17:38:39709
sammcaa222762016-09-21 07:30:42710 ASSERT_TRUE(timer_->IsRunning());
711 timer_->Fire();
712 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39713
714 // We should have 6 entries added in 2 events.
[email protected]28a66e252013-01-25 07:54:02715 EXPECT_EQ(6, context()->add_count());
716 EXPECT_EQ(2, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39717
718 // Test whether duplicate entries produce add events.
[email protected]5016ee12012-10-26 23:56:31719 master()->AddURL(GURL("http://acidtests.org/"));
sammcaa222762016-09-21 07:30:42720 EXPECT_FALSE(timer_->IsRunning());
721 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39722
723 // We should have no change in results.
[email protected]28a66e252013-01-25 07:54:02724 EXPECT_EQ(6, context()->add_count());
725 EXPECT_EQ(2, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39726
727 // Ensure that the coalescing does not resume after resetting.
[email protected]5016ee12012-10-26 23:56:31728 master()->AddURL(GURL("http://build.chromium.org/"));
sammcaa222762016-09-21 07:30:42729 EXPECT_TRUE(timer_->IsRunning());
[email protected]5016ee12012-10-26 23:56:31730 master()->DeleteAllURLs();
sammcaa222762016-09-21 07:30:42731 EXPECT_FALSE(timer_->IsRunning());
732 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39733
734 // We should have no change in results except for one new reset event.
[email protected]28a66e252013-01-25 07:54:02735 EXPECT_EQ(6, context()->add_count());
736 EXPECT_EQ(2, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42737 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39738}
739
[email protected]7fa7dd52011-04-24 01:09:42740TEST_F(VisitedLinkEventsTest, Basics) {
nasko4c0feb62015-06-05 18:37:06741 RenderViewHostTester::For(rvh())->CreateTestRenderView(
avif9a94c7e2016-10-14 15:54:11742 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false);
[email protected]6e8d64642009-08-10 15:27:19743
sathaf37203d2015-12-15 15:10:42744 // Waiting complete rebuild the table.
Gabriel Charette01507a22017-09-27 21:30:08745 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42746
sathaf37203d2015-12-15 15:10:42747 // After rebuild table expect reset event.
748 EXPECT_EQ(1, context()->reset_event_count());
749
[email protected]3e90d4a2009-07-03 17:38:39750 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31751 master()->AddURL(GURL("http://acidtests.org/"));
752 master()->AddURL(GURL("http://google.com/"));
753 master()->AddURL(GURL("http://chromium.org/"));
sammcaa222762016-09-21 07:30:42754 ASSERT_TRUE(timer_->IsRunning());
755 timer_->Fire();
756 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39757
758 // We now should have 1 add event.
[email protected]28a66e252013-01-25 07:54:02759 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42760 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39761
[email protected]5016ee12012-10-26 23:56:31762 master()->DeleteAllURLs();
[email protected]3e90d4a2009-07-03 17:38:39763
sammcaa222762016-09-21 07:30:42764 EXPECT_FALSE(timer_->IsRunning());
765 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39766
767 // We should have no change in add results, plus one new reset event.
[email protected]28a66e252013-01-25 07:54:02768 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42769 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39770}
771
[email protected]7fa7dd52011-04-24 01:09:42772TEST_F(VisitedLinkEventsTest, TabVisibility) {
nasko4c0feb62015-06-05 18:37:06773 RenderViewHostTester::For(rvh())->CreateTestRenderView(
avif9a94c7e2016-10-14 15:54:11774 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false);
[email protected]3e90d4a2009-07-03 17:38:39775
sathaf37203d2015-12-15 15:10:42776 // Waiting complete rebuild the table.
Gabriel Charette01507a22017-09-27 21:30:08777 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42778
sathaf37203d2015-12-15 15:10:42779 // After rebuild table expect reset event.
780 EXPECT_EQ(1, context()->reset_event_count());
781
[email protected]3e90d4a2009-07-03 17:38:39782 // Simulate tab becoming inactive.
bsepf3b00fa52017-02-11 00:06:38783 RenderViewHostTester::For(rvh())->SimulateWasHidden();
[email protected]3e90d4a2009-07-03 17:38:39784
785 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31786 master()->AddURL(GURL("http://acidtests.org/"));
787 master()->AddURL(GURL("http://google.com/"));
788 master()->AddURL(GURL("http://chromium.org/"));
sammcaa222762016-09-21 07:30:42789 ASSERT_TRUE(timer_->IsRunning());
790 timer_->Fire();
791 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39792
793 // We shouldn't have any events.
[email protected]28a66e252013-01-25 07:54:02794 EXPECT_EQ(0, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42795 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39796
797 // Simulate the tab becoming active.
bsepf3b00fa52017-02-11 00:06:38798 RenderViewHostTester::For(rvh())->SimulateWasShown();
sammcaa222762016-09-21 07:30:42799 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39800
801 // We should now have 3 add events, still no reset events.
[email protected]28a66e252013-01-25 07:54:02802 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42803 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39804
805 // Deactivate the tab again.
bsepf3b00fa52017-02-11 00:06:38806 RenderViewHostTester::For(rvh())->SimulateWasHidden();
[email protected]3e90d4a2009-07-03 17:38:39807
808 // Add a bunch of URLs (over 50) to exhaust the link event buffer.
809 for (int i = 0; i < 100; i++)
[email protected]5016ee12012-10-26 23:56:31810 master()->AddURL(TestURL(i));
[email protected]3e90d4a2009-07-03 17:38:39811
sammcaa222762016-09-21 07:30:42812 ASSERT_TRUE(timer_->IsRunning());
813 timer_->Fire();
814 context()->WaitForNoUpdate();
[email protected]3e90d4a2009-07-03 17:38:39815
816 // Again, no change in events until tab is active.
[email protected]28a66e252013-01-25 07:54:02817 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42818 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39819
820 // Activate the tab.
bsepf3b00fa52017-02-11 00:06:38821 RenderViewHostTester::For(rvh())->SimulateWasShown();
sammcaa222762016-09-21 07:30:42822 EXPECT_FALSE(timer_->IsRunning());
823 context()->WaitForUpdate();
[email protected]3e90d4a2009-07-03 17:38:39824
825 // We should have only one more reset event.
[email protected]28a66e252013-01-25 07:54:02826 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42827 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39828}
[email protected]1f371fa2013-01-23 00:35:14829
[email protected]c2134fb2013-01-23 04:28:52830// Tests that VisitedLink ignores renderer process creation notification for a
[email protected]28a66e252013-01-25 07:54:02831// different context.
[email protected]c2134fb2013-01-23 04:28:52832TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) {
sammcaa222762016-09-21 07:30:42833 content::TestBrowserContext different_context;
834 VisitCountingContext counting_context;
835 VisitRelayingRenderProcessHost different_process_host(&different_context,
836 &counting_context);
[email protected]c2134fb2013-01-23 04:28:52837
838 content::NotificationService::current()->Notify(
839 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
840 content::Source<content::RenderProcessHost>(&different_process_host),
841 content::NotificationService::NoDetails());
sammcaa222762016-09-21 07:30:42842 EXPECT_FALSE(counting_context.binding().is_bound());
[email protected]c2134fb2013-01-23 04:28:52843}
844
sathaf37203d2015-12-15 15:10:42845class VisitedLinkCompletelyResetEventTest : public VisitedLinkEventsTest {
846 public:
847 content::BrowserContext* CreateBrowserContext() override {
sammcaa222762016-09-21 07:30:42848 content::BrowserContext* context = new content::TestBrowserContext();
sathaf37203d2015-12-15 15:10:42849 CreateVisitedLinkFile(context);
850 CreateVisitedLinkMaster(context);
851 return context;
852 }
853
854 void CreateVisitedLinkFile(content::BrowserContext* browser_context) {
855 base::FilePath visited_file =
856 browser_context->GetPath().Append(FILE_PATH_LITERAL("Visited Links"));
dcheng3f767dc32016-04-25 22:54:22857 std::unique_ptr<VisitedLinkMaster> master(
sathaf37203d2015-12-15 15:10:42858 new VisitedLinkMaster(new TrackingVisitedLinkEventListener(),
859 &delegate_, true, true, visited_file, 0));
860 master->Init();
861 // Waiting complete create the table.
Gabriel Charette01507a22017-09-27 21:30:08862 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42863
864 master.reset();
865 // Wait for all pending file I/O to be completed.
Gabriel Charette01507a22017-09-27 21:30:08866 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42867 }
868};
869
870TEST_F(VisitedLinkCompletelyResetEventTest, LoadTable) {
871 // Waiting complete loading the table.
Gabriel Charette01507a22017-09-27 21:30:08872 content::RunAllTasksUntilIdle();
sathaf37203d2015-12-15 15:10:42873
sammcaa222762016-09-21 07:30:42874 context()->binding().FlushForTesting();
sathaf37203d2015-12-15 15:10:42875
876 // After load table expect completely reset event.
877 EXPECT_EQ(1, context()->completely_reset_event_count());
878}
879
[email protected]ab3eaeed2013-05-17 00:18:44880} // namespace visitedlink