1
/* tinymail - Tiny Mail unit test
2
 * Copyright (C) 2006 Øystein Gisnås <oystein@gisnas.net>
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with self library; if not, write to the
16
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
 * Boston, MA 02110-1301, USA.
18
 */
19
20
#include "check_libtinymail.h"
21
22
#include <gtk/gtk.h>
23
24
#include <tny-folder.h>
25
#include <tny-camel-folder.h>
26
#include <tny-folder-store.h>
27
#include <tny-list.h>
28
#include <tny-iterator.h>
29
#include <tny-simple-list.h>
30
#include <tny-account-store.h>
31
#include <tny-store-account.h>
32
#include <tny-folder.h>
33
#include <tny-camel-header.h>
34
35
#include <account-store.h>
36
37
static TnyFolderStore *account;
38
39
static void
40
tny_folder_store_test_setup (void)
41
{
42
	TnyIterator *aiter;
43
	TnyList *accounts = tny_simple_list_new ();
44
	TnyAccountStore *account_store = tny_test_account_store_new (TRUE, NULL);
45
	tny_account_store_get_accounts (account_store, accounts, TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
46
	g_object_unref (account_store);
47
	aiter = tny_list_create_iterator (accounts);
48
	tny_iterator_first (aiter);
49
	account = TNY_FOLDER_STORE (tny_iterator_get_current (aiter));
50
	g_object_unref (aiter);
51
	g_object_unref (accounts);
52
}
53
54
static void 
55
tny_folder_store_test_teardown (void)
56
{
57
    	g_object_unref (G_OBJECT (account));
58
}
59
60
START_TEST (tny_folder_store_test_get_folders)
61
{
62
     TnyList *folders;
63
#if 0
64
     GError *err = NULL;
65
#endif
66
67
     if (account == NULL)
68
     {
69
	     g_warning ("Test cannot continue (are you online?)");
70
	     return;
71
     }
72
73
     /* Test server has only one root folder */
74
     folders = tny_simple_list_new ();
75
     tny_folder_store_get_folders (account, folders, NULL, TRUE, NULL);
76
     fail_unless (tny_list_get_length (folders) == 1, "There should be only one root folder");
77
     g_object_unref (G_OBJECT (folders));
78
79
     /* Make sure errors are set - invalid URL
80
     folders = tny_simple_list_new ();
81
     tny_account_set_url_string (TNY_ACCOUNT (account), "trigger://error");
82
     tny_folder_store_get_folders (account, folders, NULL, &err);
83
     fail_unless (err != NULL, "An error should be set when the account is invalid");
84
     g_object_unref (G_OBJECT (folders)); */
85
86
}
87
END_TEST
88
89
static void
90
callback (TnyFolderStore *self, TnyList *list, GError **err, gpointer user_data)
91
{
92
     TnyFolder *folder;
93
     TnyIterator *iter = tny_list_create_iterator (list);
94
95
     fail_unless (tny_list_get_length (list) == 1, "Did not find one root folder as expected in callback");     folder = TNY_FOLDER (tny_iterator_get_current (iter));
96
     fail_unless (strcmp (tny_folder_get_id (folder), "INBOX") == 0, "Did not find INBOX");
97
     g_object_unref (G_OBJECT (iter));
98
     g_object_unref (G_OBJECT (folder));
99
     gtk_main_quit ();
100
}
101
102
static gboolean
103
timeout (gpointer data)
104
{
105
     gtk_main_quit ();
106
     return FALSE;
107
}
108
109
static void status_cb (GObject *self, TnyStatus *status, gpointer user_data) {}
110
111
START_TEST (tny_folder_store_test_get_folders_async)
112
{
113
     TnyList *folders;
114
115
     if (account == NULL)
116
     {
117
	     g_warning ("Test cannot continue (are you online?)");
118
	     return;
119
     }
120
121
     folders = tny_simple_list_new ();
122
     tny_folder_store_get_folders_async (account, folders, NULL, TRUE, callback, status_cb, NULL);
123
     g_timeout_add (1000*4, timeout, NULL);
124
     gtk_main ();
125
     fail_unless (tny_list_get_length (folders) == 1, "Did not find one root folder as expected");
126
127
     g_object_unref (G_OBJECT (folders));
128
}
129
END_TEST
130
131
TnyFolderStore *
132
get_inbox (void)
133
{
134
     TnyList *folders = tny_simple_list_new ();
135
     TnyIterator *iter;
136
     TnyFolder *folder;
137
138
     tny_folder_store_get_folders (account, folders, NULL, TRUE, NULL);
139
     /*fail_unless (tny_list_get_length (folders) == 1, "Asserted that there was only one root folder");*/
140
     iter = tny_list_create_iterator (TNY_LIST (folders));
141
     folder = TNY_FOLDER (tny_iterator_get_current (iter));
142
143
     g_object_unref (G_OBJECT (iter));
144
     g_object_unref (G_OBJECT (folders));
145
146
     return (TnyFolderStore *) folder;
147
}
148
149
/* It might be an idea to refactor remove_folder into a separate test case */
150
/* in case the folder exists. We will never get rid of it the way it is now. */
151
START_TEST (tny_folder_store_test_create_remove_folder)
152
{
153
     GError *err;
154
     TnyFolderStore *inbox;
155
     TnyFolder *new_folder;
156
     const gchar *new_folder_name = "tny-folder-store-test_temp-folder";
157
158
     /* Make sure errors are set - cannot add folder to root */
159
     err = NULL;
160
/*
161
	 tny_folder_store_create_folder (account, "tny-folder-store-test_temp-folder", &err);
162
     fail_unless (err != NULL, "Expected an error when trying to create a top level folder");
163
*/
164
165
     /* Create a folder under Inbox. Hopefully we're able to remove it later. */
166
     inbox = get_inbox();
167
     err = NULL;
168
     new_folder = tny_folder_store_create_folder (inbox, new_folder_name, &err);
169
	fail_unless (err != NULL, "Errrr");
170
171
/*     if (err != NULL)
172
	  fail (g_strdup_printf ("The attempt to create folder %s in %s failed. Error message: %s\n", new_folder_name, tny_folder_get_name (TNY_FOLDER (inbox)), err->message));
173
     fail_unless (strcmp (tny_folder_get_name (new_folder), new_folder_name) == 0, "A new folder was not created. Did it already exist?");
174
*/
175
176
     /* Attempt to remove the new folder */
177
     /*err = NULL;
178
     tny_folder_store_remove_folder (inbox, new_folder, &err);
179
     if (err != NULL)
180
	  fail (g_strdup_printf ("Could not remove folder %s from %s. Error message: %s\n", new_folder_name, tny_folder_get_name (TNY_FOLDER (inbox)), err->message));
181
     fail_unless (tny_folder_get_id (new_folder) == NULL, "The folder ID of a removed folder was still set after removal");*/
182
183
184
     g_object_unref (G_OBJECT (new_folder));
185
     g_object_unref (G_OBJECT (inbox));
186
187
}
188
END_TEST
189
190
Suite *
191
create_tny_folder_store_suite (void)
192
{
193
     TCase *tc = NULL;
194
     Suite *s = suite_create ("Folder Store");
195
196
     tc = tcase_create ("Get Folders");
197
     tcase_add_checked_fixture (tc, tny_folder_store_test_setup, tny_folder_store_test_teardown);
198
     tcase_add_test (tc, tny_folder_store_test_get_folders);
199
     suite_add_tcase (s, tc);
200
201
     tc = tcase_create ("Get Folders Async");
202
     tcase_set_timeout (tc, 5);
203
     tcase_add_checked_fixture (tc, tny_folder_store_test_setup, tny_folder_store_test_teardown);
204
     tcase_add_test (tc, tny_folder_store_test_get_folders_async);
205
     suite_add_tcase (s, tc);
206
207
     tc = tcase_create ("Create and Remove Folder");
208
     tcase_set_timeout (tc, 10);
209
     tcase_add_checked_fixture (tc, tny_folder_store_test_setup, tny_folder_store_test_teardown);
210
     tcase_add_test (tc, tny_folder_store_test_create_remove_folder);
211
     suite_add_tcase (s, tc);
212
213
     return s;
214
}