~ubuntu-branches/ubuntu/trusty/gcr/trusty-proposed

« back to all changes in this revision

Viewing changes to gcr/tests/test-memory-icon.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-03 10:18:39 UTC
  • Revision ID: package-import@ubuntu.com-20120503101839-wuvloldm7gmdsnij
Tags: upstream-3.4.1
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
   Copyright (C) 2011 Collabora Ltd
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
 
 
20
   Author: Stef Walter <stefw@collabora.co.uk>
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "gcr/gcr-memory-icon.h"
 
26
 
 
27
#include "egg/egg-testing.h"
 
28
 
 
29
#include <glib.h>
 
30
 
 
31
#include <errno.h>
 
32
#include <stdlib.h>
 
33
#include <string.h>
 
34
 
 
35
typedef struct {
 
36
        GIcon *icon;
 
37
        GAsyncResult *result;
 
38
} Test;
 
39
 
 
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,
 
57
};
 
58
 
 
59
static void
 
60
setup (Test *test, gconstpointer unused)
 
61
{
 
62
        test->icon = _gcr_memory_icon_new ("application/octet-stream",
 
63
                                           test_data, sizeof (test_data));
 
64
}
 
65
 
 
66
static void
 
67
teardown (Test *test, gconstpointer unused)
 
68
{
 
69
        g_object_unref (test->icon);
 
70
 
 
71
        if (test->result) {
 
72
                g_object_unref (test->result);
 
73
                egg_assert_not_object (test->result);
 
74
        }
 
75
 
 
76
        egg_assert_not_object (test->icon);
 
77
}
 
78
 
 
79
 
 
80
static void
 
81
test_equal_same (Test *test, gconstpointer unused)
 
82
{
 
83
        g_assert (g_icon_equal (test->icon, test->icon) == TRUE);
 
84
}
 
85
 
 
86
static void
 
87
test_equal_not (Test *test, gconstpointer unused)
 
88
{
 
89
        GIcon *icon;
 
90
 
 
91
        icon = g_themed_icon_new ("folder");
 
92
        g_assert (g_icon_equal (test->icon, icon) == FALSE);
 
93
        g_object_unref (icon);
 
94
}
 
95
 
 
96
static void
 
97
test_equal_data (Test *test, gconstpointer unused)
 
98
{
 
99
        GIcon *icon;
 
100
 
 
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);
 
106
}
 
107
 
 
108
static void
 
109
test_different_type (Test *test, gconstpointer unused)
 
110
{
 
111
        GIcon *icon;
 
112
 
 
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);
 
118
}
 
119
 
 
120
static void
 
121
test_different_offset (Test *test, gconstpointer unused)
 
122
{
 
123
        GIcon *icon;
 
124
 
 
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);
 
130
}
 
131
 
 
132
static void
 
133
test_load_sync (Test *test, gconstpointer unused)
 
134
{
 
135
        GError *error = NULL;
 
136
        GInputStream *is;
 
137
        gchar buf[1024];
 
138
        gsize length;
 
139
        gchar *type;
 
140
 
 
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");
 
145
 
 
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));
 
151
 
 
152
        g_free (type);
 
153
        g_object_unref (is);
 
154
}
 
155
 
 
156
static void
 
157
on_async_ready (GObject *source, GAsyncResult *result, gpointer user_data)
 
158
{
 
159
        Test *test = user_data;
 
160
        GObject *result_source;
 
161
 
 
162
        g_assert (G_OBJECT (test->icon) == source);
 
163
        g_assert (test->result == NULL);
 
164
 
 
165
        result_source = g_async_result_get_source_object (result);
 
166
        g_assert (result_source == source);
 
167
        g_object_unref (result_source);
 
168
 
 
169
        test->result = g_object_ref (result);
 
170
        egg_test_wait_stop ();
 
171
}
 
172
 
 
173
static void
 
174
test_load_async (Test *test, gconstpointer unused)
 
175
{
 
176
        GError *error = NULL;
 
177
        GInputStream *is;
 
178
        gchar buf[1024];
 
179
        gsize length;
 
180
        gchar *type;
 
181
 
 
182
        g_loadable_icon_load_async (G_LOADABLE_ICON (test->icon), 0, NULL, on_async_ready, test);
 
183
        egg_test_wait_until (500);
 
184
 
 
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");
 
190
 
 
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));
 
196
 
 
197
        g_free (type);
 
198
        g_object_unref (is);
 
199
        egg_assert_not_object (is);
 
200
}
 
201
 
 
202
int
 
203
main (int argc, char **argv)
 
204
{
 
205
        g_type_init ();
 
206
        g_test_init (&argc, &argv, NULL);
 
207
 
 
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);
 
215
 
 
216
        return egg_tests_run_with_loop ();
 
217
}