Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: chrome/browser/ui/search/instant_page_unittest.cc

Issue 14911005: Move instant support to SearchTabHelper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 }
129
130 TEST_F(InstantPageTest, PageURLDoesntBelongToInstantRenderer) {
131 EXPECT_FALSE(page->supports_instant());
132
133 // Navigate to a page URL that doesn't belong to Instant renderer.
134 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return
135 // immediately without dispatching any message to the renderer.
136 NavigateAndCommit(GURL("http://www.example.com"));
137 EXPECT_FALSE(page->IsLocal());
138 process()->sink().ClearMessages();
139 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), false))
140 .Times(1);
141
142 SearchTabHelper::FromWebContents(web_contents())->
143 DetermineIfPageSupportsInstant();
144 const IPC::Message* message = process()->sink().GetFirstMessageMatching(
145 ChromeViewMsg_DetermineIfPageSupportsInstant::ID);
146 ASSERT_TRUE(message == NULL);
147 EXPECT_FALSE(page->supports_instant());
148 }
149
150 // Test to verify that ChromeViewMsg_DetermineIfPageSupportsInstant message
151 // reply handler updates the instant support state in InstantPage.
152 TEST_F(InstantPageTest, PageSupportsInstant) {
153 EXPECT_FALSE(page->supports_instant());
154 NavigateAndCommit(GURL("chrome-search://foo/bar"));
155 process()->sink().ClearMessages();
156 SearchTabHelper::FromWebContents(web_contents())->
157 DetermineIfPageSupportsInstant();
158 const IPC::Message* message = process()->sink().GetFirstMessageMatching(
159 ChromeViewMsg_DetermineIfPageSupportsInstant::ID);
160 ASSERT_TRUE(message != NULL);
161 EXPECT_EQ(web_contents()->GetRoutingID(), message->routing_id());
162
163 EXPECT_CALL(delegate, InstantSupportDetermined(web_contents(), true))
164 .Times(1);
165
166 // Assume the page supports instant. Invoke the message reply handler to make
167 // sure the InstantPage is notified about the instant support state.
168 const content::NavigationEntry* entry =
169 web_contents()->GetController().GetActiveEntry();
170 EXPECT_TRUE(entry);
171 SearchTabHelper::FromWebContents(web_contents())->
172 OnInstantSupportDetermined(entry->GetPageID(), true);
173 EXPECT_TRUE(page->supports_instant());
174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698