Commit a7b1f0f9 authored by David Bertoni's avatar David Bertoni Committed by Commit Bot

[Extensions] Refactor some browserAction tests for Service Workers.

There are some unit tests that, with a little refactoring, will work
for both legacy background page-based extensions and service worker-
based ones. This CL is a start at refactoring and rewriting any tests
that are appropriate for both.

Bug: 1015136
Change-Id: I65baa3f8fa23ad1b25c067da216224b46925ea35
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1922923
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720693}
parent fb2bcb29
......@@ -78,21 +78,6 @@ void ExecuteExtensionAction(Browser* browser, const Extension* extension) {
->RunAction(extension, true);
}
// An ImageSkia source that will do nothing (i.e., have a blank skia). We need
// this because we need a blank canvas at a certain size, and that can't be done
// by just using a null ImageSkia.
class BlankImageSource : public gfx::CanvasImageSource {
public:
explicit BlankImageSource(const gfx::Size& size)
: gfx::CanvasImageSource(size) {}
~BlankImageSource() override {}
void Draw(gfx::Canvas* canvas) override {}
private:
DISALLOW_COPY_AND_ASSIGN(BlankImageSource);
};
const char kEmptyImageDataError[] =
"The imageData property must contain an ImageData object or dictionary "
"of ImageData objects.";
......@@ -147,9 +132,10 @@ class BrowserActionApiTest : public ExtensionApiTest {
return source.ptr();
}
ExtensionAction* GetBrowserAction(const Extension& extension) {
ExtensionAction* GetBrowserAction(Browser* browser,
const Extension& extension) {
ExtensionAction* extension_action =
ExtensionActionManager::Get(browser()->profile())
ExtensionActionManager::Get(browser->profile())
->GetExtensionAction(extension);
return extension_action->action_type() == ActionInfo::TYPE_BROWSER
? extension_action
......@@ -195,35 +181,59 @@ class RenderFrameChangedWatcher : public content::WebContentsObserver {
};
IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) {
ExtensionTestMessageListener ready_listener("ready", false);
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
const Extension* extension = GetSingleLoadedExtension();
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("browser_action/basics"));
ASSERT_TRUE(extension) << message_;
// Test that there is a browser action in the toolbar.
ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
// Tell the extension to update the browser action state.
ResultCatcher catcher;
ui_test_utils::NavigateToURL(browser(),
GURL(extension->GetResourceURL("update.html")));
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
// Test that we received the changes.
ExtensionAction* action = GetBrowserAction(*extension);
ASSERT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId));
ASSERT_EQ("badge",
action->GetExplicitlySetBadgeText(ExtensionAction::kDefaultTabId));
ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
// Simulate the browser action being clicked.
// Open a URL in the tab, so the event handler can check the tab's
// "url" and "title" properties.
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("/extensions/test_file.txt"));
ResultCatcher catcher;
// Simulate the browser action being clicked.
ExecuteExtensionAction(browser(), extension);
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Update) {
ExtensionTestMessageListener ready_listener("ready", true);
ASSERT_TRUE(embedded_test_server()->Start());
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("browser_action/update"));
ASSERT_TRUE(extension) << message_;
// Test that there is a browser action in the toolbar.
ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
ExtensionAction* action = GetBrowserAction(browser(), *extension);
EXPECT_EQ("This is the default title.",
action->GetTitle(ExtensionAction::kDefaultTabId));
EXPECT_EQ("",
action->GetExplicitlySetBadgeText(ExtensionAction::kDefaultTabId));
EXPECT_EQ(SkColorSetARGB(0, 0, 0, 0),
action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
// Tell the extension to update the browser action state and then
// catch the result.
ResultCatcher catcher;
ready_listener.Reply("update");
ASSERT_TRUE(catcher.GetNextResult());
// Test that we received the changes.
EXPECT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId));
EXPECT_EQ("badge",
action->GetExplicitlySetBadgeText(ExtensionAction::kDefaultTabId));
EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255),
action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
}
IN_PROC_BROWSER_TEST_F(BrowserActionApiCanvasTest, DynamicBrowserAction) {
......@@ -243,10 +253,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiCanvasTest, DynamicBrowserAction) {
// We should not be creating icons asynchronously, so we don't need an
// observer.
ExtensionActionIconFactory icon_factory(
profile(),
extension,
GetBrowserAction(*extension),
NULL);
profile(), extension, GetBrowserAction(browser(), *extension), nullptr);
// Test that there is a browser action in the toolbar.
ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
EXPECT_TRUE(GetBrowserActionsBar()->HasIcon(0));
......@@ -450,7 +457,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiCanvasTest,
std::string result;
EXPECT_TRUE(ExecuteScriptAndExtractString(
background_page->host_contents(),
base::StringPrintf(kScript, "invisible"), &result));
base::StringPrintf(kScript, "invisibleImageData"), &result));
EXPECT_EQ("Icon not sufficiently visible.", result);
// The icon should not have changed.
EXPECT_TRUE(gfx::test::AreImagesEqual(initial_bar_icon,
......@@ -466,7 +473,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiCanvasTest,
std::string result;
EXPECT_TRUE(ExecuteScriptAndExtractString(
background_page->host_contents(),
base::StringPrintf(kScript, "visible"), &result));
base::StringPrintf(kScript, "visibleImageData"), &result));
EXPECT_EQ("", result);
// The icon should have changed.
EXPECT_FALSE(gfx::test::AreImagesEqual(initial_bar_icon,
......@@ -553,7 +560,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionAddPopup) {
int tab_id = ExtensionTabUtil::GetTabId(
browser()->tab_strip_model()->GetActiveWebContents());
ExtensionAction* browser_action = GetBrowserAction(*extension);
ExtensionAction* browser_action = GetBrowserAction(browser(), *extension);
ASSERT_TRUE(browser_action)
<< "Browser action test extension should have a browser action.";
......@@ -609,7 +616,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionRemovePopup) {
int tab_id = ExtensionTabUtil::GetTabId(
browser()->tab_strip_model()->GetActiveWebContents());
ExtensionAction* browser_action = GetBrowserAction(*extension);
ExtensionAction* browser_action = GetBrowserAction(browser(), *extension);
ASSERT_TRUE(browser_action)
<< "Browser action test extension should have a browser action.";
......@@ -635,10 +642,10 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionRemovePopup) {
}
IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
ExtensionTestMessageListener ready_listener("ready", false);
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
const Extension* extension = GetSingleLoadedExtension();
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("browser_action/basics"));
ASSERT_TRUE(extension) << message_;
// Test that there is a browser action in the toolbar.
......@@ -646,29 +653,92 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
// Open an incognito window and test that the browser action isn't there by
// default.
Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
base::RunLoop().RunUntilIdle(); // Wait for profile initialization.
Browser* incognito_browser =
new Browser(Browser::CreateParams(incognito_profile, true));
Browser* incognito_browser = CreateIncognitoBrowser(browser()->profile());
ASSERT_EQ(0, BrowserActionTestUtil::Create(incognito_browser)
->NumberOfBrowserActions());
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
// Now enable the extension in incognito mode, and test that the browser
// action shows up.
// SetIsIncognitoEnabled() requires a reload of the extension, so we have to
// wait for it.
ExtensionTestMessageListener incognito_ready_listener("ready", false);
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), extension->id());
extensions::util::SetIsIncognitoEnabled(
extension->id(), browser()->profile(), true);
registry_observer.WaitForExtensionLoaded();
extension = registry_observer.WaitForExtensionLoaded();
ASSERT_EQ(1, BrowserActionTestUtil::Create(incognito_browser)
->NumberOfBrowserActions());
// TODO(mpcomplete): simulate a click and have it do the right thing in
// incognito.
ASSERT_TRUE(incognito_ready_listener.WaitUntilSatisfied());
// Open a URL in the tab, so the event handler can check the tab's
// "url" and "title" properties.
ui_test_utils::NavigateToURL(
incognito_browser,
embedded_test_server()->GetURL("/extensions/test_file.txt"));
ResultCatcher catcher;
// Simulate the browser action being clicked.
ExecuteExtensionAction(incognito_browser, extension);
EXPECT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoUpdate) {
ASSERT_TRUE(embedded_test_server()->Start());
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("browser_action/update"));
ASSERT_TRUE(extension) << message_;
// Test that there is a browser action in the toolbar.
ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
// Open an incognito window and test that the browser action isn't there by
// default.
Browser* incognito_browser = CreateIncognitoBrowser(browser()->profile());
ASSERT_EQ(0, BrowserActionTestUtil::Create(incognito_browser)
->NumberOfBrowserActions());
// Set up a listener so we can reply for the extension to do the update.
ExtensionTestMessageListener incognito_ready_listener("incognito ready",
true);
// Now enable the extension in incognito mode, and test that the browser
// action shows up.
// SetIsIncognitoEnabled() requires a reload of the extension, so we have to
// wait for it.
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), extension->id());
extensions::util::SetIsIncognitoEnabled(extension->id(), browser()->profile(),
true);
extension = registry_observer.WaitForExtensionLoaded();
ASSERT_EQ(1, BrowserActionTestUtil::Create(incognito_browser)
->NumberOfBrowserActions());
ASSERT_TRUE(incognito_ready_listener.WaitUntilSatisfied());
ExtensionAction* action = GetBrowserAction(incognito_browser, *extension);
EXPECT_EQ("This is the default title.",
action->GetTitle(ExtensionAction::kDefaultTabId));
EXPECT_EQ("",
action->GetExplicitlySetBadgeText(ExtensionAction::kDefaultTabId));
EXPECT_EQ(SkColorSetARGB(0, 0, 0, 0),
action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
// Tell the extension to update the browser action state and then
// catch the result.
ResultCatcher incognito_catcher;
incognito_ready_listener.Reply("incognito update");
ASSERT_TRUE(incognito_catcher.GetNextResult());
// Test that we received the changes.
EXPECT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId));
EXPECT_EQ("badge",
action->GetExplicitlySetBadgeText(ExtensionAction::kDefaultTabId));
EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255),
action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
}
// Tests that events are dispatched to the correct profile for split mode
......@@ -680,13 +750,8 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoSplit) {
kFlagEnableIncognito);
ASSERT_TRUE(extension) << message_;
// Open an incognito window.
Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
Browser* incognito_browser =
new Browser(Browser::CreateParams(incognito_profile, true));
base::RunLoop().RunUntilIdle(); // Wait for profile initialization.
// Navigate just to have a tab in this window, otherwise wonky things happen.
OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
// Open an incognito browser.
Browser* incognito_browser = CreateIncognitoBrowser(browser()->profile());
ASSERT_EQ(1, BrowserActionTestUtil::Create(incognito_browser)
->NumberOfBrowserActions());
......@@ -709,7 +774,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, CloseBackgroundPage) {
extensions::ProcessManager* manager =
extensions::ProcessManager::Get(browser()->profile());
ASSERT_TRUE(manager->GetBackgroundHostForExtension(extension->id()));
ExtensionAction* action = GetBrowserAction(*extension);
ExtensionAction* action = GetBrowserAction(browser(), *extension);
ASSERT_EQ("",
action->GetExplicitlySetBadgeText(ExtensionAction::kDefaultTabId));
......@@ -739,7 +804,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BadgeBackgroundColor) {
ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
// Test that CSS values (#FF0000) set color correctly.
ExtensionAction* action = GetBrowserAction(*extension);
ExtensionAction* action = GetBrowserAction(browser(), *extension);
ASSERT_EQ(SkColorSetARGB(255, 255, 0, 0),
action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
......@@ -812,8 +877,8 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) {
ui_test_utils::NavigateToURL(browser(),
embedded_test_server()->GetURL("/simple.html"));
ExtensionAction* browser_action = GetBrowserAction(*extension);
EXPECT_TRUE(browser_action != NULL);
ExtensionAction* browser_action = GetBrowserAction(browser(), *extension);
EXPECT_TRUE(browser_action);
// Simulate a click on the browser action icon.
{
......@@ -824,7 +889,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) {
WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(tab != NULL);
EXPECT_TRUE(tab);
// Verify that the browser action turned the background color red.
const std::string script =
......@@ -1213,7 +1278,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest,
// Test that there is a browser action in the toolbar.
ASSERT_EQ(1, GetBrowserActionsBar()->NumberOfBrowserActions());
ExtensionAction* browser_action = GetBrowserAction(*extension);
ExtensionAction* browser_action = GetBrowserAction(browser(), *extension);
EXPECT_TRUE(browser_action);
// Find the background page.
......
......@@ -15,4 +15,4 @@ chrome.browserAction.onClicked.addListener(function(tab) {
chrome.test.notifyPass();
});
chrome.test.notifyPass();
chrome.test.sendMessage('ready');
......@@ -6,7 +6,7 @@
"scripts": ["background.js"]
},
"permissions": [
"http://*/*"
"http://*/*test_file.txt"
],
"browser_action": {
"default_title": "Make this page red",
......
<!--
* Copyright (c) 2011 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<script src="update.js"></script>
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Test that we can change various properties of the browser action.
// The C++ verifies.
chrome.browserAction.setTitle({title: "Modified"});
chrome.browserAction.setIcon({path: "icon2.png"});
chrome.browserAction.setBadgeText({text: "badge"});
chrome.browserAction.setBadgeBackgroundColor({color: [255,255,255,255]});
chrome.test.notifyPass();
{
"browser_action": {
"default_icon": "icon.png",
"default_title": "Default"
},
"description": "An extension testing crash http://crbug.com/57536 where cached extension causes browser action container to crash",
"name": "Test crash http://crbug.com/57536",
"manifest_version": 2,
"version": "1.0"
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
<html>
<head>
</head>
<body>
<canvas id="canvas" width="21" height="21">
<script src="background.js"></script>
</body>
</html>
......@@ -3,16 +3,16 @@
// found in the LICENSE file.
// First get an empty canvas.
var invisible = document.getElementById("canvas").getContext('2d').getImageData(
0, 0, 21, 21);
var canvas = new OffscreenCanvas(21, 21);
var invisibleImageData = canvas.getContext('2d').getImageData(0, 0, 21, 21);
// Fill the "canvas" element with some color.
var ctx = document.getElementById("canvas").getContext('2d');
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 42, 42);
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF0000';
ctx.fillRect(0, 0, 21, 21);
var visible = document.getElementById("canvas").getContext('2d').getImageData(
0, 0, 21, 21);
var visibleImageData = ctx.getImageData(0, 0, 21, 21);
function setIcon(imageData) {
return new Promise(function(resolve) {
......
......@@ -3,7 +3,7 @@
"version": "1.0",
"manifest_version": 2,
"background": {
"page": "background.html"
"scripts": ["background.js"]
},
"browser_action": {
"name": "Set toolbar icon",
......
<!--
* Copyright (c) 2011 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<html>
<head>
</head>
<body>
<canvas id="canvas" width="37" height="33">
<script src="background.js"></script>
</body>
</html>
......@@ -2,24 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Fill the "canvas" element with some color.
var ctx = document.getElementById("canvas").getContext('2d');
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 42, 42);
var canvas = new OffscreenCanvas(21, 21);
var canvasHD = new OffscreenCanvas(42, 42);
var canvas = document.getElementById("canvas").getContext('2d').getImageData(
0, 0, 21, 21);
var canvasHD = document.getElementById("canvas").getContext('2d').getImageData(
0, 0, 42, 42);
// Fill the canvases element with some color.
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF0000';
ctx.fillRect(0, 0, 21, 21);
var ctxHD = canvasHD.getContext('2d');
ctxHD.fillStyle = '#FF0000';
ctxHD.fillRect(0, 0, 42, 42);
var image = ctx.getImageData(0, 0, 21, 21);
var imageHD = ctxHD.getImageData(0, 0, 42, 42);
var setIconParamQueue = [
{imageData: canvas},
{imageData: image},
{path: 'icon.png'},
{imageData: {'21': canvas, '42': canvasHD}},
{imageData: {'21': image, '42': imageHD}},
{path: {'21': 'icon.png', '42': 'icon2.png'}},
{imageData: {'21': canvas}},
{imageData: {'21': image}},
{path: {'21': 'icon.png'}},
{imageData: {'42': canvasHD}},
{imageData: {'42': imageHD}},
{imageData: {}},
{path: {}},
];
......@@ -27,7 +32,7 @@ var setIconParamQueue = [
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(windowId) {
if (setIconParamQueue.length == 0) {
chrome.test.notifyFail("Queue of params for test cases unexpectedly empty");
chrome.test.notifyFail('Queue of params for test cases unexpectedly empty');
return;
}
......
......@@ -3,7 +3,7 @@
"version": "1.0",
"manifest_version": 2,
"background": {
"page": "background.html"
"scripts": ["background.js"]
},
"permissions": [
"tabs", "http://*/*"
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function updateBrowserAction() {
chrome.browserAction.setTitle({title: 'Modified'});
chrome.browserAction.setIcon({path: 'icon2.png'});
chrome.browserAction.setBadgeText({text: 'badge'});
chrome.browserAction.setBadgeBackgroundColor({color: [255,255,255,255]});
}
chrome.extension.isAllowedIncognitoAccess(function(isAllowedAccess) {
if (isAllowedAccess == true) {
chrome.test.sendMessage('incognito ready', function(message) {
if (message == 'incognito update') {
updateBrowserAction();
chrome.test.notifyPass();
}
});
}
});
chrome.test.sendMessage('ready', function(message) {
if (message == 'update') {
updateBrowserAction();
chrome.test.notifyPass();
}
});
{
"name": "A browser action test to verify updates to the icon and title.",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "This is the default title.",
"default_icon": "icon.png"
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment