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

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

Issue 583843003: views::ImageButton: Added SetMinimumImageSize; removed SetPreferredSize. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Delete usage of SetPreferredSize entirely -- these had no effect. Created 6 years, 3 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
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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/base/layout.h" 6 #include "ui/base/layout.h"
7 #include "ui/views/border.h" 7 #include "ui/views/border.h"
8 #include "ui/views/controls/button/image_button.h" 8 #include "ui/views/controls/button/image_button.h"
9 #include "ui/views/test/views_test_base.h" 9 #include "ui/views/test/views_test_base.h"
10 10
(...skipping 10 matching lines...) Expand all
21 namespace views { 21 namespace views {
22 22
23 typedef ViewsTestBase ImageButtonTest; 23 typedef ViewsTestBase ImageButtonTest;
24 24
25 TEST_F(ImageButtonTest, Basics) { 25 TEST_F(ImageButtonTest, Basics) {
26 ImageButton button(NULL); 26 ImageButton button(NULL);
27 27
28 // Our image to paint starts empty. 28 // Our image to paint starts empty.
29 EXPECT_TRUE(button.GetImageToPaint().isNull()); 29 EXPECT_TRUE(button.GetImageToPaint().isNull());
30 30
31 // Without a theme, buttons are 16x14 by default. 31 // Without an image, buttons are 16x14 by default.
32 EXPECT_EQ("16x14", button.GetPreferredSize().ToString()); 32 EXPECT_EQ("16x14", button.GetPreferredSize().ToString());
33 33
34 // We can set a preferred size when we have no image. 34 // The minimum image size should be applied even when there is no image.
35 button.SetPreferredSize(gfx::Size(5, 15)); 35 button.SetMinimumImageSize(gfx::Size(5, 15));
36 EXPECT_EQ("5x15", button.GetPreferredSize().ToString()); 36 EXPECT_EQ("5x15", button.minimum_image_size().ToString());
37 EXPECT_EQ("16x15", button.GetPreferredSize().ToString());
37 38
38 // Set a normal image. 39 // Set a normal image.
39 gfx::ImageSkia normal_image = CreateTestImage(10, 20); 40 gfx::ImageSkia normal_image = CreateTestImage(10, 20);
40 button.SetImage(CustomButton::STATE_NORMAL, &normal_image); 41 button.SetImage(CustomButton::STATE_NORMAL, &normal_image);
41 42
42 // Image uses normal image for painting. 43 // Image uses normal image for painting.
43 EXPECT_FALSE(button.GetImageToPaint().isNull()); 44 EXPECT_FALSE(button.GetImageToPaint().isNull());
44 EXPECT_EQ(10, button.GetImageToPaint().width()); 45 EXPECT_EQ(10, button.GetImageToPaint().width());
45 EXPECT_EQ(20, button.GetImageToPaint().height()); 46 EXPECT_EQ(20, button.GetImageToPaint().height());
46 47
47 // Preferred size is the normal button size. 48 // Preferred size is the normal button size.
48 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); 49 EXPECT_EQ("10x20", button.GetPreferredSize().ToString());
49 50
50 // Set a pushed image. 51 // Set a pushed image.
51 gfx::ImageSkia pushed_image = CreateTestImage(11, 21); 52 gfx::ImageSkia pushed_image = CreateTestImage(11, 21);
52 button.SetImage(CustomButton::STATE_PRESSED, &pushed_image); 53 button.SetImage(CustomButton::STATE_PRESSED, &pushed_image);
53 54
54 // By convention, preferred size doesn't change, even though pushed image 55 // By convention, preferred size doesn't change, even though pushed image
55 // is bigger. 56 // is bigger.
56 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); 57 EXPECT_EQ("10x20", button.GetPreferredSize().ToString());
57 58
58 // We're still painting the normal image. 59 // We're still painting the normal image.
59 EXPECT_FALSE(button.GetImageToPaint().isNull()); 60 EXPECT_FALSE(button.GetImageToPaint().isNull());
60 EXPECT_EQ(10, button.GetImageToPaint().width()); 61 EXPECT_EQ(10, button.GetImageToPaint().width());
61 EXPECT_EQ(20, button.GetImageToPaint().height()); 62 EXPECT_EQ(20, button.GetImageToPaint().height());
63
64 // The minimum image size should make the preferred size bigger.
65 button.SetMinimumImageSize(gfx::Size(15, 5));
66 EXPECT_EQ("15x5", button.minimum_image_size().ToString());
67 EXPECT_EQ("15x20", button.GetPreferredSize().ToString());
68 button.SetMinimumImageSize(gfx::Size(15, 25));
69 EXPECT_EQ("15x25", button.minimum_image_size().ToString());
70 EXPECT_EQ("15x25", button.GetPreferredSize().ToString());
62 } 71 }
63 72
64 TEST_F(ImageButtonTest, SetAndGetImage) { 73 TEST_F(ImageButtonTest, SetAndGetImage) {
65 ImageButton button(NULL); 74 ImageButton button(NULL);
66 75
67 // Images start as null. 76 // Images start as null.
68 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); 77 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
69 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull()); 78 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull());
70 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull()); 79 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
71 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull()); 80 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 EXPECT_EQ(gfx::Point().ToString(), 114 EXPECT_EQ(gfx::Point().ToString(),
106 button.ComputeImagePaintPosition(image).ToString()); 115 button.ComputeImagePaintPosition(image).ToString());
107 116
108 button.SetImageAlignment(ImageButton::ALIGN_CENTER, 117 button.SetImageAlignment(ImageButton::ALIGN_CENTER,
109 ImageButton::ALIGN_MIDDLE); 118 ImageButton::ALIGN_MIDDLE);
110 EXPECT_EQ(gfx::Point(15, 10).ToString(), 119 EXPECT_EQ(gfx::Point(15, 10).ToString(),
111 button.ComputeImagePaintPosition(image).ToString()); 120 button.ComputeImagePaintPosition(image).ToString());
112 button.SetBorder(views::Border::CreateEmptyBorder(10, 10, 0, 0)); 121 button.SetBorder(views::Border::CreateEmptyBorder(10, 10, 0, 0));
113 EXPECT_EQ(gfx::Point(20, 15).ToString(), 122 EXPECT_EQ(gfx::Point(20, 15).ToString(),
114 button.ComputeImagePaintPosition(image).ToString()); 123 button.ComputeImagePaintPosition(image).ToString());
124
125 // The entire button's size should take the border into account.
126 EXPECT_EQ(gfx::Size(30, 40).ToString(), button.GetPreferredSize().ToString());
127
128 // The border should be added on top of the minimum image size.
129 button.SetMinimumImageSize(gfx::Size(30, 5));
130 EXPECT_EQ(gfx::Size(40, 40).ToString(), button.GetPreferredSize().ToString());
115 } 131 }
116 132
117 TEST_F(ImageButtonTest, LeftAlignedMirrored) { 133 TEST_F(ImageButtonTest, LeftAlignedMirrored) {
118 ImageButton button(NULL); 134 ImageButton button(NULL);
119 gfx::ImageSkia image = CreateTestImage(20, 30); 135 gfx::ImageSkia image = CreateTestImage(20, 30);
120 button.SetImage(CustomButton::STATE_NORMAL, &image); 136 button.SetImage(CustomButton::STATE_NORMAL, &image);
121 button.SetBounds(0, 0, 50, 30); 137 button.SetBounds(0, 0, 50, 30);
122 button.SetImageAlignment(ImageButton::ALIGN_LEFT, 138 button.SetImageAlignment(ImageButton::ALIGN_LEFT,
123 ImageButton::ALIGN_BOTTOM); 139 ImageButton::ALIGN_BOTTOM);
124 button.SetDrawImageMirrored(true); 140 button.SetDrawImageMirrored(true);
(...skipping 13 matching lines...) Expand all
138 ImageButton::ALIGN_BOTTOM); 154 ImageButton::ALIGN_BOTTOM);
139 button.SetDrawImageMirrored(true); 155 button.SetDrawImageMirrored(true);
140 156
141 // Because the coordinates are flipped, we should expect this to draw as if 157 // Because the coordinates are flipped, we should expect this to draw as if
142 // it were ALIGN_LEFT. 158 // it were ALIGN_LEFT.
143 EXPECT_EQ(gfx::Point(0, 0).ToString(), 159 EXPECT_EQ(gfx::Point(0, 0).ToString(),
144 button.ComputeImagePaintPosition(image).ToString()); 160 button.ComputeImagePaintPosition(image).ToString());
145 } 161 }
146 162
147 } // namespace views 163 } // namespace views
OLDNEW
« ui/views/controls/button/image_button.cc ('K') | « ui/views/controls/button/image_button.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698