1
2/* Unit tests for GVfs
3 * Copyright (C) 2011 Red Hat, Inc
4 * Author: Matthias Clasen
5 *
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
9 *
10 * This work 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.
13 *
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
22 */
23
24#include <gio/gio.h>
25
26static GFile *
27test_vfs_parse_name (GVfs *vfs,
28 const char *parse_name,
29 gpointer user_data)
30{
31 GFile *file = NULL;
32
33 if (g_strcmp0 (str1: (parse_name), str2: "testfile") == 0)
34 {
35 file = g_file_new_for_uri (uri: "file:///");
36 g_object_set_data (G_OBJECT (file), key: "testfile", GINT_TO_POINTER (1));
37 }
38
39 return file;
40}
41
42static GFile *
43test_vfs_lookup (GVfs *vfs,
44 const char *uri,
45 gpointer user_data)
46{
47 GFile *file;
48 file = g_file_new_for_uri (uri: "file:///");
49 g_object_set_data (G_OBJECT (file), key: "testfile", GINT_TO_POINTER (1));
50
51 return file;
52}
53
54static void
55test_register_scheme (void)
56{
57 GVfs *vfs;
58 GFile *file;
59 const gchar * const *schemes;
60 gboolean res;
61
62 vfs = g_vfs_get_default ();
63 g_assert_nonnull (vfs);
64 g_assert_true (g_vfs_is_active (vfs));
65
66 schemes = g_vfs_get_supported_uri_schemes (vfs);
67 g_assert_false (g_strv_contains (schemes, "test"));
68
69 res = g_vfs_unregister_uri_scheme (vfs, scheme: "test");
70 g_assert_false (res);
71
72 res = g_vfs_register_uri_scheme (vfs, scheme: "test",
73 uri_func: test_vfs_lookup, NULL, NULL,
74 parse_name_func: test_vfs_parse_name, NULL, NULL);
75 g_assert_true (res);
76
77 schemes = g_vfs_get_supported_uri_schemes (vfs);
78 g_assert_true (g_strv_contains (schemes, "test"));
79
80 file = g_file_new_for_uri (uri: "test:///foo");
81 g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
82 g_object_unref (object: file);
83
84 file = g_file_parse_name (parse_name: "testfile");
85 g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
86 g_object_unref (object: file);
87
88 res = g_vfs_register_uri_scheme (vfs, scheme: "test",
89 uri_func: test_vfs_lookup, NULL, NULL,
90 parse_name_func: test_vfs_parse_name, NULL, NULL);
91 g_assert_false (res);
92
93 res = g_vfs_unregister_uri_scheme (vfs, scheme: "test");
94 g_assert_true (res);
95
96 file = g_file_new_for_uri (uri: "test:///foo");
97 g_assert_null (g_object_get_data (G_OBJECT (file), "testfile"));
98 g_object_unref (object: file);
99}
100
101static void
102test_local (void)
103{
104 GVfs *vfs;
105 GFile *file;
106 gchar **schemes;
107
108 vfs = g_vfs_get_local ();
109 g_assert (g_vfs_is_active (vfs));
110
111 file = g_vfs_get_file_for_uri (vfs, uri: "not a good uri");
112 g_assert (G_IS_FILE (file));
113 g_object_unref (object: file);
114
115 schemes = (gchar **)g_vfs_get_supported_uri_schemes (vfs);
116
117 g_assert (g_strv_length (schemes) > 0);
118 g_assert_cmpstr (schemes[0], ==, "file");
119}
120
121int
122main (int argc, char *argv[])
123{
124 g_test_init (argc: &argc, argv: &argv, NULL);
125
126 g_test_add_func (testpath: "/gvfs/local", test_func: test_local);
127 g_test_add_func (testpath: "/gvfs/register-scheme", test_func: test_register_scheme);
128
129 return g_test_run ();
130}
131

source code of gtk/subprojects/glib/gio/tests/vfs.c