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 "third_party/skia/include/core/SkBitmap.h" |
| 7 #include "ui/gfx/canvas.h" |
| 8 #include "ui/views/controls/button/label_button.h" |
| 9 #include "ui/views/test/views_test_base.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 gfx::ImageSkia CreateTestImage(int width, int height) { |
| 14 SkBitmap bitmap; |
| 15 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 16 bitmap.allocPixels(); |
| 17 return gfx::ImageSkia(bitmap); |
| 18 } |
| 19 |
| 20 } // namespace |
| 21 |
| 22 namespace views { |
| 23 |
| 24 typedef ViewsTestBase LabelButtonTest; |
| 25 |
| 26 TEST_F(LabelButtonTest, Init) { |
| 27 const string16 text(ASCIIToUTF16("abc")); |
| 28 LabelButton button(NULL, text); |
| 29 |
| 30 EXPECT_TRUE(button.GetImage(CustomButton::BS_NORMAL).isNull()); |
| 31 EXPECT_TRUE(button.GetImage(CustomButton::BS_HOT).isNull()); |
| 32 EXPECT_TRUE(button.GetImage(CustomButton::BS_PUSHED).isNull()); |
| 33 EXPECT_TRUE(button.GetImage(CustomButton::BS_DISABLED).isNull()); |
| 34 |
| 35 EXPECT_EQ(text, button.GetText()); |
| 36 EXPECT_EQ(Label::ALIGN_LEFT, button.GetHorizontalAlignment()); |
| 37 EXPECT_FALSE(button.default_button()); |
| 38 EXPECT_FALSE(button.native_theme()); |
| 39 EXPECT_EQ(CustomButton::BS_NORMAL, button.state()); |
| 40 |
| 41 EXPECT_EQ(button.image_->parent(), &button); |
| 42 EXPECT_EQ(button.label_->parent(), &button); |
| 43 } |
| 44 |
| 45 TEST_F(LabelButtonTest, Label) { |
| 46 LabelButton button(NULL, string16()); |
| 47 EXPECT_TRUE(button.GetText().empty()); |
| 48 |
| 49 const gfx::Font font; |
| 50 const string16 short_text(ASCIIToUTF16("abcdefghijklm")); |
| 51 const string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz")); |
| 52 const int short_text_width = gfx::Canvas::GetStringWidth(short_text, font); |
| 53 const int long_text_width = gfx::Canvas::GetStringWidth(long_text, font); |
| 54 |
| 55 // The width increases monotonically with string size (it does not shrink). |
| 56 EXPECT_LT(button.GetPreferredSize().width(), short_text_width); |
| 57 button.SetText(short_text); |
| 58 EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight()); |
| 59 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
| 60 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
| 61 button.SetText(long_text); |
| 62 EXPECT_GT(button.GetPreferredSize().width(), long_text_width); |
| 63 button.SetText(short_text); |
| 64 EXPECT_GT(button.GetPreferredSize().width(), long_text_width); |
| 65 |
| 66 // Clamp the size to a maximum value. |
| 67 button.set_max_size(gfx::Size(long_text_width, 1)); |
| 68 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); |
| 69 |
| 70 // Clear the monotonically increasing minimum size. |
| 71 button.set_min_size(gfx::Size()); |
| 72 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
| 73 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
| 74 } |
| 75 |
| 76 TEST_F(LabelButtonTest, Image) { |
| 77 LabelButton button(NULL, string16()); |
| 78 |
| 79 const int small_size = 50, large_size = 100; |
| 80 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); |
| 81 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); |
| 82 |
| 83 // The width increases monotonically with image size (it does not shrink). |
| 84 EXPECT_LT(button.GetPreferredSize().width(), small_size); |
| 85 EXPECT_LT(button.GetPreferredSize().height(), small_size); |
| 86 button.SetImage(CustomButton::BS_NORMAL, small_image); |
| 87 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| 88 EXPECT_GT(button.GetPreferredSize().height(), small_size); |
| 89 EXPECT_LT(button.GetPreferredSize().width(), large_size); |
| 90 EXPECT_LT(button.GetPreferredSize().height(), large_size); |
| 91 button.SetImage(CustomButton::BS_NORMAL, large_image); |
| 92 EXPECT_GT(button.GetPreferredSize().width(), large_size); |
| 93 EXPECT_GT(button.GetPreferredSize().height(), large_size); |
| 94 button.SetImage(CustomButton::BS_NORMAL, small_image); |
| 95 EXPECT_GT(button.GetPreferredSize().width(), large_size); |
| 96 EXPECT_GT(button.GetPreferredSize().height(), large_size); |
| 97 |
| 98 // Clamp the size to a maximum value. |
| 99 button.set_max_size(gfx::Size(large_size, 1)); |
| 100 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); |
| 101 |
| 102 // Clear the monotonically increasing minimum size. |
| 103 button.set_min_size(gfx::Size()); |
| 104 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| 105 EXPECT_LT(button.GetPreferredSize().width(), large_size); |
| 106 } |
| 107 |
| 108 TEST_F(LabelButtonTest, LabelAndImage) { |
| 109 LabelButton button(NULL, string16()); |
| 110 |
| 111 const gfx::Font font; |
| 112 const string16 text(ASCIIToUTF16("abcdefghijklm")); |
| 113 const int text_width = gfx::Canvas::GetStringWidth(text, font); |
| 114 |
| 115 const int image_size = 50; |
| 116 const gfx::ImageSkia image = CreateTestImage(image_size, image_size); |
| 117 ASSERT_LT(font.GetHeight(), image_size); |
| 118 |
| 119 // The width increases monotonically with content size (it does not shrink). |
| 120 EXPECT_LT(button.GetPreferredSize().width(), text_width); |
| 121 EXPECT_LT(button.GetPreferredSize().width(), image_size); |
| 122 EXPECT_LT(button.GetPreferredSize().height(), image_size); |
| 123 button.SetText(text); |
| 124 EXPECT_GT(button.GetPreferredSize().width(), text_width); |
| 125 EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight()); |
| 126 EXPECT_LT(button.GetPreferredSize().width(), text_width + image_size); |
| 127 EXPECT_LT(button.GetPreferredSize().height(), image_size); |
| 128 button.SetImage(CustomButton::BS_NORMAL, image); |
| 129 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); |
| 130 EXPECT_GT(button.GetPreferredSize().height(), image_size); |
| 131 |
| 132 // Layout and ensure the image is left of the label except for ALIGN_RIGHT. |
| 133 // (A proper parent view or layout manager would Layout on its invalidations). |
| 134 button.SetSize(button.GetPreferredSize()); |
| 135 button.Layout(); |
| 136 EXPECT_EQ(Label::ALIGN_LEFT, button.GetHorizontalAlignment()); |
| 137 EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x()); |
| 138 button.SetHorizontalAlignment(Label::ALIGN_CENTER); |
| 139 button.Layout(); |
| 140 EXPECT_EQ(Label::ALIGN_CENTER, button.GetHorizontalAlignment()); |
| 141 EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x()); |
| 142 button.SetHorizontalAlignment(Label::ALIGN_RIGHT); |
| 143 button.Layout(); |
| 144 EXPECT_EQ(Label::ALIGN_RIGHT, button.GetHorizontalAlignment()); |
| 145 EXPECT_LT(button.label_->bounds().right(), button.image_->bounds().x()); |
| 146 |
| 147 button.SetText(string16()); |
| 148 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); |
| 149 EXPECT_GT(button.GetPreferredSize().height(), image_size); |
| 150 button.SetImage(CustomButton::BS_NORMAL, gfx::ImageSkia()); |
| 151 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); |
| 152 EXPECT_GT(button.GetPreferredSize().height(), image_size); |
| 153 |
| 154 // Clamp the size to a maximum value. |
| 155 button.set_max_size(gfx::Size(image_size, 1)); |
| 156 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1)); |
| 157 |
| 158 // Clear the monotonically increasing minimum size. |
| 159 button.set_min_size(gfx::Size()); |
| 160 EXPECT_LT(button.GetPreferredSize().width(), text_width); |
| 161 EXPECT_LT(button.GetPreferredSize().width(), image_size); |
| 162 EXPECT_LT(button.GetPreferredSize().height(), image_size); |
| 163 } |
| 164 |
| 165 } // namespace views |
OLD | NEW |