Home | History | Annotate | Download | only in gtk
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/base/gtk/gtk_expanded_container.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 #define EXPECT_ALLOCATION_EQ(widget, x_, y_, width_, height_) \
     10 do { \
     11   GtkAllocation allocation; \
     12   gtk_widget_get_allocation(widget, &allocation); \
     13   EXPECT_EQ(x_, allocation.x); \
     14   EXPECT_EQ(y_, allocation.y); \
     15   EXPECT_EQ(width_, allocation.width); \
     16   EXPECT_EQ(height_, allocation.height); \
     17 } while(0);
     18 
     19 #define EXPECT_ALLOCATION_PARAM_EQ(widget, param, value) \
     20 do { \
     21   GtkAllocation allocation; \
     22   gtk_widget_get_allocation(widget, &allocation); \
     23   EXPECT_EQ(value,allocation.param); \
     24 } while(0);
     25 
     26 class GtkExpandedContainerTest : public testing::Test {
     27  protected:
     28   GtkExpandedContainerTest()
     29       : window_(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
     30         expanded_(gtk_expanded_container_new()) {
     31     gtk_window_set_default_size(GTK_WINDOW(window_), 200, 200);
     32     gtk_container_add(GTK_CONTAINER(window_), expanded_);
     33   }
     34   virtual ~GtkExpandedContainerTest() {
     35     gtk_widget_destroy(window_);
     36   }
     37 
     38   bool FindChild(GtkWidget* widget) {
     39     GList* children = gtk_container_get_children(GTK_CONTAINER(expanded_));
     40     for (GList* child = children; child; child = child->next) {
     41       if (GTK_WIDGET(child->data) == widget) {
     42         g_list_free(children);
     43         return true;
     44       }
     45     }
     46     g_list_free(children);
     47     return false;
     48   }
     49 
     50   int GetChildX(GtkWidget* widget) {
     51     GValue x = { 0 };
     52     g_value_init(&x, G_TYPE_INT);
     53     gtk_container_child_get_property(GTK_CONTAINER(expanded_), widget, "x", &x);
     54     return g_value_get_int(&x);
     55   }
     56 
     57   int GetChildY(GtkWidget* widget) {
     58     GValue y = { 0 };
     59     g_value_init(&y, G_TYPE_INT);
     60     gtk_container_child_get_property(GTK_CONTAINER(expanded_), widget, "y", &y);
     61     return g_value_get_int(&y);
     62   }
     63 
     64  protected:
     65   GtkWidget* window_;
     66   GtkWidget* expanded_;
     67 };
     68 
     69 TEST_F(GtkExpandedContainerTest, AddRemove) {
     70   GtkWidget* child1 = gtk_fixed_new();
     71   GtkWidget* child2 = gtk_fixed_new();
     72   gtk_container_add(GTK_CONTAINER(expanded_), child1);
     73   ASSERT_TRUE(FindChild(child1));
     74 
     75   gtk_container_add(GTK_CONTAINER(expanded_), child2);
     76   ASSERT_TRUE(FindChild(child2));
     77   ASSERT_TRUE(FindChild(child1));
     78 
     79   gtk_container_remove(GTK_CONTAINER(expanded_), child1);
     80   ASSERT_FALSE(FindChild(child1));
     81   ASSERT_TRUE(FindChild(child2));
     82 
     83   gtk_container_remove(GTK_CONTAINER(expanded_), child2);
     84   ASSERT_FALSE(FindChild(child2));
     85 }
     86 
     87 TEST_F(GtkExpandedContainerTest, Expand) {
     88   GtkWidget* child1 = gtk_fixed_new();
     89   GtkWidget* child2 = gtk_fixed_new();
     90   gtk_container_add(GTK_CONTAINER(expanded_), child1);
     91   gtk_expanded_container_put(GTK_EXPANDED_CONTAINER(expanded_),
     92                              child2, 10, 20);
     93   gtk_widget_show_all(window_);
     94 
     95   GtkAllocation allocation = { 0, 0, 50, 100 };
     96   gtk_widget_size_allocate(expanded_, &allocation);
     97 
     98   EXPECT_ALLOCATION_EQ(child1, 0, 0, 50, 100);
     99 
    100   EXPECT_ALLOCATION_EQ(child2, 10, 20, 50, 100);
    101 
    102   allocation.x = 10;
    103   allocation.y = 20;
    104   gtk_widget_size_allocate(expanded_, &allocation);
    105 
    106   EXPECT_ALLOCATION_PARAM_EQ(child1, x, 10);
    107   EXPECT_ALLOCATION_PARAM_EQ(child1, y, 20);
    108   EXPECT_ALLOCATION_PARAM_EQ(child2, x, 20);
    109   EXPECT_ALLOCATION_PARAM_EQ(child2, y, 40);
    110 }
    111 
    112 // Test if the size allocation for children still works when using own
    113 // GdkWindow. In this case, the children's origin starts from (0, 0) rather
    114 // than the container's origin.
    115 TEST_F(GtkExpandedContainerTest, HasWindow) {
    116   GtkWidget* child = gtk_fixed_new();
    117   gtk_container_add(GTK_CONTAINER(expanded_), child);
    118   gtk_expanded_container_set_has_window(GTK_EXPANDED_CONTAINER(expanded_),
    119                                         TRUE);
    120   gtk_widget_show_all(window_);
    121 
    122   GtkAllocation allocation = { 10, 10, 50, 100 };
    123   gtk_widget_size_allocate(expanded_, &allocation);
    124 
    125   EXPECT_ALLOCATION_EQ(child, 0, 0, 50, 100);
    126 }
    127 
    128 static void OnChildSizeRequest(GtkExpandedContainer* container,
    129                                GtkWidget* child,
    130                                GtkRequisition* requisition,
    131                                gpointer userdata) {
    132   ASSERT_EQ(child, GTK_WIDGET(userdata));
    133   requisition->width = 250;
    134   requisition->height = -1;
    135 }
    136 
    137 TEST_F(GtkExpandedContainerTest, ChildSizeRequest) {
    138   GtkWidget* child = gtk_fixed_new();
    139   gtk_widget_set_size_request(child, 10, 25);
    140   g_signal_connect(expanded_, "child-size-request",
    141                    G_CALLBACK(OnChildSizeRequest), child);
    142   gtk_container_add(GTK_CONTAINER(expanded_), child);
    143   gtk_widget_show_all(window_);
    144 
    145   GtkAllocation allocation = { 0, 0, 300, 100 };
    146   gtk_widget_size_allocate(expanded_, &allocation);
    147 
    148   EXPECT_ALLOCATION_EQ(child, 0, 0, 250, 25);
    149 }
    150 
    151 TEST_F(GtkExpandedContainerTest, ChildPosition) {
    152   GtkWidget* child = gtk_fixed_new();
    153   gtk_expanded_container_put(GTK_EXPANDED_CONTAINER(expanded_),
    154                              child, 10, 20);
    155   gtk_widget_show_all(window_);
    156 
    157   EXPECT_EQ(10, GetChildX(child));
    158   EXPECT_EQ(20, GetChildY(child));
    159 
    160   gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(expanded_),
    161                               child, 40, 50);
    162   EXPECT_EQ(40, GetChildX(child));
    163   EXPECT_EQ(50, GetChildY(child));
    164 }
    165