1 #include <clutter/clutter.h>
2 
3 #include "tests/clutter-test-utils.h"
4 
5 static void
units_cache(void)6 units_cache (void)
7 {
8   ClutterUnits units;
9   ClutterSettings *settings;
10   gfloat pixels;
11   gint old_dpi;
12 
13   settings = clutter_settings_get_default ();
14   g_object_get (settings, "font-dpi", &old_dpi, NULL);
15 
16   g_object_set (settings, "font-dpi", 96 * 1024, NULL);
17   clutter_units_from_em (&units, 1.0);
18   pixels = clutter_units_to_pixels (&units);
19 
20   g_object_set (settings, "font-dpi", ((96 * 2) * 1024), NULL);
21   g_assert_cmpfloat (clutter_units_to_pixels (&units), !=, pixels);
22 
23   g_object_set (settings, "font-dpi", (96 * 1024), NULL);
24   g_assert_cmpfloat (clutter_units_to_pixels (&units), ==, pixels);
25 
26   g_object_set (settings, "font-dpi", old_dpi, NULL);
27 }
28 
29 static void
units_constructors(void)30 units_constructors (void)
31 {
32   ClutterUnits units, units_cm;
33 
34   clutter_units_from_pixels (&units, 100);
35   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_PIXEL);
36   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 100.0);
37   g_assert_cmpfloat (clutter_units_to_pixels (&units), ==, 100.0);
38 
39   clutter_units_from_em (&units, 5.0);
40   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_EM);
41   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 5.0);
42   g_assert_cmpfloat (clutter_units_to_pixels (&units), !=, 5.0);
43 
44   clutter_units_from_cm (&units_cm, 5.0);
45   g_assert (clutter_units_get_unit_type (&units_cm) == CLUTTER_UNIT_CM);
46   g_assert_cmpfloat (clutter_units_get_unit_value (&units_cm), ==, 5.0);
47   g_assert_cmpfloat (clutter_units_to_pixels (&units_cm), !=, 5.0);
48 
49   clutter_units_from_mm (&units, 50.0);
50   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_MM);
51   g_assert_cmpfloat (clutter_units_to_pixels (&units),
52                      ==,
53                      clutter_units_to_pixels (&units_cm));
54 }
55 
56 static void
units_string(void)57 units_string (void)
58 {
59   ClutterUnits units;
60   gchar *string;
61 
62   g_assert (clutter_units_from_string (&units, "") == FALSE);
63 
64   g_assert (clutter_units_from_string (&units, "10") == TRUE);
65   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_PIXEL);
66   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 10);
67 
68   g_assert (clutter_units_from_string (&units, "10 px") == TRUE);
69   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_PIXEL);
70 
71   g_assert (clutter_units_from_string (&units, "10 mm") == TRUE);
72   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_MM);
73 
74   g_assert (clutter_units_from_string (&units, "10 cm") == TRUE);
75   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_CM);
76 
77   g_assert (clutter_units_from_string (&units, "10  ") == TRUE);
78   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_PIXEL);
79   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 10);
80 
81   g_assert (clutter_units_from_string (&units, "5 em") == TRUE);
82   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_EM);
83   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 5);
84 
85   g_assert (clutter_units_from_string (&units, "5 emeralds") == FALSE);
86 
87   g_assert (clutter_units_from_string (&units, "  16   mm") == TRUE);
88   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_MM);
89   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 16);
90 
91   g_assert (clutter_units_from_string (&units, "  24   pt   ") == TRUE);
92   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_POINT);
93   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 24);
94 
95   g_assert (clutter_units_from_string (&units, "  32   em   garbage") == FALSE);
96 
97   g_assert (clutter_units_from_string (&units, "5.1cm") == TRUE);
98   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_CM);
99   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 5.1f);
100 
101   g_assert (clutter_units_from_string (&units, "5,mm") == FALSE);
102 
103   g_assert (clutter_units_from_string (&units, ".5pt") == TRUE);
104   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_POINT);
105   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 0.5f);
106 
107   g_assert (clutter_units_from_string (&units, "1 omg!!pony") == FALSE);
108 
109   clutter_units_from_pt (&units, 24.0);
110   string = clutter_units_to_string (&units);
111   g_assert_cmpstr (string, ==, "24.0 pt");
112   g_free (string);
113 
114   clutter_units_from_em (&units, 3.0);
115   string = clutter_units_to_string (&units);
116   g_assert_cmpstr (string, ==, "3.00 em");
117 
118   units.unit_type = CLUTTER_UNIT_PIXEL;
119   units.value = 0;
120 
121   g_assert (clutter_units_from_string (&units, string) == TRUE);
122   g_assert (clutter_units_get_unit_type (&units) != CLUTTER_UNIT_PIXEL);
123   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_EM);
124   g_assert_cmpint ((int) clutter_units_get_unit_value (&units), ==, 3);
125 
126   g_free (string);
127 }
128 
129 CLUTTER_TEST_SUITE (
130   CLUTTER_TEST_UNIT ("/units/string", units_string)
131   CLUTTER_TEST_UNIT ("/units/cache", units_cache)
132   CLUTTER_TEST_UNIT ("/units/constructors", units_constructors)
133 )
134