[geary/wip/728002-webkit2: 96/105] Make ClientWebView and derived classes unit-testable.
- From: Michael Gratton <mjog src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [geary/wip/728002-webkit2: 96/105] Make ClientWebView and derived classes unit-testable.
- Date: Sun, 1 Jan 2017 23:57:55 +0000 (UTC)
commit e0d9ff8a1746c9822706e929c524acd0a8c52d5f
Author: Michael James Gratton <mike vee net>
Date: Sat Dec 31 14:43:35 2016 +1100
Make ClientWebView and derived classes unit-testable.
* src/client/components/client-web-view.vala (ClientWebView): Don't
require an instance of GearyApplication to be passed in to resource
loading, use new GioUtil methods to load the resources instead. Update
subclasses and call sites. Add a static init method for initialising
the WebKit.WebContext move code from GearyController here.
* src/client/application/geary-application.vala (GearyApplication):
Reimplement ::create_builder and ::read_resource using new GioUtil,
deprecate them. Merge ::load_ui_resource_for_manager into
::load_ui_resource since that's its only use, deprecate it.
* src/client/util/util-gio.vala: New util methods for loading GResources,
independent of the app.
* src/CMakeLists.txt: Add new util source.
UNit tetsing
src/CMakeLists.txt | 1 +
src/client/application/geary-application.vala | 52 +++++++++----------
src/client/application/geary-controller.vala | 35 ++++---------
src/client/components/client-web-view.vala | 50 ++++++++++++++-----
src/client/composer/composer-web-view.vala | 7 ++-
.../conversation-viewer/conversation-web-view.vala | 17 ++++---
src/client/util/util-gio.vala | 48 ++++++++++++++++++
7 files changed, 135 insertions(+), 75 deletions(-)
---
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4430a0a..cc65d7a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -399,6 +399,7 @@ client/sidebar/sidebar-tree.vala
client/util/util-date.vala
client/util/util-email.vala
client/util/util-files.vala
+client/util/util-gio.vala
client/util/util-gravatar.vala
client/util/util-gtk.vala
client/util/util-international.vala
diff --git a/src/client/application/geary-application.vala b/src/client/application/geary-application.vala
index ff92f3d..89ea07c 100644
--- a/src/client/application/geary-application.vala
+++ b/src/client/application/geary-application.vala
@@ -337,43 +337,41 @@ public class GearyApplication : Gtk.Application {
return exec_dir.has_prefix(prefix_dir) ? prefix_dir : null;
}
-
- // Creates a GTK builder given the name of a GResource.
+
+ /**
+ * Creates a GTK builder given the name of a GResource.
+ *
+ * @deprecated Use {@link GioUtil.create_builder} instead.
+ */
+ [Version (deprecated = true)]
public Gtk.Builder create_builder(string name) {
- Gtk.Builder builder = new Gtk.Builder();
- try {
- builder.add_from_resource("/org/gnome/Geary/" + name);
- } catch(GLib.Error error) {
- warning("Unable to create Gtk.Builder: %s".printf(error.message));
- }
-
- return builder;
+ return GioUtil.create_builder(name);
}
+ /**
+ * Loads a GResource as a string.
+ *
+ * @deprecated Use {@link GioUtil.read_resource} instead.
+ */
+ [Version (deprecated = true)]
public string read_resource(string name) throws Error {
- InputStream input_stream = resources_open_stream(
- "/org/gnome/Geary/" + name,
- ResourceLookupFlags.NONE
- );
- DataInputStream data_stream = new DataInputStream(input_stream);
- size_t length;
- return data_stream.read_upto("\0", 1, out length);
+ return GioUtil.read_resource(name);
}
- // Loads a UI GResource into the specified UI manager.
- public void load_ui_resource_for_manager(Gtk.UIManager ui, string name) {
+ /**
+ * Loads a UI GResource into the UI manager.
+ */
+ [Version (deprecated = true)]
+ public void load_ui_resource(string name) {
try {
- ui.add_ui_from_resource("/org/gnome/Geary/" + name);
+ this.ui_manager.add_ui_from_resource("/org/gnome/Geary/" + name);
} catch(GLib.Error error) {
- warning("Unable to create Gtk.UIManager: %s".printf(error.message));
+ critical("Unable to load \"%s\" for Gtk.UIManager: %s".printf(
+ name, error.message
+ ));
}
}
-
- // Loads a UI GResource into the UI manager.
- public void load_ui_resource(string name) {
- load_ui_resource_for_manager(ui_manager, name);
- }
-
+
// This call will fire "exiting" only if it's not already been fired.
public void exit(int exitcode = 0) {
if (exiting_fired)
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index d221c47..be666a3 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -183,35 +183,22 @@ public class GearyController : Geary.BaseObject {
// Listen for attempts to close the application.
this.application.exiting.connect(on_application_exiting);
-
+
// Create DB upgrade dialog.
upgrade_dialog = new UpgradeDialog();
upgrade_dialog.notify[UpgradeDialog.PROP_VISIBLE_NAME].connect(display_main_window_if_ready);
- // Initialise global WebKit settings
- WebKit.WebContext context = WebKit.WebContext.get_default();
- context.set_process_model(WebKit.ProcessModel.SHARED_SECONDARY_PROCESS);
- context.set_cache_model(WebKit.CacheModel.DOCUMENT_BROWSER);
- context.register_uri_scheme("cid", (req) => {
- ClientWebView? view = req.get_web_view() as ClientWebView;
- if (view != null) {
- view.handle_cid_request(req);
- }
- });
- context.initialize_web_extensions.connect((context) => {
- context.set_web_extensions_directory(
- this.application.get_web_extensions_dir().get_path()
- );
- context.set_web_extensions_initialization_user_data(
- new Variant.boolean(Args.log_debug)
- );
- });
-
- // Load web view resources
+ // Initialise WebKit and WebViews
+ ClientWebView.init_web_context(
+ this.application.get_web_extensions_dir(),
+ Args.log_debug
+ );
try {
- ClientWebView.load_scripts(this.application);
- ComposerWebView.load_resources(this.application);
- ConversationWebView.load_resources(this.application);
+ ClientWebView.load_scripts();
+ ComposerWebView.load_resources();
+ ConversationWebView.load_resources(
+ this.application.get_user_config_directory()
+ );
} catch (Error err) {
error("Error loading web resources: %s", err.message);
}
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index 08e6cdc..fc10f41 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -24,22 +24,48 @@ public class ClientWebView : WebKit.WebView {
private static WebKit.UserScript? script = null;
private static WebKit.UserScript? allow_remote_images = null;
- public static void load_scripts(GearyApplication app)
+ /**
+ * Initialises WebKit.WebContext for use by the client.
+ */
+ public static void init_web_context(File web_extension_dir,
+ bool enable_logging) {
+ WebKit.WebContext context = WebKit.WebContext.get_default();
+ context.set_process_model(WebKit.ProcessModel.SHARED_SECONDARY_PROCESS);
+ context.set_cache_model(WebKit.CacheModel.DOCUMENT_BROWSER);
+ context.register_uri_scheme("cid", (req) => {
+ ClientWebView? view = req.get_web_view() as ClientWebView;
+ if (view != null) {
+ view.handle_cid_request(req);
+ }
+ });
+ context.initialize_web_extensions.connect((context) => {
+ context.set_web_extensions_directory(
+ web_extension_dir.get_path()
+ );
+ context.set_web_extensions_initialization_user_data(
+ new Variant.boolean(enable_logging)
+ );
+ });
+ }
+
+ /**
+ * Loads static resources used by ClientWebView.
+ */
+ public static void load_scripts()
throws Error {
ClientWebView.script = load_app_script(
- app, "client-web-view.js"
+ "client-web-view.js"
);
ClientWebView.allow_remote_images = load_app_script(
- app, "client-web-view-allow-remote-images.js"
+ "client-web-view-allow-remote-images.js"
);
}
/** Loads an application-specific WebKit stylesheet. */
- protected static WebKit.UserStyleSheet load_app_stylesheet(GearyApplication app,
- string name)
+ protected static WebKit.UserStyleSheet load_app_stylesheet(string name)
throws Error {
return new WebKit.UserStyleSheet(
- app.read_resource(name),
+ GioUtil.read_resource(name),
WebKit.UserContentInjectedFrames.TOP_FRAME,
WebKit.UserStyleLevel.USER,
null,
@@ -48,13 +74,10 @@ public class ClientWebView : WebKit.WebView {
}
/** Loads a user stylesheet, if any. */
- protected static WebKit.UserStyleSheet? load_user_stylesheet(GearyApplication app,
- string name) {
- File stylesheet = app.get_user_config_directory().get_child(name);
+ protected static WebKit.UserStyleSheet? load_user_stylesheet(File name) {
WebKit.UserStyleSheet? user_stylesheet = null;
try {
- Geary.Memory.FileBuffer buf =
- new Geary.Memory.FileBuffer(stylesheet, true);
+ Geary.Memory.FileBuffer buf = new Geary.Memory.FileBuffer(name, true);
user_stylesheet = new WebKit.UserStyleSheet(
buf.get_valid_utf8(),
WebKit.UserContentInjectedFrames.ALL_FRAMES,
@@ -71,11 +94,10 @@ public class ClientWebView : WebKit.WebView {
}
/** Loads an application-specific WebKit JavaScript script. */
- protected static WebKit.UserScript load_app_script(GearyApplication app,
- string name)
+ protected static WebKit.UserScript load_app_script(string name)
throws Error {
return new WebKit.UserScript(
- app.read_resource(name),
+ GioUtil.read_resource(name),
WebKit.UserContentInjectedFrames.TOP_FRAME,
WebKit.UserScriptInjectionTime.START,
null,
diff --git a/src/client/composer/composer-web-view.vala b/src/client/composer/composer-web-view.vala
index deaabbc..6b19efa 100644
--- a/src/client/composer/composer-web-view.vala
+++ b/src/client/composer/composer-web-view.vala
@@ -61,10 +61,11 @@ public class ComposerWebView : ClientWebView {
private static WebKit.UserScript? app_script = null;
- public static void load_resources(GearyApplication app)
+ public static void load_resources()
throws Error {
- ComposerWebView.app_script =
- ClientWebView.load_app_script(app, "composer-web-view.js");
+ ComposerWebView.app_script = ClientWebView.load_app_script(
+ "composer-web-view.js"
+ );
}
private bool is_shift_down = false;
diff --git a/src/client/conversation-viewer/conversation-web-view.vala
b/src/client/conversation-viewer/conversation-web-view.vala
index 91743f4..e9df54c 100644
--- a/src/client/conversation-viewer/conversation-web-view.vala
+++ b/src/client/conversation-viewer/conversation-web-view.vala
@@ -14,14 +14,17 @@ public class ConversationWebView : ClientWebView {
private static WebKit.UserStyleSheet? app_stylesheet = null;
private static WebKit.UserScript? app_script = null;
- public static void load_resources(GearyApplication app)
+ public static void load_resources(File user_dir)
throws Error {
- ConversationWebView.app_script =
- ClientWebView.load_app_script(app, "conversation-web-view.js");
- ConversationWebView.app_stylesheet =
- ClientWebView.load_app_stylesheet(app, "conversation-web-view.css");
- ConversationWebView.user_stylesheet =
- ClientWebView.load_user_stylesheet(app, "user-message.css");
+ ConversationWebView.app_script = ClientWebView.load_app_script(
+ "conversation-web-view.js"
+ );
+ ConversationWebView.app_stylesheet = ClientWebView.load_app_stylesheet(
+ "conversation-web-view.css"
+ );
+ ConversationWebView.user_stylesheet = ClientWebView.load_user_stylesheet(
+ user_dir.get_child("user-message.css")
+ );
}
diff --git a/src/client/util/util-gio.vala b/src/client/util/util-gio.vala
new file mode 100644
index 0000000..0c46d03
--- /dev/null
+++ b/src/client/util/util-gio.vala
@@ -0,0 +1,48 @@
+/* Copyright 2016 Software Freedom Conservancy Inc.
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+/**
+ * Utility functions for GIO objects.
+ */
+namespace GioUtil {
+
+ public const string GEARY_RESOURCE_PREFIX = "/org/gnome/Geary/";
+
+ /**
+ * Creates a GTK builder given the name of a GResource.
+ *
+ * The given `name` will automatically have
+ * `GEARY_RESOURCE_PREFIX` pre-pended to it.
+ */
+ public Gtk.Builder create_builder(string name) {
+ Gtk.Builder builder = new Gtk.Builder();
+ try {
+ builder.add_from_resource(GEARY_RESOURCE_PREFIX + name);
+ } catch(GLib.Error error) {
+ critical("Unable load GResource \"%s\" for Gtk.Builder: %s".printf(
+ name, error.message
+ ));
+ }
+ return builder;
+ }
+
+ /**
+ * Loads a GResource file as a string.
+ *
+ * The given `name` will automatically have
+ * `GEARY_RESOURCE_PREFIX` pre-pended to it.
+ */
+ public string read_resource(string name) throws Error {
+ InputStream input_stream = resources_open_stream(
+ GEARY_RESOURCE_PREFIX + name,
+ ResourceLookupFlags.NONE
+ );
+ DataInputStream data_stream = new DataInputStream(input_stream);
+ size_t length;
+ return data_stream.read_upto("\0", 1, out length);
+ }
+
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]