Commit b5bb5a2b authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Switch WebShare LayoutTests to the new Mojo JS bindings

This change rewrites the WebShare API layout tests to use the new Mojo
JS bindings.

Bug: 699569
Change-Id: Id95a016765c5b101ee347f760e7cec8b74ec4b7e
Reviewed-on: https://chromium-review.googlesource.com/587412Reviewed-by: default avatarYuzhu Shen <yzshen@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491079}
parent 0fbef2a0
'use strict';
let mockShareService = loadMojoModules(
'mockShareService',
['mojo/public/js/bindings',
'third_party/WebKit/public/platform/modules/webshare/webshare.mojom',
]).then(mojo => {
let [bindings, webshare] = mojo.modules;
class MockShareService {
constructor(interfaceProvider) {
this.bindingSet_ = new mojo.BindingSet(blink.mojom.ShareService);
this.interceptor_ = new MojoInterfaceInterceptor(
blink.mojom.ShareService.name);
this.interceptor_.oninterfacerequest =
e => this.bindingSet_.addBinding(this, e.handle);
this.interceptor_.start();
}
class MockShareService {
constructor(interfaceProvider) {
this.webshare_ = webshare;
this.bindingSet_ = new bindings.BindingSet(webshare.ShareService);
// Returns a Promise that gets rejected if the test should fail.
init_() {
// sequence of [expectedTitle, expectedText, result].
this.shareResultQueue_ = [];
interfaceProvider.addInterfaceOverrideForTesting(
webshare.ShareService.name,
handle => this.bindingSet_.addBinding(this, handle));
}
return new Promise((resolve, reject) => {this.reject_ = reject});
}
// Returns a Promise that gets rejected if the test should fail.
init_() {
// sequence of [expectedTitle, expectedText, result].
this.shareResultQueue_ = [];
share(title, text, url) {
let callback = null;
let result = new Promise(resolve => {callback = resolve;});
return new Promise((resolve, reject) => {this.reject_ = reject});
if (!this.shareResultQueue_.length) {
this.reject_('Unexpected call to mojo share method');
return result;
}
share(title, text, url) {
let callback = null;
let result = new Promise(resolve => {callback = resolve;});
if (!this.shareResultQueue_.length) {
this.reject_('Unexpected call to mojo share method');
return result;
}
let [expectedTitle, expectedText, expectedUrl, error] =
this.shareResultQueue_.shift();
try {
assert_equals(title, expectedTitle);
assert_equals(text, expectedText);
assert_equals(url.url, expectedUrl);
} catch (e) {
this.reject_(e);
return result;
}
callback({error: error});
let [expectedTitle, expectedText, expectedUrl, error] =
this.shareResultQueue_.shift();
try {
assert_equals(title, expectedTitle);
assert_equals(text, expectedText);
assert_equals(url.url, expectedUrl);
} catch (e) {
this.reject_(e);
return result;
}
callback({error: error});
pushShareResult(expectedTitle, expectedText, expectedUrl, result) {
this.shareResultQueue_.push(
[expectedTitle, expectedText, expectedUrl, result]);
}
return result;
}
return new MockShareService(mojo.frameInterfaces);
});
pushShareResult(expectedTitle, expectedText, expectedUrl, result) {
this.shareResultQueue_.push(
[expectedTitle, expectedText, expectedUrl, result]);
}
}
let mockShareService = new MockShareService();
function share_test(func, name, properties) {
promise_test(t => mockShareService.then(mock => {
let mockPromise = mock.init_();
return Promise.race([func(t, mock.webshare_, mock), mockPromise]);
}), name, properties);
promise_test(() => {
let mockPromise = mockShareService.init_();
return Promise.race([func(mockShareService), mockPromise]);
}, name, properties);
}
// Copied from resources/bluetooth/bluetooth-helpers.js.
......
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/third_party/WebKit/public/platform/modules/webshare/webshare.mojom.js"></script>
<script src="resources/mock-share-service.js"></script>
<script>
share_test((t, webshare, mock) => {
mock.pushShareResult('the title', 'the message', 'data:the url', webshare.ShareError.OK);
return callWithKeyDown(() => navigator.share({title: 'the title', text: 'the message', url: 'data:the url', unused: 'unexpected field'}));
share_test(mock => {
mock.pushShareResult('the title', 'the message', 'data:the url',
blink.mojom.ShareError.OK);
return callWithKeyDown(() => {
navigator.share({
title: 'the title',
text: 'the message',
url: 'data:the url',
unused: 'unexpected field'});
});
}, 'extra ShareData field (extra field ignored)');
share_test((t, webshare, mock) => {
mock.pushShareResult('the title', 'the message', 'data:the url', webshare.ShareError.OK);
return callWithKeyDown(() => navigator.share({title: 'the title', text: 'the message', url: 'data:the url'}, 'more than required'));
share_test(mock => {
mock.pushShareResult('the title', 'the message', 'data:the url',
blink.mojom.ShareError.OK);
return callWithKeyDown(() => {
navigator.share({
title: 'the title',
text: 'the message',
url: 'data:the url'}, 'more than required');
});
}, '2 arguments (extra argument ignored)');
</script>
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/third_party/WebKit/public/platform/modules/webshare/webshare.mojom.js"></script>
<script src="resources/mock-share-service.js"></script>
<script>
share_test((t, webshare, mock) => {
async function assertRejectsWithError(promise, name) {
try {
await promise;
assert_unreached('expected promise to reject with ' + name);
} catch (error) {
assert_equals(error.name, name);
}
}
share_test(mock => {
mock.pushShareResult('the title', 'the message', 'data:the url',
webshare.ShareError.CANCELED);
return callWithKeyDown(() => promise_rejects(
t, 'AbortError',
navigator.share({title: 'the title', text: 'the message', url: 'data:the url'})));
blink.mojom.ShareError.CANCELED);
return callWithKeyDown(() => assertRejectsWithError(
navigator.share({
title: 'the title',
text: 'the message',
url: 'data:the url'
}),
'AbortError'));
}, 'share with user cancellation');
share_test((t, webshare, mock) => {
share_test(mock => {
mock.pushShareResult('the title', 'the message', 'data:the url',
webshare.ShareError.INTERNAL_ERROR);
return callWithKeyDown(() => promise_rejects(
t, 'AbortError',
navigator.share({title: 'the title', text: 'the message', url: 'data:the url'})));
blink.mojom.ShareError.INTERNAL_ERROR);
return callWithKeyDown(() => assertRejectsWithError(
navigator.share({
title: 'the title',
text: 'the message',
url: 'data:the url'
}),
'AbortError'));
}, 'share with invalid url template');
</script>
......@@ -2,20 +2,21 @@
<meta charset="windows-1252">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/third_party/WebKit/public/platform/modules/webshare/webshare.mojom.js"></script>
<script src="resources/mock-share-service.js"></script>
<script>
// Exact same test as in share-success.html, with same expectations. This tests
// that the page's encoding (Windows-1252) is ignored and Unicode characters are
// always percent-encoded in UTF-8.
share_test((t, webshare, mock) => {
share_test(mock => {
assert_equals(document.characterSet, 'windows-1252');
const title = 'f\xe1ncy \u5199\u4f5c \ud83d\ude31';
const url = 'https://\u6d4b\u8bd5.example.com/\ud83d\udcc4';
const url_ascii = 'https://xn--0zwm56d.example.com/%F0%9F%93%84';
mock.pushShareResult(title, '\ufffdx', url_ascii, webshare.ShareError.OK);
mock.pushShareResult(title, '\ufffdx', url_ascii, blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share(
{title: title, text: '\ud9a3x', url: url}));
}, 'successful share with Unicode characters');
......
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/third_party/WebKit/public/platform/modules/webshare/webshare.mojom.js"></script>
<script src="resources/mock-share-service.js"></script>
<script>
......@@ -9,55 +10,55 @@ function getAbsoluteUrl(url) {
return new URL(url, document.baseURI).toString();
}
share_test((t, webshare, mock) => {
share_test(mock => {
const url = 'https://www.example.com/some/path?some_query#some_fragment';
mock.pushShareResult('the title', 'the message', getAbsoluteUrl(url),
webshare.ShareError.OK);
blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share(
{title: 'the title', text: 'the message', url: url})).then(
result => assert_equals(result, undefined));
}, 'successful share');
share_test((t, webshare, mock) => {
share_test(mock => {
const url = '//www.example.com/some/path?some_query#some_fragment';
mock.pushShareResult('', '', getAbsoluteUrl(url), webshare.ShareError.OK);
mock.pushShareResult('', '', getAbsoluteUrl(url), blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share({url: url}));
}, 'successful share with URL without a scheme');
share_test((t, webshare, mock) => {
share_test(mock => {
const url = '/some/path?some_query#some_fragment';
mock.pushShareResult('', '', getAbsoluteUrl(url), webshare.ShareError.OK);
mock.pushShareResult('', '', getAbsoluteUrl(url), blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share({url: url}));
}, 'successful share with a path-only URL');
share_test((t, webshare, mock) => {
share_test(mock => {
const url = 'foo';
mock.pushShareResult('', '', getAbsoluteUrl(url), webshare.ShareError.OK);
mock.pushShareResult('', '', getAbsoluteUrl(url), blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share({url: url}));
}, 'successful share with a relative URL');
share_test((t, webshare, mock) => {
share_test(mock => {
const url = '';
mock.pushShareResult('', '', document.baseURI, webshare.ShareError.OK);
mock.pushShareResult('', '', document.baseURI, blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share({url: url}));
}, 'successful share with an empty URL');
share_test((t, webshare, mock) => {
share_test(mock => {
const url = 'data:foo';
mock.pushShareResult('', '', getAbsoluteUrl(url), webshare.ShareError.OK);
mock.pushShareResult('', '', getAbsoluteUrl(url), blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share({url: url}));
}, 'successful share with a data URL');
share_test((t, webshare, mock) => {
share_test(mock => {
const url = 'http://example.com/foo\\ab%63\r\n\t "<>`{}';
// Expect '\' to normalize to '/', "%63" to normalize to 'c', '\r\n\t'
// to be removed, and all the other illegal characters to be percent-escaped.
const url_encoded = 'http://example.com/foo/abc%20%22%3C%3E%60%7B%7D';
mock.pushShareResult('', '', url_encoded, webshare.ShareError.OK);
mock.pushShareResult('', '', url_encoded, blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share({url: url}));
}, 'successful share with percent-encoded URL characters');
share_test((t, webshare, mock) => {
share_test(mock => {
// 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.
......@@ -65,7 +66,7 @@ share_test((t, webshare, mock) => {
const url = 'https://\u6d4b\u8bd5.example.com/\ud83d\udcc4';
// Host is IDNA-encoded. Path is percent-encoded.
const url_ascii = 'https://xn--0zwm56d.example.com/%F0%9F%93%84';
mock.pushShareResult(title, '\ufffdx', url_ascii, webshare.ShareError.OK);
mock.pushShareResult(title, '\ufffdx', url_ascii, blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share(
{title: title, text: '\ud9a3x', url: url}));
}, 'successful share with Unicode characters');
......
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/third_party/WebKit/public/platform/modules/webshare/webshare.mojom.js"></script>
<script src="resources/mock-share-service.js"></script>
<script>
......@@ -9,19 +10,20 @@ function getAbsoluteUrl(url) {
return new URL(url, document.baseURI).toString();
}
share_test((t, webshare, mock) => {
mock.pushShareResult('true', 'the object', getAbsoluteUrl('384957'), webshare.ShareError.OK);
share_test(mock => {
mock.pushShareResult('true', 'the object', getAbsoluteUrl('384957'),
blink.mojom.ShareError.OK);
const objectWithToString = {toString() { return 'the object'; }};
return callWithKeyDown(() => navigator.share(
{title: true, text: objectWithToString, url: 384957}));
}, 'share of types other than string (expect implicitly converted to string)');
share_test((t, webshare, mock) => {
share_test(mock => {
// null fields should convert into the string 'null' (because the field is
// not nullable, it just converts to a string like any other type).
mock.pushShareResult('null', '', getAbsoluteUrl('null'),
webshare.ShareError.OK);
blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share(
{title: null, text: undefined, url: null}));
}, 'share of null/undefined dict values');
......
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/third_party/WebKit/public/platform/modules/webshare/webshare.mojom.js"></script>
<script src="resources/mock-share-service.js"></script>
<base href="https://www.example.com/some/path.html">
<script>
share_test((t, webshare, mock) => {
share_test(mock => {
const url = 'foo';
mock.pushShareResult('', '', 'https://www.example.com/some/foo', webshare.ShareError.OK);
mock.pushShareResult('', '', 'https://www.example.com/some/foo',
blink.mojom.ShareError.OK);
return callWithKeyDown(() => navigator.share({url: url}));
}, 'successful share with a URL relative to document base URL');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment