blob: ce72ae9f64cf233134ca143c9572911dbb3c373b [file] [log] [blame]
[email protected]fd006c762012-03-28 20:47:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]db7331a2010-02-25 22:10:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avia2f4804a2015-12-24 23:11:135#include "build/build_config.h"
[email protected]db7331a2010-02-25 22:10:506#include "chrome/browser/extensions/extension_apitest.h"
[email protected]eaa7dd182010-12-14 11:09:007#include "chrome/browser/extensions/extension_service.h"
[email protected]8ecad5e2010-12-02 21:18:338#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:109#include "chrome/browser/ui/browser.h"
[email protected]00070c732011-04-09 15:31:3310#include "chrome/browser/ui/browser_window.h"
Devlin Cronine458a222019-12-30 22:39:3711#include "chrome/browser/ui/extensions/extension_action_test_helper.h"
[email protected]da2bfa42013-01-29 23:47:0512#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]db7331a2010-02-25 22:10:5013#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3214#include "chrome/test/base/ui_test_utils.h"
[email protected]4ca15302012-01-03 05:53:2015#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4216#include "content/public/test/browser_test_utils.h"
lfg910f2f92014-09-19 05:31:0917#include "extensions/test/extension_test_message_listener.h"
yoze8dc2f12014-09-09 23:16:3218#include "extensions/test/result_catcher.h"
[email protected]f2cb3cf2013-03-21 01:40:5319#include "net/dns/mock_host_resolver.h"
[email protected]c1dffe82013-06-26 20:59:0520#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]db7331a2010-02-25 22:10:5021
[email protected]4ca15302012-01-03 05:53:2022using content::WebContents;
yoze8dc2f12014-09-09 23:16:3223using extensions::ResultCatcher;
[email protected]4ca15302012-01-03 05:53:2024
Devlin Croninef3e37e2018-05-14 23:47:2425class IncognitoApiTest : public extensions::ExtensionApiTest {
jambb11ed742017-05-01 17:27:5926 public:
27 void SetUpOnMainThread() override {
Devlin Croninef3e37e2018-05-14 23:47:2428 extensions::ExtensionApiTest::SetUpOnMainThread();
jambb11ed742017-05-01 17:27:5929 host_resolver()->AddRule("*", "127.0.0.1");
30 ASSERT_TRUE(StartEmbeddedTestServer());
31 }
32};
[email protected]db7331a2010-02-25 22:10:5033
jambb11ed742017-05-01 17:27:5934IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoNoScript) {
[email protected]db7331a2010-02-25 22:10:5035 // Loads a simple extension which attempts to change the title of every page
36 // that loads to "modified".
[email protected]e18a19b2010-10-19 20:32:3037 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("incognito")
38 .AppendASCII("content_scripts")));
[email protected]db7331a2010-02-25 22:10:5039
40 // Open incognito window and navigate to test page.
erikchenff8b5c7a2015-07-13 23:41:2041 Browser* otr_browser = OpenURLOffTheRecord(
[email protected]fe3048872010-10-18 14:58:5942 browser()->profile(),
[email protected]c1dffe82013-06-26 20:59:0543 embedded_test_server()->GetURL("/extensions/test_file.html"));
[email protected]fe3048872010-10-18 14:58:5944
[email protected]da2bfa42013-01-29 23:47:0545 WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
[email protected]db7331a2010-02-25 22:10:5046
47 // Verify the script didn't run.
48 bool result = false;
[email protected]b6987e02013-01-04 18:30:4349 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
50 tab,
[email protected]06bc5d92013-01-02 22:44:1351 "window.domAutomationController.send(document.title == 'Unmodified')",
[email protected]9fabbf72010-09-30 21:50:0552 &result));
[email protected]db7331a2010-02-25 22:10:5053 EXPECT_TRUE(result);
54}
55
Devlin Cronind1747b9e2017-09-13 02:26:4656IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoYesScript) {
[email protected]db7331a2010-02-25 22:10:5057 // Load a dummy extension. This just tests that we don't regress a
58 // crash fix when multiple incognito- and non-incognito-enabled extensions
59 // are mixed.
[email protected]e18a19b2010-10-19 20:32:3060 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts")
61 .AppendASCII("all_frames")));
[email protected]db7331a2010-02-25 22:10:5062
63 // Loads a simple extension which attempts to change the title of every page
64 // that loads to "modified".
[email protected]e18a19b2010-10-19 20:32:3065 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
[email protected]db7331a2010-02-25 22:10:5066 .AppendASCII("incognito").AppendASCII("content_scripts")));
67
68 // Dummy extension #2.
[email protected]e18a19b2010-10-19 20:32:3069 ASSERT_TRUE(LoadExtension(test_data_dir_
[email protected]db7331a2010-02-25 22:10:5070 .AppendASCII("content_scripts").AppendASCII("isolated_world1")));
71
72 // Open incognito window and navigate to test page.
erikchenff8b5c7a2015-07-13 23:41:2073 Browser* otr_browser = OpenURLOffTheRecord(
[email protected]fe3048872010-10-18 14:58:5974 browser()->profile(),
[email protected]c1dffe82013-06-26 20:59:0575 embedded_test_server()->GetURL("/extensions/test_file.html"));
[email protected]fe3048872010-10-18 14:58:5976
[email protected]da2bfa42013-01-29 23:47:0577 WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
[email protected]db7331a2010-02-25 22:10:5078
79 // Verify the script ran.
80 bool result = false;
[email protected]b6987e02013-01-04 18:30:4381 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
82 tab,
[email protected]06bc5d92013-01-02 22:44:1383 "window.domAutomationController.send(document.title == 'modified')",
[email protected]9fabbf72010-09-30 21:50:0584 &result));
[email protected]db7331a2010-02-25 22:10:5085 EXPECT_TRUE(result);
86}
87
[email protected]a320e512010-09-21 09:58:0188// Tests that an extension which is enabled for incognito mode doesn't
89// accidentially create and incognito profile.
[email protected]471072f92011-07-12 16:53:0990// Test disabled due to http://crbug.com/89054.
jambb11ed742017-05-01 17:27:5991IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_DontCreateIncognitoProfile) {
[email protected]a320e512010-09-21 09:58:0192 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
[email protected]6df23c42010-12-15 08:52:5793 ASSERT_TRUE(RunExtensionTestIncognito(
94 "incognito/dont_create_profile")) << message_;
[email protected]a320e512010-09-21 09:58:0195 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
96}
97
[email protected]6d2662c2012-12-29 01:48:0898#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]fd006c762012-03-28 20:47:0499// http://crbug.com/120484
jambb11ed742017-05-01 17:27:59100IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_Incognito) {
[email protected]5f51a242012-03-28 00:38:07101#else
jambb11ed742017-05-01 17:27:59102IN_PROC_BROWSER_TEST_F(IncognitoApiTest, Incognito) {
[email protected]5f51a242012-03-28 00:38:07103#endif
[email protected]db7331a2010-02-25 22:10:50104 ResultCatcher catcher;
105
106 // Open incognito window and navigate to test page.
erikchenff8b5c7a2015-07-13 23:41:20107 OpenURLOffTheRecord(
[email protected]fe3048872010-10-18 14:58:59108 browser()->profile(),
[email protected]c1dffe82013-06-26 20:59:05109 embedded_test_server()->GetURL("/extensions/test_file.html"));
[email protected]db7331a2010-02-25 22:10:50110
[email protected]db7331a2010-02-25 22:10:50111 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
112 .AppendASCII("incognito").AppendASCII("apis")));
113
114 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
115}
116
[email protected]bc535ee52010-08-31 18:40:32117// Tests that the APIs in an incognito-enabled split-mode extension work
118// properly.
[email protected]fd006c762012-03-28 20:47:04119// http://crbug.com/120484
jambb11ed742017-05-01 17:27:59120IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_IncognitoSplitMode) {
[email protected]e838eaf2010-10-13 18:53:33121 // We need 2 ResultCatchers because we'll be running the same test in both
122 // regular and incognito mode.
123 ResultCatcher catcher;
yoze8dc2f12014-09-09 23:16:32124 catcher.RestrictToBrowserContext(browser()->profile());
[email protected]e838eaf2010-10-13 18:53:33125 ResultCatcher catcher_incognito;
yoze8dc2f12014-09-09 23:16:32126 catcher_incognito.RestrictToBrowserContext(
[email protected]e838eaf2010-10-13 18:53:33127 browser()->profile()->GetOffTheRecordProfile());
[email protected]bc535ee52010-08-31 18:40:32128
[email protected]d7f4bae2011-10-06 20:19:27129 ExtensionTestMessageListener listener("waiting", true);
130 ExtensionTestMessageListener listener_incognito("waiting_incognito", true);
131
[email protected]e838eaf2010-10-13 18:53:33132 // Open incognito window and navigate to test page.
erikchenff8b5c7a2015-07-13 23:41:20133 OpenURLOffTheRecord(browser()->profile(), embedded_test_server()->GetURL(
134 "/extensions/test_file.html"));
[email protected]bc535ee52010-08-31 18:40:32135
[email protected]e838eaf2010-10-13 18:53:33136 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
137 .AppendASCII("incognito").AppendASCII("split")));
[email protected]bc535ee52010-08-31 18:40:32138
[email protected]e838eaf2010-10-13 18:53:33139 // Wait for both extensions to be ready before telling them to proceed.
[email protected]e838eaf2010-10-13 18:53:33140 EXPECT_TRUE(listener.WaitUntilSatisfied());
[email protected]e838eaf2010-10-13 18:53:33141 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
142 listener.Reply("go");
143 listener_incognito.Reply("go");
[email protected]414785a2010-10-08 21:32:34144
[email protected]e838eaf2010-10-13 18:53:33145 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
146 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
[email protected]bc535ee52010-08-31 18:40:32147}
148
[email protected]db7331a2010-02-25 22:10:50149// Tests that the APIs in an incognito-disabled extension don't see incognito
150// events or callbacks.
[email protected]fd006c762012-03-28 20:47:04151#if defined(OS_WIN)
152// http://crbug.com/120484
jambb11ed742017-05-01 17:27:59153IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_IncognitoDisabled) {
[email protected]fd006c762012-03-28 20:47:04154#else
jambb11ed742017-05-01 17:27:59155IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoDisabled) {
[email protected]fd006c762012-03-28 20:47:04156#endif
[email protected]db7331a2010-02-25 22:10:50157 ResultCatcher catcher;
[email protected]575f3802014-04-02 15:00:36158 ExtensionTestMessageListener listener("createIncognitoTab", true);
[email protected]db7331a2010-02-25 22:10:50159
160 // Open incognito window and navigate to test page.
erikchenff8b5c7a2015-07-13 23:41:20161 OpenURLOffTheRecord(browser()->profile(), embedded_test_server()->GetURL(
162 "/extensions/test_file.html"));
[email protected]db7331a2010-02-25 22:10:50163
[email protected]db7331a2010-02-25 22:10:50164 ASSERT_TRUE(LoadExtension(test_data_dir_
165 .AppendASCII("incognito").AppendASCII("apis_disabled")));
166
[email protected]575f3802014-04-02 15:00:36167 EXPECT_TRUE(listener.WaitUntilSatisfied());
erikchenff8b5c7a2015-07-13 23:41:20168 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
[email protected]575f3802014-04-02 15:00:36169 listener.Reply("created");
170
[email protected]db7331a2010-02-25 22:10:50171 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
172}
173
174// Test that opening a popup from an incognito browser window works properly.
[email protected]9792d382014-08-11 16:12:37175// http://crbug.com/180759.
jambb11ed742017-05-01 17:27:59176IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_IncognitoPopup) {
[email protected]db7331a2010-02-25 22:10:50177 ResultCatcher catcher;
178
[email protected]db7331a2010-02-25 22:10:50179 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
180 .AppendASCII("incognito").AppendASCII("popup")));
181
182 // Open incognito window and navigate to test page.
erikchenff8b5c7a2015-07-13 23:41:20183 Browser* incognito_browser = OpenURLOffTheRecord(
[email protected]fe3048872010-10-18 14:58:59184 browser()->profile(),
[email protected]c1dffe82013-06-26 20:59:05185 embedded_test_server()->GetURL("/extensions/test_file.html"));
[email protected]fe3048872010-10-18 14:58:59186
[email protected]db7331a2010-02-25 22:10:50187 // Simulate the incognito's browser action being clicked.
Devlin Cronine458a222019-12-30 22:39:37188 ExtensionActionTestHelper::Create(incognito_browser)->Press(0);
[email protected]db7331a2010-02-25 22:10:50189
190 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
191}