[geary/wip/728002-webkit2] Implement milliseconds ctor and tests for Geary.TimeoutManager.



commit b21481c68f3cb8d7a84da3ec4960cbc54bdc84d9
Author: Michael James Gratton <mike vee net>
Date:   Tue Jan 17 16:27:42 2017 +1100

    Implement milliseconds ctor and tests for Geary.TimeoutManager.

 src/engine/util/util-timeout-manager.vala  |   12 ++++++++++++
 test/engine/util-timeout-manager-test.vala |   17 +++++++++++++++++
 2 files changed, 29 insertions(+), 0 deletions(-)
---
diff --git a/src/engine/util/util-timeout-manager.vala b/src/engine/util/util-timeout-manager.vala
index d8c9322..8f493ad 100644
--- a/src/engine/util/util-timeout-manager.vala
+++ b/src/engine/util/util-timeout-manager.vala
@@ -66,6 +66,18 @@ public class Geary.TimeoutManager : BaseObject {
         this.callback = callback;
     }
 
+    /**
+     * Constructs a new timeout with an interval in milliseconds.
+     *
+     * The timeout will be by default not running, and hence needs to be
+     * started by a call to {@link start}.
+     */
+    public TimeoutManager.milliseconds(uint interval, TimeoutFunc callback) {
+        this.use_seconds = false;
+        this.interval = interval;
+        this.callback = callback;
+    }
+
     ~TimeoutManager() {
         reset();
     }
diff --git a/test/engine/util-timeout-manager-test.vala b/test/engine/util-timeout-manager-test.vala
index a087162..ae439e9 100644
--- a/test/engine/util-timeout-manager-test.vala
+++ b/test/engine/util-timeout-manager-test.vala
@@ -10,11 +10,14 @@ class Geary.TimeoutManagerTest : Gee.TestCase {
     // add_seconds seems to vary wildly, so needs a large epsilon
     private const double SECONDS_EPSILON = 1.8;
 
+    private const double MILLISECONDS_EPSILON = 0.1;
+
     public TimeoutManagerTest() {
         base("Geary.TimeoutManagerTest");
         add_test("start_reset", start_reset);
         if (Test.slow()) {
             add_test("test_seconds", test_seconds);
+            add_test("test_milliseconds", test_milliseconds);
             add_test("test_repeat_forever", test_repeat_forever);
         }
     }
@@ -42,6 +45,20 @@ class Geary.TimeoutManagerTest : Gee.TestCase {
         assert_epsilon(timer.elapsed(), 1.0, SECONDS_EPSILON);
     }
 
+    public void test_milliseconds() {
+        Timer timer = new Timer();
+
+        TimeoutManager test = new TimeoutManager.milliseconds(100, () => { timer.stop(); });
+        test.start();
+
+        timer.start();
+        while (test.is_running && timer.elapsed() < 100 + MILLISECONDS_EPSILON) {
+            Gtk.main_iteration();
+        }
+
+        assert_epsilon(timer.elapsed(), 0.1, MILLISECONDS_EPSILON);
+    }
+
     public void test_repeat_forever() {
         Timer timer = new Timer();
         int count = 0;


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