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/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
20 using content::WebContents;
22 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoNoScript) {
23 ASSERT_TRUE(StartEmbeddedTestServer());
25 // Loads a simple extension which attempts to change the title of every page
26 // that loads to "modified".
27 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("incognito")
28 .AppendASCII("content_scripts")));
30 // Open incognito window and navigate to test page.
31 Browser* otr_browser = ui_test_utils::OpenURLOffTheRecord(
33 embedded_test_server()->GetURL("/extensions/test_file.html"));
35 WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
37 // Verify the script didn't run.
39 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
41 "window.domAutomationController.send(document.title == 'Unmodified')",
47 // This test is very flaky on XP. http://crbug.com/248821
48 #define MAYBE_IncognitoYesScript DISABLED_IncognitoYesScript
50 #define MAYBE_IncognitoYesScript IncognitoYesScript
53 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_IncognitoYesScript) {
54 host_resolver()->AddRule("*", "127.0.0.1");
55 ASSERT_TRUE(StartEmbeddedTestServer());
57 // Load a dummy extension. This just tests that we don't regress a
58 // crash fix when multiple incognito- and non-incognito-enabled extensions
60 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts")
61 .AppendASCII("all_frames")));
63 // Loads a simple extension which attempts to change the title of every page
64 // that loads to "modified".
65 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
66 .AppendASCII("incognito").AppendASCII("content_scripts")));
68 // Dummy extension #2.
69 ASSERT_TRUE(LoadExtension(test_data_dir_
70 .AppendASCII("content_scripts").AppendASCII("isolated_world1")));
72 // Open incognito window and navigate to test page.
73 Browser* otr_browser = ui_test_utils::OpenURLOffTheRecord(
75 embedded_test_server()->GetURL("/extensions/test_file.html"));
77 WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
79 // Verify the script ran.
81 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
83 "window.domAutomationController.send(document.title == 'modified')",
88 // Tests that an extension which is enabled for incognito mode doesn't
89 // accidentially create and incognito profile.
90 // Test disabled due to http://crbug.com/89054.
91 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_DontCreateIncognitoProfile) {
92 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
93 ASSERT_TRUE(RunExtensionTestIncognito(
94 "incognito/dont_create_profile")) << message_;
95 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
98 #if defined(OS_WIN) || defined(OS_MACOSX)
99 // http://crbug.com/120484
100 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_Incognito) {
102 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Incognito) {
104 host_resolver()->AddRule("*", "127.0.0.1");
105 ASSERT_TRUE(StartEmbeddedTestServer());
107 ResultCatcher catcher;
109 // Open incognito window and navigate to test page.
110 ui_test_utils::OpenURLOffTheRecord(
111 browser()->profile(),
112 embedded_test_server()->GetURL("/extensions/test_file.html"));
114 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
115 .AppendASCII("incognito").AppendASCII("apis")));
117 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
120 // Tests that the APIs in an incognito-enabled split-mode extension work
122 // http://crbug.com/120484
123 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_IncognitoSplitMode) {
124 host_resolver()->AddRule("*", "127.0.0.1");
125 ASSERT_TRUE(StartEmbeddedTestServer());
127 // We need 2 ResultCatchers because we'll be running the same test in both
128 // regular and incognito mode.
129 ResultCatcher catcher;
130 catcher.RestrictToProfile(browser()->profile());
131 ResultCatcher catcher_incognito;
132 catcher_incognito.RestrictToProfile(
133 browser()->profile()->GetOffTheRecordProfile());
135 ExtensionTestMessageListener listener("waiting", true);
136 ExtensionTestMessageListener listener_incognito("waiting_incognito", true);
138 // Open incognito window and navigate to test page.
139 ui_test_utils::OpenURLOffTheRecord(
140 browser()->profile(),
141 embedded_test_server()->GetURL("/extensions/test_file.html"));
143 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
144 .AppendASCII("incognito").AppendASCII("split")));
146 // Wait for both extensions to be ready before telling them to proceed.
147 EXPECT_TRUE(listener.WaitUntilSatisfied());
148 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
149 listener.Reply("go");
150 listener_incognito.Reply("go");
152 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
153 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
156 // Tests that the APIs in an incognito-disabled extension don't see incognito
157 // events or callbacks.
159 // http://crbug.com/120484
160 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_IncognitoDisabled) {
162 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoDisabled) {
164 host_resolver()->AddRule("*", "127.0.0.1");
165 ASSERT_TRUE(StartEmbeddedTestServer());
167 ResultCatcher catcher;
168 ExtensionTestMessageListener listener("createIncognitoTab", true);
170 // Open incognito window and navigate to test page.
171 ui_test_utils::OpenURLOffTheRecord(
172 browser()->profile(),
173 embedded_test_server()->GetURL("/extensions/test_file.html"));
175 ASSERT_TRUE(LoadExtension(test_data_dir_
176 .AppendASCII("incognito").AppendASCII("apis_disabled")));
178 EXPECT_TRUE(listener.WaitUntilSatisfied());
179 ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
180 GURL("about:blank"));
181 listener.Reply("created");
183 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
186 // Test that opening a popup from an incognito browser window works properly.
187 // http://crbug.com/180759.
188 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_IncognitoPopup) {
189 host_resolver()->AddRule("*", "127.0.0.1");
190 ASSERT_TRUE(StartEmbeddedTestServer());
192 ResultCatcher catcher;
194 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
195 .AppendASCII("incognito").AppendASCII("popup")));
197 // Open incognito window and navigate to test page.
198 Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
199 browser()->profile(),
200 embedded_test_server()->GetURL("/extensions/test_file.html"));
202 // Simulate the incognito's browser action being clicked.
203 BrowserActionTestUtil(incognito_browser).Press(0);
205 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();