| OLD | NEW |
| 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/views/controls/button/image_button.h" | 6 #include "ui/views/controls/button/image_button.h" |
| 7 #include "ui/views/test/views_test_base.h" | 7 #include "ui/views/test/views_test_base.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 SkBitmap CreateTestBitmap(int width, int height) { | 11 gfx::ImageSkia CreateTestImage(int width, int height) { |
| 12 SkBitmap bitmap; | 12 SkBitmap bitmap; |
| 13 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 13 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 14 bitmap.allocPixels(); | 14 bitmap.allocPixels(); |
| 15 return bitmap; | 15 return gfx::ImageSkia(bitmap); |
| 16 } | 16 } |
| 17 | 17 |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 namespace views { | 20 namespace views { |
| 21 | 21 |
| 22 typedef ViewsTestBase ImageButtonTest; | 22 typedef ViewsTestBase ImageButtonTest; |
| 23 | 23 |
| 24 TEST_F(ImageButtonTest, Basics) { | 24 TEST_F(ImageButtonTest, Basics) { |
| 25 ImageButton button(NULL); | 25 ImageButton button(NULL); |
| 26 | 26 |
| 27 // Our image to paint starts empty. | 27 // Our image to paint starts empty. |
| 28 EXPECT_TRUE(button.GetImageToPaint().empty()); | 28 EXPECT_TRUE(button.GetImageToPaint().empty()); |
| 29 | 29 |
| 30 // Without a theme, buttons are 16x14 by default. | 30 // Without a theme, buttons are 16x14 by default. |
| 31 EXPECT_EQ("16x14", button.GetPreferredSize().ToString()); | 31 EXPECT_EQ("16x14", button.GetPreferredSize().ToString()); |
| 32 | 32 |
| 33 // We can set a preferred size when we have no image. | 33 // We can set a preferred size when we have no image. |
| 34 button.SetPreferredSize(gfx::Size(5, 15)); | 34 button.SetPreferredSize(gfx::Size(5, 15)); |
| 35 EXPECT_EQ("5x15", button.GetPreferredSize().ToString()); | 35 EXPECT_EQ("5x15", button.GetPreferredSize().ToString()); |
| 36 | 36 |
| 37 // Set a normal image. | 37 // Set a normal image. |
| 38 gfx::ImageSkia normal_bitmap = CreateTestBitmap(10, 20); | 38 gfx::ImageSkia normal_image = CreateTestImage(10, 20); |
| 39 button.SetImage(CustomButton::BS_NORMAL, &normal_bitmap); | 39 button.SetImage(CustomButton::BS_NORMAL, &normal_image); |
| 40 | 40 |
| 41 // Image uses normal image for painting. | 41 // Image uses normal image for painting. |
| 42 EXPECT_FALSE(button.GetImageToPaint().empty()); | 42 EXPECT_FALSE(button.GetImageToPaint().empty()); |
| 43 EXPECT_EQ(10, button.GetImageToPaint().width()); | 43 EXPECT_EQ(10, button.GetImageToPaint().width()); |
| 44 EXPECT_EQ(20, button.GetImageToPaint().height()); | 44 EXPECT_EQ(20, button.GetImageToPaint().height()); |
| 45 | 45 |
| 46 // Preferred size is the normal button size. | 46 // Preferred size is the normal button size. |
| 47 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); | 47 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); |
| 48 | 48 |
| 49 // Set a pushed image. | 49 // Set a pushed image. |
| 50 gfx::ImageSkia pushed_bitmap = CreateTestBitmap(11, 21); | 50 gfx::ImageSkia pushed_image = CreateTestImage(11, 21); |
| 51 button.SetImage(CustomButton::BS_PUSHED, &pushed_bitmap); | 51 button.SetImage(CustomButton::BS_PUSHED, &pushed_image); |
| 52 | 52 |
| 53 // By convention, preferred size doesn't change, even though pushed image | 53 // By convention, preferred size doesn't change, even though pushed image |
| 54 // is bigger. | 54 // is bigger. |
| 55 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); | 55 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); |
| 56 | 56 |
| 57 // We're still painting the normal image. | 57 // We're still painting the normal image. |
| 58 EXPECT_FALSE(button.GetImageToPaint().empty()); | 58 EXPECT_FALSE(button.GetImageToPaint().empty()); |
| 59 EXPECT_EQ(10, button.GetImageToPaint().width()); | 59 EXPECT_EQ(10, button.GetImageToPaint().width()); |
| 60 EXPECT_EQ(20, button.GetImageToPaint().height()); | 60 EXPECT_EQ(20, button.GetImageToPaint().height()); |
| 61 | 61 |
| 62 // Set an overlay bitmap. | 62 // Set an overlay image. |
| 63 SkBitmap overlay_bitmap = CreateTestBitmap(12, 22); | 63 gfx::ImageSkia overlay_image = CreateTestImage(12, 22); |
| 64 button.SetOverlayImage(&overlay_bitmap); | 64 button.SetOverlayImage(&overlay_image); |
| 65 EXPECT_EQ(12, button.overlay_image_.width()); | 65 EXPECT_EQ(12, button.overlay_image_.width()); |
| 66 EXPECT_EQ(22, button.overlay_image_.height()); | 66 EXPECT_EQ(22, button.overlay_image_.height()); |
| 67 | 67 |
| 68 // By convention, preferred size doesn't change, even though pushed image | 68 // By convention, preferred size doesn't change, even though pushed image |
| 69 // is bigger. | 69 // is bigger. |
| 70 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); | 70 EXPECT_EQ("10x20", button.GetPreferredSize().ToString()); |
| 71 | 71 |
| 72 // We're still painting the normal image. | 72 // We're still painting the normal image. |
| 73 EXPECT_FALSE(button.GetImageToPaint().empty()); | 73 EXPECT_FALSE(button.GetImageToPaint().empty()); |
| 74 EXPECT_EQ(10, button.GetImageToPaint().width()); | 74 EXPECT_EQ(10, button.GetImageToPaint().width()); |
| 75 EXPECT_EQ(20, button.GetImageToPaint().height()); | 75 EXPECT_EQ(20, button.GetImageToPaint().height()); |
| 76 | 76 |
| 77 // Reset the overlay bitmap. | 77 // Reset the overlay image. |
| 78 button.SetOverlayImage(NULL); | 78 button.SetOverlayImage(NULL); |
| 79 EXPECT_TRUE(button.overlay_image_.empty()); | 79 EXPECT_TRUE(button.overlay_image_.empty()); |
| 80 } | 80 } |
| 81 | 81 |
| 82 } // namespace views | 82 } // namespace views |
| OLD | NEW |