1 #include <stdlib.h>
2 #include <string.h>
3
4 #include <clutter/clutter.h>
5
6 #include "tests/clutter-test-utils.h"
7
8 #define TEST_TYPE_ACTOR (test_actor_get_type ())
9
10 typedef struct _TestActor TestActor;
11 typedef struct _ClutterActorClass TestActorClass;
12
13 struct _TestActor
14 {
15 ClutterActor parent_instance;
16
17 guint preferred_width_called : 1;
18 guint preferred_height_called : 1;
19 };
20
21 GType test_actor_get_type (void);
22
23 G_DEFINE_TYPE (TestActor, test_actor, CLUTTER_TYPE_ACTOR);
24
25 static void
test_actor_get_preferred_width(ClutterActor * self,gfloat for_height,gfloat * min_width_p,gfloat * nat_width_p)26 test_actor_get_preferred_width (ClutterActor *self,
27 gfloat for_height,
28 gfloat *min_width_p,
29 gfloat *nat_width_p)
30 {
31 TestActor *test = (TestActor *) self;
32
33 test->preferred_width_called = TRUE;
34
35 if (for_height == 10)
36 {
37 *min_width_p = 10;
38 *nat_width_p = 100;
39 }
40 else
41 {
42 *min_width_p = 100;
43 *nat_width_p = 100;
44 }
45 }
46
47 static void
test_actor_get_preferred_height(ClutterActor * self,gfloat for_width,gfloat * min_height_p,gfloat * nat_height_p)48 test_actor_get_preferred_height (ClutterActor *self,
49 gfloat for_width,
50 gfloat *min_height_p,
51 gfloat *nat_height_p)
52 {
53 TestActor *test = (TestActor *) self;
54
55 test->preferred_height_called = TRUE;
56
57 if (for_width == 10)
58 {
59 *min_height_p = 50;
60 *nat_height_p = 100;
61 }
62 else
63 {
64 *min_height_p = 100;
65 *nat_height_p = 100;
66 }
67 }
68
69 static void
test_actor_class_init(TestActorClass * klass)70 test_actor_class_init (TestActorClass *klass)
71 {
72 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
73
74 actor_class->get_preferred_width = test_actor_get_preferred_width;
75 actor_class->get_preferred_height = test_actor_get_preferred_height;
76 }
77
78 static void
test_actor_init(TestActor * self)79 test_actor_init (TestActor *self)
80 {
81 }
82
83 static void
actor_preferred_size(void)84 actor_preferred_size (void)
85 {
86 ClutterActor *test;
87 TestActor *self;
88 gfloat min_width, min_height;
89 gfloat nat_width, nat_height;
90
91 test = g_object_new (TEST_TYPE_ACTOR, NULL);
92 self = (TestActor *) test;
93
94 if (!g_test_quiet ())
95 g_print ("Preferred size\n");
96
97 clutter_actor_get_preferred_size (test,
98 &min_width, &min_height,
99 &nat_width, &nat_height);
100
101 g_assert (self->preferred_width_called);
102 g_assert (self->preferred_height_called);
103 g_assert_cmpfloat (min_width, ==, 100);
104 g_assert_cmpfloat (min_height, ==, 100);
105 g_assert_cmpfloat (nat_width, ==, min_width);
106 g_assert_cmpfloat (nat_height, ==, min_height);
107
108 if (!g_test_quiet ())
109 g_print ("Preferred width\n");
110 self->preferred_width_called = FALSE;
111 clutter_actor_get_preferred_width (test, 10, &min_width, &nat_width);
112 g_assert (self->preferred_width_called);
113 g_assert_cmpfloat (min_width, ==, 10);
114 g_assert_cmpfloat (nat_width, ==, 100);
115
116 if (!g_test_quiet ())
117 g_print ("Preferred height\n");
118 self->preferred_height_called = FALSE;
119 clutter_actor_get_preferred_height (test, 200, &min_height, &nat_height);
120 g_assert (self->preferred_height_called);
121 g_assert_cmpfloat (min_height, !=, 10);
122 g_assert_cmpfloat (nat_height, ==, 100);
123
124 if (!g_test_quiet ())
125 g_print ("Preferred width (cached)\n");
126 self->preferred_width_called = FALSE;
127 clutter_actor_get_preferred_width (test, 10, &min_width, &nat_width);
128 g_assert (!self->preferred_width_called);
129 g_assert_cmpfloat (min_width, ==, 10);
130 g_assert_cmpfloat (nat_width, ==, 100);
131
132 if (!g_test_quiet ())
133 g_print ("Preferred height (cache eviction)\n");
134 self->preferred_height_called = FALSE;
135 clutter_actor_get_preferred_height (test, 10, &min_height, &nat_height);
136 g_assert (self->preferred_height_called);
137 g_assert_cmpfloat (min_height, ==, 50);
138 g_assert_cmpfloat (nat_height, ==, 100);
139
140 clutter_actor_destroy (test);
141 }
142
143 static void
actor_fixed_size(void)144 actor_fixed_size (void)
145 {
146 ClutterActor *rect;
147 gboolean min_width_set, nat_width_set;
148 gboolean min_height_set, nat_height_set;
149 gfloat min_width, min_height;
150 gfloat nat_width, nat_height;
151
152 rect = clutter_actor_new ();
153 g_object_ref_sink (rect);
154
155 if (!g_test_quiet ())
156 g_print ("Initial size is 0\n");
157
158 g_assert_cmpfloat (clutter_actor_get_width (rect), ==, 0);
159 g_assert_cmpfloat (clutter_actor_get_height (rect), ==, 0);
160
161 clutter_actor_set_size (rect, 100, 100);
162
163 if (!g_test_quiet ())
164 g_print ("Explicit size set\n");
165
166 g_assert_cmpfloat (clutter_actor_get_width (rect), ==, 100);
167 g_assert_cmpfloat (clutter_actor_get_height (rect), ==, 100);
168
169 g_object_get (G_OBJECT (rect),
170 "min-width-set", &min_width_set,
171 "min-height-set", &min_height_set,
172 "natural-width-set", &nat_width_set,
173 "natural-height-set", &nat_height_set,
174 NULL);
175
176 if (!g_test_quiet ())
177 g_print ("Notification properties\n");
178
179 g_assert (min_width_set && nat_width_set);
180 g_assert (min_height_set && nat_height_set);
181
182 clutter_actor_get_preferred_size (rect,
183 &min_width, &min_height,
184 &nat_width, &nat_height);
185
186 if (!g_test_quiet ())
187 g_print ("Preferred size\n");
188
189 g_assert_cmpfloat (min_width, ==, 100);
190 g_assert_cmpfloat (min_height, ==, 100);
191 g_assert_cmpfloat (min_width, ==, nat_width);
192 g_assert_cmpfloat (min_height, ==, nat_height);
193
194 clutter_actor_set_size (rect, -1, -1);
195
196 if (!g_test_quiet ())
197 g_print ("Explicit size unset\n");
198
199 g_object_get (G_OBJECT (rect),
200 "min-width-set", &min_width_set,
201 "min-height-set", &min_height_set,
202 "natural-width-set", &nat_width_set,
203 "natural-height-set", &nat_height_set,
204 NULL);
205 g_assert (!min_width_set && !nat_width_set);
206 g_assert (!min_height_set && !nat_height_set);
207
208 g_assert_cmpfloat (clutter_actor_get_width (rect), ==, 0);
209 g_assert_cmpfloat (clutter_actor_get_height (rect), ==, 0);
210
211 clutter_actor_destroy (rect);
212 g_object_unref (rect);
213 }
214
215 CLUTTER_TEST_SUITE (
216 CLUTTER_TEST_UNIT ("/actor/size/preferred", actor_preferred_size)
217 CLUTTER_TEST_UNIT ("/actor/size/fixed", actor_fixed_size)
218 )
219