[email protected] | fd006c76 | 2012-03-28 20:47:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 5 | #include "build/build_config.h" |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 6 | #include "chrome/browser/extensions/extension_apitest.h" |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 7 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 8 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 9 | #include "chrome/browser/ui/browser.h" |
[email protected] | 00070c73 | 2011-04-09 15:31:33 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser_window.h" |
Devlin Cronin | e458a22 | 2019-12-30 22:39:37 | [diff] [blame] | 11 | #include "chrome/browser/ui/extensions/extension_action_test_helper.h" |
[email protected] | da2bfa4 | 2013-01-29 23:47:05 | [diff] [blame] | 12 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 13 | #include "chrome/common/url_constants.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 14 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 15 | #include "content/public/browser/web_contents.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 16 | #include "content/public/test/browser_test_utils.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 17 | #include "extensions/test/extension_test_message_listener.h" |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 18 | #include "extensions/test/result_catcher.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 19 | #include "net/dns/mock_host_resolver.h" |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 20 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 21 | |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 22 | using content::WebContents; |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 23 | using extensions::ResultCatcher; |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 24 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 25 | class IncognitoApiTest : public extensions::ExtensionApiTest { |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 26 | public: |
| 27 | void SetUpOnMainThread() override { |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 28 | extensions::ExtensionApiTest::SetUpOnMainThread(); |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 29 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 30 | ASSERT_TRUE(StartEmbeddedTestServer()); |
| 31 | } |
| 32 | }; |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 33 | |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 34 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoNoScript) { |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 35 | // Loads a simple extension which attempts to change the title of every page |
| 36 | // that loads to "modified". |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 37 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("incognito") |
| 38 | .AppendASCII("content_scripts"))); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 39 | |
| 40 | // Open incognito window and navigate to test page. |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 41 | Browser* otr_browser = OpenURLOffTheRecord( |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 42 | browser()->profile(), |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 43 | embedded_test_server()->GetURL("/extensions/test_file.html")); |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 44 | |
[email protected] | da2bfa4 | 2013-01-29 23:47:05 | [diff] [blame] | 45 | WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents(); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 46 | |
| 47 | // Verify the script didn't run. |
| 48 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 49 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 50 | tab, |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 51 | "window.domAutomationController.send(document.title == 'Unmodified')", |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 52 | &result)); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 53 | EXPECT_TRUE(result); |
| 54 | } |
| 55 | |
Devlin Cronin | d1747b9e | 2017-09-13 02:26:46 | [diff] [blame] | 56 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoYesScript) { |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 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 |
| 59 | // are mixed. |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 60 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts") |
| 61 | .AppendASCII("all_frames"))); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 62 | |
| 63 | // Loads a simple extension which attempts to change the title of every page |
| 64 | // that loads to "modified". |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 65 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 66 | .AppendASCII("incognito").AppendASCII("content_scripts"))); |
| 67 | |
| 68 | // Dummy extension #2. |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 69 | ASSERT_TRUE(LoadExtension(test_data_dir_ |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 70 | .AppendASCII("content_scripts").AppendASCII("isolated_world1"))); |
| 71 | |
| 72 | // Open incognito window and navigate to test page. |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 73 | Browser* otr_browser = OpenURLOffTheRecord( |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 74 | browser()->profile(), |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 75 | embedded_test_server()->GetURL("/extensions/test_file.html")); |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 76 | |
[email protected] | da2bfa4 | 2013-01-29 23:47:05 | [diff] [blame] | 77 | WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents(); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 78 | |
| 79 | // Verify the script ran. |
| 80 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 81 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 82 | tab, |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 83 | "window.domAutomationController.send(document.title == 'modified')", |
[email protected] | 9fabbf7 | 2010-09-30 21:50:05 | [diff] [blame] | 84 | &result)); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 85 | EXPECT_TRUE(result); |
| 86 | } |
| 87 | |
[email protected] | a320e51 | 2010-09-21 09:58:01 | [diff] [blame] | 88 | // Tests that an extension which is enabled for incognito mode doesn't |
| 89 | // accidentially create and incognito profile. |
[email protected] | 471072f9 | 2011-07-12 16:53:09 | [diff] [blame] | 90 | // Test disabled due to http://crbug.com/89054. |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 91 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_DontCreateIncognitoProfile) { |
[email protected] | a320e51 | 2010-09-21 09:58:01 | [diff] [blame] | 92 | ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile()); |
[email protected] | 6df23c4 | 2010-12-15 08:52:57 | [diff] [blame] | 93 | ASSERT_TRUE(RunExtensionTestIncognito( |
| 94 | "incognito/dont_create_profile")) << message_; |
[email protected] | a320e51 | 2010-09-21 09:58:01 | [diff] [blame] | 95 | ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile()); |
| 96 | } |
| 97 | |
[email protected] | 6d2662c | 2012-12-29 01:48:08 | [diff] [blame] | 98 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | fd006c76 | 2012-03-28 20:47:04 | [diff] [blame] | 99 | // http://crbug.com/120484 |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 100 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_Incognito) { |
[email protected] | 5f51a24 | 2012-03-28 00:38:07 | [diff] [blame] | 101 | #else |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 102 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, Incognito) { |
[email protected] | 5f51a24 | 2012-03-28 00:38:07 | [diff] [blame] | 103 | #endif |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 104 | ResultCatcher catcher; |
| 105 | |
| 106 | // Open incognito window and navigate to test page. |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 107 | OpenURLOffTheRecord( |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 108 | browser()->profile(), |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 109 | embedded_test_server()->GetURL("/extensions/test_file.html")); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 110 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 111 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
| 112 | .AppendASCII("incognito").AppendASCII("apis"))); |
| 113 | |
| 114 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 115 | } |
| 116 | |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 117 | // Tests that the APIs in an incognito-enabled split-mode extension work |
| 118 | // properly. |
[email protected] | fd006c76 | 2012-03-28 20:47:04 | [diff] [blame] | 119 | // http://crbug.com/120484 |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 120 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_IncognitoSplitMode) { |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 121 | // We need 2 ResultCatchers because we'll be running the same test in both |
| 122 | // regular and incognito mode. |
| 123 | ResultCatcher catcher; |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 124 | catcher.RestrictToBrowserContext(browser()->profile()); |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 125 | ResultCatcher catcher_incognito; |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 126 | catcher_incognito.RestrictToBrowserContext( |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 127 | browser()->profile()->GetOffTheRecordProfile()); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 128 | |
[email protected] | d7f4bae | 2011-10-06 20:19:27 | [diff] [blame] | 129 | ExtensionTestMessageListener listener("waiting", true); |
| 130 | ExtensionTestMessageListener listener_incognito("waiting_incognito", true); |
| 131 | |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 132 | // Open incognito window and navigate to test page. |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 133 | OpenURLOffTheRecord(browser()->profile(), embedded_test_server()->GetURL( |
| 134 | "/extensions/test_file.html")); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 135 | |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 136 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
| 137 | .AppendASCII("incognito").AppendASCII("split"))); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 138 | |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 139 | // Wait for both extensions to be ready before telling them to proceed. |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 140 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 141 | EXPECT_TRUE(listener_incognito.WaitUntilSatisfied()); |
| 142 | listener.Reply("go"); |
| 143 | listener_incognito.Reply("go"); |
[email protected] | 414785a | 2010-10-08 21:32:34 | [diff] [blame] | 144 | |
[email protected] | e838eaf | 2010-10-13 18:53:33 | [diff] [blame] | 145 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 146 | EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message(); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 149 | // Tests that the APIs in an incognito-disabled extension don't see incognito |
| 150 | // events or callbacks. |
[email protected] | fd006c76 | 2012-03-28 20:47:04 | [diff] [blame] | 151 | #if defined(OS_WIN) |
| 152 | // http://crbug.com/120484 |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 153 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_IncognitoDisabled) { |
[email protected] | fd006c76 | 2012-03-28 20:47:04 | [diff] [blame] | 154 | #else |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 155 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoDisabled) { |
[email protected] | fd006c76 | 2012-03-28 20:47:04 | [diff] [blame] | 156 | #endif |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 157 | ResultCatcher catcher; |
[email protected] | 575f380 | 2014-04-02 15:00:36 | [diff] [blame] | 158 | ExtensionTestMessageListener listener("createIncognitoTab", true); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 159 | |
| 160 | // Open incognito window and navigate to test page. |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 161 | OpenURLOffTheRecord(browser()->profile(), embedded_test_server()->GetURL( |
| 162 | "/extensions/test_file.html")); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 163 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 164 | ASSERT_TRUE(LoadExtension(test_data_dir_ |
| 165 | .AppendASCII("incognito").AppendASCII("apis_disabled"))); |
| 166 | |
[email protected] | 575f380 | 2014-04-02 15:00:36 | [diff] [blame] | 167 | EXPECT_TRUE(listener.WaitUntilSatisfied()); |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 168 | OpenURLOffTheRecord(browser()->profile(), GURL("about:blank")); |
[email protected] | 575f380 | 2014-04-02 15:00:36 | [diff] [blame] | 169 | listener.Reply("created"); |
| 170 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 171 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 172 | } |
| 173 | |
| 174 | // Test that opening a popup from an incognito browser window works properly. |
[email protected] | 9792d38 | 2014-08-11 16:12:37 | [diff] [blame] | 175 | // http://crbug.com/180759. |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 176 | IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_IncognitoPopup) { |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 177 | ResultCatcher catcher; |
| 178 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 179 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
| 180 | .AppendASCII("incognito").AppendASCII("popup"))); |
| 181 | |
| 182 | // Open incognito window and navigate to test page. |
erikchen | ff8b5c7a | 2015-07-13 23:41:20 | [diff] [blame] | 183 | Browser* incognito_browser = OpenURLOffTheRecord( |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 184 | browser()->profile(), |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 185 | embedded_test_server()->GetURL("/extensions/test_file.html")); |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 186 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 187 | // Simulate the incognito's browser action being clicked. |
Devlin Cronin | e458a22 | 2019-12-30 22:39:37 | [diff] [blame] | 188 | ExtensionActionTestHelper::Create(incognito_browser)->Press(0); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 189 | |
| 190 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 191 | } |