summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorP. F. Chimento <philip.chimento@gmail.com>2013-07-04 23:42:53 -0700
committerP. F. Chimento <philip.chimento@gmail.com>2013-07-04 23:42:53 -0700
commit2632895f60791dee126bd7898cec748c47a8d72c (patch)
tree444fc67552925334e58018ed41f9f6e6a21bae5d
parentd0ff3c086dddcd6d40fee0cf3c297798666bc418 (diff)
parentd401dc97e1c255078e47348652e560516709ae08 (diff)
Merge pull request #130 from endlessm/issues/113
#113 Convenience library for HTML/GTK communication
-rw-r--r--test/smoke-tests/WebHelper.js86
-rw-r--r--test/smoke-tests/webview.js112
-rw-r--r--test/smoke-tests/webview/first_page.html54
3 files changed, 252 insertions, 0 deletions
diff --git a/test/smoke-tests/WebHelper.js b/test/smoke-tests/WebHelper.js
new file mode 100644
index 0000000..8b78931
--- /dev/null
+++ b/test/smoke-tests/WebHelper.js
@@ -0,0 +1,86 @@
+const Endless = imports.gi.Endless;
+const GLib = imports.gi.GLib;
+const Gtk = imports.gi.Gtk;
+const WebKit = imports.gi.WebKit;
+
+const EOS_URI_SCHEME = 'endless://';
+
+
+const Application = new Lang.Class({
+ Name: 'WebApplication',
+ Extends: Endless.Application,
+
+// Set of actions that may be invoked from a WebView.
+// Declare them as function(dict), and use links with the format
+// "endless://actionName?parameter=value"
+
+ _webActions: { },
+
+// This callback does the translation from URI to action
+// this._webview.connect('navigation-policy-decision-requested',
+// Lang.bind(this, this._webHelper.onNavigationRequested));
+
+ _onNavigationRequested : function(web_view, frame, request,
+ navigation_action, policy_decision,
+ user_data) {
+ let uri = request.get_uri();
+
+ if(uri.indexOf(EOS_URI_SCHEME) == 0) {
+ // get the name and parameters for the desired function
+ let f_call = uri.substring(EOS_URI_SCHEME.length, uri.length).split('?');
+ var function_name = f_call[0];
+ var parameters = {};
+
+ if(f_call[1]) {
+ // there are parameters
+ let params = f_call[1].split('&');
+ params.forEach(function(entry) {
+ let param = entry.split('=');
+
+ if(param.length == 2) {
+ param[0] = decodeURIComponent(param[0]);
+ param[1] = decodeURIComponent(param[1]);
+ // and now we add it...
+ parameters[param[0]] = param[1];
+ }
+ });
+ }
+
+ if(this._webActions[function_name])
+ Lang.bind(this, this._webActions[function_name])(parameters);
+ else
+ print('Unknown function '+function_name);
+
+ policy_decision.ignore();
+ return true;
+ } else {
+ // this is a regular URL, just navigate there
+ return false;
+ }
+ },
+
+// convenience functions
+
+ _getElementById: function(webview, id) {
+ // WebKit.DOMDocument
+ let dom = webview.get_dom_document();
+
+ // WebKit.DOMElement
+ return dom.get_element_by_id(id);
+ },
+
+ _translateHTML: function(webview, lang) {
+ let dom = webview.get_dom_document();
+
+ // WebKit.DOMNodeList
+ let translatable = dom.get_elements_by_name('translatable');
+
+ for (var i = 0 ; i < translatable.get_length() ; i++) {
+ // WebKit.DOMNode
+ let element = translatable.item(i);
+
+ // TODO here is where we would do the translation
+ element.inner_html = '<i>' + element.inner_text + '</i>';
+ }
+ }
+});
diff --git a/test/smoke-tests/webview.js b/test/smoke-tests/webview.js
new file mode 100644
index 0000000..9b84728
--- /dev/null
+++ b/test/smoke-tests/webview.js
@@ -0,0 +1,112 @@
+//Copyright 2013 Endless Mobile, Inc.
+
+const Lang = imports.lang;
+const Endless = imports.gi.Endless;
+const GLib = imports.gi.GLib;
+const Gtk = imports.gi.Gtk;
+const WebKit = imports.gi.WebKit;
+
+// WebHelper.js must be copied somewhere in GJS's imports.searchPath
+const WebHelper = imports.WebHelper;
+
+const TEST_APPLICATION_ID = 'com.endlessm.example.test-webview';
+
+const TestApplication = new Lang.Class({
+ Name: 'TestApplication',
+ Extends: WebHelper.Application,
+
+ /* *** ACTIONS AVAILABLE FROM THE WEB VIEW *** */
+
+ _webActions: {
+ /* dict['name'] is the name of the page to move to */
+ 'moveToPage': function(dict) {
+ print('move to page '+dict['name']);
+ this._pm.visible_page_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'] });
+ dialog.set_transient_for(this._window);
+ dialog.run();
+ dialog.destroy();
+ },
+
+ /* dict['id'] is the ID of the input field to use */
+ 'showMessageFromInputField': function(dict) {
+ let input = this._getElementById(this._webview, dict['id']);
+
+ // WebKitDOMHTMLInputElement
+ let msg = input.get_value();
+
+ let dialog = new Gtk.MessageDialog({
+ buttons: Gtk.ButtonsType.CLOSE,
+ message_type: Gtk.MessageType.INFO,
+ text: msg });
+ dialog.set_transient_for(this._window);
+ dialog.run();
+ dialog.destroy();
+ },
+
+ /* dict['id'] is the ID of the element to use */
+ 'addStars': function(dict) {
+ let e = this._getElementById(this._webview, dict['id']);
+ e.inner_text += '★ ';
+ }
+ },
+
+ /* *************************** */
+
+ vfunc_startup: function() {
+ this.parent();
+
+ this._webview = new WebKit.WebView();
+
+ let cwd = GLib.get_current_dir();
+ let target = cwd + '/test/smoke-tests/webview/first_page.html';
+ this._webview.load_uri(GLib.filename_to_uri(target, null));
+
+ this._webview.connect('notify::load-status',
+ Lang.bind(this, function (web_view, status) {
+ if (web_view.load_status == WebKit.LoadStatus.FINISHED) {
+ // now we translate to Brazilian Portuguese
+ this._translateHTML (web_view, 'pt_BR');
+ }
+ }));
+
+ this._webview.connect('navigation-policy-decision-requested',
+ Lang.bind(this, this._onNavigationRequested));
+
+ this._page1 = new Gtk.ScrolledWindow();
+ this._page1.add(this._webview);
+
+ this._page2 = new Gtk.Grid();
+ let back_button = new Gtk.Button({label:"Go back to page 1"});
+ back_button.connect('clicked', Lang.bind(this, function() {
+ this._pm.visible_page_name = 'page1';
+ }))
+ this._page2.add(back_button);
+
+ this._pm = new Endless.PageManager();
+ this._pm.set_transition_type(Endless.PageManagerTransitionType.CROSSFADE)
+ this._pm.add(this._page1, { name: 'page1' });
+ this._pm.add(this._page2, { name: 'page2' });
+
+ this._pm.visible_page = this._page1;
+
+ this._window = new Endless.Window({
+ application: this,
+ border_width: 16,
+ page_manager: this._pm
+ });
+
+ this._window.show_all();
+ }
+});
+
+let app = new TestApplication({ application_id: TEST_APPLICATION_ID,
+ flags: 0 });
+app.run(ARGV);
diff --git a/test/smoke-tests/webview/first_page.html b/test/smoke-tests/webview/first_page.html
new file mode 100644
index 0000000..33a6a8f
--- /dev/null
+++ b/test/smoke-tests/webview/first_page.html
@@ -0,0 +1,54 @@
+<html>
+
+<head>
+<title>First page</title>
+
+<style>
+p, form {
+ width: 50%;
+ padding: 1em;
+ background: #FFFFFF;
+}
+
+body {
+ background: #EEEEEE;
+}
+</style>
+</head>
+
+<body>
+
+<h1>First page</h1>
+
+<p>
+<a href="endless://moveToPage?name=page2">Move to page 2</a>
+</p>
+
+<p>
+<a href="endless://showMessageFromParameter?msg=This%20is%20a%20message%20from%20the%20URL%20parameter">Show message from parameter in this URL</a>
+</p>
+
+<form action="endless://showMessageFromParameter">
+<input name="msg" value="I am in a form!"/>
+<input type="submit" value="Show message using a form"/>
+</form>
+
+<p>
+<input id="inputformessage" value="my ID is inputformessage"/>
+<a href="endless://showMessageFromInputField?id=inputformessage">Show message using the &lt;input&gt;'s ID</a>
+</p>
+
+<p>
+<a href="http://wikipedia.org">Regular link to a Web site</a>
+</p>
+
+<p>
+<a href="endless://addStars?id=starspan">I want stars!</a> <span id="starspan" />
+</p>
+
+<p>
+This is text that will be translated: <span name="translatable">Hello, world!</span>
+</p>
+
+</body>
+</html> \ No newline at end of file