Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / volumemonitor.c
1 #include <gio/gio.h>
2
3 static GVolumeMonitor *monitor;
4
5 static void
6 do_mount_tests (GDrive *drive, GVolume *volume, GMount *mount)
7 {
8   GDrive *d;
9   GVolume *v;
10   gchar *name;
11   gchar *uuid;
12
13   name = g_mount_get_name (mount);
14   g_assert (name != NULL);
15   g_free (name);
16
17   v = g_mount_get_volume (mount);
18   g_assert (v == volume);
19   if (v != NULL)
20     g_object_unref (v);
21
22   d = g_mount_get_drive (mount);
23   g_assert (d == drive);
24   if (d != NULL)
25     g_object_unref (d);
26
27   uuid = g_mount_get_uuid (mount);
28   if (uuid)
29     {
30       GMount *m;
31       m = g_volume_monitor_get_mount_for_uuid (monitor, uuid);
32       g_assert (m == mount);
33       g_object_unref (m);
34       g_free (uuid);
35     }
36 }
37
38 static void
39 do_volume_tests (GDrive *drive, GVolume *volume)
40 {
41   GDrive *d;
42   gchar *name;
43   GMount *mount;
44   gchar *uuid;
45
46   name = g_volume_get_name (volume);
47   g_assert (name != NULL);
48   g_free (name);
49
50   d = g_volume_get_drive (volume);
51   g_assert (d == drive);
52   g_object_unref (d);
53
54   mount = g_volume_get_mount (volume);
55   if (mount != NULL)
56     {
57       do_mount_tests (drive, volume, mount);
58       g_object_unref (mount);
59     }
60
61   uuid = g_volume_get_uuid (volume);
62   if (uuid)
63     {
64       GVolume *v;
65       v = g_volume_monitor_get_volume_for_uuid (monitor, uuid);
66       g_assert (v == volume);
67       g_object_unref (v);
68       g_free (uuid);
69     }
70 }
71
72 static void
73 do_drive_tests (GDrive *drive)
74 {
75   GList *volumes, *l;
76   gchar *name;
77   gboolean has_volumes;
78
79   g_assert (G_IS_DRIVE (drive));
80   name = g_drive_get_name (drive);
81   g_assert (name != NULL);
82   g_free (name);
83
84   has_volumes = g_drive_has_volumes (drive);
85   volumes = g_drive_get_volumes (drive);
86   g_assert (has_volumes == (volumes != NULL));
87   for (l = volumes; l; l = l->next)
88     {
89       GVolume *volume = l->data;
90       do_volume_tests (drive, volume);
91     }
92
93   g_list_foreach (volumes, (GFunc)g_object_unref,  NULL);
94   g_list_free (volumes);
95 }
96
97 static void
98 test_connected_drives (void)
99 {
100   GList *drives;
101   GList *l;
102
103   drives = g_volume_monitor_get_connected_drives (monitor);
104
105   for (l = drives; l; l = l->next)
106     {
107       GDrive *drive = l->data;
108       do_drive_tests (drive);
109     }
110
111   g_list_foreach (drives, (GFunc)g_object_unref,  NULL);
112   g_list_free (drives);
113 }
114
115 static void
116 test_volumes (void)
117 {
118   GList *volumes, *l;
119
120   volumes = g_volume_monitor_get_volumes (monitor);
121
122   for (l = volumes; l; l = l->next)
123     {
124       GVolume *volume = l->data;
125       GDrive *drive;
126
127       drive = g_volume_get_drive (volume);
128       do_volume_tests (drive, volume);
129       g_object_unref (drive);
130     }
131
132   g_list_foreach (volumes, (GFunc)g_object_unref,  NULL);
133   g_list_free (volumes);
134 }
135
136 static void
137 test_mounts (void)
138 {
139   GList *mounts, *l;
140
141   mounts = g_volume_monitor_get_mounts (monitor);
142
143   for (l = mounts; l; l = l->next)
144     {
145       GMount *mount = l->data;
146       GVolume *volume;
147       GDrive *drive;
148
149       drive = g_mount_get_drive (mount);
150       volume = g_mount_get_volume (mount);
151       do_mount_tests (drive, volume, mount);
152
153       if (drive != NULL)
154         g_object_unref (drive);
155       if (volume != NULL)
156         g_object_unref (volume);
157     }
158
159   g_list_foreach (mounts, (GFunc)g_object_unref,  NULL);
160   g_list_free (mounts);
161 }
162 int
163 main (int argc, char *argv[])
164 {
165   gboolean ret;
166
167   g_setenv ("GIO_USE_VFS", "local", FALSE);
168
169   g_type_init ();
170
171   g_test_init (&argc, &argv, NULL);
172
173   monitor = g_volume_monitor_get ();
174
175   g_test_add_func ("/volumemonitor/connected_drives", test_connected_drives);
176   g_test_add_func ("/volumemonitor/volumes", test_volumes);
177   g_test_add_func ("/volumemonitor/mounts", test_mounts);
178
179   ret = g_test_run ();
180
181   g_object_unref (monitor);
182
183   return ret;
184 }
185