[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 5 | #include <cstdio> |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 10 | #include "base/message_loop.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 12 | #include "base/process_util.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 13 | #include "base/run_loop.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/shared_memory.h" |
| 15 | #include "base/string_util.h" |
[email protected] | 853e01b | 2012-09-21 20:14:11 | [diff] [blame] | 16 | #include "base/time.h" |
[email protected] | 1f371fa | 2013-01-23 00:35:14 | [diff] [blame] | 17 | #include "components/visitedlink/browser/visitedlink_delegate.h" |
| 18 | #include "components/visitedlink/browser/visitedlink_event_listener.h" |
| 19 | #include "components/visitedlink/browser/visitedlink_master.h" |
| 20 | #include "components/visitedlink/common/visitedlink_messages.h" |
| 21 | #include "components/visitedlink/renderer/visitedlink_slave.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 22 | #include "content/public/browser/browser_thread.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 23 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 24 | #include "content/public/browser/notification_types.h" |
[email protected] | 08a932d5 | 2012-06-03 21:42:12 | [diff] [blame] | 25 | #include "content/public/test/mock_render_process_host.h" |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 26 | #include "content/public/test/test_browser_context.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 27 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | b1e3f20 | 2012-06-04 14:45:50 | [diff] [blame] | 28 | #include "content/public/test/test_renderer_host.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | #include "googleurl/src/gurl.h" |
| 30 | #include "testing/gtest/include/gtest/gtest.h" |
| 31 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 32 | using content::BrowserThread; |
[email protected] | eddd88670 | 2012-03-16 14:53:23 | [diff] [blame] | 33 | using content::MockRenderProcessHost; |
[email protected] | c025738 | 2012-03-12 20:15:34 | [diff] [blame] | 34 | using content::RenderViewHostTester; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 35 | |
[email protected] | ab3eaeed | 2013-05-17 00:18:44 | [diff] [blame] | 36 | namespace visitedlink { |
[email protected] | 1f371fa | 2013-01-23 00:35:14 | [diff] [blame] | 37 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | namespace { |
| 39 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 40 | typedef std::vector<GURL> URLs; |
| 41 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | // a nice long URL that we can append numbers to to get new URLs |
| 43 | const char g_test_prefix[] = |
| 44 | "http://www.google.com/products/foo/index.html?id=45028640526508376&seq="; |
| 45 | const int g_test_count = 1000; |
| 46 | |
| 47 | // Returns a test URL for index |i| |
| 48 | GURL TestURL(int i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 49 | return GURL(base::StringPrintf("%s%d", g_test_prefix, i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 52 | std::vector<VisitedLinkSlave*> g_slaves; |
| 53 | |
| 54 | class TestVisitedLinkDelegate : public VisitedLinkDelegate { |
| 55 | public: |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 56 | virtual void RebuildTable( |
| 57 | const scoped_refptr<URLEnumerator>& enumerator) OVERRIDE; |
| 58 | |
| 59 | void AddURLForRebuild(const GURL& url); |
| 60 | |
| 61 | private: |
| 62 | |
| 63 | URLs rebuild_urls_; |
| 64 | }; |
| 65 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 66 | void TestVisitedLinkDelegate::RebuildTable( |
| 67 | const scoped_refptr<URLEnumerator>& enumerator) { |
| 68 | for (URLs::const_iterator itr = rebuild_urls_.begin(); |
| 69 | itr != rebuild_urls_.end(); |
| 70 | ++itr) |
| 71 | enumerator->OnURL(*itr); |
| 72 | enumerator->OnComplete(true); |
| 73 | } |
| 74 | |
| 75 | void TestVisitedLinkDelegate::AddURLForRebuild(const GURL& url) { |
| 76 | rebuild_urls_.push_back(url); |
| 77 | } |
| 78 | |
| 79 | class TestURLIterator : public VisitedLinkMaster::URLIterator { |
| 80 | public: |
| 81 | explicit TestURLIterator(const URLs& urls); |
| 82 | |
| 83 | virtual const GURL& NextURL() OVERRIDE; |
| 84 | virtual bool HasNextURL() const OVERRIDE; |
| 85 | |
| 86 | private: |
| 87 | URLs::const_iterator iterator_; |
| 88 | URLs::const_iterator end_; |
| 89 | }; |
| 90 | |
| 91 | TestURLIterator::TestURLIterator(const URLs& urls) |
| 92 | : iterator_(urls.begin()), |
| 93 | end_(urls.end()) { |
| 94 | } |
| 95 | |
| 96 | const GURL& TestURLIterator::NextURL() { |
| 97 | return *(iterator_++); |
| 98 | } |
| 99 | |
| 100 | bool TestURLIterator::HasNextURL() const { |
| 101 | return iterator_ != end_; |
| 102 | } |
[email protected] | dba45e3 | 2013-01-04 20:27:15 | [diff] [blame] | 103 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 104 | } // namespace |
| 105 | |
| 106 | class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener { |
| 107 | public: |
| 108 | TrackingVisitedLinkEventListener() |
| 109 | : reset_count_(0), |
| 110 | add_count_(0) {} |
| 111 | |
[email protected] | 59e6b80 | 2013-02-07 13:29:33 | [diff] [blame] | 112 | virtual void NewTable(base::SharedMemory* table) OVERRIDE { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 113 | if (table) { |
| 114 | for (std::vector<VisitedLinkSlave>::size_type i = 0; |
| 115 | i < g_slaves.size(); i++) { |
| 116 | base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); |
| 117 | table->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle); |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 118 | g_slaves[i]->OnUpdateVisitedLinks(new_handle); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 119 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | } |
| 121 | } |
[email protected] | 59e6b80 | 2013-02-07 13:29:33 | [diff] [blame] | 122 | virtual void Add(VisitedLinkCommon::Fingerprint) OVERRIDE { add_count_++; } |
| 123 | virtual void Reset() OVERRIDE { reset_count_++; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 125 | void SetUp() { |
| 126 | reset_count_ = 0; |
| 127 | add_count_ = 0; |
| 128 | } |
| 129 | |
| 130 | int reset_count() const { return reset_count_; } |
| 131 | int add_count() const { return add_count_; } |
| 132 | |
| 133 | private: |
| 134 | int reset_count_; |
| 135 | int add_count_; |
| 136 | }; |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 137 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 138 | class VisitedLinkTest : public testing::Test { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | protected: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | // Initializes the visited link objects. Pass in the size that you want a |
| 141 | // freshly created table to be. 0 means use the default. |
| 142 | // |
| 143 | // |suppress_rebuild| is set when we're not testing rebuilding, see |
| 144 | // the VisitedLinkMaster constructor. |
| 145 | bool InitVisited(int initial_size, bool suppress_rebuild) { |
| 146 | // Initialize the visited link system. |
[email protected] | b42e6d8 | 2012-11-02 02:44:56 | [diff] [blame] | 147 | master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(), |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 148 | &delegate_, |
[email protected] | bff706b | 2013-01-25 00:15:01 | [diff] [blame] | 149 | true, |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 150 | suppress_rebuild, visited_file_, |
| 151 | initial_size)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | return master_->Init(); |
| 153 | } |
| 154 | |
| 155 | // May be called multiple times (some tests will do this to clear things, |
| 156 | // and TearDown will do this to make sure eveything is shiny before quitting. |
| 157 | void ClearDB() { |
| 158 | if (master_.get()) |
| 159 | master_.reset(NULL); |
| 160 | |
[email protected] | 3189013e | 2012-01-19 04:11:57 | [diff] [blame] | 161 | // Wait for all pending file I/O to be completed. |
| 162 | BrowserThread::GetBlockingPool()->FlushForTesting(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Loads the database from disk and makes sure that the same URLs are present |
| 166 | // as were generated by TestIO_Create(). This also checks the URLs with a |
| 167 | // slave to make sure it reads the data properly. |
| 168 | void Reload() { |
| 169 | // Clean up after our caller, who may have left the database open. |
| 170 | ClearDB(); |
| 171 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | ASSERT_TRUE(InitVisited(0, true)); |
| 173 | master_->DebugValidate(); |
| 174 | |
| 175 | // check that the table has the proper number of entries |
| 176 | int used_count = master_->GetUsedCount(); |
| 177 | ASSERT_EQ(used_count, g_test_count); |
| 178 | |
| 179 | // Create a slave database. |
| 180 | VisitedLinkSlave slave; |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 181 | base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 182 | master_->shared_memory()->ShareToProcess( |
| 183 | base::GetCurrentProcessHandle(), &new_handle); |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 184 | slave.OnUpdateVisitedLinks(new_handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | g_slaves.push_back(&slave); |
| 186 | |
| 187 | bool found; |
| 188 | for (int i = 0; i < g_test_count; i++) { |
| 189 | GURL cur = TestURL(i); |
| 190 | found = master_->IsVisited(cur); |
| 191 | EXPECT_TRUE(found) << "URL " << i << "not found in master."; |
| 192 | |
| 193 | found = slave.IsVisited(cur); |
| 194 | EXPECT_TRUE(found) << "URL " << i << "not found in slave."; |
| 195 | } |
| 196 | |
| 197 | // test some random URL so we know that it returns false sometimes too |
| 198 | found = master_->IsVisited(GURL("http://unfound.site/")); |
| 199 | ASSERT_FALSE(found); |
| 200 | found = slave.IsVisited(GURL("http://unfound.site/")); |
| 201 | ASSERT_FALSE(found); |
| 202 | |
| 203 | master_->DebugValidate(); |
| 204 | |
| 205 | g_slaves.clear(); |
| 206 | } |
| 207 | |
| 208 | // testing::Test |
| 209 | virtual void SetUp() { |
[email protected] | f708ed1 | 2010-09-23 12:18:34 | [diff] [blame] | 210 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 211 | |
| 212 | history_dir_ = temp_dir_.path().AppendASCII("VisitedLinkTest"); |
| 213 | ASSERT_TRUE(file_util::CreateDirectory(history_dir_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 215 | visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | virtual void TearDown() { |
| 219 | ClearDB(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 221 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 222 | base::ScopedTempDir temp_dir_; |
[email protected] | f708ed1 | 2010-09-23 12:18:34 | [diff] [blame] | 223 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | // Filenames for the services; |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 225 | base::FilePath history_dir_; |
| 226 | base::FilePath visited_file_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | |
| 228 | scoped_ptr<VisitedLinkMaster> master_; |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 229 | TestVisitedLinkDelegate delegate_; |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 230 | content::TestBrowserThreadBundle thread_bundle_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | }; |
| 232 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | // This test creates and reads some databases to make sure the data is |
| 234 | // preserved throughout those operations. |
| 235 | TEST_F(VisitedLinkTest, DatabaseIO) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | ASSERT_TRUE(InitVisited(0, true)); |
| 237 | |
| 238 | for (int i = 0; i < g_test_count; i++) |
| 239 | master_->AddURL(TestURL(i)); |
| 240 | |
| 241 | // Test that the database was written properly |
| 242 | Reload(); |
| 243 | } |
| 244 | |
| 245 | // Checks that we can delete things properly when there are collisions. |
| 246 | TEST_F(VisitedLinkTest, Delete) { |
| 247 | static const int32 kInitialSize = 17; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | ASSERT_TRUE(InitVisited(kInitialSize, true)); |
| 249 | |
| 250 | // Add a cluster from 14-17 wrapping around to 0. These will all hash to the |
| 251 | // same value. |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 252 | const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14; |
| 253 | const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14; |
| 254 | const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14; |
| 255 | const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14; |
| 256 | const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14; |
[email protected] | f8c42c9 | 2009-07-04 17:22:08 | [diff] [blame] | 257 | master_->AddFingerprint(kFingerprint0, false); // @14 |
| 258 | master_->AddFingerprint(kFingerprint1, false); // @15 |
| 259 | master_->AddFingerprint(kFingerprint2, false); // @16 |
| 260 | master_->AddFingerprint(kFingerprint3, false); // @0 |
| 261 | master_->AddFingerprint(kFingerprint4, false); // @1 |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | |
| 263 | // Deleting 14 should move the next value up one slot (we do not specify an |
| 264 | // order). |
| 265 | EXPECT_EQ(kFingerprint3, master_->hash_table_[0]); |
| 266 | master_->DeleteFingerprint(kFingerprint3, false); |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 267 | VisitedLinkCommon::Fingerprint zero_fingerprint = 0; |
| 268 | EXPECT_EQ(zero_fingerprint, master_->hash_table_[1]); |
| 269 | EXPECT_NE(zero_fingerprint, master_->hash_table_[0]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | |
| 271 | // Deleting the other four should leave the table empty. |
| 272 | master_->DeleteFingerprint(kFingerprint0, false); |
| 273 | master_->DeleteFingerprint(kFingerprint1, false); |
| 274 | master_->DeleteFingerprint(kFingerprint2, false); |
| 275 | master_->DeleteFingerprint(kFingerprint4, false); |
| 276 | |
| 277 | EXPECT_EQ(0, master_->used_items_); |
| 278 | for (int i = 0; i < kInitialSize; i++) |
[email protected] | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 279 | EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) << |
| 280 | "Hash table has values in it."; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // When we delete more than kBigDeleteThreshold we trigger different behavior |
| 284 | // where the entire file is rewritten. |
| 285 | TEST_F(VisitedLinkTest, BigDelete) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | ASSERT_TRUE(InitVisited(16381, true)); |
| 287 | |
| 288 | // Add the base set of URLs that won't be deleted. |
| 289 | // Reload() will test for these. |
| 290 | for (int32 i = 0; i < g_test_count; i++) |
| 291 | master_->AddURL(TestURL(i)); |
| 292 | |
| 293 | // Add more URLs than necessary to trigger this case. |
| 294 | const int kTestDeleteCount = VisitedLinkMaster::kBigDeleteThreshold + 2; |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 295 | URLs urls_to_delete; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | for (int32 i = g_test_count; i < g_test_count + kTestDeleteCount; i++) { |
| 297 | GURL url(TestURL(i)); |
| 298 | master_->AddURL(url); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 299 | urls_to_delete.push_back(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 300 | } |
| 301 | |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 302 | TestURLIterator iterator(urls_to_delete); |
| 303 | master_->DeleteURLs(&iterator); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | master_->DebugValidate(); |
| 305 | |
| 306 | Reload(); |
| 307 | } |
| 308 | |
| 309 | TEST_F(VisitedLinkTest, DeleteAll) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | ASSERT_TRUE(InitVisited(0, true)); |
| 311 | |
| 312 | { |
| 313 | VisitedLinkSlave slave; |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 314 | base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 315 | master_->shared_memory()->ShareToProcess( |
| 316 | base::GetCurrentProcessHandle(), &new_handle); |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 317 | slave.OnUpdateVisitedLinks(new_handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | g_slaves.push_back(&slave); |
| 319 | |
| 320 | // Add the test URLs. |
| 321 | for (int i = 0; i < g_test_count; i++) { |
| 322 | master_->AddURL(TestURL(i)); |
| 323 | ASSERT_EQ(i + 1, master_->GetUsedCount()); |
| 324 | } |
| 325 | master_->DebugValidate(); |
| 326 | |
| 327 | // Make sure the slave picked up the adds. |
| 328 | for (int i = 0; i < g_test_count; i++) |
| 329 | EXPECT_TRUE(slave.IsVisited(TestURL(i))); |
| 330 | |
| 331 | // Clear the table and make sure the slave picked it up. |
| 332 | master_->DeleteAllURLs(); |
| 333 | EXPECT_EQ(0, master_->GetUsedCount()); |
| 334 | for (int i = 0; i < g_test_count; i++) { |
| 335 | EXPECT_FALSE(master_->IsVisited(TestURL(i))); |
| 336 | EXPECT_FALSE(slave.IsVisited(TestURL(i))); |
| 337 | } |
| 338 | |
| 339 | // Close the database. |
| 340 | g_slaves.clear(); |
| 341 | ClearDB(); |
| 342 | } |
| 343 | |
| 344 | // Reopen and validate. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | ASSERT_TRUE(InitVisited(0, true)); |
| 346 | master_->DebugValidate(); |
| 347 | EXPECT_EQ(0, master_->GetUsedCount()); |
| 348 | for (int i = 0; i < g_test_count; i++) |
| 349 | EXPECT_FALSE(master_->IsVisited(TestURL(i))); |
| 350 | } |
| 351 | |
| 352 | // This tests that the master correctly resizes its tables when it gets too |
| 353 | // full, notifies its slaves of the change, and updates the disk. |
| 354 | TEST_F(VisitedLinkTest, Resizing) { |
| 355 | // Create a very small database. |
| 356 | const int32 initial_size = 17; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | ASSERT_TRUE(InitVisited(initial_size, true)); |
| 358 | |
| 359 | // ...and a slave |
| 360 | VisitedLinkSlave slave; |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 361 | base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); |
[email protected] | 9610ef24 | 2009-11-18 02:41:26 | [diff] [blame] | 362 | master_->shared_memory()->ShareToProcess( |
| 363 | base::GetCurrentProcessHandle(), &new_handle); |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 364 | slave.OnUpdateVisitedLinks(new_handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 365 | g_slaves.push_back(&slave); |
| 366 | |
| 367 | int32 used_count = master_->GetUsedCount(); |
| 368 | ASSERT_EQ(used_count, 0); |
| 369 | |
| 370 | for (int i = 0; i < g_test_count; i++) { |
| 371 | master_->AddURL(TestURL(i)); |
| 372 | used_count = master_->GetUsedCount(); |
| 373 | ASSERT_EQ(i + 1, used_count); |
| 374 | } |
| 375 | |
| 376 | // Verify that the table got resized sufficiently. |
| 377 | int32 table_size; |
| 378 | VisitedLinkCommon::Fingerprint* table; |
| 379 | master_->GetUsageStatistics(&table_size, &table); |
| 380 | used_count = master_->GetUsedCount(); |
| 381 | ASSERT_GT(table_size, used_count); |
| 382 | ASSERT_EQ(used_count, g_test_count) << |
| 383 | "table count doesn't match the # of things we added"; |
| 384 | |
| 385 | // Verify that the slave got the resize message and has the same |
| 386 | // table information. |
| 387 | int32 child_table_size; |
| 388 | VisitedLinkCommon::Fingerprint* child_table; |
| 389 | slave.GetUsageStatistics(&child_table_size, &child_table); |
| 390 | ASSERT_EQ(table_size, child_table_size); |
| 391 | for (int32 i = 0; i < table_size; i++) { |
| 392 | ASSERT_EQ(table[i], child_table[i]); |
| 393 | } |
| 394 | |
| 395 | master_->DebugValidate(); |
| 396 | g_slaves.clear(); |
| 397 | |
| 398 | // This tests that the file is written correctly by reading it in using |
| 399 | // a new database. |
| 400 | Reload(); |
| 401 | } |
| 402 | |
| 403 | // Tests that if the database doesn't exist, it will be rebuilt from history. |
| 404 | TEST_F(VisitedLinkTest, Rebuild) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | // Add half of our URLs to history. This needs to be done before we |
| 406 | // initialize the visited link DB. |
| 407 | int history_count = g_test_count / 2; |
| 408 | for (int i = 0; i < history_count; i++) |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 409 | delegate_.AddURLForRebuild(TestURL(i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | |
| 411 | // Initialize the visited link DB. Since the visited links file doesn't exist |
| 412 | // and we don't suppress history rebuilding, this will load from history. |
| 413 | ASSERT_TRUE(InitVisited(0, false)); |
| 414 | |
| 415 | // While the table is rebuilding, add the rest of the URLs to the visited |
| 416 | // link system. This isn't guaranteed to happen during the rebuild, so we |
| 417 | // can't be 100% sure we're testing the right thing, but in practice is. |
| 418 | // All the adds above will generally take some time queuing up on the |
| 419 | // history thread, and it will take a while to catch up to actually |
| 420 | // processing the rebuild that has queued behind it. We will generally |
| 421 | // finish adding all of the URLs before it has even found the first URL. |
| 422 | for (int i = history_count; i < g_test_count; i++) |
| 423 | master_->AddURL(TestURL(i)); |
| 424 | |
| 425 | // Add one more and then delete it. |
| 426 | master_->AddURL(TestURL(g_test_count)); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 427 | URLs urls_to_delete; |
| 428 | urls_to_delete.push_back(TestURL(g_test_count)); |
| 429 | TestURLIterator iterator(urls_to_delete); |
| 430 | master_->DeleteURLs(&iterator); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | |
| 432 | // Wait for the rebuild to complete. The task will terminate the message |
| 433 | // loop when the rebuild is done. There's no chance that the rebuild will |
| 434 | // complete before we set the task because the rebuild completion message |
| 435 | // is posted to the message loop; until we Run() it, rebuild can not |
| 436 | // complete. |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 437 | base::RunLoop run_loop; |
| 438 | master_->set_rebuild_complete_task(run_loop.QuitClosure()); |
| 439 | run_loop.Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 440 | |
| 441 | // Test that all URLs were written to the database properly. |
| 442 | Reload(); |
| 443 | |
| 444 | // Make sure the extra one was *not* written (Reload won't test this). |
| 445 | EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); |
| 446 | } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 447 | |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 448 | // Test that importing a large number of URLs will work |
| 449 | TEST_F(VisitedLinkTest, BigImport) { |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 450 | ASSERT_TRUE(InitVisited(0, false)); |
| 451 | |
| 452 | // Before the table rebuilds, add a large number of URLs |
| 453 | int total_count = VisitedLinkMaster::kDefaultTableSize + 10; |
| 454 | for (int i = 0; i < total_count; i++) |
| 455 | master_->AddURL(TestURL(i)); |
| 456 | |
| 457 | // Wait for the rebuild to complete. |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 458 | base::RunLoop run_loop; |
| 459 | master_->set_rebuild_complete_task(run_loop.QuitClosure()); |
| 460 | run_loop.Run(); |
[email protected] | aed132ed | 2009-08-19 22:44:12 | [diff] [blame] | 461 | |
| 462 | // Ensure that the right number of URLs are present |
| 463 | int used_count = master_->GetUsedCount(); |
| 464 | ASSERT_EQ(used_count, total_count); |
| 465 | } |
| 466 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 467 | TEST_F(VisitedLinkTest, Listener) { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 468 | ASSERT_TRUE(InitVisited(0, true)); |
| 469 | |
| 470 | // Add test URLs. |
| 471 | for (int i = 0; i < g_test_count; i++) { |
| 472 | master_->AddURL(TestURL(i)); |
| 473 | ASSERT_EQ(i + 1, master_->GetUsedCount()); |
| 474 | } |
| 475 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 476 | // Delete an URL. |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 477 | URLs urls_to_delete; |
| 478 | urls_to_delete.push_back(TestURL(0)); |
| 479 | TestURLIterator iterator(urls_to_delete); |
| 480 | master_->DeleteURLs(&iterator); |
| 481 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 482 | // ... and all of the remaining ones. |
| 483 | master_->DeleteAllURLs(); |
| 484 | |
[email protected] | b42e6d8 | 2012-11-02 02:44:56 | [diff] [blame] | 485 | TrackingVisitedLinkEventListener* listener = |
| 486 | static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); |
| 487 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 488 | // Verify that VisitedLinkMaster::Listener::Add was called for each added URL. |
[email protected] | b42e6d8 | 2012-11-02 02:44:56 | [diff] [blame] | 489 | EXPECT_EQ(g_test_count, listener->add_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 490 | // Verify that VisitedLinkMaster::Listener::Reset was called both when one and |
| 491 | // all URLs are deleted. |
[email protected] | b42e6d8 | 2012-11-02 02:44:56 | [diff] [blame] | 492 | EXPECT_EQ(2, listener->reset_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 495 | class VisitCountingContext : public content::TestBrowserContext { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 496 | public: |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 497 | VisitCountingContext() |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 498 | : add_count_(0), |
| 499 | add_event_count_(0), |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 500 | reset_event_count_(0), |
| 501 | new_table_count_(0) {} |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 502 | |
| 503 | void CountAddEvent(int by) { |
| 504 | add_count_ += by; |
| 505 | add_event_count_++; |
| 506 | } |
| 507 | |
| 508 | void CountResetEvent() { |
| 509 | reset_event_count_++; |
| 510 | } |
| 511 | |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 512 | void CountNewTable() { |
| 513 | new_table_count_++; |
| 514 | } |
| 515 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 516 | int add_count() const { return add_count_; } |
| 517 | int add_event_count() const { return add_event_count_; } |
| 518 | int reset_event_count() const { return reset_event_count_; } |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 519 | int new_table_count() const { return new_table_count_; } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 520 | |
| 521 | private: |
| 522 | int add_count_; |
| 523 | int add_event_count_; |
| 524 | int reset_event_count_; |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 525 | int new_table_count_; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 526 | }; |
| 527 | |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 528 | // Stub out as little as possible, borrowing from RenderProcessHost. |
[email protected] | b6a2f8de | 2012-01-31 17:28:49 | [diff] [blame] | 529 | class VisitRelayingRenderProcessHost : public MockRenderProcessHost { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 530 | public: |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 531 | explicit VisitRelayingRenderProcessHost( |
| 532 | content::BrowserContext* browser_context) |
[email protected] | b6a2f8de | 2012-01-31 17:28:49 | [diff] [blame] | 533 | : MockRenderProcessHost(browser_context), widgets_(0) { |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 534 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 535 | content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 536 | content::Source<RenderProcessHost>(this), |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 537 | content::NotificationService::NoDetails()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 538 | } |
| 539 | virtual ~VisitRelayingRenderProcessHost() { |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 540 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 541 | content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 542 | content::Source<content::RenderProcessHost>(this), |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 543 | content::NotificationService::NoDetails()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 544 | } |
| 545 | |
[email protected] | b6a2f8de | 2012-01-31 17:28:49 | [diff] [blame] | 546 | virtual void WidgetRestored() OVERRIDE { widgets_++; } |
| 547 | virtual void WidgetHidden() OVERRIDE { widgets_--; } |
| 548 | virtual int VisibleWidgetCount() const OVERRIDE { return widgets_; } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 549 | |
[email protected] | b6a2f8de | 2012-01-31 17:28:49 | [diff] [blame] | 550 | virtual bool Send(IPC::Message* msg) OVERRIDE { |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 551 | VisitCountingContext* counting_context = |
| 552 | static_cast<VisitCountingContext*>( |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 553 | GetBrowserContext()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 554 | |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 555 | if (msg->type() == ChromeViewMsg_VisitedLink_Add::ID) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 556 | PickleIterator iter(*msg); |
[email protected] | 7fa7dd5 | 2011-04-24 01:09:42 | [diff] [blame] | 557 | std::vector<uint64> fingerprints; |
| 558 | CHECK(IPC::ReadParam(msg, &iter, &fingerprints)); |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 559 | counting_context->CountAddEvent(fingerprints.size()); |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 560 | } else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) { |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 561 | counting_context->CountResetEvent(); |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 562 | } else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) { |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 563 | counting_context->CountNewTable(); |
[email protected] | 7fa7dd5 | 2011-04-24 01:09:42 | [diff] [blame] | 564 | } |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 565 | |
| 566 | delete msg; |
| 567 | return true; |
| 568 | } |
| 569 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 570 | private: |
[email protected] | b6a2f8de | 2012-01-31 17:28:49 | [diff] [blame] | 571 | int widgets_; |
| 572 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 573 | DISALLOW_COPY_AND_ASSIGN(VisitRelayingRenderProcessHost); |
| 574 | }; |
| 575 | |
| 576 | class VisitedLinkRenderProcessHostFactory |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 577 | : public content::RenderProcessHostFactory { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 578 | public: |
| 579 | VisitedLinkRenderProcessHostFactory() |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 580 | : content::RenderProcessHostFactory() {} |
| 581 | virtual content::RenderProcessHost* CreateRenderProcessHost( |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 582 | content::BrowserContext* browser_context) const OVERRIDE { |
| 583 | return new VisitRelayingRenderProcessHost(browser_context); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 584 | } |
| 585 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 586 | private: |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 587 | |
| 588 | DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); |
| 589 | }; |
| 590 | |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 591 | class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 592 | public: |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 593 | virtual void SetUp() { |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 594 | browser_context_.reset(new VisitCountingContext()); |
| 595 | master_.reset(new VisitedLinkMaster(context(), &delegate_, true)); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 596 | master_->Init(); |
[email protected] | 9a46725f | 2012-10-24 18:25:48 | [diff] [blame] | 597 | SetRenderProcessHostFactory(&vc_rph_factory_); |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 598 | content::RenderViewHostTestHarness::SetUp(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 599 | } |
| 600 | |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 601 | VisitCountingContext* context() const { |
| 602 | return static_cast<VisitCountingContext*>(browser_context_.get()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 603 | } |
| 604 | |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 605 | VisitedLinkMaster* master() const { |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 606 | return master_.get(); |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 607 | } |
| 608 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 609 | void WaitForCoalescense() { |
| 610 | // Let the timer fire. |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 611 | // |
| 612 | // TODO(ajwong): This is horrid! What is the right synchronization method? |
| 613 | base::RunLoop run_loop; |
[email protected] | d14c5b3 | 2013-05-06 05:25:11 | [diff] [blame] | 614 | base::MessageLoop::current()->PostDelayedTask( |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 615 | FROM_HERE, |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 616 | run_loop.QuitClosure(), |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 617 | base::TimeDelta::FromMilliseconds(110)); |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame^] | 618 | run_loop.Run(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | protected: |
| 622 | VisitedLinkRenderProcessHostFactory vc_rph_factory_; |
| 623 | |
| 624 | private: |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 625 | TestVisitedLinkDelegate delegate_; |
| 626 | scoped_ptr<VisitedLinkMaster> master_; |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 627 | }; |
| 628 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 629 | TEST_F(VisitedLinkEventsTest, Coalescense) { |
| 630 | // add some URLs to master. |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 631 | // Add a few URLs. |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 632 | master()->AddURL(GURL("http://acidtests.org/")); |
| 633 | master()->AddURL(GURL("http://google.com/")); |
| 634 | master()->AddURL(GURL("http://chromium.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 635 | // Just for kicks, add a duplicate URL. This shouldn't increase the resulting |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 636 | master()->AddURL(GURL("http://acidtests.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 637 | |
| 638 | // Make sure that coalescing actually occurs. There should be no links or |
| 639 | // events received by the renderer. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 640 | EXPECT_EQ(0, context()->add_count()); |
| 641 | EXPECT_EQ(0, context()->add_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 642 | |
| 643 | WaitForCoalescense(); |
| 644 | |
| 645 | // We now should have 3 entries added in 1 event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 646 | EXPECT_EQ(3, context()->add_count()); |
| 647 | EXPECT_EQ(1, context()->add_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 648 | |
| 649 | // Test whether the coalescing continues by adding a few more URLs. |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 650 | master()->AddURL(GURL("http://google.com/chrome/")); |
| 651 | master()->AddURL(GURL("http://webkit.org/")); |
| 652 | master()->AddURL(GURL("http://acid3.acidtests.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 653 | |
| 654 | WaitForCoalescense(); |
| 655 | |
| 656 | // We should have 6 entries added in 2 events. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 657 | EXPECT_EQ(6, context()->add_count()); |
| 658 | EXPECT_EQ(2, context()->add_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 659 | |
| 660 | // Test whether duplicate entries produce add events. |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 661 | master()->AddURL(GURL("http://acidtests.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 662 | |
| 663 | WaitForCoalescense(); |
| 664 | |
| 665 | // We should have no change in results. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 666 | EXPECT_EQ(6, context()->add_count()); |
| 667 | EXPECT_EQ(2, context()->add_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 668 | |
| 669 | // Ensure that the coalescing does not resume after resetting. |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 670 | master()->AddURL(GURL("http://build.chromium.org/")); |
| 671 | master()->DeleteAllURLs(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 672 | |
| 673 | WaitForCoalescense(); |
| 674 | |
| 675 | // We should have no change in results except for one new reset event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 676 | EXPECT_EQ(6, context()->add_count()); |
| 677 | EXPECT_EQ(2, context()->add_event_count()); |
| 678 | EXPECT_EQ(1, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 679 | } |
| 680 | |
[email protected] | 7fa7dd5 | 2011-04-24 01:09:42 | [diff] [blame] | 681 | TEST_F(VisitedLinkEventsTest, Basics) { |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 682 | RenderViewHostTester::For(rvh())->CreateRenderView(base::string16(), |
[email protected] | 7900bfdb | 2012-05-24 19:31:24 | [diff] [blame] | 683 | MSG_ROUTING_NONE, |
[email protected] | 7900bfdb | 2012-05-24 19:31:24 | [diff] [blame] | 684 | -1); |
[email protected] | 6e8d6464 | 2009-08-10 15:27:19 | [diff] [blame] | 685 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 686 | // Add a few URLs. |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 687 | master()->AddURL(GURL("http://acidtests.org/")); |
| 688 | master()->AddURL(GURL("http://google.com/")); |
| 689 | master()->AddURL(GURL("http://chromium.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 690 | |
| 691 | WaitForCoalescense(); |
| 692 | |
| 693 | // We now should have 1 add event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 694 | EXPECT_EQ(1, context()->add_event_count()); |
| 695 | EXPECT_EQ(0, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 696 | |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 697 | master()->DeleteAllURLs(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 698 | |
| 699 | WaitForCoalescense(); |
| 700 | |
| 701 | // We should have no change in add results, plus one new reset event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 702 | EXPECT_EQ(1, context()->add_event_count()); |
| 703 | EXPECT_EQ(1, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 704 | } |
| 705 | |
[email protected] | 7fa7dd5 | 2011-04-24 01:09:42 | [diff] [blame] | 706 | TEST_F(VisitedLinkEventsTest, TabVisibility) { |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 707 | RenderViewHostTester::For(rvh())->CreateRenderView(base::string16(), |
[email protected] | 7900bfdb | 2012-05-24 19:31:24 | [diff] [blame] | 708 | MSG_ROUTING_NONE, |
[email protected] | 7900bfdb | 2012-05-24 19:31:24 | [diff] [blame] | 709 | -1); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 710 | |
| 711 | // Simulate tab becoming inactive. |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 712 | RenderViewHostTester::For(rvh())->SimulateWasHidden(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 713 | |
| 714 | // Add a few URLs. |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 715 | master()->AddURL(GURL("http://acidtests.org/")); |
| 716 | master()->AddURL(GURL("http://google.com/")); |
| 717 | master()->AddURL(GURL("http://chromium.org/")); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 718 | |
| 719 | WaitForCoalescense(); |
| 720 | |
| 721 | // We shouldn't have any events. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 722 | EXPECT_EQ(0, context()->add_event_count()); |
| 723 | EXPECT_EQ(0, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 724 | |
| 725 | // Simulate the tab becoming active. |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 726 | RenderViewHostTester::For(rvh())->SimulateWasShown(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 727 | |
| 728 | // We should now have 3 add events, still no reset events. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 729 | EXPECT_EQ(1, context()->add_event_count()); |
| 730 | EXPECT_EQ(0, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 731 | |
| 732 | // Deactivate the tab again. |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 733 | RenderViewHostTester::For(rvh())->SimulateWasHidden(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 734 | |
| 735 | // Add a bunch of URLs (over 50) to exhaust the link event buffer. |
| 736 | for (int i = 0; i < 100; i++) |
[email protected] | 5016ee1 | 2012-10-26 23:56:31 | [diff] [blame] | 737 | master()->AddURL(TestURL(i)); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 738 | |
| 739 | WaitForCoalescense(); |
| 740 | |
| 741 | // Again, no change in events until tab is active. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 742 | EXPECT_EQ(1, context()->add_event_count()); |
| 743 | EXPECT_EQ(0, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 744 | |
| 745 | // Activate the tab. |
[email protected] | 4c3d9d6 | 2013-01-09 22:37:20 | [diff] [blame] | 746 | RenderViewHostTester::For(rvh())->SimulateWasShown(); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 747 | |
| 748 | // We should have only one more reset event. |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 749 | EXPECT_EQ(1, context()->add_event_count()); |
| 750 | EXPECT_EQ(1, context()->reset_event_count()); |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 751 | } |
[email protected] | 1f371fa | 2013-01-23 00:35:14 | [diff] [blame] | 752 | |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 753 | // Tests that VisitedLink ignores renderer process creation notification for a |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 754 | // different context. |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 755 | TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) { |
[email protected] | 28a66e25 | 2013-01-25 07:54:02 | [diff] [blame] | 756 | VisitCountingContext different_context; |
[email protected] | c2134fb | 2013-01-23 04:28:52 | [diff] [blame] | 757 | VisitRelayingRenderProcessHost different_process_host(&different_context); |
| 758 | |
| 759 | content::NotificationService::current()->Notify( |
| 760 | content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 761 | content::Source<content::RenderProcessHost>(&different_process_host), |
| 762 | content::NotificationService::NoDetails()); |
| 763 | WaitForCoalescense(); |
| 764 | |
| 765 | EXPECT_EQ(0, different_context.new_table_count()); |
| 766 | |
| 767 | } |
| 768 | |
[email protected] | ab3eaeed | 2013-05-17 00:18:44 | [diff] [blame] | 769 | } // namespace visitedlink |