| 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/test/base/chrome_render_view_host_test_harness.h" | |
| 17 #include "chrome/test/base/testing_profile.h" | |
| 18 #include "components/visitedlink/browser/visitedlink_delegate.h" | 16 #include "components/visitedlink/browser/visitedlink_delegate.h" |
| 19 #include "components/visitedlink/browser/visitedlink_event_listener.h" | 17 #include "components/visitedlink/browser/visitedlink_event_listener.h" |
| 20 #include "components/visitedlink/browser/visitedlink_master.h" | 18 #include "components/visitedlink/browser/visitedlink_master.h" |
| 21 #include "components/visitedlink/common/visitedlink_messages.h" | 19 #include "components/visitedlink/common/visitedlink_messages.h" |
| 22 #include "components/visitedlink/renderer/visitedlink_slave.h" | 20 #include "components/visitedlink/renderer/visitedlink_slave.h" |
| 23 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
| 25 #include "content/public/test/mock_render_process_host.h" | 23 #include "content/public/test/mock_render_process_host.h" |
| 26 #include "content/public/test/test_browser_context.h" | 24 #include "content/public/test/test_browser_context.h" |
| 27 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 TrackingVisitedLinkEventListener* listener = | 487 TrackingVisitedLinkEventListener* listener = |
| 490 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); | 488 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); |
| 491 | 489 |
| 492 // Verify that VisitedLinkMaster::Listener::Add was called for each added URL. | 490 // Verify that VisitedLinkMaster::Listener::Add was called for each added URL. |
| 493 EXPECT_EQ(g_test_count, listener->add_count()); | 491 EXPECT_EQ(g_test_count, listener->add_count()); |
| 494 // Verify that VisitedLinkMaster::Listener::Reset was called both when one and | 492 // Verify that VisitedLinkMaster::Listener::Reset was called both when one and |
| 495 // all URLs are deleted. | 493 // all URLs are deleted. |
| 496 EXPECT_EQ(2, listener->reset_count()); | 494 EXPECT_EQ(2, listener->reset_count()); |
| 497 } | 495 } |
| 498 | 496 |
| 499 // TODO(boliu): Inherit content::TestBrowserContext when componentized. | 497 class VisitCountingContext : public content::TestBrowserContext { |
| 500 class VisitCountingProfile : public TestingProfile { | |
| 501 public: | 498 public: |
| 502 VisitCountingProfile() | 499 VisitCountingContext() |
| 503 : add_count_(0), | 500 : add_count_(0), |
| 504 add_event_count_(0), | 501 add_event_count_(0), |
| 505 reset_event_count_(0), | 502 reset_event_count_(0), |
| 506 new_table_count_(0) {} | 503 new_table_count_(0) {} |
| 507 | 504 |
| 508 void CountAddEvent(int by) { | 505 void CountAddEvent(int by) { |
| 509 add_count_ += by; | 506 add_count_ += by; |
| 510 add_event_count_++; | 507 add_event_count_++; |
| 511 } | 508 } |
| 512 | 509 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 543 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 547 content::Source<content::RenderProcessHost>(this), | 544 content::Source<content::RenderProcessHost>(this), |
| 548 content::NotificationService::NoDetails()); | 545 content::NotificationService::NoDetails()); |
| 549 } | 546 } |
| 550 | 547 |
| 551 virtual void WidgetRestored() OVERRIDE { widgets_++; } | 548 virtual void WidgetRestored() OVERRIDE { widgets_++; } |
| 552 virtual void WidgetHidden() OVERRIDE { widgets_--; } | 549 virtual void WidgetHidden() OVERRIDE { widgets_--; } |
| 553 virtual int VisibleWidgetCount() const OVERRIDE { return widgets_; } | 550 virtual int VisibleWidgetCount() const OVERRIDE { return widgets_; } |
| 554 | 551 |
| 555 virtual bool Send(IPC::Message* msg) OVERRIDE { | 552 virtual bool Send(IPC::Message* msg) OVERRIDE { |
| 556 VisitCountingProfile* counting_profile = | 553 VisitCountingContext* counting_context = |
| 557 static_cast<VisitCountingProfile*>( | 554 static_cast<VisitCountingContext*>( |
| 558 GetBrowserContext()); | 555 GetBrowserContext()); |
| 559 | 556 |
| 560 if (msg->type() == ChromeViewMsg_VisitedLink_Add::ID) { | 557 if (msg->type() == ChromeViewMsg_VisitedLink_Add::ID) { |
| 561 PickleIterator iter(*msg); | 558 PickleIterator iter(*msg); |
| 562 std::vector<uint64> fingerprints; | 559 std::vector<uint64> fingerprints; |
| 563 CHECK(IPC::ReadParam(msg, &iter, &fingerprints)); | 560 CHECK(IPC::ReadParam(msg, &iter, &fingerprints)); |
| 564 counting_profile->CountAddEvent(fingerprints.size()); | 561 counting_context->CountAddEvent(fingerprints.size()); |
| 565 } else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) { | 562 } else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) { |
| 566 counting_profile->CountResetEvent(); | 563 counting_context->CountResetEvent(); |
| 567 } else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) { | 564 } else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) { |
| 568 counting_profile->CountNewTable(); | 565 counting_context->CountNewTable(); |
| 569 } | 566 } |
| 570 | 567 |
| 571 delete msg; | 568 delete msg; |
| 572 return true; | 569 return true; |
| 573 } | 570 } |
| 574 | 571 |
| 575 private: | 572 private: |
| 576 int widgets_; | 573 int widgets_; |
| 577 | 574 |
| 578 DISALLOW_COPY_AND_ASSIGN(VisitRelayingRenderProcessHost); | 575 DISALLOW_COPY_AND_ASSIGN(VisitRelayingRenderProcessHost); |
| 579 }; | 576 }; |
| 580 | 577 |
| 581 class VisitedLinkRenderProcessHostFactory | 578 class VisitedLinkRenderProcessHostFactory |
| 582 : public content::RenderProcessHostFactory { | 579 : public content::RenderProcessHostFactory { |
| 583 public: | 580 public: |
| 584 VisitedLinkRenderProcessHostFactory() | 581 VisitedLinkRenderProcessHostFactory() |
| 585 : content::RenderProcessHostFactory() {} | 582 : content::RenderProcessHostFactory() {} |
| 586 virtual content::RenderProcessHost* CreateRenderProcessHost( | 583 virtual content::RenderProcessHost* CreateRenderProcessHost( |
| 587 content::BrowserContext* browser_context) const OVERRIDE { | 584 content::BrowserContext* browser_context) const OVERRIDE { |
| 588 return new VisitRelayingRenderProcessHost(browser_context); | 585 return new VisitRelayingRenderProcessHost(browser_context); |
| 589 } | 586 } |
| 590 | 587 |
| 591 private: | 588 private: |
| 592 | 589 |
| 593 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); | 590 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); |
| 594 }; | 591 }; |
| 595 | 592 |
| 596 // TODO(boliu): Inherit content::RenderViewHostTestHarness when componentized. | 593 class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { |
| 597 class VisitedLinkEventsTest : public ChromeRenderViewHostTestHarness { | |
| 598 public: | 594 public: |
| 599 VisitedLinkEventsTest() | 595 VisitedLinkEventsTest() |
| 600 : ui_thread_(BrowserThread::UI, &message_loop_), | 596 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 601 file_thread_(BrowserThread::FILE, &message_loop_) {} | 597 file_thread_(BrowserThread::FILE, &message_loop_) {} |
| 602 virtual ~VisitedLinkEventsTest() {} | 598 virtual ~VisitedLinkEventsTest() {} |
| 603 virtual void SetUp() { | 599 virtual void SetUp() { |
| 604 browser_context_.reset(new VisitCountingProfile()); | 600 browser_context_.reset(new VisitCountingContext()); |
| 605 master_.reset(new VisitedLinkMaster(profile(), &delegate_, true)); | 601 master_.reset(new VisitedLinkMaster(context(), &delegate_, true)); |
| 606 master_->Init(); | 602 master_->Init(); |
| 607 SetRenderProcessHostFactory(&vc_rph_factory_); | 603 SetRenderProcessHostFactory(&vc_rph_factory_); |
| 608 content::RenderViewHostTestHarness::SetUp(); | 604 content::RenderViewHostTestHarness::SetUp(); |
| 609 } | 605 } |
| 610 | 606 |
| 611 VisitCountingProfile* profile() const { | 607 VisitCountingContext* context() const { |
| 612 return static_cast<VisitCountingProfile*>(browser_context_.get()); | 608 return static_cast<VisitCountingContext*>(browser_context_.get()); |
| 613 } | 609 } |
| 614 | 610 |
| 615 VisitedLinkMaster* master() const { | 611 VisitedLinkMaster* master() const { |
| 616 return master_.get(); | 612 return master_.get(); |
| 617 } | 613 } |
| 618 | 614 |
| 619 void WaitForCoalescense() { | 615 void WaitForCoalescense() { |
| 620 // Let the timer fire. | 616 // Let the timer fire. |
| 621 MessageLoop::current()->PostDelayedTask( | 617 MessageLoop::current()->PostDelayedTask( |
| 622 FROM_HERE, | 618 FROM_HERE, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 641 // add some URLs to master. | 637 // add some URLs to master. |
| 642 // Add a few URLs. | 638 // Add a few URLs. |
| 643 master()->AddURL(GURL("http://acidtests.org/")); | 639 master()->AddURL(GURL("http://acidtests.org/")); |
| 644 master()->AddURL(GURL("http://google.com/")); | 640 master()->AddURL(GURL("http://google.com/")); |
| 645 master()->AddURL(GURL("http://chromium.org/")); | 641 master()->AddURL(GURL("http://chromium.org/")); |
| 646 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting | 642 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting |
| 647 master()->AddURL(GURL("http://acidtests.org/")); | 643 master()->AddURL(GURL("http://acidtests.org/")); |
| 648 | 644 |
| 649 // Make sure that coalescing actually occurs. There should be no links or | 645 // Make sure that coalescing actually occurs. There should be no links or |
| 650 // events received by the renderer. | 646 // events received by the renderer. |
| 651 EXPECT_EQ(0, profile()->add_count()); | 647 EXPECT_EQ(0, context()->add_count()); |
| 652 EXPECT_EQ(0, profile()->add_event_count()); | 648 EXPECT_EQ(0, context()->add_event_count()); |
| 653 | 649 |
| 654 WaitForCoalescense(); | 650 WaitForCoalescense(); |
| 655 | 651 |
| 656 // We now should have 3 entries added in 1 event. | 652 // We now should have 3 entries added in 1 event. |
| 657 EXPECT_EQ(3, profile()->add_count()); | 653 EXPECT_EQ(3, context()->add_count()); |
| 658 EXPECT_EQ(1, profile()->add_event_count()); | 654 EXPECT_EQ(1, context()->add_event_count()); |
| 659 | 655 |
| 660 // Test whether the coalescing continues by adding a few more URLs. | 656 // Test whether the coalescing continues by adding a few more URLs. |
| 661 master()->AddURL(GURL("http://google.com/chrome/")); | 657 master()->AddURL(GURL("http://google.com/chrome/")); |
| 662 master()->AddURL(GURL("http://webkit.org/")); | 658 master()->AddURL(GURL("http://webkit.org/")); |
| 663 master()->AddURL(GURL("http://acid3.acidtests.org/")); | 659 master()->AddURL(GURL("http://acid3.acidtests.org/")); |
| 664 | 660 |
| 665 WaitForCoalescense(); | 661 WaitForCoalescense(); |
| 666 | 662 |
| 667 // We should have 6 entries added in 2 events. | 663 // We should have 6 entries added in 2 events. |
| 668 EXPECT_EQ(6, profile()->add_count()); | 664 EXPECT_EQ(6, context()->add_count()); |
| 669 EXPECT_EQ(2, profile()->add_event_count()); | 665 EXPECT_EQ(2, context()->add_event_count()); |
| 670 | 666 |
| 671 // Test whether duplicate entries produce add events. | 667 // Test whether duplicate entries produce add events. |
| 672 master()->AddURL(GURL("http://acidtests.org/")); | 668 master()->AddURL(GURL("http://acidtests.org/")); |
| 673 | 669 |
| 674 WaitForCoalescense(); | 670 WaitForCoalescense(); |
| 675 | 671 |
| 676 // We should have no change in results. | 672 // We should have no change in results. |
| 677 EXPECT_EQ(6, profile()->add_count()); | 673 EXPECT_EQ(6, context()->add_count()); |
| 678 EXPECT_EQ(2, profile()->add_event_count()); | 674 EXPECT_EQ(2, context()->add_event_count()); |
| 679 | 675 |
| 680 // Ensure that the coalescing does not resume after resetting. | 676 // Ensure that the coalescing does not resume after resetting. |
| 681 master()->AddURL(GURL("http://build.chromium.org/")); | 677 master()->AddURL(GURL("http://build.chromium.org/")); |
| 682 master()->DeleteAllURLs(); | 678 master()->DeleteAllURLs(); |
| 683 | 679 |
| 684 WaitForCoalescense(); | 680 WaitForCoalescense(); |
| 685 | 681 |
| 686 // We should have no change in results except for one new reset event. | 682 // We should have no change in results except for one new reset event. |
| 687 EXPECT_EQ(6, profile()->add_count()); | 683 EXPECT_EQ(6, context()->add_count()); |
| 688 EXPECT_EQ(2, profile()->add_event_count()); | 684 EXPECT_EQ(2, context()->add_event_count()); |
| 689 EXPECT_EQ(1, profile()->reset_event_count()); | 685 EXPECT_EQ(1, context()->reset_event_count()); |
| 690 } | 686 } |
| 691 | 687 |
| 692 TEST_F(VisitedLinkEventsTest, Basics) { | 688 TEST_F(VisitedLinkEventsTest, Basics) { |
| 693 RenderViewHostTester::For(rvh())->CreateRenderView(string16(), | 689 RenderViewHostTester::For(rvh())->CreateRenderView(string16(), |
| 694 MSG_ROUTING_NONE, | 690 MSG_ROUTING_NONE, |
| 695 -1); | 691 -1); |
| 696 | 692 |
| 697 // Add a few URLs. | 693 // Add a few URLs. |
| 698 master()->AddURL(GURL("http://acidtests.org/")); | 694 master()->AddURL(GURL("http://acidtests.org/")); |
| 699 master()->AddURL(GURL("http://google.com/")); | 695 master()->AddURL(GURL("http://google.com/")); |
| 700 master()->AddURL(GURL("http://chromium.org/")); | 696 master()->AddURL(GURL("http://chromium.org/")); |
| 701 | 697 |
| 702 WaitForCoalescense(); | 698 WaitForCoalescense(); |
| 703 | 699 |
| 704 // We now should have 1 add event. | 700 // We now should have 1 add event. |
| 705 EXPECT_EQ(1, profile()->add_event_count()); | 701 EXPECT_EQ(1, context()->add_event_count()); |
| 706 EXPECT_EQ(0, profile()->reset_event_count()); | 702 EXPECT_EQ(0, context()->reset_event_count()); |
| 707 | 703 |
| 708 master()->DeleteAllURLs(); | 704 master()->DeleteAllURLs(); |
| 709 | 705 |
| 710 WaitForCoalescense(); | 706 WaitForCoalescense(); |
| 711 | 707 |
| 712 // We should have no change in add results, plus one new reset event. | 708 // We should have no change in add results, plus one new reset event. |
| 713 EXPECT_EQ(1, profile()->add_event_count()); | 709 EXPECT_EQ(1, context()->add_event_count()); |
| 714 EXPECT_EQ(1, profile()->reset_event_count()); | 710 EXPECT_EQ(1, context()->reset_event_count()); |
| 715 } | 711 } |
| 716 | 712 |
| 717 TEST_F(VisitedLinkEventsTest, TabVisibility) { | 713 TEST_F(VisitedLinkEventsTest, TabVisibility) { |
| 718 RenderViewHostTester::For(rvh())->CreateRenderView(string16(), | 714 RenderViewHostTester::For(rvh())->CreateRenderView(string16(), |
| 719 MSG_ROUTING_NONE, | 715 MSG_ROUTING_NONE, |
| 720 -1); | 716 -1); |
| 721 | 717 |
| 722 // Simulate tab becoming inactive. | 718 // Simulate tab becoming inactive. |
| 723 RenderViewHostTester::For(rvh())->SimulateWasHidden(); | 719 RenderViewHostTester::For(rvh())->SimulateWasHidden(); |
| 724 | 720 |
| 725 // Add a few URLs. | 721 // Add a few URLs. |
| 726 master()->AddURL(GURL("http://acidtests.org/")); | 722 master()->AddURL(GURL("http://acidtests.org/")); |
| 727 master()->AddURL(GURL("http://google.com/")); | 723 master()->AddURL(GURL("http://google.com/")); |
| 728 master()->AddURL(GURL("http://chromium.org/")); | 724 master()->AddURL(GURL("http://chromium.org/")); |
| 729 | 725 |
| 730 WaitForCoalescense(); | 726 WaitForCoalescense(); |
| 731 | 727 |
| 732 // We shouldn't have any events. | 728 // We shouldn't have any events. |
| 733 EXPECT_EQ(0, profile()->add_event_count()); | 729 EXPECT_EQ(0, context()->add_event_count()); |
| 734 EXPECT_EQ(0, profile()->reset_event_count()); | 730 EXPECT_EQ(0, context()->reset_event_count()); |
| 735 | 731 |
| 736 // Simulate the tab becoming active. | 732 // Simulate the tab becoming active. |
| 737 RenderViewHostTester::For(rvh())->SimulateWasShown(); | 733 RenderViewHostTester::For(rvh())->SimulateWasShown(); |
| 738 | 734 |
| 739 // We should now have 3 add events, still no reset events. | 735 // We should now have 3 add events, still no reset events. |
| 740 EXPECT_EQ(1, profile()->add_event_count()); | 736 EXPECT_EQ(1, context()->add_event_count()); |
| 741 EXPECT_EQ(0, profile()->reset_event_count()); | 737 EXPECT_EQ(0, context()->reset_event_count()); |
| 742 | 738 |
| 743 // Deactivate the tab again. | 739 // Deactivate the tab again. |
| 744 RenderViewHostTester::For(rvh())->SimulateWasHidden(); | 740 RenderViewHostTester::For(rvh())->SimulateWasHidden(); |
| 745 | 741 |
| 746 // Add a bunch of URLs (over 50) to exhaust the link event buffer. | 742 // Add a bunch of URLs (over 50) to exhaust the link event buffer. |
| 747 for (int i = 0; i < 100; i++) | 743 for (int i = 0; i < 100; i++) |
| 748 master()->AddURL(TestURL(i)); | 744 master()->AddURL(TestURL(i)); |
| 749 | 745 |
| 750 WaitForCoalescense(); | 746 WaitForCoalescense(); |
| 751 | 747 |
| 752 // Again, no change in events until tab is active. | 748 // Again, no change in events until tab is active. |
| 753 EXPECT_EQ(1, profile()->add_event_count()); | 749 EXPECT_EQ(1, context()->add_event_count()); |
| 754 EXPECT_EQ(0, profile()->reset_event_count()); | 750 EXPECT_EQ(0, context()->reset_event_count()); |
| 755 | 751 |
| 756 // Activate the tab. | 752 // Activate the tab. |
| 757 RenderViewHostTester::For(rvh())->SimulateWasShown(); | 753 RenderViewHostTester::For(rvh())->SimulateWasShown(); |
| 758 | 754 |
| 759 // We should have only one more reset event. | 755 // We should have only one more reset event. |
| 760 EXPECT_EQ(1, profile()->add_event_count()); | 756 EXPECT_EQ(1, context()->add_event_count()); |
| 761 EXPECT_EQ(1, profile()->reset_event_count()); | 757 EXPECT_EQ(1, context()->reset_event_count()); |
| 762 } | 758 } |
| 763 | 759 |
| 764 // Tests that VisitedLink ignores renderer process creation notification for a | 760 // Tests that VisitedLink ignores renderer process creation notification for a |
| 765 // different profile. | 761 // different context. |
| 766 TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) { | 762 TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) { |
| 767 VisitCountingProfile different_context; | 763 VisitCountingContext different_context; |
| 768 VisitRelayingRenderProcessHost different_process_host(&different_context); | 764 VisitRelayingRenderProcessHost different_process_host(&different_context); |
| 769 | 765 |
| 770 content::NotificationService::current()->Notify( | 766 content::NotificationService::current()->Notify( |
| 771 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 767 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 772 content::Source<content::RenderProcessHost>(&different_process_host), | 768 content::Source<content::RenderProcessHost>(&different_process_host), |
| 773 content::NotificationService::NoDetails()); | 769 content::NotificationService::NoDetails()); |
| 774 WaitForCoalescense(); | 770 WaitForCoalescense(); |
| 775 | 771 |
| 776 EXPECT_EQ(0, different_context.new_table_count()); | 772 EXPECT_EQ(0, different_context.new_table_count()); |
| 777 | 773 |
| 778 } | 774 } |
| 779 | 775 |
| 780 } // namespace components | 776 } // namespace components |
| OLD | NEW |