Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright © 2018 Endless Mobile, Inc.
4 : : *
5 : : * This library is free software; you can redistribute it and/or
6 : : * modify it under the terms of the GNU Lesser General Public
7 : : * License as published by the Free Software Foundation; either
8 : : * version 2.1 of the License, or (at your option) any later version.
9 : : *
10 : : * This 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 : : * Lesser General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU Lesser General Public
16 : : * License along with this library; if not, write to the Free Software
17 : : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 : : *
19 : : * Authors:
20 : : * - Philip Withnall <withnall@endlessm.com>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include <glib.h>
26 : : #include <gio/gio.h>
27 : : #include <libmogwai-schedule/scheduler.h>
28 : : #include <libmogwai-schedule/schedule-entry.h>
29 : : #include <libmogwai-schedule/tests/signal-logger.h>
30 : : #include <locale.h>
31 : :
32 : :
33 : : /* Test that constructing a #MwsScheduleEntry works. A basic smoketest. */
34 : : static void
35 : 1 : test_schedule_entry_construction (void)
36 : : {
37 : 2 : g_autoptr(MwsScheduleEntry) entry = mws_schedule_entry_new (":owner.1");
38 : :
39 [ - + ]: 1 : g_assert_nonnull (entry);
40 [ - + ]: 1 : g_assert_true (MWS_IS_SCHEDULE_ENTRY (entry));
41 : :
42 [ - + ]: 1 : g_assert_nonnull (mws_schedule_entry_get_id (entry));
43 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_id (entry), !=, "");
44 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_owner (entry), ==, ":owner.1");
45 [ - + ]: 1 : g_assert_false (mws_schedule_entry_get_resumable (entry));
46 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_priority (entry), ==, 0);
47 : 1 : }
48 : :
49 : : /* Test that constructing a complete #MwsScheduleEntry from a #GVariant works. */
50 : : static void
51 : 1 : test_schedule_entry_construction_variant (void)
52 : : {
53 : 1 : g_autoptr(GError) local_error = NULL;
54 : 1 : g_autoptr(MwsScheduleEntry) entry = NULL;
55 : 1 : entry = mws_schedule_entry_new_from_variant (":owner.1",
56 : : g_variant_new_parsed (
57 : : "@a{sv} {"
58 : : "'resumable': <@b true>,"
59 : : "'priority': <@u 5>"
60 : : "}"),
61 : : &local_error);
62 : :
63 [ - + ]: 1 : g_assert_no_error (local_error);
64 [ - + ]: 1 : g_assert_nonnull (entry);
65 [ - + ]: 1 : g_assert_true (MWS_IS_SCHEDULE_ENTRY (entry));
66 : :
67 [ - + ]: 1 : g_assert_nonnull (mws_schedule_entry_get_id (entry));
68 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_id (entry), !=, "");
69 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_owner (entry), ==, ":owner.1");
70 [ - + ]: 1 : g_assert_true (mws_schedule_entry_get_resumable (entry));
71 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_priority (entry), ==, 5);
72 : 1 : }
73 : :
74 : : /* Test that constructing a complete #MwsScheduleEntry from a %NULL #GVariant
75 : : * works. */
76 : : static void
77 : 1 : test_schedule_entry_construction_variant_null (void)
78 : : {
79 : 1 : g_autoptr(GError) local_error = NULL;
80 : 1 : g_autoptr(MwsScheduleEntry) entry = NULL;
81 : 1 : entry = mws_schedule_entry_new_from_variant (":owner.1", NULL, &local_error);
82 : :
83 [ - + ]: 1 : g_assert_no_error (local_error);
84 [ - + ]: 1 : g_assert_nonnull (entry);
85 [ - + ]: 1 : g_assert_true (MWS_IS_SCHEDULE_ENTRY (entry));
86 : :
87 [ - + ]: 1 : g_assert_nonnull (mws_schedule_entry_get_id (entry));
88 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_id (entry), !=, "");
89 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_owner (entry), ==, ":owner.1");
90 [ - + ]: 1 : g_assert_false (mws_schedule_entry_get_resumable (entry));
91 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_priority (entry), ==, 0);
92 : 1 : }
93 : :
94 : : /* Test that constructing a complete #MwsScheduleEntry from a #GVariant works,
95 : : * and an unknown entry in the #GVariant is ignored. */
96 : : static void
97 : 1 : test_schedule_entry_construction_variant_unknown (void)
98 : : {
99 : 1 : g_autoptr(GError) local_error = NULL;
100 : 1 : g_autoptr(MwsScheduleEntry) entry = NULL;
101 : 1 : entry = mws_schedule_entry_new_from_variant (":owner.1",
102 : : g_variant_new_parsed (
103 : : "@a{sv} {"
104 : : "'resumable': <@b false>,"
105 : : "'priority': <@u 500>,"
106 : : "'unknown value': <@b true>"
107 : : "}"),
108 : : &local_error);
109 : :
110 [ - + ]: 1 : g_assert_no_error (local_error);
111 [ - + ]: 1 : g_assert_nonnull (entry);
112 [ - + ]: 1 : g_assert_true (MWS_IS_SCHEDULE_ENTRY (entry));
113 : :
114 [ - + ]: 1 : g_assert_nonnull (mws_schedule_entry_get_id (entry));
115 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_id (entry), !=, "");
116 [ - + ]: 1 : g_assert_cmpstr (mws_schedule_entry_get_owner (entry), ==, ":owner.1");
117 [ - + ]: 1 : g_assert_false (mws_schedule_entry_get_resumable (entry));
118 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_priority (entry), ==, 500);
119 : 1 : }
120 : :
121 : : /* Test that constructing a complete #MwsScheduleEntry from a #GVariant fails
122 : : * with an error if one of the #GVariant parameters is the wrong type. */
123 : : static void
124 : 1 : test_schedule_entry_construction_variant_invalid_type (void)
125 : : {
126 : 1 : g_autoptr(GError) local_error = NULL;
127 : 1 : g_autoptr(MwsScheduleEntry) entry = NULL;
128 : 1 : entry = mws_schedule_entry_new_from_variant (":owner.1",
129 : : g_variant_new_parsed (
130 : : "@a{sv} {"
131 : : "'resumable': <@u 1000>,"
132 : : "'priority': <@u 500>"
133 : : "}"),
134 : : &local_error);
135 : :
136 [ + - + - : 1 : g_assert_error (local_error, MWS_SCHEDULER_ERROR,
- + ]
137 : : MWS_SCHEDULER_ERROR_INVALID_PARAMETERS);
138 [ - + ]: 1 : g_assert_null (entry);
139 : 1 : }
140 : :
141 : : /* Check that newly constructed entries all have different IDs. */
142 : : static void
143 : 1 : test_schedule_entry_different_ids (void)
144 : : {
145 : 2 : g_autoptr(MwsScheduleEntry) entry1 = mws_schedule_entry_new (":owner.1");
146 : 2 : g_autoptr(MwsScheduleEntry) entry2 = mws_schedule_entry_new (":owner.1");
147 : 2 : g_autoptr(MwsScheduleEntry) entry3 = mws_schedule_entry_new (":owner.1");
148 : :
149 : 1 : const gchar *id1 = mws_schedule_entry_get_id (entry1);
150 : 1 : const gchar *id2 = mws_schedule_entry_get_id (entry2);
151 : 1 : const gchar *id3 = mws_schedule_entry_get_id (entry3);
152 : :
153 [ - + ]: 1 : g_assert_cmpstr (id1, !=, id2);
154 [ - + ]: 1 : g_assert_cmpstr (id2, !=, id3);
155 [ - + ]: 1 : g_assert_cmpstr (id3, !=, id1);
156 : 1 : }
157 : :
158 : : /* Check that g_object_get() and g_object_set() work on #MwsScheduleEntry
159 : : * properties. */
160 : : static void
161 : 1 : test_schedule_entry_properties (void)
162 : : {
163 : 2 : g_autoptr(MwsScheduleEntry) entry = mws_schedule_entry_new (":owner.1");
164 : :
165 : 1 : g_autofree gchar *id = NULL;
166 : 1 : g_autofree gchar *owner = NULL;
167 : : gboolean resumable;
168 : : guint32 priority;
169 : :
170 : 1 : g_object_get (entry,
171 : : "id", &id,
172 : : "owner", &owner,
173 : : "resumable", &resumable,
174 : : "priority", &priority,
175 : : NULL);
176 : :
177 [ - + ]: 1 : g_assert_cmpstr (id, ==, mws_schedule_entry_get_id (entry));
178 [ - + ]: 1 : g_assert_cmpstr (owner, ==, mws_schedule_entry_get_owner (entry));
179 [ - + ]: 1 : g_assert_cmpstr (owner, ==, ":owner.1");
180 [ - + ]: 1 : g_assert_cmpuint (resumable, ==, mws_schedule_entry_get_resumable (entry));
181 [ - + ]: 1 : g_assert_cmpuint (priority, ==, mws_schedule_entry_get_priority (entry));
182 : :
183 : 2 : g_autoptr(MwsSignalLogger) logger = mws_signal_logger_new ();
184 : 1 : mws_signal_logger_connect (logger, entry, "notify");
185 : :
186 : 1 : g_object_set (entry,
187 : : "resumable", !resumable,
188 : : "priority", priority + 1,
189 : : NULL);
190 : :
191 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_resumable (entry), ==, !resumable);
192 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_priority (entry), ==, priority + 1);
193 : :
194 [ + - + - : 2 : mws_signal_logger_assert_notify_emission_pop (logger, entry, "priority");
- + - - -
+ ]
195 [ + - + - : 2 : mws_signal_logger_assert_notify_emission_pop (logger, entry, "resumable");
- + - - -
+ ]
196 [ - + ]: 1 : mws_signal_logger_assert_no_emissions (logger);
197 : 1 : }
198 : :
199 : : /* Test getting and setting the priority property.. */
200 : : static void
201 : 1 : test_schedule_entry_properties_priority (void)
202 : : {
203 : 2 : g_autoptr(MwsScheduleEntry) entry = mws_schedule_entry_new (":owner.1");
204 : :
205 : 2 : g_autoptr(MwsSignalLogger) logger = mws_signal_logger_new ();
206 : 1 : mws_signal_logger_connect (logger, entry, "notify::priority");
207 : :
208 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_priority (entry), ==, 0);
209 : :
210 : 1 : mws_schedule_entry_set_priority (entry, 0);
211 [ - + ]: 1 : mws_signal_logger_assert_no_emissions (logger);
212 : :
213 : 1 : mws_schedule_entry_set_priority (entry, 5);
214 [ + - + - : 2 : mws_signal_logger_assert_notify_emission_pop (logger, entry, "priority");
+ - + - -
+ ]
215 [ - + ]: 1 : mws_signal_logger_assert_no_emissions (logger);
216 : :
217 [ - + ]: 1 : g_assert_cmpuint (mws_schedule_entry_get_priority (entry), ==, 5);
218 : 1 : }
219 : :
220 : : /* Test getting and setting the resumable property.. */
221 : : static void
222 : 1 : test_schedule_entry_properties_resumable (void)
223 : : {
224 : 2 : g_autoptr(MwsScheduleEntry) entry = mws_schedule_entry_new (":owner.1");
225 : :
226 : 2 : g_autoptr(MwsSignalLogger) logger = mws_signal_logger_new ();
227 : 1 : mws_signal_logger_connect (logger, entry, "notify::resumable");
228 : :
229 [ - + ]: 1 : g_assert_false (mws_schedule_entry_get_resumable (entry));
230 : :
231 : 1 : mws_schedule_entry_set_resumable (entry, FALSE);
232 [ - + ]: 1 : mws_signal_logger_assert_no_emissions (logger);
233 : :
234 : 1 : mws_schedule_entry_set_resumable (entry, TRUE);
235 [ + - + - : 2 : mws_signal_logger_assert_notify_emission_pop (logger, entry, "resumable");
+ - + - -
+ ]
236 [ - + ]: 1 : mws_signal_logger_assert_no_emissions (logger);
237 : :
238 [ - + ]: 1 : g_assert_true (mws_schedule_entry_get_resumable (entry));
239 : 1 : }
240 : :
241 : : int
242 : 1 : main (int argc,
243 : : char **argv)
244 : : {
245 : 1 : setlocale (LC_ALL, "");
246 : 1 : g_test_init (&argc, &argv, NULL);
247 : :
248 : 1 : g_test_add_func ("/schedule-entry/construction",
249 : : test_schedule_entry_construction);
250 : 1 : g_test_add_func ("/schedule-entry/construction/variant",
251 : : test_schedule_entry_construction_variant);
252 : 1 : g_test_add_func ("/schedule-entry/construction/variant/null",
253 : : test_schedule_entry_construction_variant_null);
254 : 1 : g_test_add_func ("/schedule-entry/construction/variant/unknown",
255 : : test_schedule_entry_construction_variant_unknown);
256 : 1 : g_test_add_func ("/schedule-entry/construction/variant/invalid-type",
257 : : test_schedule_entry_construction_variant_invalid_type);
258 : 1 : g_test_add_func ("/schedule-entry/different-ids",
259 : : test_schedule_entry_different_ids);
260 : 1 : g_test_add_func ("/schedule-entry/properties",
261 : : test_schedule_entry_properties);
262 : 1 : g_test_add_func ("/schedule-entry/properties/priority",
263 : : test_schedule_entry_properties_priority);
264 : 1 : g_test_add_func ("/schedule-entry/properties/resumable",
265 : : test_schedule_entry_properties_resumable);
266 : :
267 : 1 : return g_test_run ();
268 : : }
|