aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorWilliam Hua <william.hua@canonical.com>2014-01-28 17:15:29 -0500
committerWilliam Hua <william.hua@canonical.com>2014-01-28 17:15:29 -0500
commitf402be78974708125f5e2873a295b4af4b4043bd (patch)
treeb4346377bd9565179537d9e3063d9b463e15e76c /tests
parent335ff379c254ba3b903d35ba9c89ac23f729311f (diff)
downloadayatana-indicator-keyboard-f402be78974708125f5e2873a295b4af4b4043bd.tar.gz
ayatana-indicator-keyboard-f402be78974708125f5e2873a295b4af4b4043bd.tar.bz2
ayatana-indicator-keyboard-f402be78974708125f5e2873a295b4af4b4043bd.zip
Add fixture class from libunity.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/fixture.vala125
2 files changed, 127 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index da083318..f3be1539 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -8,7 +8,8 @@ AM_LDFLAGS = -lm
AM_VALAFLAGS = --metadatadir $(top_srcdir)/deps \
--vapidir $(top_srcdir)/deps
-indicator_keyboard_tests_SOURCES = main.vala \
+indicator_keyboard_tests_SOURCES = main.vala \
+ fixture.vala \
config.vala
indicator_keyboard_tests_VALAFLAGS = $(AM_VALAFLAGS) \
--pkg gio-2.0
diff --git a/tests/fixture.vala b/tests/fixture.vala
new file mode 100644
index 00000000..7adc434f
--- /dev/null
+++ b/tests/fixture.vala
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2014 Canonical Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by Michal Hruby <michal.hruby@canonical.com>
+ *
+ * This file is taken from libunity.
+ */
+
+/* A bit of magic to get proper-ish fixture support */
+public interface Fixture : Object
+{
+ class DelegateWrapper
+ {
+ TestDataFunc func;
+ public DelegateWrapper (owned TestDataFunc f) { func = (owned) f; }
+ }
+
+ public virtual void setup () {}
+ public virtual void teardown () {}
+
+ [CCode (has_target = false)]
+ public delegate void Callback<T> (T ptr);
+
+ private static List<DelegateWrapper> _tests;
+
+ public static unowned TestDataFunc create<F> (Callback<void*> cb)
+ requires (typeof (F).is_a (typeof (Fixture)))
+ {
+ TestDataFunc functor = () =>
+ {
+ var type = typeof (F);
+ var instance = Object.new (type) as Fixture;
+ instance.setup ();
+ cb (instance);
+ instance.teardown ();
+ };
+ unowned TestDataFunc copy = functor;
+ _tests.append (new DelegateWrapper ((owned) functor));
+ return copy;
+ }
+ public static unowned TestDataFunc create_static<F> (Callback<F> cb)
+ {
+ return create<F> ((Callback<void*>) cb);
+ }
+}
+
+public static bool run_with_timeout (MainLoop ml, uint timeout_ms = 5000)
+{
+ bool timeout_reached = false;
+ var t_id = Timeout.add (timeout_ms, () =>
+ {
+ timeout_reached = true;
+ debug ("Timeout reached");
+ ml.quit ();
+ return false;
+ });
+
+ ml.run ();
+
+ if (!timeout_reached) Source.remove (t_id);
+
+ return !timeout_reached;
+}
+
+/* calling this will ensure that the object was destroyed, but note that
+ * it needs to be called with the (owned) modifier */
+public static void ensure_destruction (owned Object obj)
+{
+ var ml = new MainLoop ();
+ bool destroyed = false;
+ obj.weak_ref (() => { destroyed = true; ml.quit (); });
+
+ obj = null;
+ if (!destroyed)
+ {
+ // wait a bit if there were async operations
+ assert (run_with_timeout (ml));
+ }
+}
+
+public class ErrorHandler
+{
+ public ErrorHandler ()
+ {
+ GLib.Test.log_set_fatal_handler (handle_fatal_func);
+ }
+
+ private bool handle_fatal_func (string? log_domain, LogLevelFlags flags,
+ string message)
+ {
+ return false;
+ }
+
+ private uint[] handler_ids;
+ private GenericArray<string?> handler_domains;
+
+ public void ignore_message (string? domain, LogLevelFlags flags)
+ {
+ handler_ids += Log.set_handler (domain, flags | LogLevelFlags.FLAG_FATAL,
+ () => {});
+ if (handler_domains == null)
+ {
+ handler_domains = new GenericArray<string?> ();
+ }
+ handler_domains.add (domain);
+ }
+
+ ~ErrorHandler ()
+ {
+ for(uint i = 0; i < handler_ids.length; i++)
+ Log.remove_handler (handler_domains[i], handler_ids[i]);
+ }
+}