From 27366c135e04f7d030929df5db32d7c2b1e63e9b Mon Sep 17 00:00:00 2001 From: Xan Lopez Date: Fri, 2 Mar 2012 13:49:12 +0100 Subject: Rename the new tests to be foo-test.c --- tests/Makefile.am | 4 +- tests/ephy-history-test.c | 448 ++++++++++++++++++++++++++++++++++++++++++++++ tests/ephy-history.c | 448 ---------------------------------------------- tests/ephy-sqlite-test.c | 213 ++++++++++++++++++++++ tests/ephy-sqlite.c | 213 ---------------------- 5 files changed, 663 insertions(+), 663 deletions(-) create mode 100644 tests/ephy-history-test.c delete mode 100644 tests/ephy-history.c create mode 100644 tests/ephy-sqlite-test.c delete mode 100644 tests/ephy-sqlite.c diff --git a/tests/Makefile.am b/tests/Makefile.am index addca34e3..706fe66c6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -119,7 +119,7 @@ test_ephy_embed_utils_SOURCES = \ ephy-embed-utils-test.c test_ephy_history_SOURCES = \ - ephy-history.c + ephy-history-test.c test_ephy_location_entry_SOURCES = \ ephy-location-entry-test.c @@ -128,4 +128,4 @@ test_ephy_search_entry_SOURCES = \ ephy-search-entry-test.c test_ephy_sqlite_SOURCES = \ - ephy-sqlite.c + ephy-sqlite-test.c diff --git a/tests/ephy-history-test.c b/tests/ephy-history-test.c new file mode 100644 index 000000000..4abd1f064 --- /dev/null +++ b/tests/ephy-history-test.c @@ -0,0 +1,448 @@ +/* vim: set sw=2 ts=2 sts=2 et: */ +/* + * ephy-sqlite-statement.c + * This file is part of Epiphany + * + * Copyright © 2010 Igalia S.L. + * + * Epiphany is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Epiphany 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Epiphany; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "ephy-history-service.h" +#include +#include +#include + +static EphyHistoryService * +ensure_empty_history (const char* filename) +{ + if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) + g_unlink (filename); + + return ephy_history_service_new (filename); +} + +static void +test_create_history_service (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history (temporary_file); + + g_free (temporary_file); + g_object_unref (service); +} + +static gboolean +destroy_history_service_and_end_main_loop (EphyHistoryService *service) +{ + g_object_unref (service); + g_assert (TRUE); + gtk_main_quit (); + + return FALSE; +} + +static void +test_create_history_service_and_destroy_later (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history (temporary_file); + g_free (temporary_file); + g_timeout_add (100, (GSourceFunc) destroy_history_service_and_end_main_loop, service); + + gtk_main (); +} + +static void +page_vist_created (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + g_object_unref (service); + g_assert (result_data == NULL); + g_assert (user_data == NULL); + g_assert (success); + gtk_main_quit (); +} + +static void +test_create_history_entry (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history(temporary_file); + + EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED); + ephy_history_service_add_visit (service, visit, page_vist_created, NULL); + ephy_history_page_visit_free (visit); + g_free (temporary_file); + + gtk_main (); +} + +static GList * +create_test_page_visit_list () +{ + GList *visits = NULL; + int i; + for (i = 0; i < 100; i++) { + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 3, EPHY_PAGE_VISIT_TYPED)); + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 5, EPHY_PAGE_VISIT_TYPED)); + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.cuteoverload.com", 7, EPHY_PAGE_VISIT_TYPED)); + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.cuteoverload.com", 8, EPHY_PAGE_VISIT_TYPED)); + } + return visits; +} + +static void +verify_create_history_entry_cb (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + GList *visits = (GList *) result_data; + GList *baseline_visits = create_test_page_visit_list (); + GList *current = visits; + GList *current_baseline = baseline_visits; + + g_assert (user_data == NULL); + g_assert (success); + g_assert (visits != NULL); + g_assert_cmpint (g_list_length (visits), ==, g_list_length (baseline_visits)); + + while (current_baseline) { + EphyHistoryPageVisit *visit, *baseline_visit; + + g_assert (current); + visit = (EphyHistoryPageVisit *) current->data; + baseline_visit = (EphyHistoryPageVisit *) current_baseline->data; + + g_assert_cmpstr (visit->url->url, ==, baseline_visit->url->url); + g_assert_cmpstr (visit->url->title, ==, baseline_visit->url->title); + g_assert_cmpint (visit->visit_time, ==, baseline_visit->visit_time); + g_assert_cmpint (visit->visit_type, ==, baseline_visit->visit_type); + + current = current->next; + current_baseline = current_baseline->next; + } + + ephy_history_page_visit_list_free (visits); + ephy_history_page_visit_list_free (baseline_visits); + + g_object_unref (service); + gtk_main_quit (); +} + +static void +verify_create_history_entry (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + g_assert (result_data == NULL); + g_assert_cmpint (42, ==, GPOINTER_TO_INT(user_data)); + g_assert (success); + ephy_history_service_find_visits_in_time (service, 0, 8, verify_create_history_entry_cb, NULL); +} + +static void +test_create_history_entries (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history(temporary_file); + + GList *visits = create_test_page_visit_list (); + + /* We use 42 here just to verify that user_data is passed properly to the callback */ + ephy_history_service_add_visits (service, visits, verify_create_history_entry, GINT_TO_POINTER(42)); + ephy_history_page_visit_list_free (visits); + g_free (temporary_file); + + gtk_main (); +} + +static void +get_url (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + EphyHistoryURL *url = (EphyHistoryURL *) result_data; + + g_assert (success == TRUE); + g_assert (url != NULL); + g_assert_cmpstr (url->title, ==, "GNOME"); + + ephy_history_url_free (url); + g_object_unref (service); + gtk_main_quit(); +} + +static void +set_url_title (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + gboolean test_result = GPOINTER_TO_INT (user_data); + g_assert (success == TRUE); + + if (test_result == FALSE) { + g_object_unref (service); + gtk_main_quit (); + } else + ephy_history_service_get_url (service, "http://www.gnome.org", get_url, NULL); +} + +static void +set_url_title_visit_created (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + ephy_history_service_set_url_title (service, "http://www.gnome.org", "GNOME", set_url_title, user_data); +} + +static void +test_set_url_title_helper (gboolean test_results) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history(temporary_file); + + EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED); + ephy_history_service_add_visit (service, visit, set_url_title_visit_created, GINT_TO_POINTER (test_results)); + ephy_history_page_visit_free (visit); + g_free (temporary_file); + + gtk_main (); +} + +static void +test_set_url_title (void) +{ + test_set_url_title_helper (FALSE); +} + +static void +test_set_url_title_is_correct (void) +{ + test_set_url_title_helper (TRUE); +} + +static void +set_url_title_url_not_existent (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + g_assert (success == FALSE); + g_object_unref (service); + gtk_main_quit (); +} + +static void +test_set_url_title_url_not_existent (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history(temporary_file); + g_free (temporary_file); + + ephy_history_service_set_url_title (service, "http://www.gnome.org", "GNOME", set_url_title_url_not_existent, NULL); + + gtk_main(); +} + +static void +test_get_url_done (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + EphyHistoryURL *url; + gboolean expected_success = GPOINTER_TO_INT (user_data); + + url = (EphyHistoryURL *)result_data; + + g_assert (success == expected_success); + + if (expected_success == TRUE) { + g_assert (url != NULL); + g_assert_cmpstr (url->url, ==, "http://www.gnome.org"); + g_assert_cmpint (url->id, !=, -1); + ephy_history_url_free (url); + } else + g_assert (url == NULL); + + g_object_unref (service); + gtk_main_quit (); +} + +static void +test_get_url_visit_added (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) +{ + g_assert (success == TRUE); + + ephy_history_service_get_url (service, "http://www.gnome.org", test_get_url_done, user_data); +} + +static void +test_get_url_helper (gboolean add_entry) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history(temporary_file); + g_free (temporary_file); + + if (add_entry == TRUE) { + EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED); + ephy_history_service_add_visit (service, visit, test_get_url_visit_added, GINT_TO_POINTER (add_entry)); + ephy_history_page_visit_free (visit); + } else + ephy_history_service_get_url (service, "http://www.gnome.org", test_get_url_done, GINT_TO_POINTER (add_entry)); + + gtk_main(); +} + +static void +test_get_url (void) +{ + test_get_url_helper (TRUE); +} + +static void +test_get_url_not_existent (void) +{ + test_get_url_helper (FALSE); +} + +static GList * +create_visits_for_complex_tests (void) +{ + int i; + GList *visits = NULL; + + for (i = 0; i < 10; i++) + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); + for (i = 0; i < 30; i++) + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.wikipedia.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); + for (i = 0; i < 20; i++) + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.freedesktop.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); + for (i = 0; i < 5; i++) + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.musicbrainz.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); + for (i = 0; i < 2; i++) + visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.webkitgtk.org", 1000 * i, EPHY_PAGE_VISIT_TYPED)); + + return visits; +} + +static void +verify_complex_url_query (EphyHistoryService *service, + gboolean success, + gpointer result_data, + gpointer user_data) +{ + EphyHistoryURL *url, *baseline; + GList *urls = (GList*) result_data; + + /* Only one result expected. */ + g_assert_cmpint (g_list_length (urls), ==, 1); + + url = (EphyHistoryURL *) urls->data; + baseline = (EphyHistoryURL *) user_data; + + g_assert_cmpstr (url->url, ==, baseline->url); + g_assert_cmpuint (url->visit_count, ==, baseline->visit_count); + + g_object_unref (service); + + gtk_main_quit(); +} + +static void +perform_complex_url_query (EphyHistoryService *service, + gboolean success, + gpointer result_data, + gpointer user_data) +{ + EphyHistoryQuery *query; + EphyHistoryURL *url; + + g_assert (success == TRUE); + + /* Get the most visited site that contains 'k'. */ + query = ephy_history_query_new (); + query->substring_list = g_list_prepend (query->substring_list, "k"); + query->limit = 1; + query->sort_type = EPHY_HISTORY_SORT_MV; + + /* The expected result. */ + url = ephy_history_url_new ("http://www.wikipedia.org", + "Wikipedia", + 30, 30, 0); + + ephy_history_service_query_urls (service, query, verify_complex_url_query, url); +} + +static void +test_complex_url_query (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history(temporary_file); + GList *visits; + + visits = create_visits_for_complex_tests (); + + ephy_history_service_add_visits (service, visits, perform_complex_url_query, NULL); + + gtk_main (); +} + +static void +perform_complex_url_query_with_time_range (EphyHistoryService *service, + gboolean success, + gpointer result_data, + gpointer user_data) +{ + EphyHistoryQuery *query; + EphyHistoryURL *url; + + g_assert (success == TRUE); + + /* Get the most visited site that contains 'k' that was visited since timestamp 500. */ + query = ephy_history_query_new (); + query->substring_list = g_list_prepend (query->substring_list, "k"); + query->limit = 1; + query->sort_type = EPHY_HISTORY_SORT_MV; + query->from = 500; + + /* The expected result. */ + url = ephy_history_url_new ("http://www.webkitgtk.org", + "WebKitGTK+", + 2, 2, 0); + + ephy_history_service_query_urls (service, query, verify_complex_url_query, url); +} + +static void +test_complex_url_query_with_time_range (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); + EphyHistoryService *service = ensure_empty_history(temporary_file); + GList *visits; + + visits = create_visits_for_complex_tests (); + + ephy_history_service_add_visits (service, visits, perform_complex_url_query_with_time_range, NULL); + + gtk_main (); +} + +int +main (int argc, char *argv[]) +{ + gtk_test_init (&argc, &argv); + + g_test_add_func ("/embed/history/test_create_history_service", test_create_history_service); + g_test_add_func ("/embed/history/test_create_history_service_and_destroy_later", test_create_history_service_and_destroy_later); + g_test_add_func ("/embed/history/test_create_history_entry", test_create_history_entry); + g_test_add_func ("/embed/history/test_create_history_entries", test_create_history_entries); + g_test_add_func ("/embed/history/test_set_url_title", test_set_url_title); + g_test_add_func ("/embed/history/test_set_url_title_is_correct", test_set_url_title_is_correct); + g_test_add_func ("/embed/history/test_set_url_title_url_not_existent", test_set_url_title_url_not_existent); + g_test_add_func ("/embed/history/test_get_url", test_get_url); + g_test_add_func ("/embed/history/test_get_url_not_existent", test_get_url_not_existent); + g_test_add_func ("/embed/history/test_complex_url_query", test_complex_url_query); + g_test_add_func ("/embed/history/test_complex_url_query_with_time_range", test_complex_url_query_with_time_range); + + return g_test_run (); +} diff --git a/tests/ephy-history.c b/tests/ephy-history.c deleted file mode 100644 index 4abd1f064..000000000 --- a/tests/ephy-history.c +++ /dev/null @@ -1,448 +0,0 @@ -/* vim: set sw=2 ts=2 sts=2 et: */ -/* - * ephy-sqlite-statement.c - * This file is part of Epiphany - * - * Copyright © 2010 Igalia S.L. - * - * Epiphany is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Epiphany 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Epiphany; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301 USA - */ - -#include "config.h" - -#include "ephy-history-service.h" -#include -#include -#include - -static EphyHistoryService * -ensure_empty_history (const char* filename) -{ - if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) - g_unlink (filename); - - return ephy_history_service_new (filename); -} - -static void -test_create_history_service (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history (temporary_file); - - g_free (temporary_file); - g_object_unref (service); -} - -static gboolean -destroy_history_service_and_end_main_loop (EphyHistoryService *service) -{ - g_object_unref (service); - g_assert (TRUE); - gtk_main_quit (); - - return FALSE; -} - -static void -test_create_history_service_and_destroy_later (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history (temporary_file); - g_free (temporary_file); - g_timeout_add (100, (GSourceFunc) destroy_history_service_and_end_main_loop, service); - - gtk_main (); -} - -static void -page_vist_created (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - g_object_unref (service); - g_assert (result_data == NULL); - g_assert (user_data == NULL); - g_assert (success); - gtk_main_quit (); -} - -static void -test_create_history_entry (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history(temporary_file); - - EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED); - ephy_history_service_add_visit (service, visit, page_vist_created, NULL); - ephy_history_page_visit_free (visit); - g_free (temporary_file); - - gtk_main (); -} - -static GList * -create_test_page_visit_list () -{ - GList *visits = NULL; - int i; - for (i = 0; i < 100; i++) { - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 3, EPHY_PAGE_VISIT_TYPED)); - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 5, EPHY_PAGE_VISIT_TYPED)); - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.cuteoverload.com", 7, EPHY_PAGE_VISIT_TYPED)); - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.cuteoverload.com", 8, EPHY_PAGE_VISIT_TYPED)); - } - return visits; -} - -static void -verify_create_history_entry_cb (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - GList *visits = (GList *) result_data; - GList *baseline_visits = create_test_page_visit_list (); - GList *current = visits; - GList *current_baseline = baseline_visits; - - g_assert (user_data == NULL); - g_assert (success); - g_assert (visits != NULL); - g_assert_cmpint (g_list_length (visits), ==, g_list_length (baseline_visits)); - - while (current_baseline) { - EphyHistoryPageVisit *visit, *baseline_visit; - - g_assert (current); - visit = (EphyHistoryPageVisit *) current->data; - baseline_visit = (EphyHistoryPageVisit *) current_baseline->data; - - g_assert_cmpstr (visit->url->url, ==, baseline_visit->url->url); - g_assert_cmpstr (visit->url->title, ==, baseline_visit->url->title); - g_assert_cmpint (visit->visit_time, ==, baseline_visit->visit_time); - g_assert_cmpint (visit->visit_type, ==, baseline_visit->visit_type); - - current = current->next; - current_baseline = current_baseline->next; - } - - ephy_history_page_visit_list_free (visits); - ephy_history_page_visit_list_free (baseline_visits); - - g_object_unref (service); - gtk_main_quit (); -} - -static void -verify_create_history_entry (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - g_assert (result_data == NULL); - g_assert_cmpint (42, ==, GPOINTER_TO_INT(user_data)); - g_assert (success); - ephy_history_service_find_visits_in_time (service, 0, 8, verify_create_history_entry_cb, NULL); -} - -static void -test_create_history_entries (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history(temporary_file); - - GList *visits = create_test_page_visit_list (); - - /* We use 42 here just to verify that user_data is passed properly to the callback */ - ephy_history_service_add_visits (service, visits, verify_create_history_entry, GINT_TO_POINTER(42)); - ephy_history_page_visit_list_free (visits); - g_free (temporary_file); - - gtk_main (); -} - -static void -get_url (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - EphyHistoryURL *url = (EphyHistoryURL *) result_data; - - g_assert (success == TRUE); - g_assert (url != NULL); - g_assert_cmpstr (url->title, ==, "GNOME"); - - ephy_history_url_free (url); - g_object_unref (service); - gtk_main_quit(); -} - -static void -set_url_title (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - gboolean test_result = GPOINTER_TO_INT (user_data); - g_assert (success == TRUE); - - if (test_result == FALSE) { - g_object_unref (service); - gtk_main_quit (); - } else - ephy_history_service_get_url (service, "http://www.gnome.org", get_url, NULL); -} - -static void -set_url_title_visit_created (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - ephy_history_service_set_url_title (service, "http://www.gnome.org", "GNOME", set_url_title, user_data); -} - -static void -test_set_url_title_helper (gboolean test_results) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history(temporary_file); - - EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED); - ephy_history_service_add_visit (service, visit, set_url_title_visit_created, GINT_TO_POINTER (test_results)); - ephy_history_page_visit_free (visit); - g_free (temporary_file); - - gtk_main (); -} - -static void -test_set_url_title (void) -{ - test_set_url_title_helper (FALSE); -} - -static void -test_set_url_title_is_correct (void) -{ - test_set_url_title_helper (TRUE); -} - -static void -set_url_title_url_not_existent (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - g_assert (success == FALSE); - g_object_unref (service); - gtk_main_quit (); -} - -static void -test_set_url_title_url_not_existent (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history(temporary_file); - g_free (temporary_file); - - ephy_history_service_set_url_title (service, "http://www.gnome.org", "GNOME", set_url_title_url_not_existent, NULL); - - gtk_main(); -} - -static void -test_get_url_done (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - EphyHistoryURL *url; - gboolean expected_success = GPOINTER_TO_INT (user_data); - - url = (EphyHistoryURL *)result_data; - - g_assert (success == expected_success); - - if (expected_success == TRUE) { - g_assert (url != NULL); - g_assert_cmpstr (url->url, ==, "http://www.gnome.org"); - g_assert_cmpint (url->id, !=, -1); - ephy_history_url_free (url); - } else - g_assert (url == NULL); - - g_object_unref (service); - gtk_main_quit (); -} - -static void -test_get_url_visit_added (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data) -{ - g_assert (success == TRUE); - - ephy_history_service_get_url (service, "http://www.gnome.org", test_get_url_done, user_data); -} - -static void -test_get_url_helper (gboolean add_entry) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history(temporary_file); - g_free (temporary_file); - - if (add_entry == TRUE) { - EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED); - ephy_history_service_add_visit (service, visit, test_get_url_visit_added, GINT_TO_POINTER (add_entry)); - ephy_history_page_visit_free (visit); - } else - ephy_history_service_get_url (service, "http://www.gnome.org", test_get_url_done, GINT_TO_POINTER (add_entry)); - - gtk_main(); -} - -static void -test_get_url (void) -{ - test_get_url_helper (TRUE); -} - -static void -test_get_url_not_existent (void) -{ - test_get_url_helper (FALSE); -} - -static GList * -create_visits_for_complex_tests (void) -{ - int i; - GList *visits = NULL; - - for (i = 0; i < 10; i++) - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); - for (i = 0; i < 30; i++) - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.wikipedia.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); - for (i = 0; i < 20; i++) - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.freedesktop.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); - for (i = 0; i < 5; i++) - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.musicbrainz.org", 10 * i, EPHY_PAGE_VISIT_TYPED)); - for (i = 0; i < 2; i++) - visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.webkitgtk.org", 1000 * i, EPHY_PAGE_VISIT_TYPED)); - - return visits; -} - -static void -verify_complex_url_query (EphyHistoryService *service, - gboolean success, - gpointer result_data, - gpointer user_data) -{ - EphyHistoryURL *url, *baseline; - GList *urls = (GList*) result_data; - - /* Only one result expected. */ - g_assert_cmpint (g_list_length (urls), ==, 1); - - url = (EphyHistoryURL *) urls->data; - baseline = (EphyHistoryURL *) user_data; - - g_assert_cmpstr (url->url, ==, baseline->url); - g_assert_cmpuint (url->visit_count, ==, baseline->visit_count); - - g_object_unref (service); - - gtk_main_quit(); -} - -static void -perform_complex_url_query (EphyHistoryService *service, - gboolean success, - gpointer result_data, - gpointer user_data) -{ - EphyHistoryQuery *query; - EphyHistoryURL *url; - - g_assert (success == TRUE); - - /* Get the most visited site that contains 'k'. */ - query = ephy_history_query_new (); - query->substring_list = g_list_prepend (query->substring_list, "k"); - query->limit = 1; - query->sort_type = EPHY_HISTORY_SORT_MV; - - /* The expected result. */ - url = ephy_history_url_new ("http://www.wikipedia.org", - "Wikipedia", - 30, 30, 0); - - ephy_history_service_query_urls (service, query, verify_complex_url_query, url); -} - -static void -test_complex_url_query (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history(temporary_file); - GList *visits; - - visits = create_visits_for_complex_tests (); - - ephy_history_service_add_visits (service, visits, perform_complex_url_query, NULL); - - gtk_main (); -} - -static void -perform_complex_url_query_with_time_range (EphyHistoryService *service, - gboolean success, - gpointer result_data, - gpointer user_data) -{ - EphyHistoryQuery *query; - EphyHistoryURL *url; - - g_assert (success == TRUE); - - /* Get the most visited site that contains 'k' that was visited since timestamp 500. */ - query = ephy_history_query_new (); - query->substring_list = g_list_prepend (query->substring_list, "k"); - query->limit = 1; - query->sort_type = EPHY_HISTORY_SORT_MV; - query->from = 500; - - /* The expected result. */ - url = ephy_history_url_new ("http://www.webkitgtk.org", - "WebKitGTK+", - 2, 2, 0); - - ephy_history_service_query_urls (service, query, verify_complex_url_query, url); -} - -static void -test_complex_url_query_with_time_range (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL); - EphyHistoryService *service = ensure_empty_history(temporary_file); - GList *visits; - - visits = create_visits_for_complex_tests (); - - ephy_history_service_add_visits (service, visits, perform_complex_url_query_with_time_range, NULL); - - gtk_main (); -} - -int -main (int argc, char *argv[]) -{ - gtk_test_init (&argc, &argv); - - g_test_add_func ("/embed/history/test_create_history_service", test_create_history_service); - g_test_add_func ("/embed/history/test_create_history_service_and_destroy_later", test_create_history_service_and_destroy_later); - g_test_add_func ("/embed/history/test_create_history_entry", test_create_history_entry); - g_test_add_func ("/embed/history/test_create_history_entries", test_create_history_entries); - g_test_add_func ("/embed/history/test_set_url_title", test_set_url_title); - g_test_add_func ("/embed/history/test_set_url_title_is_correct", test_set_url_title_is_correct); - g_test_add_func ("/embed/history/test_set_url_title_url_not_existent", test_set_url_title_url_not_existent); - g_test_add_func ("/embed/history/test_get_url", test_get_url); - g_test_add_func ("/embed/history/test_get_url_not_existent", test_get_url_not_existent); - g_test_add_func ("/embed/history/test_complex_url_query", test_complex_url_query); - g_test_add_func ("/embed/history/test_complex_url_query_with_time_range", test_complex_url_query_with_time_range); - - return g_test_run (); -} diff --git a/tests/ephy-sqlite-test.c b/tests/ephy-sqlite-test.c new file mode 100644 index 000000000..309f7cfb5 --- /dev/null +++ b/tests/ephy-sqlite-test.c @@ -0,0 +1,213 @@ +/* + * ephy-sqlite-statement.c + * This file is part of Epiphany + * + * Copyright © 2011 Igalia S.L. + * + * Epiphany is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Epiphany 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Epiphany; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "ephy-sqlite-connection.h" +#include "ephy-sqlite-statement.h" +#include +#include +#include + +static EphySQLiteConnection * +ensure_empty_database (const char* filename) +{ + EphySQLiteConnection *connection = ephy_sqlite_connection_new (); + GError *error = NULL; + + if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) + g_unlink (filename); + + g_assert (ephy_sqlite_connection_open (connection, filename, &error)); + g_assert (!error); + return connection; +} + +static void +test_create_connection (void) +{ + GError *error = NULL; + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + + EphySQLiteConnection *connection = ensure_empty_database (temporary_file); + ephy_sqlite_connection_close (connection); + + g_assert ( g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); + g_unlink (temporary_file); + g_assert ( !g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); + g_free (temporary_file); + + temporary_file = g_build_filename (g_get_tmp_dir (), "directory-that-does-not-exist", "epiphany_sqlite_test.db", NULL); + g_assert (!ephy_sqlite_connection_open (connection, temporary_file, &error)); + g_assert (error); + g_assert (!g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); + g_object_unref (connection); +} + + +static void +test_create_statement (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + EphySQLiteConnection* connection = ensure_empty_database (temporary_file); + GError *error = NULL; + EphySQLiteStatement *statement = NULL; + + statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE TEST (id INTEGER)", &error); + g_assert (statement); + g_assert (!error); + g_object_unref (statement); + + statement = ephy_sqlite_connection_create_statement (connection, "BLAHBLAHBLAHBA", &error); + g_assert (!statement); + g_assert (error); + + ephy_sqlite_connection_close (connection); + g_unlink (temporary_file); + g_free (temporary_file); + + g_object_unref (connection); +} + +static void +create_table_and_insert_row (EphySQLiteConnection *connection) +{ + GError *error = NULL; + EphySQLiteStatement *statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error); + g_assert (statement); + g_assert (!error); + ephy_sqlite_statement_step (statement, &error); + g_assert (!error); + g_object_unref (statement); + + statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error); + g_assert (statement); + g_assert (!error); + g_assert (!ephy_sqlite_statement_step (statement, &error)); + g_assert (!error); + g_object_unref (statement); + + statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (3, \"test\")", &error); + g_assert (statement); + g_assert (!error); + ephy_sqlite_statement_step (statement, &error); + g_assert (!error); + g_object_unref (statement); + + statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error); + g_assert (statement); + g_assert (!error); + + g_assert (ephy_sqlite_statement_step (statement, &error)); + g_assert (!error); + + g_assert_cmpint (ephy_sqlite_connection_get_last_insert_id (connection), ==, 1); + g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2); + g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 0), ==, EPHY_SQLITE_COLUMN_TYPE_INT); + g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 1), ==, EPHY_SQLITE_COLUMN_TYPE_STRING); + + /* Step will return false here since there is only one row. */ + g_assert (!ephy_sqlite_statement_step (statement, &error)); + g_object_unref (statement); +} + +static void +test_create_table_and_insert_row (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + EphySQLiteConnection* connection = ensure_empty_database (temporary_file); + + create_table_and_insert_row (connection); + + g_object_unref (connection); + g_unlink (temporary_file); + g_free (temporary_file); +} + +static void +test_bind_data (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + EphySQLiteConnection* connection = ensure_empty_database (temporary_file); + GError *error = NULL; + EphySQLiteStatement *statement = NULL; + + ephy_sqlite_connection_execute (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error); + + statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (?, ?)", &error); + g_assert (statement); + g_assert (!error); + + g_assert (ephy_sqlite_statement_bind_int (statement, 0, 3, &error)); + g_assert (!error); + g_assert (ephy_sqlite_statement_bind_string (statement, 1, "foo", &error)); + g_assert (!error); + + /* Will return false since there are no resulting rows. */ + g_assert (!ephy_sqlite_statement_step (statement, &error)); + g_assert (!error); + g_object_unref (statement); + + statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error); + g_assert (statement); + g_assert (!error); + g_assert (ephy_sqlite_statement_step (statement, &error)); + g_assert (!error); + g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2); + g_assert_cmpint (ephy_sqlite_statement_get_column_as_int (statement, 0), ==, 3); + g_assert_cmpstr (ephy_sqlite_statement_get_column_as_string (statement, 1), ==, "foo"); + + g_object_unref (connection); + g_unlink (temporary_file); + g_free (temporary_file); +} + +static void +test_table_exists (void) +{ + gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + EphySQLiteConnection* connection = ensure_empty_database (temporary_file); + + g_assert (!ephy_sqlite_connection_table_exists (connection, "test")); + g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey")); + create_table_and_insert_row (connection); + g_assert (ephy_sqlite_connection_table_exists (connection, "test")); + g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey")); + + g_object_unref (connection); + g_unlink (temporary_file); + g_free (temporary_file); +} + +int +main (int argc, char *argv[]) +{ + gtk_test_init (&argc, &argv); + + g_test_add_func ("/lib/sqlite/ephy-sqlite/create_connection", test_create_connection); + g_test_add_func ("/lib/sqlite/ephy-sqlite/create_statement", test_create_statement); + g_test_add_func ("/lib/sqlite/ephy-sqlite/create_table_and_insert_row", test_create_table_and_insert_row); + g_test_add_func ("/lib/sqlite/ephy-sqlite/bind_data", test_bind_data); + g_test_add_func ("/lib/sqlite/ephy-sqlite/table_exists", test_table_exists); + + return g_test_run (); +} diff --git a/tests/ephy-sqlite.c b/tests/ephy-sqlite.c deleted file mode 100644 index 309f7cfb5..000000000 --- a/tests/ephy-sqlite.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * ephy-sqlite-statement.c - * This file is part of Epiphany - * - * Copyright © 2011 Igalia S.L. - * - * Epiphany is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Epiphany 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Epiphany; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301 USA - */ - -#include "config.h" - -#include "ephy-sqlite-connection.h" -#include "ephy-sqlite-statement.h" -#include -#include -#include - -static EphySQLiteConnection * -ensure_empty_database (const char* filename) -{ - EphySQLiteConnection *connection = ephy_sqlite_connection_new (); - GError *error = NULL; - - if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) - g_unlink (filename); - - g_assert (ephy_sqlite_connection_open (connection, filename, &error)); - g_assert (!error); - return connection; -} - -static void -test_create_connection (void) -{ - GError *error = NULL; - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - - EphySQLiteConnection *connection = ensure_empty_database (temporary_file); - ephy_sqlite_connection_close (connection); - - g_assert ( g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); - g_unlink (temporary_file); - g_assert ( !g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); - g_free (temporary_file); - - temporary_file = g_build_filename (g_get_tmp_dir (), "directory-that-does-not-exist", "epiphany_sqlite_test.db", NULL); - g_assert (!ephy_sqlite_connection_open (connection, temporary_file, &error)); - g_assert (error); - g_assert (!g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); - g_object_unref (connection); -} - - -static void -test_create_statement (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection* connection = ensure_empty_database (temporary_file); - GError *error = NULL; - EphySQLiteStatement *statement = NULL; - - statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE TEST (id INTEGER)", &error); - g_assert (statement); - g_assert (!error); - g_object_unref (statement); - - statement = ephy_sqlite_connection_create_statement (connection, "BLAHBLAHBLAHBA", &error); - g_assert (!statement); - g_assert (error); - - ephy_sqlite_connection_close (connection); - g_unlink (temporary_file); - g_free (temporary_file); - - g_object_unref (connection); -} - -static void -create_table_and_insert_row (EphySQLiteConnection *connection) -{ - GError *error = NULL; - EphySQLiteStatement *statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error); - g_assert (statement); - g_assert (!error); - ephy_sqlite_statement_step (statement, &error); - g_assert (!error); - g_object_unref (statement); - - statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error); - g_assert (statement); - g_assert (!error); - g_assert (!ephy_sqlite_statement_step (statement, &error)); - g_assert (!error); - g_object_unref (statement); - - statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (3, \"test\")", &error); - g_assert (statement); - g_assert (!error); - ephy_sqlite_statement_step (statement, &error); - g_assert (!error); - g_object_unref (statement); - - statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error); - g_assert (statement); - g_assert (!error); - - g_assert (ephy_sqlite_statement_step (statement, &error)); - g_assert (!error); - - g_assert_cmpint (ephy_sqlite_connection_get_last_insert_id (connection), ==, 1); - g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2); - g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 0), ==, EPHY_SQLITE_COLUMN_TYPE_INT); - g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 1), ==, EPHY_SQLITE_COLUMN_TYPE_STRING); - - /* Step will return false here since there is only one row. */ - g_assert (!ephy_sqlite_statement_step (statement, &error)); - g_object_unref (statement); -} - -static void -test_create_table_and_insert_row (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection* connection = ensure_empty_database (temporary_file); - - create_table_and_insert_row (connection); - - g_object_unref (connection); - g_unlink (temporary_file); - g_free (temporary_file); -} - -static void -test_bind_data (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection* connection = ensure_empty_database (temporary_file); - GError *error = NULL; - EphySQLiteStatement *statement = NULL; - - ephy_sqlite_connection_execute (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error); - - statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (?, ?)", &error); - g_assert (statement); - g_assert (!error); - - g_assert (ephy_sqlite_statement_bind_int (statement, 0, 3, &error)); - g_assert (!error); - g_assert (ephy_sqlite_statement_bind_string (statement, 1, "foo", &error)); - g_assert (!error); - - /* Will return false since there are no resulting rows. */ - g_assert (!ephy_sqlite_statement_step (statement, &error)); - g_assert (!error); - g_object_unref (statement); - - statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error); - g_assert (statement); - g_assert (!error); - g_assert (ephy_sqlite_statement_step (statement, &error)); - g_assert (!error); - g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2); - g_assert_cmpint (ephy_sqlite_statement_get_column_as_int (statement, 0), ==, 3); - g_assert_cmpstr (ephy_sqlite_statement_get_column_as_string (statement, 1), ==, "foo"); - - g_object_unref (connection); - g_unlink (temporary_file); - g_free (temporary_file); -} - -static void -test_table_exists (void) -{ - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection* connection = ensure_empty_database (temporary_file); - - g_assert (!ephy_sqlite_connection_table_exists (connection, "test")); - g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey")); - create_table_and_insert_row (connection); - g_assert (ephy_sqlite_connection_table_exists (connection, "test")); - g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey")); - - g_object_unref (connection); - g_unlink (temporary_file); - g_free (temporary_file); -} - -int -main (int argc, char *argv[]) -{ - gtk_test_init (&argc, &argv); - - g_test_add_func ("/lib/sqlite/ephy-sqlite/create_connection", test_create_connection); - g_test_add_func ("/lib/sqlite/ephy-sqlite/create_statement", test_create_statement); - g_test_add_func ("/lib/sqlite/ephy-sqlite/create_table_and_insert_row", test_create_table_and_insert_row); - g_test_add_func ("/lib/sqlite/ephy-sqlite/bind_data", test_bind_data); - g_test_add_func ("/lib/sqlite/ephy-sqlite/table_exists", test_table_exists); - - return g_test_run (); -} -- cgit v1.2.3