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 "chrome/browser/extensions/browser_action_test_util.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_test_message_listener.h"
9 #include "chrome/browser/extensions/user_script_master.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
21 using content::WebContents
;
23 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, IncognitoNoScript
) {
24 ASSERT_TRUE(StartEmbeddedTestServer());
26 // Loads a simple extension which attempts to change the title of every page
27 // that loads to "modified".
28 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("incognito")
29 .AppendASCII("content_scripts")));
31 // Open incognito window and navigate to test page.
32 Browser
* otr_browser
= ui_test_utils::OpenURLOffTheRecord(
34 embedded_test_server()->GetURL("/extensions/test_file.html"));
36 WebContents
* tab
= otr_browser
->tab_strip_model()->GetActiveWebContents();
38 // Verify the script didn't run.
40 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
42 "window.domAutomationController.send(document.title == 'Unmodified')",
48 // This test is very flaky on XP. http://crbug.com/248821
49 #define MAYBE_IncognitoYesScript DISABLED_IncognitoYesScript
51 #define MAYBE_IncognitoYesScript IncognitoYesScript
54 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_IncognitoYesScript
) {
55 host_resolver()->AddRule("*", "127.0.0.1");
56 ASSERT_TRUE(StartEmbeddedTestServer());
58 // Load a dummy extension. This just tests that we don't regress a
59 // crash fix when multiple incognito- and non-incognito-enabled extensions
61 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("content_scripts")
62 .AppendASCII("all_frames")));
64 // Loads a simple extension which attempts to change the title of every page
65 // that loads to "modified".
66 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
67 .AppendASCII("incognito").AppendASCII("content_scripts")));
69 // Dummy extension #2.
70 ASSERT_TRUE(LoadExtension(test_data_dir_
71 .AppendASCII("content_scripts").AppendASCII("isolated_world1")));
73 // Open incognito window and navigate to test page.
74 Browser
* otr_browser
= ui_test_utils::OpenURLOffTheRecord(
76 embedded_test_server()->GetURL("/extensions/test_file.html"));
78 WebContents
* tab
= otr_browser
->tab_strip_model()->GetActiveWebContents();
80 // Verify the script ran.
82 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
84 "window.domAutomationController.send(document.title == 'modified')",
89 // Tests that an extension which is enabled for incognito mode doesn't
90 // accidentially create and incognito profile.
91 // Test disabled due to http://crbug.com/89054.
92 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_DontCreateIncognitoProfile
) {
93 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
94 ASSERT_TRUE(RunExtensionTestIncognito(
95 "incognito/dont_create_profile")) << message_
;
96 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
99 #if defined(OS_WIN) || defined(OS_MACOSX)
100 // http://crbug.com/120484
101 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_Incognito
) {
103 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, Incognito
) {
105 host_resolver()->AddRule("*", "127.0.0.1");
106 ASSERT_TRUE(StartEmbeddedTestServer());
108 ResultCatcher catcher
;
110 // Open incognito window and navigate to test page.
111 ui_test_utils::OpenURLOffTheRecord(
112 browser()->profile(),
113 embedded_test_server()->GetURL("/extensions/test_file.html"));
115 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
116 .AppendASCII("incognito").AppendASCII("apis")));
118 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
121 // Tests that the APIs in an incognito-enabled split-mode extension work
124 // http://crbug.com/120484
125 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_IncognitoSplitMode
) {
127 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, IncognitoSplitMode
) {
129 host_resolver()->AddRule("*", "127.0.0.1");
130 ASSERT_TRUE(StartEmbeddedTestServer());
132 // We need 2 ResultCatchers because we'll be running the same test in both
133 // regular and incognito mode.
134 ResultCatcher catcher
;
135 catcher
.RestrictToProfile(browser()->profile());
136 ResultCatcher catcher_incognito
;
137 catcher_incognito
.RestrictToProfile(
138 browser()->profile()->GetOffTheRecordProfile());
140 ExtensionTestMessageListener
listener("waiting", true);
141 ExtensionTestMessageListener
listener_incognito("waiting_incognito", true);
143 // Open incognito window and navigate to test page.
144 ui_test_utils::OpenURLOffTheRecord(
145 browser()->profile(),
146 embedded_test_server()->GetURL("/extensions/test_file.html"));
148 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
149 .AppendASCII("incognito").AppendASCII("split")));
151 // Wait for both extensions to be ready before telling them to proceed.
152 EXPECT_TRUE(listener
.WaitUntilSatisfied());
153 EXPECT_TRUE(listener_incognito
.WaitUntilSatisfied());
154 listener
.Reply("go");
155 listener_incognito
.Reply("go");
157 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
158 EXPECT_TRUE(catcher_incognito
.GetNextResult()) << catcher
.message();
161 // Tests that the APIs in an incognito-disabled extension don't see incognito
162 // events or callbacks.
164 // http://crbug.com/120484
165 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, DISABLED_IncognitoDisabled
) {
167 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, IncognitoDisabled
) {
169 host_resolver()->AddRule("*", "127.0.0.1");
170 ASSERT_TRUE(StartEmbeddedTestServer());
172 ResultCatcher catcher
;
174 // Open incognito window and navigate to test page.
175 ui_test_utils::OpenURLOffTheRecord(
176 browser()->profile(),
177 embedded_test_server()->GetURL("/extensions/test_file.html"));
179 ASSERT_TRUE(LoadExtension(test_data_dir_
180 .AppendASCII("incognito").AppendASCII("apis_disabled")));
182 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
185 #if defined(OS_WIN) || defined(OS_MACOSX)
186 // http://crbug.com/104438.
187 #define MAYBE_IncognitoPopup DISABLED_IncognitoPopup
189 #define MAYBE_IncognitoPopup IncognitoPopup
191 // Test that opening a popup from an incognito browser window works properly.
192 IN_PROC_BROWSER_TEST_F(ExtensionApiTest
, MAYBE_IncognitoPopup
) {
193 host_resolver()->AddRule("*", "127.0.0.1");
194 ASSERT_TRUE(StartEmbeddedTestServer());
196 ResultCatcher catcher
;
198 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
199 .AppendASCII("incognito").AppendASCII("popup")));
201 // Open incognito window and navigate to test page.
202 Browser
* incognito_browser
= ui_test_utils::OpenURLOffTheRecord(
203 browser()->profile(),
204 embedded_test_server()->GetURL("/extensions/test_file.html"));
206 // Simulate the incognito's browser action being clicked.
207 BrowserActionTestUtil(incognito_browser
).Press(0);
209 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();