Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search / search_ipc_router_policy_unittest.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/search/search_ipc_router.h"
6
7 #include "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"
16 #include "url/gurl.h"
17
18 class SearchIPCRouterPolicyTest : public BrowserWithTestWindowTest {
19  public:
20   void SetUp() override {
21     BrowserWithTestWindowTest::SetUp();
22     AddTab(browser(), GURL("chrome://blank"));
23     SearchTabHelper::CreateForWebContents(web_contents());
24   }
25
26   content::WebContents* web_contents() {
27     return browser()->tab_strip_model()->GetActiveWebContents();
28   }
29
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_for_testing();
35   }
36
37   void SetIncognitoProfile() {
38     SearchIPCRouterPolicyImpl* policy =
39         static_cast<SearchIPCRouterPolicyImpl*>(GetSearchIPCRouterPolicy());
40     policy->set_is_incognito(true);
41   }
42 };
43
44 TEST_F(SearchIPCRouterPolicyTest, ProcessVoiceSearchSupportMsg) {
45   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
46   EXPECT_TRUE(GetSearchIPCRouterPolicy()->
47       ShouldProcessSetVoiceSearchSupport());
48 }
49
50 TEST_F(SearchIPCRouterPolicyTest, ProcessFocusOmnibox) {
51   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
52   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessFocusOmnibox(true));
53 }
54
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));
59 }
60
61 TEST_F(SearchIPCRouterPolicyTest, SendSetPromoInformation) {
62   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
63   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetPromoInformation());
64 }
65
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());
70 }
71
72 TEST_F(SearchIPCRouterPolicyTest, ProcessDeleteMostVisitedItem) {
73   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
74   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessDeleteMostVisitedItem());
75 }
76
77 TEST_F(SearchIPCRouterPolicyTest, ProcessUndoMostVisitedDeletion) {
78   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
79   EXPECT_TRUE(GetSearchIPCRouterPolicy()->
80       ShouldProcessUndoMostVisitedDeletion());
81 }
82
83 TEST_F(SearchIPCRouterPolicyTest, ProcessUndoAllMostVisitedDeletions) {
84   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
85   EXPECT_TRUE(GetSearchIPCRouterPolicy()->
86       ShouldProcessUndoAllMostVisitedDeletions());
87 }
88
89 TEST_F(SearchIPCRouterPolicyTest, ProcessLogEvent) {
90   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
91   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessLogEvent());
92 }
93
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());
98 }
99
100 TEST_F(SearchIPCRouterPolicyTest, ProcessChromeIdentityCheck) {
101   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
102   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessChromeIdentityCheck());
103 }
104
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());
109 }
110
111 TEST_F(SearchIPCRouterPolicyTest, ProcessNavigateToURL) {
112   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
113   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessNavigateToURL(true));
114 }
115
116 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessNavigateToURL) {
117   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
118   EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldProcessNavigateToURL(true));
119
120   NavigateAndCommitActiveTab(GURL("file://foo/bar"));
121   EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldProcessNavigateToURL(true));
122 }
123
124 TEST_F(SearchIPCRouterPolicyTest, ProcessPasteIntoOmniboxMsg) {
125   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
126   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldProcessPasteIntoOmnibox(true));
127 }
128
129 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessPasteIntoOmniboxMsg) {
130   // Process message only if the current tab is an Instant NTP.
131   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
132   EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldProcessPasteIntoOmnibox(true));
133 }
134
135 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessMessagesForIncognitoPage) {
136   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
137   SetIncognitoProfile();
138
139   SearchIPCRouter::Policy* router_policy = GetSearchIPCRouterPolicy();
140   EXPECT_FALSE(router_policy->ShouldProcessFocusOmnibox(true));
141   EXPECT_FALSE(router_policy->ShouldProcessNavigateToURL(true));
142   EXPECT_FALSE(router_policy->ShouldProcessDeleteMostVisitedItem());
143   EXPECT_FALSE(router_policy->ShouldProcessUndoMostVisitedDeletion());
144   EXPECT_FALSE(router_policy->ShouldProcessUndoAllMostVisitedDeletions());
145   EXPECT_FALSE(router_policy->ShouldProcessLogEvent());
146   EXPECT_FALSE(router_policy->ShouldProcessPasteIntoOmnibox(true));
147 }
148
149 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessMessagesForInactiveTab) {
150   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
151
152   // Assume the NTP is deactivated.
153   SearchIPCRouter::Policy* router_policy = GetSearchIPCRouterPolicy();
154   EXPECT_FALSE(router_policy->ShouldProcessFocusOmnibox(false));
155   EXPECT_FALSE(router_policy->ShouldProcessNavigateToURL(false));
156   EXPECT_FALSE(router_policy->ShouldProcessPasteIntoOmnibox(false));
157   EXPECT_FALSE(router_policy->ShouldSendSetInputInProgress(false));
158 }
159
160 TEST_F(SearchIPCRouterPolicyTest, SendSetDisplayInstantResults) {
161   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
162   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetDisplayInstantResults());
163 }
164
165 TEST_F(SearchIPCRouterPolicyTest, SendSetSuggestionToPrefetch) {
166   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
167   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetSuggestionToPrefetch());
168 }
169
170 TEST_F(SearchIPCRouterPolicyTest, SendSetOmniboxStartMargin) {
171   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
172   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendSetOmniboxStartMargin());
173 }
174
175 TEST_F(SearchIPCRouterPolicyTest,
176        DoNotSendSetMessagesForIncognitoPage) {
177   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
178   SetIncognitoProfile();
179
180   SearchIPCRouter::Policy* router_policy = GetSearchIPCRouterPolicy();
181   EXPECT_FALSE(router_policy->ShouldSendSetSuggestionToPrefetch());
182   EXPECT_FALSE(router_policy->ShouldSendSetDisplayInstantResults());
183   EXPECT_FALSE(router_policy->ShouldSendSetPromoInformation());
184   EXPECT_FALSE(router_policy->ShouldSendThemeBackgroundInfo());
185   EXPECT_FALSE(router_policy->ShouldSendMostVisitedItems());
186   EXPECT_FALSE(router_policy->ShouldSendSetInputInProgress(true));
187   EXPECT_FALSE(router_policy->ShouldSendOmniboxFocusChanged());
188 }
189
190 TEST_F(SearchIPCRouterPolicyTest,
191        AppropriateMessagesSentToIncognitoPages) {
192   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
193   SetIncognitoProfile();
194
195   SearchIPCRouter::Policy* router_policy = GetSearchIPCRouterPolicy();
196   EXPECT_TRUE(router_policy->ShouldSubmitQuery());
197   EXPECT_TRUE(router_policy->ShouldSendToggleVoiceSearch());
198   EXPECT_TRUE(router_policy->ShouldSendSetOmniboxStartMargin());
199 }
200
201 TEST_F(SearchIPCRouterPolicyTest, SendMostVisitedItems) {
202   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
203   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendMostVisitedItems());
204 }
205
206 TEST_F(SearchIPCRouterPolicyTest, DoNotSendMostVisitedItems) {
207   // Send most visited items only if the current tab is an Instant NTP.
208   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
209   EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldSendMostVisitedItems());
210 }
211
212 TEST_F(SearchIPCRouterPolicyTest, SendThemeBackgroundInfo) {
213   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
214   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendThemeBackgroundInfo());
215 }
216
217 TEST_F(SearchIPCRouterPolicyTest, DoNotSendThemeBackgroundInfo) {
218   // Send theme background information only if the current tab is an
219   // Instant NTP.
220   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
221   EXPECT_FALSE(GetSearchIPCRouterPolicy()->ShouldSendThemeBackgroundInfo());
222 }
223
224 TEST_F(SearchIPCRouterPolicyTest, SubmitQuery) {
225   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
226   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSubmitQuery());
227 }
228
229 TEST_F(SearchIPCRouterPolicyTest, SendToggleVoiceSearch) {
230   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
231   EXPECT_TRUE(GetSearchIPCRouterPolicy()->ShouldSendToggleVoiceSearch());
232 }