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.
5 #include "ui/base/gtk/gtk_expanded_container.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 #define EXPECT_ALLOCATION_EQ(widget, x_, y_, width_, height_) \
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); \
19 #define EXPECT_ALLOCATION_PARAM_EQ(widget, param, value) \
21 GtkAllocation allocation; \
22 gtk_widget_get_allocation(widget, &allocation); \
23 EXPECT_EQ(value,allocation.param); \
26 class GtkExpandedContainerTest
: public testing::Test
{
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_
);
34 virtual ~GtkExpandedContainerTest() {
35 gtk_widget_destroy(window_
);
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
);
46 g_list_free(children
);
50 int GetChildX(GtkWidget
* widget
) {
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
);
57 int GetChildY(GtkWidget
* widget
) {
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
);
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
));
75 gtk_container_add(GTK_CONTAINER(expanded_
), child2
);
76 ASSERT_TRUE(FindChild(child2
));
77 ASSERT_TRUE(FindChild(child1
));
79 gtk_container_remove(GTK_CONTAINER(expanded_
), child1
);
80 ASSERT_FALSE(FindChild(child1
));
81 ASSERT_TRUE(FindChild(child2
));
83 gtk_container_remove(GTK_CONTAINER(expanded_
), child2
);
84 ASSERT_FALSE(FindChild(child2
));
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_
),
93 gtk_widget_show_all(window_
);
95 GtkAllocation allocation
= { 0, 0, 50, 100 };
96 gtk_widget_size_allocate(expanded_
, &allocation
);
98 EXPECT_ALLOCATION_EQ(child1
, 0, 0, 50, 100);
100 EXPECT_ALLOCATION_EQ(child2
, 10, 20, 50, 100);
104 gtk_widget_size_allocate(expanded_
, &allocation
);
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);
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_
),
120 gtk_widget_show_all(window_
);
122 GtkAllocation allocation
= { 10, 10, 50, 100 };
123 gtk_widget_size_allocate(expanded_
, &allocation
);
125 EXPECT_ALLOCATION_EQ(child
, 0, 0, 50, 100);
128 static void OnChildSizeRequest(GtkExpandedContainer
* container
,
130 GtkRequisition
* requisition
,
132 ASSERT_EQ(child
, GTK_WIDGET(userdata
));
133 requisition
->width
= 250;
134 requisition
->height
= -1;
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_
);
145 GtkAllocation allocation
= { 0, 0, 300, 100 };
146 gtk_widget_size_allocate(expanded_
, &allocation
);
148 EXPECT_ALLOCATION_EQ(child
, 0, 0, 250, 25);
151 TEST_F(GtkExpandedContainerTest
, ChildPosition
) {
152 GtkWidget
* child
= gtk_fixed_new();
153 gtk_expanded_container_put(GTK_EXPANDED_CONTAINER(expanded_
),
155 gtk_widget_show_all(window_
);
157 EXPECT_EQ(10, GetChildX(child
));
158 EXPECT_EQ(20, GetChildY(child
));
160 gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(expanded_
),
162 EXPECT_EQ(40, GetChildX(child
));
163 EXPECT_EQ(50, GetChildY(child
));