| 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" |
| 11 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
| 12 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
| 13 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/test/mock_render_process_host.h" | 17 #include "content/public/test/mock_render_process_host.h" |
| 15 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 16 #include "ipc/ipc_test_sink.h" | 19 #include "ipc/ipc_test_sink.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 102 } |
| 100 | 103 |
| 101 bool FakePage::ShouldProcessUndoAllMostVisitedDeletions() { | 104 bool FakePage::ShouldProcessUndoAllMostVisitedDeletions() { |
| 102 return should_handle_messages_; | 105 return should_handle_messages_; |
| 103 } | 106 } |
| 104 | 107 |
| 105 } // namespace | 108 } // namespace |
| 106 | 109 |
| 107 class InstantPageTest : public ChromeRenderViewHostTestHarness { | 110 class InstantPageTest : public ChromeRenderViewHostTestHarness { |
| 108 public: | 111 public: |
| 112 virtual void SetUp() OVERRIDE; |
| 113 |
| 109 scoped_ptr<FakePage> page; | 114 scoped_ptr<FakePage> page; |
| 110 FakePageDelegate delegate; | 115 FakePageDelegate delegate; |
| 111 }; | 116 }; |
| 112 | 117 |
| 118 void InstantPageTest::SetUp() { |
| 119 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 120 switches::kEnableInstantExtendedAPI); |
| 121 ChromeRenderViewHostTestHarness::SetUp(); |
| 122 SearchTabHelper::CreateForWebContents(web_contents()); |
| 123 } |
| 124 |
| 113 TEST_F(InstantPageTest, IsLocal) { | 125 TEST_F(InstantPageTest, IsLocal) { |
| 114 page.reset(new FakePage(&delegate, "")); | 126 page.reset(new FakePage(&delegate, "")); |
| 127 EXPECT_FALSE(page->supports_instant()); |
| 115 EXPECT_FALSE(page->IsLocal()); | 128 EXPECT_FALSE(page->IsLocal()); |
| 116 page->SetContents(web_contents()); | 129 page->SetContents(web_contents()); |
| 117 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 130 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 118 EXPECT_TRUE(page->IsLocal()); | 131 EXPECT_TRUE(page->IsLocal()); |
| 119 NavigateAndCommit(GURL("http://example.com")); | 132 NavigateAndCommit(GURL("http://example.com")); |
| 120 EXPECT_FALSE(page->IsLocal()); | 133 EXPECT_FALSE(page->IsLocal()); |
| 121 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 134 NavigateAndCommit(GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
| 122 EXPECT_TRUE(page->IsLocal()); | 135 EXPECT_TRUE(page->IsLocal()); |
| 123 } | 136 } |
| 124 | 137 |
| 125 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { | 138 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_Local) { |
| 126 page.reset(new FakePage(&delegate, "")); | 139 page.reset(new FakePage(&delegate, "")); |
| 127 EXPECT_FALSE(page->supports_instant()); | 140 EXPECT_FALSE(page->supports_instant()); |
| 128 page->SetContents(web_contents()); | 141 page->SetContents(web_contents()); |
| 129 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 142 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 130 EXPECT_TRUE(page->IsLocal()); | 143 EXPECT_TRUE(page->IsLocal()); |
| 131 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) | 144 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 132 .Times(1); | 145 .Times(1); |
| 133 page->DetermineIfPageSupportsInstant(); | 146 SearchTabHelper::FromWebContents(web_contents())-> |
| 147 DetermineIfPageSupportsInstant(); |
| 134 EXPECT_TRUE(page->supports_instant()); | 148 EXPECT_TRUE(page->supports_instant()); |
| 135 } | 149 } |
| 136 | 150 |
| 137 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { | 151 TEST_F(InstantPageTest, DetermineIfPageSupportsInstant_NonLocal) { |
| 138 page.reset(new FakePage(&delegate, "")); | 152 page.reset(new FakePage(&delegate, "")); |
| 139 EXPECT_FALSE(page->supports_instant()); | 153 EXPECT_FALSE(page->supports_instant()); |
| 140 page->SetContents(web_contents()); | 154 page->SetContents(web_contents()); |
| 141 NavigateAndCommit(GURL("http://example.com/")); | 155 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 142 EXPECT_FALSE(page->IsLocal()); | 156 EXPECT_FALSE(page->IsLocal()); |
| 143 process()->sink().ClearMessages(); | 157 process()->sink().ClearMessages(); |
| 144 page->DetermineIfPageSupportsInstant(); | 158 SearchTabHelper::FromWebContents(web_contents())-> |
| 159 DetermineIfPageSupportsInstant(); |
| 145 const IPC::Message* message = process()->sink().GetFirstMessageMatching( | 160 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 146 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); | 161 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 147 ASSERT_TRUE(message != NULL); | 162 ASSERT_TRUE(message != NULL); |
| 148 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); | 163 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 149 } | 164 } |
| 150 | 165 |
| 151 TEST_F(InstantPageTest, DispatchRequestToDeleteMostVisitedItem) { | 166 TEST_F(InstantPageTest, DispatchRequestToDeleteMostVisitedItem) { |
| 152 page.reset(new FakePage(&delegate, "")); | 167 page.reset(new FakePage(&delegate, "")); |
| 153 page->SetContents(web_contents()); | 168 page->SetContents(web_contents()); |
| 154 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); | 169 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 EXPECT_CALL(delegate, UndoMostVisitedDeletion(item_url)).Times(0); | 241 EXPECT_CALL(delegate, UndoMostVisitedDeletion(item_url)).Times(0); |
| 227 EXPECT_TRUE(page->OnMessageReceived( | 242 EXPECT_TRUE(page->OnMessageReceived( |
| 228 ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(rvh()->GetRoutingID(), | 243 ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(rvh()->GetRoutingID(), |
| 229 page_id, item_url))); | 244 page_id, item_url))); |
| 230 | 245 |
| 231 EXPECT_CALL(delegate, UndoAllMostVisitedDeletions()).Times(0); | 246 EXPECT_CALL(delegate, UndoAllMostVisitedDeletions()).Times(0); |
| 232 EXPECT_TRUE(page->OnMessageReceived( | 247 EXPECT_TRUE(page->OnMessageReceived( |
| 233 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( | 248 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions( |
| 234 rvh()->GetRoutingID(), page_id))); | 249 rvh()->GetRoutingID(), page_id))); |
| 235 } | 250 } |
| 251 |
| 252 TEST_F(InstantPageTest, PageURLDoesntBelongToInstantRenderer) { |
| 253 page.reset(new FakePage(&delegate, "")); |
| 254 EXPECT_FALSE(page->supports_instant()); |
| 255 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 256 page->SetContents(web_contents()); |
| 257 |
| 258 // Navigate to a page URL that doesn't belong to Instant renderer. |
| 259 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return |
| 260 // immediately without dispatching any message to the renderer. |
| 261 NavigateAndCommit(GURL("http://www.example.com")); |
| 262 EXPECT_FALSE(page->IsLocal()); |
| 263 process()->sink().ClearMessages(); |
| 264 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false)) |
| 265 .Times(1); |
| 266 |
| 267 SearchTabHelper::FromWebContents(web_contents())-> |
| 268 DetermineIfPageSupportsInstant(); |
| 269 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 270 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 271 ASSERT_TRUE(message == NULL); |
| 272 EXPECT_FALSE(page->supports_instant()); |
| 273 } |
| 274 |
| 275 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message |
| 276 // reply handler updates the instant support state in InstantPage. |
| 277 TEST_F(InstantPageTest, PageSupportsInstant) { |
| 278 page.reset(new FakePage(&delegate, "")); |
| 279 EXPECT_FALSE(page->supports_instant()); |
| 280 page->SetContents(web_contents()); |
| 281 NavigateAndCommit(GURL("chrome-search://foo/bar")); |
| 282 process()->sink().ClearMessages(); |
| 283 SearchTabHelper::FromWebContents(web_contents())-> |
| 284 DetermineIfPageSupportsInstant(); |
| 285 const IPC::Message* message = process()->sink().GetFirstMessageMatching( |
| 286 ChromeViewMsg_DetermineIfPageSupportsInstant::ID); |
| 287 ASSERT_TRUE(message != NULL); |
| 288 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id()); |
| 289 |
| 290 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true)) |
| 291 .Times(1); |
| 292 |
| 293 // Assume the page supports instant. Invoke the message reply handler to make |
| 294 // sure the InstantPage is notified about the instant support state. |
| 295 const content::NavigationEntry* entry = |
| 296 web_contents()->GetController().GetActiveEntry(); |
| 297 EXPECT_TRUE(entry); |
| 298 SearchTabHelper::FromWebContents(web_contents())-> |
| 299 OnInstantSupportDetermined(entry->GetPageID(), true); |
| 300 EXPECT_TRUE(page->supports_instant()); |
| 301 } |
| OLD | NEW |