| 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 "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "third_party/skia/include/core/SkBitmap.h" | 6 #include "third_party/skia/include/core/SkBitmap.h" |
| 7 #include "ui/gfx/canvas.h" | 7 #include "ui/gfx/canvas.h" |
| 8 #include "ui/gfx/font.h" | 8 #include "ui/gfx/font.h" |
| 9 #include "ui/gfx/size.h" | 9 #include "ui/gfx/size.h" |
| 10 #include "ui/views/controls/button/label_button.h" | 10 #include "ui/views/controls/button/label_button.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 namespace views { | 24 namespace views { |
| 25 | 25 |
| 26 typedef ViewsTestBase LabelButtonTest; | 26 typedef ViewsTestBase LabelButtonTest; |
| 27 | 27 |
| 28 TEST_F(LabelButtonTest, Init) { | 28 TEST_F(LabelButtonTest, Init) { |
| 29 const string16 text(ASCIIToUTF16("abc")); | 29 const string16 text(ASCIIToUTF16("abc")); |
| 30 LabelButton button(NULL, text); | 30 LabelButton button(NULL, text); |
| 31 | 31 |
| 32 EXPECT_TRUE(button.GetImage(CustomButton::STATE_NORMAL).isNull()); | 32 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); |
| 33 EXPECT_TRUE(button.GetImage(CustomButton::STATE_HOVERED).isNull()); | 33 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull()); |
| 34 EXPECT_TRUE(button.GetImage(CustomButton::STATE_PRESSED).isNull()); | 34 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull()); |
| 35 EXPECT_TRUE(button.GetImage(CustomButton::STATE_DISABLED).isNull()); | 35 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull()); |
| 36 | 36 |
| 37 EXPECT_EQ(text, button.GetText()); | 37 EXPECT_EQ(text, button.GetText()); |
| 38 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); | 38 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); |
| 39 EXPECT_FALSE(button.is_default()); | 39 EXPECT_FALSE(button.is_default()); |
| 40 EXPECT_FALSE(button.native_theme()); | 40 EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); |
| 41 EXPECT_EQ(CustomButton::STATE_NORMAL, button.state()); | 41 EXPECT_EQ(Button::STATE_NORMAL, button.state()); |
| 42 | 42 |
| 43 EXPECT_EQ(button.image_->parent(), &button); | 43 EXPECT_EQ(button.image_->parent(), &button); |
| 44 EXPECT_EQ(button.label_->parent(), &button); | 44 EXPECT_EQ(button.label_->parent(), &button); |
| 45 } | 45 } |
| 46 | 46 |
| 47 TEST_F(LabelButtonTest, Label) { | 47 TEST_F(LabelButtonTest, Label) { |
| 48 LabelButton button(NULL, string16()); | 48 LabelButton button(NULL, string16()); |
| 49 EXPECT_TRUE(button.GetText().empty()); | 49 EXPECT_TRUE(button.GetText().empty()); |
| 50 | 50 |
| 51 const gfx::Font font; | 51 const gfx::Font font; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 78 TEST_F(LabelButtonTest, Image) { | 78 TEST_F(LabelButtonTest, Image) { |
| 79 LabelButton button(NULL, string16()); | 79 LabelButton button(NULL, string16()); |
| 80 | 80 |
| 81 const int small_size = 50, large_size = 100; | 81 const int small_size = 50, large_size = 100; |
| 82 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); | 82 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); |
| 83 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); | 83 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); |
| 84 | 84 |
| 85 // The width increases monotonically with image size (it does not shrink). | 85 // The width increases monotonically with image size (it does not shrink). |
| 86 EXPECT_LT(button.GetPreferredSize().width(), small_size); | 86 EXPECT_LT(button.GetPreferredSize().width(), small_size); |
| 87 EXPECT_LT(button.GetPreferredSize().height(), small_size); | 87 EXPECT_LT(button.GetPreferredSize().height(), small_size); |
| 88 button.SetImage(CustomButton::STATE_NORMAL, small_image); | 88 button.SetImage(Button::STATE_NORMAL, small_image); |
| 89 EXPECT_GT(button.GetPreferredSize().width(), small_size); | 89 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| 90 EXPECT_GT(button.GetPreferredSize().height(), small_size); | 90 EXPECT_GT(button.GetPreferredSize().height(), small_size); |
| 91 EXPECT_LT(button.GetPreferredSize().width(), large_size); | 91 EXPECT_LT(button.GetPreferredSize().width(), large_size); |
| 92 EXPECT_LT(button.GetPreferredSize().height(), large_size); | 92 EXPECT_LT(button.GetPreferredSize().height(), large_size); |
| 93 button.SetImage(CustomButton::STATE_NORMAL, large_image); | 93 button.SetImage(Button::STATE_NORMAL, large_image); |
| 94 EXPECT_GT(button.GetPreferredSize().width(), large_size); | 94 EXPECT_GT(button.GetPreferredSize().width(), large_size); |
| 95 EXPECT_GT(button.GetPreferredSize().height(), large_size); | 95 EXPECT_GT(button.GetPreferredSize().height(), large_size); |
| 96 button.SetImage(CustomButton::STATE_NORMAL, small_image); | 96 button.SetImage(Button::STATE_NORMAL, small_image); |
| 97 EXPECT_GT(button.GetPreferredSize().width(), large_size); | 97 EXPECT_GT(button.GetPreferredSize().width(), large_size); |
| 98 EXPECT_GT(button.GetPreferredSize().height(), large_size); | 98 EXPECT_GT(button.GetPreferredSize().height(), large_size); |
| 99 | 99 |
| 100 // Clamp the size to a maximum value. | 100 // Clamp the size to a maximum value. |
| 101 button.set_max_size(gfx::Size(large_size, 1)); | 101 button.set_max_size(gfx::Size(large_size, 1)); |
| 102 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); | 102 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); |
| 103 | 103 |
| 104 // Clear the monotonically increasing minimum size. | 104 // Clear the monotonically increasing minimum size. |
| 105 button.set_min_size(gfx::Size()); | 105 button.set_min_size(gfx::Size()); |
| 106 EXPECT_GT(button.GetPreferredSize().width(), small_size); | 106 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 120 | 120 |
| 121 // The width increases monotonically with content size (it does not shrink). | 121 // The width increases monotonically with content size (it does not shrink). |
| 122 EXPECT_LT(button.GetPreferredSize().width(), text_width); | 122 EXPECT_LT(button.GetPreferredSize().width(), text_width); |
| 123 EXPECT_LT(button.GetPreferredSize().width(), image_size); | 123 EXPECT_LT(button.GetPreferredSize().width(), image_size); |
| 124 EXPECT_LT(button.GetPreferredSize().height(), image_size); | 124 EXPECT_LT(button.GetPreferredSize().height(), image_size); |
| 125 button.SetText(text); | 125 button.SetText(text); |
| 126 EXPECT_GT(button.GetPreferredSize().width(), text_width); | 126 EXPECT_GT(button.GetPreferredSize().width(), text_width); |
| 127 EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight()); | 127 EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight()); |
| 128 EXPECT_LT(button.GetPreferredSize().width(), text_width + image_size); | 128 EXPECT_LT(button.GetPreferredSize().width(), text_width + image_size); |
| 129 EXPECT_LT(button.GetPreferredSize().height(), image_size); | 129 EXPECT_LT(button.GetPreferredSize().height(), image_size); |
| 130 button.SetImage(CustomButton::STATE_NORMAL, image); | 130 button.SetImage(Button::STATE_NORMAL, image); |
| 131 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); | 131 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); |
| 132 EXPECT_GT(button.GetPreferredSize().height(), image_size); | 132 EXPECT_GT(button.GetPreferredSize().height(), image_size); |
| 133 | 133 |
| 134 // Layout and ensure the image is left of the label except for ALIGN_RIGHT. | 134 // Layout and ensure the image is left of the label except for ALIGN_RIGHT. |
| 135 // (A proper parent view or layout manager would Layout on its invalidations). | 135 // (A proper parent view or layout manager would Layout on its invalidations). |
| 136 button.SetSize(button.GetPreferredSize()); | 136 button.SetSize(button.GetPreferredSize()); |
| 137 button.Layout(); | 137 button.Layout(); |
| 138 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); | 138 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); |
| 139 EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x()); | 139 EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x()); |
| 140 button.SetHorizontalAlignment(gfx::ALIGN_CENTER); | 140 button.SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 141 button.Layout(); | 141 button.Layout(); |
| 142 EXPECT_EQ(gfx::ALIGN_CENTER, button.GetHorizontalAlignment()); | 142 EXPECT_EQ(gfx::ALIGN_CENTER, button.GetHorizontalAlignment()); |
| 143 EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x()); | 143 EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x()); |
| 144 button.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 144 button.SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
| 145 button.Layout(); | 145 button.Layout(); |
| 146 EXPECT_EQ(gfx::ALIGN_RIGHT, button.GetHorizontalAlignment()); | 146 EXPECT_EQ(gfx::ALIGN_RIGHT, button.GetHorizontalAlignment()); |
| 147 EXPECT_LT(button.label_->bounds().right(), button.image_->bounds().x()); | 147 EXPECT_LT(button.label_->bounds().right(), button.image_->bounds().x()); |
| 148 | 148 |
| 149 button.SetText(string16()); | 149 button.SetText(string16()); |
| 150 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); | 150 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); |
| 151 EXPECT_GT(button.GetPreferredSize().height(), image_size); | 151 EXPECT_GT(button.GetPreferredSize().height(), image_size); |
| 152 button.SetImage(CustomButton::STATE_NORMAL, gfx::ImageSkia()); | 152 button.SetImage(Button::STATE_NORMAL, gfx::ImageSkia()); |
| 153 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); | 153 EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size); |
| 154 EXPECT_GT(button.GetPreferredSize().height(), image_size); | 154 EXPECT_GT(button.GetPreferredSize().height(), image_size); |
| 155 | 155 |
| 156 // Clamp the size to a maximum value. | 156 // Clamp the size to a maximum value. |
| 157 button.set_max_size(gfx::Size(image_size, 1)); | 157 button.set_max_size(gfx::Size(image_size, 1)); |
| 158 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1)); | 158 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1)); |
| 159 | 159 |
| 160 // Clear the monotonically increasing minimum size. | 160 // Clear the monotonically increasing minimum size. |
| 161 button.set_min_size(gfx::Size()); | 161 button.set_min_size(gfx::Size()); |
| 162 EXPECT_LT(button.GetPreferredSize().width(), text_width); | 162 EXPECT_LT(button.GetPreferredSize().width(), text_width); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 180 | 180 |
| 181 // The button returns to its original size when the minimal size is cleared | 181 // The button returns to its original size when the minimal size is cleared |
| 182 // and the original font size is restored. | 182 // and the original font size is restored. |
| 183 button.set_min_size(gfx::Size()); | 183 button.set_min_size(gfx::Size()); |
| 184 button.SetFont(original_font); | 184 button.SetFont(original_font); |
| 185 EXPECT_EQ(original_width, button.GetPreferredSize().width()); | 185 EXPECT_EQ(original_width, button.GetPreferredSize().width()); |
| 186 EXPECT_EQ(original_height, button.GetPreferredSize().height()); | 186 EXPECT_EQ(original_height, button.GetPreferredSize().height()); |
| 187 } | 187 } |
| 188 | 188 |
| 189 } // namespace views | 189 } // namespace views |
| OLD | NEW |