diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2021-05-20 09:47:09 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2021-06-07 11:15:42 +0000 |
commit | 189d4fd8fad9e3c776873be51938cd31a42b6177 (patch) | |
tree | 6497caeff5e383937996768766ab3bb2081a40b2 /chromium/chrome/browser/extensions/api/extension_action | |
parent | 8bc75099d364490b22f43a7ce366b366c08f4164 (diff) |
BASELINE: Update Chromium to 90.0.4430.221
Change-Id: Iff4d9d18d2fcf1a576f3b1f453010f744a232920
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/chrome/browser/extensions/api/extension_action')
5 files changed, 176 insertions, 129 deletions
diff --git a/chromium/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc b/chromium/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc index ef5d84656a2..2c841e641f9 100644 --- a/chromium/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc +++ b/chromium/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc @@ -58,7 +58,6 @@ #include "extensions/browser/process_manager.h" #include "extensions/browser/test_extension_registry_observer.h" #include "extensions/common/feature_switch.h" -#include "extensions/common/scoped_worker_based_extensions_channel.h" #include "extensions/test/extension_test_message_listener.h" #include "extensions/test/result_catcher.h" #include "net/test/embedded_test_server/embedded_test_server.h" @@ -153,27 +152,18 @@ using ContextType = ExtensionBrowserTest::ContextType; class BrowserActionApiLazyTest : public BrowserActionApiTest, public testing::WithParamInterface<ContextType> { - public: - BrowserActionApiLazyTest() { - // Service Workers are currently only available on certain channels, so set - // the channel for those tests. - if (GetParam() == ContextType::kServiceWorker) - current_channel_ = std::make_unique<ScopedWorkerBasedExtensionsChannel>(); - } - - const extensions::Extension* LoadExtensionWithParamFlags( + protected: + const extensions::Extension* LoadExtensionWithParamOptions( const base::FilePath& path) { - int flags = kFlagEnableFileAccess; - if (GetParam() == ContextType::kServiceWorker) - flags |= ExtensionBrowserTest::kFlagRunAsServiceWorkerBasedExtension; - return LoadExtensionWithFlags(path, flags); + return LoadExtension(path, {.load_as_service_worker = + GetParam() == ContextType::kServiceWorker}); } void RunUpdateTest(base::StringPiece path, bool expect_failure) { ExtensionTestMessageListener ready_listener("ready", true); ASSERT_TRUE(embedded_test_server()->Start()); const Extension* extension = - LoadExtensionWithParamFlags(test_data_dir_.AppendASCII(path)); + LoadExtensionWithParamOptions(test_data_dir_.AppendASCII(path)); ASSERT_TRUE(extension) << message_; // Test that there is a browser action in the toolbar. ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions()); @@ -210,7 +200,7 @@ class BrowserActionApiLazyTest void RunEnableTest(base::StringPiece path, bool start_enabled) { ExtensionTestMessageListener ready_listener("ready", true); const Extension* extension = - LoadExtensionWithParamFlags(test_data_dir_.AppendASCII(path)); + LoadExtensionWithParamOptions(test_data_dir_.AppendASCII(path)); ASSERT_TRUE(extension) << message_; // Test that there is a browser action in the toolbar. ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions()); @@ -237,14 +227,12 @@ class BrowserActionApiLazyTest private: base::test::ScopedFeatureList feature_list_; - std::unique_ptr<extensions::ScopedWorkerBasedExtensionsChannel> - current_channel_; }; IN_PROC_BROWSER_TEST_P(BrowserActionApiLazyTest, Basic) { ExtensionTestMessageListener ready_listener("ready", false); ASSERT_TRUE(embedded_test_server()->Start()); - const Extension* extension = LoadExtensionWithParamFlags( + const Extension* extension = LoadExtensionWithParamOptions( test_data_dir_.AppendASCII("browser_action/basics")); ASSERT_TRUE(extension) << message_; @@ -690,17 +678,10 @@ IN_PROC_BROWSER_TEST_P(BrowserActionApiLazyTest, BrowserActionRemovePopup) { << "a specific tab id."; } -#if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_CHROMEOS) -// Flaky: https://crbug.com/1113904 -#define MAYBE_IncognitoBasic DISABLED_IncognitoBasic -#else -#define MAYBE_IncognitoBasic IncognitoBasic -#endif - -IN_PROC_BROWSER_TEST_P(BrowserActionApiLazyTest, MAYBE_IncognitoBasic) { +IN_PROC_BROWSER_TEST_P(BrowserActionApiLazyTest, IncognitoBasic) { ExtensionTestMessageListener ready_listener("ready", false); ASSERT_TRUE(embedded_test_server()->Start()); - scoped_refptr<const Extension> extension = LoadExtensionWithParamFlags( + scoped_refptr<const Extension> extension = LoadExtensionWithParamOptions( test_data_dir_.AppendASCII("browser_action/basics")); ASSERT_TRUE(extension) << message_; @@ -749,7 +730,7 @@ IN_PROC_BROWSER_TEST_P(BrowserActionApiLazyTest, IncognitoUpdate) { ASSERT_TRUE(embedded_test_server()->Start()); ExtensionTestMessageListener incognito_not_allowed_listener( "incognito not allowed", false); - scoped_refptr<const Extension> extension = LoadExtensionWithParamFlags( + scoped_refptr<const Extension> extension = LoadExtensionWithParamOptions( test_data_dir_.AppendASCII("browser_action/update")); ASSERT_TRUE(extension) << message_; ASSERT_TRUE(incognito_not_allowed_listener.WaitUntilSatisfied()); @@ -810,9 +791,9 @@ IN_PROC_BROWSER_TEST_P(BrowserActionApiLazyTest, IncognitoUpdate) { // extensions. IN_PROC_BROWSER_TEST_P(BrowserActionApiLazyTest, IncognitoSplit) { ResultCatcher catcher; - const Extension* extension = LoadExtensionWithFlags( - test_data_dir_.AppendASCII("browser_action/split_mode"), - kFlagEnableIncognito); + const Extension* extension = + LoadExtension(test_data_dir_.AppendASCII("browser_action/split_mode"), + {.allow_in_incognito = true}); ASSERT_TRUE(extension) << message_; // Open an incognito browser. diff --git a/chromium/chrome/browser/extensions/api/extension_action/browser_action_interactive_test.cc b/chromium/chrome/browser/extensions/api/extension_action/browser_action_interactive_test.cc index 335042b7868..8cfc696c5e7 100644 --- a/chromium/chrome/browser/extensions/api/extension_action/browser_action_interactive_test.cc +++ b/chromium/chrome/browser/extensions/api/extension_action/browser_action_interactive_test.cc @@ -7,6 +7,7 @@ #include "base/run_loop.h" #include "base/test/test_timeouts.h" #include "build/build_config.h" +#include "build/chromeos_buildflags.h" #include "chrome/browser/download/download_prefs.h" #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/ui/browser.h" @@ -103,7 +104,7 @@ class PopupHostWatcher : public content::NotificationObserver { private: content::NotificationRegistrar registrar_; - base::Closure quit_closure_; + base::RepeatingClosure quit_closure_; int created_ = 0; int destroyed_ = 0; @@ -297,8 +298,9 @@ IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, TestOpenPopupIncognito) { IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, TestOpenPopupIncognitoFromBackground) { const Extension* extension = - LoadExtensionIncognito(test_data_dir_.AppendASCII("browser_action"). - AppendASCII("open_popup_background")); + LoadExtension(test_data_dir_.AppendASCII("browser_action") + .AppendASCII("open_popup_background"), + {.allow_in_incognito = true}); ASSERT_TRUE(extension); ExtensionTestMessageListener listener(false); listener.set_extension_id(extension->id()); @@ -985,7 +987,7 @@ IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupInteractiveTest, // The test verification below is applicable only to scenarios where the // download shelf is supported - on ChromeOS, instead of the download shelf, // there is a download notification in the right-bottom corner of the screen. -#if !defined(OS_CHROMEOS) +#if !BUILDFLAG(IS_CHROMEOS_ASH) EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); #endif } @@ -1020,7 +1022,7 @@ IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupInteractiveTest, // The test verification below is applicable only to scenarios where the // download shelf is supported - on ChromeOS, instead of the download shelf, // there is a download notification in the right-bottom corner of the screen. -#if !defined(OS_CHROMEOS) +#if !BUILDFLAG(IS_CHROMEOS_ASH) EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); #endif } diff --git a/chromium/chrome/browser/extensions/api/extension_action/extension_action_api.cc b/chromium/chrome/browser/extensions/api/extension_action/extension_action_api.cc index bf6aac8f9b3..401a9d1f667 100644 --- a/chromium/chrome/browser/extensions/api/extension_action/extension_action_api.cc +++ b/chromium/chrome/browser/extensions/api/extension_action/extension_action_api.cc @@ -31,6 +31,7 @@ #include "components/sessions/content/session_tab_helper.h" #include "content/public/browser/notification_service.h" #include "extensions/browser/api/declarative_net_request/constants.h" +#include "extensions/browser/api/declarative_net_request/utils.h" #include "extensions/browser/event_router.h" #include "extensions/browser/extension_action_manager.h" #include "extensions/browser/extension_host.h" @@ -513,18 +514,24 @@ ExtensionActionGetPopupFunction::RunExtensionAction() { ExtensionFunction::ResponseAction ExtensionActionGetBadgeTextFunction::RunExtensionAction() { - // Return a placeholder value if the extension has enabled using - // declarativeNetRequest action count as badge text and the badge count shown - // for this tab is the number of actions matched. - std::string badge_text = - extension_action_->UseDNRActionCountAsBadgeText(tab_id_) - ? declarative_net_request::kActionCountPlaceholderBadgeText - : extension_action_->GetExplicitlySetBadgeText(tab_id_); - - // TODO(crbug.com/990224): Document this behavior once - // chrome.declarativeNetRequest.setExtensionActionOptions is promoted to beta - // from trunk. - return RespondNow(OneArgument(base::Value(std::move(badge_text)))); + ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context()); + bool is_dnr_action_count_active = + prefs->GetDNRUseActionCountAsBadgeText(extension_id()) && + !extension_action_->HasBadgeText(tab_id_); + + // Ensure that the placeholder string is returned if this extension is + // displaying action counts for the badge labels and the extension doesn't + // have permission to view the action count for this tab. Note that + // tab-specific badge text takes priority over the action count. + if (is_dnr_action_count_active && + !declarative_net_request::HasDNRFeedbackPermission(extension(), + tab_id_)) { + return RespondNow(OneArgument(base::Value( + std::move(declarative_net_request::kActionCountPlaceholderBadgeText)))); + } + + return RespondNow(OneArgument( + base::Value(extension_action_->GetDisplayBadgeText(tab_id_)))); } ExtensionFunction::ResponseAction diff --git a/chromium/chrome/browser/extensions/api/extension_action/extension_action_api_unittest.cc b/chromium/chrome/browser/extensions/api/extension_action/extension_action_api_unittest.cc index cb36ab10316..b537f13746e 100644 --- a/chromium/chrome/browser/extensions/api/extension_action/extension_action_api_unittest.cc +++ b/chromium/chrome/browser/extensions/api/extension_action/extension_action_api_unittest.cc @@ -30,7 +30,7 @@ TEST_P(ExtensionActionAPIUnitTest, MultiIcons) { R"({ "name": "A test extension that tests multiple browser action icons", "version": "1.0", - "manifest_version": 2, + "manifest_version": %d, "%s": { "default_icon": { "19": "icon19.png", @@ -43,7 +43,8 @@ TEST_P(ExtensionActionAPIUnitTest, MultiIcons) { TestExtensionDir test_extension_dir; test_extension_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); { std::string icon_file_content; @@ -82,12 +83,13 @@ TEST_P(ExtensionActionAPIUnitTest, ActionLocalization) { R"({ "name": "Some extension", "version": "3.0", - "manifest_version": 2, + "manifest_version": %d, "default_locale": "en", "%s": { "default_title": "__MSG_default_action_title__" } })"; test_dir.WriteManifest( - base::StringPrintf(kManifest, GetManifestKeyForActionType(GetParam()))); + base::StringPrintf(kManifest, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); constexpr char kMessages[] = R"({ diff --git a/chromium/chrome/browser/extensions/api/extension_action/extension_action_apitest.cc b/chromium/chrome/browser/extensions/api/extension_action/extension_action_apitest.cc index c05c9165489..43b94da698b 100644 --- a/chromium/chrome/browser/extensions/api/extension_action/extension_action_apitest.cc +++ b/chromium/chrome/browser/extensions/api/extension_action/extension_action_apitest.cc @@ -51,16 +51,17 @@ constexpr char kSetIconBackgroundJsTemplate[] = }); })"; +constexpr char kPageHtmlTemplate[] = + R"(<html><script src="page.js"></script></html>)"; + // Runs |script| in the background page of the extension with the given // |extension_id|, and waits for it to send a test-passed result. This will // fail if the test in |script| fails. -void RunTestAndWaitForSuccess(Profile* profile, - const ExtensionId& extension_id, +void RunTestAndWaitForSuccess(content::WebContents* web_contents, const std::string& script) { SCOPED_TRACE(script); ResultCatcher result_catcher; - browsertest_util::ExecuteScriptInBackgroundPageNoWait(profile, extension_id, - script); + content::ExecuteScriptAsync(web_contents, script); EXPECT_TRUE(result_catcher.GetNextResult()) << result_catcher.message(); } @@ -101,14 +102,12 @@ class ActionTestHelper { const char* set_method_name, const char* get_method_name, const char* js_property_key, - Profile* profile, - const ExtensionId& extension_id) + content::WebContents* web_contents) : api_name_(api_name), set_method_name_(set_method_name), get_method_name_(get_method_name), js_property_key_(js_property_key), - profile_(profile), - extension_id_(extension_id) {} + web_contents_(web_contents) {} ~ActionTestHelper() = default; // Checks the value for the given |tab_id|. @@ -120,7 +119,7 @@ class ActionTestHelper { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile_, extension_id_, + web_contents_, base::StringPrintf(kScriptTemplate, api_name_, get_method_name_, tab_id, expected_js_value)); } @@ -134,9 +133,8 @@ class ActionTestHelper { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile_, extension_id_, - base::StringPrintf(kScriptTemplate, api_name_, get_method_name_, - expected_js_value)); + web_contents_, base::StringPrintf(kScriptTemplate, api_name_, + get_method_name_, expected_js_value)); } // Sets the value for a given |tab_id|. @@ -147,7 +145,7 @@ class ActionTestHelper { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile_, extension_id_, + web_contents_, base::StringPrintf(kScriptTemplate, api_name_, set_method_name_, tab_id, js_property_key_, new_js_value)); } @@ -160,7 +158,7 @@ class ActionTestHelper { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile_, extension_id_, + web_contents_, base::StringPrintf(kScriptTemplate, api_name_, set_method_name_, js_property_key_, new_js_value)); } @@ -174,10 +172,8 @@ class ActionTestHelper { const char* const get_method_name_; // The name of the property in the set method details (e.g., "popup"). const char* const js_property_key_; - // The associated profile. - Profile* const profile_; - // The id of the extension. - const ExtensionId extension_id_; + // The WebContents to use to execute API calls. + content::WebContents* const web_contents_; DISALLOW_COPY_AND_ASSIGN(ActionTestHelper); }; @@ -339,13 +335,14 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, R"({ "name": "Extension", "description": "An extension", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", "%s": {} })"; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); const Extension* extension = LoadExtension(test_dir.UnpackedPath()); ASSERT_TRUE(extension); @@ -394,7 +391,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, TitleLocalization) { R"({ "name": "Hreggvi\u00F0ur is my name", "description": "Hreggvi\u00F0ur: l10n action", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", "%s": { "default_title": "Hreggvi\u00F0ur" @@ -402,7 +399,8 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, TitleLocalization) { })"; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); const Extension* extension = LoadExtension(test_dir.UnpackedPath()); ASSERT_TRUE(extension); @@ -427,10 +425,10 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, OnClickedDispatching) { constexpr char kManifestTemplate[] = R"({ "name": "Test Clicking", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", "%s": {}, - "background": { "scripts": ["background.js"] } + "background": { %s } })"; constexpr char kBackgroundJsTemplate[] = R"(chrome.%s.onClicked.addListener((tab) => { @@ -439,11 +437,18 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, OnClickedDispatching) { chrome.test.assertTrue(tab.id > 0); chrome.test.assertTrue(tab.index > -1); chrome.test.notifyPass(); - });)"; + }); + chrome.test.sendMessage('ready');)"; + + const char* background_specification = + GetParam() == ActionInfo::TYPE_ACTION + ? R"("service_worker": "background.js")" + : R"("scripts": ["background.js"])"; TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()), background_specification)); test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), base::StringPrintf(kBackgroundJsTemplate, GetAPINameForActionType(GetParam()))); @@ -454,8 +459,11 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, OnClickedDispatching) { std::unique_ptr<ExtensionActionTestHelper> toolbar_helper = ExtensionActionTestHelper::Create(browser()); EXPECT_EQ(0, toolbar_helper->NumberOfBrowserActions()); + + ExtensionTestMessageListener listener("ready", /*will_reply=*/false); const Extension* extension = LoadExtension(test_dir.UnpackedPath()); ASSERT_TRUE(extension); + ASSERT_TRUE(listener.WaitUntilSatisfied()); ASSERT_EQ(1, toolbar_helper->NumberOfBrowserActions()); EXPECT_EQ(extension->id(), toolbar_helper->GetExtensionId(0)); @@ -477,7 +485,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, PopupCreation) { constexpr char kManifestTemplate[] = R"({ "name": "Test Clicking", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", "%s": { "default_popup": "popup.html" @@ -494,7 +502,8 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, PopupCreation) { TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); test_dir.WriteFile(FILE_PATH_LITERAL("popup.html"), kPopupHtml); test_dir.WriteFile(FILE_PATH_LITERAL("popup.js"), kPopupJs); @@ -543,7 +552,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, constexpr char kManifestTemplate[] = R"({ "name": "Test sessionStorage", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", "%s": { "default_popup": "popup.html" @@ -568,7 +577,8 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); test_dir.WriteFile(FILE_PATH_LITERAL("popup.html"), kPopupHtml); test_dir.WriteFile(FILE_PATH_LITERAL("popup.js"), kPopupJs); @@ -715,18 +725,19 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPICanvasTest, DynamicSetIcon) { constexpr char kManifestTemplate[] = R"({ "name": "Test Clicking", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", "%s": { "default_icon": "red_icon.png" - }, - "background": { "scripts": ["background.js"] } + } })"; TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); - test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); + test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHtmlTemplate); + test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), base::StringPrintf(kSetIconBackgroundJsTemplate, GetAPINameForActionType(GetParam()))); test_dir.CopyFileTo(test_data_dir_.AppendASCII("icon_rgb_0_0_255.png"), @@ -767,6 +778,14 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPICanvasTest, DynamicSetIcon) { // the icon's rendering. EXPECT_EQ(SK_ColorRED, default_icon.AsBitmap().getColor(mid_x, mid_y)); + // Open a tab to run the extension commands in. + ui_test_utils::NavigateToURLWithDisposition( + browser(), extension->GetResourceURL("page.html"), + WindowOpenDisposition::NEW_FOREGROUND_TAB, + ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + // Create a new tab. ui_test_utils::NavigateToURLWithDisposition( browser(), GURL("chrome://newtab"), @@ -785,7 +804,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPICanvasTest, DynamicSetIcon) { // Set the icon for the new tab to a different icon in the extension package. RunTestAndWaitForSuccess( - profile(), extension->id(), + web_contents, base::StringPrintf("setIcon({tabId: %d, path: 'blue_icon.png'});", new_tab_id)); @@ -807,8 +826,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPICanvasTest, DynamicSetIcon) { setIcon({tabId: %d, imageData: imageData}); })"; RunTestAndWaitForSuccess( - profile(), extension->id(), - base::StringPrintf(kSetIconFromImageData, new_tab_id)); + web_contents, base::StringPrintf(kSetIconFromImageData, new_tab_id)); new_tab_icon = toolbar_helper->GetIcon(0); EXPECT_FALSE(new_tab_icon.IsEmpty()); @@ -832,16 +850,17 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithJavascriptHooks) { constexpr char kManifestTemplate[] = R"({ "name": "JS Fun", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", - "%s": {}, - "background": { "scripts": ["background.js"] } + "%s": {} })"; TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); - test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); + test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHtmlTemplate); + test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), base::StringPrintf(kSetIconBackgroundJsTemplate, GetAPINameForActionType(GetParam()))); test_dir.CopyFileTo(test_data_dir_.AppendASCII("icon_rgb_0_0_255.png"), @@ -853,6 +872,13 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithJavascriptHooks) { ExtensionAction* action = GetExtensionAction(*extension); ASSERT_TRUE(action); + ui_test_utils::NavigateToURLWithDisposition( + browser(), extension->GetResourceURL("page.html"), + WindowOpenDisposition::NEW_FOREGROUND_TAB, + ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + int tab_id = GetActiveTabId(); EXPECT_TRUE(ActionHasDefaultState(*action, tab_id)); EnsureActionIsEnabledOnActiveTab(action); @@ -864,23 +890,24 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithJavascriptHooks) { Object.prototype, 'imageData', { set() { console.warn('intercepted set'); } }); domAutomationController.send('done');)"; - std::string result = browsertest_util::ExecuteScriptInBackgroundPage( - profile(), extension->id(), kScript); + std::string result; + ASSERT_TRUE( + content::ExecuteScriptAndExtractString(web_contents, kScript, &result)); ASSERT_EQ("done", result); constexpr char kOnePathScript[] = "setIcon({tabId: %d, path: 'blue_icon.png'});"; - RunTestAndWaitForSuccess(profile(), extension->id(), + RunTestAndWaitForSuccess(web_contents, base::StringPrintf(kOnePathScript, tab_id)); constexpr char kMultiPathScript[] = R"(setIcon({tabId: %d, path: {16: 'blue_icon.png', 24: 'blue_icon.png'}});)"; - RunTestAndWaitForSuccess(profile(), extension->id(), + RunTestAndWaitForSuccess(web_contents, base::StringPrintf(kMultiPathScript, tab_id)); constexpr char kRawImageDataScript[] = R"(setIcon({tabId: %d, imageData: {width:4,height:4,data:'a'.repeat(64)}});)"; - RunTestAndWaitForSuccess(profile(), extension->id(), + RunTestAndWaitForSuccess(web_contents, base::StringPrintf(kRawImageDataScript, tab_id)); } @@ -892,17 +919,18 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithSelfDefined) { constexpr char kManifestTemplate[] = R"({ "name": "JS Fun", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", - "%s": {}, - "background": { "scripts": ["background.js"] } + "%s": {} })"; TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); - test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), + test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHtmlTemplate); + test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), base::StringPrintf(kSetIconBackgroundJsTemplate, GetAPINameForActionType(GetParam()))); test_dir.CopyFileTo(test_data_dir_.AppendASCII("icon_rgb_0_0_255.png"), @@ -914,6 +942,13 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithSelfDefined) { ExtensionAction* action = GetExtensionAction(*extension); ASSERT_TRUE(action); + ui_test_utils::NavigateToURLWithDisposition( + browser(), extension->GetResourceURL("page.html"), + WindowOpenDisposition::NEW_FOREGROUND_TAB, + ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + int tab_id = GetActiveTabId(); EXPECT_TRUE(ActionHasDefaultState(*action, tab_id)); EnsureActionIsEnabledOnActiveTab(action); @@ -921,8 +956,9 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithSelfDefined) { // Override 'self' in a local variable. constexpr char kOverrideSelfScript[] = "var self = ''; domAutomationController.send('done');"; - std::string result = browsertest_util::ExecuteScriptInBackgroundPage( - profile(), extension->id(), kOverrideSelfScript); + std::string result; + ASSERT_TRUE(content::ExecuteScriptAndExtractString( + web_contents, kOverrideSelfScript, &result)); ASSERT_EQ("done", result); // Try setting the icon. This should succeed. Previously, the custom bindings @@ -931,7 +967,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithSelfDefined) { // See also https://crbug.com/1087948. constexpr char kSetIconScript[] = "setIcon({tabId: %d, path: 'blue_icon.png'});"; - RunTestAndWaitForSuccess(profile(), extension->id(), + RunTestAndWaitForSuccess(web_contents, base::StringPrintf(kSetIconScript, tab_id)); } @@ -941,22 +977,23 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, GettersAndSetters) { constexpr char kManifestTemplate[] = R"({ "name": "Test Getters and Setters", - "manifest_version": 2, + "manifest_version": %d, "version": "0.1", "%s": { "default_title": "default title", "default_popup": "default_popup.html" - }, - "background": { "scripts": ["background.js"] } + } })"; - constexpr char kBackgroundJs[] = "// Intentionally blank."; + constexpr char kPageJs[] = "// Intentionally blank."; constexpr char kPopupHtml[] = "<!doctype html><html><body>Blank</body></html>"; TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); - test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); + test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHtmlTemplate); + test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), kPageJs); test_dir.WriteFile(FILE_PATH_LITERAL("default_popup.html"), kPopupHtml); test_dir.WriteFile(FILE_PATH_LITERAL("custom_popup1.html"), kPopupHtml); test_dir.WriteFile(FILE_PATH_LITERAL("custom_popup2.html"), kPopupHtml); @@ -968,6 +1005,16 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, GettersAndSetters) { ASSERT_TRUE(action); int first_tab_id = GetActiveTabId(); + + // Open a tab to run the extension commands in. + ui_test_utils::NavigateToURLWithDisposition( + browser(), extension->GetResourceURL("page.html"), + WindowOpenDisposition::NEW_FOREGROUND_TAB, + ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + + // And a second new tab. ui_test_utils::NavigateToURLWithDisposition( browser(), GURL("chrome://newtab"), WindowOpenDisposition::NEW_FOREGROUND_TAB, @@ -1070,7 +1117,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, GettersAndSetters) { }; ActionTestHelper popup_helper(kApiName, "setPopup", "getPopup", "popup", - profile(), extension->id()); + web_contents); run_test(popup_helper, default_popup, custom_popup1, custom_popup2, base::BindRepeating(get_popup)); } @@ -1085,7 +1132,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, GettersAndSetters) { }; ActionTestHelper title_helper(kApiName, "setTitle", "getTitle", "title", - profile(), extension->id()); + web_contents); run_test(title_helper, default_title, custom_title1, custom_title2, base::BindRepeating(get_title)); } @@ -1105,7 +1152,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, GettersAndSetters) { }; ActionTestHelper badge_text_helper(kApiName, "setBadgeText", "getBadgeText", - "text", profile(), extension->id()); + "text", web_contents); run_test(badge_text_helper, default_badge_text, custom_badge_text1, custom_badge_text2, base::BindRepeating(get_badge_text)); } @@ -1122,7 +1169,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, GettersAndSetters) { ActionTestHelper badge_color_helper(kApiName, "setBadgeBackgroundColor", "getBadgeBackgroundColor", "color", - profile(), extension->id()); + web_contents); run_test(badge_color_helper, default_badge_color, custom_badge_color1, custom_badge_color2, base::BindRepeating(get_badge_color)); } @@ -1134,16 +1181,16 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, EnableAndDisable) { R"({ "name": "enabled/disabled action test", "version": "0.1", - "manifest_version": 2, - "%s": {}, - "background": {"scripts": ["background.js"]} + "manifest_version": %d, + "%s": {} })"; TestExtensionDir test_dir; test_dir.WriteManifest(base::StringPrintf( - kManifestTemplate, GetManifestKeyForActionType(GetParam()))); - test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), - "// This space left blank."); + kManifestTemplate, GetManifestVersionForActionType(GetParam()), + GetManifestKeyForActionType(GetParam()))); + test_dir.WriteFile(FILE_PATH_LITERAL("page.html"), kPageHtmlTemplate); + test_dir.WriteFile(FILE_PATH_LITERAL("page.js"), "// This space left blank."); const Extension* extension = LoadExtension(test_dir.UnpackedPath()); ASSERT_TRUE(extension); ExtensionAction* action = GetExtensionAction(*extension); @@ -1152,6 +1199,14 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, EnableAndDisable) { const int tab_id1 = GetActiveTabId(); EnsureActionIsEnabledOnTab(action, tab_id1); + // Open a tab to run the extension commands in. + ui_test_utils::NavigateToURLWithDisposition( + browser(), extension->GetResourceURL("page.html"), + WindowOpenDisposition::NEW_FOREGROUND_TAB, + ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + ui_test_utils::NavigateToURLWithDisposition( browser(), GURL("chrome://newtab"), WindowOpenDisposition::NEW_FOREGROUND_TAB, @@ -1184,7 +1239,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, EnableAndDisable) { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile(), extension->id(), + web_contents, base::StringPrintf(kScriptTemplate, GetAPINameForActionType(GetParam()), disable_function, tab_id2)); EXPECT_FALSE(action->GetIsVisible(tab_id2)); @@ -1198,7 +1253,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, EnableAndDisable) { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile(), extension->id(), + web_contents, base::StringPrintf(kScriptTemplate, GetAPINameForActionType(GetParam()), enable_function, tab_id2)); EXPECT_TRUE(action->GetIsVisible(tab_id2)); @@ -1221,7 +1276,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, EnableAndDisable) { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile(), extension->id(), + web_contents, base::StringPrintf(kScriptTemplate, GetAPINameForActionType(GetParam()), disable_function)); EXPECT_EQ(false, action->GetIsVisible(tab_id2)); @@ -1235,7 +1290,7 @@ IN_PROC_BROWSER_TEST_P(MultiActionAPITest, EnableAndDisable) { chrome.test.notifyPass(); });)"; RunTestAndWaitForSuccess( - profile(), extension->id(), + web_contents, base::StringPrintf(kScriptTemplate, GetAPINameForActionType(GetParam()), enable_function)); EXPECT_EQ(true, action->GetIsVisible(tab_id2)); |