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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/base/web_ui_test_data_source.h"
#include "components/guest_view/browser/guest_view_base.h"
#include "components/guest_view/browser/guest_view_manager_delegate.h"
#include "components/guest_view/browser/test_guest_view_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/event_router.h"
#include "extensions/common/api/test.h"
#include "extensions/common/extension.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/test/extension_test_message_listener.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
namespace extensions {
namespace OnMessage = api::test::OnMessage;
namespace {
#if !BUILDFLAG(IS_WIN) // flaky http://crbug.com/530722
// Tests running extension APIs on WebUI.
class ExtensionWebUITest : public ExtensionApiTest {
protected:
testing::AssertionResult RunTest(const char* name,
const GURL& page_url,
bool expected_result) {
std::string script;
{
base::ScopedAllowBlockingForTesting allow_blocking;
// Tests are located in chrome/test/data/extensions/webui/$(name).
base::FilePath path;
base::PathService::Get(chrome::DIR_TEST_DATA, &path);
path =
path.AppendASCII("extensions").AppendASCII("webui").AppendASCII(name);
// Read the test.
if (!base::PathExists(path))
return testing::AssertionFailure() << "Couldn't find " << path.value();
base::ReadFileToString(path, &script);
script = "(function(){'use strict';" + script + "}());";
}
// Run the test.
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), page_url));
content::RenderFrameHost* webui = browser()
->tab_strip_model()
->GetActiveWebContents()
->GetPrimaryMainFrame();
if (!webui)
return testing::AssertionFailure() << "Failed to navigate to WebUI";
bool actual_result = content::EvalJs(webui, script).ExtractBool();
return (expected_result == actual_result)
? testing::AssertionSuccess()
: (testing::AssertionFailure() << "Check console output");
}
testing::AssertionResult RunTestOnExtensionsPage(const char* name) {
return RunTest(name, GURL("chrome://extensions"), true);
}
testing::AssertionResult RunTestOnAboutPage(const char* name) {
// chrome://about is an innocuous page that doesn't have any bindings.
// Tests should fail.
return RunTest(name, GURL("chrome://about"), false);
}
};
// Tests running within an <extensionoptions>.
class ExtensionWebUIEmbeddedOptionsTest
: public ExtensionWebUITest,
public testing::WithParamInterface<bool> {
public:
static std::string DescribeParams(
const testing::TestParamInfo<ParamType>& info) {
return info.param ? "MPArch" : "InnerWebContents";
}
ExtensionWebUIEmbeddedOptionsTest() {
scoped_feature_list_.InitWithFeatureState(features::kGuestViewMPArch,
GetParam());
}
void SetUpOnMainThread() override {
ExtensionWebUITest::SetUpOnMainThread();
test_guest_view_manager_ =
test_guest_view_manager_factory_.GetOrCreateTestGuestViewManager(
browser()->profile(),
ExtensionsAPIClient::Get()->CreateGuestViewManagerDelegate());
}
void TearDownOnMainThread() override {
test_guest_view_manager_ = nullptr;
ExtensionWebUITest::TearDownOnMainThread();
}
protected:
// Loads |extension|'s options page in an <extensionoptions> and returns the
// <extensionoptions>'s main RenderFrameHost.
content::RenderFrameHost* OpenExtensionOptions(const Extension* extension) {
EXPECT_TRUE(ui_test_utils::NavigateToURL(
browser(), GURL(chrome::kChromeUIExtensionsURL)));
content::WebContents* webui =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(0U, test_guest_view_manager_->num_guests_created());
EXPECT_TRUE(content::ExecJs(
webui, content::JsReplace(
"(() => {"
" let extensionoptions = "
" document.createElement('extensionoptions');"
" extensionoptions.extension = $1;"
" return new Promise((resolve) => {"
" extensionoptions.addEventListener('load', () => {"
" resolve();"
" });"
" document.body.appendChild(extensionoptions);"
" });"
"})();",
extension->id())));
guest_view::GuestViewBase* guest_view =
test_guest_view_manager_->WaitForSingleGuestViewCreated();
EXPECT_TRUE(guest_view);
test_guest_view_manager_->WaitUntilAttached(guest_view);
return guest_view->GetGuestMainFrame();
}
private:
guest_view::TestGuestViewManagerFactory test_guest_view_manager_factory_;
raw_ptr<guest_view::TestGuestViewManager> test_guest_view_manager_ = nullptr;
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(/* no prefix */,
ExtensionWebUIEmbeddedOptionsTest,
testing::Bool(),
ExtensionWebUIEmbeddedOptionsTest::DescribeParams);
IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, ConfidenceCheckAvailableAPIs) {
ASSERT_TRUE(RunTestOnExtensionsPage("confidence_check_available_apis.js"));
}
IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, ConfidenceCheckUnavailableAPIs) {
ASSERT_TRUE(RunTestOnAboutPage("confidence_check_available_apis.js"));
}
// Tests chrome.test.sendMessage, which exercises WebUI making a
// function call and receiving a response.
IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SendMessage) {
ExtensionTestMessageListener ping_listener("ping", ReplyBehavior::kWillReply);
ASSERT_TRUE(RunTestOnExtensionsPage("send_message.js"));
ASSERT_TRUE(ping_listener.WaitUntilSatisfied());
ExtensionTestMessageListener result_listener;
ping_listener.Reply("pong");
ASSERT_TRUE(result_listener.WaitUntilSatisfied());
EXPECT_EQ("true", result_listener.message());
}
// Tests chrome.runtime.onMessage, which exercises WebUI registering and
// receiving an event.
IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, OnMessage) {
ASSERT_TRUE(RunTestOnExtensionsPage("on_message.js"));
ExtensionTestMessageListener result_listener;
OnMessage::Info info;
info.data = "hi";
info.last_message = true;
EventRouter::Get(profile())->BroadcastEvent(base::WrapUnique(
new Event(events::RUNTIME_ON_MESSAGE, OnMessage::kEventName,
OnMessage::Create(info))));
ASSERT_TRUE(result_listener.WaitUntilSatisfied());
EXPECT_EQ("true", result_listener.message());
}
// Tests chrome.runtime.lastError, which exercises WebUI accessing a property
// on an API which it doesn't actually have access to. A bindings test really.
IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, RuntimeLastError) {
ExtensionTestMessageListener ping_listener("ping", ReplyBehavior::kWillReply);
ASSERT_TRUE(RunTestOnExtensionsPage("runtime_last_error.js"));
ASSERT_TRUE(ping_listener.WaitUntilSatisfied());
ExtensionTestMessageListener result_listener;
ping_listener.ReplyWithError("unknown host");
ASSERT_TRUE(result_listener.WaitUntilSatisfied());
EXPECT_EQ("true", result_listener.message());
}
IN_PROC_BROWSER_TEST_P(ExtensionWebUIEmbeddedOptionsTest,
CanEmbedExtensionOptions) {
ExtensionTestMessageListener ready_listener("ready",
ReplyBehavior::kWillReply);
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("extension_options")
.AppendASCII("extension_with_options_page"));
ASSERT_TRUE(extension);
ASSERT_TRUE(RunTestOnExtensionsPage("can_embed_extension_options.js"));
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
ExtensionTestMessageListener load_listener("load");
ready_listener.Reply(extension->id());
ASSERT_TRUE(load_listener.WaitUntilSatisfied());
}
// Tests that an <extensionoptions> guest view can access appropriate APIs,
// including chrome.storage (semi-privileged; exposed to trusted contexts and
// contexts like content scripts and embedded resources in platform apps) and
// chrome.tabs (privileged; only exposed to trusted contexts).
IN_PROC_BROWSER_TEST_P(ExtensionWebUIEmbeddedOptionsTest,
ExtensionOptionsCanAccessAppropriateAPIs) {
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("extension_options")
.AppendASCII("extension_with_options_page"));
ASSERT_TRUE(extension);
auto* guest_render_frame_host = OpenExtensionOptions(extension);
// Check access to the storage API, both for getting/setting values and being
// notified of changes.
const std::string storage_key = "test";
const int storage_value = 42;
EXPECT_TRUE(content::ExecJs(
guest_render_frame_host,
content::JsReplace("var onChangedPromise = new Promise((resolve) => {"
" chrome.storage.onChanged.addListener((change) => {"
" resolve(change[$1].newValue);"
" });"
"});",
storage_key),
content::EXECUTE_SCRIPT_NO_RESOLVE_PROMISES));
ASSERT_EQ(
"success",
content::EvalJs(
guest_render_frame_host,
content::JsReplace(
"try {"
" new Promise(resolve => {"
" chrome.storage.local.set({$1: $2}, () => {"
" resolve("
" chrome.runtime.lastError ?"
" chrome.runtime.lastError.message : 'success');"
" });"
" });"
"} catch (e) {"
" e.name + ': ' + e.message;"
"}",
storage_key, storage_value)));
EXPECT_EQ(storage_value,
content::EvalJs(
guest_render_frame_host,
content::JsReplace("new Promise(resolve =>"
" chrome.storage.local.get((storage) => "
" resolve(storage[$1])));",
storage_key)));
EXPECT_EQ(storage_value,
content::EvalJs(guest_render_frame_host, "onChangedPromise;"));
// Now check access to the tabs API, which is restricted to
// mojom::ContextType::kPrivilegedExtensions (which this should be).
static constexpr char kTabsExecution[] =
R"(new Promise(r => {
chrome.tabs.create({}, (tab) => {
let message;
// Confidence check that it looks and smells like a tab.
if (tab && tab.index) {
message = 'success';
} else {
message = chrome.runtime.lastError ?
chrome.runtime.lastError.message :
'Unknown error';
}
r(message);
});
});)";
EXPECT_EQ("success",
content::EvalJs(guest_render_frame_host, kTabsExecution));
}
IN_PROC_BROWSER_TEST_P(ExtensionWebUIEmbeddedOptionsTest,
ExtensionOptionsExternalLinksOpenInNewTab) {
// TODO(crbug.com/40202416): Implement new window creation under MPArch.
if (base::FeatureList::IsEnabled(features::kGuestViewMPArch)) {
GTEST_SKIP() << "MPArch implementation skipped. https://crbug.com/40202416";
}
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("extension_options")
.AppendASCII("extension_with_options_page"));
ASSERT_TRUE(extension);
auto* guest_render_frame_host = OpenExtensionOptions(extension);
content::WebContentsAddedObserver new_contents_observer;
EXPECT_TRUE(content::ExecJs(guest_render_frame_host,
"document.getElementById('link').click();"));
content::WebContents* new_contents = new_contents_observer.GetWebContents();
EXPECT_NE(TabStripModel::kNoTab,
browser()->tab_strip_model()->GetIndexOfWebContents(new_contents));
}
IN_PROC_BROWSER_TEST_P(ExtensionWebUIEmbeddedOptionsTest,
ReceivesExtensionOptionsOnClose) {
ExtensionTestMessageListener ready_listener("ready",
ReplyBehavior::kWillReply);
const Extension* extension =
InstallExtension(test_data_dir_.AppendASCII("extension_options")
.AppendASCII("close_self"), 1);
ASSERT_TRUE(extension);
ASSERT_TRUE(
RunTestOnExtensionsPage("receives_extension_options_on_close.js"));
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
ExtensionTestMessageListener onclose_listener("onclose received");
ready_listener.Reply(extension->id());
ASSERT_TRUE(onclose_listener.WaitUntilSatisfied());
}
// Regression test for crbug.com/414526.
//
// Same setup as CanEmbedExtensionOptions but disable the extension before
// embedding.
IN_PROC_BROWSER_TEST_P(ExtensionWebUIEmbeddedOptionsTest,
EmbedDisabledExtension) {
ExtensionTestMessageListener ready_listener("ready",
ReplyBehavior::kWillReply);
std::string extension_id;
{
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("extension_options")
.AppendASCII("extension_with_options_page"));
ASSERT_TRUE(extension);
extension_id = extension->id();
DisableExtension(extension_id);
}
ASSERT_TRUE(RunTestOnExtensionsPage("can_embed_extension_options.js"));
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
ExtensionTestMessageListener create_failed_listener("createfailed");
ready_listener.Reply(extension_id);
ASSERT_TRUE(create_failed_listener.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, EmbedInvalidExtension) {
ExtensionTestMessageListener ready_listener("ready",
ReplyBehavior::kWillReply);
const std::string extension_id = "thisisprobablynotrealextensionid";
ASSERT_TRUE(RunTestOnExtensionsPage("can_embed_extension_options.js"));
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
ExtensionTestMessageListener create_failed_listener("createfailed");
ready_listener.Reply(extension_id);
ASSERT_TRUE(create_failed_listener.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, EmbedExtensionWithoutOptionsPage) {
ExtensionTestMessageListener ready_listener("ready",
ReplyBehavior::kWillReply);
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("extension_options")
.AppendASCII("extension_without_options_page"));
ASSERT_TRUE(extension);
ASSERT_TRUE(RunTestOnExtensionsPage("can_embed_extension_options.js"));
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
ExtensionTestMessageListener create_failed_listener("createfailed");
ready_listener.Reply(extension->id());
ASSERT_TRUE(create_failed_listener.WaitUntilSatisfied());
}
class ExtensionWebUIListenersTest : public ExtensionWebUITest {
public:
void SetUpOnMainThread() override {
ExtensionWebUITest::SetUpOnMainThread();
// Load browser_tests.pak.
base::FilePath pak_path;
ASSERT_TRUE(base::PathService::Get(base::DIR_ASSETS, &pak_path));
pak_path = pak_path.AppendASCII("browser_tests.pak");
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
pak_path, ui::kScaleFactorNone);
// Register the chrome://webui-test data source.
webui::CreateAndAddWebUITestDataSource(profile());
}
};
// Tests crbug.com/1253745 where adding and removing listeners in a WebUI frame
// causes all listeners to be removed.
IN_PROC_BROWSER_TEST_F(ExtensionWebUIListenersTest, MultipleURLListeners) {
// Use the same URL both for the parent and child frames for convenience.
// These could be different WebUI URLs.
GURL test_url("chrome://webui-test/extension_webui_listeners_test.html");
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), test_url));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::RenderFrameHost* main_frame = web_contents->GetPrimaryMainFrame();
EventRouter* event_router = EventRouter::Get(profile());
EXPECT_FALSE(event_router->HasEventListener("test.onMessage"));
// Register a listener and create a child frame at a different URL.
content::TestNavigationObserver observer(web_contents);
EXPECT_TRUE(content::ExecJs(main_frame, R"(
var listener = e => {};
chrome.test.onMessage.addListener(listener);
const iframe = document.createElement('iframe');
iframe.src = 'chrome://webui-test/extension_webui_listeners_test.html';
document.body.appendChild(iframe);
)"));
EXPECT_TRUE(event_router->HasEventListener("test.onMessage"));
observer.Wait();
// Add and remove the listener in the child frame.
content::RenderFrameHost* child_frame = ChildFrameAt(main_frame, 0);
EXPECT_EQ(test_url, child_frame->GetLastCommittedURL());
EXPECT_TRUE(content::ExecJs(child_frame, R"(
const listener = e => {};
chrome.test.onMessage.addListener(listener);
chrome.test.onMessage.removeListener(listener);
)"));
EXPECT_TRUE(event_router->HasEventListener("test.onMessage"));
// Now remove last listener from main frame.
EXPECT_TRUE(content::ExecJs(main_frame, R"(
chrome.test.onMessage.removeListener(listener);
)"));
EXPECT_FALSE(event_router->HasEventListener("test.onMessage"));
}
#endif
} // namespace
} // namespace extensions
|