summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorCosimo Cecchi <cosimoc@gnome.org>2015-05-22 13:18:11 -0700
committerCosimo Cecchi <cosimoc@gnome.org>2015-05-22 13:18:11 -0700
commitfe1109c96ca6185a26e293996c1bdf3871e3b09a (patch)
treeadc2d2a7d32d4c31571238708423d5d880c94b0f /test
parente131566bc1ca2e64a58c62231e8c7aaaa2ca465b (diff)
parent6a1d8e328ffe96c3e68a468545addef6ffd15da9 (diff)
Merge pull request #3183 from endlessm/sdk/291
Port WebHelper to WebKit2
Diffstat (limited to 'test')
-rw-r--r--test/Makefile.am.inc3
-rw-r--r--test/smoke-tests/webhelper/webview2.js147
-rw-r--r--test/webhelper/testTranslate2.js178
-rw-r--r--test/webhelper/testWebActions2.js121
4 files changed, 449 insertions, 0 deletions
diff --git a/test/Makefile.am.inc b/test/Makefile.am.inc
index 11e4dea..411d006 100644
--- a/test/Makefile.am.inc
+++ b/test/Makefile.am.inc
@@ -44,7 +44,9 @@ EXTRA_DIST += \
javascript_tests = \
test/tools/eos-application-manifest/testInit.js \
test/webhelper/testTranslate.js \
+ test/webhelper/testTranslate2.js \
test/webhelper/testWebActions.js \
+ test/webhelper/testWebActions2.js \
test/webhelper/testUpdateFontSize.js \
test/endless/testCustomContainer.js \
test/endless/testTopbarNavButton.js \
@@ -78,4 +80,5 @@ TESTS_ENVIRONMENT = \
export GI_TYPELIB_PATH="$(top_builddir)$${GI_TYPELIB_PATH:+:$$GI_TYPELIB_PATH}"; \
export LD_LIBRARY_PATH="$(top_builddir)/.libs$${LD_LIBRARY_PATH:+:$$LD_LIBRARY_PATH}"; \
export XDG_CONFIG_HOME=`mktemp -d $${TMPDIR:-/tmp}/sdktestconfig.XXXXXXXX`; \
+ export WEBHELPER_UNINSTALLED_EXTENSION_DIR="$(top_builddir)/.libs"; \
$(NULL)
diff --git a/test/smoke-tests/webhelper/webview2.js b/test/smoke-tests/webhelper/webview2.js
new file mode 100644
index 0000000..f50c42d
--- /dev/null
+++ b/test/smoke-tests/webhelper/webview2.js
@@ -0,0 +1,147 @@
+// Copyright 2015 Endless Mobile, Inc.
+
+const Endless = imports.gi.Endless;
+const Gettext = imports.gettext;
+const Gtk = imports.gi.Gtk;
+const Lang = imports.lang;
+const WebHelper2 = imports.webhelper2;
+const WebKit2 = imports.gi.WebKit2;
+
+const TEST_APPLICATION_ID = 'com.endlessm.example.test-webview2';
+const TEST_HTML = '\
+<html> \
+<head> \
+<title>First page</title> \
+<style> \
+p, form { \
+ width: 50%; \
+ padding: 1em; \
+ background: #FFFFFF; \
+} \
+body { \
+ background: #EEEEEE; \
+} \
+</style> \
+</head> \
+\
+<body> \
+<script type="text/javascript"> \
+ window.ngettextButton = (function () { \
+ var times = 0; \
+ return function () { \
+ times++; \
+ var message = ngettext("You have clicked me __ time", "You have clicked me __ times", times); \
+ alert(message.replace("__", times.toString())); \
+ }; \
+ })(); \
+</script> \
+<h1>First page</h1> \
+\
+<p><a href="webhelper://moveToPage?name=page2">Move to page 2</a></p> \
+\
+<p><a \
+href="webhelper://showMessageFromParameter?msg=This%20is%20a%20message%20from%20the%20URL%20parameter">Show \
+message from parameter in this URL</a></p> \
+\
+<form action="webhelper://showMessageFromParameter"> \
+<input name="msg" value="I am in a form!"/> \
+<input type="submit" value="Show message using a form"/> \
+</form> \
+\
+<p><a href="http://wikipedia.org">Regular link to a Web site</a></p> \
+\
+<p>This is text that will be italicized: <span name="translatable">Hello, \
+world!</span></p> \
+\
+<p> \
+ <button onclick="alert(gettext(\'I came from gettext\'));"> \
+ Click me to use gettext \
+ </button> \
+ <button onclick="ngettextButton();">Click me to use ngettext</button> \
+</p> \
+\
+</body> \
+</html>';
+
+const TestApplication = new Lang.Class({
+ Name: 'TestApplication',
+ Extends: Endless.Application,
+
+ vfunc_dbus_register: function (connection, object_path) {
+ this._webhelper = new WebHelper2.WebHelper({
+ well_known_name: this.application_id,
+ connection: connection,
+ });
+ return this.parent(connection, object_path);
+ },
+
+ vfunc_startup: function () {
+ this.parent();
+
+ this._webhelper.set_gettext((s) => s.italics());
+ this._webhelper.set_ngettext((s, p, n) =>
+ '** ' + (n == 1 ? s : p) + ' **');
+ this._webhelper.define_web_actions({
+ moveToPage: this.moveToPage.bind(this),
+ showMessageFromParameter: this.showMessageFromParameter.bind(this),
+ });
+
+ this._webview = new WebKit2.WebView();
+ this._webview.connect('load-changed', (webview, event) => {
+ if (event === WebKit2.LoadEvent.FINISHED)
+ this._webhelper.translate_html(webview, null, (obj, res) => {
+ this._webhelper.translate_html_finish(res);
+ });
+ });
+ this._webview.load_html(TEST_HTML, null);
+
+ this._page2 = new Gtk.Grid();
+ let back_button = new Gtk.Button({ label: 'Go back to page 1' });
+ back_button.connect('clicked', () => {
+ this._pm.visible_child_name = 'page1';
+ });
+ this._page2.add(back_button);
+
+ this._window = new Endless.Window({
+ application: this,
+ border_width: 16,
+ });
+
+ this._pm = this._window.page_manager;
+ this._pm.set_transition_type(Gtk.StackTransitionType.CROSSFADE);
+ this._pm.add(this._webview, { name: 'page1' });
+ this._pm.add(this._page2, { name: 'page2' });
+ this._pm.visible_child_name = 'page1';
+
+ this._window.show_all();
+ },
+
+ vfunc_dbus_unregister: function (connection, object_path) {
+ this.parent(connection, object_path);
+ this._webhelper.unregister();
+ },
+
+ // WEB ACTIONS
+
+ // dict['name'] is the name of the page to move to
+ moveToPage: function (dict) {
+ this._pm.visible_child_name = dict['name'];
+ },
+
+ // dict['msg'] is the message to display
+ showMessageFromParameter: function (dict) {
+ let dialog = new Gtk.MessageDialog({
+ buttons: Gtk.ButtonsType.CLOSE,
+ message_type: Gtk.MessageType.INFO,
+ text: dict['msg'],
+ transient_for: this._window,
+ });
+ dialog.run();
+ dialog.destroy();
+ },
+});
+
+let app = new TestApplication({
+ application_id: TEST_APPLICATION_ID,
+});
+app.run(ARGV);
diff --git a/test/webhelper/testTranslate2.js b/test/webhelper/testTranslate2.js
new file mode 100644
index 0000000..b34c4cf
--- /dev/null
+++ b/test/webhelper/testTranslate2.js
@@ -0,0 +1,178 @@
+// Copyright 2015 Endless Mobile, Inc.
+
+const Gio = imports.gi.Gio;
+const Gtk = imports.gi.Gtk;
+const Lang = imports.lang;
+const Mainloop = imports.mainloop;
+const WebHelper2 = imports.webhelper2;
+const WebKit2 = imports.gi.WebKit2;
+
+const WELL_KNOWN_NAME = 'com.endlessm.WebHelper.testTranslate2';
+
+Gtk.init(null);
+
+describe('WebHelper2 translator', function () {
+ let webhelper, owner_id, connection;
+
+ beforeAll(function (done) {
+ owner_id = Gio.DBus.own_name(Gio.BusType.SESSION, WELL_KNOWN_NAME,
+ Gio.BusNameOwnerFlags.NONE,
+ null, // bus acquired
+ (con, name) => { // name acquired
+ connection = con;
+ done();
+ },
+ null); // name lost
+ });
+
+ afterAll(function () {
+ Gio.DBus.unown_name(owner_id);
+ });
+
+ beforeEach(function () {
+ webhelper = new WebHelper2.WebHelper({
+ well_known_name: WELL_KNOWN_NAME,
+ connection: connection,
+ });
+ });
+
+ afterEach(function () {
+ webhelper.unregister();
+ });
+
+ it('complains about a bad gettext function', function () {
+ expect(function () {
+ webhelper.set_gettext('I am not a function');
+ }).toThrow();
+ });
+
+ it('gets and sets the gettext function', function () {
+ let translation_function = (s) => s;
+ webhelper.set_gettext(translation_function);
+ expect(webhelper.get_gettext()).toBe(translation_function);
+ });
+
+ it('has a null gettext function by default', function () {
+ expect(webhelper.get_gettext()).toBeNull();
+ });
+
+ it('can remove the gettext function by setting null', function () {
+ webhelper.set_gettext((s) => s);
+ expect(webhelper.get_gettext()).not.toBeNull();
+ webhelper.set_gettext(null);
+ expect(webhelper.get_gettext()).toBeNull();
+ });
+
+ it('complains about a bad ngettext function', function () {
+ expect(function () {
+ webhelper.set_ngettext('I am not a function');
+ }).toThrow();
+ });
+
+ it('gets and sets the ngettext function', function () {
+ let translation_function = (s, p, n) => n == 1 ? s : p;
+ webhelper.set_ngettext(translation_function);
+ expect(webhelper.get_ngettext()).toBe(translation_function);
+ });
+
+ it('has a null ngettext function by default', function () {
+ expect(webhelper.get_ngettext()).toBeNull();
+ });
+
+ it('can remove the ngettext function by setting null', function () {
+ webhelper.set_ngettext((s, p, n) => n == 1 ? s : p);
+ expect(webhelper.get_ngettext()).not.toBeNull();
+ webhelper.set_ngettext(null);
+ expect(webhelper.get_ngettext()).toBeNull();
+ });
+
+ describe('translating a page', function () {
+ let webview;
+
+ function run_loop() {
+ webview.connect('load-changed', (webview, event) => {
+ if (event === WebKit2.LoadEvent.FINISHED) {
+ webhelper.translate_html(webview, null, (obj, res) => {
+ webhelper.translate_html_finish(res);
+ Mainloop.quit('webhelper2');
+ });
+ }
+ });
+ webview.load_html('<html><body><p name="translatable">Translate Me</p></body></html>',
+ null);
+ Mainloop.run('webhelper2');
+ }
+
+ beforeEach(function () {
+ webview = new WebKit2.WebView();
+ });
+
+ it('translates a string', function () {
+ let gettext_spy = jasmine.createSpy('gettext_spy').and.callFake((s) => s);
+ webhelper.set_gettext(gettext_spy);
+ run_loop();
+ expect(gettext_spy).toHaveBeenCalledWith('Translate Me');
+ });
+
+ // The following test is disabled because GJS cannot catch exceptions
+ // across FFI interfaces (e.g. in GObject callbacks.)
+ xit('complains about a gettext function not being set', function () {
+ expect(function () {
+ run_loop();
+ }).toThrow();
+ });
+
+ it('can cancel the translation operation', function (done) {
+ webhelper.set_gettext((s) => s);
+ webview.connect('load-changed', (webview, event) => {
+ if (event === WebKit2.LoadEvent.FINISHED) {
+ let cancellable = new Gio.Cancellable();
+ cancellable.cancel();
+ webhelper.translate_html(webview, cancellable, (obj, res) => {
+ expect(function () {
+ webhelper.translate_html_finish(res);
+ }).toThrow();
+ done();
+ });
+ }
+ });
+ webview.load_html('<html><body></body></html>', null);
+ });
+ });
+
+ describe('used from client-side Javascript', function () {
+ let webview;
+
+ beforeEach(function () {
+ webview = new WebKit2.WebView();
+ });
+
+ function load_script(view, script) {
+ view.load_html('<html><body><script type="text/javascript">' +
+ script + '</script></body></html>', null);
+ Mainloop.run('webhelper2');
+ }
+
+ it('translates a string with gettext()', function (done) {
+ let gettext_spy = jasmine.createSpy('gettext_spy').and.callFake((s) => {
+ Mainloop.quit('webhelper2');
+ return s;
+ });
+ webhelper.set_gettext(gettext_spy);
+ load_script(webview, 'gettext("Translate Me");');
+ expect(gettext_spy).toHaveBeenCalledWith('Translate Me');
+ done();
+ });
+
+ it('translates a string with ngettext()', function (done) {
+ let ngettext_spy = jasmine.createSpy('ngettext_spy').and.callFake((s, p, n) => {
+ Mainloop.quit('webhelper2');
+ return n == 1 ? s : p;
+ });
+ webhelper.set_ngettext(ngettext_spy);
+ load_script(webview, 'ngettext("File", "Files", 3);');
+ expect(ngettext_spy).toHaveBeenCalledWith('File', 'Files', 3);
+ done();
+ });
+ });
+});
diff --git a/test/webhelper/testWebActions2.js b/test/webhelper/testWebActions2.js
new file mode 100644
index 0000000..5af5e2b
--- /dev/null
+++ b/test/webhelper/testWebActions2.js
@@ -0,0 +1,121 @@
+const Gio = imports.gi.Gio;
+const Gtk = imports.gi.Gtk;
+const Lang = imports.lang;
+const Mainloop = imports.mainloop;
+const WebHelper2 = imports.webhelper2;
+const WebKit2 = imports.gi.WebKit2;
+
+const WELL_KNOWN_NAME = 'com.endlessm.WebHelper.testWebActions2';
+
+Gtk.init(null);
+
+describe('WebKit2 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) => { // 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();
+ });
+});