From bec0621315c75c50b510e242d52cad96cce25919 Mon Sep 17 00:00:00 2001 From: Daisuke Akatsuka Date: Tue, 28 Apr 2020 17:26:56 +0000 Subject: [PATCH] Bug 1625942: Add a browser test for document events ResourceWatcher. r=ochameau Depends on D69329 Differential Revision: https://phabricator.services.mozilla.com/D71007 --- devtools/shared/resources/tests/browser.ini | 1 + .../browser_resources_document_events.js | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 devtools/shared/resources/tests/browser_resources_document_events.js diff --git a/devtools/shared/resources/tests/browser.ini b/devtools/shared/resources/tests/browser.ini index ca0e5390d1c0..ea57132d02c2 100644 --- a/devtools/shared/resources/tests/browser.ini +++ b/devtools/shared/resources/tests/browser.ini @@ -12,6 +12,7 @@ support-files = test_worker.js [browser_resources_console_messages.js] +[browser_resources_document_events.js] [browser_resources_error_messages.js] [browser_resources_platform_messages.js] [browser_target_list_frames.js] diff --git a/devtools/shared/resources/tests/browser_resources_document_events.js b/devtools/shared/resources/tests/browser_resources_document_events.js new file mode 100644 index 000000000000..b23e764f333f --- /dev/null +++ b/devtools/shared/resources/tests/browser_resources_document_events.js @@ -0,0 +1,103 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Test the ResourceWatcher API around DOCUMENT_EVENTS + +const { TargetList } = require("devtools/shared/resources/target-list"); +const { + ResourceWatcher, +} = require("devtools/shared/resources/resource-watcher"); + +add_task(async function() { + // Open a test tab + const tab = await addTab("data:text/html,Document Events"); + + // Create a TargetList for the test tab + const client = await createLocalClient(); + const target = await client.mainRoot.getTab({ tab }); + const targetList = new TargetList(client.mainRoot, target); + await targetList.startListening(); + + // Activate ResourceWatcher + const listener = new ResourceListener(); + const resourceWatcher = new ResourceWatcher(targetList); + + info( + "Check whether the document events are fired correctly even when the document was already loaded" + ); + const onLoadingAtInit = listener.once("dom-loading"); + const onInteractiveAtInit = listener.once("dom-interactive"); + const onCompleteAtInit = listener.once("dom-complete"); + await resourceWatcher.watch( + [ResourceWatcher.TYPES.DOCUMENT_EVENTS], + parameters => listener.dispatch(parameters) + ); + await assertEvents(onLoadingAtInit, onInteractiveAtInit, onCompleteAtInit); + ok( + true, + "Document events are fired even when the document was already loaded" + ); + + info("Check whether the document events are fired correctly when reloading"); + const onLoadingAtReloaded = listener.once("dom-loading"); + const onInteractiveAtReloaded = listener.once("dom-interactive"); + const onCompleteAtReloaded = listener.once("dom-complete"); + gBrowser.reloadTab(tab); + await assertEvents( + onLoadingAtReloaded, + onInteractiveAtReloaded, + onCompleteAtReloaded + ); + ok(true, "Document events are fired after reloading"); + + await targetList.stopListening(); + await client.close(); +}); + +async function assertEvents(onLoading, onInteractive, onComplete) { + const loadingEvent = await onLoading; + const interactiveEvent = await onInteractive; + const completeEvent = await onComplete; + is( + typeof loadingEvent.time, + "number", + "Type of time attribute for loading event is correct" + ); + is( + typeof interactiveEvent.time, + "number", + "Type of time attribute for interactive event is correct" + ); + is( + typeof completeEvent.time, + "number", + "Type of time attribute for complete event is correct" + ); + + ok( + loadingEvent.time < interactiveEvent.time, + "Timestamp for interactive event is greater than loading event" + ); + ok( + interactiveEvent.time < completeEvent.time, + "Timestamp for complete event is greater than interactive event" + ); +} + +class ResourceListener { + _listeners = new Map(); + + dispatch({ resourceType, targetFront, resource }) { + const resolve = this._listeners.get(resource.name); + if (resolve) { + resolve(resource); + this._listeners.delete(resource.name); + } + } + + once(resourceName) { + return new Promise(r => this._listeners.set(resourceName, r)); + } +}