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