summaryrefslogtreecommitdiff
path: root/tests/testserialize.c
blob: d04ec7cf4f769278cc18315fc16472948c750b5d (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* Pango
 *
 * Copyright (C) 2021 Matthias Clasen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include <glib.h>
#include <pango/pangocairo.h>
#include <gio/gio.h>

static void
test_serialize_attr_list (void)
{
  const char *valid[] = {
    "5 16 style italic",
    "0 10 foreground red, 5 15 weight bold, 0 200 font-desc \"Sans Small-Caps 10\"",
    "0 10 foreground red\n5 15 weight bold\n0 200 font-desc \"Sans Small-Caps 10\"",
    "  0   10   fallback  false,\n 5 15 weight semilight\n\n \n \n",
    "0 100 font-desc \"Cantarell, Sans, Italic Ultra-Light 64\", 10 11 weight 100",
    "0 -1 size 10",
    "0 1 weight 700, 2 4 weight book",
    "0 200 rise 100\n5 15 family Times\n10 11 size 10240\n11 100 fallback 0\n30 60 stretch 2\n",
    ""
  };
  const char *roundtripped[] = {
    "5 16 style italic",
    "0 10 foreground #ffff00000000\n5 15 weight bold\n0 200 font-desc \"Sans Small-Caps 10\"",
    "0 10 foreground #ffff00000000\n5 15 weight bold\n0 200 font-desc \"Sans Small-Caps 10\"",
    "0 10 fallback false\n5 15 weight semilight",
    "0 100 font-desc \"Cantarell,Sans Ultra-Light Italic 64\"\n10 11 weight thin",
    "0 4294967295 size 10",
    "0 1 weight bold\n2 4 weight book",
    "0 200 rise 100\n5 15 family Times\n10 11 size 10240\n11 100 fallback false\n30 60 stretch condensed",
    ""
  };
  const char *invalid[] = {
    "not an attr list",
    "0 -1 FOREGROUND xyz",
    ",,bla.wewq",
  };

  for (int i = 0; i < G_N_ELEMENTS (valid); i++)
    {
      PangoAttrList *attrs;
      char *str;

      attrs = pango_attr_list_from_string (valid[i]);
      g_assert_nonnull (attrs);
      str = pango_attr_list_to_string (attrs);
      g_assert_cmpstr (str, ==, roundtripped[i]);
      g_free (str);
      pango_attr_list_unref (attrs);
    }

  for (int i = 0; i < G_N_ELEMENTS (invalid); i++)
    {
      PangoAttrList *attrs;

      attrs = pango_attr_list_from_string (invalid[i]);
      g_assert_null (attrs);
    }
}

static void
test_serialize_tab_array (void)
{
  const char *valid[] = {
    "0 10 100 200 400",
    "0px 10px 100px 200px 400px",
    "  0    10   ",
    "20 10",
    ""
  };
  const char *roundtripped[] = {
    "0 10 100 200 400",
    "0px 10px 100px 200px 400px",
    "0 10",
    "20 10",
    ""
  };
  const char *invalid[] = {
    "not a tabarray",
    "-10\n-20",
    "10ps 20pu",
    "10, 20",
    "10 20px 30",
  };

  for (int i = 0; i < G_N_ELEMENTS (valid); i++)
    {
      PangoTabArray *tabs;
      char *str;

      tabs = pango_tab_array_from_string (valid[i]);
      g_assert_nonnull (tabs);
      str = pango_tab_array_to_string (tabs);
      g_assert_cmpstr (str, ==, roundtripped[i]);
      g_free (str);
      pango_tab_array_free (tabs);
    }

  for (int i = 0; i < G_N_ELEMENTS (invalid); i++)
    {
      PangoTabArray *tabs;

      tabs = pango_tab_array_from_string (invalid[i]);
      g_assert_null (tabs);
    }
}

static void
test_serialize_layout_minimal (void)
{
  const char *test =
    "{\n"
    "  \"text\" : \"Almost nothing\"\n"
    "}";

  PangoContext *context;
  GBytes *bytes;
  PangoLayout *layout;
  GError *error = NULL;
  GBytes *out_bytes;
  const char *str;

  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());

  bytes = g_bytes_new_static (test, -1);

  layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
  g_assert_no_error (error);
  g_assert_true (PANGO_IS_LAYOUT (layout));
  g_assert_cmpstr (pango_layout_get_text (layout), ==, "Almost nothing");
  g_assert_null (pango_layout_get_attributes (layout));
  g_assert_null (pango_layout_get_tabs (layout));
  g_assert_null (pango_layout_get_font_description (layout));
  g_assert_cmpint (pango_layout_get_alignment (layout), ==, PANGO_ALIGN_LEFT);
  g_assert_cmpint (pango_layout_get_width (layout), ==, -1);

  out_bytes = pango_layout_serialize (layout, PANGO_LAYOUT_SERIALIZE_DEFAULT);
  str = g_bytes_get_data (out_bytes, NULL);

  g_assert_cmpstr (str, ==, test);

  g_bytes_unref (out_bytes);

  g_object_unref (layout);
  g_bytes_unref (bytes);
  g_object_unref (context);
}

static void
test_serialize_layout_valid (void)
{
  const char *test =
    "{\n"
    "  \"text\" : \"Some fun with layouts!\",\n"
    "  \"attributes\" : [\n"
    "    {\n"
    "      \"end\" : 4,\n"
    "      \"type\" : \"foreground\",\n"
    "      \"value\" : \"#ffff00000000\"\n"
    "    },\n"
    "    {\n"
    "      \"start\" : 5,\n"
    "      \"end\" : 8,\n"
    "      \"type\" : \"foreground\",\n"
    "      \"value\" : \"#000080800000\"\n"
    "    },\n"
    "    {\n"
    "      \"start\" : 14,\n"
    "      \"type\" : \"foreground\",\n"
    "      \"value\" : \"#ffff0000ffff\"\n"
    "    }\n"
    "  ],\n"
    "  \"font\" : \"Sans Bold 32\",\n"
    "  \"tabs\" : {\n"
    "    \"positions-in-pixels\" : true,\n"
    "    \"positions\" : [\n"
    "      0,\n"
    "      50,\n"
    "      100\n"
    "    ]\n"
    "  },\n"
    "  \"alignment\" : \"center\",\n"
    "  \"width\" : 350000,\n"
    "  \"line-spacing\" : 1.5\n"
    "}";

  PangoContext *context;
  GBytes *bytes;
  PangoLayout *layout;
  PangoTabArray *tabs;
  GError *error = NULL;
  GBytes *out_bytes;
  const char *str;
  char *s;

  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());

  bytes = g_bytes_new_static (test, -1);

  layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
  g_assert_no_error (error);
  g_assert_true (PANGO_IS_LAYOUT (layout));
  g_assert_cmpstr (pango_layout_get_text (layout), ==, "Some fun with layouts!");
  g_assert_nonnull (pango_layout_get_attributes (layout));
  tabs = pango_layout_get_tabs (layout);
  g_assert_nonnull (tabs);
  pango_tab_array_free (tabs);
  s = pango_font_description_to_string (pango_layout_get_font_description (layout));
  g_assert_cmpstr (s, ==, "Sans Bold 32");
  g_free (s);
  g_assert_cmpint (pango_layout_get_alignment (layout), ==, PANGO_ALIGN_CENTER);
  g_assert_cmpint (pango_layout_get_width (layout), ==, 350000);
  g_assert_cmpfloat_with_epsilon (pango_layout_get_line_spacing (layout), 1.5, 0.0001);

  out_bytes = pango_layout_serialize (layout, PANGO_LAYOUT_SERIALIZE_DEFAULT);
  str = g_bytes_get_data (out_bytes, NULL);

  g_assert_cmpstr (str, ==, test);

  g_bytes_unref (out_bytes);

  g_object_unref (layout);
  g_bytes_unref (bytes);
  g_object_unref (context);
}

static void
test_serialize_layout_invalid (void)
{
  struct {
    const char *json;
    int expected_error;
  } test[] = {
    {
      "{\n"
      "  \"attributes\" : [\n"
      "    {\n"
      "      \"type\" : \"caramba\"\n"
      "    }\n"
      "  ]\n"
      "}",
      PANGO_LAYOUT_DESERIALIZE_INVALID_VALUE
    },
    {
      "{\n"
      "  \"attributes\" : [\n"
      "    {\n"
      "      \"type\" : \"weight\"\n"
      "    }\n"
      "  ]\n"
      "}",
      PANGO_LAYOUT_DESERIALIZE_MISSING_VALUE
    },
    {
      "{\n"
      "  \"attributes\" : [\n"
      "    {\n"
      "      \"type\" : \"alignment\",\n"
      "      \"value\" : \"nonsense\"\n"
      "    }\n"
      "  ]\n"
      "}",
      PANGO_LAYOUT_DESERIALIZE_INVALID_VALUE
    },
    {
      "{\n"
      "  \"alignment\" : \"nonsense\"\n"
      "}",
      PANGO_LAYOUT_DESERIALIZE_INVALID_VALUE
    }
  };

  PangoContext *context;

  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());

  for (int i = 0; i < G_N_ELEMENTS (test); i++)
    {
      GBytes *bytes;
      PangoLayout *layout;
      GError *error = NULL;

       bytes = g_bytes_new_static (test[i].json, -1);
       layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
       g_assert_null (layout);
       g_assert_error (error, PANGO_LAYOUT_DESERIALIZE_ERROR, test[i].expected_error);
       g_bytes_unref (bytes);
       g_clear_error (&error);
    }

  g_object_unref (context);
}
int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/serialize/attr-list", test_serialize_attr_list);
  g_test_add_func ("/serialize/tab-array", test_serialize_tab_array);
  g_test_add_func ("/serialize/layout/minimal", test_serialize_layout_minimal);
  g_test_add_func ("/serialize/layout/valid", test_serialize_layout_valid);
  g_test_add_func ("/serialize/layout/invalid", test_serialize_layout_invalid);

  return g_test_run ();
}