summaryrefslogtreecommitdiff
path: root/src/tests/clutter/conform/actor-size.c
blob: 7245f157dfcd769b14e6c4ce32c5fa4c9a899660 (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
#include <stdlib.h>
#include <string.h>

#include <clutter/clutter.h>

#define TEST_TYPE_ACTOR         (test_actor_get_type ())

typedef struct _TestActor               TestActor;
typedef struct _ClutterActorClass       TestActorClass;

struct _TestActor
{
  ClutterActor parent_instance;

  guint preferred_width_called  : 1;
  guint preferred_height_called : 1;
};

GType test_actor_get_type (void);

G_DEFINE_TYPE (TestActor, test_actor, CLUTTER_TYPE_ACTOR);

static void
test_actor_get_preferred_width (ClutterActor *self,
                                gfloat        for_height,
                                gfloat       *min_width_p,
                                gfloat       *nat_width_p)
{
  TestActor *test = (TestActor *) self;

  test->preferred_width_called = TRUE;

  if (for_height == 10)
    {
      *min_width_p = 10;
      *nat_width_p = 100;
    }
  else
    {
      *min_width_p = 100;
      *nat_width_p = 100;
    }
}

static void
test_actor_get_preferred_height (ClutterActor *self,
                                 gfloat        for_width,
                                 gfloat       *min_height_p,
                                 gfloat       *nat_height_p)
{
  TestActor *test = (TestActor *) self;

  test->preferred_height_called = TRUE;

  if (for_width == 10)
    {
      *min_height_p = 50;
      *nat_height_p = 100;
    }
  else
    {
      *min_height_p = 100;
      *nat_height_p = 100;
    }
}

static void
test_actor_class_init (TestActorClass *klass)
{
  ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);

  actor_class->get_preferred_width = test_actor_get_preferred_width;
  actor_class->get_preferred_height = test_actor_get_preferred_height;
}

static void
test_actor_init (TestActor *self)
{
}

static void
actor_preferred_size (void)
{
  ClutterActor *test;
  TestActor *self;
  gfloat min_width, min_height;
  gfloat nat_width, nat_height;

  test = g_object_new (TEST_TYPE_ACTOR, NULL);
  self = (TestActor *) test;

  if (g_test_verbose ())
    g_print ("Preferred size\n");

  clutter_actor_get_preferred_size (test,
                                    &min_width, &min_height,
                                    &nat_width, &nat_height);

  g_assert (self->preferred_width_called);
  g_assert (self->preferred_height_called);
  g_assert_cmpfloat (min_width, ==, 100);
  g_assert_cmpfloat (min_height, ==, 100);
  g_assert_cmpfloat (nat_width, ==, min_width);
  g_assert_cmpfloat (nat_height, ==, min_height);

  if (g_test_verbose ())
    g_print ("Preferred width\n");
  self->preferred_width_called = FALSE;
  clutter_actor_get_preferred_width (test, 10, &min_width, &nat_width);
  g_assert (self->preferred_width_called);
  g_assert_cmpfloat (min_width, ==, 10);
  g_assert_cmpfloat (nat_width, ==, 100);

  if (g_test_verbose ())
    g_print ("Preferred height\n");
  self->preferred_height_called = FALSE;
  clutter_actor_get_preferred_height (test, 200, &min_height, &nat_height);
  g_assert (self->preferred_height_called);
  g_assert_cmpfloat (min_height, !=, 10);
  g_assert_cmpfloat (nat_height, ==, 100);

  if (g_test_verbose ())
    g_print ("Preferred width (cached)\n");
  self->preferred_width_called = FALSE;
  clutter_actor_get_preferred_width (test, 10, &min_width, &nat_width);
  g_assert (!self->preferred_width_called);
  g_assert_cmpfloat (min_width, ==, 10);
  g_assert_cmpfloat (nat_width, ==, 100);

  if (g_test_verbose ())
    g_print ("Preferred height (cache eviction)\n");
  self->preferred_height_called = FALSE;
  clutter_actor_get_preferred_height (test, 10, &min_height, &nat_height);
  g_assert (self->preferred_height_called);
  g_assert_cmpfloat (min_height, ==, 50);
  g_assert_cmpfloat (nat_height, ==, 100);

  clutter_actor_destroy (test);
}

static void
actor_fixed_size (void)
{
  ClutterActor *rect;
  gboolean min_width_set, nat_width_set;
  gboolean min_height_set, nat_height_set;
  gfloat min_width, min_height;
  gfloat nat_width, nat_height;

  rect = clutter_actor_new ();
  g_object_ref_sink (rect);

  if (g_test_verbose ())
    g_print ("Initial size is 0\n");

  g_assert_cmpfloat (clutter_actor_get_width (rect), ==, 0);
  g_assert_cmpfloat (clutter_actor_get_height (rect), ==, 0);

  clutter_actor_set_size (rect, 100, 100);

  if (g_test_verbose ())
    g_print ("Explicit size set\n");

  g_assert_cmpfloat (clutter_actor_get_width (rect), ==, 100);
  g_assert_cmpfloat (clutter_actor_get_height (rect), ==, 100);

  g_object_get (G_OBJECT (rect),
                "min-width-set", &min_width_set,
                "min-height-set", &min_height_set,
                "natural-width-set", &nat_width_set,
                "natural-height-set", &nat_height_set,
                NULL);

  if (g_test_verbose ())
    g_print ("Notification properties\n");

  g_assert (min_width_set && nat_width_set);
  g_assert (min_height_set && nat_height_set);

  clutter_actor_get_preferred_size (rect,
                                    &min_width, &min_height,
                                    &nat_width, &nat_height);

  if (g_test_verbose ())
    g_print ("Preferred size\n");

  g_assert_cmpfloat (min_width, ==, 100);
  g_assert_cmpfloat (min_height, ==, 100);
  g_assert_cmpfloat (min_width, ==, nat_width);
  g_assert_cmpfloat (min_height, ==, nat_height);

  clutter_actor_set_size (rect, -1, -1);

  if (g_test_verbose ())
    g_print ("Explicit size unset\n");

  g_object_get (G_OBJECT (rect),
                "min-width-set", &min_width_set,
                "min-height-set", &min_height_set,
                "natural-width-set", &nat_width_set,
                "natural-height-set", &nat_height_set,
                NULL);
  g_assert (!min_width_set && !nat_width_set);
  g_assert (!min_height_set && !nat_height_set);

  g_assert_cmpfloat (clutter_actor_get_width (rect), ==, 0);
  g_assert_cmpfloat (clutter_actor_get_height (rect), ==, 0);

  clutter_actor_destroy (rect);
  g_object_unref (rect);
}

CLUTTER_TEST_SUITE (
  CLUTTER_TEST_UNIT ("/actor/size/preferred", actor_preferred_size)
  CLUTTER_TEST_UNIT ("/actor/size/fixed", actor_fixed_size)
)