1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
|
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2008 litl, LLC
const ByteArray = imports.byteArray;
const {Gio, GjsTestTools, GLib} = imports.gi;
/* The methods list with their signatures.
*
* *** NOTE: If you add stuff here, you need to update the Test class below.
*/
var TestIface = `<node>
<interface name="org.gnome.gjs.Test">
<method name="nonJsonFrobateStuff">
<arg type="i" direction="in"/>
<arg type="s" direction="out"/>
</method>
<method name="frobateStuff">
<arg type="a{sv}" direction="in"/>
<arg type="a{sv}" direction="out"/>
</method>
<method name="alwaysThrowException">
<arg type="a{sv}" direction="in"/>
<arg type="a{sv}" direction="out"/>
</method>
<method name="thisDoesNotExist"/>
<method name="noInParameter">
<arg type="s" direction="out"/>
</method>
<method name="multipleInArgs">
<arg type="i" direction="in"/>
<arg type="i" direction="in"/>
<arg type="i" direction="in"/>
<arg type="i" direction="in"/>
<arg type="i" direction="in"/>
<arg type="s" direction="out"/>
</method>
<method name="noReturnValue"/>
<method name="emitSignal"/>
<method name="multipleOutValues">
<arg type="s" direction="out"/>
<arg type="s" direction="out"/>
<arg type="s" direction="out"/>
</method>
<method name="oneArrayOut">
<arg type="as" direction="out"/>
</method>
<method name="arrayOfArrayOut">
<arg type="aas" direction="out"/>
</method>
<method name="multipleArrayOut">
<arg type="as" direction="out"/>
<arg type="as" direction="out"/>
</method>
<method name="arrayOutBadSig">
<arg type="i" direction="out"/>
</method>
<method name="byteArrayEcho">
<arg type="ay" direction="in"/>
<arg type="ay" direction="out"/>
</method>
<method name="byteEcho">
<arg type="y" direction="in"/>
<arg type="y" direction="out"/>
</method>
<method name="dictEcho">
<arg type="a{sv}" direction="in"/>
<arg type="a{sv}" direction="out"/>
</method>
<method name="echo">
<arg type="s" direction="in"/>
<arg type="i" direction="in"/>
<arg type="s" direction="out"/>
<arg type="i" direction="out"/>
</method>
<method name="structArray">
<arg type="a(ii)" direction="out"/>
</method>
<method name="fdIn">
<arg type="h" direction="in"/>
<arg type="ay" direction="out"/>
</method>
<method name="fdIn2">
<arg type="h" direction="in"/>
<arg type="ay" direction="out"/>
</method>
<method name="fdOut">
<arg type="ay" direction="in"/>
<arg type="h" direction="out"/>
</method>
<method name="fdOut2">
<arg type="ay" direction="in"/>
<arg type="h" direction="out"/>
</method>
<signal name="signalFoo">
<arg type="s" direction="out"/>
</signal>
<property name="PropReadOnly" type="d" access="read" />
<property name="PropWriteOnly" type="s" access="write" />
<property name="PropReadWrite" type="v" access="readwrite" />
<property name="PropPrePacked" type="a{sv}" access="read" />
</interface>
</node>`;
const PROP_READ_ONLY_INITIAL_VALUE = Math.random();
const PROP_READ_WRITE_INITIAL_VALUE = 58;
const PROP_WRITE_ONLY_INITIAL_VALUE = 'Initial value';
/* Test is the actual object exporting the dbus methods */
class Test {
constructor() {
this._propReadOnly = PROP_READ_ONLY_INITIAL_VALUE;
this._propWriteOnly = PROP_WRITE_ONLY_INITIAL_VALUE;
this._propReadWrite = PROP_READ_WRITE_INITIAL_VALUE;
this._impl = Gio.DBusExportedObject.wrapJSObject(TestIface, this);
this._impl.export(Gio.DBus.session, '/org/gnome/gjs/Test');
}
frobateStuff() {
return {hello: new GLib.Variant('s', 'world')};
}
nonJsonFrobateStuff(i) {
if (i === 42)
return '42 it is!';
else
return 'Oops';
}
alwaysThrowException() {
throw Error('Exception!');
}
thisDoesNotExist() {
/* We'll remove this later! */
}
noInParameter() {
return 'Yes!';
}
multipleInArgs(a, b, c, d, e) {
return `${a} ${b} ${c} ${d} ${e}`;
}
emitPropertyChanged(name, value) {
this._impl.emit_property_changed(name, value);
}
emitSignal() {
this._impl.emit_signal('signalFoo', GLib.Variant.new('(s)', ['foobar']));
}
noReturnValue() {
/* Empty! */
}
/* The following two functions have identical return values
* in JS, but the bus message will be different.
* multipleOutValues is "sss", while oneArrayOut is "as"
*/
multipleOutValues() {
return ['Hello', 'World', '!'];
}
oneArrayOut() {
return ['Hello', 'World', '!'];
}
/* Same thing again. In this case multipleArrayOut is "asas",
* while arrayOfArrayOut is "aas".
*/
multipleArrayOut() {
return [['Hello', 'World'], ['World', 'Hello']];
}
arrayOfArrayOut() {
return [['Hello', 'World'], ['World', 'Hello']];
}
arrayOutBadSig() {
return Symbol('Hello World!');
}
byteArrayEcho(binaryString) {
return binaryString;
}
byteEcho(aByte) {
return aByte;
}
dictEcho(dict) {
return dict;
}
/* This one is implemented asynchronously. Returns
* the input arguments */
echoAsync(parameters, invocation) {
var [someString, someInt] = parameters;
GLib.idle_add(GLib.PRIORITY_DEFAULT, function () {
invocation.return_value(new GLib.Variant('(si)', [someString, someInt]));
return false;
});
}
// double
get PropReadOnly() {
return this._propReadOnly;
}
// string
set PropWriteOnly(value) {
this._propWriteOnly = value;
}
// variant
get PropReadWrite() {
return new GLib.Variant('s', this._propReadWrite.toString());
}
set PropReadWrite(value) {
this._propReadWrite = value.deepUnpack();
}
get PropPrePacked() {
return new GLib.Variant('a{sv}', {
member: GLib.Variant.new_string('value'),
});
}
structArray() {
return [[128, 123456], [42, 654321]];
}
fdIn(fdIndex, fdList) {
const fd = fdList.get(fdIndex);
const stream = new Gio.UnixInputStream({fd, closeFd: true});
const bytes = stream.read_bytes(4096, null);
return bytes;
}
// Same as fdIn(), but implemented asynchronously
fdIn2Async([fdIndex], invocation, fdList) {
const fd = fdList.get(fdIndex);
const stream = new Gio.UnixInputStream({fd, closeFd: true});
stream.read_bytes_async(4096, GLib.PRIORITY_DEFAULT, null, (obj, res) => {
const bytes = obj.read_bytes_finish(res);
invocation.return_value(new GLib.Variant('(ay)', [bytes]));
});
}
fdOut(bytes) {
const fd = GjsTestTools.open_bytes(bytes);
const fdList = Gio.UnixFDList.new_from_array([fd]);
return [0, fdList];
}
fdOut2Async([bytes], invocation) {
GLib.idle_add(GLib.PRIORITY_DEFAULT, function () {
const fd = GjsTestTools.open_bytes(bytes);
const fdList = Gio.UnixFDList.new_from_array([fd]);
invocation.return_value_with_unix_fd_list(new GLib.Variant('(h)', [0]),
fdList);
return GLib.SOURCE_REMOVE;
});
}
}
const ProxyClass = Gio.DBusProxy.makeProxyWrapper(TestIface);
describe('Exported DBus object', function () {
let ownNameID;
var test;
var proxy;
let loop;
function waitForServerProperty(property, value = undefined, timeout = 500) {
let waitId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, timeout, () => {
waitId = 0;
throw new Error(`Timeout waiting for property ${property} expired`);
});
while (waitId && (!test[property] ||
value !== undefined && test[property] !== value))
loop.get_context().iteration(true);
if (waitId)
GLib.source_remove(waitId);
expect(waitId).not.toBe(0);
return test[property];
}
beforeAll(function () {
loop = new GLib.MainLoop(null, false);
test = new Test();
ownNameID = Gio.DBus.session.own_name('org.gnome.gjs.Test',
Gio.BusNameOwnerFlags.NONE,
name => {
log(`Acquired name ${name}`);
loop.quit();
},
name => {
log(`Lost name ${name}`);
});
loop.run();
new ProxyClass(Gio.DBus.session, 'org.gnome.gjs.Test',
'/org/gnome/gjs/Test',
(obj, error) => {
expect(error).toBeNull();
proxy = obj;
expect(proxy).not.toBeNull();
loop.quit();
},
Gio.DBusProxyFlags.NONE);
loop.run();
});
afterAll(function () {
// Not really needed, but if we don't cleanup
// memory checking will complain
Gio.DBus.session.unown_name(ownNameID);
});
beforeEach(function () {
loop = new GLib.MainLoop(null, false);
});
it('can call a remote method', function () {
proxy.frobateStuffRemote({}, ([result], excp) => {
expect(excp).toBeNull();
expect(result.hello.deepUnpack()).toEqual('world');
loop.quit();
});
loop.run();
});
it('can call a method with async/await', async function () {
const [{hello}] = await proxy.frobateStuffAsync({});
expect(hello.deepUnpack()).toEqual('world');
});
it('can initiate a proxy with promise and call a method with async/await', async function () {
const asyncProxy = await ProxyClass.newAsync(Gio.DBus.session,
'org.gnome.gjs.Test', '/org/gnome/gjs/Test');
expect(asyncProxy).toBeInstanceOf(Gio.DBusProxy);
const [{hello}] = await asyncProxy.frobateStuffAsync({});
expect(hello.deepUnpack()).toEqual('world');
});
it('can call a remote method when not using makeProxyWrapper', function () {
let info = Gio.DBusNodeInfo.new_for_xml(TestIface);
let iface = info.interfaces[0];
let otherProxy = null;
Gio.DBusProxy.new_for_bus(Gio.BusType.SESSION,
Gio.DBusProxyFlags.DO_NOT_AUTO_START,
iface,
'org.gnome.gjs.Test',
'/org/gnome/gjs/Test',
iface.name,
null,
(o, res) => {
otherProxy = Gio.DBusProxy.new_for_bus_finish(res);
loop.quit();
});
loop.run();
otherProxy.frobateStuffRemote({}, ([result], excp) => {
expect(excp).toBeNull();
expect(result.hello.deepUnpack()).toEqual('world');
loop.quit();
});
loop.run();
});
/* excp must be exactly the exception thrown by the remote method
(more or less) */
it('can handle an exception thrown by a remote method', function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
'JS ERROR: Exception in method call: alwaysThrowException: *');
proxy.alwaysThrowExceptionRemote({}, function (result, excp) {
expect(excp).not.toBeNull();
loop.quit();
});
loop.run();
});
it('can handle an exception thrown by a method with async/await', async function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
'JS ERROR: Exception in method call: alwaysThrowException: *');
await expectAsync(proxy.alwaysThrowExceptionAsync({})).toBeRejected();
});
it('can still destructure the return value when an exception is thrown', function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
'JS ERROR: Exception in method call: alwaysThrowException: *');
// This test will not fail, but instead if the functionality is not
// implemented correctly it will hang. The exception in the function
// argument destructuring will not propagate across the FFI boundary
// and the main loop will never quit.
// https://bugzilla.gnome.org/show_bug.cgi?id=729015
proxy.alwaysThrowExceptionRemote({}, function ([a, b, c], excp) {
expect(a).not.toBeDefined();
expect(b).not.toBeDefined();
expect(c).not.toBeDefined();
void excp;
loop.quit();
});
loop.run();
});
it('throws an exception when trying to call a method that does not exist', function () {
/* First remove the method from the object! */
delete Test.prototype.thisDoesNotExist;
proxy.thisDoesNotExistRemote(function (result, excp) {
expect(excp).not.toBeNull();
loop.quit();
});
loop.run();
});
it('throws an exception when trying to call an async method that does not exist', async function () {
delete Test.prototype.thisDoesNotExist;
await expectAsync(proxy.thisDoesNotExistAsync()).toBeRejected();
});
it('can pass a parameter to a remote method that is not a JSON object', function () {
proxy.nonJsonFrobateStuffRemote(42, ([result], excp) => {
expect(result).toEqual('42 it is!');
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('can pass a parameter to a method with async/await that is not a JSON object', async function () {
const [result] = await proxy.nonJsonFrobateStuffAsync(1);
expect(result).toEqual('Oops');
});
it('can call a remote method with no in parameter', function () {
proxy.noInParameterRemote(([result], excp) => {
expect(result).toEqual('Yes!');
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('can call an async/await method with no in parameter', async function () {
const [result] = await proxy.noInParameterAsync();
expect(result).toEqual('Yes!');
});
it('can call a remote method with multiple in parameters', function () {
proxy.multipleInArgsRemote(1, 2, 3, 4, 5, ([result], excp) => {
expect(result).toEqual('1 2 3 4 5');
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('can call an async/await method with multiple in parameters', async function () {
const [result] = await proxy.multipleInArgsAsync(1, 2, 3, 4, 5);
expect(result).toEqual('1 2 3 4 5');
});
it('can call a remote method with no return value', function () {
proxy.noReturnValueRemote(([result], excp) => {
expect(result).not.toBeDefined();
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('can call an async/await method with no return value', async function () {
const [result] = await proxy.noReturnValueAsync();
expect(result).not.toBeDefined();
});
it('can emit a DBus signal', function () {
let handler = jasmine.createSpy('signalFoo');
let id = proxy.connectSignal('signalFoo', handler);
handler.and.callFake(() => proxy.disconnectSignal(id));
proxy.emitSignalRemote(([result], excp) => {
expect(result).not.toBeDefined();
expect(excp).toBeNull();
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(jasmine.anything(),
jasmine.anything(), ['foobar']);
loop.quit();
});
loop.run();
});
it('can emit a DBus signal with async/await', async function () {
const handler = jasmine.createSpy('signalFoo');
const id = proxy.connectSignal('signalFoo', handler);
handler.and.callFake(() => proxy.disconnectSignal(id));
const [result] = await proxy.emitSignalAsync();
expect(result).not.toBeDefined();
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(jasmine.anything(),
jasmine.anything(), ['foobar']);
});
it('can call a remote method with multiple return values', function () {
proxy.multipleOutValuesRemote(function (result, excp) {
expect(result).toEqual(['Hello', 'World', '!']);
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('can call an async/await method with multiple return values', async function () {
const results = await proxy.multipleOutValuesAsync();
expect(results).toEqual(['Hello', 'World', '!']);
});
it('does not coalesce one array into the array of return values', function () {
proxy.oneArrayOutRemote(([result], excp) => {
expect(result).toEqual(['Hello', 'World', '!']);
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('does not coalesce one array into the array of return values with async/await', async function () {
const [result] = await proxy.oneArrayOutAsync();
expect(result).toEqual(['Hello', 'World', '!']);
});
it('does not coalesce an array of arrays into the array of return values', function () {
proxy.arrayOfArrayOutRemote(([[a1, a2]], excp) => {
expect(a1).toEqual(['Hello', 'World']);
expect(a2).toEqual(['World', 'Hello']);
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('does not coalesce an array of arrays into the array of return values with async/await', async function () {
const [[a1, a2]] = await proxy.arrayOfArrayOutAsync();
expect(a1).toEqual(['Hello', 'World']);
expect(a2).toEqual(['World', 'Hello']);
});
it('can return multiple arrays from a remote method', function () {
proxy.multipleArrayOutRemote(([a1, a2], excp) => {
expect(a1).toEqual(['Hello', 'World']);
expect(a2).toEqual(['World', 'Hello']);
expect(excp).toBeNull();
loop.quit();
});
loop.run();
});
it('can return multiple arrays from an async/await method', async function () {
const [a1, a2] = await proxy.multipleArrayOutAsync();
expect(a1).toEqual(['Hello', 'World']);
expect(a2).toEqual(['World', 'Hello']);
});
it('handles a bad signature by throwing an exception', function () {
proxy.arrayOutBadSigRemote(function (result, excp) {
expect(excp).not.toBeNull();
loop.quit();
});
loop.run();
});
it('handles a bad signature in async/await by rejecting the promise', async function () {
await expectAsync(proxy.arrayOutBadSigAsync()).toBeRejected();
});
it('can call a remote method that is implemented asynchronously', function () {
let someString = 'Hello world!';
let someInt = 42;
proxy.echoRemote(someString, someInt,
function (result, excp) {
expect(excp).toBeNull();
expect(result).toEqual([someString, someInt]);
loop.quit();
});
loop.run();
});
it('can call an async/await method that is implemented asynchronously', async function () {
const someString = 'Hello world!';
const someInt = 42;
const results = await proxy.echoAsync(someString, someInt);
expect(results).toEqual([someString, someInt]);
});
it('can send and receive bytes from a remote method', function () {
let someBytes = [0, 63, 234];
someBytes.forEach(b => {
proxy.byteEchoRemote(b, ([result], excp) => {
expect(excp).toBeNull();
expect(result).toEqual(b);
loop.quit();
});
loop.run();
});
});
it('can send and receive bytes from an async/await method', async function () {
let someBytes = [0, 63, 234];
await Promise.allSettled(someBytes.map(async b => {
const [byte] = await proxy.byteEchoAsync(b);
expect(byte).toEqual(b);
}));
});
it('can call a remote method that returns an array of structs', function () {
proxy.structArrayRemote(([result], excp) => {
expect(excp).toBeNull();
expect(result).toEqual([[128, 123456], [42, 654321]]);
loop.quit();
});
loop.run();
});
it('can call an async/await method that returns an array of structs', async function () {
const [result] = await proxy.structArrayAsync();
expect(result).toEqual([[128, 123456], [42, 654321]]);
});
it('can send and receive dicts from a remote method', function () {
let someDict = {
aDouble: new GLib.Variant('d', 10),
// should be an integer after round trip
anInteger: new GLib.Variant('i', 10.5),
// should remain a double
aDoubleBeforeAndAfter: new GLib.Variant('d', 10.5),
};
proxy.dictEchoRemote(someDict, ([result], excp) => {
expect(excp).toBeNull();
expect(result).not.toBeNull();
// verify the fractional part was dropped off int
expect(result['anInteger'].deepUnpack()).toEqual(10);
// and not dropped off a double
expect(result['aDoubleBeforeAndAfter'].deepUnpack()).toEqual(10.5);
// check without type conversion
expect(result['aDouble'].deepUnpack()).toBe(10.0);
loop.quit();
});
loop.run();
});
it('can send and receive dicts from an async/await method', async function () {
// See notes in test above
const [result] = await proxy.dictEchoAsync({
aDouble: new GLib.Variant('d', 10),
anInteger: new GLib.Variant('i', 10.5),
aDoubleBeforeAndAfter: new GLib.Variant('d', 10.5),
});
expect(result).not.toBeNull();
expect(result['anInteger'].deepUnpack()).toEqual(10);
expect(result['aDoubleBeforeAndAfter'].deepUnpack()).toEqual(10.5);
expect(result['aDouble'].deepUnpack()).toBe(10.0);
});
it('can call a remote method with a Unix FD', function (done) {
const expectedBytes = ByteArray.fromString('some bytes');
const fd = GjsTestTools.open_bytes(expectedBytes);
const fdList = Gio.UnixFDList.new_from_array([fd]);
proxy.fdInRemote(0, fdList, ([bytes], exc, outFdList) => {
expect(exc).toBeNull();
expect(outFdList).toBeNull();
expect(bytes).toEqual(expectedBytes);
done();
});
});
it('can call an async/await method with a Unix FD', async function () {
const encoder = new TextEncoder();
const expectedBytes = encoder.encode('some bytes');
const fd = GjsTestTools.open_bytes(expectedBytes);
const fdList = Gio.UnixFDList.new_from_array([fd]);
const [bytes, outFdList] = await proxy.fdInAsync(0, fdList);
expect(outFdList).not.toBeDefined();
expect(bytes).toEqual(expectedBytes);
});
it('can call an asynchronously implemented remote method with a Unix FD', function (done) {
const expectedBytes = ByteArray.fromString('some bytes');
const fd = GjsTestTools.open_bytes(expectedBytes);
const fdList = Gio.UnixFDList.new_from_array([fd]);
proxy.fdIn2Remote(0, fdList, ([bytes], exc, outFdList) => {
expect(exc).toBeNull();
expect(outFdList).toBeNull();
expect(bytes).toEqual(expectedBytes);
done();
});
});
it('can call an asynchronously implemented async/await method with a Unix FD', async function () {
const encoder = new TextEncoder();
const expectedBytes = encoder.encode('some bytes');
const fd = GjsTestTools.open_bytes(expectedBytes);
const fdList = Gio.UnixFDList.new_from_array([fd]);
const [bytes, outFdList] = await proxy.fdIn2Async(0, fdList);
expect(outFdList).not.toBeDefined();
expect(bytes).toEqual(expectedBytes);
});
function readBytesFromFdSync(fd) {
const stream = new Gio.UnixInputStream({fd, closeFd: true});
const bytes = stream.read_bytes(4096, null);
return ByteArray.fromGBytes(bytes);
}
it('can call a remote method that returns a Unix FD', function (done) {
const expectedBytes = ByteArray.fromString('some bytes');
proxy.fdOutRemote(expectedBytes, ([fdIndex], exc, outFdList) => {
expect(exc).toBeNull();
const bytes = readBytesFromFdSync(outFdList.get(fdIndex));
expect(bytes).toEqual(expectedBytes);
done();
});
});
it('can call an async/await method that returns a Unix FD', async function () {
const encoder = new TextEncoder();
const expectedBytes = encoder.encode('some bytes');
const [fdIndex, outFdList] = await proxy.fdOutAsync(expectedBytes);
const bytes = readBytesFromFdSync(outFdList.get(fdIndex));
expect(bytes).toEqual(expectedBytes);
});
it('can call an asynchronously implemented remote method that returns a Unix FD', function (done) {
const expectedBytes = ByteArray.fromString('some bytes');
proxy.fdOut2Remote(expectedBytes, ([fdIndex], exc, outFdList) => {
expect(exc).toBeNull();
const bytes = readBytesFromFdSync(outFdList.get(fdIndex));
expect(bytes).toEqual(expectedBytes);
done();
});
});
it('can call an asynchronously implemented asyc/await method that returns a Unix FD', async function () {
const encoder = new TextEncoder();
const expectedBytes = encoder.encode('some bytes');
const [fdIndex, outFdList] = await proxy.fdOut2Async(expectedBytes);
const bytes = readBytesFromFdSync(outFdList.get(fdIndex));
expect(bytes).toEqual(expectedBytes);
});
it('throws an exception when not passing a Gio.UnixFDList to a method that requires one', function () {
expect(() => proxy.fdInRemote(0, () => {})).toThrow();
});
it('rejects the promise when not passing a Gio.UnixFDList to an async method that requires one', async function () {
await expectAsync(proxy.fdInAsync(0)).toBeRejected();
});
it('throws an exception when passing a handle out of range of a Gio.UnixFDList', function () {
const fdList = new Gio.UnixFDList();
expect(() => proxy.fdInRemote(0, fdList, () => {})).toThrow();
});
it('rejects the promise when async passing a handle out of range of a Gio.UnixFDList', async function () {
const fdList = new Gio.UnixFDList();
await expectAsync(proxy.fdInAsync(0, fdList)).toBeRejected();
});
it('Has defined properties', function () {
expect(proxy.hasOwnProperty('PropReadWrite')).toBeTruthy();
expect(proxy.hasOwnProperty('PropReadOnly')).toBeTruthy();
expect(proxy.hasOwnProperty('PropWriteOnly')).toBeTruthy();
expect(proxy.hasOwnProperty('PropPrePacked')).toBeTruthy();
});
it('reading readonly property works', function () {
expect(proxy.PropReadOnly).toEqual(PROP_READ_ONLY_INITIAL_VALUE);
});
it('reading readwrite property works', function () {
expect(proxy.PropReadWrite).toEqual(
GLib.Variant.new_string(PROP_READ_WRITE_INITIAL_VALUE.toString()));
});
it('reading writeonly throws an error', function () {
expect(() => proxy.PropWriteOnly).toThrowError('Property PropWriteOnly is not readable');
});
it('Setting a readwrite property works', function () {
let testStr = 'GjsVariantValue';
expect(() => {
proxy.PropReadWrite = GLib.Variant.new_string(testStr);
}).not.toThrow();
expect(proxy.PropReadWrite.deepUnpack()).toEqual(testStr);
expect(waitForServerProperty('_propReadWrite', testStr)).toEqual(testStr);
});
it('Setting a writeonly property works', function () {
let testValue = Math.random().toString();
expect(() => {
proxy.PropWriteOnly = testValue;
}).not.toThrow();
expect(() => proxy.PropWriteOnly).toThrow();
expect(waitForServerProperty('_propWriteOnly', testValue)).toEqual(testValue);
});
it('Setting a readonly property throws an error', function () {
let testValue = Math.random().toString();
expect(() => {
proxy.PropReadOnly = testValue;
}).toThrowError('Property PropReadOnly is not writable');
expect(proxy.PropReadOnly).toBe(PROP_READ_ONLY_INITIAL_VALUE);
});
it('Reading a property that prepacks the return value works', function () {
expect(proxy.PropPrePacked.member).toBeInstanceOf(GLib.Variant);
expect(proxy.PropPrePacked.member.get_type_string()).toEqual('s');
});
it('Marking a property as invalidated works', function () {
let changedProps = {};
let invalidatedProps = [];
proxy.connect('g-properties-changed', (proxy_, changed, invalidated) => {
changedProps = changed.deepUnpack();
invalidatedProps = invalidated;
loop.quit();
});
test.emitPropertyChanged('PropReadOnly', null);
loop.run();
expect(changedProps).not.toContain('PropReadOnly');
expect(invalidatedProps).toContain('PropReadOnly');
});
});
describe('DBus Proxy wrapper', function () {
let loop;
let wasPromise;
let writerFunc;
beforeAll(function () {
loop = new GLib.MainLoop(null, false);
wasPromise = Gio.DBusProxy.prototype._original_init_async instanceof Function;
Gio._promisify(Gio.DBusProxy.prototype, 'init_async');
writerFunc = jasmine.createSpy(
'log writer func', (level, fields) => {
const decoder = new TextDecoder('utf-8');
const domain = decoder.decode(fields?.GLIB_DOMAIN);
const message = `${decoder.decode(fields?.MESSAGE)}\n`;
level |= GLib.LogLevelFlags.FLAG_RECURSION;
GLib.log_default_handler(domain, level, message, null);
return GLib.LogWriterOutput.HANDLED;
});
writerFunc.and.callThrough();
GLib.log_set_writer_func(writerFunc);
});
beforeEach(function () {
writerFunc.calls.reset();
});
it('init failures are reported in sync mode', function () {
const cancellable = new Gio.Cancellable();
cancellable.cancel();
expect(() => new ProxyClass(Gio.DBus.session, 'org.gnome.gjs.Test',
'/org/gnome/gjs/Test',
Gio.DBusProxyFlags.NONE,
cancellable)).toThrow();
});
it('init failures are reported in async mode', function () {
const cancellable = new Gio.Cancellable();
cancellable.cancel();
const initDoneSpy = jasmine.createSpy(
'init finish func', () => loop.quit());
initDoneSpy.and.callThrough();
new ProxyClass(Gio.DBus.session, 'org.gnome.gjs.Test',
'/org/gnome/gjs/Test',
initDoneSpy, cancellable, Gio.DBusProxyFlags.NONE);
loop.run();
expect(initDoneSpy).toHaveBeenCalledTimes(1);
const {args: callArgs} = initDoneSpy.calls.mostRecent();
expect(callArgs.at(0)).toBeNull();
expect(callArgs.at(1).matches(
Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)).toBeTrue();
});
it('can init a proxy asynchronously when promisified', function () {
new ProxyClass(Gio.DBus.session, 'org.gnome.gjs.Test',
'/org/gnome/gjs/Test',
() => loop.quit(),
Gio.DBusProxyFlags.NONE);
loop.run();
expect(writerFunc).not.toHaveBeenCalled();
});
it('can create a proxy from a promise', async function () {
const proxyPromise = ProxyClass.newAsync(Gio.DBus.session, 'org.gnome.gjs.Test',
'/org/gnome/gjs/Test');
await expectAsync(proxyPromise).toBeResolved();
});
it('can create fail a proxy from a promise', async function () {
const cancellable = new Gio.Cancellable();
cancellable.cancel();
const proxyPromise = ProxyClass.newAsync(Gio.DBus.session, 'org.gnome.gjs.Test',
'/org/gnome/gjs/Test', cancellable);
await expectAsync(proxyPromise).toBeRejected();
});
afterAll(function () {
if (!wasPromise) {
// Remove stuff added by Gio._promisify, this can be not needed in future
// nor should break other tests, but we didn't want to depend on those.
expect(Gio.DBusProxy.prototype._original_init_async).toBeInstanceOf(Function);
Gio.DBusProxy.prototype.init_async = Gio.DBusProxy.prototype._original_init_async;
delete Gio.DBusProxy.prototype._original_init_async;
}
});
});
|