summaryrefslogtreecommitdiffstats
path: root/test/automated/displayless/test-file-utilities.c
blob: 17a7c921eb3abe6a2147201e426760d5d1ba1f8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <glib.h>
#include <glib/gprintf.h>
#include "src/nautilus-directory.h"
#include "src/nautilus-file-utilities.h"
#include "src/nautilus-search-directory.h"
#include "src/nautilus-directory.h"
#include "src/nautilus-file.h"
#include "src/nautilus-file.c"
#include <unistd.h>
#include "eel/eel-string.h"

#define ROOT_DIR "file:///tmp"

/* Tests the function for empty selections */
static void
test_both_null (void)
{
    g_assert_true (nautilus_file_selection_equal (NULL, NULL));
}

/* Tests the function for an empty and a non-empty selection */
static void
test_either_null (void)
{
    g_autoptr (NautilusFile) file = NULL;
    g_autoptr (NautilusDirectory) directory = NULL;
    g_autolist (NautilusFile) selection = NULL;

    directory = nautilus_directory_get_by_uri (ROOT_DIR);
    g_assert_true (NAUTILUS_IS_DIRECTORY (directory));

    file = nautilus_file_new_from_filename (directory, "null_first", FALSE);
    nautilus_directory_add_file (directory, file);

    selection = g_list_append (selection, g_object_ref (file));
    g_assert_false (nautilus_file_selection_equal (NULL, selection));
    g_assert_false (nautilus_file_selection_equal (selection, NULL));
}

/* tests the function for 2 identical selections, each containing one file */
static void
test_one_file_equal (void)
{
    g_autoptr (NautilusFile) file = NULL;
    g_autoptr (NautilusDirectory) directory = NULL;
    g_autolist (NautilusFile) selection = NULL;

    directory = nautilus_directory_get_by_uri (ROOT_DIR);
    g_assert_true (NAUTILUS_IS_DIRECTORY (directory));

    file = nautilus_file_new_from_filename (directory, "one_file_equal", FALSE);
    nautilus_directory_add_file (directory, file);

    selection = g_list_append (selection, g_object_ref (file));
    g_assert_true (nautilus_file_selection_equal (selection, selection));
}

/* Tests the function for 2 different selections, each containing one file */
static void
test_one_file_different (void)
{
    g_autoptr (NautilusFile) one_file_first = NULL;
    g_autoptr (NautilusFile) one_file_second = NULL;
    g_autoptr (NautilusDirectory) directory = NULL;
    g_autolist (NautilusFile) first_selection = NULL;
    g_autolist (NautilusFile) second_selection = NULL;

    directory = nautilus_directory_get_by_uri (ROOT_DIR);
    g_assert_true (NAUTILUS_IS_DIRECTORY (directory));

    one_file_first = nautilus_file_new_from_filename (directory, "one_file_first", FALSE);
    nautilus_directory_add_file (directory, one_file_first);

    one_file_second = nautilus_file_new_from_filename (directory, "one_file_second", FALSE);
    nautilus_directory_add_file (directory, one_file_second);

    first_selection = g_list_append (first_selection, g_object_ref (one_file_first));
    second_selection = g_list_append (second_selection, g_object_ref (one_file_second));
    g_assert_false (nautilus_file_selection_equal (first_selection, second_selection));
}

/* Tests the function for 2 identical selections, each containing 50 files */
static void
test_multiple_files_equal_medium (void)
{
    g_autoptr (NautilusDirectory) directory = NULL;
    g_autolist (NautilusFile) selection = NULL;

    directory = nautilus_directory_get_by_uri (ROOT_DIR);
    g_assert_true (NAUTILUS_IS_DIRECTORY (directory));
    for (gint index = 0; index < 50; index++)
    {
        g_autoptr (NautilusFile) file = NULL;
        g_autofree gchar *file_name = NULL;

        file_name = g_strdup_printf ("multiple_files_equal_medium_%i", index);
        file = nautilus_file_new_from_filename (directory, file_name, FALSE);
        nautilus_directory_add_file (directory, file);
        selection = g_list_prepend (selection, g_object_ref (file));
    }

    g_assert_true (nautilus_file_selection_equal (selection, selection));
}

/* Tests the function for 2 different selections, each containing 51 files,
 * the last file being the different one */
static void
test_multiple_files_different_medium (void)
{
    g_autoptr (NautilusFile) first_file = NULL;
    g_autoptr (NautilusFile) second_file = NULL;
    g_autoptr (NautilusDirectory) directory = NULL;
    g_autolist (NautilusFile) first_selection = NULL;
    g_autolist (NautilusFile) second_selection = NULL;
    g_autofree gchar *first_file_name = NULL;
    g_autofree gchar *second_file_name = NULL;

    directory = nautilus_directory_get_by_uri (ROOT_DIR);
    g_assert_true (NAUTILUS_IS_DIRECTORY (directory));
    for (gint index = 0; index < 50; index++)
    {
        first_file_name = g_strdup_printf ("multiple_files_different_medium_%i", index);
        first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE);
        nautilus_directory_add_file (directory, first_file);
        g_assert_true (NAUTILUS_IS_FILE (first_file));
        first_selection = g_list_prepend (first_selection, g_object_ref (first_file));
        second_selection = g_list_prepend (second_selection, g_object_ref (first_file));
    }

    first_file_name = g_strdup_printf ("multiple_files_different_medium_lastElement");
    second_file_name = g_strdup_printf ("multiple_files_different_medium_differentElement");
    first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE);
    nautilus_directory_add_file (directory, first_file);
    second_file = nautilus_file_new_from_filename (directory, second_file_name, FALSE);
    nautilus_directory_add_file (directory, second_file);
    g_assert_true (NAUTILUS_IS_FILE (first_file));
    g_assert_true (NAUTILUS_IS_FILE (second_file));
    first_selection = g_list_append (first_selection, g_object_ref (first_file));
    second_selection = g_list_append (second_selection, g_object_ref (second_file));

    g_assert_false (nautilus_file_selection_equal (first_selection, second_selection));
}

/* Tests the function for 2 identical selections, each containing 1000 files */
static void
test_multiple_files_equal_large (void)
{
    g_autoptr (NautilusDirectory) directory = NULL;
    g_autolist (NautilusFile) selection = NULL;

    directory = nautilus_directory_get_by_uri (ROOT_DIR);
    g_assert_true (NAUTILUS_IS_DIRECTORY (directory));
    for (gint index = 0; index < 1000; index++)
    {
        g_autoptr (NautilusFile) file = NULL;
        g_autofree gchar *file_name = NULL;

        file_name = g_strdup_printf ("multiple_files_equal_large_%i", index);
        file = nautilus_file_new_from_filename (directory, file_name, FALSE);
        nautilus_directory_add_file (directory, file);
        g_assert_true (NAUTILUS_IS_FILE (file));
        selection = g_list_prepend (selection, g_object_ref (file));
    }

    g_assert_true (nautilus_file_selection_equal (selection, selection));
}

/* Tests the function for 2 different selections, each containing 1001 files,
 * the last file being the different one */
static void
test_multiple_files_different_large (void)
{
    g_autoptr (NautilusFile) first_file = NULL;
    g_autoptr (NautilusFile) second_file = NULL;
    g_autoptr (NautilusDirectory) directory = NULL;
    g_autolist (NautilusFile) first_selection = NULL;
    g_autolist (NautilusFile) second_selection = NULL;
    g_autofree gchar *first_file_name = NULL;
    g_autofree gchar *second_file_name = NULL;

    directory = nautilus_directory_get_by_uri (ROOT_DIR);
    g_assert_true (NAUTILUS_IS_DIRECTORY (directory));
    for (gint index = 0; index < 1000; index++)
    {
        first_file_name = g_strdup_printf ("multiple_files_different_large_%i", index);
        first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE);
        nautilus_directory_add_file (directory, first_file);
        g_assert_true (NAUTILUS_IS_FILE (first_file));
        first_selection = g_list_prepend (first_selection, g_object_ref (first_file));
        second_selection = g_list_prepend (second_selection, g_object_ref (first_file));
    }

    first_file_name = g_strdup_printf ("multiple_files_different_large_lastElement");
    second_file_name = g_strdup_printf ("multiple_files_different_large_differentElement");
    first_file = nautilus_file_new_from_filename (directory, first_file_name, FALSE);
    nautilus_directory_add_file (directory, first_file);
    second_file = nautilus_file_new_from_filename (directory, second_file_name, FALSE);
    nautilus_directory_add_file (directory, second_file);
    g_assert_true (NAUTILUS_IS_FILE (first_file));
    g_assert_true (NAUTILUS_IS_FILE (second_file));

    first_selection = g_list_append (first_selection, g_object_ref (first_file));
    second_selection = g_list_append (second_selection, g_object_ref (second_file));

    g_assert_false (nautilus_file_selection_equal (first_selection, second_selection));
}

static void
setup_test_suite (void)
{
    g_test_add_func ("/file-selection-equal-null/1.0",
                     test_both_null);
    g_test_add_func ("/file-selection-equal-null/1.1",
                     test_either_null);
    g_test_add_func ("/file-selection-equal-files/1.0",
                     test_one_file_equal);
    g_test_add_func ("/file-selection-equal-files/1.1",
                     test_multiple_files_equal_medium);
    g_test_add_func ("/file-selection-equal-files/1.2",
                     test_multiple_files_equal_large);
    g_test_add_func ("/file-selection-different-files/1.0",
                     test_one_file_different);
    g_test_add_func ("/file-selection-different-files/1.1",
                     test_multiple_files_different_medium);
    g_test_add_func ("/file-selection-different-files/1.2",
                     test_multiple_files_different_large);
}

int
main (int   argc,
      char *argv[])
{
    g_test_init (&argc, &argv, NULL);
    g_test_set_nonfatal_assertions ();
    nautilus_ensure_extension_points ();

    setup_test_suite ();

    return g_test_run ();
}