1 /* ide-test.c
2  *
3  * Copyright 2017-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "ide-test"
22 
23 #include "config.h"
24 
25 #include "ide-foundry-enums.h"
26 
27 #include "ide-test.h"
28 #include "ide-test-private.h"
29 #include "ide-test-provider.h"
30 
31 typedef struct
32 {
33   /* Unowned references */
34   IdeTestProvider *provider;
35 
36   /* Owned references */
37   gchar *display_name;
38   gchar *group;
39   gchar *id;
40 
41   IdeTestStatus status;
42 } IdeTestPrivate;
43 
44 G_DEFINE_TYPE_WITH_PRIVATE (IdeTest, ide_test, G_TYPE_OBJECT)
45 
46 enum {
47   PROP_0,
48   PROP_DISPLAY_NAME,
49   PROP_GROUP,
50   PROP_ID,
51   PROP_STATUS,
52   N_PROPS
53 };
54 
55 static GParamSpec *properties [N_PROPS];
56 
57 IdeTest *
ide_test_new(void)58 ide_test_new (void)
59 {
60   return g_object_new (IDE_TYPE_TEST, NULL);
61 }
62 
63 static void
ide_test_finalize(GObject * object)64 ide_test_finalize (GObject *object)
65 {
66   IdeTest *self = (IdeTest *)object;
67   IdeTestPrivate *priv = ide_test_get_instance_private (self);
68 
69   priv->provider = NULL;
70 
71   g_clear_pointer (&priv->group, g_free);
72   g_clear_pointer (&priv->id, g_free);
73   g_clear_pointer (&priv->display_name, g_free);
74 
75   G_OBJECT_CLASS (ide_test_parent_class)->finalize (object);
76 }
77 
78 static void
ide_test_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)79 ide_test_get_property (GObject    *object,
80                        guint       prop_id,
81                        GValue     *value,
82                        GParamSpec *pspec)
83 {
84   IdeTest *self = IDE_TEST (object);
85 
86   switch (prop_id)
87     {
88     case PROP_ID:
89       g_value_set_string (value, ide_test_get_id (self));
90       break;
91 
92     case PROP_GROUP:
93       g_value_set_string (value, ide_test_get_group (self));
94       break;
95 
96     case PROP_DISPLAY_NAME:
97       g_value_set_string (value, ide_test_get_display_name (self));
98       break;
99 
100     case PROP_STATUS:
101       g_value_set_enum (value, ide_test_get_status (self));
102       break;
103 
104     default:
105       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106     }
107 }
108 
109 static void
ide_test_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)110 ide_test_set_property (GObject      *object,
111                        guint         prop_id,
112                        const GValue *value,
113                        GParamSpec   *pspec)
114 {
115   IdeTest *self = IDE_TEST (object);
116 
117   switch (prop_id)
118     {
119     case PROP_GROUP:
120       ide_test_set_group (self, g_value_get_string (value));
121       break;
122 
123     case PROP_ID:
124       ide_test_set_id (self, g_value_get_string (value));
125       break;
126 
127     case PROP_DISPLAY_NAME:
128       ide_test_set_display_name (self, g_value_get_string (value));
129       break;
130 
131     case PROP_STATUS:
132       ide_test_set_status (self, g_value_get_enum (value));
133       break;
134 
135     default:
136       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137     }
138 }
139 
140 static void
ide_test_class_init(IdeTestClass * klass)141 ide_test_class_init (IdeTestClass *klass)
142 {
143   GObjectClass *object_class = G_OBJECT_CLASS (klass);
144 
145   object_class->finalize = ide_test_finalize;
146   object_class->get_property = ide_test_get_property;
147   object_class->set_property = ide_test_set_property;
148 
149   /**
150    * IdeTest:display_name:
151    *
152    * The "display-name" property contains the display name of the test as
153    * the user is expected to read in UI elements.
154    *
155    * Since: 3.32
156    */
157   properties [PROP_DISPLAY_NAME] =
158     g_param_spec_string ("display-name",
159                          "Name",
160                          "The display_name of the unit test",
161                          NULL,
162                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
163 
164   /**
165    * IdeTest:id:
166    *
167    * The "id" property contains the unique identifier of the test.
168    *
169    * Since: 3.32
170    */
171   properties [PROP_ID] =
172     g_param_spec_string ("id",
173                          "Id",
174                          "The unique identifier of the test",
175                          NULL,
176                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
177 
178   /**
179    * IdeTest:group:
180    *
181    * The "group" property contains the name of the gruop the test belongs
182    * to, if any.
183    *
184    * Since: 3.32
185    */
186   properties [PROP_GROUP] =
187     g_param_spec_string ("group",
188                          "Group",
189                          "The name of the group the test belongs to, if any",
190                          NULL,
191                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
192 
193   /**
194    * IdeTest::status:
195    *
196    * The "status" property contains the status of the test, updated by
197    * providers when they have run the test.
198    *
199    * Since: 3.32
200    */
201   properties [PROP_STATUS] =
202     g_param_spec_enum ("status",
203                        "Status",
204                        "The status of the test",
205                        IDE_TYPE_TEST_STATUS,
206                        IDE_TEST_STATUS_NONE,
207                        (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
208 
209   g_object_class_install_properties (object_class, N_PROPS, properties);
210 }
211 
212 static void
ide_test_init(IdeTest * self)213 ide_test_init (IdeTest *self)
214 {
215 }
216 
217 IdeTestProvider *
_ide_test_get_provider(IdeTest * self)218 _ide_test_get_provider (IdeTest *self)
219 {
220   IdeTestPrivate *priv = ide_test_get_instance_private (self);
221 
222   g_return_val_if_fail (IDE_IS_TEST (self), NULL);
223 
224   return priv->provider;
225 }
226 
227 void
_ide_test_set_provider(IdeTest * self,IdeTestProvider * provider)228 _ide_test_set_provider (IdeTest         *self,
229                         IdeTestProvider *provider)
230 {
231   IdeTestPrivate *priv = ide_test_get_instance_private (self);
232 
233   g_return_if_fail (IDE_IS_TEST (self));
234   g_return_if_fail (!provider || IDE_IS_TEST_PROVIDER (provider));
235 
236   priv->provider = provider;
237 }
238 
239 /**
240  * ide_test_get_display_name:
241  * @self: An #IdeTest
242  *
243  * Gets the "display-name" property of the test.
244  *
245  * Returns: (nullable): The display_name of the test or %NULL
246  *
247  * Since: 3.32
248  */
249 const gchar *
ide_test_get_display_name(IdeTest * self)250 ide_test_get_display_name (IdeTest *self)
251 {
252   IdeTestPrivate *priv = ide_test_get_instance_private (self);
253 
254   g_return_val_if_fail (IDE_IS_TEST (self), NULL);
255 
256   return priv->display_name;
257 }
258 
259 /**
260  * ide_test_set_display_name:
261  * @self: An #IdeTest
262  * @display_name: (nullable): The display_name of the test, or %NULL to unset
263  *
264  * Sets the "display-name" property of the unit test.
265  *
266  * Since: 3.32
267  */
268 void
ide_test_set_display_name(IdeTest * self,const gchar * display_name)269 ide_test_set_display_name (IdeTest     *self,
270                            const gchar *display_name)
271 {
272   IdeTestPrivate *priv = ide_test_get_instance_private (self);
273 
274   g_return_if_fail (IDE_IS_TEST (self));
275 
276   if (g_strcmp0 (display_name, priv->display_name) != 0)
277     {
278       g_free (priv->display_name);
279       priv->display_name = g_strdup (display_name);
280       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_DISPLAY_NAME]);
281     }
282 }
283 
284 /**
285  * ide_test_get_group:
286  * @self: a #IdeTest
287  *
288  * Gets the "group" property.
289  *
290  * The group name is used to group tests together.
291  *
292  * Returns: (nullable): The group name or %NULL.
293  *
294  * Since: 3.32
295  */
296 const gchar *
ide_test_get_group(IdeTest * self)297 ide_test_get_group (IdeTest *self)
298 {
299   IdeTestPrivate *priv = ide_test_get_instance_private (self);
300 
301   g_return_val_if_fail (IDE_IS_TEST (self), NULL);
302 
303   return priv->group;
304 }
305 
306 /**
307  * ide_test_set_group:
308  * @self: a #IdeTest
309  * @group: (nullable): the name of the group or %NULL
310  *
311  * Sets the #IdeTest:group property.
312  *
313  * The group property is used to group related tests together.
314  *
315  * Since: 3.32
316  */
317 void
ide_test_set_group(IdeTest * self,const gchar * group)318 ide_test_set_group (IdeTest     *self,
319                     const gchar *group)
320 {
321   IdeTestPrivate *priv = ide_test_get_instance_private (self);
322 
323   g_return_if_fail (IDE_IS_TEST (self));
324 
325   if (g_strcmp0 (group, priv->group) != 0)
326     {
327       g_free (priv->group);
328       priv->group = g_strdup (group);
329       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_GROUP]);
330     }
331 }
332 
333 /**
334  * ide_test_get_id:
335  * @self: a #IdeTest
336  *
337  * Gets the #IdeTest:id property.
338  *
339  * Returns: (nullable): The id of the test, or %NULL if it has not been set.
340  *
341  * Since: 3.32
342  */
343 const gchar *
ide_test_get_id(IdeTest * self)344 ide_test_get_id (IdeTest *self)
345 {
346   IdeTestPrivate *priv = ide_test_get_instance_private (self);
347 
348   g_return_val_if_fail (IDE_IS_TEST (self), NULL);
349 
350   return priv->id;
351 }
352 
353 /**
354  * ide_test_set_id:
355  * @self: a #IdeTest
356  * @id: (nullable): the id of the test or %NULL
357  *
358  * Sets the #IdeTest:id property.
359  *
360  * The id property is used to uniquely identify the test.
361  *
362  * Since: 3.32
363  */
364 void
ide_test_set_id(IdeTest * self,const gchar * id)365 ide_test_set_id (IdeTest     *self,
366                  const gchar *id)
367 {
368   IdeTestPrivate *priv = ide_test_get_instance_private (self);
369 
370   g_return_if_fail (IDE_IS_TEST (self));
371 
372   if (g_strcmp0 (id, priv->id) != 0)
373     {
374       g_free (priv->id);
375       priv->id = g_strdup (id);
376       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ID]);
377     }
378 }
379 
380 IdeTestStatus
ide_test_get_status(IdeTest * self)381 ide_test_get_status (IdeTest *self)
382 {
383   IdeTestPrivate *priv = ide_test_get_instance_private (self);
384 
385   g_return_val_if_fail (IDE_IS_TEST (self), 0);
386 
387   return priv->status;
388 }
389 
390 void
ide_test_set_status(IdeTest * self,IdeTestStatus status)391 ide_test_set_status (IdeTest       *self,
392                      IdeTestStatus  status)
393 {
394   IdeTestPrivate *priv = ide_test_get_instance_private (self);
395 
396   g_return_if_fail (IDE_IS_TEST (self));
397 
398   if (priv->status != status)
399     {
400       priv->status = status;
401       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_STATUS]);
402     }
403 }
404 
405 const gchar *
ide_test_get_icon_name(IdeTest * self)406 ide_test_get_icon_name (IdeTest *self)
407 {
408   IdeTestPrivate *priv = ide_test_get_instance_private (self);
409 
410   g_return_val_if_fail (IDE_IS_TEST (self), NULL);
411 
412   switch (priv->status)
413     {
414     case IDE_TEST_STATUS_NONE:
415       return "builder-unit-tests-symbolic";
416 
417     case IDE_TEST_STATUS_RUNNING:
418       return "builder-unit-tests-running-symbolic";
419 
420     case IDE_TEST_STATUS_FAILED:
421       return "builder-unit-tests-fail-symbolic";
422 
423     case IDE_TEST_STATUS_SUCCESS:
424       return "builder-unit-tests-pass-symbolic";
425 
426     default:
427       g_return_val_if_reached (NULL);
428     }
429 }
430