[tests] Add preferred size conformance test unit
authorEmmanuele Bassi <ebassi@linux.intel.com>
Tue, 15 Sep 2009 10:09:49 +0000 (11:09 +0100)
committerEmmanuele Bassi <ebassi@linux.intel.com>
Tue, 15 Sep 2009 10:27:50 +0000 (11:27 +0100)
This unit verifies that an Actor class will invoke the get_preferred_*
virtual functions unless the caching is in effect; it also verifies
that the cached values are correctly evicted.

tests/conform/test-actor-size.c
tests/conform/test-conform-main.c

index ce4e530..e0dc838 100644 (file)
@@ -5,9 +5,143 @@
 
 #include "test-conform-common.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;
+};
+
+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)
+{
+}
+
+void
+test_preferred_size (TestConformSimpleFixture *fixture,
+                     gconstpointer             data)
+{
+  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);
+}
+
 void
 test_fixed_size (TestConformSimpleFixture *fixture,
-                 gconstpointer data)
+                 gconstpointer             data)
 {
   ClutterActor *rect;
   gboolean min_width_set, nat_width_set;
index a690bd6..d8d4b1c 100644 (file)
@@ -171,6 +171,7 @@ main (int argc, char **argv)
   TEST_CONFORM_SIMPLE ("/group", test_group_depth_sorting);
 
   TEST_CONFORM_SIMPLE ("/sizing", test_fixed_size);
+  TEST_CONFORM_SIMPLE ("/sizing", test_preferred_size);
 
   return g_test_run ();
 }