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_ipc_router.h"
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/tuple.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h"
20 #include "chrome/browser/ui/search/search_tab_helper.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/instant_types.h"
24 #include "chrome/common/ntp_logging_events.h"
25 #include "chrome/common/omnibox_focus_state.h"
26 #include "chrome/common/render_messages.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/test/base/browser_with_test_window_test.h"
29 #include "chrome/test/base/ui_test_utils.h"
30 #include "content/public/browser/navigation_controller.h"
31 #include "content/public/browser/navigation_entry.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/test/mock_render_process_host.h"
34 #include "ipc/ipc_message.h"
35 #include "ipc/ipc_test_sink.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "ui/base/window_open_disposition.h"
43 class MockSearchIPCRouterDelegate
: public SearchIPCRouter::Delegate
{
45 virtual ~MockSearchIPCRouterDelegate() {}
47 MOCK_METHOD1(OnInstantSupportDetermined
, void(bool supports_instant
));
48 MOCK_METHOD1(OnSetVoiceSearchSupport
, void(bool supports_voice_search
));
49 MOCK_METHOD1(FocusOmnibox
, void(OmniboxFocusState state
));
50 MOCK_METHOD3(NavigateToURL
, void(const GURL
&, WindowOpenDisposition
, bool));
51 MOCK_METHOD1(OnDeleteMostVisitedItem
, void(const GURL
& url
));
52 MOCK_METHOD1(OnUndoMostVisitedDeletion
, void(const GURL
& url
));
53 MOCK_METHOD0(OnUndoAllMostVisitedDeletions
, void());
54 MOCK_METHOD1(OnLogEvent
, void(NTPLoggingEventType event
));
55 MOCK_METHOD2(OnLogImpression
, void(int position
,
56 const base::string16
& provider
));
57 MOCK_METHOD1(PasteIntoOmnibox
, void(const base::string16
&));
58 MOCK_METHOD1(OnChromeIdentityCheck
, void(const base::string16
& identity
));
61 class MockSearchIPCRouterPolicy
: public SearchIPCRouter::Policy
{
63 virtual ~MockSearchIPCRouterPolicy() {}
65 MOCK_METHOD0(ShouldProcessSetVoiceSearchSupport
, bool());
66 MOCK_METHOD1(ShouldProcessFocusOmnibox
, bool(bool));
67 MOCK_METHOD1(ShouldProcessNavigateToURL
, bool(bool));
68 MOCK_METHOD0(ShouldProcessDeleteMostVisitedItem
, bool());
69 MOCK_METHOD0(ShouldProcessUndoMostVisitedDeletion
, bool());
70 MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions
, bool());
71 MOCK_METHOD0(ShouldProcessLogEvent
, bool());
72 MOCK_METHOD1(ShouldProcessPasteIntoOmnibox
, bool(bool));
73 MOCK_METHOD0(ShouldProcessChromeIdentityCheck
, bool());
74 MOCK_METHOD0(ShouldSendSetPromoInformation
, bool());
75 MOCK_METHOD0(ShouldSendSetDisplayInstantResults
, bool());
76 MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch
, bool());
77 MOCK_METHOD0(ShouldSendSetOmniboxStartMargin
, bool());
78 MOCK_METHOD0(ShouldSendMostVisitedItems
, bool());
79 MOCK_METHOD0(ShouldSendThemeBackgroundInfo
, bool());
80 MOCK_METHOD0(ShouldSendToggleVoiceSearch
, bool());
81 MOCK_METHOD0(ShouldSubmitQuery
, bool());
86 class SearchIPCRouterTest
: public BrowserWithTestWindowTest
{
88 SearchIPCRouterTest() : field_trial_list_(NULL
) {}
90 virtual void SetUp() {
91 BrowserWithTestWindowTest::SetUp();
92 AddTab(browser(), GURL("chrome://blank"));
93 SearchTabHelper::CreateForWebContents(web_contents());
95 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
97 &TemplateURLServiceFactory::BuildInstanceFor
);
98 TemplateURLService
* template_url_service
=
99 TemplateURLServiceFactory::GetForProfile(profile());
100 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
102 TemplateURLData data
;
103 data
.SetURL("http://foo.com/url?bar={searchTerms}");
104 data
.instant_url
= "http://foo.com/instant?"
105 "{google:omniboxStartMarginParameter}foo=foo#foo=foo&espv";
106 data
.new_tab_url
= "https://foo.com/newtab?espv";
107 data
.alternate_urls
.push_back("http://foo.com/alt#quux={searchTerms}");
108 data
.search_terms_replacement_key
= "espv";
110 TemplateURL
* template_url
= new TemplateURL(profile(), data
);
111 // Takes ownership of |template_url|.
112 template_url_service
->Add(template_url
);
113 template_url_service
->SetDefaultSearchProvider(template_url
);
114 process()->sink().ClearMessages();
117 content::WebContents
* web_contents() {
118 return browser()->tab_strip_model()->GetActiveWebContents();
121 content::MockRenderProcessHost
* process() {
122 return static_cast<content::MockRenderProcessHost
*>(
123 web_contents()->GetRenderViewHost()->GetProcess());
126 SearchTabHelper
* GetSearchTabHelper(
127 content::WebContents
* web_contents
) {
128 EXPECT_NE(static_cast<content::WebContents
*>(NULL
), web_contents
);
129 return SearchTabHelper::FromWebContents(web_contents
);
132 void SetupMockDelegateAndPolicy() {
133 content::WebContents
* contents
= web_contents();
134 ASSERT_NE(static_cast<content::WebContents
*>(NULL
), contents
);
135 SearchTabHelper
* search_tab_helper
= GetSearchTabHelper(contents
);
136 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
137 search_tab_helper
->ipc_router().set_delegate(mock_delegate());
138 search_tab_helper
->ipc_router().set_policy(
139 make_scoped_ptr(new MockSearchIPCRouterPolicy
)
140 .PassAs
<SearchIPCRouter::Policy
>());
143 bool MessageWasSent(uint32 id
) {
144 return process()->sink().GetFirstMessageMatching(id
) != NULL
;
147 void VerifyDisplayInstantResultsMsg(bool expected_param_value
) {
148 SetupMockDelegateAndPolicy();
149 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
150 EXPECT_CALL(*policy
, ShouldSendSetDisplayInstantResults()).Times(1)
151 .WillOnce(testing::Return(true));
153 GetSearchIPCRouter().SetDisplayInstantResults();
154 const IPC::Message
* message
= process()->sink().GetFirstMessageMatching(
155 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID
);
156 EXPECT_NE(static_cast<const IPC::Message
*>(NULL
), message
);
157 Tuple1
<bool> display_instant_results_param
;
158 ChromeViewMsg_SearchBoxSetDisplayInstantResults::Read(
159 message
, &display_instant_results_param
);
160 EXPECT_EQ(expected_param_value
, display_instant_results_param
.a
);
163 MockSearchIPCRouterDelegate
* mock_delegate() { return &delegate_
; }
165 MockSearchIPCRouterPolicy
* GetSearchIPCRouterPolicy() {
166 content::WebContents
* contents
= web_contents();
167 EXPECT_NE(static_cast<content::WebContents
*>(NULL
), contents
);
168 SearchTabHelper
* search_tab_helper
= GetSearchTabHelper(contents
);
169 EXPECT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
170 return static_cast<MockSearchIPCRouterPolicy
*>(
171 search_tab_helper
->ipc_router().policy());
174 SearchIPCRouter
& GetSearchIPCRouter() {
175 return GetSearchTabHelper(web_contents())->ipc_router();
178 void OnMessageReceived(const IPC::Message
& message
) {
179 GetSearchIPCRouter().OnMessageReceived(message
);
182 bool IsActiveTab(content::WebContents
* contents
) {
183 return GetSearchTabHelper(contents
)->ipc_router().is_active_tab_
;
187 MockSearchIPCRouterDelegate delegate_
;
188 base::FieldTrialList field_trial_list_
;
191 TEST_F(SearchIPCRouterTest
, ProcessVoiceSearchSupportMsg
) {
192 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
193 SetupMockDelegateAndPolicy();
194 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
195 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(1);
196 EXPECT_CALL(*(policy
), ShouldProcessSetVoiceSearchSupport()).Times(1)
197 .WillOnce(testing::Return(true));
199 content::WebContents
* contents
= web_contents();
200 scoped_ptr
<IPC::Message
> message(
201 new ChromeViewHostMsg_SetVoiceSearchSupported(
202 contents
->GetRoutingID(),
203 contents
->GetController().GetVisibleEntry()->GetPageID(),
205 OnMessageReceived(*message
);
208 TEST_F(SearchIPCRouterTest
, IgnoreVoiceSearchSupportMsg
) {
209 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
210 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(0);
211 SetupMockDelegateAndPolicy();
212 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
213 EXPECT_CALL(*policy
, ShouldProcessSetVoiceSearchSupport()).Times(1)
214 .WillOnce(testing::Return(false));
216 content::WebContents
* contents
= web_contents();
217 scoped_ptr
<IPC::Message
> message(
218 new ChromeViewHostMsg_SetVoiceSearchSupported(
219 contents
->GetRoutingID(),
220 contents
->GetController().GetVisibleEntry()->GetPageID(),
222 OnMessageReceived(*message
);
225 TEST_F(SearchIPCRouterTest
, ProcessFocusOmniboxMsg
) {
226 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
227 SetupMockDelegateAndPolicy();
228 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
229 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(1);
231 content::WebContents
* contents
= web_contents();
232 bool is_active_tab
= IsActiveTab(contents
);
233 EXPECT_TRUE(is_active_tab
);
234 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(1)
235 .WillOnce(testing::Return(true));
237 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_FocusOmnibox(
238 contents
->GetRoutingID(),
239 contents
->GetController().GetVisibleEntry()->GetPageID(),
240 OMNIBOX_FOCUS_VISIBLE
));
241 OnMessageReceived(*message
);
244 TEST_F(SearchIPCRouterTest
, IgnoreFocusOmniboxMsg
) {
245 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
246 SetupMockDelegateAndPolicy();
247 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
248 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(0);
250 content::WebContents
* contents
= web_contents();
251 bool is_active_tab
= IsActiveTab(contents
);
252 EXPECT_TRUE(is_active_tab
);
253 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(1)
254 .WillOnce(testing::Return(false));
256 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_FocusOmnibox(
257 contents
->GetRoutingID(),
258 contents
->GetController().GetVisibleEntry()->GetPageID(),
259 OMNIBOX_FOCUS_VISIBLE
));
260 OnMessageReceived(*message
);
263 TEST_F(SearchIPCRouterTest
, HandleTabChangedEvents
) {
264 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
265 content::WebContents
* contents
= web_contents();
266 EXPECT_EQ(0, browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
267 EXPECT_TRUE(IsActiveTab(contents
));
269 // Add a new tab to deactivate the current tab.
270 AddTab(browser(), GURL(content::kAboutBlankURL
));
271 EXPECT_EQ(2, browser()->tab_strip_model()->count());
272 EXPECT_EQ(1, browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
273 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
274 EXPECT_FALSE(IsActiveTab(contents
));
276 // Activate the first tab.
277 browser()->tab_strip_model()->ActivateTabAt(1, false);
278 EXPECT_EQ(browser()->tab_strip_model()->active_index(),
279 browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
280 EXPECT_TRUE(IsActiveTab(contents
));
283 TEST_F(SearchIPCRouterTest
, ProcessNavigateToURLMsg
) {
284 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
285 SetupMockDelegateAndPolicy();
286 GURL
destination_url("www.foo.com");
287 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
289 content::WebContents
* contents
= web_contents();
290 bool is_active_tab
= IsActiveTab(contents
);
291 EXPECT_TRUE(is_active_tab
);
293 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
294 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(1)
295 .WillOnce(testing::Return(true));
297 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_SearchBoxNavigate(
298 contents
->GetRoutingID(),
299 contents
->GetController().GetVisibleEntry()->GetPageID(),
300 destination_url
, CURRENT_TAB
, true));
301 OnMessageReceived(*message
);
304 TEST_F(SearchIPCRouterTest
, IgnoreNavigateToURLMsg
) {
305 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
306 SetupMockDelegateAndPolicy();
307 GURL
destination_url("www.foo.com");
308 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
310 content::WebContents
* contents
= web_contents();
311 bool is_active_tab
= IsActiveTab(contents
);
312 EXPECT_TRUE(is_active_tab
);
314 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
315 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(1)
316 .WillOnce(testing::Return(false));
318 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_SearchBoxNavigate(
319 contents
->GetRoutingID(),
320 contents
->GetController().GetVisibleEntry()->GetPageID(),
321 destination_url
, CURRENT_TAB
, true));
322 OnMessageReceived(*message
);
325 TEST_F(SearchIPCRouterTest
, ProcessLogEventMsg
) {
326 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
327 SetupMockDelegateAndPolicy();
328 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
329 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
)).Times(1);
330 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
331 .WillOnce(testing::Return(true));
333 content::WebContents
* contents
= web_contents();
334 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_LogEvent(
335 contents
->GetRoutingID(),
336 contents
->GetController().GetVisibleEntry()->GetPageID(),
338 OnMessageReceived(*message
);
341 TEST_F(SearchIPCRouterTest
, IgnoreLogEventMsg
) {
342 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
343 SetupMockDelegateAndPolicy();
344 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
345 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
)).Times(0);
346 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
347 .WillOnce(testing::Return(false));
349 content::WebContents
* contents
= web_contents();
350 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_LogEvent(
351 contents
->GetRoutingID(),
352 contents
->GetController().GetVisibleEntry()->GetPageID(),
354 OnMessageReceived(*message
);
357 TEST_F(SearchIPCRouterTest
, ProcessLogImpressionMsg
) {
358 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
359 SetupMockDelegateAndPolicy();
360 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
361 EXPECT_CALL(*mock_delegate(),
362 OnLogImpression(3, base::ASCIIToUTF16("Server"))).Times(1);
363 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
364 .WillOnce(testing::Return(true));
366 content::WebContents
* contents
= web_contents();
367 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_LogImpression(
368 contents
->GetRoutingID(),
369 contents
->GetController().GetVisibleEntry()->GetPageID(),
371 base::ASCIIToUTF16("Server")));
372 OnMessageReceived(*message
);
375 TEST_F(SearchIPCRouterTest
, ProcessChromeIdentityCheckMsg
) {
376 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
377 SetupMockDelegateAndPolicy();
378 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
379 const base::string16 test_identity
= base::ASCIIToUTF16("foo@bar.com");
380 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity
)).Times(1);
381 EXPECT_CALL(*policy
, ShouldProcessChromeIdentityCheck()).Times(1)
382 .WillOnce(testing::Return(true));
384 content::WebContents
* contents
= web_contents();
385 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_ChromeIdentityCheck(
386 contents
->GetRoutingID(),
387 contents
->GetController().GetVisibleEntry()->GetPageID(),
389 OnMessageReceived(*message
);
392 TEST_F(SearchIPCRouterTest
, IgnoreChromeIdentityCheckMsg
) {
393 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
394 SetupMockDelegateAndPolicy();
395 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
397 const base::string16 test_identity
= base::ASCIIToUTF16("foo@bar.com");
398 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity
)).Times(0);
399 EXPECT_CALL(*policy
, ShouldProcessChromeIdentityCheck()).Times(1)
400 .WillOnce(testing::Return(false));
402 content::WebContents
* contents
= web_contents();
403 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_ChromeIdentityCheck(
404 contents
->GetRoutingID(),
405 contents
->GetController().GetVisibleEntry()->GetPageID(),
407 OnMessageReceived(*message
);
410 TEST_F(SearchIPCRouterTest
, ProcessDeleteMostVisitedItemMsg
) {
411 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
412 SetupMockDelegateAndPolicy();
413 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
414 GURL
item_url("www.foo.com");
415 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(1);
416 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(1)
417 .WillOnce(testing::Return(true));
419 content::WebContents
* contents
= web_contents();
420 scoped_ptr
<IPC::Message
> message(
421 new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
422 contents
->GetRoutingID(),
423 contents
->GetController().GetVisibleEntry()->GetPageID(),
425 OnMessageReceived(*message
);
428 TEST_F(SearchIPCRouterTest
, IgnoreDeleteMostVisitedItemMsg
) {
429 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
430 SetupMockDelegateAndPolicy();
431 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
432 GURL
item_url("www.foo.com");
433 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(0);
434 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(1)
435 .WillOnce(testing::Return(false));
437 content::WebContents
* contents
= web_contents();
438 scoped_ptr
<IPC::Message
> message(
439 new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
440 contents
->GetRoutingID(),
441 contents
->GetController().GetVisibleEntry()->GetPageID(),
443 OnMessageReceived(*message
);
446 TEST_F(SearchIPCRouterTest
, ProcessUndoMostVisitedDeletionMsg
) {
447 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
448 SetupMockDelegateAndPolicy();
449 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
450 GURL
item_url("www.foo.com");
451 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(1);
452 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(1)
453 .WillOnce(testing::Return(true));
455 content::WebContents
* contents
= web_contents();
456 scoped_ptr
<IPC::Message
> message(
457 new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
458 contents
->GetRoutingID(),
459 contents
->GetController().GetVisibleEntry()->GetPageID(),
461 OnMessageReceived(*message
);
464 TEST_F(SearchIPCRouterTest
, IgnoreUndoMostVisitedDeletionMsg
) {
465 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
466 SetupMockDelegateAndPolicy();
467 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
468 GURL
item_url("www.foo.com");
469 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(0);
470 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(1)
471 .WillOnce(testing::Return(false));
473 content::WebContents
* contents
= web_contents();
474 scoped_ptr
<IPC::Message
> message(
475 new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
476 contents
->GetRoutingID(),
477 contents
->GetController().GetVisibleEntry()->GetPageID(),
479 OnMessageReceived(*message
);
482 TEST_F(SearchIPCRouterTest
, ProcessUndoAllMostVisitedDeletionsMsg
) {
483 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
484 SetupMockDelegateAndPolicy();
485 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
486 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(1);
487 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
488 .WillOnce(testing::Return(true));
490 content::WebContents
* contents
= web_contents();
491 scoped_ptr
<IPC::Message
> message(
492 new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
493 contents
->GetRoutingID(),
494 contents
->GetController().GetVisibleEntry()->GetPageID()));
495 OnMessageReceived(*message
);
498 TEST_F(SearchIPCRouterTest
, IgnoreUndoAllMostVisitedDeletionsMsg
) {
499 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
500 SetupMockDelegateAndPolicy();
501 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
502 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
503 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
504 .WillOnce(testing::Return(false));
506 content::WebContents
* contents
= web_contents();
507 scoped_ptr
<IPC::Message
> message(
508 new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
509 contents
->GetRoutingID(),
510 contents
->GetController().GetVisibleEntry()->GetPageID()));
511 OnMessageReceived(*message
);
514 TEST_F(SearchIPCRouterTest
, IgnoreMessageIfThePageIsNotActive
) {
515 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
516 SetupMockDelegateAndPolicy();
517 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
519 content::WebContents
* contents
= web_contents();
520 bool is_active_tab
= IsActiveTab(contents
);
521 int invalid_page_id
= 1000;
522 GURL
item_url("www.foo.com");
523 EXPECT_CALL(*mock_delegate(), NavigateToURL(item_url
, CURRENT_TAB
,
525 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(0);
527 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_SearchBoxNavigate(
528 contents
->GetRoutingID(), invalid_page_id
, item_url
, CURRENT_TAB
,
530 OnMessageReceived(*message
);
532 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(0);
533 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(0);
534 message
.reset(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
535 contents
->GetRoutingID(), invalid_page_id
, item_url
));
536 OnMessageReceived(*message
);
538 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(0);
539 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(0);
540 message
.reset(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
541 contents
->GetRoutingID(), invalid_page_id
, item_url
));
542 OnMessageReceived(*message
);
544 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
545 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(0);
546 message
.reset(new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
547 contents
->GetRoutingID(), invalid_page_id
));
548 OnMessageReceived(*message
);
550 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(0);
551 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(0);
552 message
.reset(new ChromeViewHostMsg_FocusOmnibox(
553 contents
->GetRoutingID(), invalid_page_id
, OMNIBOX_FOCUS_VISIBLE
));
554 OnMessageReceived(*message
);
556 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
)).Times(0);
557 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(0);
558 message
.reset(new ChromeViewHostMsg_LogEvent(contents
->GetRoutingID(),
559 invalid_page_id
, NTP_MOUSEOVER
));
560 OnMessageReceived(*message
);
563 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(0);
564 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(0);
565 message
.reset(new ChromeViewHostMsg_PasteAndOpenDropdown(
566 contents
->GetRoutingID(), invalid_page_id
, text
));
567 OnMessageReceived(*message
);
570 TEST_F(SearchIPCRouterTest
, ProcessPasteAndOpenDropdownMsg
) {
571 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
572 SetupMockDelegateAndPolicy();
573 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
575 content::WebContents
* contents
= web_contents();
576 bool is_active_tab
= IsActiveTab(contents
);
577 EXPECT_TRUE(is_active_tab
);
580 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(1);
581 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(1)
582 .WillOnce(testing::Return(true));
583 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_PasteAndOpenDropdown(
584 contents
->GetRoutingID(),
585 contents
->GetController().GetVisibleEntry()->GetPageID(), text
));
586 OnMessageReceived(*message
);
589 TEST_F(SearchIPCRouterTest
, IgnorePasteAndOpenDropdownMsg
) {
590 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
591 SetupMockDelegateAndPolicy();
593 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(0);
595 content::WebContents
* contents
= web_contents();
596 bool is_active_tab
= IsActiveTab(contents
);
597 EXPECT_TRUE(is_active_tab
);
599 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
600 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(1)
601 .WillOnce(testing::Return(false));
603 scoped_ptr
<IPC::Message
> message(new ChromeViewHostMsg_PasteAndOpenDropdown(
604 contents
->GetRoutingID(),
605 contents
->GetController().GetVisibleEntry()->GetPageID(), text
));
606 OnMessageReceived(*message
);
609 TEST_F(SearchIPCRouterTest
, SendSetPromoInformationMsg
) {
610 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
611 SetupMockDelegateAndPolicy();
612 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
613 EXPECT_CALL(*policy
, ShouldSendSetPromoInformation()).Times(1)
614 .WillOnce(testing::Return(true));
616 GetSearchIPCRouter().SetPromoInformation(true);
617 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID
));
620 TEST_F(SearchIPCRouterTest
, DoNotSendSetPromoInformationMsg
) {
621 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
622 SetupMockDelegateAndPolicy();
623 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
624 EXPECT_CALL(*policy
, ShouldSendSetPromoInformation()).Times(1)
625 .WillOnce(testing::Return(false));
627 GetSearchIPCRouter().SetPromoInformation(false);
628 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID
));
631 TEST_F(SearchIPCRouterTest
,
632 SendSetDisplayInstantResultsMsg_EnableInstantOnResultsPage
) {
633 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
635 "Group1 espv:42 query_extraction:1 prefetch_results_srp:1"));
636 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
638 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
639 // set to true if the underlying page is a results page and
640 // "prefetch_results_srp" flag is enabled via field trials.
641 VerifyDisplayInstantResultsMsg(true);
644 TEST_F(SearchIPCRouterTest
,
645 SendSetDisplayInstantResultsMsg_DisableInstantOnResultsPage
) {
646 // |prefetch_results_srp" flag is disabled via field trials.
647 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
648 "EmbeddedSearch", "Group1 espv:42 prefetch_results_srp:0"));
649 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
651 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
653 VerifyDisplayInstantResultsMsg(false);
656 TEST_F(SearchIPCRouterTest
,
657 SendSetDisplayInstantResultsMsg_DisableInstantOutsideResultsPage
) {
658 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
659 "EmbeddedSearch", "Group1 espv:42 prefetch_results_srp:1"));
660 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
662 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
663 // false if the underlying page is not a search results page.
664 VerifyDisplayInstantResultsMsg(false);
667 TEST_F(SearchIPCRouterTest
,
668 SendSetDisplayInstantResultsMsg_InstantSearchEnabled
) {
669 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
671 "Group1 espv:42 prefetch_results:1 use_cacheable_ntp:1"));
672 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
674 // If the "prefetch_results" flag is enabled via field trials, then
675 // ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is set to
676 // true irrespective of the underlying page.
677 VerifyDisplayInstantResultsMsg(true);
680 TEST_F(SearchIPCRouterTest
,
681 SendSetDisplayInstantResultsMsg_InstantSearchDisabled
) {
682 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
684 "Group1 espv:42 use_cacheable_ntp:1 prefetch_results:0"));
685 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
687 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
688 // false if the "prefetch_results" flag is disabled via field trials.
689 VerifyDisplayInstantResultsMsg(false);
692 TEST_F(SearchIPCRouterTest
, DoNotSendSetDisplayInstantResultsMsg
) {
693 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
694 SetupMockDelegateAndPolicy();
695 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
696 EXPECT_CALL(*policy
, ShouldSendSetDisplayInstantResults()).Times(1)
697 .WillOnce(testing::Return(false));
699 process()->sink().ClearMessages();
700 GetSearchIPCRouter().SetDisplayInstantResults();
701 EXPECT_FALSE(MessageWasSent(
702 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID
));
705 TEST_F(SearchIPCRouterTest
, SendSetSuggestionToPrefetch
) {
706 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
707 SetupMockDelegateAndPolicy();
708 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
709 EXPECT_CALL(*policy
, ShouldSendSetSuggestionToPrefetch()).Times(1)
710 .WillOnce(testing::Return(true));
712 process()->sink().ClearMessages();
713 content::WebContents
* contents
= web_contents();
714 GetSearchTabHelper(contents
)->SetSuggestionToPrefetch(InstantSuggestion());
715 EXPECT_TRUE(MessageWasSent(
716 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID
));
719 TEST_F(SearchIPCRouterTest
, DoNotSendSetSuggestionToPrefetch
) {
720 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
721 SetupMockDelegateAndPolicy();
722 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
723 EXPECT_CALL(*policy
, ShouldSendSetSuggestionToPrefetch()).Times(1)
724 .WillOnce(testing::Return(false));
726 process()->sink().ClearMessages();
727 content::WebContents
* contents
= web_contents();
728 GetSearchTabHelper(contents
)->SetSuggestionToPrefetch(InstantSuggestion());
729 EXPECT_FALSE(MessageWasSent(
730 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID
));
733 TEST_F(SearchIPCRouterTest
, SendSetOmniboxStartMargin
) {
734 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
735 SetupMockDelegateAndPolicy();
736 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
737 EXPECT_CALL(*policy
, ShouldSendSetOmniboxStartMargin()).Times(1)
738 .WillOnce(testing::Return(true));
740 process()->sink().ClearMessages();
741 GetSearchIPCRouter().SetOmniboxStartMargin(92);
742 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID
));
745 TEST_F(SearchIPCRouterTest
, DoNotSendSetOmniboxStartMargin
) {
746 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
747 SetupMockDelegateAndPolicy();
748 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
749 EXPECT_CALL(*policy
, ShouldSendSetOmniboxStartMargin()).Times(1)
750 .WillOnce(testing::Return(false));
752 process()->sink().ClearMessages();
753 GetSearchIPCRouter().SetOmniboxStartMargin(92);
754 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID
));
757 TEST_F(SearchIPCRouterTest
, SendMostVisitedItemsMsg
) {
758 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
759 SetupMockDelegateAndPolicy();
760 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
761 EXPECT_CALL(*policy
, ShouldSendMostVisitedItems()).Times(1)
762 .WillOnce(testing::Return(true));
764 process()->sink().ClearMessages();
765 GetSearchIPCRouter().SendMostVisitedItems(
766 std::vector
<InstantMostVisitedItem
>());
767 EXPECT_TRUE(MessageWasSent(
768 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID
));
771 TEST_F(SearchIPCRouterTest
, DoNotSendMostVisitedItemsMsg
) {
772 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
773 SetupMockDelegateAndPolicy();
774 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
775 EXPECT_CALL(*policy
, ShouldSendMostVisitedItems()).Times(1)
776 .WillOnce(testing::Return(false));
778 process()->sink().ClearMessages();
779 GetSearchIPCRouter().SendMostVisitedItems(
780 std::vector
<InstantMostVisitedItem
>());
781 EXPECT_FALSE(MessageWasSent(
782 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID
));
785 TEST_F(SearchIPCRouterTest
, SendThemeBackgroundInfoMsg
) {
786 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
787 SetupMockDelegateAndPolicy();
788 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
789 EXPECT_CALL(*policy
, ShouldSendThemeBackgroundInfo()).Times(1)
790 .WillOnce(testing::Return(true));
792 process()->sink().ClearMessages();
793 GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
794 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID
));
797 TEST_F(SearchIPCRouterTest
, DoNotSendThemeBackgroundInfoMsg
) {
798 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
799 SetupMockDelegateAndPolicy();
800 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
801 EXPECT_CALL(*policy
, ShouldSendThemeBackgroundInfo()).Times(1)
802 .WillOnce(testing::Return(false));
804 process()->sink().ClearMessages();
805 GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
806 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID
));
809 TEST_F(SearchIPCRouterTest
, SendSubmitMsg
) {
810 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
811 SetupMockDelegateAndPolicy();
812 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
813 EXPECT_CALL(*policy
, ShouldSubmitQuery()).Times(1)
814 .WillOnce(testing::Return(true));
816 process()->sink().ClearMessages();
817 GetSearchIPCRouter().Submit(base::string16());
818 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID
));
821 TEST_F(SearchIPCRouterTest
, DoNotSendSubmitMsg
) {
822 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
823 SetupMockDelegateAndPolicy();
824 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
825 EXPECT_CALL(*policy
, ShouldSubmitQuery()).Times(1)
826 .WillOnce(testing::Return(false));
828 process()->sink().ClearMessages();
829 GetSearchIPCRouter().Submit(base::string16());
830 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID
));
833 TEST_F(SearchIPCRouterTest
, SendToggleVoiceSearch
) {
834 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
835 SetupMockDelegateAndPolicy();
836 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
837 EXPECT_CALL(*policy
, ShouldSendToggleVoiceSearch()).Times(1)
838 .WillOnce(testing::Return(true));
840 process()->sink().ClearMessages();
841 GetSearchIPCRouter().ToggleVoiceSearch();
842 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID
));
845 TEST_F(SearchIPCRouterTest
, DoNotSendToggleVoiceSearch
) {
846 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
847 SetupMockDelegateAndPolicy();
848 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
849 EXPECT_CALL(*policy
, ShouldSendToggleVoiceSearch()).Times(1)
850 .WillOnce(testing::Return(false));
852 process()->sink().ClearMessages();
853 GetSearchIPCRouter().ToggleVoiceSearch();
854 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID
));