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 <vector>
6
7#include "base/files/file_path.h"
8#include "base/strings/utf_string_conversions.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
11#include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
12#include "chrome/browser/ui/tabs/tab_strip_model.h"
13#include "chrome/test/base/in_process_browser_test.h"
14#include "chrome/test/base/ui_test_utils.h"
15#include "components/content_settings/core/common/content_settings_types.h"
16#include "content/public/test/test_navigation_observer.h"
17#include "net/test/spawned_test_server/spawned_test_server.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20const base::FilePath::CharType kDocRoot[] =
21    FILE_PATH_LITERAL("chrome/test/data");
22
23class ContentSettingBubbleModelMixedScriptTest : public InProcessBrowserTest {
24 protected:
25  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
26    https_server_.reset(
27        new net::SpawnedTestServer(
28            net::SpawnedTestServer::TYPE_HTTPS,
29            net::SpawnedTestServer::SSLOptions(
30                net::SpawnedTestServer::SSLOptions::CERT_OK),
31            base::FilePath(kDocRoot)));
32    ASSERT_TRUE(https_server_->Start());
33  }
34
35  TabSpecificContentSettings* GetActiveTabSpecificContentSettings() {
36    return TabSpecificContentSettings::FromWebContents(
37        browser()->tab_strip_model()->GetActiveWebContents());
38  }
39
40  scoped_ptr<net::SpawnedTestServer> https_server_;
41};
42
43// Tests that a MIXEDSCRIPT type ContentSettingBubbleModel sends appropriate
44// IPCs to allow the renderer to load unsafe scripts and refresh the page
45// automatically.
46IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMixedScriptTest, MainFrame) {
47  GURL url(https_server_->GetURL(
48      "files/content_setting_bubble/mixed_script.html"));
49
50  // Load a page with mixed content and do quick verification by looking at
51  // the title string.
52  ui_test_utils::NavigateToURL(browser(), url);
53
54  EXPECT_TRUE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
55      CONTENT_SETTINGS_TYPE_MIXEDSCRIPT));
56
57  // Emulate link clicking on the mixed script bubble.
58  scoped_ptr<ContentSettingBubbleModel> model(
59      ContentSettingBubbleModel::CreateContentSettingBubbleModel(
60          browser()->content_setting_bubble_model_delegate(),
61          browser()->tab_strip_model()->GetActiveWebContents(),
62          browser()->profile(),
63          CONTENT_SETTINGS_TYPE_MIXEDSCRIPT));
64  model->OnCustomLinkClicked();
65
66  // Wait for reload
67  content::TestNavigationObserver observer(
68      browser()->tab_strip_model()->GetActiveWebContents());
69  observer.Wait();
70
71  EXPECT_FALSE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
72      CONTENT_SETTINGS_TYPE_MIXEDSCRIPT));
73}
74
75// Tests that a MIXEDSCRIPT type ContentSettingBubbleModel works for an iframe.
76IN_PROC_BROWSER_TEST_F(ContentSettingBubbleModelMixedScriptTest, Iframe) {
77  GURL url(https_server_->GetURL(
78      "files/content_setting_bubble/mixed_script_in_iframe.html"));
79
80  ui_test_utils::NavigateToURL(browser(), url);
81
82  EXPECT_TRUE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
83      CONTENT_SETTINGS_TYPE_MIXEDSCRIPT));
84
85  scoped_ptr<ContentSettingBubbleModel> model(
86      ContentSettingBubbleModel::CreateContentSettingBubbleModel(
87          browser()->content_setting_bubble_model_delegate(),
88          browser()->tab_strip_model()->GetActiveWebContents(),
89          browser()->profile(),
90          CONTENT_SETTINGS_TYPE_MIXEDSCRIPT));
91  model->OnCustomLinkClicked();
92
93  content::TestNavigationObserver observer(
94      browser()->tab_strip_model()->GetActiveWebContents());
95  observer.Wait();
96
97  EXPECT_FALSE(GetActiveTabSpecificContentSettings()->IsContentBlocked(
98      CONTENT_SETTINGS_TYPE_MIXEDSCRIPT));
99}
100