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