1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/search/search_ipc_router.h"
7 #include "build/build_config.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h"
10 #include "chrome/browser/ui/search/search_tab_helper.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/test/base/browser_with_test_window_test.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 class SearchIPCRouterPolicyTest
: public BrowserWithTestWindowTest
{
20 virtual void SetUp() {
21 BrowserWithTestWindowTest::SetUp();
22 AddTab(browser(), GURL("chrome://blank"));
23 SearchTabHelper::CreateForWebContents(web_contents());
26 content::WebContents
* web_contents() {
27 return browser()->tab_strip_model()->GetActiveWebContents();
30 SearchIPCRouter::Policy
* GetSearchIPCRouterPolicy() {
31 SearchTabHelper
* search_tab_helper
=
32 SearchTabHelper::FromWebContents(web_contents());
33 EXPECT_NE(static_cast<SearchTabHelper
*>(NULL
), search_tab_helper
);
34 return search_tab_helper
->ipc_router().policy();
37 void SetIncognitoProfile() {
38 SearchIPCRouterPolicyImpl
* policy
=
39 static_cast<SearchIPCRouterPolicyImpl
*>(GetSearchIPCRouterPolicy());
40 policy
->set_is_incognito(true);
44 TEST_F(SearchIPCRouterPolicyTest
, ProcessVoiceSearchSupportMsg
) {
45 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
46 EXPECT_TRUE(GetSearchIPCRouterPolicy()->
47 ShouldProcessSetVoiceSearchSupport());
50 TEST_F(SearchIPCRouterPolicyTest
, ProcessFocusOmnibox
) {
51 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
52 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessFocusOmnibox(true));
55 TEST_F(SearchIPCRouterPolicyTest
, DoNotProcessFocusOmnibox
) {
56 // Process message only if the underlying page is an InstantNTP.
57 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
58 EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldProcessFocusOmnibox(true));
61 TEST_F(SearchIPCRouterPolicyTest
, SendSetPromoInformation
) {
62 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
63 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetPromoInformation());
66 TEST_F(SearchIPCRouterPolicyTest
, DoNotSendSetPromoInformation
) {
67 // Send promo information only if the underlying page is an InstantNTP.
68 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
69 EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldSendSetPromoInformation());
72 TEST_F(SearchIPCRouterPolicyTest
, ProcessDeleteMostVisitedItem
) {
73 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
74 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessDeleteMostVisitedItem());
77 TEST_F(SearchIPCRouterPolicyTest
, ProcessUndoMostVisitedDeletion
) {
78 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
79 EXPECT_TRUE(GetSearchIPCRouterPolicy()->
80 ShouldProcessUndoMostVisitedDeletion());
83 TEST_F(SearchIPCRouterPolicyTest
, ProcessUndoAllMostVisitedDeletions
) {
84 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
85 EXPECT_TRUE(GetSearchIPCRouterPolicy()->
86 ShouldProcessUndoAllMostVisitedDeletions());
89 TEST_F(SearchIPCRouterPolicyTest
, ProcessLogEvent
) {
90 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
91 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessLogEvent());
94 TEST_F(SearchIPCRouterPolicyTest
, DoNotProcessLogEvent
) {
95 // Process message only if the underlying page is an InstantNTP.
96 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
97 EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldProcessLogEvent());
100 TEST_F(SearchIPCRouterPolicyTest
, ProcessChromeIdentityCheck
) {
101 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
102 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessChromeIdentityCheck());
105 TEST_F(SearchIPCRouterPolicyTest
, DoNotProcessChromeIdentityCheck
) {
106 // Process message only if the underlying page is an InstantNTP.
107 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
108 EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldProcessChromeIdentityCheck());
111 TEST_F(SearchIPCRouterPolicyTest
, ProcessNavigateToURL
) {
112 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
113 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessNavigateToURL(true));
116 TEST_F(SearchIPCRouterPolicyTest
, ProcessPasteIntoOmniboxMsg
) {
117 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
118 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessPasteIntoOmnibox(true));
121 TEST_F(SearchIPCRouterPolicyTest
, DoNotProcessPasteIntoOmniboxMsg
) {
122 // Process message only if the current tab is an Instant NTP.
123 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
124 EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldProcessPasteIntoOmnibox(true));
127 TEST_F(SearchIPCRouterPolicyTest
, DoNotProcessMessagesForIncognitoPage
) {
128 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
129 SetIncognitoProfile();
131 SearchIPCRouter::Policy
* router_policy
= GetSearchIPCRouterPolicy();
132 EXPECT_FALSE(router_policy
->ShouldProcessFocusOmnibox(true));
133 EXPECT_FALSE(router_policy
->ShouldProcessNavigateToURL(true));
134 EXPECT_FALSE(router_policy
->ShouldProcessDeleteMostVisitedItem());
135 EXPECT_FALSE(router_policy
->ShouldProcessUndoMostVisitedDeletion());
136 EXPECT_FALSE(router_policy
->ShouldProcessUndoAllMostVisitedDeletions());
137 EXPECT_FALSE(router_policy
->ShouldProcessLogEvent());
138 EXPECT_FALSE(router_policy
->ShouldProcessPasteIntoOmnibox(true));
141 TEST_F(SearchIPCRouterPolicyTest
, DoNotProcessMessagesForInactiveTab
) {
142 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
144 // Assume the NTP is deactivated.
145 SearchIPCRouter::Policy
* router_policy
= GetSearchIPCRouterPolicy();
146 EXPECT_FALSE(router_policy
->ShouldProcessFocusOmnibox(false));
147 EXPECT_FALSE(router_policy
->ShouldProcessNavigateToURL(false));
148 EXPECT_FALSE(router_policy
->ShouldProcessPasteIntoOmnibox(false));
151 TEST_F(SearchIPCRouterPolicyTest
, SendSetDisplayInstantResults
) {
152 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
153 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetDisplayInstantResults());
156 TEST_F(SearchIPCRouterPolicyTest
, SendSetSuggestionToPrefetch
) {
157 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
158 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetSuggestionToPrefetch());
161 TEST_F(SearchIPCRouterPolicyTest
, SendSetOmniboxStartMargin
) {
162 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
163 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetOmniboxStartMargin());
166 TEST_F(SearchIPCRouterPolicyTest
,
167 DoNotSendSetMessagesForIncognitoPage
) {
168 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
169 SetIncognitoProfile();
171 SearchIPCRouter::Policy
* router_policy
= GetSearchIPCRouterPolicy();
172 EXPECT_FALSE(router_policy
->ShouldSendSetSuggestionToPrefetch());
173 EXPECT_FALSE(router_policy
->ShouldSendSetDisplayInstantResults());
174 EXPECT_FALSE(router_policy
->ShouldSendSetPromoInformation());
175 EXPECT_FALSE(router_policy
->ShouldSendThemeBackgroundInfo());
176 EXPECT_FALSE(router_policy
->ShouldSendMostVisitedItems());
179 TEST_F(SearchIPCRouterPolicyTest
,
180 AppropriateMessagesSentToIncognitoPages
) {
181 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
182 SetIncognitoProfile();
184 SearchIPCRouter::Policy
* router_policy
= GetSearchIPCRouterPolicy();
185 EXPECT_TRUE(router_policy
->ShouldSubmitQuery());
186 EXPECT_TRUE(router_policy
->ShouldSendToggleVoiceSearch());
187 EXPECT_TRUE(router_policy
->ShouldSendSetOmniboxStartMargin());
190 TEST_F(SearchIPCRouterPolicyTest
, SendMostVisitedItems
) {
191 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
192 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendMostVisitedItems());
195 TEST_F(SearchIPCRouterPolicyTest
, DoNotSendMostVisitedItems
) {
196 // Send most visited items only if the current tab is an Instant NTP.
197 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
198 EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldSendMostVisitedItems());
201 TEST_F(SearchIPCRouterPolicyTest
, SendThemeBackgroundInfo
) {
202 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
203 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendThemeBackgroundInfo());
206 TEST_F(SearchIPCRouterPolicyTest
, DoNotSendThemeBackgroundInfo
) {
207 // Send theme background information only if the current tab is an
209 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
210 EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldSendThemeBackgroundInfo());
213 TEST_F(SearchIPCRouterPolicyTest
, SubmitQuery
) {
214 NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
215 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSubmitQuery());
218 TEST_F(SearchIPCRouterPolicyTest
, SendToggleVoiceSearch
) {
219 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl
));
220 EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendToggleVoiceSearch());