[geary/bug/776421-make-client-unit-testable: 8/8] Add simple/demo client unit test using GSettings.



commit 50120c67ff20bbac79091eb2c36e7ea6c6c49bc0
Author: Michael James Gratton <mike vee net>
Date:   Mon Dec 26 13:21:58 2016 +1030

    Add simple/demo client unit test using GSettings.
    
    * test/client/application/geary-configuration-test.vala: New unit test
      for Configuration class.
    
    * test/main.vala: Add new unit test to the client suite, ensure the
      memory GSettings backend is used as the default so we get default
      setting values, and never save any changes made.
    
    * test/CMakeLists.txt: Add new unit test source.
    
    * src/client/application/geary-config.vala: Tidy up code a bit to adhere
      to code conventions.

 src/client/application/geary-config.vala           |   32 +++++++++++-------
 test/CMakeLists.txt                                |    2 +
 .../application/geary-configuration-test.vala      |   35 ++++++++++++++++++++
 test/main.vala                                     |   10 ++++++
 4 files changed, 66 insertions(+), 13 deletions(-)
---
diff --git a/src/client/application/geary-config.vala b/src/client/application/geary-config.vala
index 718c72a..8df13d8 100644
--- a/src/client/application/geary-config.vala
+++ b/src/client/application/geary-config.vala
@@ -4,8 +4,11 @@
  * (version 2.1 or later).  See the COPYING file in this distribution.
  */
 
-// Wrapper class for GSettings.
+/**
+ * Provides convenience properties to current Geary GSettings values.
+ */
 public class Configuration {
+
     public const string WINDOW_WIDTH_KEY = "window-width";
     public const string WINDOW_HEIGHT_KEY = "window-height";
     public const string WINDOW_MAXIMIZE_KEY = "window-maximize";
@@ -27,13 +30,27 @@ public class Configuration {
     public const string SEARCH_STRATEGY_KEY = "search-strategy";
     public const string CONVERSATION_VIEWER_ZOOM_KEY = "conversation-viewer-zoom";
 
+
     public enum DesktopEnvironment {
         UNKNOWN = 0,
         UNITY;
     }
 
+
+    // is_installed: set to true if installed, else false.
+    // schema_dir: MUST be set if not installed. Directory where GSettings schema is located.
+    public static void init(bool is_installed, string? schema_dir = null) {
+        if (!is_installed) {
+            assert(schema_dir != null);
+            // If not installed, set an environment variable pointing to where the GSettings schema
+            // is to be found.
+            GLib.Environment.set_variable("GSETTINGS_SCHEMA_DIR", schema_dir, true);
+        }
+    }
+
+
     public Settings settings { get; private set; }
-    public Settings gnome_interface;
+    public Settings gnome_interface { get; private set; }
 
     public DesktopEnvironment desktop_environment {
         get {
@@ -158,17 +175,6 @@ public class Configuration {
         Migrate.old_app_config(settings);
     }
 
-    // is_installed: set to true if installed, else false.
-    // schema_dir: MUST be set if not installed. Directory where GSettings schema is located.
-    public static void init(bool is_installed, string? schema_dir = null) {
-        if (!is_installed) {
-            assert(schema_dir != null);
-            // If not installed, set an environment variable pointing to where the GSettings schema
-            // is to be found.
-            GLib.Environment.set_variable("GSETTINGS_SCHEMA_DIR", schema_dir, true);
-        }
-    }
-    
     public void bind(string key, Object object, string property,
         SettingsBindFlags flags = GLib.SettingsBindFlags.DEFAULT) {
         settings.bind(key, object, property, flags);
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 4122212..a90dc6a 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -11,6 +11,8 @@ set(TEST_SRC
   engine/rfc822-message-data-test.vala
   engine/rfc822-utils-test.vala
   engine/util-html-test.vala
+
+  client/application/geary-configuration-test.vala
 )
 
 # Vala
diff --git a/test/client/application/geary-configuration-test.vala 
b/test/client/application/geary-configuration-test.vala
new file mode 100644
index 0000000..4ce33a2
--- /dev/null
+++ b/test/client/application/geary-configuration-test.vala
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2016 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+class ConfigurationTest : Gee.TestCase {
+
+    private Configuration test_config = null;
+
+    public ConfigurationTest() {
+        base("ConfigurationTest");
+        add_test("desktop_environment", desktop_environment);
+    }
+
+    public override void set_up() {
+        Environment.unset_variable("XDG_CURRENT_DESKTOP");
+        this.test_config = new Configuration(GearyApplication.APP_ID);
+    }
+
+    public void desktop_environment() {
+        assert(this.test_config.desktop_environment ==
+               Configuration.DesktopEnvironment.UNKNOWN);
+
+        Environment.set_variable("XDG_CURRENT_DESKTOP", "BLARG", true);
+        assert(this.test_config.desktop_environment ==
+               Configuration.DesktopEnvironment.UNKNOWN);
+
+        Environment.set_variable("XDG_CURRENT_DESKTOP", "Unity", true);
+        assert(this.test_config.desktop_environment ==
+               Configuration.DesktopEnvironment.UNITY);
+    }
+
+}
diff --git a/test/main.vala b/test/main.vala
index 34e9b18..ff99b02 100644
--- a/test/main.vala
+++ b/test/main.vala
@@ -7,6 +7,14 @@
 
 int main(string[] args) {
     /*
+     * Set env vars right up front to avoid weird bugs
+     */
+
+    // Use the memory GSettings DB so we a) always start with default
+    // values, and b) don't persist any changes made during a test
+    Environment.set_variable("GSETTINGS_BACKEND", "memory", true);
+
+    /*
      * Initialise all the things.
      */
 
@@ -29,6 +37,8 @@ int main(string[] args) {
 
     TestSuite client = new TestSuite("client");
 
+    client.add_suite(new ConfigurationTest().get_suite());
+
     /*
      * Run the tests
      */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]