OLD | NEW |
(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.GetText()); |
| 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.label_.get()); |
| 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(0), button.image_.get()); |
| 48 EXPECT_EQ(button.child_at(1), button.label_.get()); |
| 49 |
| 50 // Changing to ALIGN_RIGHT should flip the label and image order. |
| 51 button.SetHorizontalAlignment(Label::ALIGN_RIGHT); |
| 52 EXPECT_EQ(2, button.child_count()); |
| 53 EXPECT_EQ(button.child_at(0), button.label_.get()); |
| 54 EXPECT_EQ(button.child_at(1), button.image_.get()); |
| 55 } |
| 56 |
| 57 TEST_F(LabelButtonTest, Label) { |
| 58 LabelButton button(NULL, string16()); |
| 59 |
| 60 gfx::Font font; |
| 61 string16 short_text = ASCIIToUTF16("abcdefghijklm"); |
| 62 string16 long_text = ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz"); |
| 63 const int short_text_width = gfx::Canvas::GetStringWidth(short_text, font); |
| 64 const int long_text_width = gfx::Canvas::GetStringWidth(long_text, font); |
| 65 EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight()); |
| 66 |
| 67 // The width increases monotonically with string size (it does not shrink). |
| 68 EXPECT_LT(button.GetPreferredSize().width(), short_text_width); |
| 69 button.SetText(short_text); |
| 70 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
| 71 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
| 72 button.SetText(long_text); |
| 73 EXPECT_GT(button.GetPreferredSize().width(), long_text_width); |
| 74 button.SetText(short_text); |
| 75 EXPECT_GT(button.GetPreferredSize().width(), long_text_width); |
| 76 |
| 77 // Clamp the size to a maximum value. |
| 78 button.set_max_size(gfx::Size(long_text_width, 1)); |
| 79 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); |
| 80 |
| 81 // Clear the monotonically increasing minimum size. |
| 82 button.set_min_size(gfx::Size()); |
| 83 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
| 84 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
| 85 } |
| 86 |
| 87 TEST_F(LabelButtonTest, Image) { |
| 88 LabelButton button(NULL, string16()); |
| 89 |
| 90 const int small_size = 50, large_size = 100; |
| 91 gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); |
| 92 gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); |
| 93 |
| 94 // The width increases monotonically with image size (it does not shrink). |
| 95 EXPECT_LT(button.GetPreferredSize().width(), small_size); |
| 96 EXPECT_LT(button.GetPreferredSize().height(), small_size); |
| 97 button.SetImage(CustomButton::BS_NORMAL, small_image); |
| 98 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| 99 EXPECT_GT(button.GetPreferredSize().height(), small_size); |
| 100 EXPECT_LT(button.GetPreferredSize().width(), large_size); |
| 101 EXPECT_LT(button.GetPreferredSize().height(), large_size); |
| 102 button.SetImage(CustomButton::BS_NORMAL, large_image); |
| 103 EXPECT_GT(button.GetPreferredSize().width(), large_size); |
| 104 EXPECT_GT(button.GetPreferredSize().height(), large_size); |
| 105 button.SetImage(CustomButton::BS_NORMAL, small_image); |
| 106 EXPECT_GT(button.GetPreferredSize().width(), large_size); |
| 107 EXPECT_GT(button.GetPreferredSize().height(), large_size); |
| 108 |
| 109 // Clamp the size to a maximum value. |
| 110 button.set_max_size(gfx::Size(large_size, 1)); |
| 111 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); |
| 112 |
| 113 // Clear the monotonically increasing minimum size. |
| 114 button.set_min_size(gfx::Size()); |
| 115 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| 116 EXPECT_LT(button.GetPreferredSize().width(), large_size); |
| 117 } |
| 118 |
| 119 } // namespace views |
OLD | NEW |