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
|
/*
* Copyright (C) 2021 Purism SPC
* 2025 The Phosh Developers
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Author: Guido Günther <agx@sigxcpu.org>
*/
#include "util.h"
#define phosh_test_assert_cmp_markup(in, esc, cmp) \
do { \
g_autofree char *escaped = phosh_util_escape_markup (in, esc); \
g_assert_cmpstr (escaped, ==, cmp); \
} while (0);
static void
test_phosh_util_escape_markup (void)
{
/* correct markup */
phosh_test_assert_cmp_markup ("&&", TRUE, "&&");
phosh_test_assert_cmp_markup ("""", TRUE, """");
phosh_test_assert_cmp_markup ("''", TRUE, "''");
phosh_test_assert_cmp_markup ("<<", TRUE, "<<");
phosh_test_assert_cmp_markup (">>", TRUE, ">>");
phosh_test_assert_cmp_markup ("<b>bold</b>", TRUE, "<b>bold</b>");
phosh_test_assert_cmp_markup ("<i>italic</i>", TRUE, "<i>italic</i>");
phosh_test_assert_cmp_markup ("<u>underline</u>", TRUE, "<u>underline</u>");
phosh_test_assert_cmp_markup ("<u>&</u>", TRUE, "<u>&</u>");
/* unknown tags and entities */
phosh_test_assert_cmp_markup ("&foo;&foo;", TRUE, "&foo;&foo;");
phosh_test_assert_cmp_markup ("<p>para</p>", TRUE, "<p>para</p>");
/* Make sure we match the full tag */
phosh_test_assert_cmp_markup ("<pp>para</pp>", TRUE, "<pp>para</pp>");
phosh_test_assert_cmp_markup ("<", FALSE, "&lt;");
/* broken markups */
/* unbalanced markup */
phosh_test_assert_cmp_markup ("<p>para</i>", TRUE, "<p>para</i>");
phosh_test_assert_cmp_markup ("<p>para</pp>", TRUE, "<p>para</pp>");
}
static void
test_phosh_util_data_uri_to_pixbuf (void)
{
/* smallest valid png. see https://evanhahn.com/worlds-smallest-png/ */
const char *valid_uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAAC"
"klEQVR4AWNgAAAAAgABc3UBGAAAAABJRU5ErkJggg==";
const char *not_a_png = "data:image/png;base64,aGkgdGhlcmUh";
GdkPixbuf* pixbuf = NULL;
g_autoptr (GError) error = NULL;
pixbuf = phosh_util_data_uri_to_pixbuf (valid_uri, &error);
g_assert_no_error (error);
g_assert_nonnull (pixbuf);
g_assert_finalize_object (pixbuf);
pixbuf = phosh_util_data_uri_to_pixbuf (not_a_png, &error);
g_assert_null (pixbuf);
g_assert_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE);
g_clear_error (&error);
pixbuf = phosh_util_data_uri_to_pixbuf ("not-an_uri", &error);
g_assert_null (pixbuf);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
}
static void
test_phosh_util_calculate_supported_mode_scales_integer (void)
{
int num;
g_autofree float *scales = NULL;
/* Phone mode (e.g. PinePhone or Librem 5 */
scales = phosh_util_calculate_supported_mode_scales (720,
1440,
&num,
FALSE);
g_assert_cmpint (num, ==, 2);
g_assert_true (G_APPROX_VALUE (scales[0], 1.0, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[1], 2.0, FLT_EPSILON));
g_clear_pointer (&scales, g_free);
scales = phosh_util_calculate_supported_mode_scales (3840,
2160,
&num, FALSE);
g_assert_cmpint (num, ==, 4);
g_assert_true (G_APPROX_VALUE (scales[0], 1.0, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[1], 2.0, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[2], 3.0, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[3], 4.0, FLT_EPSILON));
}
static void
test_phosh_util_calculate_supported_mode_scales_fractional (void)
{
int num;
g_autofree float *scales = NULL;
/* 4K Mode */
scales = phosh_util_calculate_supported_mode_scales (720,
1440,
&num,
TRUE);
g_assert_cmpint (num, ==, 5);
g_assert_true (G_APPROX_VALUE (scales[0], 1.0, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[1], 1.25, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[2], 1.5, FLT_EPSILON));
g_assert_cmpfloat (scales[3], >=, 1.75);
g_assert_cmpfloat (scales[3], <=, 1.76);
g_assert_true (G_APPROX_VALUE (scales[4], 2.0, FLT_EPSILON));
g_clear_pointer (&scales, g_free);
scales = phosh_util_calculate_supported_mode_scales (3840,
2160,
&num,
TRUE);
g_assert_cmpint (num, ==, 13);
g_assert_true (G_APPROX_VALUE (scales[0], 1.0, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[1], 1.25, FLT_EPSILON));
/* ... */
g_assert_true (G_APPROX_VALUE (scales[11], 3.75, FLT_EPSILON));
g_assert_true (G_APPROX_VALUE (scales[12], 4.0, FLT_EPSILON));
}
int
main (int argc, char *argv[])
{
gtk_test_init (&argc, &argv, NULL);
g_test_add_func ("/phosh/util/escacpe-markup", test_phosh_util_escape_markup);
g_test_add_func ("/phosh/util/data-uri-to-pixbuf", test_phosh_util_data_uri_to_pixbuf);
g_test_add_func ("/phosh/util/scale/integer",
test_phosh_util_calculate_supported_mode_scales_integer);
g_test_add_func ("/phosh/util/scale/fractional",
test_phosh_util_calculate_supported_mode_scales_fractional);
return g_test_run ();
}
|