Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: chrome/browser/visitedlink/visitedlink_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698