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

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

Issue 11068012: Add new views::LabelButton and LabelButtonBorder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit test build error; remove stray line. Created 8 years, 2 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/utf_string_conversions.h"
6 #include "ui/gfx/canvas.h"
7 #include "ui/views/controls/button/label_button.h"
8 #include "ui/views/test/views_test_base.h"
9
10 namespace {
11
12 gfx::ImageSkia CreateTestImage(int width, int height) {
13 SkBitmap bitmap;
14 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
15 bitmap.allocPixels();
16 return gfx::ImageSkia(bitmap);
17 }
18
19 } // namespace
20
21 namespace views {
22
23 typedef ViewsTestBase LabelButtonTest;
24
25 TEST_F(LabelButtonTest, Init) {
26 string16 text = ASCIIToUTF16("abc");
27 LabelButton button(NULL, text);
28
29 EXPECT_TRUE(button.GetImage(CustomButton::BS_NORMAL).isNull());
30 EXPECT_TRUE(button.GetImage(CustomButton::BS_HOT).isNull());
31 EXPECT_TRUE(button.GetImage(CustomButton::BS_PUSHED).isNull());
32 EXPECT_TRUE(button.GetImage(CustomButton::BS_DISABLED).isNull());
33
34 EXPECT_EQ(text, button.GetLabel()->text());
35 EXPECT_EQ(Label::ALIGN_LEFT, button.GetHorizontalAlignment());
36 EXPECT_FALSE(button.default_button());
37 EXPECT_FALSE(button.native_theme());
38 EXPECT_EQ(CustomButton::BS_NORMAL, button.state());
39
40 // The button should only have a Label child view to start.
41 EXPECT_EQ(1, button.child_count());
42 EXPECT_EQ(button.child_at(0), button.GetLabel());
43
44 // Setting an image should re-run layout and place it before the Label.
45 button.SetImage(CustomButton::BS_NORMAL, CreateTestImage(1, 1));
46 EXPECT_EQ(2, button.child_count());
47 EXPECT_EQ(button.child_at(1), button.GetLabel());
48
49 // Changing to ALIGN_RIGHT should flip the label and image order.
50 button.SetHorizontalAlignment(Label::ALIGN_RIGHT);
51 EXPECT_EQ(2, button.child_count());
52 EXPECT_EQ(button.child_at(0), button.GetLabel());
53 }
54
55 TEST_F(LabelButtonTest, Label) {
56 LabelButton button(NULL, string16());
57
58 gfx::Font font;
59 string16 short_text = ASCIIToUTF16("abcdefghijklm");
60 string16 long_text = ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz");
61 const int short_text_width = gfx::Canvas::GetStringWidth(short_text, font);
62 const int long_text_width = gfx::Canvas::GetStringWidth(long_text, font);
63 EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight());
64
65 // The width increases monotonically with string size (it does not shrink).
66 EXPECT_LT(button.GetPreferredSize().width(), short_text_width);
67 button.SetText(short_text);
68 EXPECT_GT(button.GetPreferredSize().width(), short_text_width);
69 EXPECT_LT(button.GetPreferredSize().width(), long_text_width);
70 button.SetText(long_text);
71 EXPECT_GT(button.GetPreferredSize().width(), long_text_width);
72 button.SetText(short_text);
73 EXPECT_GT(button.GetPreferredSize().width(), long_text_width);
74
75 // Clamp the size to a maximum value.
76 button.set_max_size(gfx::Size(long_text_width, 1));
77 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1));
78
79 // Clear the monotonically increasing minimum size.
80 button.set_min_size(gfx::Size());
81 EXPECT_GT(button.GetPreferredSize().width(), short_text_width);
82 EXPECT_LT(button.GetPreferredSize().width(), long_text_width);
83 }
84
85 TEST_F(LabelButtonTest, Image) {
86 LabelButton button(NULL, string16());
87
88 const int small_size = 50, large_size = 100;
89 gfx::ImageSkia small_image = CreateTestImage(small_size, small_size);
90 gfx::ImageSkia large_image = CreateTestImage(large_size, large_size);
91
92 // The width increases monotonically with image size (it does not shrink).
93 EXPECT_LT(button.GetPreferredSize().width(), small_size);
94 EXPECT_LT(button.GetPreferredSize().height(), small_size);
95 button.SetImage(CustomButton::BS_NORMAL, small_image);
96 EXPECT_GT(button.GetPreferredSize().width(), small_size);
97 EXPECT_GT(button.GetPreferredSize().height(), small_size);
98 EXPECT_LT(button.GetPreferredSize().width(), large_size);
99 EXPECT_LT(button.GetPreferredSize().height(), large_size);
100 button.SetImage(CustomButton::BS_NORMAL, large_image);
101 EXPECT_GT(button.GetPreferredSize().width(), large_size);
102 EXPECT_GT(button.GetPreferredSize().height(), large_size);
103 button.SetImage(CustomButton::BS_NORMAL, small_image);
104 EXPECT_GT(button.GetPreferredSize().width(), large_size);
105 EXPECT_GT(button.GetPreferredSize().height(), large_size);
106
107 // Clamp the size to a maximum value.
108 button.set_max_size(gfx::Size(large_size, 1));
109 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1));
110
111 // Clear the monotonically increasing minimum size.
112 button.set_min_size(gfx::Size());
113 EXPECT_GT(button.GetPreferredSize().width(), small_size);
114 EXPECT_LT(button.GetPreferredSize().width(), large_size);
115 }
116
117 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698