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/omnibox_focus_state.h"
25 #include "chrome/common/render_messages.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/browser_with_test_window_test.h"
28 #include "chrome/test/base/ui_test_utils.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_METHOD1(OnLogEvent
, void(NTPLoggingEventType event
));
56 MOCK_METHOD2(OnLogMostVisitedImpression
,
57 void(int position
, const base::string16
& provider
));
58 MOCK_METHOD2(OnLogMostVisitedNavigation
,
59 void(int position
, const base::string16
& provider
));
60 MOCK_METHOD1(PasteIntoOmnibox
, void(const base::string16
&));
61 MOCK_METHOD1(OnChromeIdentityCheck
, void(const base::string16
& identity
));
62 MOCK_METHOD0(OnHistorySyncCheck
, void());
65 class MockSearchIPCRouterPolicy
: public SearchIPCRouter::Policy
{
67 virtual ~MockSearchIPCRouterPolicy() {}
69 MOCK_METHOD0(ShouldProcessSetVoiceSearchSupport
, bool());
70 MOCK_METHOD1(ShouldProcessFocusOmnibox
, bool(bool));
71 MOCK_METHOD1(ShouldProcessNavigateToURL
, bool(bool));
72 MOCK_METHOD0(ShouldProcessDeleteMostVisitedItem
, bool());
73 MOCK_METHOD0(ShouldProcessUndoMostVisitedDeletion
, bool());
74 MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions
, bool());
75 MOCK_METHOD0(ShouldProcessLogEvent
, bool());
76 MOCK_METHOD1(ShouldProcessPasteIntoOmnibox
, bool(bool));
77 MOCK_METHOD0(ShouldProcessChromeIdentityCheck
, bool());
78 MOCK_METHOD0(ShouldProcessHistorySyncCheck
, bool());
79 MOCK_METHOD0(ShouldSendSetPromoInformation
, bool());
80 MOCK_METHOD0(ShouldSendSetDisplayInstantResults
, bool());
81 MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch
, bool());
82 MOCK_METHOD0(ShouldSendSetOmniboxStartMargin
, bool());
83 MOCK_METHOD1(ShouldSendSetInputInProgress
, bool(bool));
84 MOCK_METHOD0(ShouldSendOmniboxFocusChanged
, bool());
85 MOCK_METHOD0(ShouldSendMostVisitedItems
, bool());
86 MOCK_METHOD0(ShouldSendThemeBackgroundInfo
, bool());
87 MOCK_METHOD0(ShouldSendToggleVoiceSearch
, bool());
88 MOCK_METHOD0(ShouldSubmitQuery
, bool());
93 class SearchIPCRouterTest
: public BrowserWithTestWindowTest
{
95 SearchIPCRouterTest() : field_trial_list_(NULL
) {}
97 void SetUp() override
{
98 BrowserWithTestWindowTest::SetUp();
99 AddTab(browser(), GURL("chrome://blank"));
100 SearchTabHelper::CreateForWebContents(web_contents());
102 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
104 &TemplateURLServiceFactory::BuildInstanceFor
);
105 TemplateURLService
* template_url_service
=
106 TemplateURLServiceFactory::GetForProfile(profile());
107 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service
);
109 TemplateURLData data
;
110 data
.SetURL("http://foo.com/url?bar={searchTerms}");
111 data
.instant_url
= "http://foo.com/instant?"
112 "{google:omniboxStartMarginParameter}foo=foo#foo=foo&espv";
113 data
.new_tab_url
= "https://foo.com/newtab?espv";
114 data
.alternate_urls
.push_back("http://foo.com/alt#quux={searchTerms}");
115 data
.search_terms_replacement_key
= "espv";
117 TemplateURL
* template_url
= new TemplateURL(data
);
118 // Takes ownership of |template_url|.
119 template_url_service
->Add(template_url
);
120 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
121 process()->sink().ClearMessages();
124 content::WebContents
* web_contents() {
125 return browser()->tab_strip_model()->GetActiveWebContents();
128 content::MockRenderProcessHost
* process() {
129 return static_cast<content::MockRenderProcessHost
*>(
130 web_contents()->GetRenderViewHost()->GetProcess());
133 SearchTabHelper
* GetSearchTabHelper(
134 content::WebContents
* web_contents
) {
135 EXPECT_NE(static_cast<content::WebContents
*>(NULL
), web_contents
);
136 return SearchTabHelper::FromWebContents(web_contents
);
139 void SetupMockDelegateAndPolicy() {
140 content::WebContents
* contents
= web_contents();
141 ASSERT_NE(static_cast<content::WebContents
*>(NULL
), contents
);
142 SearchTabHelper
* search_tab_helper
= GetSearchTabHelper(contents
);
143 ASSERT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
144 search_tab_helper
->ipc_router().set_delegate_for_testing(mock_delegate());
145 search_tab_helper
->ipc_router().set_policy_for_testing(
146 make_scoped_ptr(new MockSearchIPCRouterPolicy
));
149 bool MessageWasSent(uint32 id
) {
150 return process()->sink().GetFirstMessageMatching(id
) != NULL
;
153 void VerifyDisplayInstantResultsMsg(bool expected_param_value
) {
154 SetupMockDelegateAndPolicy();
155 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
156 EXPECT_CALL(*policy
, ShouldSendSetDisplayInstantResults()).Times(1)
157 .WillOnce(testing::Return(true));
159 GetSearchIPCRouter().SetDisplayInstantResults();
160 const IPC::Message
* message
= process()->sink().GetFirstMessageMatching(
161 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID
);
162 EXPECT_NE(static_cast<const IPC::Message
*>(NULL
), message
);
163 Tuple
<bool> display_instant_results_param
;
164 ChromeViewMsg_SearchBoxSetDisplayInstantResults::Read(
165 message
, &display_instant_results_param
);
166 EXPECT_EQ(expected_param_value
, get
<0>(display_instant_results_param
));
169 MockSearchIPCRouterDelegate
* mock_delegate() { return &delegate_
; }
171 MockSearchIPCRouterPolicy
* GetSearchIPCRouterPolicy() {
172 content::WebContents
* contents
= web_contents();
173 EXPECT_NE(static_cast<content::WebContents
*>(NULL
), contents
);
174 SearchTabHelper
* search_tab_helper
= GetSearchTabHelper(contents
);
175 EXPECT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
176 return static_cast<MockSearchIPCRouterPolicy
*>(
177 search_tab_helper
->ipc_router().policy_for_testing());
180 SearchIPCRouter
& GetSearchIPCRouter() {
181 return GetSearchTabHelper(web_contents())->ipc_router();
184 int GetSearchIPCRouterSeqNo() {
185 return GetSearchIPCRouter().page_seq_no_for_testing();
188 void OnMessageReceived(const IPC::Message
& message
) {
189 bool should_handle_message
=
190 chrome::IsRenderedInInstantProcess(web_contents(), profile());
191 bool handled
= GetSearchIPCRouter().OnMessageReceived(message
);
192 ASSERT_EQ(should_handle_message
, handled
);
195 bool OnSpuriousMessageReceived(const IPC::Message
& message
) {
196 return GetSearchIPCRouter().OnMessageReceived(message
);
199 bool IsActiveTab(content::WebContents
* contents
) {
200 return GetSearchTabHelper(contents
)->ipc_router().is_active_tab_
;
204 MockSearchIPCRouterDelegate delegate_
;
205 base::FieldTrialList field_trial_list_
;
208 TEST_F(SearchIPCRouterTest
, IgnoreMessagesFromNonInstantRenderers
) {
209 NavigateAndCommitActiveTab(GURL("file://foo/bar"));
210 SetupMockDelegateAndPolicy();
211 GURL
destination_url("www.foo.com");
212 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
214 content::WebContents
* contents
= web_contents();
215 bool is_active_tab
= IsActiveTab(contents
);
216 EXPECT_TRUE(is_active_tab
);
218 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
219 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(0);
221 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
222 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url
,
226 TEST_F(SearchIPCRouterTest
, ProcessVoiceSearchSupportMsg
) {
227 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
228 SetupMockDelegateAndPolicy();
229 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
230 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(1);
231 EXPECT_CALL(*(policy
), ShouldProcessSetVoiceSearchSupport()).Times(1)
232 .WillOnce(testing::Return(true));
234 content::WebContents
* contents
= web_contents();
235 OnMessageReceived(ChromeViewHostMsg_SetVoiceSearchSupported(
236 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), true));
239 TEST_F(SearchIPCRouterTest
, IgnoreVoiceSearchSupportMsg
) {
240 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
241 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(0);
242 SetupMockDelegateAndPolicy();
243 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
244 EXPECT_CALL(*policy
, ShouldProcessSetVoiceSearchSupport()).Times(1)
245 .WillOnce(testing::Return(false));
247 content::WebContents
* contents
= web_contents();
248 OnMessageReceived(ChromeViewHostMsg_SetVoiceSearchSupported(
249 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), true));
252 TEST_F(SearchIPCRouterTest
, ProcessFocusOmniboxMsg
) {
253 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
254 SetupMockDelegateAndPolicy();
255 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
256 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(1);
258 content::WebContents
* contents
= web_contents();
259 bool is_active_tab
= IsActiveTab(contents
);
260 EXPECT_TRUE(is_active_tab
);
261 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(1)
262 .WillOnce(testing::Return(true));
264 OnMessageReceived(ChromeViewHostMsg_FocusOmnibox(
265 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(),
266 OMNIBOX_FOCUS_VISIBLE
));
269 TEST_F(SearchIPCRouterTest
, IgnoreFocusOmniboxMsg
) {
270 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
271 SetupMockDelegateAndPolicy();
272 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
273 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(0);
275 content::WebContents
* contents
= web_contents();
276 bool is_active_tab
= IsActiveTab(contents
);
277 EXPECT_TRUE(is_active_tab
);
278 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(1)
279 .WillOnce(testing::Return(false));
281 OnMessageReceived(ChromeViewHostMsg_FocusOmnibox(
282 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(),
283 OMNIBOX_FOCUS_VISIBLE
));
286 TEST_F(SearchIPCRouterTest
, HandleTabChangedEvents
) {
287 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
288 content::WebContents
* contents
= web_contents();
289 EXPECT_EQ(0, browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
290 EXPECT_TRUE(IsActiveTab(contents
));
292 // Add a new tab to deactivate the current tab.
293 AddTab(browser(), GURL(url::kAboutBlankURL
));
294 EXPECT_EQ(2, browser()->tab_strip_model()->count());
295 EXPECT_EQ(1, browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
296 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
297 EXPECT_FALSE(IsActiveTab(contents
));
299 // Activate the first tab.
300 browser()->tab_strip_model()->ActivateTabAt(1, false);
301 EXPECT_EQ(browser()->tab_strip_model()->active_index(),
302 browser()->tab_strip_model()->GetIndexOfWebContents(contents
));
303 EXPECT_TRUE(IsActiveTab(contents
));
306 TEST_F(SearchIPCRouterTest
, ProcessNavigateToURLMsg
) {
307 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
308 SetupMockDelegateAndPolicy();
309 GURL
destination_url("www.foo.com");
310 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
312 content::WebContents
* contents
= web_contents();
313 bool is_active_tab
= IsActiveTab(contents
);
314 EXPECT_TRUE(is_active_tab
);
316 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
317 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(1)
318 .WillOnce(testing::Return(true));
320 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
321 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url
,
325 TEST_F(SearchIPCRouterTest
, IgnoreNavigateToURLMsg
) {
326 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
327 SetupMockDelegateAndPolicy();
328 GURL
destination_url("www.foo.com");
329 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url
, CURRENT_TAB
,
331 content::WebContents
* contents
= web_contents();
332 bool is_active_tab
= IsActiveTab(contents
);
333 EXPECT_TRUE(is_active_tab
);
335 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
336 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(1)
337 .WillOnce(testing::Return(false));
339 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
340 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), destination_url
,
344 TEST_F(SearchIPCRouterTest
, ProcessLogEventMsg
) {
345 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
346 SetupMockDelegateAndPolicy();
347 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
348 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
)).Times(1);
349 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
350 .WillOnce(testing::Return(true));
352 content::WebContents
* contents
= web_contents();
353 OnMessageReceived(ChromeViewHostMsg_LogEvent(
354 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), NTP_MOUSEOVER
));
357 TEST_F(SearchIPCRouterTest
, IgnoreLogEventMsg
) {
358 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
359 SetupMockDelegateAndPolicy();
360 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
361 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
)).Times(0);
362 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
363 .WillOnce(testing::Return(false));
365 content::WebContents
* contents
= web_contents();
366 OnMessageReceived(ChromeViewHostMsg_LogEvent(
367 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), NTP_MOUSEOVER
));
370 TEST_F(SearchIPCRouterTest
, ProcessLogMostVisitedImpressionMsg
) {
371 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
372 SetupMockDelegateAndPolicy();
373 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
374 EXPECT_CALL(*mock_delegate(),
375 OnLogMostVisitedImpression(3, base::ASCIIToUTF16("Server"))).Times(1);
376 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
377 .WillOnce(testing::Return(true));
379 content::WebContents
* contents
= web_contents();
380 OnMessageReceived(ChromeViewHostMsg_LogMostVisitedImpression(
381 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), 3,
382 base::ASCIIToUTF16("Server")));
385 TEST_F(SearchIPCRouterTest
, ProcessLogMostVisitedNavigationMsg
) {
386 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
387 SetupMockDelegateAndPolicy();
388 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
389 EXPECT_CALL(*mock_delegate(),
390 OnLogMostVisitedNavigation(3, base::ASCIIToUTF16("Server"))).Times(1);
391 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(1)
392 .WillOnce(testing::Return(true));
394 content::WebContents
* contents
= web_contents();
395 OnMessageReceived(ChromeViewHostMsg_LogMostVisitedNavigation(
396 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), 3,
397 base::ASCIIToUTF16("Server")));
400 TEST_F(SearchIPCRouterTest
, ProcessChromeIdentityCheckMsg
) {
401 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
402 SetupMockDelegateAndPolicy();
403 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
404 const base::string16 test_identity
= base::ASCIIToUTF16("foo@bar.com");
405 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity
)).Times(1);
406 EXPECT_CALL(*policy
, ShouldProcessChromeIdentityCheck()).Times(1)
407 .WillOnce(testing::Return(true));
409 content::WebContents
* contents
= web_contents();
410 OnMessageReceived(ChromeViewHostMsg_ChromeIdentityCheck(
411 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), test_identity
));
414 TEST_F(SearchIPCRouterTest
, IgnoreChromeIdentityCheckMsg
) {
415 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
416 SetupMockDelegateAndPolicy();
417 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
419 const base::string16 test_identity
= base::ASCIIToUTF16("foo@bar.com");
420 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity
)).Times(0);
421 EXPECT_CALL(*policy
, ShouldProcessChromeIdentityCheck()).Times(1)
422 .WillOnce(testing::Return(false));
424 content::WebContents
* contents
= web_contents();
425 OnMessageReceived(ChromeViewHostMsg_ChromeIdentityCheck(
426 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), test_identity
));
429 TEST_F(SearchIPCRouterTest
, ProcessHistorySyncCheckMsg
) {
430 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
431 SetupMockDelegateAndPolicy();
432 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
433 EXPECT_CALL(*mock_delegate(), OnHistorySyncCheck()).Times(1);
434 EXPECT_CALL(*policy
, ShouldProcessHistorySyncCheck()).Times(1)
435 .WillOnce(testing::Return(true));
437 content::WebContents
* contents
= web_contents();
438 OnMessageReceived(ChromeViewHostMsg_HistorySyncCheck(
439 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
442 TEST_F(SearchIPCRouterTest
, IgnoreHistorySyncCheckMsg
) {
443 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
444 SetupMockDelegateAndPolicy();
445 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
447 EXPECT_CALL(*mock_delegate(), OnHistorySyncCheck()).Times(0);
448 EXPECT_CALL(*policy
, ShouldProcessHistorySyncCheck()).Times(1)
449 .WillOnce(testing::Return(false));
451 content::WebContents
* contents
= web_contents();
452 OnMessageReceived(ChromeViewHostMsg_HistorySyncCheck(
453 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
456 TEST_F(SearchIPCRouterTest
, ProcessDeleteMostVisitedItemMsg
) {
457 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
458 SetupMockDelegateAndPolicy();
459 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
460 GURL
item_url("www.foo.com");
461 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(1);
462 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(1)
463 .WillOnce(testing::Return(true));
465 content::WebContents
* contents
= web_contents();
466 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
467 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
470 TEST_F(SearchIPCRouterTest
, IgnoreDeleteMostVisitedItemMsg
) {
471 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
472 SetupMockDelegateAndPolicy();
473 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
474 GURL
item_url("www.foo.com");
475 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(0);
476 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(1)
477 .WillOnce(testing::Return(false));
479 content::WebContents
* contents
= web_contents();
480 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
481 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
484 TEST_F(SearchIPCRouterTest
, ProcessUndoMostVisitedDeletionMsg
) {
485 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
486 SetupMockDelegateAndPolicy();
487 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
488 GURL
item_url("www.foo.com");
489 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(1);
490 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(1)
491 .WillOnce(testing::Return(true));
493 content::WebContents
* contents
= web_contents();
494 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
495 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
498 TEST_F(SearchIPCRouterTest
, IgnoreUndoMostVisitedDeletionMsg
) {
499 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
500 SetupMockDelegateAndPolicy();
501 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
502 GURL
item_url("www.foo.com");
503 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(0);
504 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(1)
505 .WillOnce(testing::Return(false));
507 content::WebContents
* contents
= web_contents();
508 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
509 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url
));
512 TEST_F(SearchIPCRouterTest
, ProcessUndoAllMostVisitedDeletionsMsg
) {
513 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
514 SetupMockDelegateAndPolicy();
515 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
516 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(1);
517 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
518 .WillOnce(testing::Return(true));
520 content::WebContents
* contents
= web_contents();
521 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
522 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
525 TEST_F(SearchIPCRouterTest
, IgnoreUndoAllMostVisitedDeletionsMsg
) {
526 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
527 SetupMockDelegateAndPolicy();
528 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
529 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
530 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
531 .WillOnce(testing::Return(false));
533 content::WebContents
* contents
= web_contents();
534 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
535 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo()));
538 TEST_F(SearchIPCRouterTest
, IgnoreMessageIfThePageIsNotActive
) {
539 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
540 SetupMockDelegateAndPolicy();
541 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
542 int page_seq_no
= GetSearchIPCRouterSeqNo();
544 content::WebContents
* contents
= web_contents();
545 bool is_active_tab
= IsActiveTab(contents
);
546 GURL
item_url("www.foo.com");
547 EXPECT_CALL(*mock_delegate(), NavigateToURL(item_url
, CURRENT_TAB
,
549 // At this point, in a real test, the navigation would cause the
550 // SearchIPCRouter's page sequence to advance. In this test it doesn't, so
551 // we'll decrement the sequence number on this side to simulate it.
553 EXPECT_CALL(*policy
, ShouldProcessNavigateToURL(is_active_tab
)).Times(0);
554 OnMessageReceived(ChromeViewHostMsg_SearchBoxNavigate(
555 contents
->GetRoutingID(), page_seq_no
, item_url
, CURRENT_TAB
, true));
557 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url
)).Times(0);
558 EXPECT_CALL(*policy
, ShouldProcessDeleteMostVisitedItem()).Times(0);
559 OnMessageReceived(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
560 contents
->GetRoutingID(), page_seq_no
, item_url
));
562 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url
)).Times(0);
563 EXPECT_CALL(*policy
, ShouldProcessUndoMostVisitedDeletion()).Times(0);
564 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
565 contents
->GetRoutingID(), page_seq_no
, item_url
));
567 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
568 EXPECT_CALL(*policy
, ShouldProcessUndoAllMostVisitedDeletions()).Times(0);
569 OnMessageReceived(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
570 contents
->GetRoutingID(), page_seq_no
));
572 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE
)).Times(0);
573 EXPECT_CALL(*policy
, ShouldProcessFocusOmnibox(is_active_tab
)).Times(0);
574 OnMessageReceived(ChromeViewHostMsg_FocusOmnibox(
575 contents
->GetRoutingID(), page_seq_no
, OMNIBOX_FOCUS_VISIBLE
));
577 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER
)).Times(0);
578 EXPECT_CALL(*policy
, ShouldProcessLogEvent()).Times(0);
579 OnMessageReceived(ChromeViewHostMsg_LogEvent(
580 contents
->GetRoutingID(), page_seq_no
, NTP_MOUSEOVER
));
583 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(0);
584 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(0);
585 OnMessageReceived(ChromeViewHostMsg_PasteAndOpenDropdown(
586 contents
->GetRoutingID(), page_seq_no
, text
));
589 TEST_F(SearchIPCRouterTest
, ProcessPasteAndOpenDropdownMsg
) {
590 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
591 SetupMockDelegateAndPolicy();
592 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
594 content::WebContents
* contents
= web_contents();
595 bool is_active_tab
= IsActiveTab(contents
);
596 EXPECT_TRUE(is_active_tab
);
599 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(1);
600 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(1)
601 .WillOnce(testing::Return(true));
602 OnMessageReceived(ChromeViewHostMsg_PasteAndOpenDropdown(
603 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), text
));
606 TEST_F(SearchIPCRouterTest
, IgnorePasteAndOpenDropdownMsg
) {
607 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
608 SetupMockDelegateAndPolicy();
610 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text
)).Times(0);
612 content::WebContents
* contents
= web_contents();
613 bool is_active_tab
= IsActiveTab(contents
);
614 EXPECT_TRUE(is_active_tab
);
616 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
617 EXPECT_CALL(*policy
, ShouldProcessPasteIntoOmnibox(is_active_tab
)).Times(1)
618 .WillOnce(testing::Return(false));
620 OnMessageReceived(ChromeViewHostMsg_PasteAndOpenDropdown(
621 contents
->GetRoutingID(), GetSearchIPCRouterSeqNo(), text
));
624 TEST_F(SearchIPCRouterTest
, SendSetPromoInformationMsg
) {
625 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
626 SetupMockDelegateAndPolicy();
627 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
628 EXPECT_CALL(*policy
, ShouldSendSetPromoInformation()).Times(1)
629 .WillOnce(testing::Return(true));
631 GetSearchIPCRouter().SetPromoInformation(true);
632 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID
));
635 TEST_F(SearchIPCRouterTest
, DoNotSendSetPromoInformationMsg
) {
636 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
637 SetupMockDelegateAndPolicy();
638 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
639 EXPECT_CALL(*policy
, ShouldSendSetPromoInformation()).Times(1)
640 .WillOnce(testing::Return(false));
642 GetSearchIPCRouter().SetPromoInformation(false);
643 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID
));
646 TEST_F(SearchIPCRouterTest
,
647 SendSetDisplayInstantResultsMsg_EnableInstantOnResultsPage
) {
648 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
650 "Group1 espv:42 query_extraction:1 prefetch_results_srp:1"));
651 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
653 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
654 // set to true if the underlying page is a results page and
655 // "prefetch_results_srp" flag is enabled via field trials.
656 VerifyDisplayInstantResultsMsg(true);
659 TEST_F(SearchIPCRouterTest
,
660 SendSetDisplayInstantResultsMsg_DisableInstantOnResultsPage
) {
661 // |prefetch_results_srp" flag is disabled via field trials.
662 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
664 "Group1 espv:42 query_extraction:1 prefetch_results_srp:0"));
665 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
667 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
669 VerifyDisplayInstantResultsMsg(false);
672 TEST_F(SearchIPCRouterTest
,
673 SendSetDisplayInstantResultsMsg_EnableInstantOutsideSearchResultsPage
) {
674 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
675 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
676 // true if the underlying page is not a search results page.
677 VerifyDisplayInstantResultsMsg(true);
680 TEST_F(SearchIPCRouterTest
, DoNotSendSetDisplayInstantResultsMsg
) {
681 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
682 SetupMockDelegateAndPolicy();
683 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
684 EXPECT_CALL(*policy
, ShouldSendSetDisplayInstantResults()).Times(1)
685 .WillOnce(testing::Return(false));
687 process()->sink().ClearMessages();
688 GetSearchIPCRouter().SetDisplayInstantResults();
689 EXPECT_FALSE(MessageWasSent(
690 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID
));
693 TEST_F(SearchIPCRouterTest
, SendSetSuggestionToPrefetch
) {
694 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
695 SetupMockDelegateAndPolicy();
696 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
697 EXPECT_CALL(*policy
, ShouldSendSetSuggestionToPrefetch()).Times(1)
698 .WillOnce(testing::Return(true));
700 process()->sink().ClearMessages();
701 content::WebContents
* contents
= web_contents();
702 GetSearchTabHelper(contents
)->SetSuggestionToPrefetch(InstantSuggestion());
703 EXPECT_TRUE(MessageWasSent(
704 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID
));
707 TEST_F(SearchIPCRouterTest
, DoNotSendSetSuggestionToPrefetch
) {
708 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
709 SetupMockDelegateAndPolicy();
710 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
711 EXPECT_CALL(*policy
, ShouldSendSetSuggestionToPrefetch()).Times(1)
712 .WillOnce(testing::Return(false));
714 process()->sink().ClearMessages();
715 content::WebContents
* contents
= web_contents();
716 GetSearchTabHelper(contents
)->SetSuggestionToPrefetch(InstantSuggestion());
717 EXPECT_FALSE(MessageWasSent(
718 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID
));
721 TEST_F(SearchIPCRouterTest
, SendSetOmniboxStartMargin
) {
722 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
723 SetupMockDelegateAndPolicy();
724 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
725 EXPECT_CALL(*policy
, ShouldSendSetOmniboxStartMargin()).Times(1)
726 .WillOnce(testing::Return(true));
728 process()->sink().ClearMessages();
729 GetSearchIPCRouter().SetOmniboxStartMargin(92);
730 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID
));
733 TEST_F(SearchIPCRouterTest
, DoNotSendSetOmniboxStartMargin
) {
734 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
735 SetupMockDelegateAndPolicy();
736 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
737 EXPECT_CALL(*policy
, ShouldSendSetOmniboxStartMargin()).Times(1)
738 .WillOnce(testing::Return(false));
740 process()->sink().ClearMessages();
741 GetSearchIPCRouter().SetOmniboxStartMargin(92);
742 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID
));
745 TEST_F(SearchIPCRouterTest
, SendOmniboxFocusChange
) {
746 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
747 SetupMockDelegateAndPolicy();
748 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
749 EXPECT_CALL(*policy
, ShouldSendOmniboxFocusChanged()).Times(1)
750 .WillOnce(testing::Return(true));
752 process()->sink().ClearMessages();
753 GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE
,
754 OMNIBOX_FOCUS_CHANGE_EXPLICIT
);
755 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID
));
758 TEST_F(SearchIPCRouterTest
, DoNotSendOmniboxFocusChange
) {
759 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
760 SetupMockDelegateAndPolicy();
761 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
762 EXPECT_CALL(*policy
, ShouldSendOmniboxFocusChanged()).Times(1)
763 .WillOnce(testing::Return(false));
765 process()->sink().ClearMessages();
766 GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE
,
767 OMNIBOX_FOCUS_CHANGE_EXPLICIT
);
768 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID
));
771 TEST_F(SearchIPCRouterTest
, SendSetInputInProgress
) {
772 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
773 SetupMockDelegateAndPolicy();
774 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
775 EXPECT_CALL(*policy
, ShouldSendSetInputInProgress(true)).Times(1)
776 .WillOnce(testing::Return(true));
778 process()->sink().ClearMessages();
779 GetSearchIPCRouter().SetInputInProgress(true);
780 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID
));
783 TEST_F(SearchIPCRouterTest
, DoNotSendSetInputInProgress
) {
784 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
785 SetupMockDelegateAndPolicy();
786 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
787 EXPECT_CALL(*policy
, ShouldSendSetInputInProgress(true)).Times(1)
788 .WillOnce(testing::Return(false));
790 process()->sink().ClearMessages();
791 GetSearchIPCRouter().SetInputInProgress(true);
792 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID
));
795 TEST_F(SearchIPCRouterTest
, SendMostVisitedItemsMsg
) {
796 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
797 SetupMockDelegateAndPolicy();
798 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
799 EXPECT_CALL(*policy
, ShouldSendMostVisitedItems()).Times(1)
800 .WillOnce(testing::Return(true));
802 process()->sink().ClearMessages();
803 GetSearchIPCRouter().SendMostVisitedItems(
804 std::vector
<InstantMostVisitedItem
>());
805 EXPECT_TRUE(MessageWasSent(
806 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID
));
809 TEST_F(SearchIPCRouterTest
, DoNotSendMostVisitedItemsMsg
) {
810 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
811 SetupMockDelegateAndPolicy();
812 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
813 EXPECT_CALL(*policy
, ShouldSendMostVisitedItems()).Times(1)
814 .WillOnce(testing::Return(false));
816 process()->sink().ClearMessages();
817 GetSearchIPCRouter().SendMostVisitedItems(
818 std::vector
<InstantMostVisitedItem
>());
819 EXPECT_FALSE(MessageWasSent(
820 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID
));
823 TEST_F(SearchIPCRouterTest
, SendThemeBackgroundInfoMsg
) {
824 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
825 SetupMockDelegateAndPolicy();
826 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
827 EXPECT_CALL(*policy
, ShouldSendThemeBackgroundInfo()).Times(1)
828 .WillOnce(testing::Return(true));
830 process()->sink().ClearMessages();
831 GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
832 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID
));
835 TEST_F(SearchIPCRouterTest
, DoNotSendThemeBackgroundInfoMsg
) {
836 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
837 SetupMockDelegateAndPolicy();
838 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
839 EXPECT_CALL(*policy
, ShouldSendThemeBackgroundInfo()).Times(1)
840 .WillOnce(testing::Return(false));
842 process()->sink().ClearMessages();
843 GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
844 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID
));
847 TEST_F(SearchIPCRouterTest
, SendSubmitMsg
) {
848 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
849 SetupMockDelegateAndPolicy();
850 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
851 EXPECT_CALL(*policy
, ShouldSubmitQuery()).Times(1)
852 .WillOnce(testing::Return(true));
854 process()->sink().ClearMessages();
855 GetSearchIPCRouter().Submit(base::string16(), EmbeddedSearchRequestParams());
856 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID
));
859 TEST_F(SearchIPCRouterTest
, DoNotSendSubmitMsg
) {
860 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
861 SetupMockDelegateAndPolicy();
862 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
863 EXPECT_CALL(*policy
, ShouldSubmitQuery()).Times(1)
864 .WillOnce(testing::Return(false));
866 process()->sink().ClearMessages();
867 GetSearchIPCRouter().Submit(base::string16(), EmbeddedSearchRequestParams());
868 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID
));
871 TEST_F(SearchIPCRouterTest
, SendToggleVoiceSearch
) {
872 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
873 SetupMockDelegateAndPolicy();
874 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
875 EXPECT_CALL(*policy
, ShouldSendToggleVoiceSearch()).Times(1)
876 .WillOnce(testing::Return(true));
878 process()->sink().ClearMessages();
879 GetSearchIPCRouter().ToggleVoiceSearch();
880 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID
));
883 TEST_F(SearchIPCRouterTest
, DoNotSendToggleVoiceSearch
) {
884 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
885 SetupMockDelegateAndPolicy();
886 MockSearchIPCRouterPolicy
* policy
= GetSearchIPCRouterPolicy();
887 EXPECT_CALL(*policy
, ShouldSendToggleVoiceSearch()).Times(1)
888 .WillOnce(testing::Return(false));
890 process()->sink().ClearMessages();
891 GetSearchIPCRouter().ToggleVoiceSearch();
892 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID
));
895 TEST_F(SearchIPCRouterTest
, SpuriousMessageTypesIgnored
) {
896 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
897 SetupMockDelegateAndPolicy();
898 const int routing_id
= web_contents()->GetRoutingID();
900 // Construct a series of synthetic messages for each valid IPC message type,
901 // ensuring the router ignores them all.
902 for (int i
= 0; i
< LastIPCMsgStart
; ++i
) {
903 const int message_id
= i
<< 16;
904 ASSERT_EQ(IPC_MESSAGE_ID_CLASS(message_id
), i
);
905 IPC::Message
msg(routing_id
, message_id
, IPC::Message::PRIORITY_LOW
);
906 EXPECT_FALSE(OnSpuriousMessageReceived(msg
)) << i
;