tests: Port InjectionManager test to jasmine

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3164>
This commit is contained in:
Florian Müllner 2024-01-20 08:00:39 +01:00 committed by Marge Bot
parent 9315d42dd6
commit d62146bc19
2 changed files with 149 additions and 92 deletions

View File

@ -21,6 +21,7 @@ unit_testenv.append('GI_TYPELIB_PATH', st_typelib_path, separator: ':')
unit_tests = [ unit_tests = [
'highlighter', 'highlighter',
'injectionManager',
] ]
foreach test : unit_tests foreach test : unit_tests
@ -39,7 +40,6 @@ foreach test : unit_tests
endforeach endforeach
legacy_tests = [ legacy_tests = [
'injectionManager',
'insertSorted', 'insertSorted',
'jsParse', 'jsParse',
'markup', 'markup',

View File

@ -1,16 +1,16 @@
const JsUnit = imports.jsUnit;
import GObject from 'gi://GObject'; import GObject from 'gi://GObject';
import 'resource:///org/gnome/shell/ui/environment.js'; import 'resource:///org/gnome/shell/ui/environment.js';
import {InjectionManager} from 'resource:///org/gnome/shell/extensions/extension.js'; import {InjectionManager} from 'resource:///org/gnome/shell/extensions/extension.js';
const FIXED_NUMBER = 42;
class Object1 { class Object1 {
count = 0; count = 0;
getNumber() { getNumber() {
return 42; return FIXED_NUMBER;
} }
getCount() { getCount() {
@ -29,89 +29,133 @@ class GObject1 extends GObject.Object {
static { static {
GObject.registerClass(this); GObject.registerClass(this);
} }
plonk() {
this.set_property('plonked', true);
}
} }
/** describe('InjectionManager', () => {
* @param {object} object to modify const INJECTIONS = {
*/ 'getNumber': originalMethod => {
function addInjections(object) {
// extend original method
injectionManager.overrideMethod(
object, 'getNumber', originalMethod => {
return function () { return function () {
// eslint-disable-next-line no-invalid-this // eslint-disable-next-line no-invalid-this
const num = originalMethod.call(this); const num = originalMethod.call(this);
return 2 * num; return 2 * num;
}; };
}); },
'getCount': () => {
// override original method
injectionManager.overrideMethod(
object, 'getCount', () => {
return function () { return function () {
return 42; return FIXED_NUMBER;
}; };
}); },
'getOtherNumber': () => {
// inject new method
injectionManager.overrideMethod(
object, 'getOtherNumber', () => {
return function () { return function () {
return 42; return FIXED_NUMBER;
}; };
}); },
};
let injectionManager;
function injectAll(obj) {
for (const func in INJECTIONS) {
injectionManager.overrideMethod(obj,
func, INJECTIONS[func]);
}
} }
beforeEach(() => {
injectionManager = new InjectionManager();
});
const injectionManager = new InjectionManager(); afterEach(() => {
let obj; injectionManager.clear();
});
// Prototype injections it('can extend prototype methods', () => {
addInjections(Object1.prototype); injectionManager.overrideMethod(Object1.prototype,
'getNumber', INJECTIONS['getNumber']);
obj = new Object1(); const obj = new Object1();
expect(obj.getNumber()).toEqual(2 * FIXED_NUMBER);
});
// new obj is modified it('can replace prototype methods', () => {
JsUnit.assertEquals(obj.getNumber(), 84); injectionManager.overrideMethod(Object1.prototype,
JsUnit.assertEquals(obj.getCount(), 42); 'getCount', INJECTIONS['getCount']);
JsUnit.assertEquals(obj.count, 0);
JsUnit.assertEquals(obj.getOtherNumber(), 42); const obj = new Object1();
expect(obj.getCount()).toEqual(42);
expect(obj.count).toEqual(0);
});
it('can inject methods into prototypes', () => {
injectionManager.overrideMethod(Object1.prototype,
'getOtherNumber', INJECTIONS['getOtherNumber']);
const obj = new Object1();
expect(obj.getOtherNumber()).toEqual(42);
});
it('can undo prototype injections', () => {
injectAll(Object1.prototype);
injectionManager.clear(); injectionManager.clear();
obj = new Object1(); const obj = new Object1();
expect(obj.getNumber()).toEqual(FIXED_NUMBER);
expect(obj.getCount()).toEqual(obj.count);
expect(obj.count).toBeGreaterThan(0);
expect(() => obj.getOtherNumber()).toThrow();
});
// new obj is unmodified it('can extend instance methods', () => {
JsUnit.assertEquals(obj.getNumber(), 42); const obj = new Object1();
JsUnit.assertEquals(obj.getCount(), obj.count); injectionManager.overrideMethod(obj,
JsUnit.assert(obj.count > 0); 'getNumber', INJECTIONS['getNumber']);
JsUnit.assertRaises(() => obj.getOtherNumber());
// instance injections expect(obj.getNumber()).toEqual(2 * FIXED_NUMBER);
addInjections(obj); });
// obj is now modified it('can replace instance methods', () => {
JsUnit.assertEquals(obj.getNumber(), 84); const obj = new Object1();
JsUnit.assertEquals(obj.getCount(), 42); injectionManager.overrideMethod(obj,
JsUnit.assertEquals(obj.count, 1); 'getCount', INJECTIONS['getCount']);
JsUnit.assertEquals(obj.getOtherNumber(), 42);
expect(obj.getCount()).toEqual(42);
expect(obj.count).toEqual(0);
});
it('can inject methods into instances', () => {
const obj = new Object1();
injectionManager.overrideMethod(obj,
'getOtherNumber', INJECTIONS['getOtherNumber']);
expect(obj.getOtherNumber()).toEqual(42);
});
it('can undo instance injections', () => {
const obj = new Object1();
injectAll(obj);
injectionManager.clear();
expect(obj.getNumber()).toEqual(FIXED_NUMBER);
expect(obj.getCount()).toEqual(obj.count);
expect(obj.count).toBeGreaterThan(0);
expect(() => obj.getOtherNumber()).toThrow();
});
it('can restore an original method', () => {
const obj = new Object1();
injectAll(obj);
expect(obj.getNumber()).toEqual(2 * FIXED_NUMBER);
injectionManager.restoreMethod(obj, 'getNumber'); injectionManager.restoreMethod(obj, 'getNumber');
JsUnit.assertEquals(obj.getNumber(), 42); expect(obj.getNumber()).toEqual(FIXED_NUMBER);
});
injectionManager.clear(); it('can extend a GObject vfunc', () => {
// obj is unmodified again
JsUnit.assertEquals(obj.getNumber(), 42);
JsUnit.assertEquals(obj.getCount(), obj.count);
JsUnit.assert(obj.count > 0);
JsUnit.assertRaises(() => obj.getOtherNumber());
// GObject injections
const gobj = new GObject1(); const gobj = new GObject1();
let vfuncCalled; let vfuncCalled;
@ -124,14 +168,27 @@ injectionManager.overrideMethod(
}; };
}); });
// gobj is now modified
vfuncCalled = false; vfuncCalled = false;
gobj.plonk(); gobj.set_property('plonked', true);
JsUnit.assertTrue(vfuncCalled); expect(vfuncCalled).toEqual(true);
});
it('can restore a GObject vfunc', () => {
const gobj = new GObject1();
let vfuncCalled;
injectionManager.overrideMethod(
GObject1.prototype, 'vfunc_set_property', originalMethod => {
return function (...args) {
// eslint-disable-next-line no-invalid-this
originalMethod.apply(this, args);
vfuncCalled = true;
};
});
injectionManager.clear(); injectionManager.clear();
// gobj is unmodified again
vfuncCalled = false; vfuncCalled = false;
gobj.plonk(); gobj.set_property('plonked', true);
JsUnit.assertFalse(vfuncCalled); expect(vfuncCalled).toEqual(false);
});
});