Skip to content

Commit

Permalink
Add manual WPT for file handling. (web-platform-tests#30128)
Browse files Browse the repository at this point in the history
Note that to test this in Chrome with run_blink_wptserve.py, Chrome
must be started with:
  --ignore-certificate-errors --enable-features=FileHandlingAPI

Bug: 1240389
Change-Id: I9462a48fa8c69beb596c23ecec878c262fad3027
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3114865
Reviewed-by: Joshua Bell <[email protected]>
Reviewed-by: Daniel Murphy <[email protected]>
Commit-Queue: Evan Stade <[email protected]>
Cr-Commit-Position: refs/heads/main@{#915261}

Co-authored-by: Evan Stade <[email protected]>
  • Loading branch information
chromium-wpt-export-bot and Evan Stade authored Sep 1, 2021
1 parent ed9b278 commit 42a5790
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Some user agents only offer app installation if there is a SW and it handles
// offline requests.

const cacheVersion = "1.1";
const CACHE_NAME = `cache-v${cacheVersion}`;

// The resources cached by this service worker.
const resources = [
"file_handlers-member-manual-service-worker.js",
"file_handlers-member-manual.html",
"resources/file_handlers-member-manual.js",
"resources/icon.png",
];

// Load all resources for this service worker.
const precache = async () => {
const cache = await caches.open(CACHE_NAME);
await cache.addAll(resources);
};

// Get a resource from the cache.
const fromCache = async request => {
const cache = await caches.open(CACHE_NAME);
return await cache.match(request.url);
};

// Attempt to get resources from the network first, fallback to the cache if we're
// offline.
const networkFallbackToCache = async request => {
try {
const response = await fetch(request);
if (response.ok) return response;
} catch (err) {}
return await fromCache(request);
};

// When we have a new service worker, update the caches and swap immediately.
self.addEventListener("install", e => {
e.waitUntil(precache().then(() => self.skipWaiting()));
});

// Claim existing clients.
self.addEventListener("activate", e => {
e.waitUntil(self.clients.claim());
});

// When a resource need to be fetched, check whether it is
// contained in the cache and return the cached version, otherwise
// get it from the network.
self.addEventListener("fetch", e => {
e.respondWith(networkFallbackToCache(e.request));
});
42 changes: 42 additions & 0 deletions appmanifest/file_handlers-member/file_handlers-member-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="manifest" href="resources/file_handlers-member-manual.webmanifest" />
<title>File Handling Web Platform Test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/file_handlers-member-manual.js" type="module"></script>
<style>
body {
margin: 2em;
}

ol {
line-height: 200%;
}
</style>
</head>
<body>
<h1>
File Handling Test
</h1>
<div class="section">
<h2>
Instructions (details may be OS-dependent)
</h2>
<ol>
<li>Install this site as a PWA</li>
<li>Download <a href="resources/file_handlers-sample-file.txt" download>this file</a></li>
<li>
In your file browser, right click the downloaded file (<code>file_handlers-sample-file.txt</code>)
</li>
<li>Select <b>"Open with"</b> &gt; <b>"File Handling WPT"</b></li>
<li>
If your browser prompts you to allow <b>File Handling WPT</b> to open
the file, choose allow
</li>
<li>The app window that opens should indicate success of this test.</li>
</ol>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Since this is a manual test, disable the automatic timeout.
setup({explicit_timeout: true});

// Redirect to https if using http, because File System Access API (previously
// Native FileSystem API) isn't supported in http.
if (location.protocol !== 'https:') {
location.replace(
`https:${location.href.substring(location.protocol.length)}`
);
}

test(function() {
assert_true('serviceWorker' in navigator);
}, 'serviceWorker exists')

navigator.serviceWorker.register(
'file_handlers-member-manual-service-worker.js');

test(function() {
assert_true('launchQueue' in window);
}, 'File Handling API enabled');

test(function() {
assert_true('showOpenFilePicker' in window);
}, 'File System Access API enabled');

promise_test(async t => {
const launchParams = await new Promise(resolve => {
window.launchQueue.setConsumer(resolve);
});

assert_equals(launchParams.files.length, 1, 'Wrong number of files found');

const readHandle = await launchParams.files[0].getFile();
assert_equals(readHandle.name, 'file_handlers-sample-file.txt');

const fileContents = await readHandle.text();
assert_equals(fileContents, 'File handling WPT - Hello world!\n');
}, 'launchQueue works as expected');
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "File Handling WPT",
"icons": [
{
"src": "icon.png",
"sizes": "144x144"
}
],
"start_url": "../file_handlers-member-manual.html",
"display": "standalone",
"scope": "../../file_handlers-member/",
"file_handlers": [
{
"action": "../file_handlers-member-manual.html",
"name": "Plain Text",
"accept": {
"text/plain": [".txt"]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File handling WPT - Hello world!
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 42a5790

Please sign in to comment.