Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / controls / button / image_button_unittest.cc
blobea35af6d671155887ec2bc2b275fe5b8eab0b076
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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/base/layout.h"
7 #include "ui/views/border.h"
8 #include "ui/views/controls/button/image_button.h"
9 #include "ui/views/test/views_test_base.h"
11 namespace {
13 gfx::ImageSkia CreateTestImage(int width, int height) {
14 SkBitmap bitmap;
15 bitmap.allocN32Pixels(width, height);
16 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
19 } // namespace
21 namespace views {
23 typedef ViewsTestBase ImageButtonTest;
25 TEST_F(ImageButtonTest, Basics) {
26 ImageButton button(NULL);
28 // Our image to paint starts empty.
29 EXPECT_TRUE(button.GetImageToPaint().isNull());
31 // Without a theme, buttons are 16x14 by default.
32 EXPECT_EQ("16x14", button.GetPreferredSize().ToString());
34 // We can set a preferred size when we have no image.
35 button.SetPreferredSize(gfx::Size(5, 15));
36 EXPECT_EQ("5x15", button.GetPreferredSize().ToString());
38 // Set a normal image.
39 gfx::ImageSkia normal_image = CreateTestImage(10, 20);
40 button.SetImage(CustomButton::STATE_NORMAL, &normal_image);
42 // Image uses normal image for painting.
43 EXPECT_FALSE(button.GetImageToPaint().isNull());
44 EXPECT_EQ(10, button.GetImageToPaint().width());
45 EXPECT_EQ(20, button.GetImageToPaint().height());
47 // Preferred size is the normal button size.
48 EXPECT_EQ("10x20", button.GetPreferredSize().ToString());
50 // Set a pushed image.
51 gfx::ImageSkia pushed_image = CreateTestImage(11, 21);
52 button.SetImage(CustomButton::STATE_PRESSED, &pushed_image);
54 // By convention, preferred size doesn't change, even though pushed image
55 // is bigger.
56 EXPECT_EQ("10x20", button.GetPreferredSize().ToString());
58 // We're still painting the normal image.
59 EXPECT_FALSE(button.GetImageToPaint().isNull());
60 EXPECT_EQ(10, button.GetImageToPaint().width());
61 EXPECT_EQ(20, button.GetImageToPaint().height());
64 TEST_F(ImageButtonTest, SetAndGetImage) {
65 ImageButton button(NULL);
67 // Images start as null.
68 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
69 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull());
70 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
71 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
73 // Setting images works as expected.
74 gfx::ImageSkia image1 = CreateTestImage(10, 11);
75 gfx::ImageSkia image2 = CreateTestImage(20, 21);
76 button.SetImage(Button::STATE_NORMAL, &image1);
77 button.SetImage(Button::STATE_HOVERED, &image2);
78 EXPECT_TRUE(
79 button.GetImage(Button::STATE_NORMAL).BackedBySameObjectAs(image1));
80 EXPECT_TRUE(
81 button.GetImage(Button::STATE_HOVERED).BackedBySameObjectAs(image2));
82 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull());
83 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
85 // ImageButton supports NULL image pointers.
86 button.SetImage(Button::STATE_NORMAL, NULL);
87 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
90 TEST_F(ImageButtonTest, ImagePositionWithBorder) {
91 ImageButton button(NULL);
92 gfx::ImageSkia image = CreateTestImage(20, 30);
93 button.SetImage(CustomButton::STATE_NORMAL, &image);
95 // The image should be painted at the top-left corner.
96 EXPECT_EQ(gfx::Point().ToString(),
97 button.ComputeImagePaintPosition(image).ToString());
99 button.SetBorder(views::Border::CreateEmptyBorder(10, 5, 0, 0));
100 EXPECT_EQ(gfx::Point(5, 10).ToString(),
101 button.ComputeImagePaintPosition(image).ToString());
103 button.SetBorder(Border::NullBorder());
104 button.SetBounds(0, 0, 50, 50);
105 EXPECT_EQ(gfx::Point().ToString(),
106 button.ComputeImagePaintPosition(image).ToString());
108 button.SetImageAlignment(ImageButton::ALIGN_CENTER,
109 ImageButton::ALIGN_MIDDLE);
110 EXPECT_EQ(gfx::Point(15, 10).ToString(),
111 button.ComputeImagePaintPosition(image).ToString());
112 button.SetBorder(views::Border::CreateEmptyBorder(10, 10, 0, 0));
113 EXPECT_EQ(gfx::Point(20, 15).ToString(),
114 button.ComputeImagePaintPosition(image).ToString());
117 TEST_F(ImageButtonTest, LeftAlignedMirrored) {
118 ImageButton button(NULL);
119 gfx::ImageSkia image = CreateTestImage(20, 30);
120 button.SetImage(CustomButton::STATE_NORMAL, &image);
121 button.SetBounds(0, 0, 50, 30);
122 button.SetImageAlignment(ImageButton::ALIGN_LEFT,
123 ImageButton::ALIGN_BOTTOM);
124 button.SetDrawImageMirrored(true);
126 // Because the coordinates are flipped, we should expect this to draw as if
127 // it were ALIGN_RIGHT.
128 EXPECT_EQ(gfx::Point(30, 0).ToString(),
129 button.ComputeImagePaintPosition(image).ToString());
132 TEST_F(ImageButtonTest, RightAlignedMirrored) {
133 ImageButton button(NULL);
134 gfx::ImageSkia image = CreateTestImage(20, 30);
135 button.SetImage(CustomButton::STATE_NORMAL, &image);
136 button.SetBounds(0, 0, 50, 30);
137 button.SetImageAlignment(ImageButton::ALIGN_RIGHT,
138 ImageButton::ALIGN_BOTTOM);
139 button.SetDrawImageMirrored(true);
141 // Because the coordinates are flipped, we should expect this to draw as if
142 // it were ALIGN_LEFT.
143 EXPECT_EQ(gfx::Point(0, 0).ToString(),
144 button.ComputeImagePaintPosition(image).ToString());
147 } // namespace views