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