1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
// Copyright (c) 2012 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.
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/test_extension_dir.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/extensions/features/feature_channel.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/test/extension_test_message_listener.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace extensions {
namespace {
const char kDeclarativeContentManifest[] =
"{\n"
" \"name\": \"Declarative Content apitest\",\n"
" \"version\": \"0.1\",\n"
" \"manifest_version\": 2,\n"
" \"description\": \n"
" \"end-to-end browser test for the declarative Content API\",\n"
" \"background\": {\n"
" \"scripts\": [\"background.js\"]\n"
" },\n"
" \"page_action\": {},\n"
" \"permissions\": [\n"
" \"declarativeContent\"\n"
" ]\n"
"}\n";
const char kBackgroundHelpers[] =
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"var ShowPageAction = chrome.declarativeContent.ShowPageAction;\n"
"var onPageChanged = chrome.declarativeContent.onPageChanged;\n"
"var Reply = window.domAutomationController.send.bind(\n"
" window.domAutomationController);\n"
"\n"
"function setRules(rules, responseString) {\n"
" onPageChanged.removeRules(undefined, function() {\n"
" onPageChanged.addRules(rules, function() {\n"
" if (chrome.runtime.lastError) {\n"
" Reply(chrome.runtime.lastError.message);\n"
" return;\n"
" }\n"
" Reply(responseString);\n"
" });\n"
" });\n"
"};\n";
class DeclarativeContentApiTest : public ExtensionApiTest {
public:
DeclarativeContentApiTest()
// Set the channel to "trunk" since declarativeContent is restricted
// to trunk.
: current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {
}
~DeclarativeContentApiTest() override {}
extensions::ScopedCurrentChannel current_channel_;
TestExtensionDir ext_dir_;
};
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, Overview) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(
FILE_PATH_LITERAL("background.js"),
"var declarative = chrome.declarative;\n"
"\n"
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"var ShowPageAction = chrome.declarativeContent.ShowPageAction;\n"
"\n"
"var rule0 = {\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test1\"}}),\n"
" new PageStateMatcher({\n"
" css: [\"input[type='password']\"]})],\n"
" actions: [new ShowPageAction()]\n"
"}\n"
"\n"
"var testEvent = chrome.declarativeContent.onPageChanged;\n"
"\n"
"testEvent.removeRules(undefined, function() {\n"
" testEvent.addRules([rule0], function() {\n"
" chrome.test.sendMessage(\"ready\", function(reply) {\n"
" })\n"
" });\n"
"});\n");
ExtensionTestMessageListener ready("ready", true);
const Extension* extension = LoadExtension(ext_dir_.unpacked_path());
ASSERT_TRUE(extension);
const ExtensionAction* page_action =
ExtensionActionManager::Get(browser()->profile())->
GetPageAction(*extension);
ASSERT_TRUE(page_action);
ASSERT_TRUE(ready.WaitUntilSatisfied());
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
NavigateInRenderer(tab, GURL("http://test1/"));
// The declarative API should show the page action instantly, rather
// than waiting for the extension to run.
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
// Make sure leaving a matching page unshows the page action.
NavigateInRenderer(tab, GURL("http://not_checked/"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
// Insert a password field to make sure that's noticed.
// Notice that we touch offsetTop to force a synchronous layout.
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = '<input type=\"password\">';"
"document.body.offsetTop;"));
// Give the style match a chance to run and send back the matching-selector
// update. This takes one time through the Blink message loop to apply the
// style to the new element, and a second to dedupe updates.
// FIXME: Remove this after https://codereview.chromium.org/145663012/
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
EXPECT_TRUE(page_action->GetIsVisible(tab_id))
<< "Adding a matching element should show the page action.";
// Remove it again to make sure that reverts the action.
// Notice that we touch offsetTop to force a synchronous layout.
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = 'Hello world';"
"document.body.offsetTop;"));
// Give the style match a chance to run and send back the matching-selector
// update. This takes one time through the Blink message loop to apply the
// style to the new element, and a second to dedupe updates.
// FIXME: Remove this after https://codereview.chromium.org/145663012/
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
EXPECT_FALSE(page_action->GetIsVisible(tab_id))
<< "Removing the matching element should hide the page action again.";
}
// http://crbug.com/304373
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
UninstallWhileActivePageAction) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
const Extension* extension = LoadExtension(ext_dir_.unpacked_path());
ASSERT_TRUE(extension);
const std::string extension_id = extension->id();
const ExtensionAction* page_action = ExtensionActionManager::Get(
browser()->profile())->GetPageAction(*extension);
ASSERT_TRUE(page_action);
const std::string kTestRule =
"setRules([{\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test\"}})],\n"
" actions: [new ShowPageAction()]\n"
"}], 'test_rule');\n";
EXPECT_EQ("test_rule",
ExecuteScriptInBackgroundPage(extension_id, kTestRule));
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
NavigateInRenderer(tab, GURL("http://test/"));
EXPECT_TRUE(page_action->GetIsVisible(tab_id));
EXPECT_TRUE(WaitForPageActionVisibilityChangeTo(1));
EXPECT_EQ(1u, extension_action_test_util::GetVisiblePageActionCount(tab));
EXPECT_EQ(1u, extension_action_test_util::GetTotalPageActionCount(tab));
ReloadExtension(extension_id); // Invalidates page_action and extension.
EXPECT_EQ("test_rule",
ExecuteScriptInBackgroundPage(extension_id, kTestRule));
// TODO(jyasskin): Apply new rules to existing tabs, without waiting for a
// navigation.
NavigateInRenderer(tab, GURL("http://test/"));
EXPECT_TRUE(WaitForPageActionVisibilityChangeTo(1));
EXPECT_EQ(1u, extension_action_test_util::GetVisiblePageActionCount(tab));
EXPECT_EQ(1u, extension_action_test_util::GetTotalPageActionCount(tab));
UnloadExtension(extension_id);
NavigateInRenderer(tab, GURL("http://test/"));
EXPECT_TRUE(WaitForPageActionVisibilityChangeTo(0));
EXPECT_EQ(0u, extension_action_test_util::GetVisiblePageActionCount(tab));
EXPECT_EQ(0u, extension_action_test_util::GetTotalPageActionCount(tab));
}
// This tests against a renderer crash that was present during development.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
DISABLED_AddExtensionMatchingExistingTabWithDeadFrames) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
const int tab_id = ExtensionTabUtil::GetTabId(tab);
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = '<iframe src=\"http://test2\">';"));
// Replace the iframe to destroy its WebFrame.
ASSERT_TRUE(content::ExecuteScript(
tab, "document.body.innerHTML = '<span class=\"foo\">';"));
const Extension* extension = LoadExtension(ext_dir_.unpacked_path());
ASSERT_TRUE(extension);
const ExtensionAction* page_action = ExtensionActionManager::Get(
browser()->profile())->GetPageAction(*extension);
ASSERT_TRUE(page_action);
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
EXPECT_EQ("rule0",
ExecuteScriptInBackgroundPage(
extension->id(),
"setRules([{\n"
" conditions: [new PageStateMatcher({\n"
" css: [\"span[class=foo]\"]})],\n"
" actions: [new ShowPageAction()]\n"
"}], 'rule0');\n"));
// Give the renderer a chance to apply the rules change and notify the
// browser. This takes one time through the Blink message loop to receive
// the rule change and apply the new stylesheet, and a second to dedupe the
// update.
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
EXPECT_FALSE(tab->IsCrashed());
EXPECT_TRUE(page_action->GetIsVisible(tab_id))
<< "Loading an extension when an open page matches its rules "
<< "should show the page action.";
EXPECT_EQ("removed",
ExecuteScriptInBackgroundPage(
extension->id(),
"onPageChanged.removeRules(undefined, function() {\n"
" window.domAutomationController.send('removed');\n"
"});\n"));
EXPECT_FALSE(page_action->GetIsVisible(tab_id));
}
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
ShowPageActionWithoutPageAction) {
std::string manifest_without_page_action = kDeclarativeContentManifest;
ReplaceSubstringsAfterOffset(
&manifest_without_page_action, 0, "\"page_action\": {},", "");
ext_dir_.WriteManifest(manifest_without_page_action);
ext_dir_.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundHelpers);
const Extension* extension = LoadExtension(ext_dir_.unpacked_path());
ASSERT_TRUE(extension);
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"setRules([{\n"
" conditions: [new PageStateMatcher({\n"
" pageUrl: {hostPrefix: \"test\"}})],\n"
" actions: [new ShowPageAction()]\n"
"}], 'test_rule');\n"),
testing::HasSubstr("without a page action"));
content::WebContents* const tab =
browser()->tab_strip_model()->GetWebContentsAt(0);
NavigateInRenderer(tab, GURL("http://test/"));
EXPECT_EQ(NULL,
ExtensionActionManager::Get(browser()->profile())->
GetPageAction(*extension));
EXPECT_EQ(0u, extension_action_test_util::GetVisiblePageActionCount(tab));
}
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
CanonicalizesPageStateMatcherCss) {
ext_dir_.WriteManifest(kDeclarativeContentManifest);
ext_dir_.WriteFile(
FILE_PATH_LITERAL("background.js"),
"var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
"function Return(obj) {\n"
" window.domAutomationController.send('' + obj);\n"
"}\n");
const Extension* extension = LoadExtension(ext_dir_.unpacked_path());
ASSERT_TRUE(extension);
EXPECT_EQ("input[type=\"password\"]",
ExecuteScriptInBackgroundPage(
extension->id(),
"var psm = new PageStateMatcher(\n"
" {css: [\"input[type='password']\"]});\n"
"Return(psm.css);"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
" new PageStateMatcher({css: 'Not-an-array'});\n"
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("css.*Expected 'array'"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
" new PageStateMatcher({css: [null]});\n" // Not a string.
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("css\\.0.*Expected 'string'"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
// Invalid CSS:
" new PageStateMatcher({css: [\"input''\"]});\n"
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("valid.*: input''$"));
EXPECT_THAT(ExecuteScriptInBackgroundPage(
extension->id(),
"try {\n"
// "Complex" selector:
" new PageStateMatcher({css: ['div input']});\n"
" Return('Failed to throw');\n"
"} catch (e) {\n"
" Return(e.message);\n"
"}\n"),
testing::ContainsRegex("compound selector.*: div input$"));
}
} // namespace
} // namespace extensions
|