Skip to content

Added new tests directory: webshare. #6287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions interfaces/web-share.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://wicg.github.io/web-share/

partial interface Navigator {
[SecureContext]
Promise<void> share(ShareData data);
};

dictionary ShareData {
USVString title;
USVString text;
USVString url;
};
2 changes: 2 additions & 0 deletions web-share/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@mgiuca
@marcoscaceres
32 changes: 32 additions & 0 deletions web-share/idlharness.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare IDL test</title>
<link rel="help" href="https://wicg.github.io/web-share/">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/resources/WebIDLParser.js></script>
<script src=/resources/idlharness.js></script>
</head>
<body>
<script>
"use strict";
var idl_array = new IdlArray();

function doTest(idl) {
idl_array.add_untested_idls('interface Navigator {};');
idl_array.add_idls(idl);
idl_array.add_objects({
Navigator: ['navigator']
});
idl_array.test();
}

promise_test(async () => {
const response = await fetch('../interfaces/web-share.idl');
doTest(await response.text());
}, 'Test driver');
</script>
</body>
</html>
78 changes: 78 additions & 0 deletions web-share/resources/manual-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Internal function. Returns [instruction, list] DOM elements.
function setupManualShareTestCommon() {
const div = document.createElement('div');
document.body.appendChild(div);

const instruction = document.createElement('div');
instruction.id = 'instruction';
div.appendChild(instruction);

const shareButton = document.createElement('input');
shareButton.id = 'share_button';
shareButton.value = 'Share button';
shareButton.type = 'button';
div.appendChild(shareButton);

let heading = document.createElement('h2');
heading.innerText = 'Instructions:';
instruction.appendChild(heading);
let list = document.createElement('ol');
instruction.appendChild(list);
let item = document.createElement('li');
list.appendChild(item);
item.innerText = 'Click the Share button.';

return [instruction, list];
}

// Sets up the page for running manual tests. Automatically creates the
// instructions (based on the parameters) and the share button.
function setupManualShareTest(expected_share_data) {
const {title, text, url} = expected_share_data;
let [instruction, list] = setupManualShareTestCommon();
let item = document.createElement('li');
list.appendChild(item);
item.innerText = 'Choose a valid share target.';

heading = document.createElement('h2');
heading.innerText = 'Pass the test iff the target app received:';
instruction.appendChild(heading);

list = document.createElement('ul');
instruction.appendChild(list);

item = document.createElement('li');
list.appendChild(item);
item.innerText = `title = "${title}"`;
item = document.createElement('li');
list.appendChild(item);
item.innerText = `text = "${text}"`;
item = document.createElement('li');
list.appendChild(item);
item.innerText = `url = "${url}"`;
}

function setupManualShareTestRequiringCancellation() {
const [instruction, list] = setupManualShareTestCommon();
const item = document.createElement('li');
list.appendChild(item);
item.innerText = 'Cancel the share dialog.';
}

// Returns a promise. When the user clicks the button, calls
// |click_handler| and resolves the promise with the result.
function callWhenButtonClicked(click_handler) {
return new Promise((resolve, reject) => {
document.querySelector('#share_button').onclick = () => {
try {
resolve(click_handler());
} catch (e) {
reject(e);
}
};
});
}

function getAbsoluteUrl(url) {
return new URL(url, document.baseURI).toString();
}
24 changes: 24 additions & 0 deletions web-share/share-cancel-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Share cancelled by user</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

setupManualShareTestRequiringCancellation();

promise_test(t => {
return callWhenButtonClicked(() => promise_rejects(
t, 'AbortError',
navigator.share({title: 'the title', text: 'the message',
url: 'data:the url'})));
}, 'share with user cancellation');
</script>
</body>
</html>
18 changes: 18 additions & 0 deletions web-share/share-empty-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Share with empty ShareData</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

setupManualShareTest({title: '', text: '', url: ''});
callWhenButtonClicked(() => navigator.share({}));
</script>
</body>
</html>
21 changes: 21 additions & 0 deletions web-share/share-extra-argument-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Surplus arguments ignored</title>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want to test for this. This might cause issues if we need to extend the API later and it's outside the scope of the API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to test this.

Surely if we extend the API later, we'll also extend the test? More importantly, this tests that a valid implementation allows us to extend the API later without breaking backwards-compatibility.

(The tests don't need to be forwards-compatible, but we do need to test that the implementation is forwards-compatible.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get a second opinion from another peer? @foolip maybe? I don't feel too strongly, just don't want us to get into trouble later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring extra attributes is the standard behavior of WebIDL, so that seems OK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should expect standards and tests to co-evolve, and that changing both is low friction, so I don't worry about the tests constraining changes. However, if an implementation did have an optional second string argument, this test wouldn't necessarily fail. Passing in { toString() { throw Error(); } } would catch it, but is there an object that will throw whatever the argument type is, including a dictionary? I suspect not.

If that can be solved generically, I think that something in idlharness.js might make sense. So, the value of this test probably isn't super high, and I would lean towards removing.

(Looking at this I noticed w3c/web-share#47)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that can be solved generically, I think that something in idlharness.js might make sense.

This is what I would have suggested except afaik we don't have a good story for using idlharness on manual tests for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@foolip: Point taken, but I think your argument is that it won't necessarily catch implementations that are actually making use of a second argument (that's correct). I think there's still utility here, though, to catch implementations that throw an exception if they receive too many arguments.

Maybe the chance of that is low (since it's atypical for JavaScript code to care if there is excess arguments) but it's still a part of the IDL interface that's worth testing since it isn't (and can't be, for manual tests) tested by idlharness.

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

setupManualShareTest(
{title: 'the title', text: 'the message', url: 'data:the url'});
callWhenButtonClicked(() => navigator.share(
{title: 'the title', text: 'the message', url: 'data:the url'},
'more than required'));
</script>
</body>
</html>
21 changes: 21 additions & 0 deletions web-share/share-extra-field-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Surplus fields ignored</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

setupManualShareTest(
{title: 'the title', text: 'the message', url: 'data:the url'});
callWhenButtonClicked(() => navigator.share(
{title: 'the title', text: 'the message', url: 'data:the url',
unused: 'unexpected field'}));
</script>
</body>
</html>
23 changes: 23 additions & 0 deletions web-share/share-non-string-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Share non-string types (test implicit conversion)</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

// Expect that each of the non-string values is converted into a string.
setupManualShareTest(
{title: 'true', text: 'the object', url: getAbsoluteUrl('384957')});

const objectWithToString = {toString() { return 'the object'; }};
callWhenButtonClicked(() => navigator.share(
{title: true, text: objectWithToString, url: 384957}));
</script>
</body>
</html>
22 changes: 22 additions & 0 deletions web-share/share-null-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Share null and undefined values</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

// Expect null to be converted into the string 'null'. On the other
// hand, undefined should be equivalent to omitting the attribute.
setupManualShareTest(
{title: 'null', text: '', url: getAbsoluteUrl('null')});
callWhenButtonClicked(() => navigator.share(
{title: null, text: undefined, url: null}));
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions web-share/share-securecontext.http.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Share from non-secure context</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
test(() => {
assert_false('share' in navigator, 'navigator has attribute \'share\'.');
}, 'navigator.share must be undefined in non-secure context');
</script>
</body>
</html>
28 changes: 28 additions & 0 deletions web-share/share-simple-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Simple share</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

const url = 'https://www.example.com/some/path?some_query#some_fragment';
setupManualShareTest({title: 'the title', text: 'the message', url});
callWhenButtonClicked(() => navigator.share(
{title: 'the title', text: 'the message', url})).then(
result => {
// Check that promise resolved with undefined. This is guarded
// by an if statement because we do not want it to register as a
// test if it passes (since this is a manual test).
if (result !== undefined) {
assert_equals(result, undefined);
}
});
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions web-share/share-unicode-strings-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Share with non-ASCII Unicode strings</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

// Title is a string with BMP and non-BMP characters.
// Text contains invalid surrogates which should be converted into U+FFFD.
// URL contains non-ASCII characters in host and path.
const title = 'fáncy 写作 😱';
const url = 'https://测试.example.com/📄';
// Host is IDNA-encoded. Path is percent-encoded.
const url_ascii = 'https://xn--0zwm56d.example.com/%F0%9F%93%84';
setupManualShareTest({title, text: '\ufffdx', url: url_ascii});
callWhenButtonClicked(() => navigator.share({title, text: '\ud9a3x', url}));
</script>
</body>
</html>
27 changes: 27 additions & 0 deletions web-share/share-unicode-strings-nonutf8-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1252">
<title>WebShare Test: Share with non-ASCII Unicode strings in a Latin-1-encoded page</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

assert_equals(document.characterSet, 'windows-1252');

// Exact same test as in share-unicode-strings-manual.html, with same
// expectations. This tests that the page's encoding (ISO-8859-1) is
// ignored and Unicode characters are always percent-encoded in UTF-8.
const title = 'f\xe1ncy \u5199\u4f5c \ud83d\ude31';
const url = 'https://\u6d4b\u8bd5.example.com/\ud83d\udcc4';
// Host is IDNA-encoded. Path is percent-encoded with UTF-8.
const url_ascii = 'https://xn--0zwm56d.example.com/%F0%9F%93%84';
setupManualShareTest({title, text: '\ufffdx', url: url_ascii});
callWhenButtonClicked(() => navigator.share({title, text: '\ud9a3x', url}));
</script>
</body>
</html>
19 changes: 19 additions & 0 deletions web-share/share-url-data-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebShare Test: Share with data URL</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/manual-helper.js"></script>
</head>
<body>
<script>
setup({explicit_timeout: true});

const url = 'data:foo';
setupManualShareTest({title: '', text: '', url: getAbsoluteUrl(url)});
callWhenButtonClicked(() => navigator.share({url}));
</script>
</body>
</html>
Loading