Remove g_type_init() calls.
[platform/upstream/ibus.git] / src / tests / ibus-bus.c
index 8ca7e4a..f3f93bd 100644 (file)
@@ -46,57 +46,6 @@ test_list_engines (void)
     g_list_free (engines);
 }
 
-#if 0
-static gchar *
-get_last_engine_id (const GList *engines)
-{
-    const char *result = NULL;
-    for (; engines; engines = g_list_next (engines)) {
-        IBusEngineDesc *engine_desc = IBUS_ENGINE_DESC (engines->data);
-        g_assert (engine_desc);
-        result = ibus_engine_desc_get_name (engine_desc);
-    }
-    return g_strdup (result);
-}
-
-static void
-test_input_context (void)
-{
-    GList *engines;
-    gchar *active_engine_name = NULL;
-    IBusInputContext *context;
-    IBusEngineDesc *engine_desc;
-    gchar *current_ic;
-
-    engines = ibus_bus_list_active_engines (bus);
-    if (engines == NULL)
-        return;
-    active_engine_name = get_last_engine_id (engines);
-    g_assert (active_engine_name);
-
-    context = ibus_bus_create_input_context (bus, "test");
-    ibus_input_context_set_capabilities (context, IBUS_CAP_FOCUS);
-    ibus_input_context_disable (context);
-    g_assert (ibus_input_context_is_enabled (context) == FALSE);
-    ibus_input_context_enable (context);
-    g_assert (ibus_input_context_is_enabled (context) == TRUE);
-    ibus_input_context_focus_in (context);
-    ibus_input_context_set_engine (context, active_engine_name);
-    current_ic = ibus_bus_current_input_context (bus);
-    g_assert (!strcmp (current_ic, g_dbus_proxy_get_object_path ((GDBusProxy *)context)));
-    engine_desc = ibus_input_context_get_engine (context);
-    g_assert (engine_desc);
-    g_assert (!strcmp (active_engine_name, ibus_engine_desc_get_name(engine_desc)));
-    g_free (current_ic);
-    g_object_unref (engine_desc);
-    g_object_unref (context);
-
-    g_free (active_engine_name);
-    g_list_foreach (engines, (GFunc) g_object_unref, NULL);
-    g_list_free (engines);
-}
-#endif
-
 static void call_next_async_function (void);
 
 static void
@@ -265,7 +214,6 @@ finish_create_input_context_async_sucess (GObject      *source_object,
 
     g_assert (IBUS_IS_INPUT_CONTEXT (context));
     g_object_unref (context);
-    g_debug ("ibus_bus_create_input_context_finish (success): OK");
     if (--create_input_context_count == 0)
         g_main_loop_quit (loop);
 }
@@ -280,16 +228,15 @@ finish_create_input_context_async_failed (GObject      *source_object,
     IBusInputContext *context =
             ibus_bus_create_input_context_async_finish (bus, res, &error);
 
-        g_assert (context == NULL);
-        g_assert (error != NULL);
-        g_error_free (error);
-        g_debug ("ibus_bus_create_input_context_finish (failed): OK");
+    g_assert (context == NULL);
+    g_assert (error != NULL);
+    g_error_free (error);
     if (--create_input_context_count <= 0)
         g_main_loop_quit (loop);
 }
 
 static void
-start_create_input_context_async (void)
+test_create_input_context_async (void)
 {
     GMainLoop *loop = NULL;
     GCancellable *cancellable = NULL;
@@ -354,7 +301,6 @@ start_create_input_context_async (void)
 
     g_main_loop_run (loop);
     g_main_loop_unref (loop);
-    call_next_async_function ();
 }
 
 static void
@@ -552,6 +498,31 @@ start_set_global_engine_async (void)
 }
 
 static void
+finish_preload_engines_async (GObject      *source_object,
+                              GAsyncResult *res,
+                              gpointer      user_data)
+{
+    GError *error = NULL;
+    ibus_bus_preload_engines_async_finish (bus, res, &error);
+    g_debug ("ibus_bus_preload_engines_async_finish: OK");
+    call_next_async_function ();
+}
+
+static void
+start_preload_engines_async (void)
+{
+    const gchar *preload_engines[] = { "xkb:us::eng", NULL };
+
+    ibus_bus_preload_engines_async (
+            bus,
+            preload_engines,
+            -1, /* timeout */
+            NULL, /* cancellable */
+            finish_preload_engines_async,
+            NULL); /* user_data */
+}
+
+static void
 finish_exit_async (GObject *source_object,
                    GAsyncResult *res,
                    gpointer user_data)
@@ -562,6 +533,7 @@ finish_exit_async (GObject *source_object,
                                                   &error);
     g_assert (result);
     g_debug ("ibus_bus_exit_finish: OK");
+    g_usleep (G_USEC_PER_SEC);
     call_next_async_function ();
 }
 
@@ -584,6 +556,42 @@ test_async_apis_finish (gpointer user_data)
 }
 
 static void
+test_get_engines_by_names (void)
+{
+    IBusEngineDesc **engines = NULL;
+    const gchar *names[] = {
+        "xkb:us::eng",
+        "xkb:ca:eng:eng",
+        "xkb:fr::fra",
+        "xkb:jp::jpn",
+        "invalid_engine_name",
+        NULL,
+    };
+
+    engines = ibus_bus_get_engines_by_names (bus, names);
+
+    g_assert(engines != NULL);
+    IBusEngineDesc **p;
+
+    gint i = 0;
+    for (p = engines; *p != NULL; p++) {
+        g_assert (IBUS_IS_ENGINE_DESC (*p));
+        g_assert_cmpstr (names[i], ==, ibus_engine_desc_get_name (*p));
+        i++;
+        g_object_unref (*p);
+        // The ref should be zero, *p is released.
+        g_assert (!IBUS_IS_ENGINE_DESC (*p));
+    }
+
+    // The last engine does not exist.
+    g_assert_cmpint (i, ==, G_N_ELEMENTS(names) - 2);
+
+    g_free (engines);
+
+    engines = NULL;
+}
+
+static void
 test_async_apis (void)
 {
     g_debug ("start");
@@ -601,7 +609,6 @@ call_next_async_function (void)
         start_release_name_async,
         start_add_match_async,
         start_remove_match_async,
-        start_create_input_context_async,
         start_current_input_context_async,
         // FIXME test ibus_bus_register_component_async.
         start_list_engines_async,
@@ -611,6 +618,7 @@ call_next_async_function (void)
         start_is_global_engine_enabled_async,
         start_set_global_engine_async,
         start_get_global_engine_async,
+        start_preload_engines_async,
         start_exit_async,
     };
     static guint index = 0;
@@ -622,22 +630,47 @@ call_next_async_function (void)
         (*async_functions[index++])();
 }
 
+static void
+_bus_connected_cb (IBusBus *bus,
+                   gpointer user_data)
+{
+    g_assert (ibus_bus_is_connected (bus));
+    ibus_quit ();
+}
+
+static void
+test_bus_new_async (void)
+{
+    g_object_unref (bus);
+    bus = ibus_bus_new_async ();
+    g_signal_connect (bus, "connected", G_CALLBACK (_bus_connected_cb), NULL);
+    ibus_main ();
+}
+
 gint
 main (gint    argc,
       gchar **argv)
 {
     gint result;
-    g_type_init ();
-    g_test_init (&argc, &argv, NULL);
     ibus_init ();
+    g_test_init (&argc, &argv, NULL);
     bus = ibus_bus_new ();
+    g_object_unref (bus);
+    bus = ibus_bus_new (); // crosbug.com/17293
 
     g_test_add_func ("/ibus/list-engines", test_list_engines);
     g_test_add_func ("/ibus/list-active-engines", test_list_active_engines);
+    g_test_add_func ("/ibus/create-input-context-async",
+                     test_create_input_context_async);
+    g_test_add_func ("/ibus/get-engines-by-names", test_get_engines_by_names);
     g_test_add_func ("/ibus/async-apis", test_async_apis);
-
-    // FIXME This test does not pass if global engine is not available. Disabling it for now.
-    // g_test_add_func ("/ibus/input_context", test_input_context);
+    g_test_add_func ("/ibus/bus-new-async", test_bus_new_async);
+    g_test_add_func ("/ibus/bus-new-async/list-engines", test_list_engines);
+    g_test_add_func ("/ibus/bus-new-async/list-active-engines", test_list_active_engines);
+    g_test_add_func ("/ibus/bus-new-async/create-input-context-async",
+                     test_create_input_context_async);
+    g_test_add_func ("/ibus/bus-new-async/get-engines-by-names", test_get_engines_by_names);
+    g_test_add_func ("/ibus/bus-new-async/async-apis", test_async_apis);
 
     result = g_test_run ();
     g_object_unref (bus);