autotestfilesystem.c 17.9 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * autotestfilesystem.c: Automated tests for GtkFileSystem implementations
 * Copyright (C) 2005, Novell, Inc.
 *
 * Authors:
 *   Federico Mena-Quintero <federico@novell.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 21 22 23
 */

#define GTK_FILE_SYSTEM_ENABLE_UNSUPPORTED

24
#include "config.h"
25
#include <string.h>
26
#ifdef HAVE_UNISTD_H
27
#include <unistd.h>
28
#endif
29 30 31 32
#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include <gtk/gtkfilesystem.h>

33 34 35 36 37
#ifdef G_OS_WIN32
#include <direct.h>
#define rmdir(d) _rmdir(d)
#endif

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
#define CALLBACK_TIMEOUT_MS 3000	/* Period after which the callback must have been called */
#define CANCEL_TIMEOUT_MS 100		/* We'll sleep for this much time before cancelling */

#define GET_FOLDER_FILENAME "/etc"
#define GET_INFO_FILENAME "/etc/passwd"
#define CREATE_FOLDER_FILENAME "/tmp/autotestfilesystem-tmp"
#define VOLUME_MOUNT_FILENAME "/"

/* This is stolen from gtkfilechooserdefault.c:set_file_system_backend() */
static GtkFileSystem *
get_file_system (void)
{
  GtkFileSystem *file_system = NULL;

#if 1
  file_system = gtk_file_system_create ("gnome-vfs");
#else
  GtkSettings *settings = gtk_settings_get_default ();
  gchar *default_backend = NULL;

  g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
  if (default_backend)
    {
      file_system = gtk_file_system_create (default_backend);
      g_free (default_backend);
    }

  if (!file_system)
    {
#if defined (G_OS_UNIX)
      file_system = gtk_file_system_unix_new ();
#elif defined (G_OS_WIN32)
      file_system = gtk_file_system_win32_new ();
#else
#error "No default filesystem implementation on the platform"
#endif
    }

#endif

  return file_system;
}



/***** Testing infrastructure *****/

typedef struct {
  gboolean callback_was_called;
  gboolean timeout_was_called;
} TestCallbackClosure;

static void
notify_callback_called (TestCallbackClosure *closure)
{
  closure->callback_was_called = TRUE;
  gtk_main_quit ();
}

static gboolean
timeout_cb (gpointer data)
{
  TestCallbackClosure *closure;

  closure = data;

  closure->timeout_was_called = TRUE;
  gtk_main_quit ();

  return FALSE;
}

static void
wait_for_callback (TestCallbackClosure *closure)
{
113
  gdk_threads_add_timeout (CALLBACK_TIMEOUT_MS, timeout_cb, closure);
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
  gtk_main ();
}

typedef struct {
  const char *test_name;
  gboolean callback_must_be_called;
  gpointer (* setup_fn) (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			 TestCallbackClosure *callback_closure);
  void (* cleanup_fn) (gpointer data, gboolean *success, char **failure_reason);
} TestSpec;

static gboolean
run_test (TestSpec *test_spec)
{
  GtkFileSystem *file_system;
  TestCallbackClosure closure;
  gboolean success_setup, success_cleanup;
  gpointer test_data;
  gboolean callback_success;
  gboolean passed;
  char *setup_failure_reason;
  char *cleanup_failure_reason;

  file_system = get_file_system ();
  if (!file_system)
    {
      printf ("FAIL: test \"%s\"\n", test_spec->test_name);
      printf ("      could not create file system!\n");
      return FALSE;
    }

  closure.callback_was_called = FALSE;
  closure.timeout_was_called = FALSE;

  success_setup = success_cleanup = callback_success = FALSE;
  setup_failure_reason = cleanup_failure_reason = NULL;

  test_data = test_spec->setup_fn (file_system, &success_setup, &setup_failure_reason, &closure);
  if (success_setup)
    {
      wait_for_callback (&closure);

      test_spec->cleanup_fn (test_data, &success_cleanup, &cleanup_failure_reason);

      callback_success = (test_spec->callback_must_be_called == closure.callback_was_called);
    }

  g_object_unref (file_system);

  passed = (success_setup && success_cleanup && callback_success);

  printf ("%s: test \"%s\"\n", passed ? "PASS" : "FAIL", test_spec->test_name);

  if (!passed)
    {
      if (!success_setup)
	printf ("      failure during setup: %s\n",
		setup_failure_reason ? setup_failure_reason : "unknown failure");
      else
	{
	  if (!success_cleanup)
	    printf ("      failure during cleanup: %s\n",
		    cleanup_failure_reason ? cleanup_failure_reason : "unknown failure");

	  if (!callback_success)
	    printf ("      callback %s called but it %s called\n",
		    test_spec->callback_must_be_called ? "MUST BE" : "MUST NOT BE",
		    closure.callback_was_called ? "WAS" : "WAS NOT");
	}
    }

  g_free (setup_failure_reason);
  g_free (cleanup_failure_reason);

  return passed;
}

static gboolean
run_tests (TestSpec *test_specs, int num_tests)
{
  int i;
  int num_passed;

  num_passed = 0;

  for (i = 0; i < num_tests; i++)
    if (run_test (test_specs + i))
      num_passed++;

  if (num_passed == num_tests)
    printf ("ALL TESTS PASSED\n");
  else
    printf ("%d of %d tests FAILED\n", (num_tests - num_passed), num_tests);

  return (num_passed == num_tests);
}



/***** Test functions *****/

static void
sleep_and_cancel_handle (GtkFileSystemHandle *handle)
{
  g_usleep (CANCEL_TIMEOUT_MS * 1000);
  gtk_file_system_cancel_operation (handle);
}

/* get_folder */

struct get_folder_data {
  TestCallbackClosure *callback_closure;
  GtkFileSystemHandle *handle;
  GtkFileFolder *folder;
};

static void
get_folder_cb (GtkFileSystemHandle *handle,
	       GtkFileFolder       *folder,
	       const GError        *error,
	       gpointer             data)
{
  struct get_folder_data *get_folder_data;

  get_folder_data = data;
  get_folder_data->folder = folder;
  notify_callback_called (get_folder_data->callback_closure);
}

static gpointer
get_folder_generic_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			  TestCallbackClosure *callback_closure)
{
  struct get_folder_data *get_folder_data;
  GtkFilePath *path;

  path = gtk_file_system_filename_to_path (file_system, GET_FOLDER_FILENAME);
  if (!path)
    {
      *success = FALSE;
      *failure_reason = g_strdup_printf ("could not turn \"%s\" into a GtkFilePath", GET_FOLDER_FILENAME);
      return NULL;
    }

  get_folder_data = g_new (struct get_folder_data, 1);

  get_folder_data->callback_closure = callback_closure;
  get_folder_data->folder = NULL;

  get_folder_data->handle = gtk_file_system_get_folder (file_system,
							path,
							GTK_FILE_INFO_ALL,
							get_folder_cb,
							get_folder_data);
  gtk_file_path_free (path);

  if (!get_folder_data->handle)
    {
      g_free (get_folder_data);
      *success = FALSE;
      *failure_reason = g_strdup ("gtk_file_system_get_folder() returned a NULL handle");
      return NULL;
    }

  *success = TRUE;

  return get_folder_data;
}

static gpointer
get_folder_no_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			    TestCallbackClosure *callback_closure)
{
  return get_folder_generic_setup (file_system, success, failure_reason, callback_closure);
}

static void
get_folder_cleanup (gpointer data, gboolean *success, char **failure_reason)
{
  struct get_folder_data *get_folder_data;

  get_folder_data = data;

  if (get_folder_data->folder)
    g_object_unref (get_folder_data->folder);

  g_free (get_folder_data);

  *success = TRUE;
}

static gpointer
get_folder_with_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			      TestCallbackClosure *callback_closure)
{
  struct get_folder_data *get_folder_data;

  get_folder_data = get_folder_generic_setup (file_system, success, failure_reason, callback_closure);

  if (*success)
    sleep_and_cancel_handle (get_folder_data->handle);

  return get_folder_data;
}

/* get_info */

struct get_info_data {
  TestCallbackClosure *callback_closure;
  GtkFileSystemHandle *handle;
};

static void
get_info_cb (GtkFileSystemHandle *handle,
	     const GtkFileInfo   *file_info,
	     const GError        *error,
	     gpointer             data)
{
  struct get_info_data *get_info_data;

  get_info_data = data;
  notify_callback_called (get_info_data->callback_closure);
}

static gpointer
get_info_generic_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			TestCallbackClosure *callback_closure)
{
  GtkFilePath *path;
  struct get_info_data *get_info_data;

  path = gtk_file_system_filename_to_path (file_system, GET_INFO_FILENAME);
  if (!path)
    {
      *success = FALSE;
      *failure_reason = g_strdup_printf ("could not turn \"%s\" into a GtkFilePath", GET_INFO_FILENAME);
      return NULL;
    }

  get_info_data = g_new (struct get_info_data, 1);

  get_info_data->callback_closure = callback_closure;
  get_info_data->handle = gtk_file_system_get_info (file_system,
						    path,
						    GTK_FILE_INFO_ALL,
						    get_info_cb,
						    get_info_data);
  gtk_file_path_free (path);

  if (!get_info_data->handle)
    {
      g_free (get_info_data);
      *success = FALSE;
      *failure_reason = g_strdup ("gtk_file_system_get_info() returned a NULL handle");
      return NULL;
    }

  *success = TRUE;
  return get_info_data;
}

static gpointer
get_info_no_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			  TestCallbackClosure *callback_closure)
{
  return get_info_generic_setup (file_system, success, failure_reason, callback_closure);
}

static void
get_info_cleanup (gpointer data, gboolean *success, char **failure_reason)
{
  struct get_info_data *get_info_data;

  get_info_data = data;
  g_free (get_info_data);

  *success = TRUE;
}

static gpointer
get_info_with_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			    TestCallbackClosure *callback_closure)
{
  struct get_info_data *get_info_data;

  get_info_data = get_info_generic_setup (file_system, success, failure_reason, callback_closure);

  if (*success)
    sleep_and_cancel_handle (get_info_data->handle);

  return get_info_data;
}

/* create_folder */

struct create_folder_data {
  TestCallbackClosure *callback_closure;
  GtkFileSystemHandle *handle;
};

static void
create_folder_cb (GtkFileSystemHandle *handle,
		  const GtkFilePath   *path,
		  const GError        *error,
		  gpointer             data)
{
  struct get_folder_data *get_folder_data;

  get_folder_data = data;
  notify_callback_called (get_folder_data->callback_closure);
}

static gpointer
create_folder_generic_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			     TestCallbackClosure *callback_closure)
{
  GtkFilePath *path;
  struct create_folder_data *create_folder_data;

  path = gtk_file_system_filename_to_path (file_system, CREATE_FOLDER_FILENAME);
  if (!path)
    {
      *success = FALSE;
      *failure_reason = g_strdup_printf ("could not turn \"%s\" into a GtkFilePath", CREATE_FOLDER_FILENAME);
      return NULL;
    }

  create_folder_data = g_new (struct create_folder_data, 1);

  create_folder_data->callback_closure = callback_closure;
  create_folder_data->handle = gtk_file_system_create_folder (file_system,
							      path,
							      create_folder_cb,
							      create_folder_data);
  gtk_file_path_free (path);

  if (!create_folder_data->handle)
    {
      g_free (create_folder_data);
      *success = FALSE;
      *failure_reason = g_strdup ("gtk_file_system_create_folder() returned a NULL handle");
      return NULL;
    }

  *success = TRUE;
  return create_folder_data;
}

static gpointer
create_folder_no_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			       TestCallbackClosure *callback_closure)
{
  return create_folder_generic_setup (file_system, success, failure_reason, callback_closure);
}

static void
create_folder_cleanup (gpointer data, gboolean *success, char **failure_reason)
{
  struct create_folder_data *create_folder_data;

  create_folder_data = data;

  rmdir (CREATE_FOLDER_FILENAME);

  g_free (create_folder_data);

  *success = TRUE;
}

static gpointer
create_folder_with_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
				 TestCallbackClosure *callback_closure)
{
  struct create_folder_data *create_folder_data;

  create_folder_data = create_folder_generic_setup (file_system, success, failure_reason, callback_closure);

  if (*success)
    sleep_and_cancel_handle (create_folder_data->handle);

  return create_folder_data;
}

/* volume_mount */

struct volume_mount_data {
  TestCallbackClosure *callback_closure;
  GtkFileSystemVolume *volume;
  GtkFileSystemHandle *handle;
};

static void
volume_mount_cb (GtkFileSystemHandle *handle,
		 GtkFileSystemVolume *volume,
		 const GError        *error,
		 gpointer             data)
{
  struct volume_mount_data *volume_mount_data;

  volume_mount_data = data;
  notify_callback_called (volume_mount_data->callback_closure);
}

static gpointer
volume_mount_generic_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			    TestCallbackClosure *callback_closure)
{
  GtkFilePath *path;
  struct volume_mount_data *volume_mount_data;

  path = gtk_file_system_filename_to_path (file_system, VOLUME_MOUNT_FILENAME);
  if (!path)
    {
      *success = FALSE;
      *failure_reason = g_strdup_printf ("could not turn \"%s\" into a GtkFilePath", VOLUME_MOUNT_FILENAME);
      return NULL;
    }

  volume_mount_data = g_new (struct volume_mount_data, 1);

  volume_mount_data->callback_closure = callback_closure;
  volume_mount_data->volume = gtk_file_system_get_volume_for_path (file_system, path);
  gtk_file_path_free (path);

  if (!volume_mount_data->volume)
    {
      g_free (volume_mount_data);
      *success = FALSE;
      *failure_reason = g_strdup ("gtk_file_system_get_volume_for_path() returned a NULL volume");
      return NULL;
    }

  volume_mount_data->handle = gtk_file_system_volume_mount (file_system,
							    volume_mount_data->volume,
							    volume_mount_cb,
							    volume_mount_data);
  if (!volume_mount_data->handle)
    {
      g_object_unref (volume_mount_data->volume);
      g_free (volume_mount_data);
      *success = FALSE;
      *failure_reason = g_strdup ("gtk_file_system_volume_mount() returned a NULL handle");
      return NULL;
    }

  *success = TRUE;
  return volume_mount_data;
}

static gpointer
volume_mount_no_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
			      TestCallbackClosure *callback_closure)
{
  return volume_mount_generic_setup (file_system, success, failure_reason, callback_closure);
}

static void
volume_mount_cleanup (gpointer data, gboolean *success, char **failure_reason)
{
  struct volume_mount_data *volume_mount_data;

  volume_mount_data = data;

  g_object_unref (volume_mount_data->volume);
  g_free (volume_mount_data);

  *success = TRUE;
}

static gpointer
volume_mount_with_cancel_setup (GtkFileSystem *file_system, gboolean *success, char **failure_reason,
				TestCallbackClosure *callback_closure)
{
  struct volume_mount_data *volume_mount_data;

  volume_mount_data = volume_mount_generic_setup (file_system, success, failure_reason, callback_closure);

  if (*success)
    sleep_and_cancel_handle (volume_mount_data->handle);

  return volume_mount_data;
}

/* tests */

static TestSpec tests[] = {
  {
    "get_folder no cancel",
    TRUE,
    get_folder_no_cancel_setup,
    get_folder_cleanup
  },
  {
    "get_folder with cancel",
    FALSE,
    get_folder_with_cancel_setup,
    get_folder_cleanup
  },
  {
    "get_info no cancel",
    TRUE,
    get_info_no_cancel_setup,
    get_info_cleanup
  },
  {
    "get_info with cancel",
    FALSE,
    get_info_with_cancel_setup,
    get_info_cleanup
  },
  {
    "create_folder no cancel",
    TRUE,
    create_folder_no_cancel_setup,
    create_folder_cleanup
  },
  {
    "create_folder with cancel",
    FALSE,
    create_folder_with_cancel_setup,
    create_folder_cleanup
  },
  {
    "volume_mount no cancel",
    TRUE,
    volume_mount_no_cancel_setup,
    volume_mount_cleanup
  },
  {
    "volume_mount with cancel",
    FALSE,
    volume_mount_with_cancel_setup,
    volume_mount_cleanup
  }
};



/***** main *****/

static GLogFunc default_log_handler;
static int num_warnings;
static int num_errors;
static int num_critical_errors;

static void
log_override_cb (const gchar   *log_domain,
		 GLogLevelFlags log_level,
		 const gchar   *message,
		 gpointer       user_data)
{
  if (log_level & G_LOG_LEVEL_WARNING)
    num_warnings++;

  if (log_level & G_LOG_LEVEL_ERROR)
    num_errors++;

  if (log_level & G_LOG_LEVEL_CRITICAL)
    num_critical_errors++;

  (* default_log_handler) (log_domain, log_level, message, user_data);
}

static void
log_test (gboolean passed, const char *test_name, ...)
{
  va_list args;
  char *str;

  va_start (args, test_name);
  str = g_strdup_vprintf (test_name, args);
  va_end (args);

  g_printf ("%s: %s\n", passed ? "PASSED" : "FAILED", str);
  g_free (str);
}

int
main (int argc, char **argv)
{
  gboolean passed;
  gboolean zero_warnings;
  gboolean zero_errors;
  gboolean zero_critical_errors;

  default_log_handler = g_log_set_default_handler (log_override_cb, NULL);

  gtk_init (&argc, &argv);

  /* Start tests */

  passed = run_tests (tests, G_N_ELEMENTS (tests));

  /* Warnings and errors */

  zero_warnings = num_warnings == 0;
  zero_errors = num_errors == 0;
  zero_critical_errors = num_critical_errors == 0;

  log_test (zero_warnings, "main(): zero warnings (actual number %d)", num_warnings);
  log_test (zero_errors, "main(): zero errors (actual number %d)", num_errors);
  log_test (zero_critical_errors, "main(): zero critical errors (actual number %d)", num_critical_errors);

  /* Done */

  passed = passed && zero_warnings && zero_errors && zero_critical_errors;

  log_test (passed, "main(): ALL TESTS");

  return 0;
}