summaryrefslogtreecommitdiff
path: root/test/webhelper/testTranslate.js
blob: 10e389c6dada822cdd6752ce725631221a69ada1 (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
const Endless = imports.gi.Endless;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const WebHelper = imports.webhelper;
const WebKit = imports.gi.WebKit;

const TestClass = new Lang.Class({
    Name: 'testclass',
    Extends: WebHelper.Application,

    vfunc_startup: function() {
        this.parent();
        this.webview = new WebKit.WebView();
        let string = '<html><body><p name="translatable">Translate Me</p></body></html>';
        this.webview.load_string(string, 'text/html', 'UTF-8', 'file://');
        this.win = new Endless.Window({
            application: this
        });
        this.scrolled = new Gtk.ScrolledWindow();
        this.scrolled.add(this.webview);
        this.win.page_manager.add(this.scrolled);

        this.webview.connect('notify::load-status', Lang.bind(this, function() {
            if(this.webview.load_status === WebKit.LoadStatus.FINISHED) {
                this.translate_html(this.webview);
                this.quit();
            }
        }));

        // Add an upper bound on how long the app runs, in case app.quit() does
        // not get called
        GLib.timeout_add_seconds(GLib.PRIORITY_HIGH, 5, Lang.bind(this, function() {
            this.quit();
        }));
    }
});

let app;

function setUp() {
    // Generate a unique ID for each app instance that we test
    let id_string = 'com.endlessm.webhelper.test' + GLib.get_real_time();
    app = new TestClass({
        application_id: id_string
    });
}

function testStringIsTranslated() {
    let translationFunctionWasCalled = false;
    let translationFunctionCalledWithString;
    app._translationFunction = function(s) {
        translationFunctionWasCalled = true;
        translationFunctionCalledWithString = s;
        return s;
    };
    app.run([]);
    assertTrue(translationFunctionWasCalled);
    assertEquals('Translate Me', translationFunctionCalledWithString);
}

// The following two tests are commented out because GJS cannot catch exceptions
// across FFI interfaces (e.g. in GObject callbacks.)

// function testMissingTranslationFunctionIsHandled() {
//     assertRaises(app.run([]));
// }

// function testBadTranslationFunctionIsHandled() {
//     app._translationFunction = "I am not a function";
//     assertRaises(app.run([]));
// }