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