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