Commit 4b78b510 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[ContentIndex] Move some of the web tests to WPT.

Moves the generic web tests to the wpt folder. Minor changes were made
to them (using wpt resources, not checking for specific error messages).

The tests that were checking for specific chromium error messages were
duplicated and kept. As were tests using testRunner functions.

Change-Id: I79d1d3c5cf2092753ef7b0bfbbfa7b227395f8ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2142214Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757549}
parent 3fdccc65
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources.js
'use strict';
contentIndexTest(async (t, index) => {
// Exposure of the interface and method.
assert_own_property(window, 'ContentIndex');
assert_own_property(ContentIndex.prototype, 'add');
assert_idl_attribute(index, 'add');
assert_idl_attribute(index, 'delete');
assert_idl_attribute(index, 'getAll');
}, 'The Content Index API is exposed');
contentIndexTest(async (t, index) => {
await expectTypeError(
index.add(createDescription({category: 'fake-category'})));
await expectTypeError(
index.add(createDescription({iconUrl: 'file://some-local-file.png'})));
await expectTypeError(index.add(createDescription({iconUrl: '/non-existent-icon.png'})));
await expectTypeError(index.add(createDescription({iconUrl: '/images/broken.png'})));
await expectTypeError(index.add(createDescription({launchUrl: 'https://other-domain.com/'})));
await expectTypeError(index.add(createDescription({launchUrl: '/different-scope'})));
await index.add(createDescription({}));
}, 'index.add parameters are validated.');
contentIndexTest(async (t, index) => {
const description = createDescription({});
// Initially there are no descriptions.
assert_array_equals(await index.getAll(), []);
await index.add(description);
const descriptions = await index.getAll();
assert_equals(descriptions.length, 1);
assert_object_equals(descriptions[0], description);
}, 'index.getAll returns the same objects provided.');
contentIndexTest(async (t, index) => {
const description1 = createDescription({title: 'title1'});
const description2 = createDescription({title: 'title2'});
await index.add(description1);
await index.add(description2);
// There should be one description.
const descriptions = await index.getAll();
assert_equals(descriptions.length, 1);
assert_object_equals(descriptions[0], description2);
}, 'index.add with same ID overwrites existing entry.');
contentIndexTest(async (t, index) => {
const description1 = createDescription({id: 'id1'});
const description2 = createDescription({id: 'id2'});
await index.add(description1);
await index.add(description2);
// There should be two descriptions.
assert_equals((await index.getAll()).length, 2);
await index.delete('id1');
// There should be one description.
const descriptions = await index.getAll();
assert_equals(descriptions.length, 1);
assert_object_equals(descriptions[0], description2);
}, 'index.delete removes entry.');
contentIndexTest(async (t, index) => {
const descriptions = await index.getAll();
assert_equals(descriptions.length, 0);
await index.delete('id');
}, 'index.delete works on invalid ID.');
'use strict';
const swUrl = 'resources/sw.js';
const scope = 'resources/';
async function expectTypeError(promise) {
try {
await promise;
assert_unreached('Promise should have rejected');
} catch (e) {
assert_equals(e.name, 'TypeError');
}
}
function createDescription({id = 'id', title = 'title', description = 'description',
category = 'homepage', iconUrl = '/images/green-256x256.png',
launchUrl = scope, includeIcons = true}) {
return {id, title, description, category, icons: includeIcons ? [{src: iconUrl}] : [], launchUrl};
}
// Creates a Promise test for |func| given the |description|. The |func| will be
// executed with the `index` object of an activated Service Worker Registration.
function contentIndexTest(func, description) {
promise_test(async t => {
const registration = await service_worker_unregister_and_register(t, swUrl, scope);
await wait_for_state(t, registration.installing, 'activated');
return func(t, registration.index);
}, description);
}
async function waitForMessageFromServiceWorker() {
return await new Promise(resolve => {
const listener = event => {
navigator.serviceWorker.removeEventListener('message', listener);
resolve(event.data);
};
navigator.serviceWorker.addEventListener('message', listener);
});
}
...@@ -8,17 +8,6 @@ ...@@ -8,17 +8,6 @@
<script> <script>
'use strict'; 'use strict';
contentIndexTest(async (t, index) => {
// Exposure of the interface and method.
assert_own_property(window, 'ContentIndex');
assert_own_property(ContentIndex.prototype, 'add');
assert_idl_attribute(index, 'add');
assert_idl_attribute(index, 'delete');
assert_idl_attribute(index, 'getAll');
}, 'The Content Index API is exposed');
contentIndexTest(async (t, index) => { contentIndexTest(async (t, index) => {
await expectTypeErrorWithMessage( await expectTypeErrorWithMessage(
index.add(createDescription({id: ''})), index.add(createDescription({id: ''})),
...@@ -56,64 +45,6 @@ contentIndexTest(async (t, index) => { ...@@ -56,64 +45,6 @@ contentIndexTest(async (t, index) => {
}, 'index.add parameters are validated.'); }, 'index.add parameters are validated.');
contentIndexTest(async (t, index) => {
const description = createDescription({});
// Initially there are no descriptions.
assert_array_equals(await index.getAll(), []);
await index.add(description);
const descriptions = await index.getAll();
assert_equals(descriptions.length, 1);
assert_object_equals(descriptions[0], description);
}, 'index.getAll returns the same objects provided.');
contentIndexTest(async (t, index) => {
const description1 = createDescription({title: 'title1'});
const description2 = createDescription({title: 'title2'});
await index.add(description1);
await index.add(description2);
// There should be one description.
const descriptions = await index.getAll();
assert_equals(descriptions.length, 1);
assert_object_equals(descriptions[0], description2);
}, 'index.add with same ID overwrites existing entry.');
contentIndexTest(async (t, index) => {
const description1 = createDescription({id: 'id1'});
const description2 = createDescription({id: 'id2'});
await index.add(description1);
await index.add(description2);
// There should be two descriptions.
assert_equals((await index.getAll()).length, 2);
await index.delete('id1');
// There should be one description.
const descriptions = await index.getAll();
assert_equals(descriptions.length, 1);
assert_object_equals(descriptions[0], description2);
}, 'index.delete removes entry.');
contentIndexTest(async (t, index) => {
const descriptions = await index.getAll();
assert_equals(descriptions.length, 0);
await index.delete('id');
}, 'index.delete works on invalid ID.');
contentIndexTest(async (t, index) => { contentIndexTest(async (t, index) => {
// Register an entry. // Register an entry.
await index.add(createDescription({id: 'my-id'})); await index.add(createDescription({id: 'my-id'}));
......
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