Home | History | Annotate | Download | only in search
      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_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:
     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:
     92   SearchIPCRouterTest() : field_trial_list_(NULL) {}
     93 
     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 
    121   content::WebContents* web_contents() {
    122     return browser()->tab_strip_model()->GetActiveWebContents();
    123   }
    124 
    125   content::MockRenderProcessHost* process() {
    126     return static_cast<content::MockRenderProcessHost*>(
    127         web_contents()->GetRenderViewHost()->GetProcess());
    128   }
    129 
    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 
    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 
    147   bool MessageWasSent(uint32 id) {
    148     return process()->sink().GetFirstMessageMatching(id) != NULL;
    149   }
    150 
    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 
    167   MockSearchIPCRouterDelegate* mock_delegate() { return &delegate_; }
    168 
    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 
    178   SearchIPCRouter& GetSearchIPCRouter() {
    179     return GetSearchTabHelper(web_contents())->ipc_router();
    180   }
    181 
    182   int GetSearchIPCRouterSeqNo() {
    183     return GetSearchIPCRouter().page_seq_no_for_testing();
    184   }
    185 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 (at) 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 
    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 (at) 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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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 
    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", "Group1 espv:42 prefetch_results_srp:0"));
    684   NavigateAndCommitActiveTab(GURL("https://foo.com/url?espv&bar=abc"));
    685 
    686   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is
    687   // set to false.
    688   VerifyDisplayInstantResultsMsg(false);
    689 }
    690 
    691 TEST_F(SearchIPCRouterTest,
    692        SendSetDisplayInstantResultsMsg_DisableInstantOutsideResultsPage) {
    693   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
    694       "EmbeddedSearch", "Group1 espv:42 prefetch_results_srp:1"));
    695   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    696 
    697   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
    698   // false if the underlying page is not a search results page.
    699   VerifyDisplayInstantResultsMsg(false);
    700 }
    701 
    702 TEST_F(SearchIPCRouterTest,
    703        SendSetDisplayInstantResultsMsg_InstantSearchEnabled) {
    704   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
    705       "EmbeddedSearch",
    706       "Group1 espv:42 prefetch_results:1 use_cacheable_ntp:1"));
    707   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    708 
    709   // If the "prefetch_results" flag is enabled via field trials, then
    710   // ChromeViewMsg_SearchBoxSetDisplayInstantResults message param is set to
    711   // true irrespective of the underlying page.
    712   VerifyDisplayInstantResultsMsg(true);
    713 }
    714 
    715 TEST_F(SearchIPCRouterTest,
    716        SendSetDisplayInstantResultsMsg_InstantSearchDisabled) {
    717   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
    718       "EmbeddedSearch",
    719       "Group1 espv:42 use_cacheable_ntp:1 prefetch_results:0"));
    720   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    721 
    722   // Make sure ChromeViewMsg_SearchBoxSetDisplayInstantResults param is set to
    723   // false if the "prefetch_results" flag is disabled via field trials.
    724   VerifyDisplayInstantResultsMsg(false);
    725 }
    726 
    727 TEST_F(SearchIPCRouterTest, DoNotSendSetDisplayInstantResultsMsg) {
    728   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    729   SetupMockDelegateAndPolicy();
    730   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    731   EXPECT_CALL(*policy, ShouldSendSetDisplayInstantResults()).Times(1)
    732       .WillOnce(testing::Return(false));
    733 
    734   process()->sink().ClearMessages();
    735   GetSearchIPCRouter().SetDisplayInstantResults();
    736   EXPECT_FALSE(MessageWasSent(
    737       ChromeViewMsg_SearchBoxSetDisplayInstantResults::ID));
    738 }
    739 
    740 TEST_F(SearchIPCRouterTest, SendSetSuggestionToPrefetch) {
    741   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    742   SetupMockDelegateAndPolicy();
    743   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    744   EXPECT_CALL(*policy, ShouldSendSetSuggestionToPrefetch()).Times(1)
    745       .WillOnce(testing::Return(true));
    746 
    747   process()->sink().ClearMessages();
    748   content::WebContents* contents = web_contents();
    749   GetSearchTabHelper(contents)->SetSuggestionToPrefetch(InstantSuggestion());
    750   EXPECT_TRUE(MessageWasSent(
    751       ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
    752 }
    753 
    754 TEST_F(SearchIPCRouterTest, DoNotSendSetSuggestionToPrefetch) {
    755   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    756   SetupMockDelegateAndPolicy();
    757   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    758   EXPECT_CALL(*policy, ShouldSendSetSuggestionToPrefetch()).Times(1)
    759       .WillOnce(testing::Return(false));
    760 
    761   process()->sink().ClearMessages();
    762   content::WebContents* contents = web_contents();
    763   GetSearchTabHelper(contents)->SetSuggestionToPrefetch(InstantSuggestion());
    764   EXPECT_FALSE(MessageWasSent(
    765       ChromeViewMsg_SearchBoxSetSuggestionToPrefetch::ID));
    766 }
    767 
    768 TEST_F(SearchIPCRouterTest, SendSetOmniboxStartMargin) {
    769   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    770   SetupMockDelegateAndPolicy();
    771   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    772   EXPECT_CALL(*policy, ShouldSendSetOmniboxStartMargin()).Times(1)
    773       .WillOnce(testing::Return(true));
    774 
    775   process()->sink().ClearMessages();
    776   GetSearchIPCRouter().SetOmniboxStartMargin(92);
    777   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID));
    778 }
    779 
    780 TEST_F(SearchIPCRouterTest, DoNotSendSetOmniboxStartMargin) {
    781   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    782   SetupMockDelegateAndPolicy();
    783   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    784   EXPECT_CALL(*policy, ShouldSendSetOmniboxStartMargin()).Times(1)
    785       .WillOnce(testing::Return(false));
    786 
    787   process()->sink().ClearMessages();
    788   GetSearchIPCRouter().SetOmniboxStartMargin(92);
    789   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxMarginChange::ID));
    790 }
    791 
    792 TEST_F(SearchIPCRouterTest, SendOmniboxFocusChange) {
    793   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    794   SetupMockDelegateAndPolicy();
    795   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    796   EXPECT_CALL(*policy, ShouldSendOmniboxFocusChanged()).Times(1)
    797       .WillOnce(testing::Return(true));
    798 
    799   process()->sink().ClearMessages();
    800   GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE,
    801                                            OMNIBOX_FOCUS_CHANGE_EXPLICIT);
    802   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID));
    803 }
    804 
    805 TEST_F(SearchIPCRouterTest, DoNotSendOmniboxFocusChange) {
    806   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    807   SetupMockDelegateAndPolicy();
    808   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    809   EXPECT_CALL(*policy, ShouldSendOmniboxFocusChanged()).Times(1)
    810       .WillOnce(testing::Return(false));
    811 
    812   process()->sink().ClearMessages();
    813   GetSearchIPCRouter().OmniboxFocusChanged(OMNIBOX_FOCUS_NONE,
    814                                            OMNIBOX_FOCUS_CHANGE_EXPLICIT);
    815   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxFocusChanged::ID));
    816 }
    817 
    818 TEST_F(SearchIPCRouterTest, SendSetInputInProgress) {
    819   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    820   SetupMockDelegateAndPolicy();
    821   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    822   EXPECT_CALL(*policy, ShouldSendSetInputInProgress(true)).Times(1)
    823       .WillOnce(testing::Return(true));
    824 
    825   process()->sink().ClearMessages();
    826   GetSearchIPCRouter().SetInputInProgress(true);
    827   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID));
    828 }
    829 
    830 TEST_F(SearchIPCRouterTest, DoNotSendSetInputInProgress) {
    831   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    832   SetupMockDelegateAndPolicy();
    833   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    834   EXPECT_CALL(*policy, ShouldSendSetInputInProgress(true)).Times(1)
    835       .WillOnce(testing::Return(false));
    836 
    837   process()->sink().ClearMessages();
    838   GetSearchIPCRouter().SetInputInProgress(true);
    839   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSetInputInProgress::ID));
    840 }
    841 
    842 TEST_F(SearchIPCRouterTest, SendMostVisitedItemsMsg) {
    843   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    844   SetupMockDelegateAndPolicy();
    845   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    846   EXPECT_CALL(*policy, ShouldSendMostVisitedItems()).Times(1)
    847       .WillOnce(testing::Return(true));
    848 
    849   process()->sink().ClearMessages();
    850   GetSearchIPCRouter().SendMostVisitedItems(
    851       std::vector<InstantMostVisitedItem>());
    852   EXPECT_TRUE(MessageWasSent(
    853       ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
    854 }
    855 
    856 TEST_F(SearchIPCRouterTest, DoNotSendMostVisitedItemsMsg) {
    857   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    858   SetupMockDelegateAndPolicy();
    859   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    860   EXPECT_CALL(*policy, ShouldSendMostVisitedItems()).Times(1)
    861       .WillOnce(testing::Return(false));
    862 
    863   process()->sink().ClearMessages();
    864   GetSearchIPCRouter().SendMostVisitedItems(
    865       std::vector<InstantMostVisitedItem>());
    866   EXPECT_FALSE(MessageWasSent(
    867       ChromeViewMsg_SearchBoxMostVisitedItemsChanged::ID));
    868 }
    869 
    870 TEST_F(SearchIPCRouterTest, SendThemeBackgroundInfoMsg) {
    871   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    872   SetupMockDelegateAndPolicy();
    873   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    874   EXPECT_CALL(*policy, ShouldSendThemeBackgroundInfo()).Times(1)
    875       .WillOnce(testing::Return(true));
    876 
    877   process()->sink().ClearMessages();
    878   GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
    879   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
    880 }
    881 
    882 TEST_F(SearchIPCRouterTest, DoNotSendThemeBackgroundInfoMsg) {
    883   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    884   SetupMockDelegateAndPolicy();
    885   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    886   EXPECT_CALL(*policy, ShouldSendThemeBackgroundInfo()).Times(1)
    887       .WillOnce(testing::Return(false));
    888 
    889   process()->sink().ClearMessages();
    890   GetSearchIPCRouter().SendThemeBackgroundInfo(ThemeBackgroundInfo());
    891   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxThemeChanged::ID));
    892 }
    893 
    894 TEST_F(SearchIPCRouterTest, SendSubmitMsg) {
    895   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    896   SetupMockDelegateAndPolicy();
    897   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    898   EXPECT_CALL(*policy, ShouldSubmitQuery()).Times(1)
    899       .WillOnce(testing::Return(true));
    900 
    901   process()->sink().ClearMessages();
    902   GetSearchIPCRouter().Submit(base::string16());
    903   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));
    904 }
    905 
    906 TEST_F(SearchIPCRouterTest, DoNotSendSubmitMsg) {
    907   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
    908   SetupMockDelegateAndPolicy();
    909   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    910   EXPECT_CALL(*policy, ShouldSubmitQuery()).Times(1)
    911       .WillOnce(testing::Return(false));
    912 
    913   process()->sink().ClearMessages();
    914   GetSearchIPCRouter().Submit(base::string16());
    915   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxSubmit::ID));
    916 }
    917 
    918 TEST_F(SearchIPCRouterTest, SendToggleVoiceSearch) {
    919   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    920   SetupMockDelegateAndPolicy();
    921   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    922   EXPECT_CALL(*policy, ShouldSendToggleVoiceSearch()).Times(1)
    923       .WillOnce(testing::Return(true));
    924 
    925   process()->sink().ClearMessages();
    926   GetSearchIPCRouter().ToggleVoiceSearch();
    927   EXPECT_TRUE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID));
    928 }
    929 
    930 TEST_F(SearchIPCRouterTest, DoNotSendToggleVoiceSearch) {
    931   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
    932   SetupMockDelegateAndPolicy();
    933   MockSearchIPCRouterPolicy* policy = GetSearchIPCRouterPolicy();
    934   EXPECT_CALL(*policy, ShouldSendToggleVoiceSearch()).Times(1)
    935       .WillOnce(testing::Return(false));
    936 
    937   process()->sink().ClearMessages();
    938   GetSearchIPCRouter().ToggleVoiceSearch();
    939   EXPECT_FALSE(MessageWasSent(ChromeViewMsg_SearchBoxToggleVoiceSearch::ID));
    940 }
    941