blob: 29a155d221f17673e74eba16c997cbb4a097bc82 [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>
[email protected]c38831a12011-10-28 12:44:499#include <string>
10#include <vector>
initial.commit09911bf2008-07-26 23:55:2911
thestig819adcc82014-09-10 22:24:5312#include "base/files/file_util.h"
skyostilb0daa012015-06-02 19:03:4813#include "base/location.h"
avi5dd91f82015-12-25 22:30:4614#include "base/macros.h"
[email protected]65654eeb2013-07-17 06:51:2415#include "base/memory/shared_memory.h"
[email protected]27730162013-07-23 14:51:5516#include "base/process/process_handle.h"
[email protected]ec04d3f2013-06-06 21:31:3917#include "base/run_loop.h"
skyostilb0daa012015-06-02 19:03:4818#include "base/single_thread_task_runner.h"
[email protected]c72674b2013-06-11 04:16:4319#include "base/strings/string_util.h"
skyostilb0daa012015-06-02 19:03:4820#include "base/thread_task_runner_handle.h"
[email protected]bbd8da92013-06-28 02:12:2021#include "base/time/time.h"
[email protected]1f371fa2013-01-23 00:35:1422#include "components/visitedlink/browser/visitedlink_delegate.h"
23#include "components/visitedlink/browser/visitedlink_event_listener.h"
24#include "components/visitedlink/browser/visitedlink_master.h"
25#include "components/visitedlink/common/visitedlink_messages.h"
26#include "components/visitedlink/renderer/visitedlink_slave.h"
[email protected]ec04d3f2013-06-06 21:31:3927#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0728#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1629#include "content/public/browser/notification_types.h"
[email protected]08a932d52012-06-03 21:42:1230#include "content/public/test/mock_render_process_host.h"
[email protected]4c3d9d62013-01-09 22:37:2031#include "content/public/test/test_browser_context.h"
[email protected]ec04d3f2013-06-06 21:31:3932#include "content/public/test/test_browser_thread_bundle.h"
[email protected]b1e3f202012-06-04 14:45:5033#include "content/public/test/test_renderer_host.h"
mtomaszf7d99a5c2014-09-15 16:23:4634#include "content/public/test/test_utils.h"
initial.commit09911bf2008-07-26 23:55:2935#include "testing/gtest/include/gtest/gtest.h"
[email protected]e3b599e2013-07-05 07:15:1736#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2937
[email protected]631bb742011-11-02 11:29:3938using content::BrowserThread;
[email protected]eddd886702012-03-16 14:53:2339using content::MockRenderProcessHost;
[email protected]c0257382012-03-12 20:15:3440using content::RenderViewHostTester;
[email protected]631bb742011-11-02 11:29:3941
[email protected]ab3eaeed2013-05-17 00:18:4442namespace visitedlink {
[email protected]1f371fa2013-01-23 00:35:1443
initial.commit09911bf2008-07-26 23:55:2944namespace {
45
[email protected]4c3d9d62013-01-09 22:37:2046typedef std::vector<GURL> URLs;
47
initial.commit09911bf2008-07-26 23:55:2948// a nice long URL that we can append numbers to to get new URLs
49const char g_test_prefix[] =
50 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq=";
51const int g_test_count = 1000;
52
53// Returns a test URL for index |i|
54GURL TestURL(int i) {
[email protected]7d3cbc92013-03-18 22:33:0455 return GURL(base::StringPrintf("%s%d", g_test_prefix, i));
initial.commit09911bf2008-07-26 23:55:2956}
57
[email protected]4c3d9d62013-01-09 22:37:2058std::vector<VisitedLinkSlave*> g_slaves;
59
60class TestVisitedLinkDelegate : public VisitedLinkDelegate {
61 public:
dcheng00ea022b2014-10-21 11:24:5662 void RebuildTable(const scoped_refptr<URLEnumerator>& enumerator) override;
[email protected]4c3d9d62013-01-09 22:37:2063
64 void AddURLForRebuild(const GURL& url);
65
66 private:
[email protected]4c3d9d62013-01-09 22:37:2067 URLs rebuild_urls_;
68};
69
[email protected]4c3d9d62013-01-09 22:37:2070void TestVisitedLinkDelegate::RebuildTable(
71 const scoped_refptr<URLEnumerator>& enumerator) {
72 for (URLs::const_iterator itr = rebuild_urls_.begin();
73 itr != rebuild_urls_.end();
74 ++itr)
75 enumerator->OnURL(*itr);
76 enumerator->OnComplete(true);
77}
78
79void TestVisitedLinkDelegate::AddURLForRebuild(const GURL& url) {
80 rebuild_urls_.push_back(url);
81}
82
83class TestURLIterator : public VisitedLinkMaster::URLIterator {
84 public:
85 explicit TestURLIterator(const URLs& urls);
86
dcheng00ea022b2014-10-21 11:24:5687 const GURL& NextURL() override;
88 bool HasNextURL() const override;
[email protected]4c3d9d62013-01-09 22:37:2089
90 private:
91 URLs::const_iterator iterator_;
92 URLs::const_iterator end_;
93};
94
95TestURLIterator::TestURLIterator(const URLs& urls)
96 : iterator_(urls.begin()),
97 end_(urls.end()) {
98}
99
100const GURL& TestURLIterator::NextURL() {
101 return *(iterator_++);
102}
103
104bool TestURLIterator::HasNextURL() const {
105 return iterator_ != end_;
106}
[email protected]dba45e32013-01-04 20:27:15107
[email protected]3e90d4a2009-07-03 17:38:39108} // namespace
109
110class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener {
111 public:
112 TrackingVisitedLinkEventListener()
113 : reset_count_(0),
sathaf37203d2015-12-15 15:10:42114 completely_reset_count_(0),
[email protected]3e90d4a2009-07-03 17:38:39115 add_count_(0) {}
116
dcheng00ea022b2014-10-21 11:24:56117 void NewTable(base::SharedMemory* table) override {
[email protected]3e90d4a2009-07-03 17:38:39118 if (table) {
119 for (std::vector<VisitedLinkSlave>::size_type i = 0;
120 i < g_slaves.size(); i++) {
121 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
122 table->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle);
[email protected]8d97ade2011-04-14 18:17:08123 g_slaves[i]->OnUpdateVisitedLinks(new_handle);
[email protected]3e90d4a2009-07-03 17:38:39124 }
initial.commit09911bf2008-07-26 23:55:29125 }
126 }
dcheng00ea022b2014-10-21 11:24:56127 void Add(VisitedLinkCommon::Fingerprint) override { add_count_++; }
sathaf37203d2015-12-15 15:10:42128 void Reset(bool invalidate_hashes) override {
129 if (invalidate_hashes)
130 completely_reset_count_++;
131 else
132 reset_count_++;
133 }
initial.commit09911bf2008-07-26 23:55:29134
[email protected]3e90d4a2009-07-03 17:38:39135 void SetUp() {
136 reset_count_ = 0;
137 add_count_ = 0;
138 }
139
140 int reset_count() const { return reset_count_; }
sathaf37203d2015-12-15 15:10:42141 int completely_reset_count() { return completely_reset_count_; }
[email protected]3e90d4a2009-07-03 17:38:39142 int add_count() const { return add_count_; }
143
144 private:
145 int reset_count_;
sathaf37203d2015-12-15 15:10:42146 int completely_reset_count_;
[email protected]3e90d4a2009-07-03 17:38:39147 int add_count_;
148};
[email protected]c2c998c2009-01-27 19:08:39149
[email protected]583844c2011-08-27 00:38:35150class VisitedLinkTest : public testing::Test {
initial.commit09911bf2008-07-26 23:55:29151 protected:
initial.commit09911bf2008-07-26 23:55:29152 // Initializes the visited link objects. Pass in the size that you want a
153 // freshly created table to be. 0 means use the default.
154 //
155 // |suppress_rebuild| is set when we're not testing rebuilding, see
156 // the VisitedLinkMaster constructor.
sathaf37203d2015-12-15 15:10:42157 //
158 // |wait_for_io_complete| wait for result of async loading.
159 bool InitVisited(int initial_size,
160 bool suppress_rebuild,
161 bool wait_for_io_complete) {
initial.commit09911bf2008-07-26 23:55:29162 // Initialize the visited link system.
[email protected]b42e6d82012-11-02 02:44:56163 master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(),
[email protected]4c3d9d62013-01-09 22:37:20164 &delegate_,
[email protected]bff706b2013-01-25 00:15:01165 true,
[email protected]3e90d4a2009-07-03 17:38:39166 suppress_rebuild, visited_file_,
167 initial_size));
sathaf37203d2015-12-15 15:10:42168 bool result = master_->Init();
169 if (result && wait_for_io_complete) {
170 // Wait for all pending file I/O to be completed.
171 content::RunAllBlockingPoolTasksUntilIdle();
172 }
173 return result;
initial.commit09911bf2008-07-26 23:55:29174 }
175
176 // May be called multiple times (some tests will do this to clear things,
177 // and TearDown will do this to make sure eveything is shiny before quitting.
178 void ClearDB() {
179 if (master_.get())
180 master_.reset(NULL);
181
[email protected]3189013e2012-01-19 04:11:57182 // Wait for all pending file I/O to be completed.
mtomaszf7d99a5c2014-09-15 16:23:46183 content::RunAllBlockingPoolTasksUntilIdle();
initial.commit09911bf2008-07-26 23:55:29184 }
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
188 // slave to make sure it reads the data properly.
189 void Reload() {
190 // Clean up after our caller, who may have left the database open.
191 ClearDB();
192
sathaf37203d2015-12-15 15:10:42193 ASSERT_TRUE(InitVisited(0, true, true));
194
initial.commit09911bf2008-07-26 23:55:29195 master_->DebugValidate();
196
197 // check that the table has the proper number of entries
198 int used_count = master_->GetUsedCount();
199 ASSERT_EQ(used_count, g_test_count);
200
201 // Create a slave database.
202 VisitedLinkSlave slave;
[email protected]76aac1e2009-03-16 16:45:36203 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
[email protected]9610ef242009-11-18 02:41:26204 master_->shared_memory()->ShareToProcess(
205 base::GetCurrentProcessHandle(), &new_handle);
[email protected]8d97ade2011-04-14 18:17:08206 slave.OnUpdateVisitedLinks(new_handle);
initial.commit09911bf2008-07-26 23:55:29207 g_slaves.push_back(&slave);
208
209 bool found;
210 for (int i = 0; i < g_test_count; i++) {
211 GURL cur = TestURL(i);
212 found = master_->IsVisited(cur);
213 EXPECT_TRUE(found) << "URL " << i << "not found in master.";
214
215 found = slave.IsVisited(cur);
216 EXPECT_TRUE(found) << "URL " << i << "not found in slave.";
217 }
218
219 // test some random URL so we know that it returns false sometimes too
220 found = master_->IsVisited(GURL("http://unfound.site/"));
221 ASSERT_FALSE(found);
222 found = slave.IsVisited(GURL("http://unfound.site/"));
223 ASSERT_FALSE(found);
224
225 master_->DebugValidate();
226
227 g_slaves.clear();
228 }
229
230 // testing::Test
dcheng30a1b1542014-10-29 21:27:50231 void SetUp() override {
[email protected]f708ed12010-09-23 12:18:34232 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
233
234 history_dir_ = temp_dir_.path().AppendASCII("VisitedLinkTest");
[email protected]426d1c92013-12-03 20:08:54235 ASSERT_TRUE(base::CreateDirectory(history_dir_));
initial.commit09911bf2008-07-26 23:55:29236
[email protected]c2c998c2009-01-27 19:08:39237 visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks"));
initial.commit09911bf2008-07-26 23:55:29238 }
239
dcheng30a1b1542014-10-29 21:27:50240 void TearDown() override { ClearDB(); }
[email protected]f0a51fb52009-03-05 12:46:38241
[email protected]ea1a3f62012-11-16 20:34:23242 base::ScopedTempDir temp_dir_;
[email protected]f708ed12010-09-23 12:18:34243
initial.commit09911bf2008-07-26 23:55:29244 // Filenames for the services;
[email protected]9e275712013-02-10 19:20:14245 base::FilePath history_dir_;
246 base::FilePath visited_file_;
initial.commit09911bf2008-07-26 23:55:29247
248 scoped_ptr<VisitedLinkMaster> master_;
[email protected]4c3d9d62013-01-09 22:37:20249 TestVisitedLinkDelegate delegate_;
[email protected]ec04d3f2013-06-06 21:31:39250 content::TestBrowserThreadBundle thread_bundle_;
initial.commit09911bf2008-07-26 23:55:29251};
252
initial.commit09911bf2008-07-26 23:55:29253// This test creates and reads some databases to make sure the data is
254// preserved throughout those operations.
255TEST_F(VisitedLinkTest, DatabaseIO) {
sathaf37203d2015-12-15 15:10:42256 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29257
258 for (int i = 0; i < g_test_count; i++)
259 master_->AddURL(TestURL(i));
260
261 // Test that the database was written properly
262 Reload();
263}
264
265// Checks that we can delete things properly when there are collisions.
266TEST_F(VisitedLinkTest, Delete) {
avi5dd91f82015-12-25 22:30:46267 static const int32_t kInitialSize = 17;
sathaf37203d2015-12-15 15:10:42268 ASSERT_TRUE(InitVisited(kInitialSize, true, true));
initial.commit09911bf2008-07-26 23:55:29269
270 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the
271 // same value.
[email protected]c2c998c2009-01-27 19:08:39272 const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14;
273 const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14;
274 const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14;
275 const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14;
276 const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14;
[email protected]f8c42c92009-07-04 17:22:08277 master_->AddFingerprint(kFingerprint0, false); // @14
278 master_->AddFingerprint(kFingerprint1, false); // @15
279 master_->AddFingerprint(kFingerprint2, false); // @16
280 master_->AddFingerprint(kFingerprint3, false); // @0
281 master_->AddFingerprint(kFingerprint4, false); // @1
initial.commit09911bf2008-07-26 23:55:29282
283 // Deleting 14 should move the next value up one slot (we do not specify an
284 // order).
285 EXPECT_EQ(kFingerprint3, master_->hash_table_[0]);
286 master_->DeleteFingerprint(kFingerprint3, false);
[email protected]c2c998c2009-01-27 19:08:39287 VisitedLinkCommon::Fingerprint zero_fingerprint = 0;
288 EXPECT_EQ(zero_fingerprint, master_->hash_table_[1]);
289 EXPECT_NE(zero_fingerprint, master_->hash_table_[0]);
initial.commit09911bf2008-07-26 23:55:29290
291 // Deleting the other four should leave the table empty.
292 master_->DeleteFingerprint(kFingerprint0, false);
293 master_->DeleteFingerprint(kFingerprint1, false);
294 master_->DeleteFingerprint(kFingerprint2, false);
295 master_->DeleteFingerprint(kFingerprint4, false);
296
297 EXPECT_EQ(0, master_->used_items_);
298 for (int i = 0; i < kInitialSize; i++)
[email protected]c2c998c2009-01-27 19:08:39299 EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) <<
300 "Hash table has values in it.";
initial.commit09911bf2008-07-26 23:55:29301}
302
303// When we delete more than kBigDeleteThreshold we trigger different behavior
304// where the entire file is rewritten.
305TEST_F(VisitedLinkTest, BigDelete) {
sathaf37203d2015-12-15 15:10:42306 ASSERT_TRUE(InitVisited(16381, true, true));
initial.commit09911bf2008-07-26 23:55:29307
308 // Add the base set of URLs that won't be deleted.
309 // Reload() will test for these.
avi5dd91f82015-12-25 22:30:46310 for (int32_t i = 0; i < g_test_count; i++)
initial.commit09911bf2008-07-26 23:55:29311 master_->AddURL(TestURL(i));
312
313 // Add more URLs than necessary to trigger this case.
314 const int kTestDeleteCount = VisitedLinkMaster::kBigDeleteThreshold + 2;
[email protected]4c3d9d62013-01-09 22:37:20315 URLs urls_to_delete;
avi5dd91f82015-12-25 22:30:46316 for (int32_t i = g_test_count; i < g_test_count + kTestDeleteCount; i++) {
initial.commit09911bf2008-07-26 23:55:29317 GURL url(TestURL(i));
318 master_->AddURL(url);
[email protected]4c3d9d62013-01-09 22:37:20319 urls_to_delete.push_back(url);
initial.commit09911bf2008-07-26 23:55:29320 }
321
[email protected]4c3d9d62013-01-09 22:37:20322 TestURLIterator iterator(urls_to_delete);
323 master_->DeleteURLs(&iterator);
initial.commit09911bf2008-07-26 23:55:29324 master_->DebugValidate();
325
326 Reload();
327}
328
329TEST_F(VisitedLinkTest, DeleteAll) {
sathaf37203d2015-12-15 15:10:42330 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29331
332 {
333 VisitedLinkSlave slave;
[email protected]76aac1e2009-03-16 16:45:36334 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
[email protected]9610ef242009-11-18 02:41:26335 master_->shared_memory()->ShareToProcess(
336 base::GetCurrentProcessHandle(), &new_handle);
[email protected]8d97ade2011-04-14 18:17:08337 slave.OnUpdateVisitedLinks(new_handle);
initial.commit09911bf2008-07-26 23:55:29338 g_slaves.push_back(&slave);
339
340 // Add the test URLs.
341 for (int i = 0; i < g_test_count; i++) {
342 master_->AddURL(TestURL(i));
343 ASSERT_EQ(i + 1, master_->GetUsedCount());
344 }
345 master_->DebugValidate();
346
347 // Make sure the slave picked up the adds.
348 for (int i = 0; i < g_test_count; i++)
349 EXPECT_TRUE(slave.IsVisited(TestURL(i)));
350
351 // Clear the table and make sure the slave picked it up.
352 master_->DeleteAllURLs();
353 EXPECT_EQ(0, master_->GetUsedCount());
354 for (int i = 0; i < g_test_count; i++) {
355 EXPECT_FALSE(master_->IsVisited(TestURL(i)));
356 EXPECT_FALSE(slave.IsVisited(TestURL(i)));
357 }
358
359 // Close the database.
360 g_slaves.clear();
361 ClearDB();
362 }
363
364 // Reopen and validate.
sathaf37203d2015-12-15 15:10:42365 ASSERT_TRUE(InitVisited(0, true, true));
initial.commit09911bf2008-07-26 23:55:29366 master_->DebugValidate();
367 EXPECT_EQ(0, master_->GetUsedCount());
368 for (int i = 0; i < g_test_count; i++)
369 EXPECT_FALSE(master_->IsVisited(TestURL(i)));
370}
371
372// This tests that the master correctly resizes its tables when it gets too
373// full, notifies its slaves of the change, and updates the disk.
374TEST_F(VisitedLinkTest, Resizing) {
375 // Create a very small database.
avi5dd91f82015-12-25 22:30:46376 const int32_t initial_size = 17;
sathaf37203d2015-12-15 15:10:42377 ASSERT_TRUE(InitVisited(initial_size, true, true));
initial.commit09911bf2008-07-26 23:55:29378
379 // ...and a slave
380 VisitedLinkSlave slave;
[email protected]76aac1e2009-03-16 16:45:36381 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
[email protected]9610ef242009-11-18 02:41:26382 master_->shared_memory()->ShareToProcess(
383 base::GetCurrentProcessHandle(), &new_handle);
[email protected]8d97ade2011-04-14 18:17:08384 slave.OnUpdateVisitedLinks(new_handle);
initial.commit09911bf2008-07-26 23:55:29385 g_slaves.push_back(&slave);
386
avi5dd91f82015-12-25 22:30:46387 int32_t used_count = master_->GetUsedCount();
initial.commit09911bf2008-07-26 23:55:29388 ASSERT_EQ(used_count, 0);
389
390 for (int i = 0; i < g_test_count; i++) {
391 master_->AddURL(TestURL(i));
392 used_count = master_->GetUsedCount();
393 ASSERT_EQ(i + 1, used_count);
394 }
395
396 // Verify that the table got resized sufficiently.
avi5dd91f82015-12-25 22:30:46397 int32_t table_size;
initial.commit09911bf2008-07-26 23:55:29398 VisitedLinkCommon::Fingerprint* table;
399 master_->GetUsageStatistics(&table_size, &table);
400 used_count = master_->GetUsedCount();
401 ASSERT_GT(table_size, used_count);
402 ASSERT_EQ(used_count, g_test_count) <<
403 "table count doesn't match the # of things we added";
404
405 // Verify that the slave got the resize message and has the same
406 // table information.
avi5dd91f82015-12-25 22:30:46407 int32_t child_table_size;
initial.commit09911bf2008-07-26 23:55:29408 VisitedLinkCommon::Fingerprint* child_table;
409 slave.GetUsageStatistics(&child_table_size, &child_table);
410 ASSERT_EQ(table_size, child_table_size);
avi5dd91f82015-12-25 22:30:46411 for (int32_t i = 0; i < table_size; i++) {
initial.commit09911bf2008-07-26 23:55:29412 ASSERT_EQ(table[i], child_table[i]);
413 }
414
415 master_->DebugValidate();
416 g_slaves.clear();
417
418 // This tests that the file is written correctly by reading it in using
419 // a new database.
420 Reload();
421}
422
423// Tests that if the database doesn't exist, it will be rebuilt from history.
424TEST_F(VisitedLinkTest, Rebuild) {
initial.commit09911bf2008-07-26 23:55:29425 // Add half of our URLs to history. This needs to be done before we
426 // initialize the visited link DB.
427 int history_count = g_test_count / 2;
428 for (int i = 0; i < history_count; i++)
[email protected]4c3d9d62013-01-09 22:37:20429 delegate_.AddURLForRebuild(TestURL(i));
initial.commit09911bf2008-07-26 23:55:29430
431 // Initialize the visited link DB. Since the visited links file doesn't exist
432 // and we don't suppress history rebuilding, this will load from history.
sathaf37203d2015-12-15 15:10:42433 ASSERT_TRUE(InitVisited(0, false, false));
initial.commit09911bf2008-07-26 23:55:29434
435 // While the table is rebuilding, add the rest of the URLs to the visited
436 // link system. This isn't guaranteed to happen during the rebuild, so we
437 // can't be 100% sure we're testing the right thing, but in practice is.
438 // All the adds above will generally take some time queuing up on the
439 // history thread, and it will take a while to catch up to actually
440 // processing the rebuild that has queued behind it. We will generally
441 // finish adding all of the URLs before it has even found the first URL.
442 for (int i = history_count; i < g_test_count; i++)
443 master_->AddURL(TestURL(i));
444
445 // Add one more and then delete it.
446 master_->AddURL(TestURL(g_test_count));
[email protected]4c3d9d62013-01-09 22:37:20447 URLs urls_to_delete;
448 urls_to_delete.push_back(TestURL(g_test_count));
449 TestURLIterator iterator(urls_to_delete);
450 master_->DeleteURLs(&iterator);
initial.commit09911bf2008-07-26 23:55:29451
452 // Wait for the rebuild to complete. The task will terminate the message
453 // loop when the rebuild is done. There's no chance that the rebuild will
454 // complete before we set the task because the rebuild completion message
455 // is posted to the message loop; until we Run() it, rebuild can not
456 // complete.
[email protected]ec04d3f2013-06-06 21:31:39457 base::RunLoop run_loop;
458 master_->set_rebuild_complete_task(run_loop.QuitClosure());
459 run_loop.Run();
initial.commit09911bf2008-07-26 23:55:29460
461 // Test that all URLs were written to the database properly.
462 Reload();
463
464 // Make sure the extra one was *not* written (Reload won't test this).
465 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count)));
466}
[email protected]3e90d4a2009-07-03 17:38:39467
[email protected]aed132ed2009-08-19 22:44:12468// Test that importing a large number of URLs will work
469TEST_F(VisitedLinkTest, BigImport) {
sathaf37203d2015-12-15 15:10:42470 ASSERT_TRUE(InitVisited(0, false, false));
[email protected]aed132ed2009-08-19 22:44:12471
472 // Before the table rebuilds, add a large number of URLs
473 int total_count = VisitedLinkMaster::kDefaultTableSize + 10;
474 for (int i = 0; i < total_count; i++)
475 master_->AddURL(TestURL(i));
476
477 // Wait for the rebuild to complete.
[email protected]ec04d3f2013-06-06 21:31:39478 base::RunLoop run_loop;
479 master_->set_rebuild_complete_task(run_loop.QuitClosure());
480 run_loop.Run();
[email protected]aed132ed2009-08-19 22:44:12481
482 // Ensure that the right number of URLs are present
483 int used_count = master_->GetUsedCount();
484 ASSERT_EQ(used_count, total_count);
485}
486
[email protected]3e90d4a2009-07-03 17:38:39487TEST_F(VisitedLinkTest, Listener) {
sathaf37203d2015-12-15 15:10:42488 ASSERT_TRUE(InitVisited(0, true, true));
489
490 TrackingVisitedLinkEventListener* listener =
491 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener());
492
493 // Verify that VisitedLinkMaster::Listener::Reset(true) was never called when
494 // the table was created.
495 EXPECT_EQ(0, listener->completely_reset_count());
[email protected]3e90d4a2009-07-03 17:38:39496
497 // Add test URLs.
498 for (int i = 0; i < g_test_count; i++) {
499 master_->AddURL(TestURL(i));
500 ASSERT_EQ(i + 1, master_->GetUsedCount());
501 }
502
[email protected]3e90d4a2009-07-03 17:38:39503 // Delete an URL.
[email protected]4c3d9d62013-01-09 22:37:20504 URLs urls_to_delete;
505 urls_to_delete.push_back(TestURL(0));
506 TestURLIterator iterator(urls_to_delete);
507 master_->DeleteURLs(&iterator);
508
[email protected]3e90d4a2009-07-03 17:38:39509 // ... and all of the remaining ones.
510 master_->DeleteAllURLs();
511
[email protected]3e90d4a2009-07-03 17:38:39512 // Verify that VisitedLinkMaster::Listener::Add was called for each added URL.
[email protected]b42e6d82012-11-02 02:44:56513 EXPECT_EQ(g_test_count, listener->add_count());
[email protected]3e90d4a2009-07-03 17:38:39514 // Verify that VisitedLinkMaster::Listener::Reset was called both when one and
515 // all URLs are deleted.
[email protected]b42e6d82012-11-02 02:44:56516 EXPECT_EQ(2, listener->reset_count());
sathaf37203d2015-12-15 15:10:42517
518 ClearDB();
519
520 ASSERT_TRUE(InitVisited(0, true, true));
521
522 listener =
523 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener());
524 // Verify that VisitedLinkMaster::Listener::Reset(true) was called when the
525 // table was loaded.
526 EXPECT_EQ(1, listener->completely_reset_count());
[email protected]3e90d4a2009-07-03 17:38:39527}
528
[email protected]28a66e252013-01-25 07:54:02529class VisitCountingContext : public content::TestBrowserContext {
[email protected]3e90d4a2009-07-03 17:38:39530 public:
[email protected]28a66e252013-01-25 07:54:02531 VisitCountingContext()
[email protected]3e90d4a2009-07-03 17:38:39532 : add_count_(0),
533 add_event_count_(0),
[email protected]c2134fb2013-01-23 04:28:52534 reset_event_count_(0),
sathaf37203d2015-12-15 15:10:42535 completely_reset_event_count_(0),
[email protected]c2134fb2013-01-23 04:28:52536 new_table_count_(0) {}
[email protected]3e90d4a2009-07-03 17:38:39537
538 void CountAddEvent(int by) {
539 add_count_ += by;
540 add_event_count_++;
541 }
542
543 void CountResetEvent() {
544 reset_event_count_++;
545 }
546
sathaf37203d2015-12-15 15:10:42547 void CountCompletelyResetEvent() {
548 completely_reset_event_count_++;
549 }
550
[email protected]c2134fb2013-01-23 04:28:52551 void CountNewTable() {
552 new_table_count_++;
553 }
554
[email protected]3e90d4a2009-07-03 17:38:39555 int add_count() const { return add_count_; }
556 int add_event_count() const { return add_event_count_; }
557 int reset_event_count() const { return reset_event_count_; }
sathaf37203d2015-12-15 15:10:42558 int completely_reset_event_count() const {
559 return completely_reset_event_count_;
560 }
[email protected]c2134fb2013-01-23 04:28:52561 int new_table_count() const { return new_table_count_; }
[email protected]3e90d4a2009-07-03 17:38:39562
563 private:
564 int add_count_;
565 int add_event_count_;
566 int reset_event_count_;
sathaf37203d2015-12-15 15:10:42567 int completely_reset_event_count_;
[email protected]c2134fb2013-01-23 04:28:52568 int new_table_count_;
[email protected]3e90d4a2009-07-03 17:38:39569};
570
[email protected]f3b1a082011-11-18 00:34:30571// Stub out as little as possible, borrowing from RenderProcessHost.
[email protected]b6a2f8de2012-01-31 17:28:49572class VisitRelayingRenderProcessHost : public MockRenderProcessHost {
[email protected]3e90d4a2009-07-03 17:38:39573 public:
[email protected]3d7474ff2011-07-27 17:47:37574 explicit VisitRelayingRenderProcessHost(
575 content::BrowserContext* browser_context)
[email protected]b6a2f8de2012-01-31 17:28:49576 : MockRenderProcessHost(browser_context), widgets_(0) {
[email protected]ad50def52011-10-19 23:17:07577 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27578 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]6c2381d2011-10-19 02:52:53579 content::Source<RenderProcessHost>(this),
[email protected]ad50def52011-10-19 23:17:07580 content::NotificationService::NoDetails());
[email protected]3e90d4a2009-07-03 17:38:39581 }
dcheng00ea022b2014-10-21 11:24:56582 ~VisitRelayingRenderProcessHost() override {
[email protected]ad50def52011-10-19 23:17:07583 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27584 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]f3b1a082011-11-18 00:34:30585 content::Source<content::RenderProcessHost>(this),
[email protected]ad50def52011-10-19 23:17:07586 content::NotificationService::NoDetails());
[email protected]3e90d4a2009-07-03 17:38:39587 }
588
dcheng00ea022b2014-10-21 11:24:56589 void WidgetRestored() override { widgets_++; }
590 void WidgetHidden() override { widgets_--; }
591 int VisibleWidgetCount() const override { return widgets_; }
[email protected]3e90d4a2009-07-03 17:38:39592
dcheng00ea022b2014-10-21 11:24:56593 bool Send(IPC::Message* msg) override {
[email protected]28a66e252013-01-25 07:54:02594 VisitCountingContext* counting_context =
595 static_cast<VisitCountingContext*>(
[email protected]4c3d9d62013-01-09 22:37:20596 GetBrowserContext());
[email protected]3e90d4a2009-07-03 17:38:39597
[email protected]2ccf45c2011-08-19 23:35:50598 if (msg->type() == ChromeViewMsg_VisitedLink_Add::ID) {
brettwbd4d7112015-06-03 04:29:25599 base::PickleIterator iter(*msg);
avi5dd91f82015-12-25 22:30:46600 std::vector<uint64_t> fingerprints;
[email protected]7fa7dd52011-04-24 01:09:42601 CHECK(IPC::ReadParam(msg, &iter, &fingerprints));
[email protected]28a66e252013-01-25 07:54:02602 counting_context->CountAddEvent(fingerprints.size());
[email protected]2ccf45c2011-08-19 23:35:50603 } else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) {
sathaf37203d2015-12-15 15:10:42604 base::PickleIterator iter(*msg);
605 bool invalidate_hashes;
606 CHECK(IPC::ReadParam(msg, &iter, &invalidate_hashes));
607 if (invalidate_hashes)
608 counting_context->CountCompletelyResetEvent();
609 else
610 counting_context->CountResetEvent();
[email protected]c2134fb2013-01-23 04:28:52611 } else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) {
[email protected]28a66e252013-01-25 07:54:02612 counting_context->CountNewTable();
[email protected]7fa7dd52011-04-24 01:09:42613 }
[email protected]3e90d4a2009-07-03 17:38:39614
615 delete msg;
616 return true;
617 }
618
[email protected]3e90d4a2009-07-03 17:38:39619 private:
[email protected]b6a2f8de2012-01-31 17:28:49620 int widgets_;
621
[email protected]3e90d4a2009-07-03 17:38:39622 DISALLOW_COPY_AND_ASSIGN(VisitRelayingRenderProcessHost);
623};
624
625class VisitedLinkRenderProcessHostFactory
[email protected]f3b1a082011-11-18 00:34:30626 : public content::RenderProcessHostFactory {
[email protected]3e90d4a2009-07-03 17:38:39627 public:
628 VisitedLinkRenderProcessHostFactory()
[email protected]f3b1a082011-11-18 00:34:30629 : content::RenderProcessHostFactory() {}
dcheng00ea022b2014-10-21 11:24:56630 content::RenderProcessHost* CreateRenderProcessHost(
[email protected]f681b072013-06-06 21:53:12631 content::BrowserContext* browser_context,
mostynbfe59f482014-10-06 15:04:46632 content::SiteInstance* site_instance) const override {
[email protected]3d7474ff2011-07-27 17:47:37633 return new VisitRelayingRenderProcessHost(browser_context);
[email protected]3e90d4a2009-07-03 17:38:39634 }
635
[email protected]3e90d4a2009-07-03 17:38:39636 private:
[email protected]3e90d4a2009-07-03 17:38:39637 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory);
638};
639
[email protected]28a66e252013-01-25 07:54:02640class VisitedLinkEventsTest : public content::RenderViewHostTestHarness {
[email protected]3e90d4a2009-07-03 17:38:39641 public:
dcheng30a1b1542014-10-29 21:27:50642 void SetUp() override {
[email protected]9a46725f2012-10-24 18:25:48643 SetRenderProcessHostFactory(&vc_rph_factory_);
[email protected]4c3d9d62013-01-09 22:37:20644 content::RenderViewHostTestHarness::SetUp();
[email protected]3e90d4a2009-07-03 17:38:39645 }
646
wkorman1c17345b2015-11-12 03:02:42647 void TearDown() override {
648 // Explicitly destroy the master before proceeding with the rest
649 // of teardown because it posts a task to close a file handle, and
650 // we need to make sure we've finished all file related work
651 // before our superclass sets about destroying the scoped temp
652 // directory.
653 master_.reset();
654 RenderViewHostTestHarness::TearDown();
655 }
656
dcheng00ea022b2014-10-21 11:24:56657 content::BrowserContext* CreateBrowserContext() override {
[email protected]cc99c9f2013-07-15 12:26:09658 VisitCountingContext* context = new VisitCountingContext();
sathaf37203d2015-12-15 15:10:42659 CreateVisitedLinkMaster(context);
[email protected]cc99c9f2013-07-15 12:26:09660 return context;
661 }
662
663 VisitCountingContext* context() {
664 return static_cast<VisitCountingContext*>(browser_context());
[email protected]3e90d4a2009-07-03 17:38:39665 }
666
[email protected]5016ee12012-10-26 23:56:31667 VisitedLinkMaster* master() const {
[email protected]4c3d9d62013-01-09 22:37:20668 return master_.get();
[email protected]5016ee12012-10-26 23:56:31669 }
670
wkorman1c17345b2015-11-12 03:02:42671 void WaitForCoalescence() {
[email protected]3e90d4a2009-07-03 17:38:39672 // Let the timer fire.
[email protected]ec04d3f2013-06-06 21:31:39673 //
674 // TODO(ajwong): This is horrid! What is the right synchronization method?
675 base::RunLoop run_loop;
skyostilb0daa012015-06-02 19:03:48676 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
677 FROM_HERE, run_loop.QuitClosure(),
[email protected]02798a982012-01-27 00:45:33678 base::TimeDelta::FromMilliseconds(110));
[email protected]ec04d3f2013-06-06 21:31:39679 run_loop.Run();
[email protected]3e90d4a2009-07-03 17:38:39680 }
681
682 protected:
sathaf37203d2015-12-15 15:10:42683 void CreateVisitedLinkMaster(content::BrowserContext* browser_context) {
684 master_.reset(new VisitedLinkMaster(browser_context, &delegate_, true));
685 master_->Init();
686 }
687
[email protected]3e90d4a2009-07-03 17:38:39688 VisitedLinkRenderProcessHostFactory vc_rph_factory_;
689
[email protected]4c3d9d62013-01-09 22:37:20690 TestVisitedLinkDelegate delegate_;
691 scoped_ptr<VisitedLinkMaster> master_;
[email protected]3e90d4a2009-07-03 17:38:39692};
693
wkorman1c17345b2015-11-12 03:02:42694TEST_F(VisitedLinkEventsTest, Coalescence) {
sathaf37203d2015-12-15 15:10:42695 // Waiting complete rebuild the table.
696 content::RunAllBlockingPoolTasksUntilIdle();
697
698 WaitForCoalescence();
699
700 // After rebuild table expect reset event.
701 EXPECT_EQ(1, context()->reset_event_count());
702
[email protected]3e90d4a2009-07-03 17:38:39703 // add some URLs to master.
[email protected]3e90d4a2009-07-03 17:38:39704 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31705 master()->AddURL(GURL("http://acidtests.org/"));
706 master()->AddURL(GURL("http://google.com/"));
707 master()->AddURL(GURL("http://chromium.org/"));
[email protected]3e90d4a2009-07-03 17:38:39708 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting
[email protected]5016ee12012-10-26 23:56:31709 master()->AddURL(GURL("http://acidtests.org/"));
[email protected]3e90d4a2009-07-03 17:38:39710
711 // Make sure that coalescing actually occurs. There should be no links or
712 // events received by the renderer.
[email protected]28a66e252013-01-25 07:54:02713 EXPECT_EQ(0, context()->add_count());
714 EXPECT_EQ(0, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39715
wkorman1c17345b2015-11-12 03:02:42716 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39717
718 // We now should have 3 entries added in 1 event.
[email protected]28a66e252013-01-25 07:54:02719 EXPECT_EQ(3, context()->add_count());
720 EXPECT_EQ(1, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39721
722 // Test whether the coalescing continues by adding a few more URLs.
[email protected]5016ee12012-10-26 23:56:31723 master()->AddURL(GURL("http://google.com/chrome/"));
724 master()->AddURL(GURL("http://webkit.org/"));
725 master()->AddURL(GURL("http://acid3.acidtests.org/"));
[email protected]3e90d4a2009-07-03 17:38:39726
wkorman1c17345b2015-11-12 03:02:42727 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39728
729 // We should have 6 entries added in 2 events.
[email protected]28a66e252013-01-25 07:54:02730 EXPECT_EQ(6, context()->add_count());
731 EXPECT_EQ(2, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39732
733 // Test whether duplicate entries produce add events.
[email protected]5016ee12012-10-26 23:56:31734 master()->AddURL(GURL("http://acidtests.org/"));
[email protected]3e90d4a2009-07-03 17:38:39735
wkorman1c17345b2015-11-12 03:02:42736 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39737
738 // We should have no change in results.
[email protected]28a66e252013-01-25 07:54:02739 EXPECT_EQ(6, context()->add_count());
740 EXPECT_EQ(2, context()->add_event_count());
[email protected]3e90d4a2009-07-03 17:38:39741
742 // Ensure that the coalescing does not resume after resetting.
[email protected]5016ee12012-10-26 23:56:31743 master()->AddURL(GURL("http://build.chromium.org/"));
744 master()->DeleteAllURLs();
[email protected]3e90d4a2009-07-03 17:38:39745
wkorman1c17345b2015-11-12 03:02:42746 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39747
748 // We should have no change in results except for one new reset event.
[email protected]28a66e252013-01-25 07:54:02749 EXPECT_EQ(6, context()->add_count());
750 EXPECT_EQ(2, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42751 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39752}
753
[email protected]7fa7dd52011-04-24 01:09:42754TEST_F(VisitedLinkEventsTest, Basics) {
nasko4c0feb62015-06-05 18:37:06755 RenderViewHostTester::For(rvh())->CreateTestRenderView(
[email protected]5a7100d2014-05-19 01:29:04756 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false);
[email protected]6e8d64642009-08-10 15:27:19757
sathaf37203d2015-12-15 15:10:42758 // Waiting complete rebuild the table.
759 content::RunAllBlockingPoolTasksUntilIdle();
760
761 WaitForCoalescence();
762
763 // After rebuild table expect reset event.
764 EXPECT_EQ(1, context()->reset_event_count());
765
[email protected]3e90d4a2009-07-03 17:38:39766 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31767 master()->AddURL(GURL("http://acidtests.org/"));
768 master()->AddURL(GURL("http://google.com/"));
769 master()->AddURL(GURL("http://chromium.org/"));
[email protected]3e90d4a2009-07-03 17:38:39770
wkorman1c17345b2015-11-12 03:02:42771 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39772
773 // We now should have 1 add event.
[email protected]28a66e252013-01-25 07:54:02774 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42775 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39776
[email protected]5016ee12012-10-26 23:56:31777 master()->DeleteAllURLs();
[email protected]3e90d4a2009-07-03 17:38:39778
wkorman1c17345b2015-11-12 03:02:42779 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39780
781 // We should have no change in add results, plus one new reset event.
[email protected]28a66e252013-01-25 07:54:02782 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42783 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39784}
785
[email protected]7fa7dd52011-04-24 01:09:42786TEST_F(VisitedLinkEventsTest, TabVisibility) {
nasko4c0feb62015-06-05 18:37:06787 RenderViewHostTester::For(rvh())->CreateTestRenderView(
[email protected]5a7100d2014-05-19 01:29:04788 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false);
[email protected]3e90d4a2009-07-03 17:38:39789
sathaf37203d2015-12-15 15:10:42790 // Waiting complete rebuild the table.
791 content::RunAllBlockingPoolTasksUntilIdle();
792
793 WaitForCoalescence();
794
795 // After rebuild table expect reset event.
796 EXPECT_EQ(1, context()->reset_event_count());
797
[email protected]3e90d4a2009-07-03 17:38:39798 // Simulate tab becoming inactive.
[email protected]4c3d9d62013-01-09 22:37:20799 RenderViewHostTester::For(rvh())->SimulateWasHidden();
[email protected]3e90d4a2009-07-03 17:38:39800
801 // Add a few URLs.
[email protected]5016ee12012-10-26 23:56:31802 master()->AddURL(GURL("http://acidtests.org/"));
803 master()->AddURL(GURL("http://google.com/"));
804 master()->AddURL(GURL("http://chromium.org/"));
[email protected]3e90d4a2009-07-03 17:38:39805
wkorman1c17345b2015-11-12 03:02:42806 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39807
808 // We shouldn't have any events.
[email protected]28a66e252013-01-25 07:54:02809 EXPECT_EQ(0, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42810 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39811
812 // Simulate the tab becoming active.
[email protected]4c3d9d62013-01-09 22:37:20813 RenderViewHostTester::For(rvh())->SimulateWasShown();
[email protected]3e90d4a2009-07-03 17:38:39814
815 // We should now have 3 add events, still no reset events.
[email protected]28a66e252013-01-25 07:54:02816 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42817 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39818
819 // Deactivate the tab again.
[email protected]4c3d9d62013-01-09 22:37:20820 RenderViewHostTester::For(rvh())->SimulateWasHidden();
[email protected]3e90d4a2009-07-03 17:38:39821
822 // Add a bunch of URLs (over 50) to exhaust the link event buffer.
823 for (int i = 0; i < 100; i++)
[email protected]5016ee12012-10-26 23:56:31824 master()->AddURL(TestURL(i));
[email protected]3e90d4a2009-07-03 17:38:39825
wkorman1c17345b2015-11-12 03:02:42826 WaitForCoalescence();
[email protected]3e90d4a2009-07-03 17:38:39827
828 // Again, no change in events until tab is active.
[email protected]28a66e252013-01-25 07:54:02829 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42830 EXPECT_EQ(1, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39831
832 // Activate the tab.
[email protected]4c3d9d62013-01-09 22:37:20833 RenderViewHostTester::For(rvh())->SimulateWasShown();
[email protected]3e90d4a2009-07-03 17:38:39834
835 // We should have only one more reset event.
[email protected]28a66e252013-01-25 07:54:02836 EXPECT_EQ(1, context()->add_event_count());
sathaf37203d2015-12-15 15:10:42837 EXPECT_EQ(2, context()->reset_event_count());
[email protected]3e90d4a2009-07-03 17:38:39838}
[email protected]1f371fa2013-01-23 00:35:14839
[email protected]c2134fb2013-01-23 04:28:52840// Tests that VisitedLink ignores renderer process creation notification for a
[email protected]28a66e252013-01-25 07:54:02841// different context.
[email protected]c2134fb2013-01-23 04:28:52842TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) {
[email protected]28a66e252013-01-25 07:54:02843 VisitCountingContext different_context;
[email protected]c2134fb2013-01-23 04:28:52844 VisitRelayingRenderProcessHost different_process_host(&different_context);
845
846 content::NotificationService::current()->Notify(
847 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
848 content::Source<content::RenderProcessHost>(&different_process_host),
849 content::NotificationService::NoDetails());
wkorman1c17345b2015-11-12 03:02:42850 WaitForCoalescence();
[email protected]c2134fb2013-01-23 04:28:52851
852 EXPECT_EQ(0, different_context.new_table_count());
[email protected]c2134fb2013-01-23 04:28:52853}
854
sathaf37203d2015-12-15 15:10:42855class VisitedLinkCompletelyResetEventTest : public VisitedLinkEventsTest {
856 public:
857 content::BrowserContext* CreateBrowserContext() override {
858 VisitCountingContext* context = new VisitCountingContext();
859 CreateVisitedLinkFile(context);
860 CreateVisitedLinkMaster(context);
861 return context;
862 }
863
864 void CreateVisitedLinkFile(content::BrowserContext* browser_context) {
865 base::FilePath visited_file =
866 browser_context->GetPath().Append(FILE_PATH_LITERAL("Visited Links"));
867 scoped_ptr<VisitedLinkMaster> master(
868 new VisitedLinkMaster(new TrackingVisitedLinkEventListener(),
869 &delegate_, true, true, visited_file, 0));
870 master->Init();
871 // Waiting complete create the table.
872 content::RunAllBlockingPoolTasksUntilIdle();
873
874 master.reset();
875 // Wait for all pending file I/O to be completed.
876 content::RunAllBlockingPoolTasksUntilIdle();
877 }
878};
879
880TEST_F(VisitedLinkCompletelyResetEventTest, LoadTable) {
881 // Waiting complete loading the table.
882 content::RunAllBlockingPoolTasksUntilIdle();
883
884 WaitForCoalescence();
885
886 // After load table expect completely reset event.
887 EXPECT_EQ(1, context()->completely_reset_event_count());
888}
889
[email protected]ab3eaeed2013-05-17 00:18:44890} // namespace visitedlink