1 // Copyright (c) 2011 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/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_list.h" 13 #include "chrome/browser/ui/browser_window.h" 14 #include "chrome/common/url_constants.h" 15 #include "chrome/test/ui_test_utils.h" 16 #include "content/browser/tab_contents/tab_contents.h" 17 #include "net/base/mock_host_resolver.h" 18 19 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoNoScript) { 20 ASSERT_TRUE(StartTestServer()); 21 22 // Loads a simple extension which attempts to change the title of every page 23 // that loads to "modified". 24 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("incognito") 25 .AppendASCII("content_scripts"))); 26 27 // Open incognito window and navigate to test page. 28 ui_test_utils::OpenURLOffTheRecord( 29 browser()->profile(), 30 test_server()->GetURL("files/extensions/test_file.html")); 31 32 Browser* otr_browser = BrowserList::FindBrowserWithType( 33 browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, 34 false); 35 TabContents* tab = otr_browser->GetSelectedTabContents(); 36 37 // Verify the script didn't run. 38 bool result = false; 39 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( 40 tab->render_view_host(), L"", 41 L"window.domAutomationController.send(document.title == 'Unmodified')", 42 &result)); 43 EXPECT_TRUE(result); 44 } 45 46 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoYesScript) { 47 host_resolver()->AddRule("*", "127.0.0.1"); 48 ASSERT_TRUE(StartTestServer()); 49 50 // Load a dummy extension. This just tests that we don't regress a 51 // crash fix when multiple incognito- and non-incognito-enabled extensions 52 // are mixed. 53 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts") 54 .AppendASCII("all_frames"))); 55 56 // Loads a simple extension which attempts to change the title of every page 57 // that loads to "modified". 58 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ 59 .AppendASCII("incognito").AppendASCII("content_scripts"))); 60 61 // Dummy extension #2. 62 ASSERT_TRUE(LoadExtension(test_data_dir_ 63 .AppendASCII("content_scripts").AppendASCII("isolated_world1"))); 64 65 // Open incognito window and navigate to test page. 66 ui_test_utils::OpenURLOffTheRecord( 67 browser()->profile(), 68 test_server()->GetURL("files/extensions/test_file.html")); 69 70 Browser* otr_browser = BrowserList::FindBrowserWithType( 71 browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, 72 false); 73 TabContents* tab = otr_browser->GetSelectedTabContents(); 74 75 // Verify the script ran. 76 bool result = false; 77 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( 78 tab->render_view_host(), L"", 79 L"window.domAutomationController.send(document.title == 'modified')", 80 &result)); 81 EXPECT_TRUE(result); 82 } 83 84 // Tests that an extension which is enabled for incognito mode doesn't 85 // accidentially create and incognito profile. 86 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DontCreateIncognitoProfile) { 87 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile()); 88 ASSERT_TRUE(RunExtensionTestIncognito( 89 "incognito/dont_create_profile")) << message_; 90 ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile()); 91 } 92 93 // Tests that the APIs in an incognito-enabled extension work properly. 94 // Flaky - see crbug.com/77951. 95 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FLAKY_Incognito) { 96 host_resolver()->AddRule("*", "127.0.0.1"); 97 ASSERT_TRUE(StartTestServer()); 98 99 ResultCatcher catcher; 100 101 // Open incognito window and navigate to test page. 102 ui_test_utils::OpenURLOffTheRecord( 103 browser()->profile(), 104 test_server()->GetURL("files/extensions/test_file.html")); 105 106 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ 107 .AppendASCII("incognito").AppendASCII("apis"))); 108 109 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 110 } 111 112 // Tests that the APIs in an incognito-enabled split-mode extension work 113 // properly. 114 // Hangs flakily on mac, linux, win: http://crbug.com/53991 115 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_IncognitoSplitMode) { 116 host_resolver()->AddRule("*", "127.0.0.1"); 117 ASSERT_TRUE(StartTestServer()); 118 119 // We need 2 ResultCatchers because we'll be running the same test in both 120 // regular and incognito mode. 121 ResultCatcher catcher; 122 catcher.RestrictToProfile(browser()->profile()); 123 ResultCatcher catcher_incognito; 124 catcher_incognito.RestrictToProfile( 125 browser()->profile()->GetOffTheRecordProfile()); 126 127 // Open incognito window and navigate to test page. 128 ui_test_utils::OpenURLOffTheRecord( 129 browser()->profile(), 130 test_server()->GetURL("files/extensions/test_file.html")); 131 132 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ 133 .AppendASCII("incognito").AppendASCII("split"))); 134 135 // Wait for both extensions to be ready before telling them to proceed. 136 ExtensionTestMessageListener listener("waiting", true); 137 EXPECT_TRUE(listener.WaitUntilSatisfied()); 138 ExtensionTestMessageListener listener_incognito("waiting", true); 139 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied()); 140 listener.Reply("go"); 141 listener_incognito.Reply("go"); 142 143 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 144 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message(); 145 } 146 147 // Tests that the APIs in an incognito-disabled extension don't see incognito 148 // events or callbacks. 149 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoDisabled) { 150 host_resolver()->AddRule("*", "127.0.0.1"); 151 ASSERT_TRUE(StartTestServer()); 152 153 ResultCatcher catcher; 154 155 // Open incognito window and navigate to test page. 156 ui_test_utils::OpenURLOffTheRecord( 157 browser()->profile(), 158 test_server()->GetURL("files/extensions/test_file.html")); 159 160 ASSERT_TRUE(LoadExtension(test_data_dir_ 161 .AppendASCII("incognito").AppendASCII("apis_disabled"))); 162 163 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 164 } 165 166 // Test that opening a popup from an incognito browser window works properly. 167 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoPopup) { 168 host_resolver()->AddRule("*", "127.0.0.1"); 169 ASSERT_TRUE(StartTestServer()); 170 171 ResultCatcher catcher; 172 173 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ 174 .AppendASCII("incognito").AppendASCII("popup"))); 175 176 // Open incognito window and navigate to test page. 177 ui_test_utils::OpenURLOffTheRecord( 178 browser()->profile(), 179 test_server()->GetURL("files/extensions/test_file.html")); 180 181 Browser* incognito_browser = BrowserList::FindBrowserWithType( 182 browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, 183 false); 184 185 // Simulate the incognito's browser action being clicked. 186 BrowserActionTestUtil(incognito_browser).Press(0); 187 188 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 189 } 190