summaryrefslogtreecommitdiff
path: root/test/webhelper/testWebActions2Old.js
blob: a602dc160dace0301c2913163963f1936dbcdcce (plain)
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
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Mainloop = imports.mainloop;
const WebHelper2 = imports.webhelper2_old;
const WebKit2 = imports.gi.WebKit2;

const WELL_KNOWN_NAME = 'com.endlessm.WebHelper.testWebActions2';

Gtk.init(null);

describe('WebKit2-3.0 actions bindings', function () {
    let owner_id, connection, webview, webhelper, web_action_spy;

    beforeAll(function (done) {
        owner_id = Gio.DBus.own_name(Gio.BusType.SESSION, WELL_KNOWN_NAME,
            Gio.BusNameOwnerFlags.NONE,
            null,  // bus acquired
            (con) => {  // name acquired
                connection = con;
                done();
            },
            null);  // name lost
    });

    afterAll(function () {
        Gio.DBus.unown_name(owner_id);
    });

    function run_loop(action_to_test) {
        let string = '<html><head><meta http-equiv="refresh" content="0;url=' +
            action_to_test + '"></head><body></body></html>';
        webview.load_html(string, null);
        Mainloop.run('webhelper2');
    }

    beforeEach(function () {
        webhelper = new WebHelper2.WebHelper({
            well_known_name: WELL_KNOWN_NAME,
            connection: connection,
        });
        webview = new WebKit2.WebView();
        web_action_spy = jasmine.createSpy('web_action_spy').and.callFake(() =>
            Mainloop.quit('webhelper2'));
        webhelper.define_web_action('action', web_action_spy);
    });

    afterEach(function () {
        webhelper.unregister();
    });

    it('calls a web action', function () {
        run_loop('webhelper://action');
        expect(web_action_spy).toHaveBeenCalled();
    });

    it('calls a web action with a parameter', function () {
        run_loop('webhelper://action?param=value');
        expect(web_action_spy).toHaveBeenCalledWith(jasmine.objectContaining({
            param: 'value',
        }));
    });

    it('calls a web action with many parameters', function () {
        run_loop('webhelper://action?first=thefirst&second=thesecond&third=thethird');
        expect(web_action_spy).toHaveBeenCalledWith(jasmine.objectContaining({
            first: 'thefirst',
            second: 'thesecond',
            third: 'thethird',
        }));
    });

    it('uri-decodes parameter names', function () {
        run_loop('webhelper://action?p%C3%A4r%C3%A4m%F0%9F%92%A9=value');
        expect(web_action_spy).toHaveBeenCalledWith(jasmine.objectContaining({
            'päräm💩': 'value',
        }));
    });

    it('uri-decodes parameter values', function () {
        run_loop('webhelper://action?param=v%C3%A1lu%C3%A9%F0%9F%92%A9');
        expect(web_action_spy).toHaveBeenCalledWith(jasmine.objectContaining({
            param: 'válué💩',
        }));
    });

    // This is commented out because GJS cannot catch exceptions across FFI
    // interfaces (e.g. in GObject callbacks.)
    xit('raises an exception on a nonexistent action instead of calling it', function () {
        expect(function () {
            run_loop('webhelper://nonexistentAction?param=value');
        }).toThrow();
    });

    it('calls a web action with a blank parameter', function () {
        run_loop('webhelper://action?param=');
        expect(web_action_spy).toHaveBeenCalledWith(jasmine.objectContaining({
            param: '',
        }));
    });

    it('uri-decodes web action names', function () {
        webhelper.define_web_action('äction💩Quit', web_action_spy);
        run_loop('webhelper://%C3%A4ction%F0%9F%92%A9Quit');
        expect(web_action_spy).toHaveBeenCalled();
    });

    it('can define more than one action with define_web_actions()', function () {
        webhelper.define_web_actions({
            action2: web_action_spy,
        });
        run_loop('webhelper://action2');
        expect(web_action_spy).toBeTruthy();
    });

    it('complains when defining an action that is not a function', function () {
        expect(function () {
            webhelper.define_web_action('badAction', 'not a function');
        }).toThrow();
    });
});