File: wall-clock.c

package info (click to toggle)
gnome-desktop 44.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,240 kB
  • sloc: ansic: 17,628; sh: 297; xml: 89; makefile: 34
file content (233 lines) | stat: -rw-r--r-- 7,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* -*- mode: C; c-file-style: "linux"; indent-tabs-mode: t -*-
 *
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <gdesktop-enums.h>
#include <glib.h>
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-wall-clock.h>
#include <locale.h>
#include <string.h>

#define SPACE "  "
#define EN_SPACE " "

static void
test_utf8_character (const char *utf8_char,
                     const char *non_utf8_fallback)
{
	GDateTime  *datetime;
	GnomeWallClock *clock;
	const char *save_locale;
	const char *str;

	/* When testing that UTF8 locales don't use double spaces
	   to separate date and time, make sure the date itself
	   doesn't contain double spaces ("Aug  1") */
	datetime = g_date_time_new_local (2014, 5, 28, 23, 59, 59);

	/* In the C locale, make sure the time string is formatted with regular
         * colons */
	save_locale = setlocale (LC_ALL, NULL);
	clock = gnome_wall_clock_new ();
	str = gnome_wall_clock_string_for_datetime (clock,
	                                            datetime,
	                                            G_DESKTOP_CLOCK_FORMAT_24H,
	                                            TRUE, TRUE, TRUE);
	g_assert (strstr (str, non_utf8_fallback) != NULL);
	g_assert (strstr (str, utf8_char) == NULL);
	g_object_unref (clock);

	/* In a UTF8 locale, we want ratio characters and no colons. */
	setlocale (LC_ALL, "en_US.UTF-8");
	clock = gnome_wall_clock_new ();
	str = gnome_wall_clock_string_for_datetime (clock,
	                                            datetime,
	                                            G_DESKTOP_CLOCK_FORMAT_24H,
	                                            TRUE, TRUE, TRUE);
	g_assert (strstr (str, non_utf8_fallback) == NULL);
	g_assert (strstr (str, utf8_char) != NULL);
	g_object_unref (clock);

	/* ... and same thing with an RTL locale: should be formatted with
         * ratio characters */
	setlocale (LC_ALL, "he_IL.UTF-8");
	clock = gnome_wall_clock_new ();
	str = gnome_wall_clock_string_for_datetime (clock,
	                                            datetime,
	                                            G_DESKTOP_CLOCK_FORMAT_24H,
	                                            TRUE, TRUE, TRUE);
	g_assert (strstr (str, non_utf8_fallback) == NULL);
	g_assert (strstr (str, utf8_char) != NULL);
	g_object_unref (clock);

	g_date_time_unref (datetime);

	/* Restore previous locale */
	setlocale (LC_ALL, save_locale);
}

static void
test_space_vs_en_space (void)
{
	test_utf8_character (EN_SPACE, SPACE);
}

static void
test_clock_format_setting (void)
{
	GnomeWallClock *clock;
	GSettings *settings;
	const char *save_locale;
	const char *str;

	save_locale = setlocale (LC_ALL, NULL);
	setlocale (LC_ALL, "en_US.UTF-8");

	settings = g_settings_new ("org.gnome.desktop.interface");

	/* In 12h format, the string ends with AM or PM */
	g_settings_set_enum (settings, "clock-format", G_DESKTOP_CLOCK_FORMAT_12H);
	clock = gnome_wall_clock_new ();
	str = gnome_wall_clock_get_clock (clock);
	g_assert (g_str_has_suffix (str, "AM") || g_str_has_suffix (str, "PM"));
	g_object_unref (clock);

	/* After setting the 24h format, AM / PM should be gone */
	g_settings_set_enum (settings, "clock-format", G_DESKTOP_CLOCK_FORMAT_24H);
	clock = gnome_wall_clock_new ();
	str = gnome_wall_clock_get_clock (clock);
	g_assert (!g_str_has_suffix (str, "AM") && !g_str_has_suffix (str, "PM"));
	g_object_unref (clock);

	g_object_unref (settings);

	/* Restore previous locale */
	setlocale (LC_ALL, save_locale);
}

static gboolean
on_notify_clock_timeout (gpointer user_data)
{
	g_error ("Timeout waiting for notify::clock");
	g_assert_not_reached ();
	return FALSE;
}

static void
on_clock_changed (GnomeWallClock *clock,
                  GParamSpec     *pspec,
                  gpointer        user_data)
{
	GMainLoop *main_loop = user_data;

	/* Nothing much to do here; the test was just to ensure we get the callback */

	g_main_loop_quit (main_loop);
}

static void
test_notify_clock (void)
{
	GMainLoop *main_loop;
	GnomeWallClock *clock;
	GSettings *settings;

	main_loop = g_main_loop_new (NULL, FALSE);
	settings = g_settings_new ("org.gnome.desktop.interface");

	/* Show seconds so we don't have to wait too long for the callback */
	g_settings_set_boolean (settings, "clock-show-seconds", TRUE);

	clock = gnome_wall_clock_new ();
	g_signal_connect (clock, "notify::clock", G_CALLBACK (on_clock_changed), main_loop);
	g_timeout_add_seconds (5, on_notify_clock_timeout, NULL);

	g_main_loop_run (main_loop);

	g_main_loop_unref (main_loop);
	g_object_unref (clock);
	g_object_unref (settings);
}

static void
test_weekday_setting (void)
{
	GnomeWallClock *clock;
	GSettings *settings;
	const char *save_locale;
	const char *str, *ptr, *s;

	/* Save current locale */
	save_locale = setlocale (LC_ALL, NULL);
	setlocale (LC_ALL, "C");
	settings = g_settings_new ("org.gnome.desktop.interface");

	/* Set 24h format, so that the only alphabetical part will be the weekday */
	g_settings_set_enum (settings, "clock-format", G_DESKTOP_CLOCK_FORMAT_24H);

	g_settings_set_boolean (settings, "clock-show-weekday", FALSE);
	g_settings_set_boolean (settings, "clock-show-date", FALSE);
	clock = gnome_wall_clock_new ();
	str = gnome_wall_clock_get_clock (clock);

	/* Verify that no character is alphabetical */
	for (s = str; *s != '\0'; s++)
		g_assert (!g_ascii_isalpha (*s));

	g_object_unref (clock);

	g_settings_set_boolean (settings, "clock-show-weekday", TRUE);
	clock = gnome_wall_clock_new ();
	str = gnome_wall_clock_get_clock (clock);

	/* Verify that every character before the first space is alphabetical */
	ptr = strchr (str, ' ');
	g_assert (ptr != NULL);

	for (s = str; s != ptr; s++)
		g_assert (g_ascii_isalpha (*s));

	for (s = ptr; *s != '\0'; s++)
		g_assert (!g_ascii_isalpha (*s));

	g_object_unref (clock);

	g_object_unref (settings);

	/* Restore previous locale */
	setlocale (LC_ALL, save_locale);
}

int
main (int   argc,
      char *argv[])
{
	g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);

	g_test_init (&argc, &argv, NULL);

	g_test_add_func ("/wall-clock/space-vs-en-space", test_space_vs_en_space);
	g_test_add_func ("/wall-clock/24h-clock-format", test_clock_format_setting);
	g_test_add_func ("/wall-clock/notify-clock", test_notify_clock);
	g_test_add_func ("/wall-clock/weekday-setting", test_weekday_setting);

	return g_test_run ();
}