[gjs: 8/22] testGobjectDestructionAccess: Verify usage of vfunc_dispose




commit a90459e4ae873e257b55adf4e4cb43f875bf0d33
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Mar 24 22:40:09 2021 +0100

    testGobjectDestructionAccess: Verify usage of vfunc_dispose
    
    Like any other virtual function it can be "safely" invoked during normal
    usage without warnings, but will warn on finalization caused by GC.

 installed-tests/js/testGObjectDestructionAccess.js | 32 ++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
---
diff --git a/installed-tests/js/testGObjectDestructionAccess.js 
b/installed-tests/js/testGObjectDestructionAccess.js
index 28901460..3b711e81 100644
--- a/installed-tests/js/testGObjectDestructionAccess.js
+++ b/installed-tests/js/testGObjectDestructionAccess.js
@@ -144,10 +144,10 @@ describe('Access to destroyed GObject', function () {
 describe('Disposed or finalized GObject', function () {
     it('is marked as disposed when it is a manually disposed property', function () {
         const emblem = new Gio.EmblemedIcon({
-            gicon: new Gio.ThemedIcon({ name: 'alarm' }),
+            gicon: new Gio.ThemedIcon({name: 'alarm'}),
         });
 
-        let { gicon } = emblem;
+        let {gicon} = emblem;
         gicon.run_dispose();
         gicon = null;
         System.gc();
@@ -156,6 +156,34 @@ describe('Disposed or finalized GObject', function () {
             /\[object \(DISPOSED\) instance wrapper .* jsobj@0x[a-f0-9]+ native@0x[a-f0-9]+\]/);
     });
 
+    it('calls dispose vfunc on explicit disposal only', function () {
+        const callSpy = jasmine.createSpy('vfunc_dispose');
+        const DisposeFile = GObject.registerClass(class DisposeFile extends Gio.ThemedIcon {
+            vfunc_dispose(...args) {
+                expect(this.names).toEqual(['dummy']);
+                callSpy(...args);
+            }
+        });
+
+        let file = new DisposeFile({name: 'dummy'});
+        file.run_dispose();
+        expect(callSpy).toHaveBeenCalledOnceWith();
+
+        file.run_dispose();
+        expect(callSpy).toHaveBeenCalledTimes(2);
+        file = null;
+
+        GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_CRITICAL,
+            '*during garbage collection*');
+        GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_CRITICAL,
+            '*dispose*');
+        System.gc();
+        GLib.test_assert_expected_messages_internal('Gjs', 'testGObjectDestructionAccess.js', 0,
+            'calls dispose vfunc on explicit disposal only');
+
+        expect(callSpy).toHaveBeenCalledTimes(2);
+    });
+
     it('generates a warn on object garbage collection', function () {
         Gio.File.new_for_path('/').unref();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]