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