- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search / search_ipc_router_unittest.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/search/search_ipc_router.h"
6
7 #include <vector>
8
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"
39 #include "url/gurl.h"
40
41 namespace {
42
43 class MockSearchIPCRouterDelegate : public SearchIPCRouter::Delegate {
44  public:
45   virtual ~MockSearchIPCRouterDelegate() {}
46
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));
57 };
58
59 class MockSearchIPCRouterPolicy : public SearchIPCRouter::Policy {
60  public:
61   virtual ~MockSearchIPCRouterPolicy() {}
62
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());
78 };
79
80 }  // namespace
81
82 class SearchIPCRouterTest : public BrowserWithTestWindowTest {
83  public:
84   SearchIPCRouterTest() : field_trial_list_(NULL) {}
85
86   virtual void SetUp() {
87     BrowserWithTestWindowTest::SetUp();
88     AddTab(browser(), GURL("chrome://blank"));
89     SearchTabHelper::CreateForWebContents(web_contents());
90
91     TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
92         profile(),
93         &TemplateURLServiceFactory::BuildInstanceFor);
94     TemplateURLService* template_url_service =
95         TemplateURLServiceFactory::GetForProfile(profile());
96     ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
97
98     TemplateURLData data;
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";
105
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);
110   }
111
112   content::WebContents* web_contents() {
113     return browser()->tab_strip_model()->GetActiveWebContents();
114   }
115
116   content::MockRenderProcessHost* process() {
117     return static_cast<content::MockRenderProcessHost*>(
118         web_contents()->GetRenderViewHost()->GetProcess());
119   }
120
121   SearchTabHelper* GetSearchTabHelper(
122       content::WebContents* web_contents) {
123     EXPECT_NE(static_cast<content::WebContents*>(NULL), web_contents);
124     return SearchTabHelper::FromWebContents(web_contents);
125   }
126
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>());
136   }
137
138   bool MessageWasSent(uint32 id) {
139     return process()->sink().GetFirstMessageMatching(id) != NULL;
140   }
141
142   void VerifyDisplayInstantResultsMsg(bool expected_param_value) {
143     process()->sink().ClearMessages();
144
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));
151
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);
160   }
161
162   MockSearchIPCRouterDelegate* mock_delegate() { return &delegate_; }
163
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());
172   }
173
174  private:
175   MockSearchIPCRouterDelegate delegate_;
176   base::FieldTrialList field_trial_list_;
177 };
178
179 TEST_F(SearchIPCRouterTest, ProcessVoiceSearchSupportMsg) {
180   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
181   process()->sink().ClearMessages();
182
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));
190
191   scoped_ptr<IPC::Message> message(
192       new ChromeViewHostMsg_SetVoiceSearchSupported(
193           contents->GetRoutingID(),
194           contents->GetController().GetVisibleEntry()->GetPageID(),
195           true));
196   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
197 }
198
199 TEST_F(SearchIPCRouterTest, IgnoreVoiceSearchSupportMsg) {
200   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
201   process()->sink().ClearMessages();
202
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));
210
211   scoped_ptr<IPC::Message> message(
212       new ChromeViewHostMsg_SetVoiceSearchSupported(
213           contents->GetRoutingID(),
214           contents->GetController().GetVisibleEntry()->GetPageID(),
215           true));
216   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
217 }
218
219 TEST_F(SearchIPCRouterTest, ProcessFocusOmniboxMsg) {
220   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
221   process()->sink().ClearMessages();
222
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);
228
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));
234
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);
240 }
241
242 TEST_F(SearchIPCRouterTest, IgnoreFocusOmniboxMsg) {
243   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
244   process()->sink().ClearMessages();
245
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);
251
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));
257
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);
263 }
264
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_);
271
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_);
278
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_);
284 }
285
286 TEST_F(SearchIPCRouterTest, ProcessNavigateToURLMsg) {
287   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
288   process()->sink().ClearMessages();
289
290   content::WebContents* contents = web_contents();
291   SetupMockDelegateAndPolicy(contents);
292   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(contents);
293
294   GURL destination_url("www.foo.com");
295   EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url, CURRENT_TAB,
296                                               true)).Times(1);
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));
302
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);
308 }
309
310 TEST_F(SearchIPCRouterTest, IgnoreNavigateToURLMsg) {
311   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
312   process()->sink().ClearMessages();
313   GURL destination_url("www.foo.com");
314
315   content::WebContents* contents = web_contents();
316   SetupMockDelegateAndPolicy(contents);
317   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy(contents);
318   EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url, CURRENT_TAB,
319                                               true)).Times(0);
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));
325
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);
331 }
332
333 TEST_F(SearchIPCRouterTest, ProcessLogEventMsg) {
334   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
335   process()->sink().ClearMessages();
336   EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER)).Times(1);
337
338   content::WebContents* contents = web_contents();
339   SetupMockDelegateAndPolicy(contents);
340   MockSearchIPCRouterPolicy* policy =
341       GetSearchIPCRouterPolicy(contents);
342
343   EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1)
344       .WillOnce(testing::Return(true));
345
346   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_LogEvent(
347       contents->GetRoutingID(),
348       contents->GetController().GetVisibleEntry()->GetPageID(),
349       NTP_MOUSEOVER));
350   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
351 }
352
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);
357
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));
364
365   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_LogEvent(
366       contents->GetRoutingID(),
367       contents->GetController().GetVisibleEntry()->GetPageID(),
368       NTP_MOUSEOVER));
369   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
370 }
371
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);
377
378   content::WebContents* contents = web_contents();
379   SetupMockDelegateAndPolicy(contents);
380   MockSearchIPCRouterPolicy* policy =
381       GetSearchIPCRouterPolicy(contents);
382
383   EXPECT_CALL(*policy, ShouldProcessChromeIdentityCheck()).Times(1)
384       .WillOnce(testing::Return(true));
385
386   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck(
387       contents->GetRoutingID(),
388       contents->GetController().GetVisibleEntry()->GetPageID(),
389       test_identity));
390   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
391 }
392
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);
398
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));
405
406   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck(
407       contents->GetRoutingID(),
408       contents->GetController().GetVisibleEntry()->GetPageID(),
409       test_identity));
410   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
411 }
412
413 TEST_F(SearchIPCRouterTest, ProcessDeleteMostVisitedItemMsg) {
414   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
415   process()->sink().ClearMessages();
416
417   content::WebContents* contents = web_contents();
418   SetupMockDelegateAndPolicy(contents);
419   MockSearchIPCRouterPolicy* policy =
420       GetSearchIPCRouterPolicy(contents);
421
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));
426
427   scoped_ptr<IPC::Message> message(
428       new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
429           contents->GetRoutingID(),
430           contents->GetController().GetVisibleEntry()->GetPageID(),
431           item_url));
432   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
433 }
434
435 TEST_F(SearchIPCRouterTest, IgnoreDeleteMostVisitedItemMsg) {
436   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
437   process()->sink().ClearMessages();
438
439   content::WebContents* contents = web_contents();
440   SetupMockDelegateAndPolicy(contents);
441   MockSearchIPCRouterPolicy* policy =
442       GetSearchIPCRouterPolicy(contents);
443
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));
448
449   scoped_ptr<IPC::Message> message(
450       new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
451           contents->GetRoutingID(),
452           contents->GetController().GetVisibleEntry()->GetPageID(),
453           item_url));
454   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
455 }
456
457 TEST_F(SearchIPCRouterTest, ProcessUndoMostVisitedDeletionMsg) {
458   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
459   process()->sink().ClearMessages();
460
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));
469
470   scoped_ptr<IPC::Message> message(
471       new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
472           contents->GetRoutingID(),
473           contents->GetController().GetVisibleEntry()->GetPageID(),
474           item_url));
475   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
476 }
477
478 TEST_F(SearchIPCRouterTest, IgnoreUndoMostVisitedDeletionMsg) {
479   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
480   process()->sink().ClearMessages();
481
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));
490
491   scoped_ptr<IPC::Message> message(
492       new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
493           contents->GetRoutingID(),
494           contents->GetController().GetVisibleEntry()->GetPageID(),
495           item_url));
496   GetSearchTabHelper(contents)->ipc_router().OnMessageReceived(*message);
497 }
498
499 TEST_F(SearchIPCRouterTest, ProcessUndoAllMostVisitedDeletionsMsg) {
500   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
501   process()->sink().ClearMessages();
502
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));
510
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);
516 }
517
518 TEST_F(SearchIPCRouterTest, IgnoreUndoAllMostVisitedDeletionsMsg) {
519   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
520   process()->sink().ClearMessages();
521
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));
529
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);
535 }
536
537 TEST_F(SearchIPCRouterTest, IgnoreMessageIfThePageIsNotActive) {
538   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
539   process()->sink().ClearMessages();
540
541   content::WebContents* contents = web_contents();
542   SetupMockDelegateAndPolicy(contents);
543   MockSearchIPCRouterPolicy* policy =
544       GetSearchIPCRouterPolicy(contents);
545
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,
550                                               true)).Times(0);
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,
555       true));
556   search_tab_helper->ipc_router().OnMessageReceived(*message);
557
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);
563
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);
569
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);
575
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);
582
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);
588
589   string16 text;
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);
596 }
597
598 TEST_F(SearchIPCRouterTest, ProcessPasteAndOpenDropdownMsg) {
599   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
600   process()->sink().ClearMessages();
601
602   content::WebContents* contents = web_contents();
603   string16 text;
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));
612
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);
617 }
618
619 TEST_F(SearchIPCRouterTest, IgnorePasteAndOpenDropdownMsg) {
620   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
621   process()->sink().ClearMessages();
622
623   content::WebContents* contents = web_contents();
624   string16 text;
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));
633
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);
638 }
639
640 TEST_F(SearchIPCRouterTest, SendSetPromoInformationMsg) {
641   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
642   process()->sink().ClearMessages();
643
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));
650
651   GetSearchTabHelper(contents)->ipc_router().SetPromoInformation(true);
652   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID));
653 }
654
655 TEST_F(SearchIPCRouterTest, DoNotSendSetPromoInformationMsg) {
656   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
657   process()->sink().ClearMessages();
658
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));
665
666   GetSearchTabHelper(contents)->ipc_router().SetPromoInformation(false);
667   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID));
668 }
669
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"));
675
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);
680 }
681
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"));
688
689   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
690   // set to false.
691   VerifyDisplayInstantResultsMsg(false);
692 }
693
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));
699
700   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
701   // false if the underlying page is not a search results page.
702   VerifyDisplayInstantResultsMsg(false);
703 }
704
705 TEST_F(SearchIPCRouterTest, DoNotSendSetDisplayInstantResultsMsg) {
706   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
707   process()->sink().ClearMessages();
708
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));
715
716   GetSearchTabHelper(contents)->ipc_router().SetDisplayInstantResults();
717   EXPECT_FALSE(MessageWasSent(
718       ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID));
719 }
720
721 TEST_F(SearchIPCRouterTest, SendSetSuggestionToPrefetch) {
722   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
723   process()->sink().ClearMessages();
724
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));
731
732   GetSearchTabHelper(contents)->SetSuggestionToPrefetch(
733       InstantSuggestion());
734   EXPECT_TRUE(MessageWasSent(
735       ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
736 }
737
738 TEST_F(SearchIPCRouterTest, DoNotSendSetSuggestionToPrefetch) {
739   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
740   process()->sink().ClearMessages();
741
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));
748
749   GetSearchTabHelper(contents)->SetSuggestionToPrefetch(
750       InstantSuggestion());
751   EXPECT_FALSE(MessageWasSent(
752       ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
753 }
754
755 TEST_F(SearchIPCRouterTest, SendMostVisitedItemsMsg) {
756   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
757   process()->sink().ClearMessages();
758
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));
765
766   GetSearchTabHelper(contents)->ipc_router().SendMostVisitedItems(
767       std::vector<InstantMostVisitedItem>());
768   EXPECT_TRUE(MessageWasSent(
769       ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
770 }
771
772 TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) {
773   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
774   process()->sink().ClearMessages();
775
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));
782
783   GetSearchTabHelper(contents)->ipc_router().SendMostVisitedItems(
784       std::vector<InstantMostVisitedItem>());
785   EXPECT_FALSE(MessageWasSent(
786       ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
787 }
788
789 TEST_F(SearchIPCRouterTest, SendThemeBackgroundInfoMsg) {
790   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
791   process()->sink().ClearMessages();
792
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));
799
800   GetSearchTabHelper(contents)->ipc_router().SendThemeBackgroundInfo(
801       ThemeBackgroundInfo());
802   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
803 }
804
805 TEST_F(SearchIPCRouterTest, DoNotSendThemeBackgroundInfoMsg) {
806   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
807   process()->sink().ClearMessages();
808
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));
815
816   GetSearchTabHelper(contents)->ipc_router().SendThemeBackgroundInfo(
817       ThemeBackgroundInfo());
818   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
819 }
820
821 TEST_F(SearchIPCRouterTest, SendSubmitMsg) {
822   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
823   process()->sink().ClearMessages();
824
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));
831
832   GetSearchTabHelper(contents)->ipc_router().Submit(string16());
833   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));
834 }
835
836 TEST_F(SearchIPCRouterTest, DoNotSendSubmitMsg) {
837   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
838   process()->sink().ClearMessages();
839
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));
846
847   GetSearchTabHelper(contents)->ipc_router().Submit(string16());
848   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));
849 }