[nautilus/sam/tracker-3: 2/2] Add test for starred files feature



commit d3e0b692d7d72c9ffba198aa283ba37b71584369
Author: Sam Thursfield <sam afuera me uk>
Date:   Sun May 3 01:54:04 2020 +0200

    Add test for starred files feature
    
    I'm using to test that my re-implementation of NautilusTagManager
    works as expected.

 test/automated/displayless/test-tag-manager.c      | 121 +++++++++++++++++
 .../automated/displayless/test-utilities-tracker.c | 144 +++++++++++++++++++++
 .../automated/displayless/test-utilities-tracker.h |  12 ++
 3 files changed, 277 insertions(+)
---
diff --git a/test/automated/displayless/test-tag-manager.c b/test/automated/displayless/test-tag-manager.c
new file mode 100644
index 000000000..c6d1a5deb
--- /dev/null
+++ b/test/automated/displayless/test-tag-manager.c
@@ -0,0 +1,121 @@
+#include "test-utilities.h"
+#include "test-utilities-tracker.h"
+
+#include <src/nautilus-file.h>
+#include <src/nautilus-tag-manager.h>
+
+typedef struct {
+    NautilusTagManager *tag_manager;
+    GFile *file_one;
+} TagManagerFixture;
+
+static void
+tag_manager_fixture_set_up (TagManagerFixture *fixture,
+                            gconstpointer      user_data)
+{
+    TrackerFilesProcessedWatcher *watcher;
+    g_autofree gchar *path = NULL;
+
+    fixture->tag_manager = nautilus_tag_manager_get ();
+    nautilus_tag_manager_set_cancellable (fixture->tag_manager, NULL);
+
+    watcher = tracker_files_processed_watcher_new ();
+
+    create_one_file ("stars");
+
+    path = g_build_filename (g_get_tmp_dir (), "stars_first_dir", "stars_first_dir_child", NULL);
+    fixture->file_one = g_file_new_for_path (path);
+
+    tracker_files_processed_watcher_await_file (watcher, fixture->file_one);
+    tracker_files_processed_watcher_free (watcher);
+}
+
+static void
+tag_manager_fixture_tear_down (TagManagerFixture *fixture,
+                               gconstpointer      user_data)
+{
+    g_clear_object (&fixture->tag_manager);
+    g_clear_object (&fixture->file_one);
+};
+
+static void
+starred_changed_cb (NautilusTagManager *tag_manager,
+                    GList              *selection,
+                    gpointer            user_data)
+{
+    GMainLoop *loop = user_data;
+
+    g_main_loop_quit (loop);
+}
+
+static void
+test_star_unstar (TagManagerFixture *fixture,
+             gconstpointer      user_data)
+{
+    GList *selection;
+    gulong signal_id;
+    g_autoptr (GMainLoop) loop = NULL;
+
+    loop = g_main_loop_new (NULL, 0);
+
+    signal_id = g_signal_connect (fixture->tag_manager, "starred-changed", G_CALLBACK (starred_changed_cb), 
loop);
+
+    selection = g_list_prepend (NULL, nautilus_file_get (fixture->file_one));
+
+    /* Nothing is starred to begin with */
+    g_assert (nautilus_tag_manager_get_starred_files (fixture->tag_manager) == NULL);
+
+    /* Star one file and check. */
+    nautilus_tag_manager_star_files (fixture->tag_manager, NULL, selection, NULL, NULL);
+    g_main_loop_run (loop);
+
+    g_assert_cmpint (g_list_length (nautilus_tag_manager_get_starred_files (fixture->tag_manager)), ==, 1);
+
+    /* Unstar the file and check again. */
+    nautilus_tag_manager_unstar_files (fixture->tag_manager, NULL, selection, NULL, NULL);
+    g_main_loop_run (loop);
+
+    g_assert_cmpint (g_list_length (nautilus_tag_manager_get_starred_files (fixture->tag_manager)), ==, 0);
+
+    g_signal_handler_disconnect (fixture->tag_manager, signal_id);
+}
+
+static void
+setup_test_suite (void)
+{
+    g_test_add ("/test-tag-manager/star_unstar", TagManagerFixture, NULL,
+                tag_manager_fixture_set_up, test_star_unstar,
+                tag_manager_fixture_tear_down);
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+    g_autoptr (TrackerSparqlConnection) connection = NULL;
+    g_autoptr (GError) error = NULL;
+    g_autoptr (NautilusFileUndoManager) undo_manager = NULL;
+
+    g_test_init (&argc, &argv, NULL);
+
+    nautilus_ensure_extension_points ();
+
+    undo_manager = nautilus_file_undo_manager_new ();
+
+    /* Make sure to run this test using the 'tracker-sandbox' script
+     * so it doesn't make changes to your real Tracker index.
+     */
+    connection = tracker_sparql_connection_bus_new ("org.freedesktop.Tracker3.Miner.Files", NULL, NULL, 
&error);
+
+    g_assert_no_error (error);
+
+    if (!g_getenv ("TRACKER_INDEXED_TMPDIR")) {
+        g_error ("This test expects to be run inside `tracker sandbox --index-recursive-tmpdir`.");
+    }
+
+    g_setenv ("TMPDIR", g_getenv ("TRACKER_INDEXED_TMPDIR"), TRUE);
+
+    setup_test_suite ();
+
+    return g_test_run ();
+}
diff --git a/test/automated/displayless/test-utilities-tracker.c 
b/test/automated/displayless/test-utilities-tracker.c
new file mode 100644
index 000000000..a2b2ea506
--- /dev/null
+++ b/test/automated/displayless/test-utilities-tracker.c
@@ -0,0 +1,144 @@
+#include "test-utilities-tracker.h"
+
+#include "tracker-miner-proxy.h"
+
+#define AWAIT_FILE_PROCESSED_TIMEOUT 10
+
+struct _TrackerFilesProcessedWatcher {
+    TrackerMiner *proxy;
+    GList *file_list;
+};
+
+static void
+files_processed_cb (TrackerMiner *proxy,
+                    GVariant    *files,
+                    gpointer     user_data)
+{
+    TrackerFilesProcessedWatcher *data = user_data;
+    GVariantIter iter;
+    GVariant *file_info;
+
+    g_debug ("files_processed_cb");
+
+    /* Save all the status info in our structure. Don't do this in real apps,
+     * but it's useful for tests to assert about what the miner did.
+     */
+    g_variant_iter_init (&iter, files);
+    while ((file_info = g_variant_iter_next_value (&iter)))
+    {
+        g_debug ("files_processed_cb: saving %p", file_info);
+
+        data->file_list = g_list_prepend (data->file_list, file_info);
+    }
+}
+
+TrackerFilesProcessedWatcher *
+tracker_files_processed_watcher_new (void)
+{
+    TrackerFilesProcessedWatcher *data;
+    g_autoptr (GError) error = NULL;
+
+    data = g_slice_new0 (TrackerFilesProcessedWatcher);
+    data->proxy = tracker_miner_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                                        G_DBUS_PROXY_FLAGS_NONE,
+                                                        "org.freedesktop.Tracker3.Miner.Files",
+                                                        "/org/freedesktop/Tracker3/Miner/Files",
+                                                        NULL,
+                                                        &error);
+
+    g_assert_no_error (error);
+
+    g_signal_connect (data->proxy, "files-processed", G_CALLBACK (files_processed_cb), data);
+
+    return data;
+}
+
+void
+tracker_files_processed_watcher_free (TrackerFilesProcessedWatcher *data)
+{
+    g_clear_object (&data->proxy);
+    g_list_free_full (data->file_list, (GDestroyNotify) g_variant_unref);
+
+    g_slice_free (TrackerFilesProcessedWatcher, data);
+}
+
+static gboolean await_file_timeout_cb (gpointer user_data)
+{
+    GFile *file = user_data;
+    g_autofree gchar *uri;
+
+    uri = g_file_get_uri (file);
+
+    g_error ("Timeout waiting for %s to be processed.", uri);
+
+    return G_SOURCE_REMOVE;
+}
+
+static gint
+check_file_status_cb (GVariant *status,
+                      GFile    *target_file)
+{
+    gchar *uri, *message;
+    gboolean success;
+    g_autoptr (GFile) file = NULL;
+
+    g_variant_get (status, "(&sb&s)", &uri, &success, &message);
+
+    g_debug ("File processed: %s %i %s", uri, success, message);
+
+    file = g_file_new_for_uri (uri);
+
+    if (g_file_equal (file, target_file))
+    {
+        if (success)
+        {
+            g_info ("Matched successful processing of %s", uri);
+            return 0;
+        }
+        else
+        {
+            g_error ("Error processing %s: %s", uri, message);
+        }
+    }
+
+    return 1;
+}
+
+
+static void
+files_processed_quit_main_loop_cb (TrackerMiner *proxy,
+                                   GVariant    *files,
+                                   gpointer     user_data)
+{
+    GMainLoop *loop = user_data;
+
+    g_main_loop_quit (loop);
+}
+
+void
+tracker_files_processed_watcher_await_file (TrackerFilesProcessedWatcher *watcher,
+                                            GFile                        *file)
+{
+    g_autoptr (GMainLoop) loop = NULL;
+    g_autoptr (GDBusProxy) proxy = NULL;
+    guint timeout_id;
+    gulong signal_id;
+
+    if (g_list_find_custom (watcher->file_list, file, (GCompareFunc) check_file_status_cb))
+        return;
+
+    loop = g_main_loop_new (NULL, 0);
+
+    signal_id = g_signal_connect_after (watcher->proxy, "files-processed", G_CALLBACK 
(files_processed_quit_main_loop_cb), loop);
+    timeout_id = g_timeout_add_seconds (AWAIT_FILE_PROCESSED_TIMEOUT, await_file_timeout_cb, file);
+
+    while (TRUE) {
+        g_main_loop_run (loop);
+
+        if (g_list_find_custom (watcher->file_list, file, (GCompareFunc) check_file_status_cb))
+            break;
+    }
+
+    g_source_remove (timeout_id);
+    g_signal_handler_disconnect (watcher->proxy, signal_id);
+}
diff --git a/test/automated/displayless/test-utilities-tracker.h 
b/test/automated/displayless/test-utilities-tracker.h
new file mode 100644
index 000000000..2af0eb1a7
--- /dev/null
+++ b/test/automated/displayless/test-utilities-tracker.h
@@ -0,0 +1,12 @@
+#include <gio/gio.h>
+
+#pragma once
+
+typedef struct _TrackerFilesProcessedWatcher TrackerFilesProcessedWatcher;
+
+TrackerFilesProcessedWatcher *tracker_files_processed_watcher_new (void);
+
+void tracker_files_processed_watcher_free (TrackerFilesProcessedWatcher *data);
+
+void tracker_files_processed_watcher_await_file (TrackerFilesProcessedWatcher *watcher,
+                                                 GFile                        *file);


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