Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / managed_mode / managed_mode_browsertest.cc
1 // Copyright (c) 2012 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 "base/command_line.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/common/cancelable_request.h"
11 #include "chrome/browser/history/history_service_factory.h"
12 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/managed_mode/managed_mode_interstitial.h"
15 #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
16 #include "chrome/browser/managed_mode/managed_user_constants.h"
17 #include "chrome/browser/managed_mode/managed_user_service.h"
18 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
19 #include "chrome/browser/managed_mode/managed_user_settings_service.h"
20 #include "chrome/browser/managed_mode/managed_user_settings_service_factory.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_navigator.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/pref_names.h"
28 #include "chrome/test/base/in_process_browser_test.h"
29 #include "chrome/test/base/ui_test_utils.h"
30 #include "components/infobars/core/infobar.h"
31 #include "components/user_prefs/pref_registry_syncable.h"
32 #include "content/public/browser/interstitial_page.h"
33 #include "content/public/browser/navigation_controller.h"
34 #include "content/public/browser/navigation_entry.h"
35 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/browser/web_contents_observer.h"
38 #include "content/public/test/browser_test_utils.h"
39 #include "grit/generated_resources.h"
40 #include "testing/gmock/include/gmock/gmock.h"
41 #include "ui/base/l10n/l10n_util.h"
42
43 using content::InterstitialPage;
44 using content::NavigationController;
45 using content::NavigationEntry;
46 using content::WebContents;
47
48 namespace {
49
50 // Tests the filter mode in which all sites are blocked by default.
51 class ManagedModeBlockModeTest : public InProcessBrowserTest {
52  public:
53   // Indicates whether the interstitial should proceed or not.
54   enum InterstitialAction {
55     INTERSTITIAL_PROCEED,
56     INTERSTITIAL_DONTPROCEED,
57   };
58
59   ManagedModeBlockModeTest() : managed_user_service_(NULL) {}
60   virtual ~ManagedModeBlockModeTest() {}
61
62   void CheckShownPageIsInterstitial(WebContents* tab) {
63     CheckShownPage(tab, content::PAGE_TYPE_INTERSTITIAL);
64   }
65
66   void CheckShownPageIsNotInterstitial(WebContents* tab) {
67     CheckShownPage(tab, content::PAGE_TYPE_NORMAL);
68   }
69
70   // Checks to see if the type of the current page is |page_type|.
71   void CheckShownPage(WebContents* tab, content::PageType page_type) {
72     ASSERT_FALSE(tab->IsCrashed());
73     NavigationEntry* entry = tab->GetController().GetActiveEntry();
74     ASSERT_TRUE(entry);
75     ASSERT_EQ(page_type, entry->GetPageType());
76   }
77
78   void SendAccessRequest(WebContents* tab) {
79     InterstitialPage* interstitial_page = tab->GetInterstitialPage();
80     ASSERT_TRUE(interstitial_page);
81
82     // Get the ManagedModeInterstitial delegate.
83     content::InterstitialPageDelegate* delegate =
84         interstitial_page->GetDelegateForTesting();
85
86     // Simulate the click on the "request" button.
87     delegate->CommandReceived("\"request\"");
88   }
89
90   void GoBack(WebContents* tab) {
91     InterstitialPage* interstitial_page = tab->GetInterstitialPage();
92     ASSERT_TRUE(interstitial_page);
93
94     // Get the ManagedModeInterstitial delegate.
95     content::InterstitialPageDelegate* delegate =
96         interstitial_page->GetDelegateForTesting();
97
98     // Simulate the click on the "back" button.
99     delegate->CommandReceived("\"back\"");
100   }
101
102  protected:
103   virtual void SetUpOnMainThread() OVERRIDE {
104     // Set up the ManagedModeNavigationObserver manually since the profile was
105     // not managed when the browser was created.
106     content::WebContents* web_contents =
107         browser()->tab_strip_model()->GetActiveWebContents();
108     ManagedModeNavigationObserver::CreateForWebContents(web_contents);
109
110     Profile* profile = browser()->profile();
111     managed_user_service_ = ManagedUserServiceFactory::GetForProfile(profile);
112     ManagedUserSettingsService* managed_user_settings_service =
113         ManagedUserSettingsServiceFactory::GetForProfile(profile);
114     managed_user_settings_service->SetLocalSettingForTesting(
115         managed_users::kContentPackDefaultFilteringBehavior,
116         scoped_ptr<base::Value>(
117             new base::FundamentalValue(ManagedModeURLFilter::BLOCK)));
118   }
119
120   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
121     // Enable the test server and remap all URLs to it.
122     ASSERT_TRUE(test_server()->Start());
123     std::string host_port = test_server()->host_port_pair().ToString();
124     command_line->AppendSwitchASCII(switches::kHostResolverRules,
125         "MAP *.example.com " + host_port + "," +
126         "MAP *.new-example.com " + host_port + "," +
127         "MAP *.a.com " + host_port);
128
129     command_line->AppendSwitchASCII(switches::kManagedUserId, "asdf");
130   }
131
132   // Acts like a synchronous call to history's QueryHistory. Modified from
133   // history_querying_unittest.cc.
134   void QueryHistory(HistoryService* history_service,
135                     const std::string& text_query,
136                     const history::QueryOptions& options,
137                     history::QueryResults* results) {
138     CancelableRequestConsumer history_request_consumer;
139     base::RunLoop run_loop;
140     history_service->QueryHistory(
141         base::UTF8ToUTF16(text_query),
142         options,
143         &history_request_consumer,
144         base::Bind(&ManagedModeBlockModeTest::QueryHistoryComplete,
145                    base::Unretained(this),
146                    results,
147                    &run_loop));
148     run_loop.Run();  // Will go until ...Complete calls Quit.
149   }
150
151   void QueryHistoryComplete(history::QueryResults* new_results,
152                             base::RunLoop* run_loop,
153                             HistoryService::Handle /* handle */,
154                             history::QueryResults* results) {
155     results->Swap(new_results);
156     run_loop->Quit();  // Will return out to QueryHistory.
157   }
158
159   ManagedUserService* managed_user_service_;
160 };
161
162 class MockTabStripModelObserver : public TabStripModelObserver {
163  public:
164   explicit MockTabStripModelObserver(TabStripModel* tab_strip)
165       : tab_strip_(tab_strip) {
166     tab_strip_->AddObserver(this);
167   }
168
169   ~MockTabStripModelObserver() {
170     tab_strip_->RemoveObserver(this);
171   }
172
173   MOCK_METHOD3(TabClosingAt, void(TabStripModel*, content::WebContents*, int));
174
175  private:
176   TabStripModel* tab_strip_;
177 };
178
179 // Navigates to a blocked URL.
180 IN_PROC_BROWSER_TEST_F(ManagedModeBlockModeTest,
181                        SendAccessRequestOnBlockedURL) {
182   GURL test_url("http://www.example.com/files/simple.html");
183   ui_test_utils::NavigateToURL(browser(), test_url);
184
185   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
186
187   CheckShownPageIsInterstitial(tab);
188
189   SendAccessRequest(tab);
190
191   // TODO(sergiu): Properly check that the access request was sent here.
192
193   GoBack(tab);
194
195   // Make sure that the tab is still there.
196   EXPECT_EQ(tab, browser()->tab_strip_model()->GetActiveWebContents());
197
198   CheckShownPageIsNotInterstitial(tab);
199 }
200
201 // Navigates to a blocked URL in a new tab. We expect the tab to be closed
202 // automatically on pressing the "back" button on the interstitial.
203 IN_PROC_BROWSER_TEST_F(ManagedModeBlockModeTest, OpenBlockedURLInNewTab) {
204   TabStripModel* tab_strip = browser()->tab_strip_model();
205   WebContents* prev_tab = tab_strip->GetActiveWebContents();
206
207   // Open blocked URL in a new tab.
208   GURL test_url("http://www.example.com/files/simple.html");
209   ui_test_utils::NavigateToURLWithDisposition(
210       browser(), test_url, NEW_FOREGROUND_TAB,
211       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
212
213   // Check that we got the interstitial.
214   WebContents* tab = tab_strip->GetActiveWebContents();
215   CheckShownPageIsInterstitial(tab);
216
217   // On pressing the "back" button, the new tab should be closed, and we should
218   // get back to the previous active tab.
219   MockTabStripModelObserver observer(tab_strip);
220   EXPECT_CALL(observer,
221               TabClosingAt(tab_strip, tab, tab_strip->active_index()));
222   GoBack(tab);
223   EXPECT_EQ(prev_tab, tab_strip->GetActiveWebContents());
224 }
225
226 // Tests whether a visit attempt adds a special history entry.
227 IN_PROC_BROWSER_TEST_F(ManagedModeBlockModeTest,
228                        HistoryVisitRecorded) {
229   GURL allowed_url("http://www.example.com/files/simple.html");
230
231   // Set the host as allowed.
232   scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
233   dict->SetBooleanWithoutPathExpansion(allowed_url.host(), true);
234   ManagedUserSettingsService* managed_user_settings_service =
235       ManagedUserSettingsServiceFactory::GetForProfile(
236           browser()->profile());
237   managed_user_settings_service->SetLocalSettingForTesting(
238       managed_users::kContentPackManualBehaviorHosts,
239       dict.PassAs<base::Value>());
240   EXPECT_EQ(
241       ManagedUserService::MANUAL_ALLOW,
242       managed_user_service_->GetManualBehaviorForHost(allowed_url.host()));
243
244   ui_test_utils::NavigateToURL(browser(), allowed_url);
245
246   // Navigate to it and check that we don't get an interstitial.
247   WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
248   CheckShownPageIsNotInterstitial(tab);
249
250   // Navigate to a blocked page and go back on the interstitial.
251   GURL blocked_url("http://www.new-example.com/files/simple.html");
252   ui_test_utils::NavigateToURL(browser(), blocked_url);
253
254   tab = browser()->tab_strip_model()->GetActiveWebContents();
255
256   CheckShownPageIsInterstitial(tab);
257   GoBack(tab);
258
259   // Check that we went back to the first URL and that the manual behaviors
260   // have not changed.
261   EXPECT_EQ(allowed_url.spec(), tab->GetURL().spec());
262   EXPECT_EQ(ManagedUserService::MANUAL_ALLOW,
263             managed_user_service_->GetManualBehaviorForHost("www.example.com"));
264   EXPECT_EQ(
265       ManagedUserService::MANUAL_NONE,
266       managed_user_service_->GetManualBehaviorForHost("www.new-example.com"));
267
268   // Query the history entry.
269   HistoryService* history_service = HistoryServiceFactory::GetForProfile(
270       browser()->profile(), Profile::EXPLICIT_ACCESS);
271   history::QueryOptions options;
272   history::QueryResults results;
273   QueryHistory(history_service, "", options, &results);
274
275   // Check that the entries have the correct blocked_visit value.
276   ASSERT_EQ(2u, results.size());
277   EXPECT_EQ(blocked_url.spec(), results[0].url().spec());
278   EXPECT_TRUE(results[0].blocked_visit());
279   EXPECT_EQ(allowed_url.spec(), results[1].url().spec());
280   EXPECT_FALSE(results[1].blocked_visit());
281 }
282
283 IN_PROC_BROWSER_TEST_F(ManagedModeBlockModeTest, Unblock) {
284   GURL test_url("http://www.example.com/files/simple.html");
285   ui_test_utils::NavigateToURL(browser(), test_url);
286
287   WebContents* web_contents =
288       browser()->tab_strip_model()->GetActiveWebContents();
289
290   CheckShownPageIsInterstitial(web_contents);
291
292   content::WindowedNotificationObserver observer(
293       content::NOTIFICATION_LOAD_STOP,
294       content::NotificationService::AllSources());
295
296   // Set the host as allowed.
297   scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
298   dict->SetBooleanWithoutPathExpansion(test_url.host(), true);
299   ManagedUserSettingsService* managed_user_settings_service =
300       ManagedUserSettingsServiceFactory::GetForProfile(
301           browser()->profile());
302   managed_user_settings_service->SetLocalSettingForTesting(
303       managed_users::kContentPackManualBehaviorHosts,
304       dict.PassAs<base::Value>());
305   EXPECT_EQ(
306       ManagedUserService::MANUAL_ALLOW,
307       managed_user_service_->GetManualBehaviorForHost(test_url.host()));
308
309   observer.Wait();
310   EXPECT_EQ(test_url, web_contents->GetURL());
311 }
312
313 }  // namespace