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/run_loop.h" |
13 #include "base/shared_memory.h" | 14 #include "base/shared_memory.h" |
14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
15 #include "base/time.h" | 16 #include "base/time.h" |
16 #include "components/visitedlink/browser/visitedlink_delegate.h" | 17 #include "components/visitedlink/browser/visitedlink_delegate.h" |
17 #include "components/visitedlink/browser/visitedlink_event_listener.h" | 18 #include "components/visitedlink/browser/visitedlink_event_listener.h" |
18 #include "components/visitedlink/browser/visitedlink_master.h" | 19 #include "components/visitedlink/browser/visitedlink_master.h" |
19 #include "components/visitedlink/common/visitedlink_messages.h" | 20 #include "components/visitedlink/common/visitedlink_messages.h" |
20 #include "components/visitedlink/renderer/visitedlink_slave.h" | 21 #include "components/visitedlink/renderer/visitedlink_slave.h" |
| 22 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/notification_types.h" | 24 #include "content/public/browser/notification_types.h" |
23 #include "content/public/test/mock_render_process_host.h" | 25 #include "content/public/test/mock_render_process_host.h" |
24 #include "content/public/test/test_browser_context.h" | 26 #include "content/public/test/test_browser_context.h" |
25 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
26 #include "content/public/test/test_renderer_host.h" | 28 #include "content/public/test/test_renderer_host.h" |
27 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
29 | 31 |
30 using content::BrowserThread; | 32 using content::BrowserThread; |
31 using content::MockRenderProcessHost; | 33 using content::MockRenderProcessHost; |
32 using content::RenderViewHostTester; | 34 using content::RenderViewHostTester; |
33 | 35 |
34 namespace visitedlink { | 36 namespace visitedlink { |
35 | 37 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 int reset_count() const { return reset_count_; } | 130 int reset_count() const { return reset_count_; } |
129 int add_count() const { return add_count_; } | 131 int add_count() const { return add_count_; } |
130 | 132 |
131 private: | 133 private: |
132 int reset_count_; | 134 int reset_count_; |
133 int add_count_; | 135 int add_count_; |
134 }; | 136 }; |
135 | 137 |
136 class VisitedLinkTest : public testing::Test { | 138 class VisitedLinkTest : public testing::Test { |
137 protected: | 139 protected: |
138 VisitedLinkTest() | |
139 : ui_thread_(BrowserThread::UI, &message_loop_), | |
140 file_thread_(BrowserThread::FILE, &message_loop_) {} | |
141 // Initializes the visited link objects. Pass in the size that you want a | 140 // Initializes the visited link objects. Pass in the size that you want a |
142 // freshly created table to be. 0 means use the default. | 141 // freshly created table to be. 0 means use the default. |
143 // | 142 // |
144 // |suppress_rebuild| is set when we're not testing rebuilding, see | 143 // |suppress_rebuild| is set when we're not testing rebuilding, see |
145 // the VisitedLinkMaster constructor. | 144 // the VisitedLinkMaster constructor. |
146 bool InitVisited(int initial_size, bool suppress_rebuild) { | 145 bool InitVisited(int initial_size, bool suppress_rebuild) { |
147 // Initialize the visited link system. | 146 // Initialize the visited link system. |
148 master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(), | 147 master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(), |
149 &delegate_, | 148 &delegate_, |
150 true, | 149 true, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 214 |
216 visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks")); | 215 visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks")); |
217 } | 216 } |
218 | 217 |
219 virtual void TearDown() { | 218 virtual void TearDown() { |
220 ClearDB(); | 219 ClearDB(); |
221 } | 220 } |
222 | 221 |
223 base::ScopedTempDir temp_dir_; | 222 base::ScopedTempDir temp_dir_; |
224 | 223 |
225 base::MessageLoop message_loop_; | |
226 content::TestBrowserThread ui_thread_; | |
227 content::TestBrowserThread file_thread_; | |
228 | |
229 // Filenames for the services; | 224 // Filenames for the services; |
230 base::FilePath history_dir_; | 225 base::FilePath history_dir_; |
231 base::FilePath visited_file_; | 226 base::FilePath visited_file_; |
232 | 227 |
233 scoped_ptr<VisitedLinkMaster> master_; | 228 scoped_ptr<VisitedLinkMaster> master_; |
234 TestVisitedLinkDelegate delegate_; | 229 TestVisitedLinkDelegate delegate_; |
| 230 content::TestBrowserThreadBundle thread_bundle_; |
235 }; | 231 }; |
236 | 232 |
237 // This test creates and reads some databases to make sure the data is | 233 // This test creates and reads some databases to make sure the data is |
238 // preserved throughout those operations. | 234 // preserved throughout those operations. |
239 TEST_F(VisitedLinkTest, DatabaseIO) { | 235 TEST_F(VisitedLinkTest, DatabaseIO) { |
240 ASSERT_TRUE(InitVisited(0, true)); | 236 ASSERT_TRUE(InitVisited(0, true)); |
241 | 237 |
242 for (int i = 0; i < g_test_count; i++) | 238 for (int i = 0; i < g_test_count; i++) |
243 master_->AddURL(TestURL(i)); | 239 master_->AddURL(TestURL(i)); |
244 | 240 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 URLs urls_to_delete; | 427 URLs urls_to_delete; |
432 urls_to_delete.push_back(TestURL(g_test_count)); | 428 urls_to_delete.push_back(TestURL(g_test_count)); |
433 TestURLIterator iterator(urls_to_delete); | 429 TestURLIterator iterator(urls_to_delete); |
434 master_->DeleteURLs(&iterator); | 430 master_->DeleteURLs(&iterator); |
435 | 431 |
436 // Wait for the rebuild to complete. The task will terminate the message | 432 // Wait for the rebuild to complete. The task will terminate the message |
437 // loop when the rebuild is done. There's no chance that the rebuild will | 433 // loop when the rebuild is done. There's no chance that the rebuild will |
438 // complete before we set the task because the rebuild completion message | 434 // complete before we set the task because the rebuild completion message |
439 // is posted to the message loop; until we Run() it, rebuild can not | 435 // is posted to the message loop; until we Run() it, rebuild can not |
440 // complete. | 436 // complete. |
441 master_->set_rebuild_complete_task(base::MessageLoop::QuitClosure()); | 437 base::RunLoop run_loop; |
442 base::MessageLoop::current()->Run(); | 438 master_->set_rebuild_complete_task(run_loop.QuitClosure()); |
| 439 run_loop.Run(); |
443 | 440 |
444 // Test that all URLs were written to the database properly. | 441 // Test that all URLs were written to the database properly. |
445 Reload(); | 442 Reload(); |
446 | 443 |
447 // Make sure the extra one was *not* written (Reload won't test this). | 444 // Make sure the extra one was *not* written (Reload won't test this). |
448 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); | 445 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); |
449 } | 446 } |
450 | 447 |
451 // Test that importing a large number of URLs will work | 448 // Test that importing a large number of URLs will work |
452 TEST_F(VisitedLinkTest, BigImport) { | 449 TEST_F(VisitedLinkTest, BigImport) { |
453 ASSERT_TRUE(InitVisited(0, false)); | 450 ASSERT_TRUE(InitVisited(0, false)); |
454 | 451 |
455 // Before the table rebuilds, add a large number of URLs | 452 // Before the table rebuilds, add a large number of URLs |
456 int total_count = VisitedLinkMaster::kDefaultTableSize + 10; | 453 int total_count = VisitedLinkMaster::kDefaultTableSize + 10; |
457 for (int i = 0; i < total_count; i++) | 454 for (int i = 0; i < total_count; i++) |
458 master_->AddURL(TestURL(i)); | 455 master_->AddURL(TestURL(i)); |
459 | 456 |
460 // Wait for the rebuild to complete. | 457 // Wait for the rebuild to complete. |
461 master_->set_rebuild_complete_task(base::MessageLoop::QuitClosure()); | 458 base::RunLoop run_loop; |
462 base::MessageLoop::current()->Run(); | 459 master_->set_rebuild_complete_task(run_loop.QuitClosure()); |
| 460 run_loop.Run(); |
463 | 461 |
464 // Ensure that the right number of URLs are present | 462 // Ensure that the right number of URLs are present |
465 int used_count = master_->GetUsedCount(); | 463 int used_count = master_->GetUsedCount(); |
466 ASSERT_EQ(used_count, total_count); | 464 ASSERT_EQ(used_count, total_count); |
467 } | 465 } |
468 | 466 |
469 TEST_F(VisitedLinkTest, Listener) { | 467 TEST_F(VisitedLinkTest, Listener) { |
470 ASSERT_TRUE(InitVisited(0, true)); | 468 ASSERT_TRUE(InitVisited(0, true)); |
471 | 469 |
472 // Add test URLs. | 470 // Add test URLs. |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 return new VisitRelayingRenderProcessHost(browser_context); | 583 return new VisitRelayingRenderProcessHost(browser_context); |
586 } | 584 } |
587 | 585 |
588 private: | 586 private: |
589 | 587 |
590 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); | 588 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); |
591 }; | 589 }; |
592 | 590 |
593 class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { | 591 class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { |
594 public: | 592 public: |
595 VisitedLinkEventsTest() | |
596 : ui_thread_(BrowserThread::UI, &message_loop_), | |
597 file_thread_(BrowserThread::FILE, &message_loop_) {} | |
598 virtual ~VisitedLinkEventsTest() {} | |
599 virtual void SetUp() { | 593 virtual void SetUp() { |
600 browser_context_.reset(new VisitCountingContext()); | 594 browser_context_.reset(new VisitCountingContext()); |
601 master_.reset(new VisitedLinkMaster(context(), &delegate_, true)); | 595 master_.reset(new VisitedLinkMaster(context(), &delegate_, true)); |
602 master_->Init(); | 596 master_->Init(); |
603 SetRenderProcessHostFactory(&vc_rph_factory_); | 597 SetRenderProcessHostFactory(&vc_rph_factory_); |
604 content::RenderViewHostTestHarness::SetUp(); | 598 content::RenderViewHostTestHarness::SetUp(); |
605 } | 599 } |
606 | 600 |
607 VisitCountingContext* context() const { | 601 VisitCountingContext* context() const { |
608 return static_cast<VisitCountingContext*>(browser_context_.get()); | 602 return static_cast<VisitCountingContext*>(browser_context_.get()); |
609 } | 603 } |
610 | 604 |
611 VisitedLinkMaster* master() const { | 605 VisitedLinkMaster* master() const { |
612 return master_.get(); | 606 return master_.get(); |
613 } | 607 } |
614 | 608 |
615 void WaitForCoalescense() { | 609 void WaitForCoalescense() { |
616 // Let the timer fire. | 610 // Let the timer fire. |
| 611 // |
| 612 // TODO(ajwong): This is horrid! What is the right synchronization method? |
| 613 base::RunLoop run_loop; |
617 base::MessageLoop::current()->PostDelayedTask( | 614 base::MessageLoop::current()->PostDelayedTask( |
618 FROM_HERE, | 615 FROM_HERE, |
619 base::MessageLoop::QuitClosure(), | 616 run_loop.QuitClosure(), |
620 base::TimeDelta::FromMilliseconds(110)); | 617 base::TimeDelta::FromMilliseconds(110)); |
621 base::MessageLoop::current()->Run(); | 618 run_loop.Run(); |
622 } | 619 } |
623 | 620 |
624 protected: | 621 protected: |
625 VisitedLinkRenderProcessHostFactory vc_rph_factory_; | 622 VisitedLinkRenderProcessHostFactory vc_rph_factory_; |
626 | 623 |
627 private: | 624 private: |
628 TestVisitedLinkDelegate delegate_; | 625 TestVisitedLinkDelegate delegate_; |
629 scoped_ptr<VisitedLinkMaster> master_; | 626 scoped_ptr<VisitedLinkMaster> master_; |
630 content::TestBrowserThread ui_thread_; | |
631 content::TestBrowserThread file_thread_; | |
632 | |
633 DISALLOW_COPY_AND_ASSIGN(VisitedLinkEventsTest); | |
634 }; | 627 }; |
635 | 628 |
636 TEST_F(VisitedLinkEventsTest, Coalescense) { | 629 TEST_F(VisitedLinkEventsTest, Coalescense) { |
637 // add some URLs to master. | 630 // add some URLs to master. |
638 // Add a few URLs. | 631 // Add a few URLs. |
639 master()->AddURL(GURL("http://acidtests.org/")); | 632 master()->AddURL(GURL("http://acidtests.org/")); |
640 master()->AddURL(GURL("http://google.com/")); | 633 master()->AddURL(GURL("http://google.com/")); |
641 master()->AddURL(GURL("http://chromium.org/")); | 634 master()->AddURL(GURL("http://chromium.org/")); |
642 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting | 635 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting |
643 master()->AddURL(GURL("http://acidtests.org/")); | 636 master()->AddURL(GURL("http://acidtests.org/")); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 760 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
768 content::Source<content::RenderProcessHost>(&different_process_host), | 761 content::Source<content::RenderProcessHost>(&different_process_host), |
769 content::NotificationService::NoDetails()); | 762 content::NotificationService::NoDetails()); |
770 WaitForCoalescense(); | 763 WaitForCoalescense(); |
771 | 764 |
772 EXPECT_EQ(0, different_context.new_table_count()); | 765 EXPECT_EQ(0, different_context.new_table_count()); |
773 | 766 |
774 } | 767 } |
775 | 768 |
776 } // namespace visitedlink | 769 } // namespace visitedlink |
OLD | NEW |