sl@0: /* sl@0: * Portions copyright (c) 2009 Nokia Corporation. All rights reserved. sl@0: */ sl@0: #undef G_DISABLE_ASSERT sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifdef __SYMBIAN32__ sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*__SYMBIAN32__*/ sl@0: sl@0: #define TEST_URI_0 "file:///abc/defgh/ijklmnopqrstuvwxyz" sl@0: #define TEST_URI_1 "file:///test/uri/1" sl@0: #define TEST_URI_2 "file:///test/uri/2" sl@0: sl@0: #define TEST_MIME "text/plain" sl@0: sl@0: #define TEST_APP_NAME "bookmarkfile-test" sl@0: #define TEST_APP_EXEC "bookmarkfile-test %f" sl@0: sl@0: static gboolean sl@0: test_load (GBookmarkFile *bookmark, sl@0: const gchar *filename) sl@0: { sl@0: GError *error = NULL; sl@0: gboolean res; sl@0: sl@0: res = g_bookmark_file_load_from_file (bookmark, filename, &error); sl@0: if (error) sl@0: { sl@0: g_print ("Load error: %s\n", error->message); sl@0: g_error_free (error); sl@0: } sl@0: sl@0: return res; sl@0: } sl@0: sl@0: static gboolean sl@0: test_query (GBookmarkFile *bookmark) sl@0: { sl@0: gint size; sl@0: gchar **uris; sl@0: gsize uris_len, i; sl@0: gboolean res = TRUE; sl@0: sl@0: size = g_bookmark_file_get_size (bookmark); sl@0: uris = g_bookmark_file_get_uris (bookmark, &uris_len); sl@0: sl@0: if (uris_len != size) sl@0: { sl@0: g_print ("URI/size mismatch: URI count is %d (should be %d)\n", uris_len, size); sl@0: sl@0: res = FALSE; sl@0: } sl@0: sl@0: for (i = 0; i < uris_len; i++) sl@0: if (!g_bookmark_file_has_item (bookmark, uris[i])) sl@0: { sl@0: g_print ("URI/bookmark mismatch: bookmark for '%s' does not exist\n", uris[i]); sl@0: sl@0: res = FALSE; sl@0: } sl@0: sl@0: g_strfreev (uris); sl@0: sl@0: return res; sl@0: } sl@0: sl@0: static gboolean sl@0: test_modify (GBookmarkFile *bookmark) sl@0: { sl@0: gchar *text; sl@0: guint count; sl@0: time_t stamp; sl@0: GError *error = NULL; sl@0: sl@0: g_print ("\t=> check global title/description..."); sl@0: g_bookmark_file_set_title (bookmark, NULL, "a file"); sl@0: g_bookmark_file_set_description (bookmark, NULL, "a bookmark file"); sl@0: sl@0: text = g_bookmark_file_get_title (bookmark, NULL, &error); sl@0: g_assert_no_error (error); sl@0: g_assert_cmpstr (text, ==, "a file"); sl@0: g_free (text); sl@0: sl@0: text = g_bookmark_file_get_description (bookmark, NULL, &error); sl@0: g_assert_no_error (error); sl@0: g_assert_cmpstr (text, ==, "a bookmark file"); sl@0: g_free (text); sl@0: g_print ("ok\n"); sl@0: sl@0: g_print ("\t=> check bookmark title/description..."); sl@0: g_bookmark_file_set_title (bookmark, TEST_URI_0, "a title"); sl@0: g_bookmark_file_set_description (bookmark, TEST_URI_0, "a description"); sl@0: sl@0: text = g_bookmark_file_get_title (bookmark, TEST_URI_0, &error); sl@0: g_assert_no_error (error); sl@0: g_assert_cmpstr (text, ==, "a title"); sl@0: g_free (text); sl@0: g_print ("ok\n"); sl@0: sl@0: g_print ("\t=> check non existing bookmark..."); sl@0: g_bookmark_file_get_description (bookmark, TEST_URI_1, &error); sl@0: g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); sl@0: g_clear_error (&error); sl@0: g_print ("ok\n"); sl@0: sl@0: g_print ("\t=> check application..."); sl@0: g_bookmark_file_set_mime_type (bookmark, TEST_URI_0, TEST_MIME); sl@0: g_bookmark_file_add_application (bookmark, TEST_URI_0, sl@0: TEST_APP_NAME, sl@0: TEST_APP_EXEC); sl@0: g_assert (g_bookmark_file_has_application (bookmark, TEST_URI_0, TEST_APP_NAME, NULL) == TRUE); sl@0: g_bookmark_file_get_app_info (bookmark, TEST_URI_0, TEST_APP_NAME, sl@0: &text, sl@0: &count, sl@0: &stamp, sl@0: &error); sl@0: g_assert_no_error (error); sl@0: g_assert (count == 1); sl@0: g_assert (stamp == g_bookmark_file_get_modified (bookmark, TEST_URI_0, NULL)); sl@0: g_free (text); sl@0: sl@0: g_bookmark_file_get_app_info (bookmark, TEST_URI_0, "fail", sl@0: &text, sl@0: &count, sl@0: &stamp, sl@0: &error); sl@0: g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED); sl@0: g_clear_error (&error); sl@0: g_print ("ok\n"); sl@0: sl@0: g_print ("\t=> check groups..."); sl@0: g_bookmark_file_add_group (bookmark, TEST_URI_1, "Test"); sl@0: g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Test", NULL) == TRUE); sl@0: g_assert (g_bookmark_file_has_group (bookmark, TEST_URI_1, "Fail", NULL) == FALSE); sl@0: g_print ("ok\n"); sl@0: sl@0: g_print ("\t=> check remove..."); sl@0: g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == TRUE); sl@0: g_assert_no_error (error); sl@0: g_assert (g_bookmark_file_remove_item (bookmark, TEST_URI_1, &error) == FALSE); sl@0: g_assert_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND); sl@0: g_clear_error (&error); sl@0: g_print ("ok\n"); sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: static gint sl@0: test_file (const gchar *filename) sl@0: { sl@0: GBookmarkFile *bookmark_file; sl@0: gboolean success; sl@0: sl@0: g_return_val_if_fail (filename != NULL, 1); sl@0: sl@0: g_print ("checking GBookmarkFile...\n"); sl@0: sl@0: bookmark_file = g_bookmark_file_new (); sl@0: g_assert (bookmark_file != NULL); sl@0: sl@0: success = test_load (bookmark_file, filename); sl@0: sl@0: if (success) sl@0: { sl@0: success = test_query (bookmark_file); sl@0: success = test_modify (bookmark_file); sl@0: } sl@0: sl@0: g_bookmark_file_free (bookmark_file); sl@0: sl@0: g_print ("ok\n"); sl@0: sl@0: return (success == TRUE ? 0 : 1); sl@0: } sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: #ifdef __SYMBIAN32__ sl@0: 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); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*__SYMBIAN32__*/ sl@0: sl@0: if (argc > 1){ sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("bookmarkfile-test"); sl@0: #endif /* EMULATOR */ sl@0: return test_file (argv[1]); sl@0: } sl@0: else sl@0: { sl@0: fprintf (stderr, "Usage: bookmarkfile-test \n"); sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("bookmarkfile-test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return 1; sl@0: } sl@0: }