1
/* tinymail - Tiny Mail unit test
2
 * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
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 <tny-account.h>
23
#include <tny-folder-store.h>
24
#include <tny-camel-store-account.h>
25
#include <tny-account-store.h>
26
#include <tny-store-account.h>
27
#include <tny-folder.h>
28
29
#include <account-store.h>
30
31
#include <tny-folder-store.h>
32
#include <tny-list.h>
33
#include <tny-iterator.h>
34
#include <tny-simple-list.h>
35
#include <tny-camel-header.h>
36
37
#include <tny-folder-store-query.h>
38
39
static TnyAccountStore *account_store;
40
static TnyList *accounts;
41
static TnyIterator *aiter;
42
static TnyStoreAccount *account=NULL;
43
static gboolean online_tests=FALSE;
44
static gchar *str;
45
46
static void
47
tny_folder_store_query_test_setup (void)
48
{
49
	if (online_tests) {
50
		accounts = tny_simple_list_new ();
51
		account_store = tny_test_account_store_new (TRUE, NULL);
52
		tny_account_store_get_accounts (account_store, accounts, 
53
				TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
54
		aiter = tny_list_create_iterator (accounts);
55
		tny_iterator_first (aiter);
56
		account = TNY_STORE_ACCOUNT (tny_iterator_get_current (aiter));
57
		g_object_unref (aiter);
58
59
		if (!account)
60
			online_tests = FALSE;
61
	}
62
63
	return;
64
}
65
66
static void 
67
tny_folder_store_query_test_teardown (void)
68
{
69
	if (online_tests) {
70
		g_object_unref (account);
71
		g_object_unref (accounts);
72
	}
73
	return;
74
}
75
76
START_TEST (tny_folder_store_query_test_match_on_name)
77
{
78
	TnyFolderStoreQuery *query = NULL;
79
	TnyList *folders = NULL, *subfolders;
80
	TnyIterator *iter = NULL;
81
	TnyFolder *folder;
82
	gint length=0;
83
    
84
	if (!online_tests)
85
		return;
86
87
	query = tny_folder_store_query_new ();
88
	tny_folder_store_query_add_item (query, "^tny.*$", TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_NAME);
89
90
	folders = tny_simple_list_new();
91
	subfolders = tny_simple_list_new();
92
93
	tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
94
			folders, NULL, TRUE, NULL);
95
	length = tny_list_get_length (folders);
96
97
	str = g_strdup_printf ("Root should have exactly one folder in the test account, it matches %d\n", length);
98
	fail_unless (length == 1, str);
99
	g_free (str);
100
101
	if (length >= 1)  {
102
		iter = tny_list_create_iterator (folders);
103
		folder = (TnyFolder *) tny_iterator_get_current (iter);
104
		tny_folder_store_get_folders (TNY_FOLDER_STORE (folder),
105
				subfolders, query, TRUE, NULL);
106
		g_object_unref (G_OBJECT (folder));
107
		length = tny_list_get_length (subfolders);
108
		str = g_strdup_printf ("^tny.*$ should match exactly one folder in the test account, it matches %d\n", length);
109
		fail_unless (tny_list_get_length (subfolders) == 1, str);
110
		g_free (str);
111
		g_object_unref (iter);
112
	}
113
114
	g_object_unref (folders);
115
	g_object_unref (subfolders);
116
	g_object_unref (query);
117
118
}
119
END_TEST
120
121
START_TEST (tny_folder_store_query_test_match_on_id)
122
{
123
	TnyFolderStoreQuery *query = NULL;
124
	TnyList *folders = NULL, *subfolders;
125
	TnyIterator *iter = NULL;
126
	TnyFolder *folder;
127
	gint length=0;
128
129
	if (!online_tests)
130
		return;
131
132
	query = tny_folder_store_query_new ();
133
	tny_folder_store_query_add_item (query, "^INBOX/tny.*$", TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_ID);
134
135
	folders = tny_simple_list_new();
136
	subfolders = tny_simple_list_new();
137
138
	tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
139
			folders, NULL, TRUE, NULL);
140
	length = tny_list_get_length (folders);
141
142
	str = g_strdup_printf ("Root should have exactly one folder in the test account, it matches %d\n", length);
143
	fail_unless (length == 1, str);
144
	g_free (str);
145
146
	if (length >= 1) {
147
		iter = tny_list_create_iterator (folders);
148
		folder = (TnyFolder *) tny_iterator_get_current (iter);
149
		tny_folder_store_get_folders (TNY_FOLDER_STORE (folder),
150
				subfolders, query, TRUE, NULL);
151
		g_object_unref (G_OBJECT (folder));
152
		length = tny_list_get_length (subfolders);
153
		str = g_strdup_printf ("^INBOX/tny.*$ should match exactly one folder in the test account, it matches %d\n", length);
154
		fail_unless (tny_list_get_length (subfolders) == 1, str);
155
		g_object_unref (iter);
156
	}
157
158
	g_object_unref (folders);
159
	g_object_unref (subfolders);
160
	g_object_unref (query);
161
}
162
END_TEST
163
164
165
START_TEST (tny_folder_store_query_test_match_subscribed)
166
{
167
	TnyFolderStoreQuery *query = NULL;
168
	TnyList *folders = NULL, *subfolders;
169
	TnyIterator *iter = NULL;
170
	TnyFolder *folder;
171
	gint length=0;
172
    
173
	if (!online_tests)
174
		return;
175
    
176
	query = tny_folder_store_query_new ();
177
	tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
178
179
	folders = tny_simple_list_new();
180
	subfolders = tny_simple_list_new();
181
182
	tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
183
			folders, NULL, TRUE, NULL);
184
    	length = tny_list_get_length (folders);
185
    
186
	str = g_strdup_printf ("Root should have exactly one folder in the test account, it matches %d\n", length);
187
	fail_unless (length == 1, str);
188
	g_free (str);
189
    
190
    	if (length >= 1) 
191
	{	
192
		iter = tny_list_create_iterator (folders);
193
		folder = (TnyFolder *) tny_iterator_get_current (iter);
194
		tny_folder_store_get_folders (TNY_FOLDER_STORE (folder),
195
				subfolders, query, TRUE, NULL);
196
		g_object_unref (G_OBJECT (folder));
197
		length = tny_list_get_length (subfolders);
198
	    
199
		str = g_strdup_printf ("There's 17 subscribed folders in the test account, I received %d\n", length);
200
		fail_unless (tny_list_get_length (subfolders) == 17, str);
201
		g_free (str);
202
		g_object_unref (G_OBJECT (iter));
203
	}
204
205
	g_object_unref (G_OBJECT (folders));
206
	g_object_unref (G_OBJECT (subfolders));
207
	g_object_unref (G_OBJECT (query));
208
}		
209
END_TEST
210
211
212
START_TEST (tny_folder_store_query_test_match_unsubscribed)
213
{
214
	TnyFolderStoreQuery *query = NULL;
215
	TnyList *folders = NULL, *subfolders;
216
	TnyIterator *iter = NULL;
217
	TnyFolder *folder;
218
	gint length=0;
219
    
220
	if (!online_tests)
221
		return;
222
    
223
	query = tny_folder_store_query_new ();
224
	tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_UNSUBSCRIBED);
225
226
	folders = tny_simple_list_new();
227
	subfolders = tny_simple_list_new();
228
229
	tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
230
			folders, NULL, TRUE, NULL);
231
    	length = tny_list_get_length (folders);
232
    
233
	str = g_strdup_printf ("Root should have exactly one folder in the test account, it matches %d\n", length);
234
	fail_unless (length == 1, str);
235
	g_free (str);
236
    
237
    	if (length >= 1) 
238
	{	
239
		iter = tny_list_create_iterator (folders);
240
		folder = (TnyFolder *) tny_iterator_get_current (iter);
241
		tny_folder_store_get_folders (TNY_FOLDER_STORE (folder),
242
				subfolders, query, TRUE, NULL);
243
		g_object_unref (G_OBJECT (folder));
244
		length = tny_list_get_length (subfolders);
245
	    
246
		str = g_strdup_printf ("There's 1 subscribed folder in the test account, I received %d\n", length);
247
		fail_unless (tny_list_get_length (subfolders) == 1, str);
248
		g_free (str);
249
	    
250
		g_object_unref (G_OBJECT (iter));
251
	}
252
253
	g_object_unref (G_OBJECT (folders));
254
	g_object_unref (G_OBJECT (subfolders));
255
	g_object_unref (G_OBJECT (query));
256
}
257
END_TEST
258
259
Suite *
260
create_tny_folder_store_query_suite (void)
261
{
262
     TCase *tc = NULL;
263
     Suite *s = suite_create ("Folder Store Query");
264
265
     tc = tcase_create ("Match Name");
266
     tcase_add_checked_fixture (tc, tny_folder_store_query_test_setup, tny_folder_store_query_test_teardown);
267
     tcase_add_test (tc, tny_folder_store_query_test_match_on_name);
268
     suite_add_tcase (s, tc);
269
270
     tc = tcase_create ("Match Id");
271
     tcase_add_checked_fixture (tc, tny_folder_store_query_test_setup, tny_folder_store_query_test_teardown);
272
     tcase_add_test (tc, tny_folder_store_query_test_match_on_id);
273
     suite_add_tcase (s, tc);
274
275
     tc = tcase_create ("Match Subscribed");
276
     tcase_add_checked_fixture (tc, tny_folder_store_query_test_setup, tny_folder_store_query_test_teardown);
277
     tcase_add_test (tc, tny_folder_store_query_test_match_subscribed);
278
     suite_add_tcase (s, tc);
279
280
     tc = tcase_create ("Match Unsubscribed");
281
     tcase_add_checked_fixture (tc, tny_folder_store_query_test_setup, tny_folder_store_query_test_teardown);
282
     tcase_add_test (tc, tny_folder_store_query_test_match_unsubscribed);
283
     suite_add_tcase (s, tc);
284
285
     return s;
286
}