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_factory.h"
18 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h"
19 #include "chrome/browser/ui/search/search_tab_helper.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/instant_types.h"
23 #include "chrome/common/ntp_logging_events.h"
24 #include "chrome/common/render_messages.h"
25 #include "chrome/common/url_constants.h"
26 #include "chrome/test/base/browser_with_test_window_test.h"
27 #include "chrome/test/base/ui_test_utils.h"
28 #include "components/omnibox/common/omnibox_focus_state.h"
29 #include "components/search_engines/template_url_service.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_message_start.h"
36 #include "ipc/ipc_test_sink.h"
37 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gtest/include/gtest/gtest.h"
39 #include "ui/base/window_open_disposition.h"
44 class MockSearchIPCRouterDelegate
: public SearchIPCRouter::Delegate
{
46 virtual ~MockSearchIPCRouterDelegate() {}
48 MOCK_METHOD1(OnInstantSupportDetermined
, void(bool supports_instant
));
49 MOCK_METHOD1(OnSetVoiceSearchSupport
, void(bool supports_voice_search
));
50 MOCK_METHOD1(FocusOmnibox
, void(OmniboxFocusState state
));
51 MOCK_METHOD3(NavigateToURL
, void(const GURL
&, WindowOpenDisposition
, bool));
52 MOCK_METHOD1(OnDeleteMostVisitedItem
, void(const GURL
& url
));
53 MOCK_METHOD1(OnUndoMostVisitedDeletion
, void(const GURL
& url
));
54 MOCK_METHOD0(OnUndoAllMostVisitedDeletions
, void());
55 MOCK_METHOD2(OnLogEvent
, void(NTPLoggingEventType event
,
56 base::TimeDelta time
));
57 MOCK_METHOD2(OnLogMostVisitedImpression
,
58 void(int position
, const base::string16
& provider
));
59 MOCK_METHOD2(OnLogMostVisitedNavigation
,
60 void(int position
, const base::string16
& provider
));
61 MOCK_METHOD1(PasteIntoOmnibox
, void(const base::string16
&));
62 MOCK_METHOD1(OnChromeIdentityCheck
, void(const base::string16
& identity
));
63 MOCK_METHOD0(OnHistorySyncCheck
, void());
66 class MockSearchIPCRouterPolicy
: public SearchIPCRouter::Policy
{
68 virtual ~MockSearchIPCRouterPolicy() {}
70 MOCK_METHOD0(ShouldProcessSetVoiceSearchSupport
, bool());
71 MOCK_METHOD1(ShouldProcessFocusOmnibox
, bool(bool));
72 MOCK_METHOD1(ShouldProcessNavigateToURL
, bool(bool));
73 MOCK_METHOD0(ShouldProcessDeleteMostVisitedItem
, bool());
74 MOCK_METHOD0(ShouldProcessUndoMostVisitedDeletion
, bool());
75 MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions
, bool());
76 MOCK_METHOD0(ShouldProcessLogEvent
, bool());
77 MOCK_METHOD1(ShouldProcessPasteIntoOmnibox
, bool(bool));
78 MOCK_METHOD0(ShouldProcessChromeIdentityCheck
, bool());
79 MOCK_METHOD0(ShouldProcessHistorySyncCheck
, bool());
80 MOCK_METHOD0(ShouldSendSetPromoInformation
, bool());
81 MOCK_METHOD0(ShouldSendSetDisplayInstantResults
, bool());
82 MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch
, bool());
83 MOCK_METHOD0(ShouldSendSetOmniboxStartMargin
, bool());
84 MOCK_METHOD1(ShouldSendSetInputInProgress
, bool(bool));
85 MOCK_METHOD0(ShouldSendOmniboxFocusChanged
, bool());
86 MOCK_METHOD0(ShouldSendMostVisitedItems
, bool());
87 MOCK_METHOD0(ShouldSendThemeBackgroundInfo
, bool());
88 MOCK_METHOD0(ShouldSendToggleVoiceSearch
, bool());
89 MOCK_METHOD0(ShouldSubmitQuery
, bool());
94 class SearchIPCRouterTest
: public BrowserWithTestWindowTest
{
96 SearchIPCRouterTest() : field_trial_list_(NULL
) {}
98 void SetUp() override
{
99 BrowserWithTestWindowTest::SetUp();
100 AddTab(browser(), GURL("chrome://blank"));
101 SearchTabHelper::CreateForWebContents(web_contents());
103 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
105 &TemplateURLServiceFactory::BuildInstanceFor
);
106 TemplateURLService
* template_url_service
=
107 TemplateURLServiceFactory::GetForProfile(profile());
108 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
110 TemplateURLData data
;
111 data
.SetShortName(base::ASCIIToUTF16("foo.com"));
112 data
.SetURL("http://foo.com/url?bar={searchTerms}");
113 data
.instant_url
= "http://foo.com/instant?"
114 "{google:omniboxStartMarginParameter}foo=foo#foo=foo&espv";
115 data
.new_tab_url
= "https://foo.com/newtab?espv";
116 data
.alternate_urls
.push_back("http://foo.com/alt#quux={searchTerms}");
117 data
.search_terms_replacement_key
= "espv";
119 TemplateURL
* template_url
= new TemplateURL(data
);
120 // Takes ownership of |template_url|.
121 template_url_service
->Add(template_url
);
122 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
123 process()->sink().ClearMessages();
126 content::WebContents
* web_contents() {
127 return browser()->tab_strip_model()->GetActiveWebContents();
130 content::MockRenderProcessHost
* process() {
131 return static_cast<content::MockRenderProcessHost
*>(
132 web_contents()->GetRenderViewHost()->GetProcess());
135 SearchTabHelper
* GetSearchTabHelper(
136 content::WebContents
* web_contents
) {
137 EXPECT_NE(static_cast<content::WebContents
*>(NULL
), web_contents
);
138 return SearchTabHelper::FromWebContents(web_contents
);
141 void SetupMockDelegateAndPolicy() {
142 content::WebContents
* contents
= web_contents();
143 ASSERT_NE(static_cast<content::WebContents
*>(NULL
), contents
);
144 SearchTabHelper
* search_tab_helper
= GetSearchTabHelper(contents
);
145 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
146 search_tab_helper
->ipc_router().set_delegate_for_testing(mock_delegate());
147 search_tab_helper
->ipc_router().set_policy_for_testing(
148 make_scoped_ptr(new MockSearchIPCRouterPolicy
));
151 bool MessageWasSent(uint32 id
) {
152 return process()->sink().GetFirstMessageMatching(id
) != NULL
;
155 void VerifyDisplayInstantResultsMsg(bool expected_param_value
) {
156 SetupMockDelegateAndPolicy();
157 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
158 EXPECT_CALL(*policy
, ShouldSendSetDisplayInstantResults()).Times(1)
159 .WillOnce(testing::Return(true));
161 GetSearchIPCRouter().SetDisplayInstantResults();
162 const IPC::Message
* message
= process()->sink().GetFirstMessageMatching(
163 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID
);
164 EXPECT_NE(static_cast<const IPC::Message
*>(NULL
), message
);
165 base::Tuple
<bool> display_instant_results_param
;
166 ChromeViewMsg_SearchBoxSetDisplayInstantResults::Read(
167 message
, &display_instant_results_param
);
168 EXPECT_EQ(expected_param_value
,
169 base::get
<0>(display_instant_results_param
));
172 MockSearchIPCRouterDelegate
* mock_delegate() { return &delegate_
; }
174 MockSearchIPCRouterPolicy
* GetSearchIPCRouterPolicy() {
175 content::WebContents
* contents
= web_contents();
176 EXPECT_NE(static_cast<content::WebContents
*>(NULL
), contents
);
177 SearchTabHelper
* search_tab_helper
= GetSearchTabHelper(contents
);
178 EXPECT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
179 return static_cast<MockSearchIPCRouterPolicy
*>(
180 search_tab_helper
->ipc_router().policy_for_testing());
183 SearchIPCRouter
& GetSearchIPCRouter() {
184 return GetSearchTabHelper(web_contents())->ipc_router();
187 int GetSearchIPCRouterSeqNo() {
188 return GetSearchIPCRouter().page_seq_no_for_testing();
191 void OnMessageReceived(const IPC::Message
& message
) {
192 bool should_handle_message
=
193 search::IsRenderedInInstantProcess(web_contents(), profile());
194 bool handled
= GetSearchIPCRouter().OnMessageReceived(message
);
195 ASSERT_EQ(should_handle_message
, handled
);
198 bool OnSpuriousMessageReceived(const IPC::Message
& message
) {
199 return GetSearchIPCRouter().OnMessageReceived(message
);
202 bool IsActiveTab(content::WebContents
* contents
) {
203 return GetSearchTabHelper(contents
)->ipc_router().is_active_tab_
;
207 MockSearchIPCRouterDelegate delegate_
;
208 base::FieldTrialList field_trial_list_
;
211 TEST_F(SearchIPCRouterTest
, IgnoreMessagesFromNonInstantRenderers
) {
212 NavigateAndCommitActiveTab(GURL("file://foo/bar"));
213 SetupMockDelegateAndPolicy();
214 GURL
destination_url("www.foo.com");
215 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
217 content::WebContents
* contents
= web_contents();
218 bool is_active_tab
= IsActiveTab(contents
);
219 EXPECT_TRUE(is_active_tab
);
221 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
222 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(0);
224 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
225 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url
,
229 TEST_F(SearchIPCRouterTest
, ProcessVoiceSearchSupportMsg
) {
230 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
231 SetupMockDelegateAndPolicy();
232 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
233 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(1);
234 EXPECT_CALL(*(policy
), ShouldProcessSetVoiceSearchSupport()).Times(1)
235 .WillOnce(testing::Return(true));
237 content::WebContents
* contents
= web_contents();
238 OnMessageReceived(ChromeViewHostMsg_SetVoiceSearchSupported(
239 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), true));
242 TEST_F(SearchIPCRouterTest
, IgnoreVoiceSearchSupportMsg
) {
243 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
244 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(0);
245 SetupMockDelegateAndPolicy();
246 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
247 EXPECT_CALL(*policy
, ShouldProcessSetVoiceSearchSupport()).Times(1)
248 .WillOnce(testing::Return(false));
250 content::WebContents
* contents
= web_contents();
251 OnMessageReceived(ChromeViewHostMsg_SetVoiceSearchSupported(
252 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), true));
255 TEST_F(SearchIPCRouterTest
, ProcessFocusOmniboxMsg
) {
256 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
257 SetupMockDelegateAndPolicy();
258 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
259 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(1);
261 content::WebContents
* contents
= web_contents();
262 bool is_active_tab
= IsActiveTab(contents
);
263 EXPECT_TRUE(is_active_tab
);
264 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(1)
265 .WillOnce(testing::Return(true));
267 OnMessageReceived(ChromeViewHostMsg_FocusOmnibox(
268 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(),
269 OMNIBOX_FOCUS_VISIBLE
));
272 TEST_F(SearchIPCRouterTest
, IgnoreFocusOmniboxMsg
) {
273 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
274 SetupMockDelegateAndPolicy();
275 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
276 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(0);
278 content::WebContents
* contents
= web_contents();
279 bool is_active_tab
= IsActiveTab(contents
);
280 EXPECT_TRUE(is_active_tab
);
281 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(1)
282 .WillOnce(testing::Return(false));
284 OnMessageReceived(ChromeViewHostMsg_FocusOmnibox(
285 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(),
286 OMNIBOX_FOCUS_VISIBLE
));
289 TEST_F(SearchIPCRouterTest
, HandleTabChangedEvents
) {
290 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
291 content::WebContents
* contents
= web_contents();
292 EXPECT_EQ(0, browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
293 EXPECT_TRUE(IsActiveTab(contents
));
295 // Add a new tab to deactivate the current tab.
296 AddTab(browser(), GURL(url::kAboutBlankURL
));
297 EXPECT_EQ(2, browser()->tab_strip_model()->count());
298 EXPECT_EQ(1, browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
299 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
300 EXPECT_FALSE(IsActiveTab(contents
));
302 // Activate the first tab.
303 browser()->tab_strip_model()->ActivateTabAt(1, false);
304 EXPECT_EQ(browser()->tab_strip_model()->active_index(),
305 browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
306 EXPECT_TRUE(IsActiveTab(contents
));
309 TEST_F(SearchIPCRouterTest
, ProcessNavigateToURLMsg
) {
310 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
311 SetupMockDelegateAndPolicy();
312 GURL
destination_url("www.foo.com");
313 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
315 content::WebContents
* contents
= web_contents();
316 bool is_active_tab
= IsActiveTab(contents
);
317 EXPECT_TRUE(is_active_tab
);
319 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
320 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(1)
321 .WillOnce(testing::Return(true));
323 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
324 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url
,
328 TEST_F(SearchIPCRouterTest
, IgnoreNavigateToURLMsg
) {
329 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
330 SetupMockDelegateAndPolicy();
331 GURL
destination_url("www.foo.com");
332 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
334 content::WebContents
* contents
= web_contents();
335 bool is_active_tab
= IsActiveTab(contents
);
336 EXPECT_TRUE(is_active_tab
);
338 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
339 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(1)
340 .WillOnce(testing::Return(false));
342 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
343 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url
,
347 TEST_F(SearchIPCRouterTest
, ProcessLogEventMsg
) {
348 base::TimeDelta delta
= base::TimeDelta::FromMilliseconds(123);
349 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
350 SetupMockDelegateAndPolicy();
351 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
352 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
, delta
)).Times(1);
353 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
354 .WillOnce(testing::Return(true));
356 content::WebContents
* contents
= web_contents();
357 OnMessageReceived(ChromeViewHostMsg_LogEvent(
358 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(),
359 NTP_MOUSEOVER
, delta
));
362 TEST_F(SearchIPCRouterTest
, IgnoreLogEventMsg
) {
363 base::TimeDelta delta
= base::TimeDelta::FromMilliseconds(123);
364 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
365 SetupMockDelegateAndPolicy();
366 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
367 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
, delta
)).Times(0);
368 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
369 .WillOnce(testing::Return(false));
371 content::WebContents
* contents
= web_contents();
372 OnMessageReceived(ChromeViewHostMsg_LogEvent(
373 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(),
374 NTP_MOUSEOVER
, delta
));
377 TEST_F(SearchIPCRouterTest
, ProcessLogMostVisitedImpressionMsg
) {
378 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
379 SetupMockDelegateAndPolicy();
380 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
381 EXPECT_CALL(*mock_delegate(),
382 OnLogMostVisitedImpression(3, base::ASCIIToUTF16("Server"))).Times(1);
383 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
384 .WillOnce(testing::Return(true));
386 content::WebContents
* contents
= web_contents();
387 OnMessageReceived(ChromeViewHostMsg_LogMostVisitedImpression(
388 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), 3,
389 base::ASCIIToUTF16("Server")));
392 TEST_F(SearchIPCRouterTest
, ProcessLogMostVisitedNavigationMsg
) {
393 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
394 SetupMockDelegateAndPolicy();
395 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
396 EXPECT_CALL(*mock_delegate(),
397 OnLogMostVisitedNavigation(3, base::ASCIIToUTF16("Server"))).Times(1);
398 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
399 .WillOnce(testing::Return(true));
401 content::WebContents
* contents
= web_contents();
402 OnMessageReceived(ChromeViewHostMsg_LogMostVisitedNavigation(
403 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), 3,
404 base::ASCIIToUTF16("Server")));
407 TEST_F(SearchIPCRouterTest
, ProcessChromeIdentityCheckMsg
) {
408 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
409 SetupMockDelegateAndPolicy();
410 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
411 const base::string16 test_identity
= base::ASCIIToUTF16("foo@bar.com");
412 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity
)).Times(1);
413 EXPECT_CALL(*policy
, ShouldProcessChromeIdentityCheck()).Times(1)
414 .WillOnce(testing::Return(true));
416 content::WebContents
* contents
= web_contents();
417 OnMessageReceived(ChromeViewHostMsg_ChromeIdentityCheck(
418 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), test_identity
));
421 TEST_F(SearchIPCRouterTest
, IgnoreChromeIdentityCheckMsg
) {
422 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
423 SetupMockDelegateAndPolicy();
424 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
426 const base::string16 test_identity
= base::ASCIIToUTF16("foo@bar.com");
427 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity
)).Times(0);
428 EXPECT_CALL(*policy
, ShouldProcessChromeIdentityCheck()).Times(1)
429 .WillOnce(testing::Return(false));
431 content::WebContents
* contents
= web_contents();
432 OnMessageReceived(ChromeViewHostMsg_ChromeIdentityCheck(
433 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), test_identity
));
436 TEST_F(SearchIPCRouterTest
, ProcessHistorySyncCheckMsg
) {
437 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
438 SetupMockDelegateAndPolicy();
439 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
440 EXPECT_CALL(*mock_delegate(), OnHistorySyncCheck()).Times(1);
441 EXPECT_CALL(*policy
, ShouldProcessHistorySyncCheck()).Times(1)
442 .WillOnce(testing::Return(true));
444 content::WebContents
* contents
= web_contents();
445 OnMessageReceived(ChromeViewHostMsg_HistorySyncCheck(
446 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
449 TEST_F(SearchIPCRouterTest
, IgnoreHistorySyncCheckMsg
) {
450 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
451 SetupMockDelegateAndPolicy();
452 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
454 EXPECT_CALL(*mock_delegate(), OnHistorySyncCheck()).Times(0);
455 EXPECT_CALL(*policy
, ShouldProcessHistorySyncCheck()).Times(1)
456 .WillOnce(testing::Return(false));
458 content::WebContents
* contents
= web_contents();
459 OnMessageReceived(ChromeViewHostMsg_HistorySyncCheck(
460 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
463 TEST_F(SearchIPCRouterTest
, ProcessDeleteMostVisitedItemMsg
) {
464 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
465 SetupMockDelegateAndPolicy();
466 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
467 GURL
item_url("www.foo.com");
468 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(1);
469 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(1)
470 .WillOnce(testing::Return(true));
472 content::WebContents
* contents
= web_contents();
473 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
474 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
477 TEST_F(SearchIPCRouterTest
, IgnoreDeleteMostVisitedItemMsg
) {
478 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
479 SetupMockDelegateAndPolicy();
480 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
481 GURL
item_url("www.foo.com");
482 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(0);
483 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(1)
484 .WillOnce(testing::Return(false));
486 content::WebContents
* contents
= web_contents();
487 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
488 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
491 TEST_F(SearchIPCRouterTest
, ProcessUndoMostVisitedDeletionMsg
) {
492 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
493 SetupMockDelegateAndPolicy();
494 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
495 GURL
item_url("www.foo.com");
496 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(1);
497 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(1)
498 .WillOnce(testing::Return(true));
500 content::WebContents
* contents
= web_contents();
501 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
502 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
505 TEST_F(SearchIPCRouterTest
, IgnoreUndoMostVisitedDeletionMsg
) {
506 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
507 SetupMockDelegateAndPolicy();
508 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
509 GURL
item_url("www.foo.com");
510 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(0);
511 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(1)
512 .WillOnce(testing::Return(false));
514 content::WebContents
* contents
= web_contents();
515 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
516 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
519 TEST_F(SearchIPCRouterTest
, ProcessUndoAllMostVisitedDeletionsMsg
) {
520 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
521 SetupMockDelegateAndPolicy();
522 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
523 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(1);
524 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
525 .WillOnce(testing::Return(true));
527 content::WebContents
* contents
= web_contents();
528 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
529 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
532 TEST_F(SearchIPCRouterTest
, IgnoreUndoAllMostVisitedDeletionsMsg
) {
533 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
534 SetupMockDelegateAndPolicy();
535 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
536 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
537 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
538 .WillOnce(testing::Return(false));
540 content::WebContents
* contents
= web_contents();
541 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
542 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
545 TEST_F(SearchIPCRouterTest
, IgnoreMessageIfThePageIsNotActive
) {
546 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
547 SetupMockDelegateAndPolicy();
548 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
549 int page_seq_no
= GetSearchIPCRouterSeqNo();
551 content::WebContents
* contents
= web_contents();
552 bool is_active_tab
= IsActiveTab(contents
);
553 GURL
item_url("www.foo.com");
554 EXPECT_CALL(*mock_delegate(), NavigateToURL(item_url
, CURRENT_TAB
,
556 // At this point, in a real test, the navigation would cause the
557 // SearchIPCRouter's page sequence to advance. In this test it doesn't, so
558 // we'll decrement the sequence number on this side to simulate it.
560 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(0);
561 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
562 contents
->GetRoutingID(), page_seq_no
, item_url
, CURRENT_TAB
, true));
564 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(0);
565 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(0);
566 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
567 contents
->GetRoutingID(), page_seq_no
, item_url
));
569 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(0);
570 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(0);
571 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
572 contents
->GetRoutingID(), page_seq_no
, item_url
));
574 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
575 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(0);
576 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
577 contents
->GetRoutingID(), page_seq_no
));
579 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(0);
580 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(0);
581 OnMessageReceived(ChromeViewHostMsg_FocusOmnibox(
582 contents
->GetRoutingID(), page_seq_no
, OMNIBOX_FOCUS_VISIBLE
));
584 base::TimeDelta delta
= base::TimeDelta::FromMilliseconds(123);
585 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
, delta
)).Times(0);
586 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(0);
587 OnMessageReceived(ChromeViewHostMsg_LogEvent(contents
->GetRoutingID(),
589 NTP_MOUSEOVER
, delta
));
592 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(0);
593 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(0);
594 OnMessageReceived(ChromeViewHostMsg_PasteAndOpenDropdown(
595 contents
->GetRoutingID(), page_seq_no
, text
));
598 TEST_F(SearchIPCRouterTest
, ProcessPasteAndOpenDropdownMsg
) {
599 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
600 SetupMockDelegateAndPolicy();
601 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
603 content::WebContents
* contents
= web_contents();
604 bool is_active_tab
= IsActiveTab(contents
);
605 EXPECT_TRUE(is_active_tab
);
608 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(1);
609 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(1)
610 .WillOnce(testing::Return(true));
611 OnMessageReceived(ChromeViewHostMsg_PasteAndOpenDropdown(
612 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), text
));
615 TEST_F(SearchIPCRouterTest
, IgnorePasteAndOpenDropdownMsg
) {
616 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
617 SetupMockDelegateAndPolicy();
619 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(0);
621 content::WebContents
* contents
= web_contents();
622 bool is_active_tab
= IsActiveTab(contents
);
623 EXPECT_TRUE(is_active_tab
);
625 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
626 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(1)
627 .WillOnce(testing::Return(false));
629 OnMessageReceived(ChromeViewHostMsg_PasteAndOpenDropdown(
630 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), text
));
633 TEST_F(SearchIPCRouterTest
, SendSetPromoInformationMsg
) {
634 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
635 SetupMockDelegateAndPolicy();
636 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
637 EXPECT_CALL(*policy
, ShouldSendSetPromoInformation()).Times(1)
638 .WillOnce(testing::Return(true));
640 GetSearchIPCRouter().SetPromoInformation(true);
641 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID
));
644 TEST_F(SearchIPCRouterTest
, DoNotSendSetPromoInformationMsg
) {
645 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
646 SetupMockDelegateAndPolicy();
647 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
648 EXPECT_CALL(*policy
, ShouldSendSetPromoInformation()).Times(1)
649 .WillOnce(testing::Return(false));
651 GetSearchIPCRouter().SetPromoInformation(false);
652 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID
));
655 TEST_F(SearchIPCRouterTest
,
656 SendSetDisplayInstantResultsMsg_EnableInstantOnResultsPage
) {
657 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
659 "Group1 espv:42 query_extraction:1 prefetch_results_srp:1"));
660 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
662 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
663 // set to true if the underlying page is a results page and
664 // "prefetch_results_srp" flag is enabled via field trials.
665 VerifyDisplayInstantResultsMsg(true);
668 TEST_F(SearchIPCRouterTest
,
669 SendSetDisplayInstantResultsMsg_DisableInstantOnResultsPage
) {
670 // |prefetch_results_srp" flag is disabled via field trials.
671 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
673 "Group1 espv:42 query_extraction:1 prefetch_results_srp:0"));
674 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
676 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
678 VerifyDisplayInstantResultsMsg(false);
681 TEST_F(SearchIPCRouterTest
,
682 SendSetDisplayInstantResultsMsg_EnableInstantOutsideSearchResultsPage
) {
683 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
684 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
685 // true if the underlying page is not a search results page.
686 VerifyDisplayInstantResultsMsg(true);
689 TEST_F(SearchIPCRouterTest
, DoNotSendSetDisplayInstantResultsMsg
) {
690 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
691 SetupMockDelegateAndPolicy();
692 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
693 EXPECT_CALL(*policy
, ShouldSendSetDisplayInstantResults()).Times(1)
694 .WillOnce(testing::Return(false));
696 process()->sink().ClearMessages();
697 GetSearchIPCRouter().SetDisplayInstantResults();
698 EXPECT_FALSE(MessageWasSent(
699 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID
));
702 TEST_F(SearchIPCRouterTest
, SendSetSuggestionToPrefetch
) {
703 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
704 SetupMockDelegateAndPolicy();
705 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
706 EXPECT_CALL(*policy
, ShouldSendSetSuggestionToPrefetch()).Times(1)
707 .WillOnce(testing::Return(true));
709 process()->sink().ClearMessages();
710 content::WebContents
* contents
= web_contents();
711 GetSearchTabHelper(contents
)->SetSuggestionToPrefetch(InstantSuggestion());
712 EXPECT_TRUE(MessageWasSent(
713 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID
));
716 TEST_F(SearchIPCRouterTest
, DoNotSendSetSuggestionToPrefetch
) {
717 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
718 SetupMockDelegateAndPolicy();
719 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
720 EXPECT_CALL(*policy
, ShouldSendSetSuggestionToPrefetch()).Times(1)
721 .WillOnce(testing::Return(false));
723 process()->sink().ClearMessages();
724 content::WebContents
* contents
= web_contents();
725 GetSearchTabHelper(contents
)->SetSuggestionToPrefetch(InstantSuggestion());
726 EXPECT_FALSE(MessageWasSent(
727 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID
));
730 TEST_F(SearchIPCRouterTest
, SendSetOmniboxStartMargin
) {
731 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
732 SetupMockDelegateAndPolicy();
733 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
734 EXPECT_CALL(*policy
, ShouldSendSetOmniboxStartMargin()).Times(1)
735 .WillOnce(testing::Return(true));
737 process()->sink().ClearMessages();
738 GetSearchIPCRouter().SetOmniboxStartMargin(92);
739 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID
));
742 TEST_F(SearchIPCRouterTest
, DoNotSendSetOmniboxStartMargin
) {
743 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
744 SetupMockDelegateAndPolicy();
745 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
746 EXPECT_CALL(*policy
, ShouldSendSetOmniboxStartMargin()).Times(1)
747 .WillOnce(testing::Return(false));
749 process()->sink().ClearMessages();
750 GetSearchIPCRouter().SetOmniboxStartMargin(92);
751 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID
));
754 TEST_F(SearchIPCRouterTest
, SendOmniboxFocusChange
) {
755 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
756 SetupMockDelegateAndPolicy();
757 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
758 EXPECT_CALL(*policy
, ShouldSendOmniboxFocusChanged()).Times(1)
759 .WillOnce(testing::Return(true));
761 process()->sink().ClearMessages();
762 GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE
,
763 OMNIBOX_FOCUS_CHANGE_EXPLICIT
);
764 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID
));
767 TEST_F(SearchIPCRouterTest
, DoNotSendOmniboxFocusChange
) {
768 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
769 SetupMockDelegateAndPolicy();
770 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
771 EXPECT_CALL(*policy
, ShouldSendOmniboxFocusChanged()).Times(1)
772 .WillOnce(testing::Return(false));
774 process()->sink().ClearMessages();
775 GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE
,
776 OMNIBOX_FOCUS_CHANGE_EXPLICIT
);
777 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID
));
780 TEST_F(SearchIPCRouterTest
, SendSetInputInProgress
) {
781 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
782 SetupMockDelegateAndPolicy();
783 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
784 EXPECT_CALL(*policy
, ShouldSendSetInputInProgress(true)).Times(1)
785 .WillOnce(testing::Return(true));
787 process()->sink().ClearMessages();
788 GetSearchIPCRouter().SetInputInProgress(true);
789 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID
));
792 TEST_F(SearchIPCRouterTest
, DoNotSendSetInputInProgress
) {
793 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
794 SetupMockDelegateAndPolicy();
795 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
796 EXPECT_CALL(*policy
, ShouldSendSetInputInProgress(true)).Times(1)
797 .WillOnce(testing::Return(false));
799 process()->sink().ClearMessages();
800 GetSearchIPCRouter().SetInputInProgress(true);
801 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID
));
804 TEST_F(SearchIPCRouterTest
, SendMostVisitedItemsMsg
) {
805 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
806 SetupMockDelegateAndPolicy();
807 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
808 EXPECT_CALL(*policy
, ShouldSendMostVisitedItems()).Times(1)
809 .WillOnce(testing::Return(true));
811 process()->sink().ClearMessages();
812 GetSearchIPCRouter().SendMostVisitedItems(
813 std::vector
<InstantMostVisitedItem
>());
814 EXPECT_TRUE(MessageWasSent(
815 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID
));
818 TEST_F(SearchIPCRouterTest
, DoNotSendMostVisitedItemsMsg
) {
819 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
820 SetupMockDelegateAndPolicy();
821 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
822 EXPECT_CALL(*policy
, ShouldSendMostVisitedItems()).Times(1)
823 .WillOnce(testing::Return(false));
825 process()->sink().ClearMessages();
826 GetSearchIPCRouter().SendMostVisitedItems(
827 std::vector
<InstantMostVisitedItem
>());
828 EXPECT_FALSE(MessageWasSent(
829 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID
));
832 TEST_F(SearchIPCRouterTest
, SendThemeBackgroundInfoMsg
) {
833 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
834 SetupMockDelegateAndPolicy();
835 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
836 EXPECT_CALL(*policy
, ShouldSendThemeBackgroundInfo()).Times(1)
837 .WillOnce(testing::Return(true));
839 process()->sink().ClearMessages();
840 GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
841 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID
));
844 TEST_F(SearchIPCRouterTest
, DoNotSendThemeBackgroundInfoMsg
) {
845 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
846 SetupMockDelegateAndPolicy();
847 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
848 EXPECT_CALL(*policy
, ShouldSendThemeBackgroundInfo()).Times(1)
849 .WillOnce(testing::Return(false));
851 process()->sink().ClearMessages();
852 GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
853 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID
));
856 TEST_F(SearchIPCRouterTest
, SendSubmitMsg
) {
857 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
858 SetupMockDelegateAndPolicy();
859 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
860 EXPECT_CALL(*policy
, ShouldSubmitQuery()).Times(1)
861 .WillOnce(testing::Return(true));
863 process()->sink().ClearMessages();
864 GetSearchIPCRouter().Submit(base::string16(), EmbeddedSearchRequestParams());
865 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID
));
868 TEST_F(SearchIPCRouterTest
, DoNotSendSubmitMsg
) {
869 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
870 SetupMockDelegateAndPolicy();
871 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
872 EXPECT_CALL(*policy
, ShouldSubmitQuery()).Times(1)
873 .WillOnce(testing::Return(false));
875 process()->sink().ClearMessages();
876 GetSearchIPCRouter().Submit(base::string16(), EmbeddedSearchRequestParams());
877 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID
));
880 TEST_F(SearchIPCRouterTest
, SendToggleVoiceSearch
) {
881 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
882 SetupMockDelegateAndPolicy();
883 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
884 EXPECT_CALL(*policy
, ShouldSendToggleVoiceSearch()).Times(1)
885 .WillOnce(testing::Return(true));
887 process()->sink().ClearMessages();
888 GetSearchIPCRouter().ToggleVoiceSearch();
889 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID
));
892 TEST_F(SearchIPCRouterTest
, DoNotSendToggleVoiceSearch
) {
893 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
894 SetupMockDelegateAndPolicy();
895 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
896 EXPECT_CALL(*policy
, ShouldSendToggleVoiceSearch()).Times(1)
897 .WillOnce(testing::Return(false));
899 process()->sink().ClearMessages();
900 GetSearchIPCRouter().ToggleVoiceSearch();
901 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID
));
904 TEST_F(SearchIPCRouterTest
, SpuriousMessageTypesIgnored
) {
905 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
906 SetupMockDelegateAndPolicy();
907 const int routing_id
= web_contents()->GetRoutingID();
909 // Construct a series of synthetic messages for each valid IPC message type,
910 // ensuring the router ignores them all.
911 for (int i
= 0; i
< LastIPCMsgStart
; ++i
) {
912 const int message_id
= i
<< 16;
913 ASSERT_EQ(IPC_MESSAGE_ID_CLASS(message_id
), i
);
914 IPC::Message
msg(routing_id
, message_id
, IPC::Message::PRIORITY_LOW
);
915 EXPECT_FALSE(OnSpuriousMessageReceived(msg
)) << i
;