| 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_page.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 10 #include "chrome/common/chrome_switches.h" |
| 8 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
| 9 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 14 #include "content/public/browser/navigation_entry.h" |
| 11 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/test/mock_render_process_host.h" | 16 #include "content/public/test/mock_render_process_host.h" |
| 13 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 14 #include "ipc/ipc_test_sink.h" | 18 #include "ipc/ipc_test_sink.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 class FakePageDelegate : public InstantPage::Delegate { | 24 class FakePageDelegate : public InstantPage::Delegate { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 61 |
| 58 class FakePage : public InstantPage { | 62 class FakePage : public InstantPage { |
| 59 public: | 63 public: |
| 60 FakePage(Delegate* delegate, const std::string& instant_url) | 64 FakePage(Delegate* delegate, const std::string& instant_url) |
| 61 : InstantPage(delegate, instant_url) { | 65 : InstantPage(delegate, instant_url) { |
| 62 } | 66 } |
| 63 virtual ~FakePage() { | 67 virtual ~FakePage() { |
| 64 } | 68 } |
| 65 | 69 |
| 66 using InstantPage::SetContents; | 70 using InstantPage::SetContents; |
| 71 |
| 72 private: |
| 73 DISALLOW_COPY_AND_ASSIGN(FakePage); |
| 67 }; | 74 }; |
| 68 | 75 |
| 69 } // namespace | 76 } // namespace |
| 70 | 77 |
| 71 class InstantPageTest : public ChromeRenderViewHostTestHarness { | 78 class InstantPageTest : public ChromeRenderViewHostTestHarness { |
| 72 public: | 79 public: |
| 80 virtual void SetUp() OVERRIDE; |
| 81 |
| 73 scoped_ptr<FakePage> page; | 82 scoped_ptr<FakePage> page; |
| 74 FakePageDelegate delegate; | 83 FakePageDelegate delegate; |
| 75 }; | 84 }; |
| 76 | 85 |
| 86 void InstantPageTest::SetUp() { |
| 87 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 88 switches::kEnableInstantExtendedAPI); |
| 89 ChromeRenderViewHostTestHarness::SetUp(); |
| 90 SearchTabHelper::CreateForWebContents(web_contents()); |
| 91 page.reset(new FakePage(&delegate, "")); |
| 92 page->SetContents(web_contents()); |
| 93 } |
| 94 |
| 77 TEST_F(InstantPageTest, IsLocal) { | 95 TEST_F(InstantPageTest, IsLocal) { |
| 78 page.reset(new FakePage(&delegate, "")); | 96 EXPECT_FALSE(page->supports_instant()); |
| 79 EXPECT_FALSE(page->IsLocal()); | 97 EXPECT_FALSE(page->IsLocal()); |
| 80 page->SetContents(web_contents()); | |
| 81 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 98 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 82 EXPECT_TRUE(page->IsLocal()); | 99 EXPECT_TRUE(page->IsLocal()); |
| 83 NavigateAndCommit(GURL("http://example.com")); | 100 NavigateAndCommit(GURL("http://example.com")); |
| 84 EXPECT_FALSE(page->IsLocal()); | 101 EXPECT_FALSE(page->IsLocal()); |
| 85 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 102 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
| 86 EXPECT_TRUE(page->IsLocal()); | 103 EXPECT_TRUE(page->IsLocal()); |
| 87 } | 104 } |
| 88 | 105 |
| 89 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { | 106 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { |
| 90 page.reset(new FakePage(&delegate, "")); | |
| 91 EXPECT_FALSE(page->supports_instant()); | 107 EXPECT_FALSE(page->supports_instant()); |
| 92 page->SetContents(web_contents()); | |
| 93 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 108 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 94 EXPECT_TRUE(page->IsLocal()); | 109 EXPECT_TRUE(page->IsLocal()); |
| 95 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 110 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 96 .Times(1); | 111 .Times(1); |
| 97 page->DetermineIfPageSupportsInstant(); | 112 SearchTabHelper::FromWebContents(web_contents())-> |
| 113 DetermineIfPageSupportsInstant(); |
| 98 EXPECT_TRUE(page->supports_instant()); | 114 EXPECT_TRUE(page->supports_instant()); |
| 99 } | 115 } |
| 100 | 116 |
| 101 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { | 117 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { |
| 102 page.reset(new FakePage(&delegate, "")); | |
| 103 EXPECT_FALSE(page->supports_instant()); | 118 EXPECT_FALSE(page->supports_instant()); |
| 104 page->SetContents(web_contents()); | 119 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 105 NavigateAndCommit(GURL("http://example.com/")); | |
| 106 EXPECT_FALSE(page->IsLocal()); | 120 EXPECT_FALSE(page->IsLocal()); |
| 107 process()->sink().ClearMessages(); | 121 process()->sink().ClearMessages(); |
| 108 page->DetermineIfPageSupportsInstant(); | 122 SearchTabHelper::FromWebContents(web_contents())-> |
| 123 DetermineIfPageSupportsInstant(); |
| 109 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 124 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 110 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 125 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 111 ASSERT_TRUE(message != NULL); | 126 ASSERT_TRUE(message != NULL); |
| 112 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 127 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 113 } | 128 } |
| 114 | 129 |
| 115 TEST_F(InstantPageTest, DispatchRequestToDeleteMostVisitedItem) { | 130 TEST_F(InstantPageTest, DispatchRequestToDeleteMostVisitedItem) { |
| 116 page.reset(new FakePage(&delegate, "")); | 131 page.reset(new FakePage(&delegate, "")); |
| 117 page->SetContents(web_contents()); | 132 page->SetContents(web_contents()); |
| 118 GURL item_url("www.foo.com"); | 133 GURL item_url("www.foo.com"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 133 } | 148 } |
| 134 | 149 |
| 135 TEST_F(InstantPageTest, DispatchRequestToUndoAllMostVisitedDeletions) { | 150 TEST_F(InstantPageTest, DispatchRequestToUndoAllMostVisitedDeletions) { |
| 136 page.reset(new FakePage(&delegate, "")); | 151 page.reset(new FakePage(&delegate, "")); |
| 137 page->SetContents(web_contents()); | 152 page->SetContents(web_contents()); |
| 138 EXPECT_CALL(delegate, UndoAllMostVisitedDeletions()).Times(1); | 153 EXPECT_CALL(delegate, UndoAllMostVisitedDeletions()).Times(1); |
| 139 EXPECT_TRUE(page->OnMessageReceived( | 154 EXPECT_TRUE(page->OnMessageReceived( |
| 140 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( | 155 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( |
| 141 rvh()->GetRoutingID()))); | 156 rvh()->GetRoutingID()))); |
| 142 } | 157 } |
| 158 |
| 159 TEST_F(InstantPageTest, PageURLDoesntBelongToInstantRenderer) { |
| 160 EXPECT_FALSE(page->supports_instant()); |
| 161 |
| 162 // Navigate to a page URL that doesn't belong to Instant renderer. |
| 163 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return |
| 164 // immediately without dispatching any message to the renderer. |
| 165 NavigateAndCommit(GURL("http://www.example.com")); |
| 166 EXPECT_FALSE(page->IsLocal()); |
| 167 process()->sink().ClearMessages(); |
| 168 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false)) |
| 169 .Times(1); |
| 170 |
| 171 SearchTabHelper::FromWebContents(web_contents())-> |
| 172 DetermineIfPageSupportsInstant(); |
| 173 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 174 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 175 ASSERT_TRUE(message == NULL); |
| 176 EXPECT_FALSE(page->supports_instant()); |
| 177 } |
| 178 |
| 179 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message |
| 180 // reply handler updates the instant support state in InstantPage. |
| 181 TEST_F(InstantPageTest, PageSupportsInstant) { |
| 182 EXPECT_FALSE(page->supports_instant()); |
| 183 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 184 process()->sink().ClearMessages(); |
| 185 SearchTabHelper::FromWebContents(web_contents())-> |
| 186 DetermineIfPageSupportsInstant(); |
| 187 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 188 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 189 ASSERT_TRUE(message != NULL); |
| 190 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 191 |
| 192 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 193 .Times(1); |
| 194 |
| 195 // Assume the page supports instant. Invoke the message reply handler to make |
| 196 // sure the InstantPage is notified about the instant support state. |
| 197 const content::NavigationEntry* entry = |
| 198 web_contents()->GetController().GetActiveEntry(); |
| 199 EXPECT_TRUE(entry); |
| 200 SearchTabHelper::FromWebContents(web_contents())-> |
| 201 OnInstantSupportDetermined(entry->GetPageID(), true); |
| 202 EXPECT_TRUE(page->supports_instant()); |
| 203 } |
| OLD | NEW |