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