| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <cstdio> | 7 #include <cstdio> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.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 "chrome/browser/visitedlink/visitedlink_master.h" | 15 #include "chrome/browser/visitedlink/visitedlink_master.h" |
| 16 #include "chrome/browser/visitedlink/visitedlink_event_listener.h" | 16 #include "chrome/browser/visitedlink/visitedlink_event_listener.h" |
| 17 #include "chrome/common/render_messages.h" | 17 #include "chrome/common/render_messages.h" |
| 18 #include "chrome/renderer/visitedlink_slave.h" | 18 #include "chrome/renderer/visitedlink_slave.h" |
| 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/browser/browser_thread.h" | 21 #include "content/browser/browser_thread.h" |
| 21 #include "content/browser/renderer_host/browser_render_process_host.h" | 22 #include "content/browser/renderer_host/browser_render_process_host.h" |
| 22 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 23 #include "content/common/notification_service.h" | 23 #include "content/common/notification_service.h" |
| 24 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // a nice long URL that we can append numbers to to get new URLs | 29 // a nice long URL that we can append numbers to to get new URLs |
| 30 const char g_test_prefix[] = | 30 const char g_test_prefix[] = |
| 31 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq="; | 31 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq="; |
| 32 const int g_test_count = 1000; | 32 const int g_test_count = 1000; |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 virtual RenderProcessHost* CreateRenderProcessHost( | 556 virtual RenderProcessHost* CreateRenderProcessHost( |
| 557 content::BrowserContext* browser_context) const OVERRIDE { | 557 content::BrowserContext* browser_context) const OVERRIDE { |
| 558 return new VisitRelayingRenderProcessHost(browser_context); | 558 return new VisitRelayingRenderProcessHost(browser_context); |
| 559 } | 559 } |
| 560 | 560 |
| 561 private: | 561 private: |
| 562 | 562 |
| 563 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); | 563 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); |
| 564 }; | 564 }; |
| 565 | 565 |
| 566 class VisitedLinkEventsTest : public RenderViewHostTestHarness { | 566 class VisitedLinkEventsTest : public ChromeRenderViewHostTestHarness { |
| 567 public: | 567 public: |
| 568 VisitedLinkEventsTest() | 568 VisitedLinkEventsTest() |
| 569 : RenderViewHostTestHarness(), | 569 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 570 ui_thread_(BrowserThread::UI, &message_loop_), | |
| 571 file_thread_(BrowserThread::FILE, &message_loop_) {} | 570 file_thread_(BrowserThread::FILE, &message_loop_) {} |
| 572 ~VisitedLinkEventsTest() { | 571 virtual ~VisitedLinkEventsTest() {} |
| 573 // This ends up using the file thread to schedule the delete. | |
| 574 profile_.reset(); | |
| 575 message_loop_.RunAllPending(); | |
| 576 } | |
| 577 virtual void SetFactoryMode() {} | 572 virtual void SetFactoryMode() {} |
| 578 virtual void SetUp() { | 573 virtual void SetUp() { |
| 579 SetFactoryMode(); | 574 SetFactoryMode(); |
| 580 event_listener_.reset(new VisitedLinkEventListener()); | 575 event_listener_.reset(new VisitedLinkEventListener()); |
| 581 rvh_factory_.set_render_process_host_factory(&vc_rph_factory_); | 576 rvh_factory_.set_render_process_host_factory(&vc_rph_factory_); |
| 582 profile_.reset(new VisitCountingProfile(event_listener_.get())); | 577 browser_context_.reset(new VisitCountingProfile(event_listener_.get())); |
| 583 RenderViewHostTestHarness::SetUp(); | 578 ChromeRenderViewHostTestHarness::SetUp(); |
| 584 } | 579 } |
| 585 | 580 |
| 586 VisitCountingProfile* profile() const { | 581 VisitCountingProfile* profile() const { |
| 587 return static_cast<VisitCountingProfile*>(profile_.get()); | 582 return static_cast<VisitCountingProfile*>(browser_context_.get()); |
| 588 } | 583 } |
| 589 | 584 |
| 590 void WaitForCoalescense() { | 585 void WaitForCoalescense() { |
| 591 // Let the timer fire. | 586 // Let the timer fire. |
| 592 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 587 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 593 new MessageLoop::QuitTask(), 110); | 588 new MessageLoop::QuitTask(), 110); |
| 594 MessageLoop::current()->Run(); | 589 MessageLoop::current()->Run(); |
| 595 } | 590 } |
| 596 | 591 |
| 597 protected: | 592 protected: |
| 598 VisitedLinkRenderProcessHostFactory vc_rph_factory_; | 593 VisitedLinkRenderProcessHostFactory vc_rph_factory_; |
| 599 | 594 |
| 600 private: | 595 private: |
| 601 scoped_ptr<VisitedLinkEventListener> event_listener_; | 596 scoped_ptr<VisitedLinkEventListener> event_listener_; |
| 602 BrowserThread ui_thread_; | 597 BrowserThread ui_thread_; |
| 603 BrowserThread file_thread_; | 598 BrowserThread file_thread_; |
| 604 | 599 |
| 605 DISALLOW_COPY_AND_ASSIGN(VisitedLinkEventsTest); | 600 DISALLOW_COPY_AND_ASSIGN(VisitedLinkEventsTest); |
| 606 }; | 601 }; |
| 607 | 602 |
| 608 TEST_F(VisitedLinkEventsTest, Coalescense) { | 603 TEST_F(VisitedLinkEventsTest, Coalescense) { |
| 609 // add some URLs to master. | 604 // add some URLs to master. |
| 610 VisitedLinkMaster* master = profile_->GetVisitedLinkMaster(); | 605 VisitedLinkMaster* master = profile()->GetVisitedLinkMaster(); |
| 611 // Add a few URLs. | 606 // Add a few URLs. |
| 612 master->AddURL(GURL("http://acidtests.org/")); | 607 master->AddURL(GURL("http://acidtests.org/")); |
| 613 master->AddURL(GURL("http://google.com/")); | 608 master->AddURL(GURL("http://google.com/")); |
| 614 master->AddURL(GURL("http://chromium.org/")); | 609 master->AddURL(GURL("http://chromium.org/")); |
| 615 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting | 610 // Just for kicks, add a duplicate URL. This shouldn't increase the resulting |
| 616 master->AddURL(GURL("http://acidtests.org/")); | 611 master->AddURL(GURL("http://acidtests.org/")); |
| 617 | 612 |
| 618 // Make sure that coalescing actually occurs. There should be no links or | 613 // Make sure that coalescing actually occurs. There should be no links or |
| 619 // events received by the renderer. | 614 // events received by the renderer. |
| 620 EXPECT_EQ(0, profile()->add_count()); | 615 EXPECT_EQ(0, profile()->add_count()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 | 647 |
| 653 WaitForCoalescense(); | 648 WaitForCoalescense(); |
| 654 | 649 |
| 655 // We should have no change in results except for one new reset event. | 650 // We should have no change in results except for one new reset event. |
| 656 EXPECT_EQ(6, profile()->add_count()); | 651 EXPECT_EQ(6, profile()->add_count()); |
| 657 EXPECT_EQ(2, profile()->add_event_count()); | 652 EXPECT_EQ(2, profile()->add_event_count()); |
| 658 EXPECT_EQ(1, profile()->reset_event_count()); | 653 EXPECT_EQ(1, profile()->reset_event_count()); |
| 659 } | 654 } |
| 660 | 655 |
| 661 TEST_F(VisitedLinkEventsTest, Basics) { | 656 TEST_F(VisitedLinkEventsTest, Basics) { |
| 662 VisitedLinkMaster* master = profile_->GetVisitedLinkMaster(); | 657 VisitedLinkMaster* master = profile()->GetVisitedLinkMaster(); |
| 663 rvh()->CreateRenderView(string16()); | 658 rvh()->CreateRenderView(string16()); |
| 664 | 659 |
| 665 // Add a few URLs. | 660 // Add a few URLs. |
| 666 master->AddURL(GURL("http://acidtests.org/")); | 661 master->AddURL(GURL("http://acidtests.org/")); |
| 667 master->AddURL(GURL("http://google.com/")); | 662 master->AddURL(GURL("http://google.com/")); |
| 668 master->AddURL(GURL("http://chromium.org/")); | 663 master->AddURL(GURL("http://chromium.org/")); |
| 669 | 664 |
| 670 WaitForCoalescense(); | 665 WaitForCoalescense(); |
| 671 | 666 |
| 672 // We now should have 1 add event. | 667 // We now should have 1 add event. |
| 673 EXPECT_EQ(1, profile()->add_event_count()); | 668 EXPECT_EQ(1, profile()->add_event_count()); |
| 674 EXPECT_EQ(0, profile()->reset_event_count()); | 669 EXPECT_EQ(0, profile()->reset_event_count()); |
| 675 | 670 |
| 676 master->DeleteAllURLs(); | 671 master->DeleteAllURLs(); |
| 677 | 672 |
| 678 WaitForCoalescense(); | 673 WaitForCoalescense(); |
| 679 | 674 |
| 680 // We should have no change in add results, plus one new reset event. | 675 // We should have no change in add results, plus one new reset event. |
| 681 EXPECT_EQ(1, profile()->add_event_count()); | 676 EXPECT_EQ(1, profile()->add_event_count()); |
| 682 EXPECT_EQ(1, profile()->reset_event_count()); | 677 EXPECT_EQ(1, profile()->reset_event_count()); |
| 683 } | 678 } |
| 684 | 679 |
| 685 TEST_F(VisitedLinkEventsTest, TabVisibility) { | 680 TEST_F(VisitedLinkEventsTest, TabVisibility) { |
| 686 VisitedLinkMaster* master = profile_->GetVisitedLinkMaster(); | 681 VisitedLinkMaster* master = profile()->GetVisitedLinkMaster(); |
| 687 rvh()->CreateRenderView(string16()); | 682 rvh()->CreateRenderView(string16()); |
| 688 | 683 |
| 689 // Simulate tab becoming inactive. | 684 // Simulate tab becoming inactive. |
| 690 rvh()->WasHidden(); | 685 rvh()->WasHidden(); |
| 691 | 686 |
| 692 // Add a few URLs. | 687 // Add a few URLs. |
| 693 master->AddURL(GURL("http://acidtests.org/")); | 688 master->AddURL(GURL("http://acidtests.org/")); |
| 694 master->AddURL(GURL("http://google.com/")); | 689 master->AddURL(GURL("http://google.com/")); |
| 695 master->AddURL(GURL("http://chromium.org/")); | 690 master->AddURL(GURL("http://chromium.org/")); |
| 696 | 691 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 720 EXPECT_EQ(1, profile()->add_event_count()); | 715 EXPECT_EQ(1, profile()->add_event_count()); |
| 721 EXPECT_EQ(0, profile()->reset_event_count()); | 716 EXPECT_EQ(0, profile()->reset_event_count()); |
| 722 | 717 |
| 723 // Activate the tab. | 718 // Activate the tab. |
| 724 rvh()->WasRestored(); | 719 rvh()->WasRestored(); |
| 725 | 720 |
| 726 // We should have only one more reset event. | 721 // We should have only one more reset event. |
| 727 EXPECT_EQ(1, profile()->add_event_count()); | 722 EXPECT_EQ(1, profile()->add_event_count()); |
| 728 EXPECT_EQ(1, profile()->reset_event_count()); | 723 EXPECT_EQ(1, profile()->reset_event_count()); |
| 729 } | 724 } |
| OLD | NEW |