summaryrefslogtreecommitdiff
path: root/test/display/test-file-operations-create.c
blob: c834b1e8d50c3f7608001e721818b90b1481afcb (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright © 2025 Khalid Abu Shawarib
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Tests for file creation operations
 */

#include <nautilus-file-undo-manager.h>
#include <nautilus-file-operations.h>
#include <nautilus-tag-manager.h>

#include <gtk/gtk.h>

#include "test-utilities.h"

typedef struct
{
    GFile *file;
    gboolean success;
} CreateTestData;

static void
create_done_callback (GFile    *created_file,
                      gboolean  success,
                      gpointer  callback_data)
{
    CreateTestData *test_data = callback_data;

    g_assert_true (g_file_equal (test_data->file, created_file));
    g_assert_true (success);

    test_data->success = success;
}

static void
assert_is_directory (GFile *location)
{
    g_autoptr (GFileInfo) info = g_file_query_info (location,
                                                    G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                                    G_FILE_QUERY_INFO_NONE,
                                                    NULL, NULL);

    g_assert_nonnull (info);
    g_assert_cmpint (g_file_info_get_file_type (info), ==, G_FILE_TYPE_DIRECTORY);
}

static void
test_create_folder (void)
{
    g_autofree gchar *parent_uri = g_strconcat ("file://", test_get_tmp_dir (), NULL);
    const char *foldername = "created_folder";
    g_autoptr (GFile) created_file = g_file_new_build_filename (test_get_tmp_dir (),
                                                                foldername,
                                                                NULL);
    CreateTestData data = { created_file, FALSE };

    nautilus_file_operations_new_folder (NULL,
                                         NULL,
                                         parent_uri,
                                         foldername,
                                         create_done_callback,
                                         &data);

    ITER_CONTEXT_WHILE (!data.success);

    assert_is_directory (created_file);

    test_operation_undo ();

    g_assert_false (g_file_query_exists (created_file, NULL));

    test_operation_redo ();

    assert_is_directory (created_file);

    test_clear_tmp_dir ();
}

static void
assert_file_content (GFile_autoptr  location,
                     const gchar   *expected_content,
                     const gsize    content_length,
                     const char    *mime_type)
{
    g_autoptr (GFileInputStream) stream = g_file_read (location, NULL, NULL);
    gchar buffer[512];
    gsize read_bytes = 0;

    g_assert_nonnull (stream);
    g_input_stream_read_all (G_INPUT_STREAM (stream),
                             buffer, sizeof (buffer), &read_bytes,
                             NULL, NULL);
    g_assert_cmpint (read_bytes, ==, content_length);
    g_assert_cmpmem (buffer, read_bytes, expected_content, content_length);

    g_autoptr (GFileInfo) info = g_file_query_info (location,
                                                    G_FILE_ATTRIBUTE_STANDARD_SIZE ","
                                                    G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                                                    G_FILE_QUERY_INFO_NONE,
                                                    NULL, NULL);
    g_assert_nonnull (info);
    g_assert_cmpint (g_file_info_get_size (info), ==, content_length);
    g_assert_cmpstr (g_file_info_get_content_type (info), ==, mime_type);
}

static void
test_create_file (void)
{
    g_autofree char *parent_uri = g_strconcat ("file://", test_get_tmp_dir (), NULL);
    const char *filename = "created_file.txt";
    const char *contents = "Hello, World!\n";
    const gsize content_length = strlen (contents);
    const char *mime_type = "text/plain";
    g_autoptr (GFile) created_file = g_file_new_build_filename (test_get_tmp_dir (),
                                                                filename,
                                                                NULL);
    CreateTestData data = { created_file, FALSE };

    nautilus_file_operations_new_file (NULL,
                                       parent_uri,
                                       filename,
                                       contents,
                                       content_length,
                                       create_done_callback,
                                       &data);

    ITER_CONTEXT_WHILE (!data.success);

    assert_file_content (created_file, contents, content_length, mime_type);

    test_operation_undo ();

    g_assert_false (g_file_query_exists (created_file, NULL));

    test_operation_redo ();

    assert_file_content (created_file, contents, content_length, mime_type);

    test_clear_tmp_dir ();
}

static void
create_file_with_content (GFile_autoptr  location,
                          const gchar   *content,
                          const gsize    content_length)
{
    g_autoptr (GFileOutputStream) stream = g_file_create (location, G_TYPE_FILE_CREATE_FLAGS, NULL, NULL);

    g_assert_nonnull (stream);
    g_output_stream_write_all (G_OUTPUT_STREAM (stream),
                               content, content_length,
                               NULL,
                               NULL,
                               NULL);
}

static void
test_create_file_from_template (void)
{
    g_autofree char *template_uri = g_strconcat ("file://",
                                                 test_get_tmp_dir (),
                                                 "/template_file.txt",
                                                 NULL);
    g_autoptr (GFile) template_location = g_file_new_for_uri (template_uri);
    g_autofree char *parent_uri = g_strconcat ("file://", test_get_tmp_dir (), NULL);
    const char *filename = "created_file_from_template.txt";
    const char *contents = "Hello, World!\n";
    const gsize content_length = strlen (contents);
    g_autoptr (GFile) created_file = g_file_new_build_filename (test_get_tmp_dir (),
                                                                filename,
                                                                NULL);
    const char *duplicate_filename = "created_file_from_template (2).txt";
    g_autoptr (GFile) duplicate_created_file = g_file_new_build_filename (test_get_tmp_dir (),
                                                                          duplicate_filename,
                                                                          NULL);
    CreateTestData data = { created_file, FALSE };
    const char *mime_type = "text/plain";

    create_file_with_content (template_location, contents, content_length);

    nautilus_file_operations_new_file_from_template (NULL,
                                                     parent_uri,
                                                     filename,
                                                     template_uri,
                                                     create_done_callback,
                                                     &data);

    ITER_CONTEXT_WHILE (!data.success);

    assert_file_content (created_file, contents, content_length, mime_type);

    test_operation_undo ();

    g_assert_false (g_file_query_exists (created_file, NULL));

    test_operation_redo ();

    assert_file_content (created_file, contents, content_length, mime_type);

    /* Try to create from the template with the same name to verify conflict
     * resolution. */
    data.file = duplicate_created_file;
    data.success = FALSE;
    nautilus_file_operations_new_file_from_template (NULL,
                                                     parent_uri,
                                                     filename,
                                                     template_uri,
                                                     create_done_callback,
                                                     &data);

    ITER_CONTEXT_WHILE (!data.success);

    assert_file_content (duplicate_created_file, contents, content_length, mime_type);

    test_operation_undo ();

    g_assert_false (g_file_query_exists (duplicate_created_file, NULL));

    test_operation_redo ();

    assert_file_content (duplicate_created_file, contents, content_length, mime_type);

    test_clear_tmp_dir ();
}

static void
save_done_callback (GHashTable *debuting_uris,
                    gboolean    success,
                    gpointer    callback_data)
{
    gboolean *success_data = callback_data;

    g_assert_true (success);

    *success_data = success;
}

static GdkTexture *
make_test_texture (void)
{
    /* Create a simple 1x1 RGBA texture using gdk_pixbuf as an easy route */
    g_autoptr (GdkPixbuf) pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);

    gdk_pixbuf_fill (pixbuf, 0x00ff00ff);

    return gdk_texture_new_for_pixbuf (pixbuf);
}

static void
test_save_image_from_texture (void)
{
    g_autofree gchar *parent_uri = g_strconcat ("file://", test_get_tmp_dir (), NULL);
    const char *image_basename = "Dropped image";
    g_autoptr (GdkTexture) texture = make_test_texture ();
    gboolean success = FALSE;

    nautilus_file_operations_save_image_from_texture (NULL,
                                                      NULL,
                                                      parent_uri,
                                                      image_basename,
                                                      texture,
                                                      save_done_callback,
                                                      &success);

    ITER_CONTEXT_WHILE (!success);

    const char *image_name = "Dropped image.png";
    g_autoptr (GFile) saved_image = g_file_new_build_filename (test_get_tmp_dir (),
                                                               image_name,
                                                               NULL);

    g_assert_true (g_file_query_exists (saved_image, NULL));
}

static void
test_save_image_from_clipboard (void)
{
    /* Dumb widget to use with the operation call */
    g_autoptr (GtkWidget) box = g_object_ref_sink (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
    GdkClipboard *clipboard = gtk_widget_get_clipboard (box);
    g_autoptr (GdkTexture) texture = make_test_texture ();
    g_autofree gchar *parent_uri = g_strconcat ("file://", test_get_tmp_dir (), NULL);
    gboolean success = FALSE;

    gdk_clipboard_set_texture (clipboard, texture);

    nautilus_file_operations_paste_image_from_clipboard (GTK_WIDGET (box),
                                                         NULL,
                                                         parent_uri,
                                                         save_done_callback,
                                                         &success);

    ITER_CONTEXT_WHILE (!success);

    const char *image_name = "Dropped image.png";
    g_autoptr (GFile) saved_image = g_file_new_build_filename (test_get_tmp_dir (), image_name, NULL);

    g_assert_true (g_file_query_exists (saved_image, NULL));

    test_clear_tmp_dir ();
}

int
main (int   argc,
      char *argv[])
{
    g_autoptr (NautilusFileUndoManager) undo_manager = NULL;
    g_autoptr (NautilusTagManager) tag_manager = NULL;

    gtk_test_init (&argc, &argv, NULL);

    g_setenv ("RUNNING_TESTS", "TRUE", TRUE);

    undo_manager = nautilus_file_undo_manager_new ();
    test_init_config_dir ();

    g_test_add_func ("/create/folder",
                     test_create_folder);
    g_test_add_func ("/create/file/direct-content",
                     test_create_file);
    g_test_add_func ("/create/file/template",
                     test_create_file_from_template);
    g_test_add_func ("/create/image/texture",
                     test_save_image_from_texture);
    g_test_add_func ("/create/image/clipboard-texture",
                     test_save_image_from_clipboard);

    return g_test_run ();
}