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