Merge pull request #3522 from lucasvieites/patch-2
[geany-mirror.git] / tests / test_sidebar.c
blob9659dc72e71ea8669a3aba86bbce810d83515e4d
2 #ifdef HAVE_CONFIG_H
3 # include "config.h"
4 #endif
6 #include "document.h"
7 #include "documentprivate.h"
8 #include "keyfile.h"
9 #include "main.h"
10 #include "sidebar.h"
11 #include "ui_utils.h"
12 #include "utils.h"
14 #define SIDEBAR_TEST_ADD(path, func) g_test_add_func("/sidebar/" path, func);
16 static void openfiles_add(const gchar **file_names)
18 const gchar *file;
20 while ((file = *file_names++))
22 GeanyDocument *doc = g_new0(GeanyDocument, 1);
24 doc->priv = g_new0(GeanyDocumentPrivate, 1);
25 doc->file_name = strdup(file);
27 sidebar_openfiles_add(doc);
32 static gboolean tree_count_cb(GtkTreeModel *model, GtkTreePath *path,
33 GtkTreeIter *iter, gpointer data_in)
35 gint *c = (gint *) data_in;
37 *c = *c + 1;
38 return FALSE;
42 static gboolean tree_strings_cb(GtkTreeModel *model, GtkTreePath *path,
43 GtkTreeIter *iter, gpointer data_in)
45 gchar **data = (gchar **) data_in;
46 gchar *file;
48 gtk_tree_model_get(model, iter, DOCUMENTS_SHORTNAME, &file, -1);
49 data[g_strv_length(data)] = file;
51 printf("%s\n", file);
52 return FALSE;
55 void do_test_sidebar_openfiles(const gchar **test_data, const gchar **expected)
57 #ifdef HAVE_G_STRV_EQUAL
58 int count = 0;
59 GtkTreeStore *store;
60 gchar **data;
62 store = sidebar_create_store_openfiles();
64 openfiles_add(test_data);
66 gtk_tree_model_foreach(GTK_TREE_MODEL(store), tree_count_cb, (gpointer) &count);
67 data = g_new0(gchar *, count + 1);
68 gtk_tree_model_foreach(GTK_TREE_MODEL(store), tree_strings_cb, (gpointer) data);
69 g_assert_true(g_strv_equal(expected, (const gchar * const *) data));
70 #else
71 g_test_skip("Need g_strv_equal(), since GLib 2.60");
72 #endif
75 void test_sidebar_openfiles_none(void)
77 const gchar *files[] = {
78 "/tmp/x",
79 "/tmp/b/a",
80 "/tmp/b/b",
81 NULL
83 const gchar *expected[] = {
84 "a",
85 "b",
86 "x",
87 NULL
90 interface_prefs.openfiles_path_mode = OPENFILES_PATHS_NONE;
91 do_test_sidebar_openfiles(files, expected);
95 void test_sidebar_openfiles_path(void)
97 const gchar *files[] = {
98 "/tmp/x",
99 "/tmp/b/a",
100 "/tmp/b/b",
101 NULL
103 const gchar *expected[] = {
104 "/tmp",
105 "x",
106 "/tmp/b",
107 "a",
108 "b",
109 NULL
112 interface_prefs.openfiles_path_mode = OPENFILES_PATHS_LIST;
113 do_test_sidebar_openfiles(files, expected);
117 void test_sidebar_openfiles_tree(void)
119 const gchar *files[] = {
120 "/tmp/x",
121 "/tmp/b/a",
122 "/tmp/b/b",
123 NULL
125 const gchar *expected[] = {
126 "/tmp",
127 "x",
128 "b",
129 "a",
130 "b",
131 NULL
134 interface_prefs.openfiles_path_mode = OPENFILES_PATHS_TREE;
135 do_test_sidebar_openfiles(files, expected);
138 int main(int argc, char **argv)
140 g_test_init(&argc, &argv, NULL);
141 /* Not sure if we can really continue without DISPLAY. Fake X display perhaps?
143 * This test seems to work, at least.
145 gtk_init_check(&argc, &argv);
147 main_init_headless();
149 SIDEBAR_TEST_ADD("openfiles_none", test_sidebar_openfiles_none);
150 SIDEBAR_TEST_ADD("openfiles_path", test_sidebar_openfiles_path);
151 SIDEBAR_TEST_ADD("openfiles_tree", test_sidebar_openfiles_tree);
153 return g_test_run();