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 "chrome/browser/supervised_user/supervised_user_resource_throttle.h"
6
7#include "base/command_line.h"
8#include "base/prefs/pref_service.h"
9#include "base/values.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/supervised_user/supervised_user_constants.h"
12#include "chrome/browser/supervised_user/supervised_user_service.h"
13#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
14#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
15#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
16#include "chrome/browser/ui/browser.h"
17#include "chrome/common/chrome_switches.h"
18#include "chrome/test/base/in_process_browser_test.h"
19#include "chrome/test/base/ui_test_utils.h"
20#include "content/public/browser/navigation_entry.h"
21#include "content/public/browser/web_contents.h"
22#include "content/public/common/page_type.h"
23#include "content/public/test/test_navigation_observer.h"
24#include "content/public/test/test_utils.h"
25
26using content::MessageLoopRunner;
27using content::NavigationController;
28using content::WebContents;
29
30class SupervisedUserResourceThrottleTest : public InProcessBrowserTest {
31 protected:
32  SupervisedUserResourceThrottleTest() : supervised_user_service_(NULL) {}
33  virtual ~SupervisedUserResourceThrottleTest() {}
34
35 private:
36  virtual void SetUpOnMainThread() OVERRIDE;
37  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
38
39  SupervisedUserService* supervised_user_service_;
40};
41
42void SupervisedUserResourceThrottleTest::SetUpOnMainThread() {
43  supervised_user_service_ =
44      SupervisedUserServiceFactory::GetForProfile(browser()->profile());
45}
46
47void SupervisedUserResourceThrottleTest::SetUpCommandLine(
48    CommandLine* command_line) {
49  command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf");
50}
51
52// Tests that showing the blocking interstitial for a WebContents without a
53// SupervisedUserNavigationObserver doesn't crash.
54IN_PROC_BROWSER_TEST_F(SupervisedUserResourceThrottleTest,
55                       NoNavigationObserverBlock) {
56  Profile* profile = browser()->profile();
57  SupervisedUserSettingsService* supervised_user_settings_service =
58      SupervisedUserSettingsServiceFactory::GetForProfile(profile);
59  supervised_user_settings_service->SetLocalSettingForTesting(
60      supervised_users::kContentPackDefaultFilteringBehavior,
61      scoped_ptr<base::Value>(
62          new base::FundamentalValue(SupervisedUserURLFilter::BLOCK)));
63
64  scoped_ptr<WebContents> web_contents(
65      WebContents::Create(WebContents::CreateParams(profile)));
66  NavigationController& controller = web_contents->GetController();
67  content::TestNavigationObserver observer(web_contents.get());
68  controller.LoadURL(GURL("http://www.example.com"), content::Referrer(),
69                     ui::PAGE_TRANSITION_TYPED, std::string());
70  observer.Wait();
71  content::NavigationEntry* entry = controller.GetActiveEntry();
72  ASSERT_TRUE(entry);
73  EXPECT_EQ(content::PAGE_TYPE_INTERSTITIAL, entry->GetPageType());
74}
75