Chromium Code Reviews| 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" |
| 28 #include "content/public/test/test_renderer_host.h" | 26 #include "content/public/test/test_renderer_host.h" |
| 27 #include "content/public/test/test_renderer_host.h" | |
|
Jói
2013/01/24 11:01:55
delete this duplicate line
| |
| 29 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 30 |
| 32 using content::BrowserThread; | 31 using content::BrowserThread; |
| 33 using content::MockRenderProcessHost; | 32 using content::MockRenderProcessHost; |
| 34 using content::RenderViewHostTester; | 33 using content::RenderViewHostTester; |
| 35 | 34 |
| 36 namespace components { | 35 namespace components { |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 TrackingVisitedLinkEventListener* listener = | 487 TrackingVisitedLinkEventListener* listener = |
| 489 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); | 488 static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); |
| 490 | 489 |
| 491 // Verify that VisitedLinkMaster::Listener::Add was called for each added URL. | 490 // Verify that VisitedLinkMaster::Listener::Add was called for each added URL. |
| 492 EXPECT_EQ(g_test_count, listener->add_count()); | 491 EXPECT_EQ(g_test_count, listener->add_count()); |
| 493 // Verify that VisitedLinkMaster::Listener::Reset was called both when one and | 492 // Verify that VisitedLinkMaster::Listener::Reset was called both when one and |
| 494 // all URLs are deleted. | 493 // all URLs are deleted. |
| 495 EXPECT_EQ(2, listener->reset_count()); | 494 EXPECT_EQ(2, listener->reset_count()); |
| 496 } | 495 } |
| 497 | 496 |
| 498 // TODO(boliu): Inherit content::TestBrowserContext when componentized. | 497 class VisitCountingContext : public content::TestBrowserContext { |
| 499 class VisitCountingProfile : public TestingProfile { | |
| 500 public: | 498 public: |
| 501 VisitCountingProfile() | 499 VisitCountingContext() |
| 502 : add_count_(0), | 500 : add_count_(0), |
| 503 add_event_count_(0), | 501 add_event_count_(0), |
| 504 reset_event_count_(0), | 502 reset_event_count_(0), |
| 505 new_table_count_(0) {} | 503 new_table_count_(0) {} |
| 506 | 504 |
| 507 void CountAddEvent(int by) { | 505 void CountAddEvent(int by) { |
| 508 add_count_ += by; | 506 add_count_ += by; |
| 509 add_event_count_++; | 507 add_event_count_++; |
| 510 } | 508 } |
| 511 | 509 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 545 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 543 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 546 content::Source<content::RenderProcessHost>(this), | 544 content::Source<content::RenderProcessHost>(this), |
| 547 content::NotificationService::NoDetails()); | 545 content::NotificationService::NoDetails()); |
| 548 } | 546 } |
| 549 | 547 |
| 550 virtual void WidgetRestored() OVERRIDE { widgets_++; } | 548 virtual void WidgetRestored() OVERRIDE { widgets_++; } |
| 551 virtual void WidgetHidden() OVERRIDE { widgets_--; } | 549 virtual void WidgetHidden() OVERRIDE { widgets_--; } |
| 552 virtual int VisibleWidgetCount() const OVERRIDE { return widgets_; } | 550 virtual int VisibleWidgetCount() const OVERRIDE { return widgets_; } |
| 553 | 551 |
| 554 virtual bool Send(IPC::Message* msg) OVERRIDE { | 552 virtual bool Send(IPC::Message* msg) OVERRIDE { |
| 555 VisitCountingProfile* counting_profile = | 553 VisitCountingContext* counting_profile = |
| 556 static_cast<VisitCountingProfile*>( | 554 static_cast<VisitCountingContext*>( |
| 557 GetBrowserContext()); | 555 GetBrowserContext()); |
| 558 | 556 |
| 559 if (msg->type() == ChromeViewMsg_VisitedLink_Add::ID) { | 557 if (msg->type() == ChromeViewMsg_VisitedLink_Add::ID) { |
| 560 PickleIterator iter(*msg); | 558 PickleIterator iter(*msg); |
| 561 std::vector<uint64> fingerprints; | 559 std::vector<uint64> fingerprints; |
| 562 CHECK(IPC::ReadParam(msg, &iter, &fingerprints)); | 560 CHECK(IPC::ReadParam(msg, &iter, &fingerprints)); |
| 563 counting_profile->CountAddEvent(fingerprints.size()); | 561 counting_profile->CountAddEvent(fingerprints.size()); |
| 564 } else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) { | 562 } else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) { |
| 565 counting_profile->CountResetEvent(); | 563 counting_profile->CountResetEvent(); |
| 566 } else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) { | 564 } else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 585 virtual content::RenderProcessHost* CreateRenderProcessHost( | 583 virtual content::RenderProcessHost* CreateRenderProcessHost( |
| 586 content::BrowserContext* browser_context) const OVERRIDE { | 584 content::BrowserContext* browser_context) const OVERRIDE { |
| 587 return new VisitRelayingRenderProcessHost(browser_context); | 585 return new VisitRelayingRenderProcessHost(browser_context); |
| 588 } | 586 } |
| 589 | 587 |
| 590 private: | 588 private: |
| 591 | 589 |
| 592 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); | 590 DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory); |
| 593 }; | 591 }; |
| 594 | 592 |
| 595 // TODO(boliu): Inherit content::RenderViewHostTestHarness when componentized. | 593 class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { |
| 596 class VisitedLinkEventsTest : public ChromeRenderViewHostTestHarness { | |
| 597 public: | 594 public: |
| 598 VisitedLinkEventsTest() | 595 VisitedLinkEventsTest() |
| 599 : ui_thread_(BrowserThread::UI, &message_loop_), | 596 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 600 file_thread_(BrowserThread::FILE, &message_loop_) {} | 597 file_thread_(BrowserThread::FILE, &message_loop_) {} |
| 601 virtual ~VisitedLinkEventsTest() {} | 598 virtual ~VisitedLinkEventsTest() {} |
| 602 virtual void SetUp() { | 599 virtual void SetUp() { |
| 603 browser_context_.reset(new VisitCountingProfile()); | 600 browser_context_.reset(new VisitCountingContext()); |
| 604 master_.reset(new VisitedLinkMaster(profile(), &delegate_)); | 601 master_.reset(new VisitedLinkMaster(profile(), &delegate_)); |
| 605 master_->Init(); | 602 master_->Init(); |
| 606 SetRenderProcessHostFactory(&vc_rph_factory_); | 603 SetRenderProcessHostFactory(&vc_rph_factory_); |
| 607 content::RenderViewHostTestHarness::SetUp(); | 604 content::RenderViewHostTestHarness::SetUp(); |
| 608 } | 605 } |
| 609 | 606 |
| 610 VisitCountingProfile* profile() const { | 607 VisitCountingContext* profile() const { |
| 611 return static_cast<VisitCountingProfile*>(browser_context_.get()); | 608 return static_cast<VisitCountingContext*>(browser_context_.get()); |
| 612 } | 609 } |
| 613 | 610 |
| 614 VisitedLinkMaster* master() const { | 611 VisitedLinkMaster* master() const { |
| 615 return master_.get(); | 612 return master_.get(); |
| 616 } | 613 } |
| 617 | 614 |
| 618 void WaitForCoalescense() { | 615 void WaitForCoalescense() { |
| 619 // Let the timer fire. | 616 // Let the timer fire. |
| 620 MessageLoop::current()->PostDelayedTask( | 617 MessageLoop::current()->PostDelayedTask( |
| 621 FROM_HERE, | 618 FROM_HERE, |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 756 RenderViewHostTester::For(rvh())->SimulateWasShown(); | 753 RenderViewHostTester::For(rvh())->SimulateWasShown(); |
| 757 | 754 |
| 758 // We should have only one more reset event. | 755 // We should have only one more reset event. |
| 759 EXPECT_EQ(1, profile()->add_event_count()); | 756 EXPECT_EQ(1, profile()->add_event_count()); |
| 760 EXPECT_EQ(1, profile()->reset_event_count()); | 757 EXPECT_EQ(1, profile()->reset_event_count()); |
| 761 } | 758 } |
| 762 | 759 |
| 763 // Tests that VisitedLink ignores renderer process creation notification for a | 760 // Tests that VisitedLink ignores renderer process creation notification for a |
| 764 // different profile. | 761 // different profile. |
| 765 TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) { | 762 TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) { |
| 766 VisitCountingProfile different_context; | 763 VisitCountingContext different_context; |
| 767 VisitRelayingRenderProcessHost different_process_host(&different_context); | 764 VisitRelayingRenderProcessHost different_process_host(&different_context); |
| 768 | 765 |
| 769 content::NotificationService::current()->Notify( | 766 content::NotificationService::current()->Notify( |
| 770 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 767 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 771 content::Source<content::RenderProcessHost>(&different_process_host), | 768 content::Source<content::RenderProcessHost>(&different_process_host), |
| 772 content::NotificationService::NoDetails()); | 769 content::NotificationService::NoDetails()); |
| 773 WaitForCoalescense(); | 770 WaitForCoalescense(); |
| 774 | 771 |
| 775 EXPECT_EQ(0, different_context.new_table_count()); | 772 EXPECT_EQ(0, different_context.new_table_count()); |
| 776 | 773 |
| 777 } | 774 } |
| 778 | 775 |
| 779 } // namespace components | 776 } // namespace components |
| OLD | NEW |