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_METHOD1(PasteIntoOmnibox, void(const string16&));
56 MOCK_METHOD1(OnChromeIdentityCheck, void(const string16& identity));
59 class MockSearchIPCRouterPolicy : public SearchIPCRouter::Policy {
61 virtual ~MockSearchIPCRouterPolicy() {}
63 MOCK_METHOD0(ShouldProcessSetVoiceSearchSupport, bool());
64 MOCK_METHOD1(ShouldProcessFocusOmnibox, bool(bool));
65 MOCK_METHOD1(ShouldProcessNavigateToURL, bool(bool));
66 MOCK_METHOD0(ShouldProcessDeleteMostVisitedItem, bool());
67 MOCK_METHOD0(ShouldProcessUndoMostVisitedDeletion, bool());
68 MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions, bool());
69 MOCK_METHOD0(ShouldProcessLogEvent, bool());
70 MOCK_METHOD1(ShouldProcessPasteIntoOmnibox, bool(bool));
71 MOCK_METHOD0(ShouldProcessChromeIdentityCheck, bool());
72 MOCK_METHOD0(ShouldSendSetPromoInformation, bool());
73 MOCK_METHOD0(ShouldSendSetDisplayInstantResults, bool());
74 MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch, bool());
75 MOCK_METHOD0(ShouldSendMostVisitedItems, bool());
76 MOCK_METHOD0(ShouldSendThemeBackgroundInfo, bool());
77 MOCK_METHOD0(ShouldSubmitQuery, bool());
82 class SearchIPCRouterTest : public BrowserWithTestWindowTest {
84 SearchIPCRouterTest() : field_trial_list_(NULL) {}
86 virtual void SetUp() {
87 BrowserWithTestWindowTest::SetUp();
88 AddTab(browser(), GURL("chrome://blank"));
89 SearchTabHelper::CreateForWebContents(web_contents());
91 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
93 &TemplateURLServiceFactory::BuildInstanceFor);
94 TemplateURLService* template_url_service =
95 TemplateURLServiceFactory::GetForProfile(profile());
96 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
99 data.SetURL("http://foo.com/url?bar={searchTerms}");
100 data.instant_url = "http://foo.com/instant?"
101 "{google:omniboxStartMarginParameter}foo=foo#foo=foo&espv";
102 data.new_tab_url = "https://foo.com/newtab?espv";
103 data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}");
104 data.search_terms_replacement_key = "espv";
106 TemplateURL* template_url = new TemplateURL(profile(), data);
107 // Takes ownership of |template_url|.
108 template_url_service->Add(template_url);
109 template_url_service->SetDefaultSearchProvider(template_url);
112 content::WebContents* web_contents() {
113 return browser()->tab_strip_model()->GetActiveWebContents();
116 content::MockRenderProcessHost* process() {
117 return static_cast<content::MockRenderProcessHost*>(
118 web_contents()->GetRenderViewHost()->GetProcess());
121 SearchTabHelper* GetSearchTabHelper(
122 content::WebContents* web_contents) {
123 EXPECT_NE(static_cast<content::WebContents*>(NULL), web_contents);
124 return SearchTabHelper::FromWebContents(web_contents);
127 void SetupMockDelegateAndPolicy(content::WebContents* web_contents) {
128 ASSERT_NE(static_cast<content::WebContents*>(NULL), web_contents);
129 SearchTabHelper* search_tab_helper =
130 GetSearchTabHelper(web_contents);
131 ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
132 search_tab_helper->ipc_router().set_delegate(mock_delegate());
133 search_tab_helper->ipc_router().set_policy(
134 make_scoped_ptr(new MockSearchIPCRouterPolicy)
135 .PassAs<SearchIPCRouter::Policy>());
138 bool MessageWasSent(uint32 id) {
139 return process()->sink().GetFirstMessageMatching(id) != NULL;
142 void VerifyDisplayInstantResultsMsg(bool expected_param_value) {
143 process()->sink().ClearMessages();
145 content::WebContents* contents = web_contents();
146 SetupMockDelegateAndPolicy(contents);
147 MockSearchIPCRouterPolicy* policy =
148 GetSearchIPCRouterPolicy(contents);
149 EXPECT_CALL(*policy, ShouldSendSetDisplayInstantResults()).Times(1)
150 .WillOnce(testing::Return(true));
152 GetSearchTabHelper(contents)->ipc_router().SetDisplayInstantResults();
153 const IPC::Message* message = process()->sink().GetFirstMessageMatching(
154 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID);
155 EXPECT_NE(static_cast<const IPC::Message*>(NULL), message);
156 Tuple1<bool> display_instant_results_param;
157 ChromeViewMsg_SearchBoxSetDisplayInstantResults::Read(
158 message, &display_instant_results_param);
159 EXPECT_EQ(expected_param_value, display_instant_results_param.a);
162 MockSearchIPCRouterDelegate* mock_delegate() { return &delegate_; }
164 MockSearchIPCRouterPolicy* GetSearchIPCRouterPolicy(
165 content::WebContents* web_contents) {
166 EXPECT_NE(static_cast<content::WebContents*>(NULL), web_contents);
167 SearchTabHelper* search_tab_helper =
168 GetSearchTabHelper(web_contents);
169 EXPECT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
170 return static_cast<MockSearchIPCRouterPolicy*>(
171 search_tab_helper->ipc_router().policy());
175 MockSearchIPCRouterDelegate delegate_;
176 base::FieldTrialList field_trial_list_;
179 TEST_F(SearchIPCRouterTest, ProcessVoiceSearchSupportMsg) {
180 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
181 process()->sink().ClearMessages();
183 content::WebContents* contents = web_contents();
184 SetupMockDelegateAndPolicy(contents);
185 MockSearchIPCRouterPolicy* policy =
186 GetSearchIPCRouterPolicy(contents);
187 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(1);
188 EXPECT_CALL(*policy, ShouldProcessSetVoiceSearchSupport()).Times(1)
189 .WillOnce(testing::Return(true));
191 scoped_ptr<IPC::Message> message(
192 new ChromeViewHostMsg_SetVoiceSearchSupported(
193 contents->GetRoutingID(),
194 contents->GetController().GetVisibleEntry()->GetPageID(),
196 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
199 TEST_F(SearchIPCRouterTest, IgnoreVoiceSearchSupportMsg) {
200 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
201 process()->sink().ClearMessages();
203 content::WebContents* contents = web_contents();
204 EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(0);
205 SetupMockDelegateAndPolicy(contents);
206 MockSearchIPCRouterPolicy* policy =
207 GetSearchIPCRouterPolicy(contents);
208 EXPECT_CALL(*policy, ShouldProcessSetVoiceSearchSupport()).Times(1)
209 .WillOnce(testing::Return(false));
211 scoped_ptr<IPC::Message> message(
212 new ChromeViewHostMsg_SetVoiceSearchSupported(
213 contents->GetRoutingID(),
214 contents->GetController().GetVisibleEntry()->GetPageID(),
216 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
219 TEST_F(SearchIPCRouterTest, ProcessFocusOmniboxMsg) {
220 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
221 process()->sink().ClearMessages();
223 content::WebContents* contents = web_contents();
224 SetupMockDelegateAndPolicy(contents);
225 MockSearchIPCRouterPolicy* policy =
226 GetSearchIPCRouterPolicy(contents);
227 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(1);
229 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
230 bool is_active_tab = search_tab_helper->ipc_router().is_active_tab_;
231 EXPECT_TRUE(is_active_tab);
232 EXPECT_CALL(*policy, ShouldProcessFocusOmnibox(is_active_tab)).Times(1)
233 .WillOnce(testing::Return(true));
235 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_FocusOmnibox(
236 contents->GetRoutingID(),
237 contents->GetController().GetVisibleEntry()->GetPageID(),
238 OMNIBOX_FOCUS_VISIBLE));
239 search_tab_helper->ipc_router().OnMessageReceived(*message);
242 TEST_F(SearchIPCRouterTest, IgnoreFocusOmniboxMsg) {
243 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
244 process()->sink().ClearMessages();
246 content::WebContents* contents = web_contents();
247 SetupMockDelegateAndPolicy(contents);
248 MockSearchIPCRouterPolicy* policy =
249 GetSearchIPCRouterPolicy(contents);
250 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(0);
252 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
253 bool is_active_tab = search_tab_helper->ipc_router().is_active_tab_;
254 EXPECT_TRUE(is_active_tab);
255 EXPECT_CALL(*policy, ShouldProcessFocusOmnibox(is_active_tab)).Times(1)
256 .WillOnce(testing::Return(false));
258 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_FocusOmnibox(
259 contents->GetRoutingID(),
260 contents->GetController().GetVisibleEntry()->GetPageID(),
261 OMNIBOX_FOCUS_VISIBLE));
262 search_tab_helper->ipc_router().OnMessageReceived(*message);
265 TEST_F(SearchIPCRouterTest, HandleTabChangedEvents) {
266 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
267 content::WebContents* contents = web_contents();
268 EXPECT_EQ(0, browser()->tab_strip_model()->GetIndexOfWebContents(contents));
269 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
270 EXPECT_TRUE(search_tab_helper->ipc_router().is_active_tab_);
272 // Add a new tab to deactivate the current tab.
273 AddTab(browser(), GURL(content::kAboutBlankURL));
274 EXPECT_EQ(2, browser()->tab_strip_model()->count());
275 EXPECT_EQ(1, browser()->tab_strip_model()->GetIndexOfWebContents(contents));
276 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
277 EXPECT_FALSE(search_tab_helper->ipc_router().is_active_tab_);
279 // Activate the first tab.
280 browser()->tab_strip_model()->ActivateTabAt(1, false);
281 EXPECT_EQ(browser()->tab_strip_model()->active_index(),
282 browser()->tab_strip_model()->GetIndexOfWebContents(contents));
283 EXPECT_TRUE(search_tab_helper->ipc_router().is_active_tab_);
286 TEST_F(SearchIPCRouterTest, ProcessNavigateToURLMsg) {
287 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
288 process()->sink().ClearMessages();
290 content::WebContents* contents = web_contents();
291 SetupMockDelegateAndPolicy(contents);
292 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(contents);
294 GURL destination_url("www.foo.com");
295 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url, CURRENT_TAB,
297 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
298 bool is_active_tab = search_tab_helper->ipc_router().is_active_tab_;
299 EXPECT_TRUE(is_active_tab);
300 EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(1)
301 .WillOnce(testing::Return(true));
303 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_SearchBoxNavigate(
304 contents->GetRoutingID(),
305 contents->GetController().GetVisibleEntry()->GetPageID(),
306 destination_url, CURRENT_TAB, true));
307 search_tab_helper->ipc_router().OnMessageReceived(*message);
310 TEST_F(SearchIPCRouterTest, IgnoreNavigateToURLMsg) {
311 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
312 process()->sink().ClearMessages();
313 GURL destination_url("www.foo.com");
315 content::WebContents* contents = web_contents();
316 SetupMockDelegateAndPolicy(contents);
317 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(contents);
318 EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url, CURRENT_TAB,
320 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
321 bool is_active_tab = search_tab_helper->ipc_router().is_active_tab_;
322 EXPECT_TRUE(is_active_tab);
323 EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(1)
324 .WillOnce(testing::Return(false));
326 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_SearchBoxNavigate(
327 contents->GetRoutingID(),
328 contents->GetController().GetVisibleEntry()->GetPageID(),
329 destination_url, CURRENT_TAB, true));
330 search_tab_helper->ipc_router().OnMessageReceived(*message);
333 TEST_F(SearchIPCRouterTest, ProcessLogEventMsg) {
334 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
335 process()->sink().ClearMessages();
336 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER)).Times(1);
338 content::WebContents* contents = web_contents();
339 SetupMockDelegateAndPolicy(contents);
340 MockSearchIPCRouterPolicy* policy =
341 GetSearchIPCRouterPolicy(contents);
343 EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1)
344 .WillOnce(testing::Return(true));
346 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_LogEvent(
347 contents->GetRoutingID(),
348 contents->GetController().GetVisibleEntry()->GetPageID(),
350 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
353 TEST_F(SearchIPCRouterTest, IgnoreLogEventMsg) {
354 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
355 process()->sink().ClearMessages();
356 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER)).Times(0);
358 content::WebContents* contents = web_contents();
359 SetupMockDelegateAndPolicy(contents);
360 MockSearchIPCRouterPolicy* policy =
361 GetSearchIPCRouterPolicy(contents);
362 EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1)
363 .WillOnce(testing::Return(false));
365 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_LogEvent(
366 contents->GetRoutingID(),
367 contents->GetController().GetVisibleEntry()->GetPageID(),
369 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
372 TEST_F(SearchIPCRouterTest, ProcessChromeIdentityCheckMsg) {
373 const string16 test_identity = ASCIIToUTF16("foo@bar.com");
374 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
375 process()->sink().ClearMessages();
376 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity)).Times(1);
378 content::WebContents* contents = web_contents();
379 SetupMockDelegateAndPolicy(contents);
380 MockSearchIPCRouterPolicy* policy =
381 GetSearchIPCRouterPolicy(contents);
383 EXPECT_CALL(*policy, ShouldProcessChromeIdentityCheck()).Times(1)
384 .WillOnce(testing::Return(true));
386 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck(
387 contents->GetRoutingID(),
388 contents->GetController().GetVisibleEntry()->GetPageID(),
390 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
393 TEST_F(SearchIPCRouterTest, IgnoreChromeIdentityCheckMsg) {
394 const string16 test_identity = ASCIIToUTF16("foo@bar.com");
395 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
396 process()->sink().ClearMessages();
397 EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity)).Times(0);
399 content::WebContents* contents = web_contents();
400 SetupMockDelegateAndPolicy(contents);
401 MockSearchIPCRouterPolicy* policy =
402 GetSearchIPCRouterPolicy(contents);
403 EXPECT_CALL(*policy, ShouldProcessChromeIdentityCheck()).Times(1)
404 .WillOnce(testing::Return(false));
406 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck(
407 contents->GetRoutingID(),
408 contents->GetController().GetVisibleEntry()->GetPageID(),
410 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
413 TEST_F(SearchIPCRouterTest, ProcessDeleteMostVisitedItemMsg) {
414 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
415 process()->sink().ClearMessages();
417 content::WebContents* contents = web_contents();
418 SetupMockDelegateAndPolicy(contents);
419 MockSearchIPCRouterPolicy* policy =
420 GetSearchIPCRouterPolicy(contents);
422 GURL item_url("www.foo.com");
423 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(1);
424 EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(1)
425 .WillOnce(testing::Return(true));
427 scoped_ptr<IPC::Message> message(
428 new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
429 contents->GetRoutingID(),
430 contents->GetController().GetVisibleEntry()->GetPageID(),
432 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
435 TEST_F(SearchIPCRouterTest, IgnoreDeleteMostVisitedItemMsg) {
436 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
437 process()->sink().ClearMessages();
439 content::WebContents* contents = web_contents();
440 SetupMockDelegateAndPolicy(contents);
441 MockSearchIPCRouterPolicy* policy =
442 GetSearchIPCRouterPolicy(contents);
444 GURL item_url("www.foo.com");
445 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(0);
446 EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(1)
447 .WillOnce(testing::Return(false));
449 scoped_ptr<IPC::Message> message(
450 new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
451 contents->GetRoutingID(),
452 contents->GetController().GetVisibleEntry()->GetPageID(),
454 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
457 TEST_F(SearchIPCRouterTest, ProcessUndoMostVisitedDeletionMsg) {
458 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
459 process()->sink().ClearMessages();
461 content::WebContents* contents = web_contents();
462 SetupMockDelegateAndPolicy(contents);
463 MockSearchIPCRouterPolicy* policy =
464 GetSearchIPCRouterPolicy(contents);
465 GURL item_url("www.foo.com");
466 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(1);
467 EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(1)
468 .WillOnce(testing::Return(true));
470 scoped_ptr<IPC::Message> message(
471 new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
472 contents->GetRoutingID(),
473 contents->GetController().GetVisibleEntry()->GetPageID(),
475 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
478 TEST_F(SearchIPCRouterTest, IgnoreUndoMostVisitedDeletionMsg) {
479 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
480 process()->sink().ClearMessages();
482 content::WebContents* contents = web_contents();
483 SetupMockDelegateAndPolicy(contents);
484 MockSearchIPCRouterPolicy* policy =
485 GetSearchIPCRouterPolicy(contents);
486 GURL item_url("www.foo.com");
487 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(0);
488 EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(1)
489 .WillOnce(testing::Return(false));
491 scoped_ptr<IPC::Message> message(
492 new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
493 contents->GetRoutingID(),
494 contents->GetController().GetVisibleEntry()->GetPageID(),
496 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
499 TEST_F(SearchIPCRouterTest, ProcessUndoAllMostVisitedDeletionsMsg) {
500 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
501 process()->sink().ClearMessages();
503 content::WebContents* contents = web_contents();
504 SetupMockDelegateAndPolicy(contents);
505 MockSearchIPCRouterPolicy* policy =
506 GetSearchIPCRouterPolicy(contents);
507 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(1);
508 EXPECT_CALL(*policy, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
509 .WillOnce(testing::Return(true));
511 scoped_ptr<IPC::Message> message(
512 new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
513 contents->GetRoutingID(),
514 contents->GetController().GetVisibleEntry()->GetPageID()));
515 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
518 TEST_F(SearchIPCRouterTest, IgnoreUndoAllMostVisitedDeletionsMsg) {
519 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
520 process()->sink().ClearMessages();
522 content::WebContents* contents = web_contents();
523 SetupMockDelegateAndPolicy(contents);
524 MockSearchIPCRouterPolicy* policy =
525 GetSearchIPCRouterPolicy(contents);
526 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
527 EXPECT_CALL(*policy, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
528 .WillOnce(testing::Return(false));
530 scoped_ptr<IPC::Message> message(
531 new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
532 contents->GetRoutingID(),
533 contents->GetController().GetVisibleEntry()->GetPageID()));
534 GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
537 TEST_F(SearchIPCRouterTest, IgnoreMessageIfThePageIsNotActive) {
538 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
539 process()->sink().ClearMessages();
541 content::WebContents* contents = web_contents();
542 SetupMockDelegateAndPolicy(contents);
543 MockSearchIPCRouterPolicy* policy =
544 GetSearchIPCRouterPolicy(contents);
546 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
547 int invalid_page_id = 1000;
548 GURL item_url("www.foo.com");
549 EXPECT_CALL(*mock_delegate(), NavigateToURL(item_url, CURRENT_TAB,
551 EXPECT_CALL(*policy, ShouldProcessNavigateToURL(
552 search_tab_helper->ipc_router().is_active_tab_)).Times(0);
553 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_SearchBoxNavigate(
554 contents->GetRoutingID(), invalid_page_id, item_url, CURRENT_TAB,
556 search_tab_helper->ipc_router().OnMessageReceived(*message);
558 EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(0);
559 EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(0);
560 message.reset(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
561 contents->GetRoutingID(), invalid_page_id, item_url));
562 search_tab_helper->ipc_router().OnMessageReceived(*message);
564 EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(0);
565 EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(0);
566 message.reset(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
567 contents->GetRoutingID(), invalid_page_id, item_url));
568 search_tab_helper->ipc_router().OnMessageReceived(*message);
570 EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
571 EXPECT_CALL(*policy, ShouldProcessUndoAllMostVisitedDeletions()).Times(0);
572 message.reset(new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
573 contents->GetRoutingID(), invalid_page_id));
574 search_tab_helper->ipc_router().OnMessageReceived(*message);
576 EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(0);
577 EXPECT_CALL(*policy, ShouldProcessFocusOmnibox(
578 search_tab_helper->ipc_router().is_active_tab_)).Times(0);
579 message.reset(new ChromeViewHostMsg_FocusOmnibox(
580 contents->GetRoutingID(), invalid_page_id, OMNIBOX_FOCUS_VISIBLE));
581 search_tab_helper->ipc_router().OnMessageReceived(*message);
583 EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER)).Times(0);
584 EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(0);
585 message.reset(new ChromeViewHostMsg_LogEvent(contents->GetRoutingID(),
586 invalid_page_id, NTP_MOUSEOVER));
587 search_tab_helper->ipc_router().OnMessageReceived(*message);
590 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text)).Times(0);
591 EXPECT_CALL(*policy, ShouldProcessPasteIntoOmnibox(
592 search_tab_helper->ipc_router().is_active_tab_)).Times(0);
593 message.reset(new ChromeViewHostMsg_PasteAndOpenDropdown(
594 contents->GetRoutingID(), invalid_page_id, text));
595 search_tab_helper->ipc_router().OnMessageReceived(*message);
598 TEST_F(SearchIPCRouterTest, ProcessPasteAndOpenDropdownMsg) {
599 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
600 process()->sink().ClearMessages();
602 content::WebContents* contents = web_contents();
604 SetupMockDelegateAndPolicy(contents);
605 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(contents);
606 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
607 bool is_active_tab = search_tab_helper->ipc_router().is_active_tab_;
608 EXPECT_TRUE(is_active_tab);
609 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text)).Times(1);
610 EXPECT_CALL(*policy, ShouldProcessPasteIntoOmnibox(is_active_tab)).Times(1)
611 .WillOnce(testing::Return(true));
613 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_PasteAndOpenDropdown(
614 contents->GetRoutingID(),
615 contents->GetController().GetVisibleEntry()->GetPageID(), text));
616 search_tab_helper->ipc_router().OnMessageReceived(*message);
619 TEST_F(SearchIPCRouterTest, IgnorePasteAndOpenDropdownMsg) {
620 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
621 process()->sink().ClearMessages();
623 content::WebContents* contents = web_contents();
625 SetupMockDelegateAndPolicy(contents);
626 MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(contents);
627 SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
628 bool is_active_tab = search_tab_helper->ipc_router().is_active_tab_;
629 EXPECT_TRUE(is_active_tab);
630 EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text)).Times(0);
631 EXPECT_CALL(*policy, ShouldProcessPasteIntoOmnibox(is_active_tab)).Times(1)
632 .WillOnce(testing::Return(false));
634 scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_PasteAndOpenDropdown(
635 contents->GetRoutingID(),
636 contents->GetController().GetVisibleEntry()->GetPageID(), text));
637 search_tab_helper->ipc_router().OnMessageReceived(*message);
640 TEST_F(SearchIPCRouterTest, SendSetPromoInformationMsg) {
641 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
642 process()->sink().ClearMessages();
644 content::WebContents* contents = web_contents();
645 SetupMockDelegateAndPolicy(contents);
646 MockSearchIPCRouterPolicy* policy =
647 GetSearchIPCRouterPolicy(contents);
648 EXPECT_CALL(*policy, ShouldSendSetPromoInformation()).Times(1)
649 .WillOnce(testing::Return(true));
651 GetSearchTabHelper(contents)->ipc_router().SetPromoInformation(true);
652 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID));
655 TEST_F(SearchIPCRouterTest, DoNotSendSetPromoInformationMsg) {
656 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
657 process()->sink().ClearMessages();
659 content::WebContents* contents = web_contents();
660 SetupMockDelegateAndPolicy(contents);
661 MockSearchIPCRouterPolicy* policy =
662 GetSearchIPCRouterPolicy(contents);
663 EXPECT_CALL(*policy, ShouldSendSetPromoInformation()).Times(1)
664 .WillOnce(testing::Return(false));
666 GetSearchTabHelper(contents)->ipc_router().SetPromoInformation(false);
667 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID));
670 TEST_F(SearchIPCRouterTest,
671 SendSetDisplayInstantResultsMsg_EnableInstantOnResultsPage) {
672 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
673 "EmbeddedSearch", "Group1 espv:42 prefetch_results_srp:1"));
674 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
676 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
677 // set to true if the underlying page is a results page and
678 // "prefetch_results_srp" flag is enabled via field trials.
679 VerifyDisplayInstantResultsMsg(true);
682 TEST_F(SearchIPCRouterTest,
683 SendSetDisplayInstantResultsMsg_DisableInstantOnResultsPage) {
684 // |prefetch_results_srp" flag is disabled via field trials.
685 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
686 "EmbeddedSearch", "Group1 espv:42 prefetch_results_srp:0"));
687 NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
689 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
691 VerifyDisplayInstantResultsMsg(false);
694 TEST_F(SearchIPCRouterTest,
695 SendSetDisplayInstantResultsMsg_DisableInstantOutsideResultsPage) {
696 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
697 "EmbeddedSearch", "Group1 espv:42 prefetch_results_srp:1"));
698 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
700 // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
701 // false if the underlying page is not a search results page.
702 VerifyDisplayInstantResultsMsg(false);
705 TEST_F(SearchIPCRouterTest, DoNotSendSetDisplayInstantResultsMsg) {
706 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
707 process()->sink().ClearMessages();
709 content::WebContents* contents = web_contents();
710 SetupMockDelegateAndPolicy(contents);
711 MockSearchIPCRouterPolicy* policy =
712 GetSearchIPCRouterPolicy(contents);
713 EXPECT_CALL(*policy, ShouldSendSetDisplayInstantResults()).Times(1)
714 .WillOnce(testing::Return(false));
716 GetSearchTabHelper(contents)->ipc_router().SetDisplayInstantResults();
717 EXPECT_FALSE(MessageWasSent(
718 ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID));
721 TEST_F(SearchIPCRouterTest, SendSetSuggestionToPrefetch) {
722 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
723 process()->sink().ClearMessages();
725 content::WebContents* contents = web_contents();
726 SetupMockDelegateAndPolicy(contents);
727 MockSearchIPCRouterPolicy* policy =
728 GetSearchIPCRouterPolicy(contents);
729 EXPECT_CALL(*policy, ShouldSendSetSuggestionToPrefetch()).Times(1)
730 .WillOnce(testing::Return(true));
732 GetSearchTabHelper(contents)->SetSuggestionToPrefetch(
733 InstantSuggestion());
734 EXPECT_TRUE(MessageWasSent(
735 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
738 TEST_F(SearchIPCRouterTest, DoNotSendSetSuggestionToPrefetch) {
739 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
740 process()->sink().ClearMessages();
742 content::WebContents* contents = web_contents();
743 SetupMockDelegateAndPolicy(contents);
744 MockSearchIPCRouterPolicy* policy =
745 GetSearchIPCRouterPolicy(contents);
746 EXPECT_CALL(*policy, ShouldSendSetSuggestionToPrefetch()).Times(1)
747 .WillOnce(testing::Return(false));
749 GetSearchTabHelper(contents)->SetSuggestionToPrefetch(
750 InstantSuggestion());
751 EXPECT_FALSE(MessageWasSent(
752 ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
755 TEST_F(SearchIPCRouterTest, SendMostVisitedItemsMsg) {
756 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
757 process()->sink().ClearMessages();
759 content::WebContents* contents = web_contents();
760 SetupMockDelegateAndPolicy(contents);
761 MockSearchIPCRouterPolicy* policy =
762 GetSearchIPCRouterPolicy(contents);
763 EXPECT_CALL(*policy, ShouldSendMostVisitedItems()).Times(1)
764 .WillOnce(testing::Return(true));
766 GetSearchTabHelper(contents)->ipc_router().SendMostVisitedItems(
767 std::vector<InstantMostVisitedItem>());
768 EXPECT_TRUE(MessageWasSent(
769 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
772 TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) {
773 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
774 process()->sink().ClearMessages();
776 content::WebContents* contents = web_contents();
777 SetupMockDelegateAndPolicy(contents);
778 MockSearchIPCRouterPolicy* policy =
779 GetSearchIPCRouterPolicy(contents);
780 EXPECT_CALL(*policy, ShouldSendMostVisitedItems()).Times(1)
781 .WillOnce(testing::Return(false));
783 GetSearchTabHelper(contents)->ipc_router().SendMostVisitedItems(
784 std::vector<InstantMostVisitedItem>());
785 EXPECT_FALSE(MessageWasSent(
786 ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
789 TEST_F(SearchIPCRouterTest, SendThemeBackgroundInfoMsg) {
790 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
791 process()->sink().ClearMessages();
793 content::WebContents* contents = web_contents();
794 SetupMockDelegateAndPolicy(contents);
795 MockSearchIPCRouterPolicy* policy =
796 GetSearchIPCRouterPolicy(contents);
797 EXPECT_CALL(*policy, ShouldSendThemeBackgroundInfo()).Times(1)
798 .WillOnce(testing::Return(true));
800 GetSearchTabHelper(contents)->ipc_router().SendThemeBackgroundInfo(
801 ThemeBackgroundInfo());
802 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
805 TEST_F(SearchIPCRouterTest, DoNotSendThemeBackgroundInfoMsg) {
806 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
807 process()->sink().ClearMessages();
809 content::WebContents* contents = web_contents();
810 SetupMockDelegateAndPolicy(contents);
811 MockSearchIPCRouterPolicy* policy =
812 GetSearchIPCRouterPolicy(contents);
813 EXPECT_CALL(*policy, ShouldSendThemeBackgroundInfo()).Times(1)
814 .WillOnce(testing::Return(false));
816 GetSearchTabHelper(contents)->ipc_router().SendThemeBackgroundInfo(
817 ThemeBackgroundInfo());
818 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
821 TEST_F(SearchIPCRouterTest, SendSubmitMsg) {
822 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
823 process()->sink().ClearMessages();
825 content::WebContents* contents = web_contents();
826 SetupMockDelegateAndPolicy(contents);
827 MockSearchIPCRouterPolicy* policy =
828 GetSearchIPCRouterPolicy(contents);
829 EXPECT_CALL(*policy, ShouldSubmitQuery()).Times(1)
830 .WillOnce(testing::Return(true));
832 GetSearchTabHelper(contents)->ipc_router().Submit(string16());
833 EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));
836 TEST_F(SearchIPCRouterTest, DoNotSendSubmitMsg) {
837 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
838 process()->sink().ClearMessages();
840 content::WebContents* contents = web_contents();
841 SetupMockDelegateAndPolicy(contents);
842 MockSearchIPCRouterPolicy* policy =
843 GetSearchIPCRouterPolicy(contents);
844 EXPECT_CALL(*policy, ShouldSubmitQuery()).Times(1)
845 .WillOnce(testing::Return(false));
847 GetSearchTabHelper(contents)->ipc_router().Submit(string16());
848 EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));