1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3
Copyright (C) 2011 Collabora Ltd
5
The Gnome Keyring Library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public License as
7
published by the Free Software Foundation; either version 2 of the
8
License, or (at your option) any later version.
10
The Gnome Keyring Library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with the Gnome Library; see the file COPYING.LIB. If not,
17
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
20
Author: Stef Walter <stefw@collabora.co.uk>
25
#include "gcr/gcr-memory-icon.h"
27
#include "egg/egg-testing.h"
40
static const guint8 test_data[256] = {
41
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
42
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
43
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
44
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
45
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
46
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
47
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
48
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
49
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
50
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
51
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
52
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
53
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
54
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
55
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
56
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
60
setup (Test *test, gconstpointer unused)
62
test->icon = _gcr_memory_icon_new ("application/octet-stream",
63
test_data, sizeof (test_data));
67
teardown (Test *test, gconstpointer unused)
69
g_object_unref (test->icon);
72
g_object_unref (test->result);
73
egg_assert_not_object (test->result);
76
egg_assert_not_object (test->icon);
81
test_equal_same (Test *test, gconstpointer unused)
83
g_assert (g_icon_equal (test->icon, test->icon) == TRUE);
87
test_equal_not (Test *test, gconstpointer unused)
91
icon = g_themed_icon_new ("folder");
92
g_assert (g_icon_equal (test->icon, icon) == FALSE);
93
g_object_unref (icon);
97
test_equal_data (Test *test, gconstpointer unused)
101
icon = _gcr_memory_icon_new ("application/octet-stream",
102
test_data, sizeof (test_data));
103
g_assert (g_icon_equal (test->icon, icon) == TRUE);
104
g_assert_cmpuint (g_icon_hash (test->icon), ==, g_icon_hash (icon));
105
g_object_unref (icon);
109
test_different_type (Test *test, gconstpointer unused)
113
icon = _gcr_memory_icon_new ("application/x-other",
114
test_data, sizeof (test_data));
115
g_assert (g_icon_equal (test->icon, icon) == FALSE);
116
g_assert_cmpuint (g_icon_hash (test->icon), !=, g_icon_hash (icon));
117
g_object_unref (icon);
121
test_different_offset (Test *test, gconstpointer unused)
125
icon = _gcr_memory_icon_new_full ("application/octet-stream", (gpointer)test_data,
126
sizeof (test_data) - 16, 16, NULL);
127
g_assert (g_icon_equal (test->icon, icon) == FALSE);
128
g_assert_cmpuint (g_icon_hash (test->icon), !=, g_icon_hash (icon));
129
g_object_unref (icon);
133
test_load_sync (Test *test, gconstpointer unused)
135
GError *error = NULL;
141
is = g_loadable_icon_load (G_LOADABLE_ICON (test->icon), 0, &type, NULL, &error);
142
g_assert (is != NULL);
143
g_assert_no_error (error);
144
g_assert_cmpstr (type, ==, "application/octet-stream");
146
if (!g_input_stream_read_all (is, buf, sizeof (buf), &length, NULL, &error))
147
g_assert_not_reached ();
148
g_assert_no_error (error);
149
egg_assert_cmpsize (length, ==, sizeof (test_data));
150
egg_assert_cmpmem (buf, length, ==, test_data, sizeof (test_data));
157
on_async_ready (GObject *source, GAsyncResult *result, gpointer user_data)
159
Test *test = user_data;
160
GObject *result_source;
162
g_assert (G_OBJECT (test->icon) == source);
163
g_assert (test->result == NULL);
165
result_source = g_async_result_get_source_object (result);
166
g_assert (result_source == source);
167
g_object_unref (result_source);
169
test->result = g_object_ref (result);
170
egg_test_wait_stop ();
174
test_load_async (Test *test, gconstpointer unused)
176
GError *error = NULL;
182
g_loadable_icon_load_async (G_LOADABLE_ICON (test->icon), 0, NULL, on_async_ready, test);
183
egg_test_wait_until (500);
185
g_assert (test->result);
186
is = g_loadable_icon_load_finish (G_LOADABLE_ICON (test->icon), test->result, &type, &error);
187
g_assert (is != NULL);
188
g_assert_no_error (error);
189
g_assert_cmpstr (type, ==, "application/octet-stream");
191
if (!g_input_stream_read_all (is, buf, sizeof (buf), &length, NULL, &error))
192
g_assert_not_reached ();
193
g_assert_no_error (error);
194
egg_assert_cmpsize (length, ==, sizeof (test_data));
195
egg_assert_cmpmem (buf, length, ==, test_data, sizeof (test_data));
199
egg_assert_not_object (is);
203
main (int argc, char **argv)
206
g_test_init (&argc, &argv, NULL);
208
g_test_add ("/gcr/memory-icon/equal_same", Test, NULL, setup, test_equal_same, teardown);
209
g_test_add ("/gcr/memory-icon/equal_not", Test, NULL, setup, test_equal_not, teardown);
210
g_test_add ("/gcr/memory-icon/equal_data", Test, NULL, setup, test_equal_data, teardown);
211
g_test_add ("/gcr/memory-icon/different_type", Test, NULL, setup, test_different_type, teardown);
212
g_test_add ("/gcr/memory-icon/different_offset", Test, NULL, setup, test_different_offset, teardown);
213
g_test_add ("/gcr/memory-icon/load_sync", Test, NULL, setup, test_load_sync, teardown);
214
g_test_add ("/gcr/memory-icon/load_async", Test, NULL, setup, test_load_async, teardown);
216
return egg_tests_run_with_loop ();