From 3f12f3a87cc7fa1e02fc90f0631acef1be2cff7a Mon Sep 17 00:00:00 2001 From: Evan Welsh Date: Wed, 5 Jul 2023 18:08:09 -0700 Subject: [PATCH] scripting: Convert to ES module and migrate tests Part-of: --- .eslintrc.yml | 1 + js/ui/main.js | 3 +- js/ui/scripting.js | 47 ++++++++++++++------------- tests/shell/basic.js | 2 +- tests/shell/closeWithActiveWindows.js | 2 +- tests/shell/core.js | 2 +- tests/shell/headlessStart.js | 2 +- tests/shell/hwtest.js | 2 +- 8 files changed, 32 insertions(+), 29 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 16c8ab094..a9bf8e04c 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -5,6 +5,7 @@ overrides: - files: - js/ui/init.js - js/ui/environment.js + - js/ui/scripting.js - js/dbusServices/** - js/portalHelper/** - subprojects/extensions-app/** diff --git a/js/ui/main.js b/js/ui/main.js index 34a3b0e78..a7d86b570 100644 --- a/js/ui/main.js +++ b/js/ui/main.js @@ -44,7 +44,6 @@ const NotificationDaemon = imports.ui.notificationDaemon; const WindowAttentionHandler = imports.ui.windowAttentionHandler; const Screenshot = imports.ui.screenshot; const ScreenShield = imports.ui.screenShield; -const Scripting = imports.ui.scripting; const SessionMode = imports.ui.sessionMode; const ShellDBus = imports.ui.shellDBus; const ShellMountOperation = imports.ui.shellMountOperation; @@ -303,9 +302,11 @@ async function _initializeUI() { }); } + let Scripting; let perfModule; const {automationScript} = global; if (automationScript) { + Scripting = await import('./scripting.js'); perfModule = await import(automationScript.get_uri()); if (perfModule.init) perfModule.init(); diff --git a/js/ui/scripting.js b/js/ui/scripting.js index 353734899..fb31bab27 100644 --- a/js/ui/scripting.js +++ b/js/ui/scripting.js @@ -1,12 +1,9 @@ // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- -/* exported sleep, waitLeisure, createTestWindow, waitTestWindows, - destroyTestWindows, defineScriptEvent, scriptEvent, - collectStatistics, runPerfScript, disableHelperAutoExit */ -const Gio = imports.gi.Gio; -const GLib = imports.gi.GLib; -const Meta = imports.gi.Meta; -const Shell = imports.gi.Shell; +import Gio from 'gi://Gio'; +import GLib from 'gi://GLib'; +import Meta from 'gi://Meta'; +import Shell from 'gi://Shell'; const Config = imports.misc.config; const Main = imports.ui.main; @@ -41,7 +38,7 @@ const { loadInterfaceXML } = imports.misc.fileUtils; * current script for the specified amount of time. Use as * 'yield Scripting.sleep(500);' */ -function sleep(milliseconds) { +export function sleep(milliseconds) { return new Promise(resolve => { let id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, milliseconds, () => { resolve(); @@ -59,18 +56,19 @@ function sleep(milliseconds) { * current script until the shell is completely idle. Use as * 'yield Scripting.waitLeisure();' */ -function waitLeisure() { +export function waitLeisure() { return new Promise(resolve => { global.run_at_leisure(resolve); }); } const PerfHelperIface = loadInterfaceXML('org.gnome.Shell.PerfHelper'); -var PerfHelperProxy = Gio.DBusProxy.makeProxyWrapper(PerfHelperIface); +export const PerfHelperProxy = Gio.DBusProxy.makeProxyWrapper(PerfHelperIface); let _perfHelper = null; -/** private */ -async function _getPerfHelper() { + +/** @private */ +export async function _getPerfHelper() { if (_perfHelper == null) { _perfHelper = await PerfHelperProxy.newAsync( Gio.DBus.session, 'org.gnome.Shell.PerfHelper', '/org/gnome/Shell/PerfHelper'); @@ -80,7 +78,8 @@ async function _getPerfHelper() { return _perfHelper; } -function _spawnPerfHelper() { +/** @private */ +export function _spawnPerfHelper() { let path = GLib.getenv('GNOME_SHELL_BUILDDIR') || Config.LIBEXECDIR; let command = `${path}/gnome-shell-perf-helper`; Util.trySpawnCommandLine(command); @@ -102,7 +101,7 @@ function _spawnPerfHelper() { * because of the normal X asynchronous mapping process, to actually wait * until the window has been mapped and exposed, use waitTestWindows(). */ -async function createTestWindow(params) { +export async function createTestWindow(params) { params = Params.parse(params, { width: 640, height: 480, @@ -121,12 +120,13 @@ async function createTestWindow(params) { /** * waitTestWindows: + * * @returns {Promise} * * Used within an automation script to pause until all windows previously * created with createTestWindow have been mapped and exposed. */ -async function waitTestWindows() { +export async function waitTestWindows() { let perfHelper = await _getPerfHelper(); return perfHelper.WaitWindowsAsync().catch(logError); } @@ -141,7 +141,7 @@ async function waitTestWindows() { * this doesn't guarantee that Mutter has actually finished the destroy * process because of normal X asynchronicity. */ -async function destroyTestWindows() { +export async function destroyTestWindows() { let perfHelper = await _getPerfHelper(); return perfHelper.DestroyWindowsAsync().catch(logError); } @@ -152,7 +152,7 @@ async function destroyTestWindows() { * Don't exixt the perf helper after running the script. Instead it will remain * running until something else makes it exit, e.g. the Wayland socket closing. */ -async function disableHelperAutoExit() { +export async function disableHelperAutoExit() { let perfHelper = await _getPerfHelper(); perfHelper._autoExit = false; } @@ -166,7 +166,7 @@ async function disableHelperAutoExit() { * within the 'script' namespace that is reserved for events defined locally * within a performance automation script */ -function defineScriptEvent(name, description) { +export function defineScriptEvent(name, description) { Shell.PerfLog.get_default().define_event(`script.${name}`, description, ""); @@ -174,12 +174,13 @@ function defineScriptEvent(name, description) { /** * scriptEvent - * @param {string} name: Name registered with defineScriptEvent() + * + * @param {string} name Name registered with defineScriptEvent() * * Convenience function to record a script-local performance event * previously defined with defineScriptEvent */ -function scriptEvent(name) { +export function scriptEvent(name) { Shell.PerfLog.get_default().event(`script.${name}`); } @@ -188,7 +189,7 @@ function scriptEvent(name) { * * Convenience function to trigger statistics collection */ -function collectStatistics() { +export function collectStatistics() { Shell.PerfLog.get_default().collect_statistics(); } @@ -354,8 +355,8 @@ async function _runPerfScript(scriptModule, outputFile) { * * After running the script and collecting statistics from the * event log, GNOME Shell will exit. - **/ -function runPerfScript(scriptModule, outputFile) { + */ +export function runPerfScript(scriptModule, outputFile) { Shell.PerfLog.get_default().set_enabled(true); _spawnPerfHelper(); diff --git a/tests/shell/basic.js b/tests/shell/basic.js index 04c3f8711..30f183aa1 100644 --- a/tests/shell/basic.js +++ b/tests/shell/basic.js @@ -5,7 +5,7 @@ import St from 'gi://St'; const Main = imports.ui.main; const MessageTray = imports.ui.messageTray; -const Scripting = imports.ui.scripting; +import * as Scripting from 'resource://org/gnome/shell/ui/scripting.js'; // This script tests the most important (basic) functionality of the shell. diff --git a/tests/shell/closeWithActiveWindows.js b/tests/shell/closeWithActiveWindows.js index a2bec7954..cee470095 100644 --- a/tests/shell/closeWithActiveWindows.js +++ b/tests/shell/closeWithActiveWindows.js @@ -4,7 +4,7 @@ import Clutter from 'gi://Clutter'; const Main = imports.ui.main; -const Scripting = imports.ui.scripting; +import * as Scripting from 'resource://org/gnome/shell/ui/scripting.js'; /** Run test. */ export async function run() { diff --git a/tests/shell/core.js b/tests/shell/core.js index 12d9cb060..593382ac4 100644 --- a/tests/shell/core.js +++ b/tests/shell/core.js @@ -4,7 +4,7 @@ import System from 'system'; const Main = imports.ui.main; -const Scripting = imports.ui.scripting; +import * as Scripting from 'resource://org/gnome/shell/ui/scripting.js'; // This performance script measure the most important (core) performance // metrics for the shell. By looking at the output metrics of this script diff --git a/tests/shell/headlessStart.js b/tests/shell/headlessStart.js index 120d259d0..cf9c9436c 100644 --- a/tests/shell/headlessStart.js +++ b/tests/shell/headlessStart.js @@ -6,7 +6,7 @@ import MetaTest from 'gi://MetaTest'; import Shell from 'gi://Shell'; const Main = imports.ui.main; -const Scripting = imports.ui.scripting; +import * as Scripting from 'resource://org/gnome/shell/ui/scripting.js'; // This script tests that the shell handles connecting monitors after startup // is properly handled. diff --git a/tests/shell/hwtest.js b/tests/shell/hwtest.js index 6e7267614..e9e0ab074 100644 --- a/tests/shell/hwtest.js +++ b/tests/shell/hwtest.js @@ -3,7 +3,7 @@ import Clutter from 'gi://Clutter'; import Gio from 'gi://Gio'; import Shell from 'gi://Shell'; const Main = imports.ui.main; -const Scripting = imports.ui.scripting; +import * as Scripting from 'resource://org/gnome/shell/ui/scripting.js'; export var METRICS = { timeToDesktop: {