Skip to content

Commit

Permalink
Prerender: Test "throw" restriction APIs using wpt-internal
Browse files Browse the repository at this point in the history
This CL changes the following prerender browser tests to wpt-internal:
- LocalFileSystemAccessError
- AsyncClipboardAccessError
- PointerLockError
It also changes the expected message sent from the prerendering page
while testing the PresentationRequest API.

Bug: 1158250
Change-Id: Ie60c81fc480be2d62530a6e93309deb86bf22249
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2784538
Commit-Queue: Lingqi Chi <lingqi@chromium.org>
Reviewed-by: Takashi Toyoshima <toyoshim@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#868019}
  • Loading branch information
Clqsin45 authored and Chromium LUCI CQ committed Mar 31, 2021
1 parent 2ccb024 commit 21cf0f9
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 143 deletions.
99 changes: 0 additions & 99 deletions content/browser/prerender/prerender_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1567,43 +1567,6 @@ IN_PROC_BROWSER_TEST_P(PrerenderBrowserTest, CSPDefaultSrc) {
}
}

// TODO(https://crbug.com/1182032): Now the File System Access API is not
// supported on Android. Enable this browser test after
// https://crbug.com/1011535 is fixed.
#if defined(OS_ANDROID)
#define MAYBE_LocalFileSystemAccessError DISABLED_LocalFileSystemAccessError
#else
#define MAYBE_LocalFileSystemAccessError LocalFileSystemAccessError
#endif

// Tests that the prerendering page cannot access local file system via the File
// System Access API.
IN_PROC_BROWSER_TEST_P(PrerenderBrowserTest, MAYBE_LocalFileSystemAccessError) {
const GURL kInitialUrl = GetUrl("/prerender/add_prerender.html");
const GURL kPrerenderingUrl = GetUrl("/empty.html");

// Navigate to an initial page.
ASSERT_TRUE(NavigateToURL(shell(), kInitialUrl));

// Start a prerender.
AddPrerender(kPrerenderingUrl);
PrerenderHostRegistry& registry = GetPrerenderHostRegistry();
PrerenderHost* prerender_host =
registry.FindHostByUrlForTesting(kPrerenderingUrl);
ASSERT_TRUE(prerender_host);
RenderFrameHostImpl* prerender_render_frame_host =
prerender_host->GetPrerenderedMainFrameHost();

// Executing showOpenFilePicker() on the prerendered page should fail.
auto result =
EvalJs(prerender_render_frame_host, "window.showOpenFilePicker()",
EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(result.error,
::testing::HasSubstr(
"Failed to execute 'showOpenFilePicker' on 'Window': Must be "
"handling a user gesture to show a file picker."));
}

// TODO(https://crbug.com/1182032): Now the File System Access API is not
// supported on Android. Enable this browser test after
// https://crbug.com/1011535 is fixed.
Expand Down Expand Up @@ -1729,68 +1692,6 @@ IN_PROC_BROWSER_TEST_P(PrerenderBrowserTest, ClipboardByExecCommandFail) {
EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE));
}

// Tests that prerendering pages cannot access the Async Clipboard API because
// they are not focused.
// This cannot be upstreamed as a WPT test because the spec (probably) will
// require that no error is thrown until activation.
IN_PROC_BROWSER_TEST_P(PrerenderBrowserTest, AsyncClipboardAccessError) {
const GURL kInitialUrl = GetUrl("/prerender/add_prerender.html");
const GURL kPrerenderingUrl = GetUrl("/empty.html");

// Navigate to an initial page.
ASSERT_TRUE(NavigateToURL(shell(), kInitialUrl));

// Make a prerendered page.
AddPrerender(kPrerenderingUrl);

PrerenderHostRegistry& registry = GetPrerenderHostRegistry();
PrerenderHost* prerender_host =
registry.FindHostByUrlForTesting(kPrerenderingUrl);
ASSERT_TRUE(prerender_host);
RenderFrameHostImpl* prerendered_render_frame_host =
prerender_host->GetPrerenderedMainFrameHost();

// Accessing Clipboard on prerendering pages should fail because the
// prerendering documents are not focused.
// https://w3c.github.io/clipboard-apis/#privacy-async
auto result = EvalJs(prerendered_render_frame_host,
"navigator.clipboard.writeText(location.href);",
EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(result.error, ::testing::HasSubstr(
"NotAllowedError: Document is not focused."));
}

// Tests that the Pointer Lock API is not valid on prerendering pages.
// This cannot be upstreamed as a WPT test because the spec (probably) will
// require that no error is thrown until activation.
IN_PROC_BROWSER_TEST_P(PrerenderBrowserTest, PointerLockError) {
const GURL kInitialUrl = GetUrl("/prerender/add_prerender.html");
const GURL kPrerenderingUrl =
GetUrl("/prerender/restriction_pointer_lock.html");

// Navigate to an initial page.
ASSERT_TRUE(NavigateToURL(shell(), kInitialUrl));

// Start a prerender.
AddPrerender(kPrerenderingUrl);
PrerenderHostRegistry& registry = GetPrerenderHostRegistry();
PrerenderHost* prerender_host =
registry.FindHostByUrlForTesting(kPrerenderingUrl);
ASSERT_TRUE(prerender_host);
RenderFrameHostImpl* prerender_render_frame_host =
prerender_host->GetPrerenderedMainFrameHost();

// The Pointer Lock API is not engaged on prerendering pages because the
// prerendering documents are not focused.
// https://w3c.github.io/pointerlock/#extensions-to-the-element-interface
auto result =
EvalJs(prerender_render_frame_host, "canvas.requestPointerLock();",
EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE);
EXPECT_THAT(result.error, ::testing::HasSubstr(
"WrongDocumentError: The root document of this "
"element is not valid for pointer lock."));
}

// End: Tests for feature restrictions in prerendered pages ====================

// Tests that prerendering doesn't run for low-end devices.
Expand Down
20 changes: 0 additions & 20 deletions content/test/data/prerender/restriction_broadcast_channel.html

This file was deleted.

16 changes: 0 additions & 16 deletions content/test/data/prerender/restriction_pointer_lock.html

This file was deleted.

3 changes: 3 additions & 0 deletions third_party/blink/web_tests/TestExpectations
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ crbug.com/1093466 virtual/portals/wpt_internal/portals/* [ Pass ]
crbug.com/1126305 wpt_internal/prerender/* [ Skip ]
crbug.com/1126305 virtual/prerender/wpt_internal/prerender/* [ Pass ]

## prerender test: the File System Access API is not supported on Android ##
crbug.com/1182032 [ Android ] virtual/prerender/wpt_internal/prerender/restriction-local-file-system-access.https.html [ Skip ]

# These tests require BFCache, which is currently disabled by default on Desktop.
# Keep this in sync with VirtualTestSuites.
crbug.com/1171298 http/tests/inspector-protocol/bfcache/* [ Skip ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

assert_true(document.prerendering);

async function asyncAccessClipboard(){
const bc = new BroadcastChannel('prerender-channel');

try {
const _ = await navigator.clipboard.writeText(location.href);
bc.postMessage('unexpected success');
} catch (err){
bc.postMessage(err.name);
} finally {
bc.close();
}
}

asyncAccessClipboard();

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

assert_true(document.prerendering);

async function showFilePicker(){
const bc = new BroadcastChannel('prerender-channel');

try {
const _ = await window.showOpenFilePicker()
bc.postMessage('unexpected success');
} catch (err){
bc.postMessage(err.name);
} finally {
bc.close();
}
}

showFilePicker();

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<canvas id="pcanvas" width="320" height="320">
This is a canvas.
</canvas>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

assert_true(document.prerendering);

async function requestPointerLock(){
const bc = new BroadcastChannel('prerender-channel');
try {
const canvas = document.getElementById('pcanvas');
const _ = await canvas.requestPointerLock();
bc.postMessage('unexpected success');
} catch (err){
bc.postMessage(err.name);
} finally {
bc.close();
}
}

requestPointerLock();

</script>
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

assert_true(document.prerendering);

async function startPresentationRequest(){
async function startPresentationRequest() {
const bc = new BroadcastChannel('prerender-channel');
const presentationRequest = new PresentationRequest(
'https://example.com/presentation.html');
'https://example.com/presentation.html');

try {
result = await presentationRequest.start();
const _ = await presentationRequest.start();
bc.postMessage('unexpected success');
} catch (err){
bc.postMessage(err.message);
} catch (err) {
bc.postMessage('request failed');
} finally {
bc.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<!-- This cannot be upstreamed as a WPT test because the spec (probably) will
require that no error is thrown until activation. -->
<title>Same-origin prerendering cannot access the Async Clipboard API</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
<body>
<script>

promise_test(async t => {
const bc = new BroadcastChannel('prerender-channel');

const gotMessage = new Promise(resolve => {
bc.addEventListener('message', e => {
resolve(e.data);
}, {
once: true
});
});

// Start prerendering a page that attempts to access the clipboard via the
// Async Clipboard API.
startPrerendering(`resources/async-clipboard.html`);
const result = await gotMessage;
assert_equals(result, 'NotAllowedError');
}, `prerendering pages should not be able to access the clipboard via the ` +
`Async Clipboard API.`);

</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<!-- This cannot be upstreamed as a WPT test because the spec (probably) will
require that no error is thrown until activation. -->
<title>Same-origin prerendering cannot access local file system</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
<body>
<script>

promise_test(async t => {
const bc = new BroadcastChannel('prerender-channel');

const gotMessage = new Promise(resolve => {
bc.addEventListener('message', e => {
resolve(e.data);
}, {
once: true
});
});

// Start prerendering a page that attempts to show a local file picker.
startPrerendering(`resources/file-picker.html`);
const result = await gotMessage;
assert_equals(result, 'SecurityError')
}, `prerendering pages should not be able to access the local file system ` +
`via the File System Access API`);

</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<!-- This cannot be upstreamed as a WPT test because the spec (probably) will
require that no error is thrown until activation. -->
<title>Same-origin prerendering cannot request PointerLock</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
<body>
<script>

promise_test(async t => {
const bc = new BroadcastChannel('prerender-channel');

const gotMessage = new Promise(resolve => {
bc.addEventListener('message', e => {
resolve(e.data);
}, {
once: true
});
});

// Start prerendering a page that attempts to request pointer lock.
startPrerendering(`resources/pointer-lock.html`);
const result = await gotMessage;
assert_equals(result, 'WrongDocumentError');
}, 'prerendering pages should not be able to request pointer lock');

</script>
</body>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!DOCTYPE html>
<title>Same-origin prerendering cannot start presentation</title>
<!-- This cannot be upstreamed as a WPT test because the spec (probably) will
require that no error is thrown until activation. -->
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
Expand All @@ -21,9 +23,7 @@
startPrerendering(`resources/presentation-request.html`);

const result = await gotMessage;
assert_equals(result,
"Failed to execute 'start' on 'PresentationRequest': PresentationRequest::start() requires user gesture."
);
assert_equals(result, 'request failed');
}, 'prerendering page cannot start presentation');

</script>
Expand Down

0 comments on commit 21cf0f9

Please sign in to comment.