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

Side by Side Diff: components/visitedlink/test/visitedlink_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698