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.
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"
43 using content::InterstitialPage;
44 using content::NavigationController;
45 using content::NavigationEntry;
46 using content::WebContents;
50 // Tests the filter mode in which all sites are blocked by default.
51 class ManagedModeBlockModeTest : public InProcessBrowserTest {
53 // Indicates whether the interstitial should proceed or not.
54 enum InterstitialAction {
56 INTERSTITIAL_DONTPROCEED,
59 ManagedModeBlockModeTest() : managed_user_service_(NULL) {}
60 virtual ~ManagedModeBlockModeTest() {}
62 void CheckShownPageIsInterstitial(WebContents* tab) {
63 CheckShownPage(tab, content::PAGE_TYPE_INTERSTITIAL);
66 void CheckShownPageIsNotInterstitial(WebContents* tab) {
67 CheckShownPage(tab, content::PAGE_TYPE_NORMAL);
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();
75 ASSERT_EQ(page_type, entry->GetPageType());
78 void SendAccessRequest(WebContents* tab) {
79 InterstitialPage* interstitial_page = tab->GetInterstitialPage();
80 ASSERT_TRUE(interstitial_page);
82 // Get the ManagedModeInterstitial delegate.
83 content::InterstitialPageDelegate* delegate =
84 interstitial_page->GetDelegateForTesting();
86 // Simulate the click on the "request" button.
87 delegate->CommandReceived("\"request\"");
90 void GoBack(WebContents* tab) {
91 InterstitialPage* interstitial_page = tab->GetInterstitialPage();
92 ASSERT_TRUE(interstitial_page);
94 // Get the ManagedModeInterstitial delegate.
95 content::InterstitialPageDelegate* delegate =
96 interstitial_page->GetDelegateForTesting();
98 // Simulate the click on the "back" button.
99 delegate->CommandReceived("\"back\"");
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);
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)));
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);
129 command_line->AppendSwitchASCII(switches::kManagedUserId, "asdf");
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),
143 &history_request_consumer,
144 base::Bind(&ManagedModeBlockModeTest::QueryHistoryComplete,
145 base::Unretained(this),
148 run_loop.Run(); // Will go until ...Complete calls Quit.
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.
159 ManagedUserService* managed_user_service_;
162 class MockTabStripModelObserver : public TabStripModelObserver {
164 explicit MockTabStripModelObserver(TabStripModel* tab_strip)
165 : tab_strip_(tab_strip) {
166 tab_strip_->AddObserver(this);
169 ~MockTabStripModelObserver() {
170 tab_strip_->RemoveObserver(this);
173 MOCK_METHOD3(TabClosingAt, void(TabStripModel*, content::WebContents*, int));
176 TabStripModel* tab_strip_;
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);
185 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
187 CheckShownPageIsInterstitial(tab);
189 SendAccessRequest(tab);
191 // TODO(sergiu): Properly check that the access request was sent here.
195 // Make sure that the tab is still there.
196 EXPECT_EQ(tab, browser()->tab_strip_model()->GetActiveWebContents());
198 CheckShownPageIsNotInterstitial(tab);
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();
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);
213 // Check that we got the interstitial.
214 WebContents* tab = tab_strip->GetActiveWebContents();
215 CheckShownPageIsInterstitial(tab);
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()));
223 EXPECT_EQ(prev_tab, tab_strip->GetActiveWebContents());
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");
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>());
241 ManagedUserService::MANUAL_ALLOW,
242 managed_user_service_->GetManualBehaviorForHost(allowed_url.host()));
244 ui_test_utils::NavigateToURL(browser(), allowed_url);
246 // Navigate to it and check that we don't get an interstitial.
247 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
248 CheckShownPageIsNotInterstitial(tab);
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);
254 tab = browser()->tab_strip_model()->GetActiveWebContents();
256 CheckShownPageIsInterstitial(tab);
259 // Check that we went back to the first URL and that the manual behaviors
261 EXPECT_EQ(allowed_url.spec(), tab->GetURL().spec());
262 EXPECT_EQ(ManagedUserService::MANUAL_ALLOW,
263 managed_user_service_->GetManualBehaviorForHost("www.example.com"));
265 ManagedUserService::MANUAL_NONE,
266 managed_user_service_->GetManualBehaviorForHost("www.new-example.com"));
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);
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());
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);
287 WebContents* web_contents =
288 browser()->tab_strip_model()->GetActiveWebContents();
290 CheckShownPageIsInterstitial(web_contents);
292 content::WindowedNotificationObserver observer(
293 content::NOTIFICATION_LOAD_STOP,
294 content::NotificationService::AllSources());
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>());
306 ManagedUserService::MANUAL_ALLOW,
307 managed_user_service_->GetManualBehaviorForHost(test_url.host()));
310 EXPECT_EQ(test_url, web_contents->GetURL());