1 /* GLib testing framework examples and tests 2 * Copyright (C) 2008 Red Hat, Inc. 3 * Authors: Tomas Bzatek <tbzatek (at) redhat.com> 4 * 5 * This work is provided "as is"; redistribution and modification 6 * in whole or in part, in any medium, physical or electronic is 7 * permitted without restriction. 8 * 9 * This work is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 * In no event shall the authors or contributors be liable for any 14 * direct, indirect, incidental, special, exemplary, or consequential 15 * damages (including, but not limited to, procurement of substitute 16 * goods or services; loss of use, data, or profits; or business 17 * interruption) however caused and on any theory of liability, whether 18 * in contract, strict liability, or tort (including negligence or 19 * otherwise) arising in any way out of the use of this software, even 20 * if advised of the possibility of such damage. 21 */ 22 23 #include <glib/glib.h> 24 #include <gio/gio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #define TEST_NAME "Prilis zlutoucky kun" 29 #define TEST_DISPLAY_NAME "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88" 30 #define TEST_SIZE 0xFFFFFFF0 31 32 static void 33 test_assigned_values (GFileInfo *info) 34 { 35 const char *name, *display_name, *mistake; 36 guint64 size; 37 GFileType type; 38 39 /* Test for attributes presence */ 40 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_NAME) == TRUE); 41 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME) == TRUE); 42 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE) == TRUE); 43 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME) == FALSE); 44 45 /* Retrieve data back and compare */ 46 47 name = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME); 48 display_name = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME); 49 mistake = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME); 50 size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE); 51 type = g_file_info_get_file_type (info); 52 53 g_assert_cmpstr (name, ==, TEST_NAME); 54 g_assert_cmpstr (display_name, ==, TEST_DISPLAY_NAME); 55 g_assert (mistake == NULL); 56 g_assert_cmpint (size, ==, TEST_SIZE); 57 g_assert_cmpstr (name, ==, g_file_info_get_name (info)); 58 g_assert_cmpstr (display_name, ==, g_file_info_get_display_name (info)); 59 g_assert_cmpint (size, ==, g_file_info_get_size (info) ); 60 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY); 61 } 62 63 64 65 static void 66 test_g_file_info (void) 67 { 68 GFileInfo *info; 69 GFileInfo *info_dup; 70 GFileInfo *info_copy; 71 char **attr_list; 72 73 info = g_file_info_new (); 74 75 /* Test for empty instance */ 76 attr_list = g_file_info_list_attributes (info, NULL); 77 g_assert (attr_list != NULL); 78 g_assert (*attr_list == NULL); 79 80 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME, TEST_NAME); 81 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, TEST_DISPLAY_NAME); 82 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE, TEST_SIZE); 83 g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY); 84 85 /* The attr list should not be empty now */ 86 attr_list = g_file_info_list_attributes (info, NULL); 87 g_assert (attr_list != NULL); 88 g_assert (*attr_list != NULL); 89 90 test_assigned_values (info); 91 92 /* Test dups */ 93 info_dup = g_file_info_dup (info); 94 g_assert (info_dup != NULL); 95 test_assigned_values (info_dup); 96 97 info_copy = g_file_info_new (); 98 g_file_info_copy_into (info_dup, info_copy); 99 g_assert (info_copy != NULL); 100 test_assigned_values (info_copy); 101 102 /* Test remove attribute */ 103 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == FALSE); 104 g_file_info_set_attribute_int32 (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER, 10); 105 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == TRUE); 106 107 g_file_info_remove_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER); 108 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == FALSE); 109 110 g_object_unref (info); 111 g_object_unref (info_dup); 112 g_object_unref (info_copy); 113 } 114 115 int 116 main (int argc, 117 char *argv[]) 118 { 119 g_type_init (); 120 g_test_init (&argc, &argv, NULL); 121 122 g_test_add_func ("/g-file-info/test_g_file_info", test_g_file_info); 123 124 return g_test_run(); 125 } 126