diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/content-index | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/content-index')
5 files changed, 191 insertions, 0 deletions
diff --git a/testing/web-platform/tests/content-index/META.yml b/testing/web-platform/tests/content-index/META.yml new file mode 100644 index 0000000000..9c88d6e2af --- /dev/null +++ b/testing/web-platform/tests/content-index/META.yml @@ -0,0 +1 @@ +spec: https://wicg.github.io/content-index/spec/ diff --git a/testing/web-platform/tests/content-index/content-index.https.window.js b/testing/web-platform/tests/content-index/content-index.https.window.js new file mode 100644 index 0000000000..690b23176c --- /dev/null +++ b/testing/web-platform/tests/content-index/content-index.https.window.js @@ -0,0 +1,100 @@ +// META: script=/resources/test-only-api.js +// 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'}))); + + const isFetchingIcons = await fetchesIcons(); + if (isFetchingIcons) { + // If the browser will try to fetch these icons we expect it to fail. + await expectTypeError( + index.add(createDescription({iconUrl: '/non-existent-icon.png'}))); + await expectTypeError( + index.add(createDescription({iconUrl: '/images/broken.png'}))); + } else { + // If the browser will not try to fetch these icons this should succeed. + await index.add(createDescription({iconUrl: '/non-existent-icon.png'})); + await index.add(createDescription({iconUrl: '/images/broken.png'})); + } + + await expectTypeError(index.add(createDescription({url: 'https://other-domain.com/'}))); + await expectTypeError(index.add(createDescription({url: '/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.'); diff --git a/testing/web-platform/tests/content-index/idlharness.https.any.js b/testing/web-platform/tests/content-index/idlharness.https.any.js new file mode 100644 index 0000000000..599aba4666 --- /dev/null +++ b/testing/web-platform/tests/content-index/idlharness.https.any.js @@ -0,0 +1,33 @@ +// META: global=window,worker +// META: script=/resources/WebIDLParser.js +// META: script=/resources/idlharness.js +// META: script=/service-workers/service-worker/resources/test-helpers.sub.js + +'use strict'; + +// https://wicg.github.io/content-index/spec/ + +idl_test( + ['content-index'], + ['service-workers', 'html', 'dom'], + async (idl_array, t) => { + // TODO: Handle other worker types. + if (self.GLOBAL.isWindow()) { + idl_array.add_objects({ + ServiceWorkerRegistration: ['registration'], + ContentIndex: ['registration.index'], + }); + self.registration = await service_worker_unregister_and_register( + t, 'resources/sw.js', 'resources/does/not/exist'); + t.add_cleanup(() => registration.unregister()); + } + else if (self.ServiceWorkerGlobalScope) { + idl_array.add_objects({ + ServiceWorkerGlobalScope: ['self'], + ServiceWorkerRegistration: ['registration'], + ContentIndex: ['registration.index'], + ContentIndexEvent: ['new ContentIndexEvent("type", {id: "foo"})'], + }); + } + } +); diff --git a/testing/web-platform/tests/content-index/resources.js b/testing/web-platform/tests/content-index/resources.js new file mode 100644 index 0000000000..cf96dd5390 --- /dev/null +++ b/testing/web-platform/tests/content-index/resources.js @@ -0,0 +1,57 @@ +'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', + url = scope, includeIcons = true}) { + return {id, title, description, category, icons: includeIcons ? [{src: iconUrl}] : [], url}; +} + +// 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); + }); +} + +// Returns a promise if the chromium based browser fetches icons for +// content-index. +async function fetchesIconsChromium() { + const {fetchesIcons} = + await import('/resources/chromium/content-index-helpers.js'); + return fetchesIcons(); +} + +// Returns a promise if the browser fetches icons for content-index and should +// therefore validate them. +async function fetchesIcons() { + if (isChromiumBased) { + return fetchesIconsChromium(); + } + return false; +} diff --git a/testing/web-platform/tests/content-index/resources/sw.js b/testing/web-platform/tests/content-index/resources/sw.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/testing/web-platform/tests/content-index/resources/sw.js |