glib/tests/bookmarkfile-test.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /*
       
     2 * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
       
     3 */
       
     4 #undef G_DISABLE_ASSERT
       
     5 
       
     6 #include <glib.h>
       
     7 #include <time.h>
       
     8 #include <locale.h>
       
     9 #include <string.h>
       
    10 #include <stdio.h>
       
    11 #include <stdlib.h>
       
    12 #ifdef __SYMBIAN32__
       
    13 #include "mrt2_glib2_test.h"
       
    14 #endif /*__SYMBIAN32__*/
       
    15 
       
    16 #define TEST_URI_0 	"file:///abc/defgh/ijklmnopqrstuvwxyz"
       
    17 #define TEST_URI_1 	"file:///test/uri/1"
       
    18 #define TEST_URI_2 	"file:///test/uri/2"
       
    19 
       
    20 #define TEST_MIME 	"text/plain"
       
    21 
       
    22 #define TEST_APP_NAME 	"bookmarkfile-test"
       
    23 #define TEST_APP_EXEC 	"bookmarkfile-test %f"
       
    24 
       
    25 static gboolean
       
    26 test_load (GBookmarkFile *bookmark,
       
    27            const gchar   *filename)
       
    28 {
       
    29   GError *error = NULL;
       
    30   gboolean res;
       
    31   
       
    32   res = g_bookmark_file_load_from_file (bookmark, filename, &error);
       
    33   if (error)
       
    34     {
       
    35       g_print ("Load error: %s\n", error->message);
       
    36       g_error_free (error);
       
    37     }
       
    38 
       
    39   return res;
       
    40 }
       
    41 
       
    42 static gboolean
       
    43 test_query (GBookmarkFile *bookmark)
       
    44 {
       
    45   gint size;
       
    46   gchar **uris;
       
    47   gsize uris_len, i;
       
    48   gboolean res = TRUE;
       
    49 
       
    50   size = g_bookmark_file_get_size (bookmark);
       
    51   uris = g_bookmark_file_get_uris (bookmark, &uris_len);
       
    52  
       
    53   if (uris_len != size)
       
    54     {
       
    55       g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size);
       
    56 
       
    57       res = FALSE;
       
    58     }
       
    59 
       
    60   for (i = 0; i < uris_len; i++)
       
    61     if (!g_bookmark_file_has_item (bookmark, uris[i]))
       
    62       {
       
    63         g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]);
       
    64 
       
    65 	res = FALSE;
       
    66       }
       
    67 
       
    68   g_strfreev (uris);
       
    69   
       
    70   return res;
       
    71 }
       
    72 
       
    73 static gboolean
       
    74 test_modify (GBookmarkFile *bookmark)
       
    75 {
       
    76   gchar *text;
       
    77   guint count;
       
    78   time_t stamp;
       
    79   GError *error = NULL;
       
    80   
       
    81   g_print ("\t=> check global title/description...");
       
    82   g_bookmark_file_set_title (bookmark, NULL, "a file");
       
    83   g_bookmark_file_set_description (bookmark, NULL, "a bookmark file");
       
    84   
       
    85   text = g_bookmark_file_get_title (bookmark, NULL, &error);
       
    86   g_assert_no_error (error);
       
    87   g_assert_cmpstr (text, ==, "a file");
       
    88   g_free (text);
       
    89 
       
    90   text = g_bookmark_file_get_description (bookmark, NULL, &error);
       
    91   g_assert_no_error (error);
       
    92   g_assert_cmpstr (text, ==, "a bookmark file");
       
    93   g_free (text);
       
    94   g_print ("ok\n");
       
    95 
       
    96   g_print ("\t=> check bookmark title/description...");
       
    97   g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title");
       
    98   g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description");
       
    99 
       
   100   text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error);
       
   101   g_assert_no_error (error);
       
   102   g_assert_cmpstr (text, ==, "a title");
       
   103   g_free (text);
       
   104   g_print ("ok\n");
       
   105 
       
   106   g_print ("\t=> check non existing bookmark...");
       
   107   g_bookmark_file_get_description (bookmark, TEST_URI_1, &error);
       
   108   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
       
   109   g_clear_error (&error);
       
   110   g_print ("ok\n");
       
   111   
       
   112   g_print ("\t=> check application...");
       
   113   g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME);
       
   114   g_bookmark_file_add_application (bookmark, TEST_URI_0,
       
   115 				   TEST_APP_NAME,
       
   116 				   TEST_APP_EXEC);
       
   117   g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL) == TRUE);
       
   118   g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME,
       
   119 		  		&text,
       
   120 				&count,
       
   121 				&stamp,
       
   122 				&error);
       
   123   g_assert_no_error (error);
       
   124   g_assert (count == 1);
       
   125   g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL));
       
   126   g_free (text);
       
   127   
       
   128   g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail",
       
   129 		  		&text,
       
   130 				&count,
       
   131 				&stamp,
       
   132 				&error);
       
   133   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED);
       
   134   g_clear_error (&error);
       
   135   g_print ("ok\n"); 
       
   136 
       
   137   g_print ("\t=> check groups...");
       
   138   g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test");
       
   139   g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL) == TRUE);
       
   140   g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL) == FALSE);
       
   141   g_print ("ok\n");
       
   142 
       
   143   g_print ("\t=> check remove...");
       
   144   g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE);
       
   145   g_assert_no_error (error);
       
   146   g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE);
       
   147   g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND);
       
   148   g_clear_error (&error);
       
   149   g_print ("ok\n");
       
   150   
       
   151   return TRUE;
       
   152 }
       
   153 
       
   154 static gint
       
   155 test_file (const gchar *filename)
       
   156 {
       
   157   GBookmarkFile *bookmark_file;
       
   158   gboolean success;
       
   159 
       
   160   g_return_val_if_fail (filename != NULL, 1);
       
   161 
       
   162   g_print ("checking GBookmarkFile...\n");
       
   163 
       
   164   bookmark_file = g_bookmark_file_new ();
       
   165   g_assert (bookmark_file != NULL);
       
   166 
       
   167   success = test_load (bookmark_file, filename);
       
   168   
       
   169   if (success)
       
   170     {
       
   171       success = test_query (bookmark_file);
       
   172       success = test_modify (bookmark_file);
       
   173     }
       
   174 
       
   175   g_bookmark_file_free (bookmark_file);
       
   176 
       
   177   g_print ("ok\n");
       
   178 
       
   179   return (success == TRUE ? 0 : 1);
       
   180 }
       
   181 
       
   182 int
       
   183 main (int   argc,
       
   184       char *argv[])
       
   185 {
       
   186   #ifdef __SYMBIAN32__
       
   187   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   188   g_set_print_handler(mrtPrintHandler);
       
   189   #endif /*__SYMBIAN32__*/
       
   190 
       
   191   if (argc > 1){
       
   192 	   #ifdef __SYMBIAN32__
       
   193   testResultXml("bookmarkfile-test");
       
   194   #endif /* EMULATOR */
       
   195   return test_file (argv[1]);
       
   196   }
       
   197   else
       
   198     {
       
   199       fprintf (stderr, "Usage: bookmarkfile-test <bookmarkfile>\n");
       
   200 	  #ifdef __SYMBIAN32__
       
   201   testResultXml("bookmarkfile-test");
       
   202   #endif /* EMULATOR */
       
   203   
       
   204       return 1;
       
   205     }
       
   206 }