Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search / search_tab_helper_unittest.cc
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/search_tab_helper.h"
6
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"
37 #include "url/gurl.h"
38
39 namespace {
40
41 class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate {
42  public:
43   virtual ~MockSearchIPCRouterDelegate() {}
44
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));
57 };
58
59 }  // namespace
60
61 class SearchTabHelperTest : public ChromeRenderViewHostTestHarness {
62  public:
63   virtual void SetUp() {
64     ChromeRenderViewHostTestHarness::SetUp();
65     SearchTabHelper::CreateForWebContents(web_contents());
66   }
67
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));
74
75     if (!username.empty()) {
76       ASSERT_TRUE(signin_manager);
77       signin_manager->SetAuthenticatedUsername(username);
78     }
79   }
80
81   bool MessageWasSent(uint32 id) {
82     return process()->sink().GetFirstMessageMatching(id) != NULL;
83   }
84
85   MockSearchIPCRouterDelegate* mock_delegate() { return &delegate_; }
86
87  private:
88   MockSearchIPCRouterDelegate delegate_;
89 };
90
91 TEST_F(SearchTabHelperTest, DetermineIfPageSupportsInstant_Local) {
92   NavigateAndCommit(GURL(chrome::kChromeSearchLocalNtpUrl));
93   EXPECT_CALL(*mock_delegate(), OnInstantSupportDetermined(true)).Times(0);
94
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();
100 }
101
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);
106
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));
113
114   scoped_ptr<IPC::Message> response(
115       new ChromeViewHostMsg_InstantSupportDetermined(
116           web_contents()->GetRoutingID(),
117           web_contents()->GetController().GetVisibleEntry()->GetPageID(),
118           true));
119   search_tab_helper->ipc_router().OnMessageReceived(*response);
120 }
121
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);
129
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));
137 }
138
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);
145
146   const base::string16 test_identity = base::ASCIIToUTF16("foo@bar.com");
147   search_tab_helper->OnChromeIdentityCheck(test_identity);
148
149   const IPC::Message* message = process()->sink().GetUniqueMessageMatching(
150       ChromeViewMsg_ChromeIdentityCheckResult::ID);
151   ASSERT_TRUE(message != NULL);
152
153   ChromeViewMsg_ChromeIdentityCheckResult::Param params;
154   ChromeViewMsg_ChromeIdentityCheckResult::Read(message, &params);
155   EXPECT_EQ(test_identity, params.a);
156   ASSERT_TRUE(params.b);
157 }
158
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);
165
166   const base::string16 test_identity = base::ASCIIToUTF16("bar@foo.com");
167   search_tab_helper->OnChromeIdentityCheck(test_identity);
168
169   const IPC::Message* message = process()->sink().GetUniqueMessageMatching(
170       ChromeViewMsg_ChromeIdentityCheckResult::ID);
171   ASSERT_TRUE(message != NULL);
172
173   ChromeViewMsg_ChromeIdentityCheckResult::Param params;
174   ChromeViewMsg_ChromeIdentityCheckResult::Read(message, &params);
175   EXPECT_EQ(test_identity, params.a);
176   ASSERT_FALSE(params.b);
177 }
178
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);
185
186   const base::string16 test_identity;
187   search_tab_helper->OnChromeIdentityCheck(test_identity);
188
189   const IPC::Message* message = process()->sink().GetUniqueMessageMatching(
190       ChromeViewMsg_ChromeIdentityCheckResult::ID);
191   ASSERT_TRUE(message != NULL);
192
193   ChromeViewMsg_ChromeIdentityCheckResult::Param params;
194   ChromeViewMsg_ChromeIdentityCheckResult::Read(message, &params);
195   EXPECT_EQ(test_identity, params.a);
196   ASSERT_TRUE(params.b);
197 }
198
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);
205
206   const base::string16 test_identity = base::ASCIIToUTF16("bar@foo.com");
207   search_tab_helper->OnChromeIdentityCheck(test_identity);
208
209   const IPC::Message* message = process()->sink().GetUniqueMessageMatching(
210       ChromeViewMsg_ChromeIdentityCheckResult::ID);
211   ASSERT_TRUE(message != NULL);
212
213   ChromeViewMsg_ChromeIdentityCheckResult::Param params;
214   ChromeViewMsg_ChromeIdentityCheckResult::Read(message, &params);
215   EXPECT_EQ(test_identity, params.a);
216   ASSERT_FALSE(params.b);
217 }
218
219 class TabTitleObserver : public content::WebContentsObserver {
220  public:
221   explicit TabTitleObserver(content::WebContents* contents)
222       : WebContentsObserver(contents) {}
223
224   base::string16 title_on_start() { return title_on_start_; }
225   base::string16 title_on_commit() { return title_on_commit_; }
226
227  private:
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();
237   }
238
239   virtual void DidNavigateMainFrame(
240       const content::LoadCommittedDetails& /* details */,
241       const content::FrameNavigateParams& /* params */) OVERRIDE {
242     title_on_commit_ = web_contents()->GetTitle();
243   }
244
245   base::string16 title_on_start_;
246   base::string16 title_on_commit_;
247 };
248
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());
256 }
257
258 class SearchTabHelperWindowTest : public BrowserWithTestWindowTest {
259  protected:
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);
267
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";
276
277     TemplateURL* template_url = new TemplateURL(profile(), data);
278     template_url_service->Add(template_url);
279     template_url_service->SetDefaultSearchProvider(template_url);
280   }
281 };
282
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();
288
289   SearchTabHelper* search_tab_helper =
290       SearchTabHelper::FromWebContents(contents);
291   ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
292
293   // A failed provisional load of a cacheable NTP should be redirected to local
294   // NTP.
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());
301 }
302
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();
308
309   SearchTabHelper* search_tab_helper =
310       SearchTabHelper::FromWebContents(contents);
311   ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
312
313   // A failed provisional load of a cacheable NTP should be redirected to local
314   // NTP.
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());
321 }
322
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();
328
329   SearchTabHelper* search_tab_helper =
330       SearchTabHelper::FromWebContents(contents);
331   ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
332
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());
339 }