- add sources.
[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   virtual void SetUp() {
21     CommandLine::ForCurrentProcess()->AppendSwitch(
22         switches::kEnableInstantExtendedAPI);
23     BrowserWithTestWindowTest::SetUp();
24     AddTab(browser(), GURL("chrome://blank"));
25     SearchTabHelper::CreateForWebContents(web_contents());
26   }
27
28   SearchTabHelper* GetSearchTabHelper() {
29     SearchTabHelper* search_tab_helper =
30         SearchTabHelper::FromWebContents(web_contents());
31     EXPECT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
32     return search_tab_helper;
33   }
34
35   content::WebContents* web_contents() {
36     return browser()->tab_strip_model()->GetActiveWebContents();
37   }
38 };
39
40 TEST_F(SearchIPCRouterPolicyTest, ProcessVoiceSearchSupportMsg) {
41   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
42   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
43       ShouldProcessSetVoiceSearchSupport());
44 }
45
46 TEST_F(SearchIPCRouterPolicyTest, ProcessFocusOmnibox) {
47   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
48   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
49   EXPECT_TRUE(search_tab_helper->ipc_router().policy()->
50       ShouldProcessFocusOmnibox(true));
51 }
52
53 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessFocusOmnibox) {
54   // Process message only if the underlying page is an InstantNTP.
55   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
56   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
57   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
58       ShouldProcessFocusOmnibox(true));
59 }
60
61 TEST_F(SearchIPCRouterPolicyTest, SendSetPromoInformation) {
62   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
63   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
64       ShouldSendSetPromoInformation());
65 }
66
67 TEST_F(SearchIPCRouterPolicyTest, DoNotSendSetPromoInformation) {
68   // Send promo information only if the underlying page is an InstantNTP.
69   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
70   EXPECT_FALSE(GetSearchTabHelper()->ipc_router().policy()->
71       ShouldSendSetPromoInformation());
72 }
73
74 TEST_F(SearchIPCRouterPolicyTest, ProcessDeleteMostVisitedItem) {
75   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
76   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
77       ShouldProcessDeleteMostVisitedItem());
78 }
79
80 TEST_F(SearchIPCRouterPolicyTest, ProcessUndoMostVisitedDeletion) {
81   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
82   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
83       ShouldProcessUndoMostVisitedDeletion());
84 }
85
86 TEST_F(SearchIPCRouterPolicyTest, ProcessUndoAllMostVisitedDeletions) {
87   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
88   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
89       ShouldProcessUndoAllMostVisitedDeletions());
90 }
91
92 TEST_F(SearchIPCRouterPolicyTest, ProcessLogEvent) {
93   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
94   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
95       ShouldProcessLogEvent());
96 }
97
98 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessLogEvent) {
99   // Process message only if the underlying page is an InstantNTP.
100   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
101   EXPECT_FALSE(GetSearchTabHelper()->ipc_router().policy()->
102       ShouldProcessLogEvent());
103 }
104
105 TEST_F(SearchIPCRouterPolicyTest, ProcessChromeIdentityCheck) {
106   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
107   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
108       ShouldProcessChromeIdentityCheck());
109 }
110
111 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessChromeIdentityCheck) {
112   // Process message only if the underlying page is an InstantNTP.
113   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
114   EXPECT_FALSE(GetSearchTabHelper()->ipc_router().policy()->
115       ShouldProcessChromeIdentityCheck());
116 }
117
118 TEST_F(SearchIPCRouterPolicyTest, ProcessNavigateToURL) {
119   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
120   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
121       ShouldProcessNavigateToURL(true));
122 }
123
124 TEST_F(SearchIPCRouterPolicyTest, ProcessPasteIntoOmniboxMsg) {
125   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
126   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
127       ShouldProcessPasteIntoOmnibox(true));
128 }
129
130 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessPasteIntoOmniboxMsg) {
131   // Process message only if the current tab is an Instant NTP.
132   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
133   EXPECT_FALSE(GetSearchTabHelper()->ipc_router().policy()->
134       ShouldProcessPasteIntoOmnibox(true));
135 }
136
137 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessMessagesForIncognitoPage) {
138   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
139   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
140   SearchIPCRouterPolicyImpl* policy =
141       static_cast<SearchIPCRouterPolicyImpl*>(
142           search_tab_helper->ipc_router().policy());
143   policy->set_is_incognito(true);
144
145   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
146       ShouldProcessFocusOmnibox(true));
147   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
148       ShouldProcessNavigateToURL(true));
149   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
150       ShouldProcessDeleteMostVisitedItem());
151   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
152       ShouldProcessUndoMostVisitedDeletion());
153   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
154       ShouldProcessUndoAllMostVisitedDeletions());
155   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
156       ShouldProcessLogEvent());
157   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
158       ShouldProcessPasteIntoOmnibox(true));
159 }
160
161 TEST_F(SearchIPCRouterPolicyTest, DoNotProcessMessagesForInactiveTab) {
162   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
163
164   // Assume the NTP is deactivated.
165   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
166   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
167       ShouldProcessFocusOmnibox(false));
168   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
169       ShouldProcessNavigateToURL(false));
170   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
171       ShouldProcessPasteIntoOmnibox(false));
172 }
173
174 TEST_F(SearchIPCRouterPolicyTest, SendSetDisplayInstantResults) {
175   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
176   EXPECT_TRUE(GetSearchTabHelper()->ipc_router().policy()->
177       ShouldSendSetDisplayInstantResults());
178 }
179
180 TEST_F(SearchIPCRouterPolicyTest, SendSetSuggestionToPrefetch) {
181   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
182   SearchTabHelper* search_tab_helper =
183       SearchTabHelper::FromWebContents(web_contents());
184   ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
185   EXPECT_TRUE(search_tab_helper->ipc_router().policy()->
186       ShouldSendSetSuggestionToPrefetch());
187 }
188
189 TEST_F(SearchIPCRouterPolicyTest,
190        DoNotSendSetMessagesForIncognitoPage) {
191   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
192   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
193   SearchIPCRouterPolicyImpl* policy =
194       static_cast<SearchIPCRouterPolicyImpl*>(
195           search_tab_helper->ipc_router().policy());
196   policy->set_is_incognito(true);
197
198   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
199       ShouldSendSetSuggestionToPrefetch());
200   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
201       ShouldSendSetDisplayInstantResults());
202   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
203       ShouldSendSetPromoInformation());
204   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
205       ShouldSendThemeBackgroundInfo());
206   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
207       ShouldSendMostVisitedItems());
208 }
209
210 TEST_F(SearchIPCRouterPolicyTest,
211        AppropriateMessagesSentToIncognitoPages) {
212   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
213   SearchTabHelper* search_tab_helper =
214       SearchTabHelper::FromWebContents(web_contents());
215   ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
216   SearchIPCRouterPolicyImpl* policy =
217       static_cast<SearchIPCRouterPolicyImpl*>(
218           search_tab_helper->ipc_router().policy());
219   policy->set_is_incognito(true);
220
221   EXPECT_TRUE(search_tab_helper->ipc_router().policy()->ShouldSubmitQuery());
222 }
223
224 TEST_F(SearchIPCRouterPolicyTest, SendMostVisitedItems) {
225   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
226   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
227   EXPECT_TRUE(search_tab_helper->ipc_router().policy()->
228       ShouldSendMostVisitedItems());
229 }
230
231 TEST_F(SearchIPCRouterPolicyTest, DoNotSendMostVisitedItems) {
232   // Send most visited items only if the current tab is an Instant NTP.
233   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
234   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
235   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
236       ShouldSendMostVisitedItems());
237 }
238
239 TEST_F(SearchIPCRouterPolicyTest, SendThemeBackgroundInfo) {
240   NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
241   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
242   EXPECT_TRUE(search_tab_helper->ipc_router().policy()->
243       ShouldSendThemeBackgroundInfo());
244 }
245
246 TEST_F(SearchIPCRouterPolicyTest, DoNotSendThemeBackgroundInfo) {
247   // Send theme background information only if the current tab is an
248   // Instant NTP.
249   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
250   SearchTabHelper* search_tab_helper = GetSearchTabHelper();
251   EXPECT_FALSE(search_tab_helper->ipc_router().policy()->
252       ShouldSendThemeBackgroundInfo());
253 }
254
255 TEST_F(SearchIPCRouterPolicyTest, SubmitQuery) {
256   NavigateAndCommitActiveTab(GURL("chrome-search://foo/bar"));
257   SearchTabHelper* search_tab_helper =
258       SearchTabHelper::FromWebContents(web_contents());
259   ASSERT_NE(static_cast<SearchTabHelper*>(NULL), search_tab_helper);
260   EXPECT_TRUE(search_tab_helper->ipc_router().policy()->ShouldSubmitQuery());
261 }