| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/search/instant_page.h" | 5 #include "chrome/browser/ui/search/instant_tab.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "chrome/browser/ui/search/search_tab_helper.h" | 12 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/render_messages.h" |
| 15 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "content/public/browser/navigation_controller.h" | 17 #include "content/public/browser/navigation_controller.h" |
| 18 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
| 19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/mock_render_process_host.h" | 20 #include "content/public/test/mock_render_process_host.h" |
| 21 #include "ipc/ipc_test_sink.h" | 21 #include "ipc/ipc_test_sink.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 25 | 25 |
| 26 class Profile; | 26 class Profile; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 class FakePageDelegate : public InstantPage::Delegate { | 30 class FakePageDelegate : public InstantTab::Delegate { |
| 31 public: | 31 public: |
| 32 virtual ~FakePageDelegate() { | 32 virtual ~FakePageDelegate() { |
| 33 } | 33 } |
| 34 | 34 |
| 35 MOCK_METHOD2(InstantSupportDetermined, | 35 MOCK_METHOD2(InstantSupportDetermined, |
| 36 void(const content::WebContents* contents, | 36 void(const content::WebContents* contents, |
| 37 bool supports_instant)); | 37 bool supports_instant)); |
| 38 MOCK_METHOD1(InstantPageRenderProcessGone, | 38 MOCK_METHOD1(InstantTabRenderProcessGone, |
| 39 void(const content::WebContents* contents)); | 39 void(const content::WebContents* contents)); |
| 40 MOCK_METHOD2(InstantPageAboutToNavigateMainFrame, | 40 MOCK_METHOD2(InstantTabAboutToNavigateMainFrame, |
| 41 void(const content::WebContents* contents, | 41 void(const content::WebContents* contents, |
| 42 const GURL& url)); | 42 const GURL& url)); |
| 43 MOCK_METHOD5(NavigateToURL, | 43 MOCK_METHOD5(NavigateToURL, |
| 44 void(const content::WebContents* contents, | 44 void(const content::WebContents* contents, |
| 45 const GURL& url, | 45 const GURL& url, |
| 46 ui::PageTransition transition, | 46 ui::PageTransition transition, |
| 47 WindowOpenDisposition disposition, | 47 WindowOpenDisposition disposition, |
| 48 bool is_search_type)); | 48 bool is_search_type)); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 class InstantPageTest : public ChromeRenderViewHostTestHarness { | 53 class InstantTabTest : public ChromeRenderViewHostTestHarness { |
| 54 public: | 54 public: |
| 55 void SetUp() override; | 55 void SetUp() override; |
| 56 | 56 |
| 57 bool MessageWasSent(uint32_t id) { | 57 bool MessageWasSent(uint32_t id) { |
| 58 return process()->sink().GetFirstMessageMatching(id) != NULL; | 58 return process()->sink().GetFirstMessageMatching(id) != NULL; |
| 59 } | 59 } |
| 60 | 60 |
| 61 std::unique_ptr<InstantPage> page; | 61 std::unique_ptr<InstantTab> page; |
| 62 FakePageDelegate delegate; | 62 FakePageDelegate delegate; |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 void InstantPageTest::SetUp() { | 65 void InstantTabTest::SetUp() { |
| 66 ChromeRenderViewHostTestHarness::SetUp(); | 66 ChromeRenderViewHostTestHarness::SetUp(); |
| 67 SearchTabHelper::CreateForWebContents(web_contents()); | 67 SearchTabHelper::CreateForWebContents(web_contents()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 TEST_F(InstantPageTest, IsLocal) { | 70 TEST_F(InstantTabTest, IsLocal) { |
| 71 page.reset(new InstantPage(&delegate)); | 71 page.reset(new InstantTab(&delegate)); |
| 72 EXPECT_FALSE(page->supports_instant()); | 72 EXPECT_FALSE(page->supports_instant()); |
| 73 EXPECT_FALSE(page->IsLocal()); | 73 EXPECT_FALSE(page->IsLocal()); |
| 74 page->SetContents(web_contents()); | 74 page->SetContents(web_contents()); |
| 75 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 75 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 76 EXPECT_TRUE(page->IsLocal()); | 76 EXPECT_TRUE(page->IsLocal()); |
| 77 NavigateAndCommit(GURL("http://example.com")); | 77 NavigateAndCommit(GURL("http://example.com")); |
| 78 EXPECT_FALSE(page->IsLocal()); | 78 EXPECT_FALSE(page->IsLocal()); |
| 79 } | 79 } |
| 80 | 80 |
| 81 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { | 81 TEST_F(InstantTabTest, DetermineIfPageSupportsInstant_Local) { |
| 82 page.reset(new InstantPage(&delegate)); | 82 page.reset(new InstantTab(&delegate)); |
| 83 EXPECT_FALSE(page->supports_instant()); | 83 EXPECT_FALSE(page->supports_instant()); |
| 84 page->SetContents(web_contents()); | 84 page->SetContents(web_contents()); |
| 85 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 85 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 86 EXPECT_TRUE(page->IsLocal()); | 86 EXPECT_TRUE(page->IsLocal()); |
| 87 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 87 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 88 .Times(1); | 88 .Times(1); |
| 89 SearchTabHelper::FromWebContents(web_contents())-> | 89 SearchTabHelper::FromWebContents(web_contents())-> |
| 90 DetermineIfPageSupportsInstant(); | 90 DetermineIfPageSupportsInstant(); |
| 91 EXPECT_TRUE(page->supports_instant()); | 91 EXPECT_TRUE(page->supports_instant()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { | 94 TEST_F(InstantTabTest, DetermineIfPageSupportsInstant_NonLocal) { |
| 95 page.reset(new InstantPage(&delegate)); | 95 page.reset(new InstantTab(&delegate)); |
| 96 EXPECT_FALSE(page->supports_instant()); | 96 EXPECT_FALSE(page->supports_instant()); |
| 97 page->SetContents(web_contents()); | 97 page->SetContents(web_contents()); |
| 98 NavigateAndCommit(GURL("chrome-search://foo/bar")); | 98 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 99 EXPECT_FALSE(page->IsLocal()); | 99 EXPECT_FALSE(page->IsLocal()); |
| 100 process()->sink().ClearMessages(); | 100 process()->sink().ClearMessages(); |
| 101 SearchTabHelper::FromWebContents(web_contents())-> | 101 SearchTabHelper::FromWebContents(web_contents())-> |
| 102 DetermineIfPageSupportsInstant(); | 102 DetermineIfPageSupportsInstant(); |
| 103 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 103 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 104 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 104 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 105 ASSERT_TRUE(message != NULL); | 105 ASSERT_TRUE(message != NULL); |
| 106 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 106 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 TEST_F(InstantPageTest, PageURLDoesntBelongToInstantRenderer) { | 109 TEST_F(InstantTabTest, PageURLDoesntBelongToInstantRenderer) { |
| 110 page.reset(new InstantPage(&delegate)); | 110 page.reset(new InstantTab(&delegate)); |
| 111 EXPECT_FALSE(page->supports_instant()); | 111 EXPECT_FALSE(page->supports_instant()); |
| 112 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 112 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 113 page->SetContents(web_contents()); | 113 page->SetContents(web_contents()); |
| 114 | 114 |
| 115 // Navigate to a page URL that doesn't belong to Instant renderer. | 115 // Navigate to a page URL that doesn't belong to Instant renderer. |
| 116 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return | 116 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return |
| 117 // immediately without dispatching any message to the renderer. | 117 // immediately without dispatching any message to the renderer. |
| 118 NavigateAndCommit(GURL("http://www.example.com")); | 118 NavigateAndCommit(GURL("http://www.example.com")); |
| 119 EXPECT_FALSE(page->IsLocal()); | 119 EXPECT_FALSE(page->IsLocal()); |
| 120 process()->sink().ClearMessages(); | 120 process()->sink().ClearMessages(); |
| 121 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false)) | 121 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false)) |
| 122 .Times(1); | 122 .Times(1); |
| 123 | 123 |
| 124 SearchTabHelper::FromWebContents(web_contents())-> | 124 SearchTabHelper::FromWebContents(web_contents())-> |
| 125 DetermineIfPageSupportsInstant(); | 125 DetermineIfPageSupportsInstant(); |
| 126 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 126 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 127 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 127 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 128 ASSERT_TRUE(message == NULL); | 128 ASSERT_TRUE(message == NULL); |
| 129 EXPECT_FALSE(page->supports_instant()); | 129 EXPECT_FALSE(page->supports_instant()); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message | 132 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message |
| 133 // reply handler updates the instant support state in InstantPage. | 133 // reply handler updates the instant support state in InstantTab. |
| 134 TEST_F(InstantPageTest, PageSupportsInstant) { | 134 TEST_F(InstantTabTest, PageSupportsInstant) { |
| 135 page.reset(new InstantPage(&delegate)); | 135 page.reset(new InstantTab(&delegate)); |
| 136 EXPECT_FALSE(page->supports_instant()); | 136 EXPECT_FALSE(page->supports_instant()); |
| 137 page->SetContents(web_contents()); | 137 page->SetContents(web_contents()); |
| 138 NavigateAndCommit(GURL("chrome-search://foo/bar")); | 138 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 139 process()->sink().ClearMessages(); | 139 process()->sink().ClearMessages(); |
| 140 SearchTabHelper::FromWebContents(web_contents())-> | 140 SearchTabHelper::FromWebContents(web_contents())-> |
| 141 DetermineIfPageSupportsInstant(); | 141 DetermineIfPageSupportsInstant(); |
| 142 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 142 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 143 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 143 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 144 ASSERT_TRUE(message != NULL); | 144 ASSERT_TRUE(message != NULL); |
| 145 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 145 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 146 | 146 |
| 147 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 147 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 148 .Times(1); | 148 .Times(1); |
| 149 | 149 |
| 150 // Assume the page supports instant. Invoke the message reply handler to make | 150 // Assume the page supports instant. Invoke the message reply handler to make |
| 151 // sure the InstantPage is notified about the instant support state. | 151 // sure the InstantTab is notified about the instant support state. |
| 152 const content::NavigationEntry* entry = | 152 const content::NavigationEntry* entry = |
| 153 web_contents()->GetController().GetLastCommittedEntry(); | 153 web_contents()->GetController().GetLastCommittedEntry(); |
| 154 EXPECT_TRUE(entry); | 154 EXPECT_TRUE(entry); |
| 155 SearchTabHelper::FromWebContents(web_contents())->InstantSupportChanged(true); | 155 SearchTabHelper::FromWebContents(web_contents())->InstantSupportChanged(true); |
| 156 EXPECT_TRUE(page->supports_instant()); | 156 EXPECT_TRUE(page->supports_instant()); |
| 157 } | 157 } |
| OLD | NEW |