summaryrefslogtreecommitdiff
path: root/tests/conform/actor-layout.c
blob: cb7f31572fa05f113a7cfc2170361bf6c5546c25 (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
#include <math.h>
#include <clutter/clutter.h>
#include "test-conform-common.h"

typedef struct _TestState       TestState;

struct _TestState
{
  GPtrArray *actors;
  GPtrArray *colors;

  ClutterActor *stage;

  gulong id;

  gint in_validation;

  guint was_painted : 1;
};

static TestState *
test_state_new (void)
{
  return g_slice_new0 (TestState);
}

static void
test_state_free (TestState *state)
{
  if (state->id != 0)
    g_source_remove (state->id);

  if (state->actors != NULL)
    g_ptr_array_unref (state->actors);

  if (state->colors != NULL)
    g_ptr_array_unref (state->colors);

  if (state->stage != NULL)
    clutter_actor_destroy (state->stage);

  g_slice_free (TestState, state);
}

static void
test_state_set_stage (TestState    *state,
                      ClutterActor *stage)
{
  g_assert (!state->was_painted);

  state->stage = stage;
}

static void
test_state_add_actor (TestState          *state,
                      ClutterActor       *actor,
                      const ClutterColor *color)
{
  g_assert (!state->was_painted);

  if (state->actors == NULL)
    {
      state->actors = g_ptr_array_new ();
      g_ptr_array_set_free_func (state->actors,
                                 (GDestroyNotify) g_object_unref);
    }

  g_ptr_array_add (state->actors, g_object_ref (actor));

  if (state->colors == NULL)
    {
      state->colors = g_ptr_array_new ();
      g_ptr_array_set_free_func (state->colors,
                                 (GDestroyNotify) clutter_color_free);
    }

  g_ptr_array_add (state->colors, clutter_color_copy (color));
}

static void
test_state_push_validation (TestState *state)
{
  state->in_validation += 1;
}

static void
test_state_pop_validation (TestState *state)
{
  state->in_validation -= 1;
}

static gboolean
test_state_in_validation (TestState *state)
{
  return state->in_validation > 0;
}

static gboolean
check_color_at (ClutterActor *stage,
                ClutterActor *actor,
                ClutterColor *expected_color,
                float         x,
                float         y)
{
  guchar *buffer;

  if (g_test_verbose ())
    g_print ("Checking actor '%s'\n", clutter_actor_get_name (actor));

  if (g_test_verbose ())
    g_print ("Sampling at { %d, %d }\t", (int) x, (int) y);

  buffer = clutter_stage_read_pixels (CLUTTER_STAGE (stage), x, y, 1, 1);
  g_assert (buffer != NULL);

  if (g_test_verbose ())
    g_print ("Color: { %d, %d, %d } - Expected color { %d, %d, %d }\n",
             buffer[0],
             buffer[1],
             buffer[2],
             expected_color->red,
             expected_color->green,
             expected_color->blue);

  g_assert_cmpint (buffer[0], ==, expected_color->red);
  g_assert_cmpint (buffer[1], ==, expected_color->green);
  g_assert_cmpint (buffer[2], ==, expected_color->blue);

  g_free (buffer);

  return TRUE;
}

static gboolean
validate_state (gpointer data)
{
  TestState *state = data;
  int i;

  /* avoid recursion */
  if (test_state_in_validation (state))
    return G_SOURCE_REMOVE;

  g_assert (state->actors != NULL);
  g_assert (state->colors != NULL);

  g_assert_cmpint (state->actors->len, ==, state->colors->len);

  test_state_push_validation (state);

  if (g_test_verbose ())
    g_print ("Sampling %d actors\n", state->actors->len);

  for (i = 0; i < state->actors->len; i++)
    {
      ClutterActor *actor = g_ptr_array_index (state->actors, i);
      ClutterColor *color = g_ptr_array_index (state->colors, i);
      ClutterActorBox box;

      clutter_actor_get_allocation_box (actor, &box);

      check_color_at (state->stage, actor, color, box.x1 + 2, box.y1 + 2);
      check_color_at (state->stage, actor, color, box.x2 - 2, box.y2 - 2);
    }

  test_state_pop_validation (state);

  state->was_painted = TRUE;

  return G_SOURCE_REMOVE;
}

static gboolean
test_state_run (TestState *state)
{
  clutter_threads_add_repaint_func_full (CLUTTER_REPAINT_FLAGS_POST_PAINT,
                                         validate_state,
                                         state,
                                         NULL);

  while (!state->was_painted)
    g_main_context_iteration (NULL, FALSE);

  return TRUE;
}

void
actor_basic_layout (TestConformSimpleFixture *fixture,
                    gconstpointer data)
{
  ClutterActor *stage = clutter_stage_new ();
  ClutterActor *vase;
  ClutterActor *flower[3];
  TestState *state;

  vase = clutter_actor_new ();
  clutter_actor_set_layout_manager (vase, clutter_flow_layout_new (CLUTTER_FLOW_HORIZONTAL));
  clutter_actor_add_child (stage, vase);

  flower[0] = clutter_actor_new ();
  clutter_actor_set_background_color (flower[0], CLUTTER_COLOR_Red);
  clutter_actor_set_size (flower[0], 100, 100);
  clutter_actor_set_name (flower[0], "Red Flower");
  clutter_actor_add_child (vase, flower[0]);

  flower[1] = clutter_actor_new ();
  clutter_actor_set_background_color (flower[1], CLUTTER_COLOR_Yellow);
  clutter_actor_set_size (flower[1], 100, 100);
  clutter_actor_set_name (flower[1], "Yellow Flower");
  clutter_actor_add_child (vase, flower[1]);

  flower[2] = clutter_actor_new ();
  clutter_actor_set_background_color (flower[2], CLUTTER_COLOR_Green);
  clutter_actor_set_size (flower[2], 100, 100);
  clutter_actor_set_name (flower[2], "Green Flower");
  clutter_actor_add_child (vase, flower[2]);

  clutter_actor_show_all (stage);

  state = test_state_new ();
  test_state_set_stage (state, stage);
  test_state_add_actor (state, flower[0], CLUTTER_COLOR_Red);
  test_state_add_actor (state, flower[1], CLUTTER_COLOR_Yellow);
  test_state_add_actor (state, flower[2], CLUTTER_COLOR_Green);

  g_assert (test_state_run (state));

  test_state_free (state);
}

void
actor_margin_layout (TestConformSimpleFixture *fixture,
                     gconstpointer data)
{
  ClutterActor *stage = clutter_stage_new ();
  ClutterActor *vase;
  ClutterActor *flower[3];
  TestState *state;

  vase = clutter_actor_new ();
  clutter_actor_set_layout_manager (vase, clutter_flow_layout_new (CLUTTER_FLOW_HORIZONTAL));
  clutter_actor_add_child (stage, vase);

  flower[0] = clutter_actor_new ();
  clutter_actor_set_background_color (flower[0], CLUTTER_COLOR_Red);
  clutter_actor_set_size (flower[0], 100, 100);
  clutter_actor_set_name (flower[0], "Red Flower");
  clutter_actor_add_child (vase, flower[0]);

  flower[1] = clutter_actor_new ();
  clutter_actor_set_background_color (flower[1], CLUTTER_COLOR_Yellow);
  clutter_actor_set_size (flower[1], 100, 100);
  clutter_actor_set_name (flower[1], "Yellow Flower");
  clutter_actor_set_margin_right (flower[1], 6);
  clutter_actor_set_margin_left (flower[1], 6);
  clutter_actor_add_child (vase, flower[1]);

  flower[2] = clutter_actor_new ();
  clutter_actor_set_background_color (flower[2], CLUTTER_COLOR_Green);
  clutter_actor_set_size (flower[2], 100, 100);
  clutter_actor_set_name (flower[2], "Green Flower");
  clutter_actor_set_margin_top (flower[2], 6);
  clutter_actor_set_margin_bottom (flower[2], 6);
  clutter_actor_add_child (vase, flower[2]);

  clutter_actor_show_all (stage);

  state = test_state_new ();
  test_state_set_stage (state, stage);
  test_state_add_actor (state, flower[0], CLUTTER_COLOR_Red);
  test_state_add_actor (state, flower[1], CLUTTER_COLOR_Yellow);
  test_state_add_actor (state, flower[2], CLUTTER_COLOR_Green);

  g_assert (test_state_run (state));

  test_state_free (state);
}