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