Debian Codegraph @

tests/conform/text-cache.c

raw
1 #include <clutter/clutter.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include "test-conform-common.h"
6
7 #define TEST_FONT "Sans 10"
8
9 static const char long_text[] =
10 "<b>This</b> <i>is</i> some <span size=\"x-large\">REALLY</span> "
11 "long text that contains markup for testing the <tt>use_markup</tt> "
12 "property and to test word-wrapping, justification and alignment.";
13
14 typedef struct _CallbackData CallbackData;
15
16 struct _CallbackData
17 {
18 ClutterActor *stage;
19 ClutterActor *label;
20
21 PangoLayout *old_layout;
22 gboolean layout_changed;
23 PangoRectangle label_extents;
24
25 PangoLayout *test_layout;
26
27 gboolean test_failed;
28 };
29
30 static void
31 on_paint (ClutterActor *stage, CallbackData *data)
32 {
33 PangoLayout *new_layout;
34
35 /* Check whether the layout used for this paint is different from
36 the layout used for the last paint */
37 new_layout = clutter_text_get_layout (CLUTTER_TEXT (data->label));
38 data->layout_changed = data->old_layout != new_layout;
39
40 if (data->old_layout)
41 g_object_unref (data->old_layout);
42 /* Keep a reference to the old layout so we can be sure it won't
43 just reallocate a new layout with the same address */
44 data->old_layout = g_object_ref (new_layout);
45
46 pango_layout_get_extents (new_layout, NULL, &data->label_extents);
47 }
48
49 static void
50 force_redraw (CallbackData *data)
51 {
52 /* XXX - this is fugly; we force a paint on the stage, which
53 * will then paint the Text actor. inside the Text actor we
54 * check for a Layout with the allocation size. if the allocation
55 * has changed it will cause a relayout in the middle of the
56 * paint, which is expensive and broken. this will ensure that
57 * the test passes, though
58 */
59 clutter_actor_paint (clutter_actor_get_stage (data->label));
60 }
61
62 static gboolean
63 check_result (CallbackData *data, const char *note,
64 gboolean layout_should_change)
65 {
66 PangoRectangle test_extents;
67 gboolean fail = FALSE;
68
69 if (g_test_verbose ())
70 g_print ("%s: ", note);
71
72 /* Force a redraw to get the on_paint handler to run */
73 force_redraw (data);
74
75 /* Compare the extents from the label with the extents from our test
76 layout */
77 pango_layout_get_extents (data->test_layout, NULL, &test_extents);
78 if (memcmp (&test_extents, &data->label_extents, sizeof (PangoRectangle)))
79 {
80 if (g_test_verbose ())
81 g_print ("extents are different: expected: %d, %d, %d, %d "
82 "-> text: %d, %d, %d, %d\n",
83 test_extents.x / 1024,
84 test_extents.y / 1024,
85 test_extents.width / 1024,
86 test_extents.height / 1024,
87 data->label_extents.x / 1024,
88 data->label_extents.y / 1024,
89 data->label_extents.width / 1024,
90 data->label_extents.height / 1024);
91
92 fail = TRUE;
93 }
94 else
95 {
96 if (g_test_verbose ())
97 g_print ("extents are the same, ");
98 }
99
100 if (data->layout_changed)
101 {
102 if (g_test_verbose ())
103 g_print ("layout changed, ");
104 }
105 else
106 {
107 if (g_test_verbose ())
108 g_print ("layout did not change, ");
109 }
110
111 if (data->layout_changed != layout_should_change)
112 fail = TRUE;
113
114 if (fail)
115 {
116 if (g_test_verbose ())
117 g_print ("FAIL\n");
118
119 data->test_failed = TRUE;
120 }
121 else
122 {
123 if (g_test_verbose ())
124 g_print ("pass\n");
125 }
126
127 return fail;
128 }
129
130 static gboolean
131 do_tests (CallbackData *data)
132 {
133 PangoFontDescription *fd;
134 static const ClutterColor red = { 0xff, 0x00, 0x00, 0xff };
135 PangoAttrList *attr_list, *attr_list_copy;
136 PangoAttribute *attr;
137
138 /* TEST 1: change the text */
139 clutter_text_set_text (CLUTTER_TEXT (data->label), "Counter 0");
140 pango_layout_set_text (data->test_layout, "Counter 0", -1);
141 g_assert (check_result (data, "Change text", TRUE) == FALSE);
142
143 /* TEST 2: change a single character */
144 clutter_text_set_text (CLUTTER_TEXT (data->label), "Counter 1");
145 pango_layout_set_text (data->test_layout, "Counter 1", -1);
146 g_assert (check_result (data, "Change a single character", TRUE) == FALSE);
147
148 /* TEST 3: move the label */
149 clutter_actor_set_position (data->label, 10, 0);
150 g_assert (check_result (data, "Move the label", FALSE) == FALSE);
151
152 /* TEST 4: change the font */
153 clutter_text_set_font_name (CLUTTER_TEXT (data->label), "Serif 15");
154 fd = pango_font_description_from_string ("Serif 15");
155 pango_layout_set_font_description (data->test_layout, fd);
156 pango_font_description_free (fd);
157 g_assert (check_result (data, "Change the font", TRUE) == FALSE);
158
159 /* TEST 5: change the color */
160 clutter_text_set_color (CLUTTER_TEXT (data->label), &red);
161 g_assert (check_result (data, "Change the color", FALSE) == FALSE);
162
163 /* TEST 6: change the attributes */
164 attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
165 attr->start_index = 0;
166 attr->end_index = 2;
167 attr_list = pango_attr_list_new ();
168 pango_attr_list_insert (attr_list, attr);
169 attr_list_copy = pango_attr_list_copy (attr_list);
170 clutter_text_set_attributes (CLUTTER_TEXT (data->label), attr_list);
171 pango_layout_set_attributes (data->test_layout, attr_list_copy);
172 pango_attr_list_unref (attr_list_copy);
173 pango_attr_list_unref (attr_list);
174 g_assert (check_result (data, "Change the attributes", TRUE) == FALSE);
175
176 /* TEST 7: change the text again */
177 clutter_text_set_attributes (CLUTTER_TEXT (data->label), NULL);
178 clutter_text_set_text (CLUTTER_TEXT (data->label), long_text);
179 pango_layout_set_attributes (data->test_layout, NULL);
180 pango_layout_set_text (data->test_layout, long_text, -1);
181 g_assert (check_result (data, "Change the text again", TRUE) == FALSE);
182
183 /* TEST 8: enable markup */
184 clutter_text_set_use_markup (CLUTTER_TEXT (data->label), TRUE);
185 pango_layout_set_markup (data->test_layout, long_text, -1);
186 g_assert (check_result (data, "Enable markup", TRUE) == FALSE);
187
188 /* This part can't be a test because Clutter won't restrict the
189 width if wrapping and ellipsizing is disabled so the extents will
190 be different, but we still want to do it for the later tests */
191 clutter_actor_set_width (data->label, 200);
192 pango_layout_set_width (data->test_layout, 200 * PANGO_SCALE);
193 /* Force a redraw so that changing the width won't affect the
194 results */
195 force_redraw (data);
196
197 /* TEST 9: enable ellipsize */
198 clutter_text_set_ellipsize (CLUTTER_TEXT (data->label),
199 PANGO_ELLIPSIZE_END);
200 pango_layout_set_ellipsize (data->test_layout, PANGO_ELLIPSIZE_END);
201 g_assert (check_result (data, "Enable ellipsize", TRUE) == FALSE);
202 clutter_text_set_ellipsize (CLUTTER_TEXT (data->label),
203 PANGO_ELLIPSIZE_NONE);
204 pango_layout_set_ellipsize (data->test_layout, PANGO_ELLIPSIZE_NONE);
205 force_redraw (data);
206
207 /* TEST 10: enable line wrap */
208 clutter_text_set_line_wrap (CLUTTER_TEXT (data->label), TRUE);
209 pango_layout_set_wrap (data->test_layout, PANGO_WRAP_WORD);
210 g_assert (check_result (data, "Enable line wrap", TRUE) == FALSE);
211
212 /* TEST 11: change wrap mode
213 * FIXME - broken
214 */
215 clutter_text_set_line_wrap_mode (CLUTTER_TEXT (data->label),
216 PANGO_WRAP_CHAR);
217 pango_layout_set_wrap (data->test_layout, PANGO_WRAP_CHAR);
218 g_assert (check_result (data, "Change wrap mode", TRUE) == FALSE);
219
220 /* TEST 12: enable justify */
221 clutter_text_set_justify (CLUTTER_TEXT (data->label), TRUE);
222 pango_layout_set_justify (data->test_layout, TRUE);
223 /* Pango appears to have a bug which means that you can't change the
224 justification after setting the text but this fixes it.
225 See http://bugzilla.gnome.org/show_bug.cgi?id=551865 */
226 pango_layout_context_changed (data->test_layout);
227 g_assert (check_result (data, "Enable justify", TRUE) == FALSE);
228
229 /* TEST 13: change alignment */
230 clutter_text_set_line_alignment (CLUTTER_TEXT (data->label),
231 PANGO_ALIGN_RIGHT);
232 pango_layout_set_alignment (data->test_layout, PANGO_ALIGN_RIGHT);
233 g_assert (check_result (data, "Change alignment", TRUE) == FALSE);
234
235 clutter_main_quit ();
236
237 return FALSE;
238 }
239
240 static PangoLayout *
241 make_layout_like_label (ClutterText *label)
242 {
243 PangoLayout *label_layout, *new_layout;
244 PangoContext *context;
245 PangoFontDescription *fd;
246
247 /* Make another layout using the same context as the layout from the
248 label */
249 label_layout = clutter_text_get_layout (label);
250 context = pango_layout_get_context (label_layout);
251 new_layout = pango_layout_new (context);
252 fd = pango_font_description_from_string (TEST_FONT);
253 pango_layout_set_font_description (new_layout, fd);
254 pango_font_description_free (fd);
255
256 return new_layout;
257 }
258
259 void
260 text_cache (void)
261 {
262 CallbackData data;
263
264 memset (&data, 0, sizeof (data));
265
266 data.stage = clutter_stage_new ();
267
268 data.label = clutter_text_new_with_text (TEST_FONT, "");
269
270 data.test_layout = make_layout_like_label (CLUTTER_TEXT (data.label));
271
272 g_signal_connect (data.stage, "paint", G_CALLBACK (on_paint), &data);
273
274 clutter_container_add (CLUTTER_CONTAINER (data.stage), data.label, NULL);
275
276 clutter_actor_show (data.stage);
277
278 clutter_threads_add_idle ((GSourceFunc) do_tests, &data);
279
280 clutter_main ();
281
282 clutter_actor_destroy (data.stage);
283
284 if (g_test_verbose ())
285 g_print ("\nOverall result: ");
286
287 if (g_test_verbose ())
288 {
289 if (data.test_failed)
290 g_print ("FAIL\n");
291 else
292 g_print ("pass\n");
293 }
294 else
295 g_assert (data.test_failed != TRUE);
296 }
297