Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: ui/views/controls/button/label_button_unittest.cc

Issue 371633002: LabelButton: cache the last computed preferred size (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/button/label_button.cc ('k') | ui/views/examples/button_example.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/button/label_button.h" 5 #include "ui/views/controls/button/label_button.h"
6 6
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "third_party/skia/include/core/SkBitmap.h" 8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "ui/gfx/canvas.h" 9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/font_list.h" 10 #include "ui/gfx/font_list.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 button.SetText(short_text); 62 button.SetText(short_text);
63 EXPECT_GT(button.GetPreferredSize().height(), font_list.GetHeight()); 63 EXPECT_GT(button.GetPreferredSize().height(), font_list.GetHeight());
64 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); 64 EXPECT_GT(button.GetPreferredSize().width(), short_text_width);
65 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); 65 EXPECT_LT(button.GetPreferredSize().width(), long_text_width);
66 button.SetText(long_text); 66 button.SetText(long_text);
67 EXPECT_GT(button.GetPreferredSize().width(), long_text_width); 67 EXPECT_GT(button.GetPreferredSize().width(), long_text_width);
68 button.SetText(short_text); 68 button.SetText(short_text);
69 EXPECT_GT(button.GetPreferredSize().width(), long_text_width); 69 EXPECT_GT(button.GetPreferredSize().width(), long_text_width);
70 70
71 // Clamp the size to a maximum value. 71 // Clamp the size to a maximum value.
72 button.set_max_size(gfx::Size(long_text_width, 1)); 72 button.SetMaxSize(gfx::Size(long_text_width, 1));
73 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); 73 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1));
74 74
75 // Clear the monotonically increasing minimum size. 75 // Clear the monotonically increasing minimum size.
76 button.set_min_size(gfx::Size()); 76 button.SetMinSize(gfx::Size());
77 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); 77 EXPECT_GT(button.GetPreferredSize().width(), short_text_width);
78 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); 78 EXPECT_LT(button.GetPreferredSize().width(), long_text_width);
79 } 79 }
80 80
81 TEST_F(LabelButtonTest, Image) { 81 TEST_F(LabelButtonTest, Image) {
82 LabelButton button(NULL, base::string16()); 82 LabelButton button(NULL, base::string16());
83 83
84 const int small_size = 50, large_size = 100; 84 const int small_size = 50, large_size = 100;
85 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); 85 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size);
86 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); 86 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size);
87 87
88 // The width increases monotonically with image size (it does not shrink). 88 // The width increases monotonically with image size (it does not shrink).
89 EXPECT_LT(button.GetPreferredSize().width(), small_size); 89 EXPECT_LT(button.GetPreferredSize().width(), small_size);
90 EXPECT_LT(button.GetPreferredSize().height(), small_size); 90 EXPECT_LT(button.GetPreferredSize().height(), small_size);
91 button.SetImage(Button::STATE_NORMAL, small_image); 91 button.SetImage(Button::STATE_NORMAL, small_image);
92 EXPECT_GT(button.GetPreferredSize().width(), small_size); 92 EXPECT_GT(button.GetPreferredSize().width(), small_size);
93 EXPECT_GT(button.GetPreferredSize().height(), small_size); 93 EXPECT_GT(button.GetPreferredSize().height(), small_size);
94 EXPECT_LT(button.GetPreferredSize().width(), large_size); 94 EXPECT_LT(button.GetPreferredSize().width(), large_size);
95 EXPECT_LT(button.GetPreferredSize().height(), large_size); 95 EXPECT_LT(button.GetPreferredSize().height(), large_size);
96 button.SetImage(Button::STATE_NORMAL, large_image); 96 button.SetImage(Button::STATE_NORMAL, large_image);
97 EXPECT_GT(button.GetPreferredSize().width(), large_size); 97 EXPECT_GT(button.GetPreferredSize().width(), large_size);
98 EXPECT_GT(button.GetPreferredSize().height(), large_size); 98 EXPECT_GT(button.GetPreferredSize().height(), large_size);
99 button.SetImage(Button::STATE_NORMAL, small_image); 99 button.SetImage(Button::STATE_NORMAL, small_image);
100 EXPECT_GT(button.GetPreferredSize().width(), large_size); 100 EXPECT_GT(button.GetPreferredSize().width(), large_size);
101 EXPECT_GT(button.GetPreferredSize().height(), large_size); 101 EXPECT_GT(button.GetPreferredSize().height(), large_size);
102 102
103 // Clamp the size to a maximum value. 103 // Clamp the size to a maximum value.
104 button.set_max_size(gfx::Size(large_size, 1)); 104 button.SetMaxSize(gfx::Size(large_size, 1));
105 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); 105 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1));
106 106
107 // Clear the monotonically increasing minimum size. 107 // Clear the monotonically increasing minimum size.
108 button.set_min_size(gfx::Size()); 108 button.SetMinSize(gfx::Size());
109 EXPECT_GT(button.GetPreferredSize().width(), small_size); 109 EXPECT_GT(button.GetPreferredSize().width(), small_size);
110 EXPECT_LT(button.GetPreferredSize().width(), large_size); 110 EXPECT_LT(button.GetPreferredSize().width(), large_size);
111 } 111 }
112 112
113 TEST_F(LabelButtonTest, LabelAndImage) { 113 TEST_F(LabelButtonTest, LabelAndImage) {
114 LabelButton button(NULL, base::string16()); 114 LabelButton button(NULL, base::string16());
115 115
116 const gfx::FontList font_list; 116 const gfx::FontList font_list;
117 const base::string16 text(ASCIIToUTF16("abcdefghijklm")); 117 const base::string16 text(ASCIIToUTF16("abcdefghijklm"));
118 const int text_width = gfx::GetStringWidth(text, font_list); 118 const int text_width = gfx::GetStringWidth(text, font_list);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 EXPECT_LT(button.label_->bounds().right(), button.image_->bounds().x()); 150 EXPECT_LT(button.label_->bounds().right(), button.image_->bounds().x());
151 151
152 button.SetText(base::string16()); 152 button.SetText(base::string16());
153 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); 153 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
154 EXPECT_GT(button.GetPreferredSize().height(), image_size); 154 EXPECT_GT(button.GetPreferredSize().height(), image_size);
155 button.SetImage(Button::STATE_NORMAL, gfx::ImageSkia()); 155 button.SetImage(Button::STATE_NORMAL, gfx::ImageSkia());
156 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); 156 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
157 EXPECT_GT(button.GetPreferredSize().height(), image_size); 157 EXPECT_GT(button.GetPreferredSize().height(), image_size);
158 158
159 // Clamp the size to a maximum value. 159 // Clamp the size to a maximum value.
160 button.set_max_size(gfx::Size(image_size, 1)); 160 button.SetMaxSize(gfx::Size(image_size, 1));
161 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1)); 161 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1));
162 162
163 // Clear the monotonically increasing minimum size. 163 // Clear the monotonically increasing minimum size.
164 button.set_min_size(gfx::Size()); 164 button.SetMinSize(gfx::Size());
165 EXPECT_LT(button.GetPreferredSize().width(), text_width); 165 EXPECT_LT(button.GetPreferredSize().width(), text_width);
166 EXPECT_LT(button.GetPreferredSize().width(), image_size); 166 EXPECT_LT(button.GetPreferredSize().width(), image_size);
167 EXPECT_LT(button.GetPreferredSize().height(), image_size); 167 EXPECT_LT(button.GetPreferredSize().height(), image_size);
168 } 168 }
169 169
170 TEST_F(LabelButtonTest, FontList) { 170 TEST_F(LabelButtonTest, FontList) {
171 const base::string16 text(ASCIIToUTF16("abc")); 171 const base::string16 text(ASCIIToUTF16("abc"));
172 LabelButton button(NULL, text); 172 LabelButton button(NULL, text);
173 173
174 const gfx::FontList original_font_list = button.GetFontList(); 174 const gfx::FontList original_font_list = button.GetFontList();
175 const gfx::FontList large_font_list = 175 const gfx::FontList large_font_list =
176 original_font_list.DeriveWithSizeDelta(100); 176 original_font_list.DeriveWithSizeDelta(100);
177 const int original_width = button.GetPreferredSize().width(); 177 const int original_width = button.GetPreferredSize().width();
178 const int original_height = button.GetPreferredSize().height(); 178 const int original_height = button.GetPreferredSize().height();
179 179
180 // The button size increases when the font size is increased. 180 // The button size increases when the font size is increased.
181 button.SetFontList(large_font_list); 181 button.SetFontList(large_font_list);
182 EXPECT_GT(button.GetPreferredSize().width(), original_width); 182 EXPECT_GT(button.GetPreferredSize().width(), original_width);
183 EXPECT_GT(button.GetPreferredSize().height(), original_height); 183 EXPECT_GT(button.GetPreferredSize().height(), original_height);
184 184
185 // The button returns to its original size when the minimal size is cleared 185 // The button returns to its original size when the minimal size is cleared
186 // and the original font size is restored. 186 // and the original font size is restored.
187 button.set_min_size(gfx::Size()); 187 button.SetMinSize(gfx::Size());
188 button.SetFontList(original_font_list); 188 button.SetFontList(original_font_list);
189 EXPECT_EQ(original_width, button.GetPreferredSize().width()); 189 EXPECT_EQ(original_width, button.GetPreferredSize().width());
190 EXPECT_EQ(original_height, button.GetPreferredSize().height()); 190 EXPECT_EQ(original_height, button.GetPreferredSize().height());
191 } 191 }
192 192
193 TEST_F(LabelButtonTest, ChangeTextSize) {
194 const base::string16 text(ASCIIToUTF16("abc"));
195 const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm"));
196 LabelButton button(NULL, text);
197
198 const int original_width = button.GetPreferredSize().width();
199
200 // The button size increases when the text size is increased.
201 button.SetText(longer_text);
202 EXPECT_GT(button.GetPreferredSize().width(), original_width);
203
204 // The button returns to its original size when the original text is restored.
205 button.SetMinSize(gfx::Size());
206 button.SetText(text);
207 EXPECT_EQ(original_width, button.GetPreferredSize().width());
208 }
209
193 } // namespace views 210 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/label_button.cc ('k') | ui/views/examples/button_example.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698