File: label_button_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (228 lines) | stat: -rw-r--r-- 9,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/button/label_button.h"

#include "base/strings/utf_string_conversions.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/text_utils.h"
#include "ui/views/test/views_test_base.h"

using base::ASCIIToUTF16;

namespace {

gfx::ImageSkia CreateTestImage(int width, int height) {
  SkBitmap bitmap;
  bitmap.allocN32Pixels(width, height);
  return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
}

}  // namespace

namespace views {

typedef ViewsTestBase LabelButtonTest;

TEST_F(LabelButtonTest, Init) {
  const base::string16 text(ASCIIToUTF16("abc"));
  LabelButton button(NULL, text);

  EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
  EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull());
  EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
  EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());

  EXPECT_EQ(text, button.GetText());
  EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment());
  EXPECT_FALSE(button.is_default());
  EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON);
  EXPECT_EQ(Button::STATE_NORMAL, button.state());

  EXPECT_EQ(button.image_->parent(), &button);
  EXPECT_EQ(button.label_->parent(), &button);
}

TEST_F(LabelButtonTest, Label) {
  LabelButton button(NULL, base::string16());
  EXPECT_TRUE(button.GetText().empty());

  const gfx::FontList font_list;
  const base::string16 short_text(ASCIIToUTF16("abcdefghijklm"));
  const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz"));
  const int short_text_width = gfx::GetStringWidth(short_text, font_list);
  const int long_text_width = gfx::GetStringWidth(long_text, font_list);

  // The width increases monotonically with string size (it does not shrink).
  EXPECT_LT(button.GetPreferredSize().width(), short_text_width);
  button.SetText(short_text);
  EXPECT_GT(button.GetPreferredSize().height(), font_list.GetHeight());
  EXPECT_GT(button.GetPreferredSize().width(), short_text_width);
  EXPECT_LT(button.GetPreferredSize().width(), long_text_width);
  button.SetText(long_text);
  EXPECT_GT(button.GetPreferredSize().width(), long_text_width);
  button.SetText(short_text);
  EXPECT_GT(button.GetPreferredSize().width(), long_text_width);

  // Clamp the size to a maximum value.
  button.SetMaxSize(gfx::Size(long_text_width, 1));
  EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1));

  // Clear the monotonically increasing minimum size.
  button.SetMinSize(gfx::Size());
  EXPECT_GT(button.GetPreferredSize().width(), short_text_width);
  EXPECT_LT(button.GetPreferredSize().width(), long_text_width);
}

TEST_F(LabelButtonTest, Image) {
  LabelButton button(NULL, base::string16());

  const int small_size = 50, large_size = 100;
  const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size);
  const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size);

  // The width increases monotonically with image size (it does not shrink).
  EXPECT_LT(button.GetPreferredSize().width(), small_size);
  EXPECT_LT(button.GetPreferredSize().height(), small_size);
  button.SetImage(Button::STATE_NORMAL, small_image);
  EXPECT_GT(button.GetPreferredSize().width(), small_size);
  EXPECT_GT(button.GetPreferredSize().height(), small_size);
  EXPECT_LT(button.GetPreferredSize().width(), large_size);
  EXPECT_LT(button.GetPreferredSize().height(), large_size);
  button.SetImage(Button::STATE_NORMAL, large_image);
  EXPECT_GT(button.GetPreferredSize().width(), large_size);
  EXPECT_GT(button.GetPreferredSize().height(), large_size);
  button.SetImage(Button::STATE_NORMAL, small_image);
  EXPECT_GT(button.GetPreferredSize().width(), large_size);
  EXPECT_GT(button.GetPreferredSize().height(), large_size);

  // Clamp the size to a maximum value.
  button.SetMaxSize(gfx::Size(large_size, 1));
  EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1));

  // Clear the monotonically increasing minimum size.
  button.SetMinSize(gfx::Size());
  EXPECT_GT(button.GetPreferredSize().width(), small_size);
  EXPECT_LT(button.GetPreferredSize().width(), large_size);
}

TEST_F(LabelButtonTest, LabelAndImage) {
  LabelButton button(NULL, base::string16());

  const gfx::FontList font_list;
  const base::string16 text(ASCIIToUTF16("abcdefghijklm"));
  const int text_width = gfx::GetStringWidth(text, font_list);

  const int image_size = 50;
  const gfx::ImageSkia image = CreateTestImage(image_size, image_size);
  ASSERT_LT(font_list.GetHeight(), image_size);

  // The width increases monotonically with content size (it does not shrink).
  EXPECT_LT(button.GetPreferredSize().width(), text_width);
  EXPECT_LT(button.GetPreferredSize().width(), image_size);
  EXPECT_LT(button.GetPreferredSize().height(), image_size);
  button.SetText(text);
  EXPECT_GT(button.GetPreferredSize().width(), text_width);
  EXPECT_GT(button.GetPreferredSize().height(), font_list.GetHeight());
  EXPECT_LT(button.GetPreferredSize().width(), text_width + image_size);
  EXPECT_LT(button.GetPreferredSize().height(), image_size);
  button.SetImage(Button::STATE_NORMAL, image);
  EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
  EXPECT_GT(button.GetPreferredSize().height(), image_size);

  // Layout and ensure the image is left of the label except for ALIGN_RIGHT.
  // (A proper parent view or layout manager would Layout on its invalidations).
  button.SetSize(button.GetPreferredSize());
  button.Layout();
  EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment());
  EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x());
  button.SetHorizontalAlignment(gfx::ALIGN_CENTER);
  button.Layout();
  EXPECT_EQ(gfx::ALIGN_CENTER, button.GetHorizontalAlignment());
  EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x());
  button.SetHorizontalAlignment(gfx::ALIGN_RIGHT);
  button.Layout();
  EXPECT_EQ(gfx::ALIGN_RIGHT, button.GetHorizontalAlignment());
  EXPECT_LT(button.label_->bounds().right(), button.image_->bounds().x());

  button.SetText(base::string16());
  EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
  EXPECT_GT(button.GetPreferredSize().height(), image_size);
  button.SetImage(Button::STATE_NORMAL, gfx::ImageSkia());
  EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
  EXPECT_GT(button.GetPreferredSize().height(), image_size);

  // Clamp the size to a maximum value.
  button.SetMaxSize(gfx::Size(image_size, 1));
  EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1));

  // Clear the monotonically increasing minimum size.
  button.SetMinSize(gfx::Size());
  EXPECT_LT(button.GetPreferredSize().width(), text_width);
  EXPECT_LT(button.GetPreferredSize().width(), image_size);
  EXPECT_LT(button.GetPreferredSize().height(), image_size);
}

TEST_F(LabelButtonTest, FontList) {
  const base::string16 text(ASCIIToUTF16("abc"));
  LabelButton button(NULL, text);

  const gfx::FontList original_font_list = button.GetFontList();
  const gfx::FontList large_font_list =
      original_font_list.DeriveWithSizeDelta(100);
  const int original_width = button.GetPreferredSize().width();
  const int original_height = button.GetPreferredSize().height();

  // The button size increases when the font size is increased.
  button.SetFontList(large_font_list);
  EXPECT_GT(button.GetPreferredSize().width(), original_width);
  EXPECT_GT(button.GetPreferredSize().height(), original_height);

  // The button returns to its original size when the minimal size is cleared
  // and the original font size is restored.
  button.SetMinSize(gfx::Size());
  button.SetFontList(original_font_list);
  EXPECT_EQ(original_width, button.GetPreferredSize().width());
  EXPECT_EQ(original_height, button.GetPreferredSize().height());
}

TEST_F(LabelButtonTest, ChangeTextSize) {
  const base::string16 text(ASCIIToUTF16("abc"));
  const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm"));
  LabelButton button(NULL, text);

  const int original_width = button.GetPreferredSize().width();

  // The button size increases when the text size is increased.
  button.SetText(longer_text);
  EXPECT_GT(button.GetPreferredSize().width(), original_width);

  // The button returns to its original size when the original text is restored.
  button.SetMinSize(gfx::Size());
  button.SetText(text);
  EXPECT_EQ(original_width, button.GetPreferredSize().width());
}

TEST_F(LabelButtonTest, ChangeLabelImageSpacing) {
  LabelButton button(NULL, ASCIIToUTF16("abc"));
  button.SetImage(Button::STATE_NORMAL, CreateTestImage(50, 50));

  const int kOriginalSpacing = 5;
  button.SetImageLabelSpacing(kOriginalSpacing);
  const int original_width = button.GetPreferredSize().width();

  // Increasing the spacing between the text and label should increase the size.
  button.SetImageLabelSpacing(2 * kOriginalSpacing);
  EXPECT_GT(button.GetPreferredSize().width(), original_width);

  // The button shrinks if the original spacing is restored.
  button.SetMinSize(gfx::Size());
  button.SetImageLabelSpacing(kOriginalSpacing);
  EXPECT_EQ(original_width, button.GetPreferredSize().width());
}

}  // namespace views