From: Sergio Villar Senin Date: Fri, 15 Dec 2006 15:08:40 +0000 (+0000) Subject: * Added some unit tests for ModestTextUtils X-Git-Tag: git_migration_finished~4318 X-Git-Url: http://git.maemo.org/git/?p=modest;a=commitdiff_plain;h=b43518714ee9ac0b6fdd0ab7d08f2c1de67846f5;ds=sidebyside * Added some unit tests for ModestTextUtils * Added unit tests for ModestAccountMgr * Added code to manage accounts removal * Added the ModestMailOperationQueue to the platform factory * ModestMailOperationQueue is no longer a singleton by itself * Added code to support mail operations cancels * Added a lot of API documentation * Added a new method to the ModestAccountView to retrieve the selected account pmo-trunk-r561 --- diff --git a/src/gtk/modest-account-view-window.c b/src/gtk/modest-account-view-window.c index bda98ee..a8cb91f 100644 --- a/src/gtk/modest-account-view-window.c +++ b/src/gtk/modest-account-view-window.c @@ -150,7 +150,35 @@ on_selection_changed (GtkTreeSelection *sel, ModestAccountViewWindow *self) static void on_remove_button_clicked (GtkWidget *button, ModestAccountViewWindow *self) { - g_message (__FUNCTION__); + TnyPlatformFactory *fact; + ModestAccountViewWindowPrivate *priv; + ModestAccountMgr *account_mgr; + ModestAccountView *account_view; + gchar *account_name; + + priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self); + fact = modest_tny_platform_factory_get_instance (); + account_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact); + + account_view = modest_widget_factory_get_account_view (priv->widget_factory); + account_name = modest_account_view_get_selected_account (account_view); + + if (account_name) { + gboolean removed; + GError *err = NULL; + + removed = modest_account_mgr_remove_account (account_mgr, + account_name, + FALSE, + &err); + if (removed) { + /* Show confirmation dialog */ + /* Remove from model & reload it */ + } else { + if (err) + g_error_free (err); + } + } } static void diff --git a/src/modest-mail-operation-queue.c b/src/modest-mail-operation-queue.c index d4e0c01..028b381 100644 --- a/src/modest-mail-operation-queue.c +++ b/src/modest-mail-operation-queue.c @@ -35,9 +35,6 @@ static void modest_mail_operation_queue_class_init (ModestMailOperationQueueClas static void modest_mail_operation_queue_init (ModestMailOperationQueue *obj); static void modest_mail_operation_queue_finalize (GObject *obj); -static GObject *modest_mail_operation_queue_constructor (GType type, guint n_construct_params, - GObjectConstructParam *construct_params); - static void modest_mail_operation_queue_cancel_no_block_wrapper (ModestMailOperation *mail_op, ModestMailOperationQueue *op_queue); @@ -61,7 +58,6 @@ struct _ModestMailOperationQueuePrivate { ModestMailOperationQueuePrivate)) /* globals */ static GObjectClass *parent_class = NULL; -static ModestMailOperationQueue *singleton = NULL; /* uncomment the following if you have defined any signals */ /* static guint signals[LAST_SIGNAL] = {0}; */ @@ -100,7 +96,6 @@ modest_mail_operation_queue_class_init (ModestMailOperationQueueClass *klass) parent_class = g_type_class_peek_parent (klass); gobject_class->finalize = modest_mail_operation_queue_finalize; - gobject_class->constructor = modest_mail_operation_queue_constructor; g_type_class_add_private (gobject_class, sizeof(ModestMailOperationQueuePrivate)); } @@ -116,25 +111,6 @@ modest_mail_operation_queue_init (ModestMailOperationQueue *obj) priv->queue_lock = g_mutex_new (); } -static GObject* -modest_mail_operation_queue_constructor (GType type, guint n_construct_params, - GObjectConstructParam *construct_params) -{ - GObject *object; - - if (!singleton) { - object = G_OBJECT_CLASS (parent_class)->constructor (type, - n_construct_params, construct_params); - - singleton = MODEST_MAIL_OPERATION_QUEUE (object); - } else { - object = G_OBJECT (singleton); - g_object_freeze_notify (G_OBJECT (singleton)); - } - - return object; -} - static void modest_mail_operation_queue_finalize (GObject *obj) { @@ -157,7 +133,7 @@ modest_mail_operation_queue_finalize (GObject *obj) } ModestMailOperationQueue * -modest_mail_operation_queue_get_instance (void) +modest_mail_operation_queue_new (void) { ModestMailOperationQueue *self = g_object_new (MODEST_TYPE_MAIL_OPERATION_QUEUE, NULL); @@ -200,8 +176,6 @@ modest_mail_operation_queue_remove (ModestMailOperationQueue *op_queue, g_mutex_lock (priv->queue_lock); g_queue_remove (priv->op_queue, mail_op); g_mutex_unlock (priv->queue_lock); - - g_object_unref (G_OBJECT (mail_op)); } @@ -217,7 +191,14 @@ static void modest_mail_operation_queue_cancel_no_block (ModestMailOperationQueue *op_queue, ModestMailOperation *mail_op) { - /* TODO: place here the cancel code */ + if (modest_mail_operation_is_finished (mail_op)) + return; + + /* TODO: the implementation is still empty */ + modest_mail_operation_cancel (mail_op); + + /* Remove from the queue */ + modest_mail_operation_queue_remove (op_queue, mail_op); } void diff --git a/src/modest-mail-operation-queue.h b/src/modest-mail-operation-queue.h index 54972df..7a0759e 100644 --- a/src/modest-mail-operation-queue.h +++ b/src/modest-mail-operation-queue.h @@ -59,17 +59,45 @@ struct _ModestMailOperationQueueClass { /* member functions */ GType modest_mail_operation_queue_get_type (void) G_GNUC_CONST; -ModestMailOperationQueue * modest_mail_operation_queue_get_instance (void); - +ModestMailOperationQueue * modest_mail_operation_queue_new (void); + +/** + * modest_mail_operation_queue_add: + * @op_queue: a #ModestMailOperationQueue + * @mail_op: the #ModestMailOperation that will be added to the queue + * + * Adds a mail operation at the end of the queue. It also adds a + * reference to the mail operation so the caller could free it + **/ void modest_mail_operation_queue_add (ModestMailOperationQueue *op_queue, ModestMailOperation *mail_op); +/** + * modest_mail_operation_queue_remove: + * @op_queue: a #ModestMailOperationQueue + * @mail_op: the #ModestMailOperation that will be removed from the queue + * + * Removes a mail operation from the queue. This method does not free + * the mail operation + **/ void modest_mail_operation_queue_remove (ModestMailOperationQueue *op_queue, ModestMailOperation *mail_op); - +/** + * modest_mail_operation_queue_cancel: + * @op_queue: a #ModestMailOperationQueue + * @mail_op: the #ModestMailOperation that will be canceled + * + * Cancels a #ModestMailOperation if it's not finished and removes it + * from the queue + **/ void modest_mail_operation_queue_cancel (ModestMailOperationQueue *op_queue, ModestMailOperation *mail_op); - +/** + * modest_mail_operation_queue_cancel_all: + * @op_queue: a #ModestMailOperationQueue + * + * Cancels all the unfinished #ModestMailOperation of the queue + **/ void modest_mail_operation_queue_cancel_all (ModestMailOperationQueue *op_queue); G_END_DECLS diff --git a/src/modest-mail-operation.c b/src/modest-mail-operation.c index 8c23dd6..ea8f567 100644 --- a/src/modest-mail-operation.c +++ b/src/modest-mail-operation.c @@ -808,6 +808,9 @@ modest_mail_operation_xfer_folder (ModestMailOperation *mail_op, g_object_unref (G_OBJECT (iter)); } + +/* FIXME: this method should be rewritten when the policy for the + Trash folder becomes clearer */ static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op, TnyStoreAccount *store_account) diff --git a/src/modest-tny-msg-actions.c b/src/modest-tny-msg-actions.c index 11d124e..b7dd0fc 100644 --- a/src/modest-tny-msg-actions.c +++ b/src/modest-tny-msg-actions.c @@ -27,7 +27,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - +#include #include #include #include diff --git a/src/modest-tny-platform-factory.c b/src/modest-tny-platform-factory.c index 669f757..0f664c6 100644 --- a/src/modest-tny-platform-factory.c +++ b/src/modest-tny-platform-factory.c @@ -50,6 +50,7 @@ static void tny_platform_factory_init (gpointer g, gpointer iface_data); static TnyAccountStore *modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self); static TnyDevice *modest_tny_platform_factory_new_device (TnyPlatformFactory *self); static TnyMsgView *modest_tny_platform_factory_new_msg_view (TnyPlatformFactory *self); + /* list my signals */ enum { /* MY_SIGNAL_1, */ @@ -59,10 +60,12 @@ enum { typedef struct _ModestTnyPlatformFactoryPrivate ModestTnyPlatformFactoryPrivate; struct _ModestTnyPlatformFactoryPrivate { - ModestTnyAccountStore *account_store; - ModestConf *conf; - ModestAccountMgr *account_mgr; + ModestTnyAccountStore *account_store; + ModestConf *conf; + ModestAccountMgr *account_mgr; + ModestMailOperationQueue *mail_op_queue; }; + #define MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \ MODEST_TYPE_TNY_PLATFORM_FACTORY, \ ModestTnyPlatformFactoryPrivate)) @@ -130,6 +133,7 @@ modest_tny_platform_factory_init (ModestTnyPlatformFactory *obj) priv->account_mgr = NULL; priv->conf = NULL; priv->account_store = NULL; + priv->mail_op_queue = NULL; } static GObject* @@ -158,20 +162,17 @@ modest_tny_platform_factory_finalize (GObject *obj) priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(obj); - if (priv->account_mgr) { + if (priv->account_mgr) g_object_unref (priv->account_mgr); - priv->account_mgr = NULL; - } - if (priv->conf) { + if (priv->conf) g_object_unref (priv->conf); - priv->conf = NULL; - } - if (priv->account_store) { + if (priv->account_store) g_object_unref (priv->account_store); - priv->account_store = NULL; - } + + if (priv->mail_op_queue) + g_object_unref (priv->mail_op_queue); G_OBJECT_CLASS(parent_class)->finalize (obj); } @@ -262,3 +263,16 @@ modest_tny_platform_factory_get_modest_conf_instance (TnyPlatformFactory *fact) return priv->conf; } + +ModestMailOperationQueue* +modest_tny_platform_factory_get_modest_mail_operation_queue_instance (TnyPlatformFactory *fact) +{ + ModestTnyPlatformFactoryPrivate *priv; + + priv = MODEST_TNY_PLATFORM_FACTORY_GET_PRIVATE(fact); + + if (!priv->mail_op_queue) + priv->mail_op_queue = modest_mail_operation_queue_new (); + + return priv->mail_op_queue; +} diff --git a/src/modest-tny-platform-factory.h b/src/modest-tny-platform-factory.h index d7368e8..3686cfb 100644 --- a/src/modest-tny-platform-factory.h +++ b/src/modest-tny-platform-factory.h @@ -37,6 +37,7 @@ #include #include "modest-account-mgr.h" #include "modest-conf.h" +#include "modest-mail-operation-queue.h" G_BEGIN_DECLS @@ -62,11 +63,53 @@ struct _ModestTnyPlatformFactoryClass { /* member functions */ GType modest_tny_platform_factory_get_type (void) G_GNUC_CONST; +/** + * modest_tny_platform_factory_get_instance: + * @void: + * + * Gets a new instance of the platform factory if it is the first call + * to the function, or the current one otherwise. This object is + * supposed to be a singleton + * + * Returns: the instance of a #ModestTnyPlatformFactory + **/ TnyPlatformFactory* modest_tny_platform_factory_get_instance (void); -ModestAccountMgr* modest_tny_platform_factory_get_modest_account_mgr_instance (TnyPlatformFactory *fact); -ModestConf* modest_tny_platform_factory_get_modest_conf_instance (TnyPlatformFactory *fact); +/** + * modest_tny_platform_factory_get_modest_account_mgr_instance: + * @fact: the #TnyPlatformFactory that holds the #ModestAccountMgr instance + * + * Gets a new instance of a #ModestAccountMgr if it is the first call + * to the function, or the current instantiated one otherwise. This + * object is supposed to be a singleton + * + * Returns: an instance of a #ModestAccountMgr + **/ +ModestAccountMgr* modest_tny_platform_factory_get_modest_account_mgr_instance (TnyPlatformFactory *fact); +/** + * modest_tny_platform_factory_get_modest_conf_instance: + * @fact: the #TnyPlatformFactory that holds the #ModestConf instance + * + * Gets a new instance of a #ModestConf if it is the first call to the + * function, or the current instantiated one otherwise. This object is + * supposed to be a singleton + * + * Returns: an instance of a #ModestConf + **/ +ModestConf* modest_tny_platform_factory_get_modest_conf_instance (TnyPlatformFactory *fact); + +/** + * modest_tny_platform_factory_get_modest_mail_operation_queue_instance: + * @fact: the #TnyPlatformFactory that holds the #ModestMailOperationQueue instance + * + * Gets a new instance of a #ModestMailOperationQueue if it is the + * first call to the function, or the current instantiated one + * otherwise. This object is supposed to be a singleton + * + * Returns: an instance of a #ModestMailOperationQueue + **/ +ModestMailOperationQueue* modest_tny_platform_factory_get_modest_mail_operation_queue_instance (TnyPlatformFactory *fact); G_END_DECLS diff --git a/src/widgets/modest-account-view.c b/src/widgets/modest-account-view.c index 29291a6..e236c69 100644 --- a/src/widgets/modest-account-view.c +++ b/src/widgets/modest-account-view.c @@ -311,3 +311,22 @@ modest_account_view_new (ModestAccountMgr *account_mgr) return MODEST_ACCOUNT_VIEW(obj); } +const gchar * +modest_account_view_get_selected_account (ModestAccountView *self) +{ + const gchar *account_name = NULL; + GtkTreeSelection *sel; + GtkTreeModel *model; + GtkTreeIter iter; + + g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL); + + sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self)); + if (gtk_tree_selection_get_selected (sel, &model, &iter)) { + gtk_tree_model_get (model, &iter, + NAME_COLUMN, &account_name, + -1); + } + + return account_name; +} diff --git a/src/widgets/modest-account-view.h b/src/widgets/modest-account-view.h index c0c2ed1..be71578 100644 --- a/src/widgets/modest-account-view.h +++ b/src/widgets/modest-account-view.h @@ -62,6 +62,17 @@ GType modest_account_view_get_type (void) G_GNUC_CONST; ModestAccountView* modest_account_view_new (ModestAccountMgr *account_mgr); +/** + * modest_account_view_get_selected_account: + * @account_view: a #ModestAccountView + * + * Gets the name of the account currently selected + * + * Returns: the name of the selected account or NULL if none is + * selected + **/ +const gchar* modest_account_view_get_selected_account (ModestAccountView *account_view); + G_END_DECLS #endif /* __MODEST_ACCOUNT_VIEW_H__ */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 7f6bacb..dada03e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,21 +4,25 @@ MAINTAINERCLEANFILES = Makefile.in -TESTS_ENVIRONMENT = top_builddir=$(top_builddir) +TESTS_ENVIRONMENT = top_builddir=$(top_builddir) \ + CK_FORK=yes \ + CK_VERBOSITY=verbose TESTS = \ + check_modest-presets \ check_folder-xfer \ - check_update-account \ + check_text-utils \ check_modest-conf \ - check_text-utils \ - check_modest-presets + check_update-account \ + check_account-mgr noinst_PROGRAMS= \ check_folder-xfer \ check_update-account \ check_modest-conf \ check_text-utils \ - check_modest-presets + check_modest-presets \ + check_account-mgr INCLUDES=\ @CHECK_CFLAGS@ \ @@ -70,3 +74,7 @@ check_text_utils_LDADD = $(objects) check_modest_presets_SOURCES=\ check_modest-presets.c check_modest_presets_LDADD = $(objects) + +check_account_mgr_SOURCES=\ + check_account-mgr.c +check_account_mgr_LDADD = $(objects) diff --git a/tests/check_account-mgr.c b/tests/check_account-mgr.c new file mode 100644 index 0000000..c35317a --- /dev/null +++ b/tests/check_account-mgr.c @@ -0,0 +1,399 @@ +/* Copyright (c) 2006, Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Nokia Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +/* ----------------------- Defines ---------------------- */ + +#define TEST_MODEST_ACCOUNT_NAME "modest-unit-tests-account" + +/* ------------------ Global variables ------------------ */ + +static ModestAccountMgr *account_mgr = NULL; + +/* ---------------------- Fixtures --------------------- */ + +static void +fx_setup_default_account_mgr () +{ + int argc = 0; + ModestConf *conf = NULL; + + g_type_init (); + + conf = modest_conf_new (); + fail_unless (MODEST_IS_CONF (conf), + "modest_conf_new failed"); + + account_mgr = modest_account_mgr_new (conf); + fail_unless (MODEST_IS_ACCOUNT_MGR (account_mgr), + "modest_account_mgr_new failed"); +} + +static void +fx_teardown_default_account_mgr () +{ + g_object_unref (account_mgr); +} + +/* ---------- add/exists/remove account tests ---------- */ + +/** + * Test regular usage of + * modest_account_mgr_add_account + * modest_account_mgr_add_server_account + * modest_account_mgr_account_exists + * modest_account_mgr_remove_account + * - Test 1: Create anaccount + * - Test 2: Check account exists + * - Test 3: Remove account + * - Test 4: Create a server account + * - Test 5: Check server account exists + * - Test 6: Remove server account + * - Test 7: Check if a non-existing account exists + * - Test 8: Check if a non-existing server account exists + */ +START_TEST (test_add_exists_remove_account_regular) +{ + gchar *name = NULL; + gchar *store_account = NULL; + gchar *transport_account = NULL; + gchar *hostname = NULL; + gchar *username = NULL; + gchar *password = NULL; + gchar *proto = NULL; + GError *error = NULL; + gboolean result; + + name = g_strdup (TEST_MODEST_ACCOUNT_NAME); + + /* Test 1 */ + store_account = g_strdup ("imap://me@myserver"); + transport_account = g_strdup ("local-smtp"); + result = modest_account_mgr_add_account (account_mgr, + name, + store_account, + transport_account, + &error); + fail_unless (result && !error, + "modest_account_mgr_add_account failed:\n" \ + "name: %s\nstore: %s\ntransport: %s\nerror: %s", + name, store_account, transport_account, + error ? error->message : ""); + + g_free (store_account); + g_free (transport_account); + + /* Test 2 */ + result = modest_account_mgr_account_exists (account_mgr, + name, + FALSE, + &error); + fail_unless (result && !error, + "modest_account_mgr_account_exists failed: " \ + "Account with name \"%s\" should exist. Error: %s", + name, error ? error->message : ""); + + + /* Test 3 */ + result = modest_account_mgr_remove_account (account_mgr, + name, + FALSE, + &error); + fail_unless (result && !error, + "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s", + name, error ? error->message : ""); + + /* Test 4 */ + hostname = g_strdup ("myhostname.mydomain.com"); + username = g_strdup ("myusername"); + password = g_strdup ("mypassword"); + proto = g_strdup ("smtp"); + result = modest_account_mgr_add_server_account (account_mgr, + name, + hostname, + username, + password, + proto); + fail_unless (result, + "modest_account_mgr_add_server_account failed:\n" \ + "name: %s\nhostname: %s\nusername: %s\npassword: %s\nproto: %s", + name, hostname, username, password, proto); + + g_free (hostname); + g_free (username); + g_free (password); + g_free (proto); + + /* Test 5 */ + result = modest_account_mgr_account_exists (account_mgr, + name, + TRUE, + &error); + fail_unless (result && !error, + "modest_account_mgr_account_exists failed: " \ + "Server account with name \"%s\" should exist. Error: %s", + name, error ? error->message : ""); + + + /* Test 6 */ + result = modest_account_mgr_remove_account (account_mgr, + name, + TRUE, + &error); + fail_unless (result && !error, + "modest_account_mgr_remove_account failed:\nname: %s\nerror: %s", + name, error ? error->message : ""); + + + /* Test 7 */ + result = modest_account_mgr_account_exists (account_mgr, + "a_name_that_does_not_exist", + FALSE, + NULL); + fail_unless (!result, + "modest_account_mgr_exists_account does not return " \ + "FALSE when passing an account that does not exist"); + + /* Test 8 */ + result = modest_account_mgr_account_exists (account_mgr, + "a_name_that_does_not_exist", + TRUE, + NULL); + fail_unless (!result, + "modest_account_mgr_exists_account does not return " \ + "FALSE when passing a server account that does not exist"); + + g_free (name); +} +END_TEST + +/** + * Test regular usage of + * modest_account_mgr_add_account, + * modest_account_mgr_add_server_account + * modest_account_mgr_account_exists + * modest_account_mgr_remove_account + * - Test 1: Create account with NULL account_mgr + * - Test 2: Create account with NULL name + * - Test 3: Create account with invalid name string + * - Test 4: Create server account with NULL account_mgr + * - Test 5: Create server account with NULL name + * - Test 6: Create server account with invalid name string + * - Test 7: Remove a non-existing account + * - Test 8: Remove a non-existing server account + * - Test 9: Remove with NULL acount manager + * - Test 10: Remove with NULL name + * - Test 11: Check if an account exists with NULL account_mgr + * - Test 12: Check if a server account exists with a NULL account_mgr + * - Test 13: Check if a NULL account exists + * - Test 14: Check if a NULL server account exists + */ +START_TEST (test_add_exists_remove_account_invalid) +{ + gboolean result; + + /* Test 1 */ + result = modest_account_mgr_add_account (NULL, + TEST_MODEST_ACCOUNT_NAME, + "store_account", + "transport_account", + NULL); + fail_unless (!result, + "modest_account_mgr_add_account does not return FALSE when" \ + "passing a NULL ModestAccountMgr"); + + /* Test 2 */ + result = modest_account_mgr_add_account (account_mgr, + NULL, + "store_account", + "transport_account", + NULL); + fail_unless (!result, + "modest_account_mgr_add_account does not return FALSE when" \ + "passing a NULL account name"); + + /* Test 3*/ + result = modest_account_mgr_add_account (account_mgr, + "ïnválid_accountñ_nÄméç", + "store_account", + "transport_account", + NULL); + fail_unless (!result, + "modest_account_mgr_add_account does not return FALSE when" \ + "passing an invalid account name"); + + /* Test 4 */ + result = modest_account_mgr_add_server_account (NULL, + TEST_MODEST_ACCOUNT_NAME, + "hostname", + "username", + "password", + "proto"); + fail_unless (!result, + "modest_account_mgr_add_server_account does not return " \ + "FALSE when passing a NULL ModestAccountMgr"); + + /* Test 5 */ + result = modest_account_mgr_add_server_account (account_mgr, + NULL, + "hostname", + "username", + "password", + "proto"); + fail_unless (!result, + "modest_account_mgr_add_server_account does not return " \ + "FALSE when passing a NULL account name"); + + /* Test 6 */ + result = modest_account_mgr_add_server_account (account_mgr, + "ïnválid_accountñ_nÄméç", + "hostname", + "username", + "password", + "proto"); + fail_unless (!result, + "modest_account_mgr_add_server_account does not return " \ + "FALSE when passing an invalid account name"); + + /* Test 7 */ + result = modest_account_mgr_remove_account (account_mgr, + "a_name_that_does_not_exist", + FALSE, + NULL); + fail_unless (!result, + "modest_account_mgr_remove_acccount does not return FALSE " \ + "when trying to remove an account that does not exist"); + + /* Test 8 */ + result = modest_account_mgr_remove_account (account_mgr, + "a_name_that_does_not_exist", + TRUE, + NULL); + fail_unless (!result, + "modest_account_mgr_remove_acccount does not return FALSE " \ + "when trying to remove a server account that does not exist"); + + /* Test 9 */ + result = modest_account_mgr_remove_account (NULL, + TEST_MODEST_ACCOUNT_NAME, + FALSE, + NULL); + fail_unless (!result, + "modest_account_mgr_remove_acccount does not return " \ + "FALSE when passing a NULL ModestAccountMgr"); + + /* Test 10 */ + result = modest_account_mgr_remove_account (account_mgr, + NULL, + FALSE, + NULL); + fail_unless (!result, + "modest_account_mgr_remove_acccount does not return " \ + "FALSE when passing a NULL account name"); + + /* Test 11 */ + result = modest_account_mgr_account_exists (NULL, + TEST_MODEST_ACCOUNT_NAME, + TRUE, + NULL); + fail_unless (!result, + "modest_account_mgr_exists_account does not return " \ + "FALSE when passing a NULL ModestAccountMgr"); + + /* Test 12 */ + result = modest_account_mgr_account_exists (NULL, + TEST_MODEST_ACCOUNT_NAME, + FALSE, + NULL); + fail_unless (!result, + "modest_account_mgr_exists_account does not return " \ + "FALSE when passing a NULL ModestAccountMgr"); + + /* Test 13 */ + result = modest_account_mgr_account_exists (account_mgr, + NULL, + FALSE, + NULL); + fail_unless (!result, + "modest_account_mgr_exists_acccount does not return " \ + "FALSE when passing a NULL account name"); + + /* Test 14 */ + result = modest_account_mgr_account_exists (account_mgr, + NULL, + TRUE, + NULL); + fail_unless (!result, + "modest_account_mgr_exists_account does not return " \ + "FALSE when passing a NULL server account name"); +} +END_TEST + +/* ------------------- Suite creation ------------------- */ + +static Suite* +account_mgr_suite (void) +{ + Suite *suite = suite_create ("ModestAccountMgr"); + TCase *tc = NULL; + + /* Tests case for "add/exists/remove account" */ + tc = tcase_create ("add_exists_remove_account"); + tcase_add_checked_fixture (tc, + fx_setup_default_account_mgr, + fx_teardown_default_account_mgr); + tcase_add_test (tc, test_add_exists_remove_account_regular); + tcase_add_test (tc, test_add_exists_remove_account_invalid); + suite_add_tcase (suite, tc); + + return suite; +} + +/* --------------------- Main program ------------------- */ + +gint +main () +{ + SRunner *srunner; + Suite *suite; + int failures; + + suite = account_mgr_suite (); + srunner = srunner_create (suite); + + srunner_run_all (srunner, CK_ENV); + failures = srunner_ntests_failed (srunner); + srunner_free (srunner); + + return failures; +} diff --git a/tests/check_text-utils.c b/tests/check_text-utils.c index 9baadb1..3fb4568 100644 --- a/tests/check_text-utils.c +++ b/tests/check_text-utils.c @@ -28,6 +28,7 @@ */ #include +#include #include typedef struct { @@ -35,18 +36,28 @@ typedef struct { const gchar *expected; } StringPair; -START_TEST (test_display_address) +/* ----------------- display address tests -------------- */ + +/** + * Test regular usage of modest_text_utils_display_address + * - Test 1: Check "<...>" deletion + * - Test 2: Check "(...)" deletion + * - Test 3: Check address left and right trim, also non ASCII chars + * - Test 4: Check issues in tests 1, 2 and 3 together + * - Test 5: Check with an empty address + */ +START_TEST (test_display_address_regular) { - int i; - const StringPair tests[] = { + gint i; + const StringPair tests[] = { { "John Doe ", "John Doe" }, { "Rupert Griffin (test)", "Rupert Griffin"}, - { "Hyvää päivää ", "Hyvää päivää"}, + { " Hyvää päivää ", "Hyvää päivää"}, + { " John Doe (test) ", "John Doe" }, + { "", "" }, }; - fail_unless (modest_text_utils_display_address (NULL) == NULL, - "modest_text_utils_display_address(NULL) should be NULL"); - + /* Tests 1, 2, 3, 4 */ for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) { gchar *str = g_strdup (tests[i].original); str = modest_text_utils_display_address (str); @@ -59,53 +70,756 @@ START_TEST (test_display_address) } END_TEST -START_TEST (test_derived_address) + +/** + * Test invalid usage of modest_text_utils_display_address + * - Test 1: Check with NULL address (should return NULL) + */ +START_TEST (test_display_address_invalid) +{ + /* Test 1 */ + fail_unless (modest_text_utils_display_address (NULL) == NULL, + "modest_text_utils_display_address(NULL) should be NULL"); + +} +END_TEST + +/* ----------------- derived address tests -------------- */ + +/** + * Test regular usage of modest_text_utils_derived_subject + * - Test 1: Check with a normal subject + * - Test 2: Test with NULL subject + * - Test 3: Test with non ASCII chars + * - Test 4: Test with an empty subject + */ +START_TEST (test_derived_subject_regular) { - int i; + gint i; const gchar *prefix="Re:"; - - const StringPair tests[] = { + const StringPair tests[] = { { "subject", "Re: subject" }, { NULL, "Re:"}, { "Hyvää päivää", "Re: Hyvää päivää"}, + { "", "Re: "}, }; - fail_unless (modest_text_utils_derived_subject (NULL, NULL) == NULL, - "modest_text_utils_derived_subject (NULL,NULL) should be NULL"); - + /* Tests 1, 2, 3, 4 */ for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) { - gchar *str = g_strdup (tests[i].original); - str = modest_text_utils_derived_subject (str, prefix); + gchar *orig = g_strdup (tests[i].original); + gchar *str = modest_text_utils_derived_subject (orig, prefix); fail_unless (str && strcmp(str, tests[i].expected) == 0, "modest_text_utils_derived_subject failed for '%s': " "expected '%s' but got '%s'", tests[i].original, tests[i].expected, str); + g_free (orig); g_free (str); } } END_TEST +/** + * Test invalid usage of modest_text_utils_derived_subject + * - Test 1: Check with NULL prefix (should return NULL) + */ +START_TEST (test_derived_subject_invalid) +{ + /* Test 1 */ + fail_unless (modest_text_utils_derived_subject (NULL, NULL) == NULL, + "modest_text_utils_derived_subject (*,NULL) should be NULL"); +} +END_TEST + +/* --------------------- quote tests -------------------- */ + +/** + * Test regular usage of modest_text_utils_quote + * - Test 1: Quote empty text + * - Test 2: Quote 1 line of plain text + * - Test 3: Quote several lines of plain text + * - Test 4: Quote non ASCII chars + * - Test 5: Quote with a limit lower than the any word in the text + * - TODO: Test HTML quotation once its implementation if finished + */ +START_TEST (test_quote_regular) +{ + gint i; + gchar *text = NULL; + gchar *expected_quoted_text = NULL; + gchar *quoted_text = NULL; + + /* Tests 1, 2, 3 */ + const StringPair tests[] = { + { "", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n> \n" }, + { "This text should be quoted", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n> This text\n> should be quoted\n> \n" }, + { "These are several\nlines\nof plain text", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n> These are\n> several\n> lines\n> of text\n> \n" }, + { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n> áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš" }, + }; + + for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) { + text = g_strdup (tests[i].original); + expected_quoted_text = g_strdup (tests[i].expected); + quoted_text = modest_text_utils_quote (text, "text/plain", "foo@bar", 0, 15); + fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text), + "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \ + "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"", + text, expected_quoted_text, quoted_text); + g_free (text); + g_free (expected_quoted_text); + g_free (quoted_text); + } + + /* Test 5 */ + text = g_strdup ("Quotation test example"); + expected_quoted_text = + g_strdup ("On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n> Quotation\n> test\n> example\n> \n"); + quoted_text = modest_text_utils_quote (text, "text/plain", "foo@bar", 0, 1); + fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text), + "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \ + "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"", + text, expected_quoted_text, quoted_text); + g_free (text); + g_free (expected_quoted_text); + g_free (quoted_text); +} +END_TEST + +/** + * Test invalid usage of modest_text_utils_quote + * - Test 1: Check NULL text (should return NULL) + * - Test 2: Check NULL content_type (should quote as text/plain) + * - Test 3: Check NULL address (should cite address as "(null)") + * - Test 4: Check negative limit (should write each word in its own line) + * - TODO: Repeat tests for HTML quotation when its implementation is finished + */ +START_TEST (test_quote_invalid) +{ + gchar *text = NULL; + gchar *expected_quoted_text = NULL; + gchar *quoted_text = NULL; + + /* Test 1 (Fault) */ + text = NULL; + quoted_text = modest_text_utils_quote (NULL, "text/plain", "foo@bar", 0, 15); + fail_unless (quoted_text == NULL, + "modest_text_utils_quote failed:\nOriginal text: NULL\n" \ + "Expected quotation: NULL\nQuoted text: \"%s\"", + quoted_text); + g_free (quoted_text); + + + /* Test 2 (Fault) */ + text = g_strdup ("Text"); + expected_quoted_text = g_strdup ("On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n> Text\n> \n"); + quoted_text = modest_text_utils_quote (text, NULL, "foo@bar", 0, 15); + fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text), + "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \ + "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"", + text, expected_quoted_text, quoted_text); + g_free (text); + g_free (expected_quoted_text); + g_free (quoted_text); + + /* Test 3 */ + text = g_strdup ("Text"); + expected_quoted_text = g_strdup ("On Thu Jan 1 01:00:00 1970, (null) wrote:\n> Text\n"); + quoted_text = modest_text_utils_quote (text, "text/plain", NULL, 0, 15); + fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text), + "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \ + "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"", + text, expected_quoted_text, quoted_text); + g_free (text); + g_free (expected_quoted_text); + g_free (quoted_text); + + /* Test 4 */ + text = g_strdup ("This is a text"); + expected_quoted_text = g_strdup ("On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n> This\n> is\n> a\n> text\n> \n"); + quoted_text = modest_text_utils_quote (text, "text/plain", "foo@bar", 0, 0); + fail_unless (quoted_text && !strcmp (expected_quoted_text, quoted_text), + "modest_text_utils_quote failed:\nOriginal text:\n\"%s\"\n" \ + "Expected quotation:\n\"%s\"\nQuoted text:\n\"%s\"", + text, expected_quoted_text, quoted_text); + g_free (text); + g_free (expected_quoted_text); + g_free (quoted_text); +} +END_TEST + +/* ---------------------- cite tests -------------------- */ + +/** + * Test regular usage of modest_text_utils_cite + * - Test 1: Check cite of an empty text + * - Test 2: Check cite of a line of text + * - Test 3: Check cite of several lines of text + * - Test 4: Check with non ASCII chars + */ +START_TEST (test_cite_regular) +{ + gint i; + gchar *cited_text = NULL; + const StringPair tests[] = { + { "", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n\n" }, + { "This is some text", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\nThis is some text\n" }, + { "This\nis some\ntext", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\nThis\nis some\ntext\n" }, + { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš", + "On Thu Jan 1 01:00:00 1970, foo@bar wrote:\náéíÍÓÚäëïÏÖÜñÑçÇŽÊîš\n" }, + }; + + /* Tests 1, 2, 3, 4 */ + for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) { + cited_text = modest_text_utils_cite (tests[i].original, "text/plain", "foo@bar", 0); + fail_unless (cited_text && !strcmp (tests[i].expected, cited_text), + "modest_text_utils_cite failed:\nOriginal text:\n\"%s\"\n" \ + "Expected cite:\n\"%s\"\nCite obtained:\n\"%s\"", + tests[i].original, tests[i].expected, cited_text); + g_free (cited_text); + } +} +END_TEST + +/** + * Test invalid usage of modest_text_utils_cite + * - Test 1: Check NULL text (should cite text as (null)) + * - Test 2: Check NULL address (should cite address as (null) + * TODO: add tests for content_type once it is used in the implementation + */ +START_TEST (test_cite_invalid) +{ + gchar *text = NULL; + gchar *cited_text = NULL; + gchar *expected_cite = NULL; + + /* Test 1 */ + text = NULL; + expected_cite = g_strdup ("On Thu Jan 1 01:00:00 1970, foo@bar wrote:\n(null)\n"); + cited_text = modest_text_utils_cite (text, "text/plain", "foo@bar", 0); + fail_unless (cited_text && !strcmp (expected_cite, cited_text), + "modest_text_utils_cite failed:\nOriginal text:\nNULL\n" \ + "Expected cite:\n\"%s\"\nCite obtained:\n\"%s\"", + expected_cite, cited_text); + g_free (expected_cite); + g_free (cited_text); + /* Test 2 */ + text = g_strdup ("This is some text"); + expected_cite = g_strdup ("On Thu Jan 1 01:00:00 1970, (null) wrote:\nThis is some text\n"); + cited_text = modest_text_utils_cite (text, "text/plain", NULL, 0); + fail_unless (cited_text && !strcmp (expected_cite, cited_text), + "modest_text_utils_cite failed:\nOriginal text:\n\"%s\"\n" \ + "Expected cite:\n\"%s\"\nCite obtained:\n\"%s\"", + text, expected_cite, cited_text); + g_free (text); + g_free (expected_cite); + g_free (cited_text); +} +END_TEST +/* -------------------- inline tests -------------------- */ +/** + * Test regular usage of modest_text_utils_inline + * - Test 1: Check inline of an empty text + * - Test 2: Check inline of a regular text + * - Test 3: Check with non ASCII chars + * TODO: add tests for HTML when implementation is finished + */ +START_TEST (test_inline_regular) +{ + gint i; + gchar *inlined_text = NULL; + const StringPair tests[] = { + { "", + "-----Forwarded Message-----\n" \ + "From: foo@bar\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: bar@foo\n" \ + "Subject: Any subject\n\n" }, + { "Some text\nto inline", + "-----Forwarded Message-----\n" \ + "From: foo@bar\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: bar@foo\n" \ + "Subject: Any subject\n\n" \ + "Some text\nto inline" }, + { "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš", + "-----Forwarded Message-----\n" \ + "From: foo@bar\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: bar@foo\n" \ + "Subject: Any subject\n\n" \ + "áéíÍÓÚäëïÏÖÜñÑçÇŽÊîš" }, + }; + /* Tests 1, 2, 3 */ + for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) { + inlined_text = modest_text_utils_inline (tests[i].original, + "text/plain", + "foo@bar", + 0, + "bar@foo", + "Any subject"); + fail_unless (inlined_text && !strcmp (tests[i].expected, inlined_text), + "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \ + "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"", + tests[i].original, tests[i].expected, inlined_text); + g_free (inlined_text); + } +} +END_TEST + +/** + * Test invalid usage of modest_text_utils_inline + * - Test 1: Check NULL text (should inline text as (null)) + * - Test 2: Check NULL content_type (should assume text/plain) + * - Test 3: Check NULL from (should write "(null)") + * - Test 4: Check NULL to (should write ("null")) + * - Test 5: Check NULL subject (should write ("null")) + * TODO: repeat tests from HTML when implemented + */ +START_TEST (test_inline_invalid) +{ + gchar *text = NULL; + gchar *expected_inline = NULL; + gchar *inlined_text = NULL; + + /* Test 1 */ + expected_inline = g_strdup("-----Forwarded Message-----\n" \ + "From: foo@bar\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: bar@foo\n" \ + "Subject: Any subject\n\n" \ + "(null)"); + inlined_text = modest_text_utils_inline (NULL, + "text/plain", + "foo@bar", + 0, + "bar@foo", + "Any subject"); + fail_unless (inlined_text && !strcmp (expected_inline, inlined_text), + "modest_text_utils_inline failed:\nOriginal text:\nNULL\n" \ + "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"", + expected_inline, inlined_text); + g_free (inlined_text); + + /* Test 2 (Fault) */ + text = g_strdup ("Some text"); + expected_inline = g_strdup("-----Forwarded Message-----\n" \ + "From: foo@bar\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: bar@foo\n" \ + "Subject: Any subject\n\n" \ + "Some text"); + inlined_text = modest_text_utils_inline (text, + NULL, + "foo@bar", + 0, + "bar@foo", + "Any subject"); + fail_unless (inlined_text && !strcmp (expected_inline, inlined_text), + "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \ + "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"", + text, expected_inline, inlined_text); + g_free (inlined_text); + + /* Test 3 */ + text = g_strdup ("Some text"); + expected_inline = g_strdup("-----Forwarded Message-----\n" \ + "From: (null)\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: bar@foo\n" \ + "Subject: Any subject\n\n" \ + "Some text"); + inlined_text = modest_text_utils_inline (text, + "text/plain", + NULL, + 0, + "bar@foo", + "Any subject"); + fail_unless (inlined_text && !strcmp (expected_inline, inlined_text), + "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \ + "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"", + text, expected_inline, inlined_text); + g_free (inlined_text); + + /* Test 4 */ + text = g_strdup ("Some text"); + expected_inline = g_strdup("-----Forwarded Message-----\n" \ + "From: foo@bar\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: (null)\n" \ + "Subject: Any subject\n\n" \ + "Some text"); + inlined_text = modest_text_utils_inline (text, + "text/plain", + "foo@bar", + 0, + NULL, + "Any subject"); + fail_unless (inlined_text && !strcmp (expected_inline, inlined_text), + "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \ + "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"", + text, expected_inline, inlined_text); + g_free (inlined_text); + + /* Test 5 */ + text = g_strdup ("Some text"); + expected_inline = g_strdup("-----Forwarded Message-----\n" \ + "From: foo@bar\n" \ + "Sent: Thu Jan 1 01:00:00 1970\n" \ + "To: bar@foo\n" \ + "Subject: (null)\n\n" \ + "Some text"); + inlined_text = modest_text_utils_inline (text, + "text/plain", + "foo@bar", + 0, + "bar@foo", + NULL); + fail_unless (inlined_text && !strcmp (expected_inline, inlined_text), + "modest_text_utils_inline failed:\nOriginal text:\n\"%s\"\n" \ + "Expected inline:\n\"%s\"\nInline obtained:\n\"%s\"", + text, expected_inline, inlined_text); + g_free (inlined_text); +} +END_TEST + +/* ---------------- remove address tests ---------------- */ + +/** + * Test regular usage of modest_text_utils_remove_address + * - Test 1: Remove a single address in the middle of a list + * - Test 2: Remove an extended address in the middle of a list + * - Test 3: Remove an address that appears more than once in the list + * - Test 4: Remove address from an empty list + * - Test 5: Check with non ASCII characters + */ +START_TEST (test_remove_address_regular) +{ + gchar *list = NULL; + gchar *new_list = NULL; + gchar *address = NULL; + + /* Test 1 */ + list = g_strdup ("foo@bar , myname@mycompany.com (myname), " \ + "xxx@yyy.zzz (xxx yyy zzz), bar@foo"); + address = g_strdup ("xxx@yyy.zzz"); + new_list = modest_text_utils_remove_address (list, address); + + fail_unless (new_list && strstr (new_list, "foo@bar ") != NULL, + "modest_text_utils_remove_address failed: "\ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "myname@mycompany.com (myname)") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "bar@foo") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + + g_free (list); + g_free (new_list); + g_free (address); + + /* Test 2 */ + list = g_strdup ("foo@bar , xxx@yyy.zzz (xxx yyy zzz), bar@foo"); + address = g_strdup ("xxx@yyy.zzz (xxx yyy zzz)"); + new_list = modest_text_utils_remove_address (list, address); + + fail_unless (new_list && strstr (new_list, "foo@bar ") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "bar@foo") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "(xxx yyy zzz)") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + + g_free (list); + g_free (new_list); + g_free (address); + + /* Test 3 */ + list = g_strdup ("foo@bar , bar@foo (BAR), xxx@yyy.zzz (xxx yyy zzz), bar@foo"); + address = g_strdup ("bar@foo"); + new_list = modest_text_utils_remove_address (list, address); + + fail_unless (new_list && strstr (new_list, "foo@bar ") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "xxx@yyy.zzz (xxx yyy zzz)") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "bar@foo") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "(BAR)") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + + g_free (list); + g_free (new_list); + g_free (address); + + /* Test 4 */ + list = g_strdup (""); + address = g_strdup ("bar@foo"); + new_list = modest_text_utils_remove_address (list, address); + + fail_unless (new_list && !strcmp (new_list, list), + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + + g_free (list); + g_free (new_list); + g_free (address); + + /* Test 5 */ + list = g_strdup ("foo@bar, áñï@Èž.com (Àç ÑŸž), bar@foo"); + address = g_strdup ("áñï@Èž.com"); + new_list = modest_text_utils_remove_address (list, address); + + fail_unless (new_list && strstr (new_list, "foo@bar") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "bar@foo") != NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "áñï@Èž.com") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + fail_unless (new_list && strstr (new_list, "(Àç ÑŸž)") == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\"", + address, list, new_list); + + g_free (list); + g_free (new_list); + g_free (address); +} +END_TEST + +/** + * Test limits usage of modest_text_utils_remove_address + * - Test 1: Remove address at the beginning of the list + * - Test 2: Remove address at the end of the list + * - Test 3: Remove the address of a one-element-size list + */ +START_TEST (test_remove_address_limits) +{ + gchar *list = NULL; + gchar *new_list = NULL; + gchar *expected_list = NULL; + gchar *address = NULL; + gint i; + + const StringPair tests[] = { + { "foo@bar , bar@foo (BAR FOO)", "bar@foo (BAR FOO)" }, + { "foo@bar , bar@foo (BAR FOO)", "foo@bar " }, + { "foo@bar ", "" }, + }; + const gchar *remove[] = { "foo@bar", "bar@foo", "foo@bar" }; + + /* Tests 1, 2, 3 */ + for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) { + list = g_strdup (tests[i].original); + expected_list = g_strdup (tests[i].expected); + new_list = modest_text_utils_remove_address (list, remove[i]); + fail_unless (new_list && !strcmp (expected_list, new_list), + "modest_text_utils_remove_address failed: " \ + "Removing \"%s\" from address list \"%s\" returns \"%s\" instead of \"%s\"", + remove[i], list, new_list, expected_list); + g_free (list); + g_free (expected_list); + g_free (new_list); + } +} +END_TEST + +/** + * Test invalid usage of modest_text_utils_remove_address + * - Test 1: Remove a NULL address (should return the same list) + * - Test 2: Remove an address form a NULL list (should return NULL) + */ +START_TEST (test_remove_address_invalid) +{ + gchar *list = NULL; + gchar *new_list = NULL; + + /* Test 1 (Fault) */ + list = g_strdup ("foo@bar, bar@foo"); + new_list = modest_text_utils_remove_address (list, NULL); + fail_unless (new_list && !strcmp (list, new_list), + "modest_text_utils_remove_address failed: " \ + "Removing a NULL address from \"%s\" returns \"%s\"", + list, new_list); + g_free (list); + g_free (new_list); + + /* Test 2 */ + new_list = modest_text_utils_remove_address (NULL, "foo@bar"); + fail_unless (new_list == NULL, + "modest_text_utils_remove_address failed: " \ + "Removing an address from a NULL list does not return NULL"); + g_free (new_list); +} +END_TEST + +/* --------------- convert to html tests ---------------- */ + +/** + * Test regular usage of modest_text_utils_convert_to_html + * - Test 1: Check conversion of a regular text + * - Test 2: Check conversion of a regular text with links + * - Test 3: Check conversion of a regular text with special html chars + */ +START_TEST (test_convert_to_html_regular) +{ + gchar text_in_html[256]; + gchar *html_start = NULL; + gchar *html_end = NULL; + gchar *html = NULL; + gint bytes; + const StringPair tests[] = { + { "This is some text.", + "This is some text." }, + { "http://xxx.yyy.zzz/myfile", + "http://xxx.yyy.zzz/myfile" }, + { "\n\tx", + "<a  "  b>
\n    x
" }, + }; + + /* Tests 1, 2, 3 */ + for (i = 0; i != sizeof(tests)/sizeof(StringPair); ++i) { + html = modest_text_utils_convert_to_html (tests[i].original); + fail_unless (html != NULL, + "modest_text_utils_convert_to_html failed:" \ + "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\n\NULL", + tests[i].original, tests[i].expected); + html_start = strstr (html, ""); + html_end = strstr (html, ""); + bytes = html_end - html_start; + memset (text_in_html, 0, 256); + memcpy (text_in_html, html_start, bytes); + fail_unless (strstr (html, tests[i].expected) != NULL, + "modest_text_utils_convert_to_html failed:" \ + "Original text:\n\"%s\"\nExpected html:\n\"%s\"\nObtained html:\n\"%s\"", + tests[i].original, tests[i].expected, text_in_html); + g_free (html_start); + g_free (html_end); + g_free (html); + } +} +END_TEST + +/** + * Test invalid usage of modest_text_utils_convert_to_html + * - Test 1: Check with NULL data + */ +START_TEST (test_convert_to_html_invalid) +{ + gchar *html = NULL; + + /* Test 1 */ + html = modest_text_utils_convert_to_html (NULL); + fail_unless (html == NULL, + "modest_text_utils_convert_to_html failed:" \ + "Does not return NULL when passed NULL data"); +} +END_TEST + + +/* ------------------- Suite creation ------------------- */ static Suite* text_utils_suite (void) { Suite *suite = suite_create ("ModestTextUtils"); + TCase *tc = NULL; - TCase *tc_core = tcase_create ("modest"); - tcase_add_test (tc_core, test_display_address); - tcase_add_test (tc_core, test_derived_address); - - suite_add_tcase (suite, tc_core); + /* Tests case for "display adress" */ + tc = tcase_create ("display_adress"); + tcase_add_test (tc, test_display_address_regular); + tcase_add_test (tc, test_display_address_invalid); + suite_add_tcase (suite, tc); + + /* Test case for "derived subject" */ + tc = tcase_create ("derived_subject"); + tcase_add_test (tc, test_derived_subject_regular); + tcase_add_test (tc, test_derived_subject_invalid); + suite_add_tcase (suite, tc); + + /* Test case for "quote" */ + tc = tcase_create ("quote"); + tcase_add_test (tc, test_quote_regular); + tcase_add_test (tc, test_quote_invalid); + suite_add_tcase (suite, tc); + + /* Test case for "cite" */ + tc = tcase_create ("cite"); + tcase_add_test (tc, test_cite_regular); + tcase_add_test (tc, test_cite_invalid); + suite_add_tcase (suite, tc); + + /* Test case for "inline" */ + tc = tcase_create ("inline"); + tcase_add_test (tc, test_inline_regular); + tcase_add_test (tc, test_inline_invalid); + suite_add_tcase (suite, tc); + + /* Test case for "remove address" */ + tc = tcase_create ("remove_address"); + tcase_add_test (tc, test_remove_address_regular); + tcase_add_test (tc, test_remove_address_limits); + tcase_add_test (tc, test_remove_address_invalid); + suite_add_tcase (suite, tc); + + /* Test case for "convert to html" */ + tc = tcase_create ("convert_to_html"); + tcase_add_test (tc, test_convert_to_html_regular); + tcase_add_test (tc, test_convert_to_html_invalid); + suite_add_tcase (suite, tc); return suite; } +/* --------------------- Main program ------------------- */ -int +gint main () { SRunner *srunner; @@ -115,7 +829,7 @@ main () suite = text_utils_suite (); srunner = srunner_create (suite); - srunner_run_all (srunner, CK_NORMAL); + srunner_run_all (srunner, CK_ENV); failures = srunner_ntests_failed (srunner); srunner_free (srunner); diff --git a/tests/check_update-account.c b/tests/check_update-account.c index 6290707..b9213d9 100644 --- a/tests/check_update-account.c +++ b/tests/check_update-account.c @@ -91,7 +91,7 @@ func (gpointer_data) g_object_unref (G_OBJECT (iter)); g_object_unref (G_OBJECT (accounts)); - queue = modest_mail_operation_queue_get_instance (); + queue = modest_tny_platform_factory_get_modest_mail_operation_queue_instance (fact); mail_op = modest_mail_operation_new (); g_signal_connect (G_OBJECT (mail_op), "progress_changed",