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.
5 #include "chrome/browser/ui/search/search_tab_helper.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/search/search.h"
11 #include "chrome/browser/search_engines/template_url_service.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/signin/fake_signin_manager.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/ui/search/search_ipc_router.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/ntp_logging_events.h"
19 #include "chrome/common/omnibox_focus_state.h"
20 #include "chrome/common/render_messages.h"
21 #include "chrome/common/url_constants.h"
22 #include "chrome/test/base/browser_with_test_window_test.h"
23 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "content/public/browser/navigation_controller.h"
27 #include "content/public/browser/navigation_entry.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/test/mock_render_process_host.h"
30 #include "grit/generated_resources.h"
31 #include "ipc/ipc_message.h"
32 #include "ipc/ipc_test_sink.h"
33 #include "net/base/net_errors.h"
34 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36 #include "ui/base/l10n/l10n_util.h"
41 class MockSearchIPCRouterDelegate
: public SearchIPCRouter::Delegate
{
43 virtual ~MockSearchIPCRouterDelegate() {}
45 MOCK_METHOD1(OnInstantSupportDetermined
, void(bool supports_instant
));
46 MOCK_METHOD1(OnSetVoiceSearchSupport
, void(bool supports_voice_search
));
47 MOCK_METHOD1(FocusOmnibox
, void(OmniboxFocusState state
));
48 MOCK_METHOD3(NavigateToURL
, void(const GURL
&, WindowOpenDisposition
, bool));
49 MOCK_METHOD1(OnDeleteMostVisitedItem
, void(const GURL
& url
));
50 MOCK_METHOD1(OnUndoMostVisitedDeletion
, void(const GURL
& url
));
51 MOCK_METHOD0(OnUndoAllMostVisitedDeletions
, void());
52 MOCK_METHOD1(OnLogEvent
, void(NTPLoggingEventType event
));
53 MOCK_METHOD2(OnLogImpression
, void(int position
,
54 const base::string16
& provider
));
55 MOCK_METHOD1(PasteIntoOmnibox
, void(const base::string16
&));
56 MOCK_METHOD1(OnChromeIdentityCheck
, void(const base::string16
& identity
));
61 class SearchTabHelperTest
: public ChromeRenderViewHostTestHarness
{
63 virtual void SetUp() {
64 ChromeRenderViewHostTestHarness::SetUp();
65 SearchTabHelper::CreateForWebContents(web_contents());
68 // Creates a sign-in manager for tests. If |username| is not empty, the
69 // testing profile of the WebContents will be connected to the given account.
70 void CreateSigninManager(const std::string
& username
) {
71 SigninManagerBase
* signin_manager
= static_cast<SigninManagerBase
*>(
72 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
73 profile(), FakeSigninManagerBase::Build
));
75 if (!username
.empty()) {
76 ASSERT_TRUE(signin_manager
);
77 signin_manager
->SetAuthenticatedUsername(username
);
81 bool MessageWasSent(uint32 id
) {
82 return process()->sink().GetFirstMessageMatching(id
) != NULL
;
85 MockSearchIPCRouterDelegate
* mock_delegate() { return &delegate_
; }
88 MockSearchIPCRouterDelegate delegate_
;
91 TEST_F(SearchTabHelperTest
, DetermineIfPageSupportsInstant_Local
) {
92 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl
));
93 EXPECT_CALL(*mock_delegate(), OnInstantSupportDetermined(true)).Times(0);
95 SearchTabHelper
* search_tab_helper
=
96 SearchTabHelper::FromWebContents(web_contents());
97 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
98 search_tab_helper
->ipc_router().set_delegate(mock_delegate());
99 search_tab_helper
->DetermineIfPageSupportsInstant();
102 TEST_F(SearchTabHelperTest
, DetermineIfPageSupportsInstant_NonLocal
) {
103 NavigateAndCommit(GURL("chrome-search://foo/bar"));
104 process()->sink().ClearMessages();
105 EXPECT_CALL(*mock_delegate(), OnInstantSupportDetermined(true)).Times(1);
107 SearchTabHelper
* search_tab_helper
=
108 SearchTabHelper::FromWebContents(web_contents());
109 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
110 search_tab_helper
->ipc_router().set_delegate(mock_delegate());
111 search_tab_helper
->DetermineIfPageSupportsInstant();
112 ASSERT_TRUE(MessageWasSent(ChromeViewMsg_DetermineIfPageSupportsInstant::ID
));
114 scoped_ptr
<IPC::Message
> response(
115 new ChromeViewHostMsg_InstantSupportDetermined(
116 web_contents()->GetRoutingID(),
117 web_contents()->GetController().GetVisibleEntry()->GetPageID(),
119 search_tab_helper
->ipc_router().OnMessageReceived(*response
);
122 TEST_F(SearchTabHelperTest
, PageURLDoesntBelongToInstantRenderer
) {
123 // Navigate to a page URL that doesn't belong to Instant renderer.
124 // SearchTabHelper::DeterminerIfPageSupportsInstant() should return
125 // immediately without dispatching any message to the renderer.
126 NavigateAndCommit(GURL("http://www.example.com"));
127 process()->sink().ClearMessages();
128 EXPECT_CALL(*mock_delegate(), OnInstantSupportDetermined(false)).Times(0);
130 SearchTabHelper
* search_tab_helper
=
131 SearchTabHelper::FromWebContents(web_contents());
132 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
133 search_tab_helper
->ipc_router().set_delegate(mock_delegate());
134 search_tab_helper
->DetermineIfPageSupportsInstant();
135 ASSERT_FALSE(MessageWasSent(
136 ChromeViewMsg_DetermineIfPageSupportsInstant::ID
));
139 TEST_F(SearchTabHelperTest
, OnChromeIdentityCheckMatch
) {
140 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl
));
141 CreateSigninManager(std::string("foo@bar.com"));
142 SearchTabHelper
* search_tab_helper
=
143 SearchTabHelper::FromWebContents(web_contents());
144 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
146 const base::string16 test_identity
= base::ASCIIToUTF16("foo@bar.com");
147 search_tab_helper
->OnChromeIdentityCheck(test_identity
);
149 const IPC::Message
* message
= process()->sink().GetUniqueMessageMatching(
150 ChromeViewMsg_ChromeIdentityCheckResult::ID
);
151 ASSERT_TRUE(message
!= NULL
);
153 ChromeViewMsg_ChromeIdentityCheckResult::Param params
;
154 ChromeViewMsg_ChromeIdentityCheckResult::Read(message
, ¶ms
);
155 EXPECT_EQ(test_identity
, params
.a
);
156 ASSERT_TRUE(params
.b
);
159 TEST_F(SearchTabHelperTest
, OnChromeIdentityCheckMismatch
) {
160 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl
));
161 CreateSigninManager(std::string("foo@bar.com"));
162 SearchTabHelper
* search_tab_helper
=
163 SearchTabHelper::FromWebContents(web_contents());
164 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
166 const base::string16 test_identity
= base::ASCIIToUTF16("bar@foo.com");
167 search_tab_helper
->OnChromeIdentityCheck(test_identity
);
169 const IPC::Message
* message
= process()->sink().GetUniqueMessageMatching(
170 ChromeViewMsg_ChromeIdentityCheckResult::ID
);
171 ASSERT_TRUE(message
!= NULL
);
173 ChromeViewMsg_ChromeIdentityCheckResult::Param params
;
174 ChromeViewMsg_ChromeIdentityCheckResult::Read(message
, ¶ms
);
175 EXPECT_EQ(test_identity
, params
.a
);
176 ASSERT_FALSE(params
.b
);
179 TEST_F(SearchTabHelperTest
, OnChromeIdentityCheckSignedOutMatch
) {
180 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl
));
181 // This test does not sign in.
182 SearchTabHelper
* search_tab_helper
=
183 SearchTabHelper::FromWebContents(web_contents());
184 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
186 const base::string16 test_identity
;
187 search_tab_helper
->OnChromeIdentityCheck(test_identity
);
189 const IPC::Message
* message
= process()->sink().GetUniqueMessageMatching(
190 ChromeViewMsg_ChromeIdentityCheckResult::ID
);
191 ASSERT_TRUE(message
!= NULL
);
193 ChromeViewMsg_ChromeIdentityCheckResult::Param params
;
194 ChromeViewMsg_ChromeIdentityCheckResult::Read(message
, ¶ms
);
195 EXPECT_EQ(test_identity
, params
.a
);
196 ASSERT_TRUE(params
.b
);
199 TEST_F(SearchTabHelperTest
, OnChromeIdentityCheckSignedOutMismatch
) {
200 NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl
));
201 // This test does not sign in.
202 SearchTabHelper
* search_tab_helper
=
203 SearchTabHelper::FromWebContents(web_contents());
204 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
206 const base::string16 test_identity
= base::ASCIIToUTF16("bar@foo.com");
207 search_tab_helper
->OnChromeIdentityCheck(test_identity
);
209 const IPC::Message
* message
= process()->sink().GetUniqueMessageMatching(
210 ChromeViewMsg_ChromeIdentityCheckResult::ID
);
211 ASSERT_TRUE(message
!= NULL
);
213 ChromeViewMsg_ChromeIdentityCheckResult::Param params
;
214 ChromeViewMsg_ChromeIdentityCheckResult::Read(message
, ¶ms
);
215 EXPECT_EQ(test_identity
, params
.a
);
216 ASSERT_FALSE(params
.b
);
219 class TabTitleObserver
: public content::WebContentsObserver
{
221 explicit TabTitleObserver(content::WebContents
* contents
)
222 : WebContentsObserver(contents
) {}
224 base::string16
title_on_start() { return title_on_start_
; }
225 base::string16
title_on_commit() { return title_on_commit_
; }
228 virtual void DidStartProvisionalLoadForFrame(
229 int64
/* frame_id */,
230 int64
/* parent_frame_id */,
231 bool /* is_main_frame */,
232 const GURL
& /* validated_url */,
233 bool /* is_error_page */,
234 bool /* is_iframe_srcdoc */,
235 content::RenderViewHost
* /* render_view_host */) OVERRIDE
{
236 title_on_start_
= web_contents()->GetTitle();
239 virtual void DidNavigateMainFrame(
240 const content::LoadCommittedDetails
& /* details */,
241 const content::FrameNavigateParams
& /* params */) OVERRIDE
{
242 title_on_commit_
= web_contents()->GetTitle();
245 base::string16 title_on_start_
;
246 base::string16 title_on_commit_
;
249 TEST_F(SearchTabHelperTest
, TitleIsSetForNTP
) {
250 TabTitleObserver
title_observer(web_contents());
251 NavigateAndCommit(GURL(chrome::kChromeUINewTabURL
));
252 const base::string16 title
= l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE
);
253 EXPECT_EQ(title
, title_observer
.title_on_start());
254 EXPECT_EQ(title
, title_observer
.title_on_commit());
255 EXPECT_EQ(title
, web_contents()->GetTitle());
258 class SearchTabHelperWindowTest
: public BrowserWithTestWindowTest
{
260 virtual void SetUp() OVERRIDE
{
261 BrowserWithTestWindowTest::SetUp();
262 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
263 profile(), &TemplateURLServiceFactory::BuildInstanceFor
);
264 TemplateURLService
* template_url_service
=
265 TemplateURLServiceFactory::GetForProfile(profile());
266 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
268 TemplateURLData data
;
269 data
.SetURL("http://foo.com/url?bar={searchTerms}");
270 data
.instant_url
= "http://foo.com/instant?"
271 "{google:omniboxStartMarginParameter}{google:forceInstantResults}"
272 "foo=foo#foo=foo&strk";
273 data
.new_tab_url
= std::string("https://foo.com/newtab?strk");
274 data
.alternate_urls
.push_back("http://foo.com/alt#quux={searchTerms}");
275 data
.search_terms_replacement_key
= "strk";
277 TemplateURL
* template_url
= new TemplateURL(profile(), data
);
278 template_url_service
->Add(template_url
);
279 template_url_service
->SetDefaultSearchProvider(template_url
);
283 TEST_F(SearchTabHelperWindowTest
, OnProvisionalLoadFailRedirectNTPToLocal
) {
284 AddTab(browser(), GURL(chrome::kChromeUINewTabURL
));
285 content::WebContents
* contents
=
286 browser()->tab_strip_model()->GetWebContentsAt(0);
287 content::NavigationController
* controller
= &contents
->GetController();
289 SearchTabHelper
* search_tab_helper
=
290 SearchTabHelper::FromWebContents(contents
);
291 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
293 // A failed provisional load of a cacheable NTP should be redirected to local
295 const GURL cacheableNTPURL
= chrome::GetNewTabPageURL(profile());
296 search_tab_helper
->DidFailProvisionalLoad(1, base::string16(), true,
297 cacheableNTPURL
, 1, base::string16(), NULL
);
298 CommitPendingLoad(controller
);
299 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
),
300 controller
->GetLastCommittedEntry()->GetURL());
303 TEST_F(SearchTabHelperWindowTest
, OnProvisionalLoadFailDontRedirectIfAborted
) {
304 AddTab(browser(), GURL("chrome://blank"));
305 content::WebContents
* contents
=
306 browser()->tab_strip_model()->GetWebContentsAt(0);
307 content::NavigationController
* controller
= &contents
->GetController();
309 SearchTabHelper
* search_tab_helper
=
310 SearchTabHelper::FromWebContents(contents
);
311 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
313 // A failed provisional load of a cacheable NTP should be redirected to local
315 const GURL cacheableNTPURL
= chrome::GetNewTabPageURL(profile());
316 search_tab_helper
->DidFailProvisionalLoad(1, base::string16(), true,
317 cacheableNTPURL
, net::ERR_ABORTED
, base::string16(), NULL
);
318 CommitPendingLoad(controller
);
319 EXPECT_EQ(GURL("chrome://blank"),
320 controller
->GetLastCommittedEntry()->GetURL());
323 TEST_F(SearchTabHelperWindowTest
, OnProvisionalLoadFailDontRedirectNonNTP
) {
324 AddTab(browser(), GURL(chrome::kChromeUINewTabURL
));
325 content::WebContents
* contents
=
326 browser()->tab_strip_model()->GetWebContentsAt(0);
327 content::NavigationController
* controller
= &contents
->GetController();
329 SearchTabHelper
* search_tab_helper
=
330 SearchTabHelper::FromWebContents(contents
);
331 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
333 // Any other web page shouldn't be redirected when provisional load fails.
334 search_tab_helper
->DidFailProvisionalLoad(1, base::string16(), true,
335 GURL("http://www.example.com"), 1, base::string16(), NULL
);
336 CommitPendingLoad(controller
);
337 EXPECT_NE(GURL(chrome::kChromeSearchLocalNtpUrl
),
338 controller
->GetLastCommittedEntry()->GetURL());