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