test-save-and-export.c 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 2009 Martin Nordholts <martinn@src.gnome.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17
 */

18 19
#include "config.h"

20 21 22 23
#include <stdlib.h>
#include <string.h>

#include <gegl.h>
24
#include <glib/gstdio.h>
25 26 27 28 29 30 31 32
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "dialogs/dialogs-types.h"

33 34 35 36 37 38 39 40 41 42 43 44
#include "core/gimp.h"
#include "core/gimpchannel.h"
#include "core/gimpcontext.h"
#include "core/gimpimage.h"
#include "core/gimplayer.h"
#include "core/gimptoolinfo.h"
#include "core/gimptooloptions.h"

#include "plug-in/gimppluginmanager-file.h"

#include "file/file-open.h"
#include "file/file-save.h"
45 46 47 48 49 50 51 52 53 54 55 56 57 58

#include "widgets/gimpdialogfactory.h"
#include "widgets/gimpdock.h"
#include "widgets/gimpdockable.h"
#include "widgets/gimpdockbook.h"
#include "widgets/gimpdocked.h"
#include "widgets/gimpdockwindow.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpsessioninfo.h"
#include "widgets/gimptoolbox.h"
#include "widgets/gimptooloptionseditor.h"
#include "widgets/gimpuimanager.h"
#include "widgets/gimpwidgets-utils.h"

59 60 61 62 63
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-scale.h"
#include "display/gimpdisplayshell-transform.h"
#include "display/gimpimagewindow.h"
64 65 66 67 68 69 70

#include "tests.h"

#include "gimp-app-test-utils.h"


#define ADD_TEST(function) \
Martin Nordholts's avatar
Martin Nordholts committed
71
  g_test_add_data_func ("/gimp-save-and-export/" #function, gimp, function);
72 73 74 75 76 77


typedef gboolean (*GimpUiTestFunc) (GObject *object);


/**
78
 * new_file_has_no_files:
79 80 81 82 83
 * @data:
 *
 * Tests that the URIs are correct for a newly created image.
 **/
static void
84
new_file_has_no_files (gconstpointer    data)
85 86
{
  Gimp      *gimp  = GIMP (data);
87
  GimpImage *image = gimp_test_utils_create_image_from_dialog (gimp);
88

89 90 91
  g_assert (gimp_image_get_file (image) == NULL);
  g_assert (gimp_image_get_imported_file (image) == NULL);
  g_assert (gimp_image_get_exported_file (image) == NULL);
92 93 94
}

/**
95
 * opened_xcf_file_files:
96 97 98 99 100 101
 * @data:
 *
 * Tests that GimpImage URIs are correct for an XCF file that has just
 * been opened.
 **/
static void
102
opened_xcf_file_files (gconstpointer data)
103 104 105
{
  Gimp              *gimp = GIMP (data);
  GimpImage         *image;
106
  GFile             *file;
107 108 109 110 111 112
  gchar             *filename;
  GimpPDBStatusType  status;

  filename = g_build_filename (g_getenv ("GIMP_TESTING_ABS_TOP_SRCDIR"),
                               "app/tests/files/gimp-2-6-file.xcf",
                               NULL);
113 114
  file = g_file_new_for_path (filename);
  g_free (filename);
115 116 117 118

  image = file_open_image (gimp,
                           gimp_get_user_context (gimp),
                           NULL /*progress*/,
119 120
                           file,
                           file,
121 122 123 124 125 126 127
                           FALSE /*as_new*/,
                           NULL /*file_proc*/,
                           GIMP_RUN_NONINTERACTIVE,
                           &status,
                           NULL /*mime_type*/,
                           NULL /*error*/);

128 129 130
  g_assert (g_file_equal (gimp_image_get_file (image), file));
  g_assert (gimp_image_get_imported_file (image) == NULL);
  g_assert (gimp_image_get_exported_file (image) == NULL);
131

132
  g_object_unref (file);
133 134 135
}

/**
136
 * imported_file_files:
137 138 139 140 141
 * @data:
 *
 * Tests that URIs are correct for an imported image.
 **/
static void
142
imported_file_files (gconstpointer data)
143 144 145
{
  Gimp              *gimp = GIMP (data);
  GimpImage         *image;
146
  GFile             *file;
147 148 149 150 151 152
  gchar             *filename;
  GimpPDBStatusType  status;

  filename = g_build_filename (g_getenv ("GIMP_TESTING_ABS_TOP_SRCDIR"),
                               "desktop/64x64/gimp.png",
                               NULL);
153
  g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));
154 155
  file = g_file_new_for_path (filename);
  g_free (filename);
156 157 158 159

  image = file_open_image (gimp,
                           gimp_get_user_context (gimp),
                           NULL /*progress*/,
160 161
                           file,
                           file,
162 163 164 165 166 167 168
                           FALSE /*as_new*/,
                           NULL /*file_proc*/,
                           GIMP_RUN_NONINTERACTIVE,
                           &status,
                           NULL /*mime_type*/,
                           NULL /*error*/);

169 170 171 172 173
  g_assert (gimp_image_get_file (image) == NULL);
  g_assert (g_file_equal (gimp_image_get_imported_file (image), file));
  g_assert (gimp_image_get_exported_file (image) == NULL);

  g_object_unref (file);
174 175 176
}

/**
177
 * saved_imported_file_files:
178 179 180 181 182 183
 * @data:
 *
 * Tests that the URIs are correct for an image that has been imported
 * and then saved.
 **/
static void
184
saved_imported_file_files (gconstpointer data)
185 186 187
{
  Gimp                *gimp = GIMP (data);
  GimpImage           *image;
188
  GFile               *import_file;
189
  gchar               *import_filename;
190
  GFile               *save_file;
191 192 193 194 195 196 197
  gchar               *save_filename;
  GimpPDBStatusType    status;
  GimpPlugInProcedure *proc;

  import_filename = g_build_filename (g_getenv ("GIMP_TESTING_ABS_TOP_SRCDIR"),
                                      "desktop/64x64/gimp.png",
                                      NULL);
198 199 200
  import_file = g_file_new_for_path (import_filename);
  g_free (import_filename);

201
  save_filename = g_build_filename (g_get_tmp_dir (), "gimp-test.xcf", NULL);
202 203
  save_file = g_file_new_for_path (save_filename);
  g_free (save_filename);
204 205 206 207 208

  /* Import */
  image = file_open_image (gimp,
                           gimp_get_user_context (gimp),
                           NULL /*progress*/,
209 210
                           import_file,
                           import_file,
211 212 213 214 215 216 217
                           FALSE /*as_new*/,
                           NULL /*file_proc*/,
                           GIMP_RUN_NONINTERACTIVE,
                           &status,
                           NULL /*mime_type*/,
                           NULL /*error*/);

218 219
  g_object_unref (import_file);

220
  /* Save */
221 222 223 224
  proc = gimp_plug_in_manager_file_procedure_find (image->gimp->plug_in_manager,
                                                   GIMP_FILE_PROCEDURE_GROUP_SAVE,
                                                   save_file,
                                                   NULL /*error*/);
225 226 227
  file_save (gimp,
             image,
             NULL /*progress*/,
228
             save_file,
229 230 231
             proc,
             GIMP_RUN_NONINTERACTIVE,
             TRUE /*change_saved_state*/,
232 233
             FALSE /*export_backward*/,
             FALSE /*export_forward*/,
234 235 236
             NULL /*error*/);

  /* Assert */
237 238 239
  g_assert (g_file_equal (gimp_image_get_file (image), save_file));
  g_assert (gimp_image_get_imported_file (image) == NULL);
  g_assert (gimp_image_get_exported_file (image) == NULL);
240

241 242
  g_file_delete (save_file, NULL, NULL);
  g_object_unref (save_file);
243 244 245
}

/**
246
 * exported_file_files:
247 248 249 250 251 252
 * @data:
 *
 * Tests that the URIs for an exported, newly created file are
 * correct.
 **/
static void
253
exported_file_files (gconstpointer data)
254
{
255
  GFile               *save_file;
256 257 258
  gchar               *save_filename;
  GimpPlugInProcedure *proc;
  Gimp                *gimp  = GIMP (data);
259
  GimpImage           *image = gimp_test_utils_create_image_from_dialog (gimp);
260 261

  save_filename = g_build_filename (g_get_tmp_dir (), "gimp-test.png", NULL);
262 263
  save_file = g_file_new_for_path (save_filename);
  g_free (save_filename);
264

265 266 267 268
  proc = gimp_plug_in_manager_file_procedure_find (image->gimp->plug_in_manager,
                                                   GIMP_FILE_PROCEDURE_GROUP_EXPORT,
                                                   save_file,
                                                   NULL /*error*/);
269 270 271
  file_save (gimp,
             image,
             NULL /*progress*/,
272
             save_file,
273 274 275
             proc,
             GIMP_RUN_NONINTERACTIVE,
             FALSE /*change_saved_state*/,
276 277
             FALSE /*export_backward*/,
             TRUE /*export_forward*/,
278 279
             NULL /*error*/);

280 281 282
  g_assert (gimp_image_get_file (image) == NULL);
  g_assert (gimp_image_get_imported_file (image) == NULL);
  g_assert (g_file_equal (gimp_image_get_exported_file (image), save_file));
283

284 285
  g_file_delete (save_file, NULL, NULL);
  g_object_unref (save_file);
286 287
}

288
/**
289
 * clear_import_file_after_export:
290 291 292 293 294 295 296
 * @data:
 *
 * Tests that after a XCF file that was imported has been exported,
 * the import URI is cleared. An image can not be considered both
 * imported and exported at the same time.
 **/
static void
297
clear_import_file_after_export (gconstpointer data)
298 299 300
{
  Gimp                *gimp = GIMP (data);
  GimpImage           *image;
301
  GFile               *file;
302
  gchar               *filename;
303
  GFile               *save_file;
304 305 306 307 308 309 310
  gchar               *save_filename;
  GimpPlugInProcedure *proc;
  GimpPDBStatusType    status;

  filename = g_build_filename (g_getenv ("GIMP_TESTING_ABS_TOP_SRCDIR"),
                               "desktop/64x64/gimp.png",
                               NULL);
311 312
  file = g_file_new_for_path (filename);
  g_free (filename);
313 314 315 316

  image = file_open_image (gimp,
                           gimp_get_user_context (gimp),
                           NULL /*progress*/,
317 318
                           file,
                           file,
319 320 321 322 323 324 325
                           FALSE /*as_new*/,
                           NULL /*file_proc*/,
                           GIMP_RUN_NONINTERACTIVE,
                           &status,
                           NULL /*mime_type*/,
                           NULL /*error*/);

326 327 328 329 330
  g_assert (gimp_image_get_file (image) == NULL);
  g_assert (g_file_equal (gimp_image_get_imported_file (image), file));
  g_assert (gimp_image_get_exported_file (image) == NULL);

  g_object_unref (file);
331 332

  save_filename = g_build_filename (g_get_tmp_dir (), "gimp-test.png", NULL);
333 334
  save_file = g_file_new_for_path (save_filename);
  g_free (save_filename);
335

336 337 338 339
  proc = gimp_plug_in_manager_file_procedure_find (image->gimp->plug_in_manager,
                                                   GIMP_FILE_PROCEDURE_GROUP_EXPORT,
                                                   save_file,
                                                   NULL /*error*/);
340 341 342
  file_save (gimp,
             image,
             NULL /*progress*/,
343
             save_file,
344 345 346
             proc,
             GIMP_RUN_NONINTERACTIVE,
             FALSE /*change_saved_state*/,
347 348
             FALSE /*export_backward*/,
             TRUE /*export_forward*/,
349 350
             NULL /*error*/);

351 352 353
  g_assert (gimp_image_get_file (image) == NULL);
  g_assert (gimp_image_get_imported_file (image) == NULL);
  g_assert (g_file_equal (gimp_image_get_exported_file (image), save_file));
354

355 356
  g_file_delete (save_file, NULL, NULL);
  g_object_unref (save_file);
357 358
}

359 360 361
int
main(int    argc,
     char **argv)
362 363 364 365 366 367 368
{
  Gimp *gimp   = NULL;
  gint  result = -1;

  gimp_test_bail_if_no_display ();
  gtk_test_init (&argc, &argv, NULL);

369
  gimp_test_utils_set_gimp3_directory ("GIMP_TESTING_ABS_TOP_SRCDIR",
370
                                       "app/tests/gimpdir");
371
  gimp_test_utils_setup_menus_path ();
372 373 374 375 376

  /* Start up GIMP */
  gimp = gimp_init_for_gui_testing (TRUE /*show_gui*/);
  gimp_test_run_mainloop_until_idle ();

377 378 379 380 381 382
  ADD_TEST (new_file_has_no_files);
  ADD_TEST (opened_xcf_file_files);
  ADD_TEST (imported_file_files);
  ADD_TEST (saved_imported_file_files);
  ADD_TEST (exported_file_files);
  ADD_TEST (clear_import_file_after_export);
383 384 385 386 387

  /* Run the tests and return status */
  result = g_test_run ();

  /* Don't write files to the source dir */
388
  gimp_test_utils_set_gimp3_directory ("GIMP_TESTING_ABS_TOP_BUILDDIR",
389 390 391 392 393 394 395
                                       "app/tests/gimpdir-output");

  /* Exit properly so we don't break script-fu plug-in wire */
  gimp_exit (gimp, TRUE);

  return result;
}