From 658cb6d84afcb01fc121a04df0d82bbea22c2c25 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 6 Sep 2013 09:26:02 -0700 Subject: Unit tests for webhelper These are tests for the basic functionality and a few fail cases of webhelper, by no means exhaustive. There are a few tests that cannot execute because I do not currently know of any way to catch exceptions that occur inside GObject callbacks. [endlessm/eos-sdk#290] --- test/Makefile.am | 2 + test/webhelper/testTranslate.js | 72 ++++++++++++++++++++ test/webhelper/testWebActions.js | 140 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 test/webhelper/testTranslate.js create mode 100644 test/webhelper/testWebActions.js (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am index dcf6ee4..e913d57 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -27,5 +27,7 @@ test_smoke_tests_hello_LDADD = $(TEST_LIBS) javascript_tests = \ test/tools/eos-run-test/sanitycheck.js \ + test/webhelper/testTranslate.js \ + test/webhelper/testWebActions.js \ $(NULL) EXTRA_DIST += $(javascript_tests) diff --git a/test/webhelper/testTranslate.js b/test/webhelper/testTranslate.js new file mode 100644 index 0000000..10e389c --- /dev/null +++ b/test/webhelper/testTranslate.js @@ -0,0 +1,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 = '

Translate Me

'; + 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([])); +// } \ No newline at end of file diff --git a/test/webhelper/testWebActions.js b/test/webhelper/testWebActions.js new file mode 100644 index 0000000..585ab00 --- /dev/null +++ b/test/webhelper/testWebActions.js @@ -0,0 +1,140 @@ +const Endless = imports.gi.Endless; +const Gio = imports.gi.Gio; +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(); + this.webview.connect('navigation-policy-decision-requested', + Lang.bind(this, this.web_actions_handler)); + let string = (''); + 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); + + // 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 testWebActionIsCalled() { + let actionWasCalled = false; + app._webActions = { + quitApplication: function() { + actionWasCalled = true; + app.quit(); + } + }; + app.webActionToTest = 'endless://quitApplication'; + app.run([]); + assertTrue(actionWasCalled); +} + +function testWebActionIsCalledWithParameter() { + let actionParameter; + app._webActions = { + getParameterAndQuit: function(dict) { + actionParameter = dict['param']; + app.quit(); + } + }; + app.webActionToTest = 'endless://getParameterAndQuit?param=value'; + app.run([]); + assertEquals('value', actionParameter); +} + +function testWebActionIsCalledWithManyParameters() { + let firstParameter, secondParameter, thirdParameter; + app._webActions = { + getParametersAndQuit: function(dict) { + firstParameter = dict['first']; + secondParameter = dict['second']; + thirdParameter = dict['third']; + app.quit(); + } + }; + app.webActionToTest = 'endless://getParametersAndQuit?first=thefirst&second=thesecond&third=thethird'; + app.run([]); + assertEquals('thefirst', firstParameter); + assertEquals('thesecond', secondParameter); + assertEquals('thethird', thirdParameter); +} + +function testParameterNameIsUriDecoded() { + let expectedParameter = 'päräm💩'; + let parameterWasFound = false; + app._webActions = { + getUriDecodedParameterAndQuit: function(dict) { + parameterWasFound = (expectedParameter in dict); + app.quit(); + } + }; + app.webActionToTest = 'endless://getUriDecodedParameterAndQuit?p%C3%A4r%C3%A4m%F0%9F%92%A9=value'; + app.run([]); + assertTrue(parameterWasFound); +} + +function testParameterValueIsUriDecoded() { + let expectedValue = 'válué💩'; + let actualValue; + app._webActions = { + getUriDecodedValueAndQuit: function(dict) { + actualValue = dict['param']; + app.quit(); + } + }; + app.webActionToTest = 'endless://getUriDecodedValueAndQuit?param=v%C3%A1lu%C3%A9%F0%9F%92%A9'; + app.run([]); + assertEquals(expectedValue, actualValue); +} + +// This is commented out because GJS cannot catch exceptions across FFI +// interfaces (e.g. in GObject callbacks.) +// function testBadActionIsNotCalled() { +// app.webActionToTest = 'endless://nonexistentAction?param=value'; +// assertRaises(function() { app.run([]); }); +// } + +function testWebActionIsCalledWithBlankParameter() { + let parameterWasFound = false; + let parameterValue; + app._webActions = { + getBlankValueAndQuit: function(dict) { + parameterWasFound = ('param' in dict); + if(parameterWasFound) + parameterValue = dict['param']; + app.quit(); + } + }; + app.webActionToTest = 'endless://getBlankValueAndQuit?param='; + app.run([]); + assertTrue(parameterWasFound); + assertNotUndefined(parameterValue); + assertEquals('', parameterValue); +} -- cgit v1.2.3