• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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_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:
~MockSearchIPCRouterDelegate()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_METHOD2(OnLogMostVisitedImpression,
56                void(int position, const base::string16& provider));
57   MOCK_METHOD2(OnLogMostVisitedNavigation,
58                void(int position, const base::string16& provider));
59   MOCK_METHOD1(PasteIntoOmnibox, void(const base::string16&));
60   MOCK_METHOD1(OnChromeIdentityCheck, void(const base::string16& identity));
61 };
62 
63 class MockSearchIPCRouterPolicy : public SearchIPCRouter::Policy {
64  public:
~MockSearchIPCRouterPolicy()65   virtual ~MockSearchIPCRouterPolicy() {}
66 
67   MOCK_METHOD0(ShouldProcessSetVoiceSearchSupport, bool());
68   MOCK_METHOD1(ShouldProcessFocusOmnibox, bool(bool));
69   MOCK_METHOD1(ShouldProcessNavigateToURL, bool(bool));
70   MOCK_METHOD0(ShouldProcessDeleteMostVisitedItem, bool());
71   MOCK_METHOD0(ShouldProcessUndoMostVisitedDeletion, bool());
72   MOCK_METHOD0(ShouldProcessUndoAllMostVisitedDeletions, bool());
73   MOCK_METHOD0(ShouldProcessLogEvent, bool());
74   MOCK_METHOD1(ShouldProcessPasteIntoOmnibox, bool(bool));
75   MOCK_METHOD0(ShouldProcessChromeIdentityCheck, bool());
76   MOCK_METHOD0(ShouldSendSetPromoInformation, bool());
77   MOCK_METHOD0(ShouldSendSetDisplayInstantResults, bool());
78   MOCK_METHOD0(ShouldSendSetSuggestionToPrefetch, bool());
79   MOCK_METHOD0(ShouldSendSetOmniboxStartMargin, bool());
80   MOCK_METHOD1(ShouldSendSetInputInProgress, bool(bool));
81   MOCK_METHOD0(ShouldSendOmniboxFocusChanged, bool());
82   MOCK_METHOD0(ShouldSendMostVisitedItems, bool());
83   MOCK_METHOD0(ShouldSendThemeBackgroundInfo, bool());
84   MOCK_METHOD0(ShouldSendToggleVoiceSearch, bool());
85   MOCK_METHOD0(ShouldSubmitQuery, bool());
86 };
87 
88 }  // namespace
89 
90 class SearchIPCRouterTest : public BrowserWithTestWindowTest {
91  public:
SearchIPCRouterTest()92   SearchIPCRouterTest() : field_trial_list_(NULL) {}
93 
SetUp()94   virtual void SetUp() {
95     BrowserWithTestWindowTest::SetUp();
96     AddTab(browser(), GURL("chrome://blank"));
97     SearchTabHelper::CreateForWebContents(web_contents());
98 
99     TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
100         profile(),
101         &TemplateURLServiceFactory::BuildInstanceFor);
102     TemplateURLService* template_url_service =
103         TemplateURLServiceFactory::GetForProfile(profile());
104     ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
105 
106     TemplateURLData data;
107     data.SetURL("http://foo.com/url?bar={searchTerms}");
108     data.instant_url = "http://foo.com/instant?"
109         "{google:omniboxStartMarginParameter}foo=foo#foo=foo&espv";
110     data.new_tab_url = "https://foo.com/newtab?espv";
111     data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}");
112     data.search_terms_replacement_key = "espv";
113 
114     TemplateURL* template_url = new TemplateURL(data);
115     // Takes ownership of |template_url|.
116     template_url_service->Add(template_url);
117     template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
118     process()->sink().ClearMessages();
119   }
120 
web_contents()121   content::WebContents* web_contents() {
122     return browser()->tab_strip_model()->GetActiveWebContents();
123   }
124 
process()125   content::MockRenderProcessHost* process() {
126     return static_cast<content::MockRenderProcessHost*>(
127         web_contents()->GetRenderViewHost()->GetProcess());
128   }
129 
GetSearchTabHelper(content::WebContents * web_contents)130   SearchTabHelper* GetSearchTabHelper(
131       content::WebContents* web_contents) {
132     EXPECT_NE(static_cast<content::WebContents*>(NULL), web_contents);
133     return SearchTabHelper::FromWebContents(web_contents);
134   }
135 
SetupMockDelegateAndPolicy()136   void SetupMockDelegateAndPolicy() {
137     content::WebContents* contents = web_contents();
138     ASSERT_NE(static_cast<content::WebContents*>(NULL), contents);
139     SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
140     ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
141     search_tab_helper->ipc_router().set_delegate_for_testing(mock_delegate());
142     search_tab_helper->ipc_router().set_policy_for_testing(
143         make_scoped_ptr(new MockSearchIPCRouterPolicy)
144             .PassAs<SearchIPCRouter::Policy>());
145   }
146 
MessageWasSent(uint32 id)147   bool MessageWasSent(uint32 id) {
148     return process()->sink().GetFirstMessageMatching(id) != NULL;
149   }
150 
VerifyDisplayInstantResultsMsg(bool expected_param_value)151   void VerifyDisplayInstantResultsMsg(bool expected_param_value) {
152     SetupMockDelegateAndPolicy();
153     MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
154     EXPECT_CALL(*policy, ShouldSendSetDisplayInstantResults()).Times(1)
155         .WillOnce(testing::Return(true));
156 
157     GetSearchIPCRouter().SetDisplayInstantResults();
158     const IPC::Message* message = process()->sink().GetFirstMessageMatching(
159         ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID);
160     EXPECT_NE(static_cast<const IPC::Message*>(NULL), message);
161     Tuple1<bool> display_instant_results_param;
162     ChromeViewMsg_SearchBoxSetDisplayInstantResults::Read(
163         message, &display_instant_results_param);
164     EXPECT_EQ(expected_param_value, display_instant_results_param.a);
165   }
166 
mock_delegate()167   MockSearchIPCRouterDelegate* mock_delegate() { return &delegate_; }
168 
GetSearchIPCRouterPolicy()169   MockSearchIPCRouterPolicy* GetSearchIPCRouterPolicy() {
170     content::WebContents* contents = web_contents();
171     EXPECT_NE(static_cast<content::WebContents*>(NULL), contents);
172     SearchTabHelper* search_tab_helper = GetSearchTabHelper(contents);
173     EXPECT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
174     return static_cast<MockSearchIPCRouterPolicy*>(
175         search_tab_helper->ipc_router().policy_for_testing());
176   }
177 
GetSearchIPCRouter()178   SearchIPCRouter& GetSearchIPCRouter() {
179     return GetSearchTabHelper(web_contents())->ipc_router();
180   }
181 
GetSearchIPCRouterSeqNo()182   int GetSearchIPCRouterSeqNo() {
183     return GetSearchIPCRouter().page_seq_no_for_testing();
184   }
185 
OnMessageReceived(const IPC::Message & message)186   void OnMessageReceived(const IPC::Message& message) {
187     bool should_handle_message =
188         chrome::IsRenderedInInstantProcess(web_contents(), profile());
189     bool handled = GetSearchIPCRouter().OnMessageReceived(message);
190     ASSERT_EQ(should_handle_message, handled);
191   }
192 
IsActiveTab(content::WebContents * contents)193   bool IsActiveTab(content::WebContents* contents) {
194     return GetSearchTabHelper(contents)->ipc_router().is_active_tab_;
195   }
196 
197  private:
198   MockSearchIPCRouterDelegate delegate_;
199   base::FieldTrialList field_trial_list_;
200 };
201 
TEST_F(SearchIPCRouterTest,IgnoreMessagesFromNonInstantRenderers)202 TEST_F(SearchIPCRouterTest, IgnoreMessagesFromNonInstantRenderers) {
203   NavigateAndCommitActiveTab(GURL("file://foo/bar"));
204   SetupMockDelegateAndPolicy();
205   GURL destination_url("www.foo.com");
206   EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url, CURRENT_TAB,
207                                               true)).Times(0);
208   content::WebContents* contents = web_contents();
209   bool is_active_tab = IsActiveTab(contents);
210   EXPECT_TRUE(is_active_tab);
211 
212   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
213   EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(0);
214 
215   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_SearchBoxNavigate(
216       contents->GetRoutingID(),
217       GetSearchIPCRouterSeqNo(),
218       destination_url, CURRENT_TAB, true));
219   OnMessageReceived(*message);
220 }
221 
TEST_F(SearchIPCRouterTest,ProcessVoiceSearchSupportMsg)222 TEST_F(SearchIPCRouterTest, ProcessVoiceSearchSupportMsg) {
223   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
224   SetupMockDelegateAndPolicy();
225   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
226   EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(1);
227   EXPECT_CALL(*(policy), ShouldProcessSetVoiceSearchSupport()).Times(1)
228       .WillOnce(testing::Return(true));
229 
230   content::WebContents* contents = web_contents();
231   scoped_ptr<IPC::Message> message(
232       new ChromeViewHostMsg_SetVoiceSearchSupported(
233           contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), true));
234   OnMessageReceived(*message);
235 }
236 
TEST_F(SearchIPCRouterTest,IgnoreVoiceSearchSupportMsg)237 TEST_F(SearchIPCRouterTest, IgnoreVoiceSearchSupportMsg) {
238   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
239   EXPECT_CALL(*mock_delegate(), OnSetVoiceSearchSupport(true)).Times(0);
240   SetupMockDelegateAndPolicy();
241   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
242   EXPECT_CALL(*policy, ShouldProcessSetVoiceSearchSupport()).Times(1)
243       .WillOnce(testing::Return(false));
244 
245   content::WebContents* contents = web_contents();
246   scoped_ptr<IPC::Message> message(
247       new ChromeViewHostMsg_SetVoiceSearchSupported(
248           contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), true));
249   OnMessageReceived(*message);
250 }
251 
TEST_F(SearchIPCRouterTest,ProcessFocusOmniboxMsg)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);
257 
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));
263 
264   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_FocusOmnibox(
265       contents->GetRoutingID(),
266       GetSearchIPCRouterSeqNo(),
267       OMNIBOX_FOCUS_VISIBLE));
268   OnMessageReceived(*message);
269 }
270 
TEST_F(SearchIPCRouterTest,IgnoreFocusOmniboxMsg)271 TEST_F(SearchIPCRouterTest, IgnoreFocusOmniboxMsg) {
272   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
273   SetupMockDelegateAndPolicy();
274   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
275   EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(0);
276 
277   content::WebContents* contents = web_contents();
278   bool is_active_tab = IsActiveTab(contents);
279   EXPECT_TRUE(is_active_tab);
280   EXPECT_CALL(*policy, ShouldProcessFocusOmnibox(is_active_tab)).Times(1)
281       .WillOnce(testing::Return(false));
282 
283   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_FocusOmnibox(
284       contents->GetRoutingID(),
285       GetSearchIPCRouterSeqNo(),
286       OMNIBOX_FOCUS_VISIBLE));
287   OnMessageReceived(*message);
288 }
289 
TEST_F(SearchIPCRouterTest,HandleTabChangedEvents)290 TEST_F(SearchIPCRouterTest, HandleTabChangedEvents) {
291   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
292   content::WebContents* contents = web_contents();
293   EXPECT_EQ(0, browser()->tab_strip_model()->GetIndexOfWebContents(contents));
294   EXPECT_TRUE(IsActiveTab(contents));
295 
296   // Add a new tab to deactivate the current tab.
297   AddTab(browser(), GURL(url::kAboutBlankURL));
298   EXPECT_EQ(2, browser()->tab_strip_model()->count());
299   EXPECT_EQ(1, browser()->tab_strip_model()->GetIndexOfWebContents(contents));
300   EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
301   EXPECT_FALSE(IsActiveTab(contents));
302 
303   // Activate the first tab.
304   browser()->tab_strip_model()->ActivateTabAt(1, false);
305   EXPECT_EQ(browser()->tab_strip_model()->active_index(),
306             browser()->tab_strip_model()->GetIndexOfWebContents(contents));
307   EXPECT_TRUE(IsActiveTab(contents));
308 }
309 
TEST_F(SearchIPCRouterTest,ProcessNavigateToURLMsg)310 TEST_F(SearchIPCRouterTest, ProcessNavigateToURLMsg) {
311   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
312   SetupMockDelegateAndPolicy();
313   GURL destination_url("www.foo.com");
314   EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url, CURRENT_TAB,
315                                               true)).Times(1);
316   content::WebContents* contents = web_contents();
317   bool is_active_tab = IsActiveTab(contents);
318   EXPECT_TRUE(is_active_tab);
319 
320   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
321   EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(1)
322       .WillOnce(testing::Return(true));
323 
324   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_SearchBoxNavigate(
325       contents->GetRoutingID(),
326       GetSearchIPCRouterSeqNo(),
327       destination_url, CURRENT_TAB, true));
328   OnMessageReceived(*message);
329 }
330 
TEST_F(SearchIPCRouterTest,IgnoreNavigateToURLMsg)331 TEST_F(SearchIPCRouterTest, IgnoreNavigateToURLMsg) {
332   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
333   SetupMockDelegateAndPolicy();
334   GURL destination_url("www.foo.com");
335   EXPECT_CALL(*mock_delegate(), NavigateToURL(destination_url, CURRENT_TAB,
336                                               true)).Times(0);
337   content::WebContents* contents = web_contents();
338   bool is_active_tab = IsActiveTab(contents);
339   EXPECT_TRUE(is_active_tab);
340 
341   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
342   EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(1)
343       .WillOnce(testing::Return(false));
344 
345   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_SearchBoxNavigate(
346       contents->GetRoutingID(),
347       GetSearchIPCRouterSeqNo(),
348       destination_url, CURRENT_TAB, true));
349   OnMessageReceived(*message);
350 }
351 
TEST_F(SearchIPCRouterTest,ProcessLogEventMsg)352 TEST_F(SearchIPCRouterTest, ProcessLogEventMsg) {
353   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
354   SetupMockDelegateAndPolicy();
355   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
356   EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER)).Times(1);
357   EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1)
358       .WillOnce(testing::Return(true));
359 
360   content::WebContents* contents = web_contents();
361   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_LogEvent(
362       contents->GetRoutingID(), GetSearchIPCRouterSeqNo(),
363       NTP_MOUSEOVER));
364   OnMessageReceived(*message);
365 }
366 
TEST_F(SearchIPCRouterTest,IgnoreLogEventMsg)367 TEST_F(SearchIPCRouterTest, IgnoreLogEventMsg) {
368   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
369   SetupMockDelegateAndPolicy();
370   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
371   EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER)).Times(0);
372   EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1)
373       .WillOnce(testing::Return(false));
374 
375   content::WebContents* contents = web_contents();
376   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_LogEvent(
377       contents->GetRoutingID(), GetSearchIPCRouterSeqNo(),
378       NTP_MOUSEOVER));
379   OnMessageReceived(*message);
380 }
381 
TEST_F(SearchIPCRouterTest,ProcessLogMostVisitedImpressionMsg)382 TEST_F(SearchIPCRouterTest, ProcessLogMostVisitedImpressionMsg) {
383   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
384   SetupMockDelegateAndPolicy();
385   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
386   EXPECT_CALL(*mock_delegate(),
387       OnLogMostVisitedImpression(3, base::ASCIIToUTF16("Server"))).Times(1);
388   EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1)
389       .WillOnce(testing::Return(true));
390 
391   content::WebContents* contents = web_contents();
392   scoped_ptr<IPC::Message> message(
393       new ChromeViewHostMsg_LogMostVisitedImpression(
394       contents->GetRoutingID(),
395       GetSearchIPCRouterSeqNo(),
396       3,
397       base::ASCIIToUTF16("Server")));
398   OnMessageReceived(*message);
399 }
400 
TEST_F(SearchIPCRouterTest,ProcessLogMostVisitedNavigationMsg)401 TEST_F(SearchIPCRouterTest, ProcessLogMostVisitedNavigationMsg) {
402   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
403   SetupMockDelegateAndPolicy();
404   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
405   EXPECT_CALL(*mock_delegate(),
406       OnLogMostVisitedNavigation(3, base::ASCIIToUTF16("Server"))).Times(1);
407   EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(1)
408       .WillOnce(testing::Return(true));
409 
410   content::WebContents* contents = web_contents();
411   scoped_ptr<IPC::Message> message(
412       new ChromeViewHostMsg_LogMostVisitedNavigation(
413       contents->GetRoutingID(),
414       GetSearchIPCRouterSeqNo(),
415       3,
416       base::ASCIIToUTF16("Server")));
417   OnMessageReceived(*message);
418 }
419 
TEST_F(SearchIPCRouterTest,ProcessChromeIdentityCheckMsg)420 TEST_F(SearchIPCRouterTest, ProcessChromeIdentityCheckMsg) {
421   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
422   SetupMockDelegateAndPolicy();
423   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
424   const base::string16 test_identity = base::ASCIIToUTF16("foo@bar.com");
425   EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity)).Times(1);
426   EXPECT_CALL(*policy, ShouldProcessChromeIdentityCheck()).Times(1)
427       .WillOnce(testing::Return(true));
428 
429   content::WebContents* contents = web_contents();
430   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck(
431       contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), test_identity));
432   OnMessageReceived(*message);
433 }
434 
TEST_F(SearchIPCRouterTest,IgnoreChromeIdentityCheckMsg)435 TEST_F(SearchIPCRouterTest, IgnoreChromeIdentityCheckMsg) {
436   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
437   SetupMockDelegateAndPolicy();
438   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
439 
440   const base::string16 test_identity = base::ASCIIToUTF16("foo@bar.com");
441   EXPECT_CALL(*mock_delegate(), OnChromeIdentityCheck(test_identity)).Times(0);
442   EXPECT_CALL(*policy, ShouldProcessChromeIdentityCheck()).Times(1)
443       .WillOnce(testing::Return(false));
444 
445   content::WebContents* contents = web_contents();
446   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_ChromeIdentityCheck(
447       contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), test_identity));
448   OnMessageReceived(*message);
449 }
450 
TEST_F(SearchIPCRouterTest,ProcessDeleteMostVisitedItemMsg)451 TEST_F(SearchIPCRouterTest, ProcessDeleteMostVisitedItemMsg) {
452   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
453   SetupMockDelegateAndPolicy();
454   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
455   GURL item_url("www.foo.com");
456   EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(1);
457   EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(1)
458       .WillOnce(testing::Return(true));
459 
460   content::WebContents* contents = web_contents();
461   scoped_ptr<IPC::Message> message(
462       new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
463           contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url));
464   OnMessageReceived(*message);
465 }
466 
TEST_F(SearchIPCRouterTest,IgnoreDeleteMostVisitedItemMsg)467 TEST_F(SearchIPCRouterTest, IgnoreDeleteMostVisitedItemMsg) {
468   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
469   SetupMockDelegateAndPolicy();
470   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
471   GURL item_url("www.foo.com");
472   EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(0);
473   EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(1)
474       .WillOnce(testing::Return(false));
475 
476   content::WebContents* contents = web_contents();
477   scoped_ptr<IPC::Message> message(
478       new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
479           contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url));
480   OnMessageReceived(*message);
481 }
482 
TEST_F(SearchIPCRouterTest,ProcessUndoMostVisitedDeletionMsg)483 TEST_F(SearchIPCRouterTest, ProcessUndoMostVisitedDeletionMsg) {
484   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
485   SetupMockDelegateAndPolicy();
486   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
487   GURL item_url("www.foo.com");
488   EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(1);
489   EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(1)
490       .WillOnce(testing::Return(true));
491 
492   content::WebContents* contents = web_contents();
493   scoped_ptr<IPC::Message> message(
494       new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
495           contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url));
496   OnMessageReceived(*message);
497 }
498 
TEST_F(SearchIPCRouterTest,IgnoreUndoMostVisitedDeletionMsg)499 TEST_F(SearchIPCRouterTest, IgnoreUndoMostVisitedDeletionMsg) {
500   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
501   SetupMockDelegateAndPolicy();
502   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
503   GURL item_url("www.foo.com");
504   EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(0);
505   EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(1)
506       .WillOnce(testing::Return(false));
507 
508   content::WebContents* contents = web_contents();
509   scoped_ptr<IPC::Message> message(
510       new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
511           contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), item_url));
512   OnMessageReceived(*message);
513 }
514 
TEST_F(SearchIPCRouterTest,ProcessUndoAllMostVisitedDeletionsMsg)515 TEST_F(SearchIPCRouterTest, ProcessUndoAllMostVisitedDeletionsMsg) {
516   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
517   SetupMockDelegateAndPolicy();
518   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
519   EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(1);
520   EXPECT_CALL(*policy, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
521       .WillOnce(testing::Return(true));
522 
523   content::WebContents* contents = web_contents();
524   scoped_ptr<IPC::Message> message(
525       new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
526           contents->GetRoutingID(), GetSearchIPCRouterSeqNo()));
527   OnMessageReceived(*message);
528 }
529 
TEST_F(SearchIPCRouterTest,IgnoreUndoAllMostVisitedDeletionsMsg)530 TEST_F(SearchIPCRouterTest, IgnoreUndoAllMostVisitedDeletionsMsg) {
531   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
532   SetupMockDelegateAndPolicy();
533   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
534   EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
535   EXPECT_CALL(*policy, ShouldProcessUndoAllMostVisitedDeletions()).Times(1)
536       .WillOnce(testing::Return(false));
537 
538   content::WebContents* contents = web_contents();
539   scoped_ptr<IPC::Message> message(
540       new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
541           contents->GetRoutingID(), GetSearchIPCRouterSeqNo()));
542   OnMessageReceived(*message);
543 }
544 
TEST_F(SearchIPCRouterTest,IgnoreMessageIfThePageIsNotActive)545 TEST_F(SearchIPCRouterTest, IgnoreMessageIfThePageIsNotActive) {
546   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
547   SetupMockDelegateAndPolicy();
548   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
549   int page_seq_no = GetSearchIPCRouterSeqNo();
550 
551   content::WebContents* contents = web_contents();
552   bool is_active_tab = IsActiveTab(contents);
553   GURL item_url("www.foo.com");
554   EXPECT_CALL(*mock_delegate(), NavigateToURL(item_url, CURRENT_TAB,
555                                               true)).Times(0);
556   // At this point, in a real test, the navigation would cause the
557   // SearchIPCRouter's page sequence to advance. In this test it doesn't, so
558   // we'll decrement the sequence number on this side to simulate it.
559   --page_seq_no;
560   EXPECT_CALL(*policy, ShouldProcessNavigateToURL(is_active_tab)).Times(0);
561 
562   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_SearchBoxNavigate(
563       contents->GetRoutingID(), page_seq_no, item_url,
564       CURRENT_TAB, true));
565   OnMessageReceived(*message);
566 
567   EXPECT_CALL(*mock_delegate(), OnDeleteMostVisitedItem(item_url)).Times(0);
568   EXPECT_CALL(*policy, ShouldProcessDeleteMostVisitedItem()).Times(0);
569   message.reset(new ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem(
570       contents->GetRoutingID(), page_seq_no, item_url));
571   OnMessageReceived(*message);
572 
573   EXPECT_CALL(*mock_delegate(), OnUndoMostVisitedDeletion(item_url)).Times(0);
574   EXPECT_CALL(*policy, ShouldProcessUndoMostVisitedDeletion()).Times(0);
575   message.reset(new ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion(
576       contents->GetRoutingID(), page_seq_no, item_url));
577   OnMessageReceived(*message);
578 
579   EXPECT_CALL(*mock_delegate(), OnUndoAllMostVisitedDeletions()).Times(0);
580   EXPECT_CALL(*policy, ShouldProcessUndoAllMostVisitedDeletions()).Times(0);
581   message.reset(new ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions(
582       contents->GetRoutingID(), page_seq_no));
583   OnMessageReceived(*message);
584 
585   EXPECT_CALL(*mock_delegate(), FocusOmnibox(OMNIBOX_FOCUS_VISIBLE)).Times(0);
586   EXPECT_CALL(*policy, ShouldProcessFocusOmnibox(is_active_tab)).Times(0);
587   message.reset(new ChromeViewHostMsg_FocusOmnibox(
588       contents->GetRoutingID(), page_seq_no,
589       OMNIBOX_FOCUS_VISIBLE));
590   OnMessageReceived(*message);
591 
592   EXPECT_CALL(*mock_delegate(), OnLogEvent(NTP_MOUSEOVER)).Times(0);
593   EXPECT_CALL(*policy, ShouldProcessLogEvent()).Times(0);
594   message.reset(new ChromeViewHostMsg_LogEvent(contents->GetRoutingID(),
595                                                page_seq_no,
596                                                NTP_MOUSEOVER));
597   OnMessageReceived(*message);
598 
599   base::string16 text;
600   EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text)).Times(0);
601   EXPECT_CALL(*policy, ShouldProcessPasteIntoOmnibox(is_active_tab)).Times(0);
602   message.reset(new ChromeViewHostMsg_PasteAndOpenDropdown(
603       contents->GetRoutingID(), page_seq_no, text));
604   OnMessageReceived(*message);
605 }
606 
TEST_F(SearchIPCRouterTest,ProcessPasteAndOpenDropdownMsg)607 TEST_F(SearchIPCRouterTest, ProcessPasteAndOpenDropdownMsg) {
608   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
609   SetupMockDelegateAndPolicy();
610   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
611 
612   content::WebContents* contents = web_contents();
613   bool is_active_tab = IsActiveTab(contents);
614   EXPECT_TRUE(is_active_tab);
615 
616   base::string16 text;
617   EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text)).Times(1);
618   EXPECT_CALL(*policy, ShouldProcessPasteIntoOmnibox(is_active_tab)).Times(1)
619       .WillOnce(testing::Return(true));
620   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_PasteAndOpenDropdown(
621       contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), text));
622   OnMessageReceived(*message);
623 }
624 
TEST_F(SearchIPCRouterTest,IgnorePasteAndOpenDropdownMsg)625 TEST_F(SearchIPCRouterTest, IgnorePasteAndOpenDropdownMsg) {
626   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
627   SetupMockDelegateAndPolicy();
628   base::string16 text;
629   EXPECT_CALL(*mock_delegate(), PasteIntoOmnibox(text)).Times(0);
630 
631   content::WebContents* contents = web_contents();
632   bool is_active_tab = IsActiveTab(contents);
633   EXPECT_TRUE(is_active_tab);
634 
635   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
636   EXPECT_CALL(*policy, ShouldProcessPasteIntoOmnibox(is_active_tab)).Times(1)
637       .WillOnce(testing::Return(false));
638 
639   scoped_ptr<IPC::Message> message(new ChromeViewHostMsg_PasteAndOpenDropdown(
640       contents->GetRoutingID(), GetSearchIPCRouterSeqNo(), text));
641   OnMessageReceived(*message);
642 }
643 
TEST_F(SearchIPCRouterTest,SendSetPromoInformationMsg)644 TEST_F(SearchIPCRouterTest, SendSetPromoInformationMsg) {
645   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
646   SetupMockDelegateAndPolicy();
647   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
648   EXPECT_CALL(*policy, ShouldSendSetPromoInformation()).Times(1)
649       .WillOnce(testing::Return(true));
650 
651   GetSearchIPCRouter().SetPromoInformation(true);
652   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID));
653 }
654 
TEST_F(SearchIPCRouterTest,DoNotSendSetPromoInformationMsg)655 TEST_F(SearchIPCRouterTest, DoNotSendSetPromoInformationMsg) {
656   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
657   SetupMockDelegateAndPolicy();
658   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
659   EXPECT_CALL(*policy, ShouldSendSetPromoInformation()).Times(1)
660       .WillOnce(testing::Return(false));
661 
662   GetSearchIPCRouter().SetPromoInformation(false);
663   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxPromoInformation::ID));
664 }
665 
TEST_F(SearchIPCRouterTest,SendSetDisplayInstantResultsMsg_EnableInstantOnResultsPage)666 TEST_F(SearchIPCRouterTest,
667        SendSetDisplayInstantResultsMsg_EnableInstantOnResultsPage) {
668   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
669       "EmbeddedSearch",
670       "Group1 espv:42 query_extraction:1 prefetch_results_srp:1"));
671   NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
672 
673   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
674   // set to true if the underlying page is a results page and
675   // "prefetch_results_srp" flag is enabled via field trials.
676   VerifyDisplayInstantResultsMsg(true);
677 }
678 
TEST_F(SearchIPCRouterTest,SendSetDisplayInstantResultsMsg_DisableInstantOnResultsPage)679 TEST_F(SearchIPCRouterTest,
680        SendSetDisplayInstantResultsMsg_DisableInstantOnResultsPage) {
681   // |prefetch_results_srp" flag is disabled via field trials.
682   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
683       "EmbeddedSearch",
684       "Group1 espv:42 query_extraction:1 prefetch_results_srp:0"));
685   NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
686 
687   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
688   // set to false.
689   VerifyDisplayInstantResultsMsg(false);
690 }
691 
TEST_F(SearchIPCRouterTest,SendSetDisplayInstantResultsMsg_EnableInstantOutsideSearchResultsPage)692 TEST_F(SearchIPCRouterTest,
693        SendSetDisplayInstantResultsMsg_EnableInstantOutsideSearchResultsPage) {
694   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
695   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
696   // true if the underlying page is not a search results page.
697   VerifyDisplayInstantResultsMsg(true);
698 }
699 
TEST_F(SearchIPCRouterTest,DoNotSendSetDisplayInstantResultsMsg)700 TEST_F(SearchIPCRouterTest, DoNotSendSetDisplayInstantResultsMsg) {
701   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
702   SetupMockDelegateAndPolicy();
703   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
704   EXPECT_CALL(*policy, ShouldSendSetDisplayInstantResults()).Times(1)
705       .WillOnce(testing::Return(false));
706 
707   process()->sink().ClearMessages();
708   GetSearchIPCRouter().SetDisplayInstantResults();
709   EXPECT_FALSE(MessageWasSent(
710       ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID));
711 }
712 
TEST_F(SearchIPCRouterTest,SendSetSuggestionToPrefetch)713 TEST_F(SearchIPCRouterTest, SendSetSuggestionToPrefetch) {
714   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
715   SetupMockDelegateAndPolicy();
716   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
717   EXPECT_CALL(*policy, ShouldSendSetSuggestionToPrefetch()).Times(1)
718       .WillOnce(testing::Return(true));
719 
720   process()->sink().ClearMessages();
721   content::WebContents* contents = web_contents();
722   GetSearchTabHelper(contents)->SetSuggestionToPrefetch(InstantSuggestion());
723   EXPECT_TRUE(MessageWasSent(
724       ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
725 }
726 
TEST_F(SearchIPCRouterTest,DoNotSendSetSuggestionToPrefetch)727 TEST_F(SearchIPCRouterTest, DoNotSendSetSuggestionToPrefetch) {
728   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
729   SetupMockDelegateAndPolicy();
730   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
731   EXPECT_CALL(*policy, ShouldSendSetSuggestionToPrefetch()).Times(1)
732       .WillOnce(testing::Return(false));
733 
734   process()->sink().ClearMessages();
735   content::WebContents* contents = web_contents();
736   GetSearchTabHelper(contents)->SetSuggestionToPrefetch(InstantSuggestion());
737   EXPECT_FALSE(MessageWasSent(
738       ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
739 }
740 
TEST_F(SearchIPCRouterTest,SendSetOmniboxStartMargin)741 TEST_F(SearchIPCRouterTest, SendSetOmniboxStartMargin) {
742   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
743   SetupMockDelegateAndPolicy();
744   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
745   EXPECT_CALL(*policy, ShouldSendSetOmniboxStartMargin()).Times(1)
746       .WillOnce(testing::Return(true));
747 
748   process()->sink().ClearMessages();
749   GetSearchIPCRouter().SetOmniboxStartMargin(92);
750   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID));
751 }
752 
TEST_F(SearchIPCRouterTest,DoNotSendSetOmniboxStartMargin)753 TEST_F(SearchIPCRouterTest, DoNotSendSetOmniboxStartMargin) {
754   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
755   SetupMockDelegateAndPolicy();
756   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
757   EXPECT_CALL(*policy, ShouldSendSetOmniboxStartMargin()).Times(1)
758       .WillOnce(testing::Return(false));
759 
760   process()->sink().ClearMessages();
761   GetSearchIPCRouter().SetOmniboxStartMargin(92);
762   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID));
763 }
764 
TEST_F(SearchIPCRouterTest,SendOmniboxFocusChange)765 TEST_F(SearchIPCRouterTest, SendOmniboxFocusChange) {
766   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
767   SetupMockDelegateAndPolicy();
768   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
769   EXPECT_CALL(*policy, ShouldSendOmniboxFocusChanged()).Times(1)
770       .WillOnce(testing::Return(true));
771 
772   process()->sink().ClearMessages();
773   GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE,
774                                            OMNIBOX_FOCUS_CHANGE_EXPLICIT);
775   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID));
776 }
777 
TEST_F(SearchIPCRouterTest,DoNotSendOmniboxFocusChange)778 TEST_F(SearchIPCRouterTest, DoNotSendOmniboxFocusChange) {
779   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
780   SetupMockDelegateAndPolicy();
781   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
782   EXPECT_CALL(*policy, ShouldSendOmniboxFocusChanged()).Times(1)
783       .WillOnce(testing::Return(false));
784 
785   process()->sink().ClearMessages();
786   GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE,
787                                            OMNIBOX_FOCUS_CHANGE_EXPLICIT);
788   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID));
789 }
790 
TEST_F(SearchIPCRouterTest,SendSetInputInProgress)791 TEST_F(SearchIPCRouterTest, SendSetInputInProgress) {
792   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
793   SetupMockDelegateAndPolicy();
794   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
795   EXPECT_CALL(*policy, ShouldSendSetInputInProgress(true)).Times(1)
796       .WillOnce(testing::Return(true));
797 
798   process()->sink().ClearMessages();
799   GetSearchIPCRouter().SetInputInProgress(true);
800   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID));
801 }
802 
TEST_F(SearchIPCRouterTest,DoNotSendSetInputInProgress)803 TEST_F(SearchIPCRouterTest, DoNotSendSetInputInProgress) {
804   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
805   SetupMockDelegateAndPolicy();
806   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
807   EXPECT_CALL(*policy, ShouldSendSetInputInProgress(true)).Times(1)
808       .WillOnce(testing::Return(false));
809 
810   process()->sink().ClearMessages();
811   GetSearchIPCRouter().SetInputInProgress(true);
812   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID));
813 }
814 
TEST_F(SearchIPCRouterTest,SendMostVisitedItemsMsg)815 TEST_F(SearchIPCRouterTest, SendMostVisitedItemsMsg) {
816   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
817   SetupMockDelegateAndPolicy();
818   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
819   EXPECT_CALL(*policy, ShouldSendMostVisitedItems()).Times(1)
820       .WillOnce(testing::Return(true));
821 
822   process()->sink().ClearMessages();
823   GetSearchIPCRouter().SendMostVisitedItems(
824       std::vector<InstantMostVisitedItem>());
825   EXPECT_TRUE(MessageWasSent(
826       ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
827 }
828 
TEST_F(SearchIPCRouterTest,DoNotSendMostVisitedItemsMsg)829 TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) {
830   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
831   SetupMockDelegateAndPolicy();
832   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
833   EXPECT_CALL(*policy, ShouldSendMostVisitedItems()).Times(1)
834       .WillOnce(testing::Return(false));
835 
836   process()->sink().ClearMessages();
837   GetSearchIPCRouter().SendMostVisitedItems(
838       std::vector<InstantMostVisitedItem>());
839   EXPECT_FALSE(MessageWasSent(
840       ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
841 }
842 
TEST_F(SearchIPCRouterTest,SendThemeBackgroundInfoMsg)843 TEST_F(SearchIPCRouterTest, SendThemeBackgroundInfoMsg) {
844   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
845   SetupMockDelegateAndPolicy();
846   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
847   EXPECT_CALL(*policy, ShouldSendThemeBackgroundInfo()).Times(1)
848       .WillOnce(testing::Return(true));
849 
850   process()->sink().ClearMessages();
851   GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
852   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
853 }
854 
TEST_F(SearchIPCRouterTest,DoNotSendThemeBackgroundInfoMsg)855 TEST_F(SearchIPCRouterTest, DoNotSendThemeBackgroundInfoMsg) {
856   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
857   SetupMockDelegateAndPolicy();
858   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
859   EXPECT_CALL(*policy, ShouldSendThemeBackgroundInfo()).Times(1)
860       .WillOnce(testing::Return(false));
861 
862   process()->sink().ClearMessages();
863   GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
864   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
865 }
866 
TEST_F(SearchIPCRouterTest,SendSubmitMsg)867 TEST_F(SearchIPCRouterTest, SendSubmitMsg) {
868   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
869   SetupMockDelegateAndPolicy();
870   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
871   EXPECT_CALL(*policy, ShouldSubmitQuery()).Times(1)
872       .WillOnce(testing::Return(true));
873 
874   process()->sink().ClearMessages();
875   GetSearchIPCRouter().Submit(base::string16());
876   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));
877 }
878 
TEST_F(SearchIPCRouterTest,DoNotSendSubmitMsg)879 TEST_F(SearchIPCRouterTest, DoNotSendSubmitMsg) {
880   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
881   SetupMockDelegateAndPolicy();
882   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
883   EXPECT_CALL(*policy, ShouldSubmitQuery()).Times(1)
884       .WillOnce(testing::Return(false));
885 
886   process()->sink().ClearMessages();
887   GetSearchIPCRouter().Submit(base::string16());
888   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));
889 }
890 
TEST_F(SearchIPCRouterTest,SendToggleVoiceSearch)891 TEST_F(SearchIPCRouterTest, SendToggleVoiceSearch) {
892   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
893   SetupMockDelegateAndPolicy();
894   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
895   EXPECT_CALL(*policy, ShouldSendToggleVoiceSearch()).Times(1)
896       .WillOnce(testing::Return(true));
897 
898   process()->sink().ClearMessages();
899   GetSearchIPCRouter().ToggleVoiceSearch();
900   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID));
901 }
902 
TEST_F(SearchIPCRouterTest,DoNotSendToggleVoiceSearch)903 TEST_F(SearchIPCRouterTest, DoNotSendToggleVoiceSearch) {
904   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
905   SetupMockDelegateAndPolicy();
906   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
907   EXPECT_CALL(*policy, ShouldSendToggleVoiceSearch()).Times(1)
908       .WillOnce(testing::Return(false));
909 
910   process()->sink().ClearMessages();
911   GetSearchIPCRouter().ToggleVoiceSearch();
912   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID));
913 }
914