Index: ui/views/controls/button/label_button_unittest.cc |
diff --git a/ui/views/controls/button/label_button_unittest.cc b/ui/views/controls/button/label_button_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f98b768c68752e75a6b84cf3bc3f23f6cc83dd2c |
--- /dev/null |
+++ b/ui/views/controls/button/label_button_unittest.cc |
@@ -0,0 +1,119 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/utf_string_conversions.h" |
+#include "ui/gfx/canvas.h" |
+#include "ui/views/controls/button/label_button.h" |
+#include "ui/views/test/views_test_base.h" |
+ |
+namespace { |
+ |
+gfx::ImageSkia CreateTestImage(int width, int height) { |
+ SkBitmap bitmap; |
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
+ bitmap.allocPixels(); |
+ return gfx::ImageSkia(bitmap); |
+} |
+ |
+} // namespace |
+ |
+namespace views { |
+ |
+typedef ViewsTestBase LabelButtonTest; |
+ |
+TEST_F(LabelButtonTest, Init) { |
+ string16 text = ASCIIToUTF16("abc"); |
+ LabelButton button(NULL, text); |
+ |
+ EXPECT_TRUE(button.GetImage(CustomButton::BS_NORMAL).isNull()); |
+ EXPECT_TRUE(button.GetImage(CustomButton::BS_HOT).isNull()); |
+ EXPECT_TRUE(button.GetImage(CustomButton::BS_PUSHED).isNull()); |
+ EXPECT_TRUE(button.GetImage(CustomButton::BS_DISABLED).isNull()); |
+ |
+ EXPECT_EQ(text, button.GetText()); |
+ EXPECT_EQ(Label::ALIGN_LEFT, button.GetHorizontalAlignment()); |
+ EXPECT_FALSE(button.default_button()); |
+ EXPECT_FALSE(button.native_theme()); |
+ EXPECT_EQ(CustomButton::BS_NORMAL, button.state()); |
+ |
+ // The button should only have a Label child view to start. |
+ EXPECT_EQ(1, button.child_count()); |
+ EXPECT_EQ(button.child_at(0), button.label_.get()); |
+ |
+ // Setting an image should re-run layout and place it before the Label. |
+ button.SetImage(CustomButton::BS_NORMAL, CreateTestImage(1, 1)); |
+ EXPECT_EQ(2, button.child_count()); |
+ EXPECT_EQ(button.child_at(0), button.image_.get()); |
+ EXPECT_EQ(button.child_at(1), button.label_.get()); |
+ |
+ // Changing to ALIGN_RIGHT should flip the label and image order. |
+ button.SetHorizontalAlignment(Label::ALIGN_RIGHT); |
+ EXPECT_EQ(2, button.child_count()); |
+ EXPECT_EQ(button.child_at(0), button.label_.get()); |
+ EXPECT_EQ(button.child_at(1), button.image_.get()); |
+} |
+ |
+TEST_F(LabelButtonTest, Label) { |
+ LabelButton button(NULL, string16()); |
+ |
+ gfx::Font font; |
+ string16 short_text = ASCIIToUTF16("abcdefghijklm"); |
+ string16 long_text = ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz"); |
+ const int short_text_width = gfx::Canvas::GetStringWidth(short_text, font); |
+ const int long_text_width = gfx::Canvas::GetStringWidth(long_text, font); |
+ EXPECT_GT(button.GetPreferredSize().height(), font.GetHeight()); |
+ |
+ // The width increases monotonically with string size (it does not shrink). |
+ EXPECT_LT(button.GetPreferredSize().width(), short_text_width); |
+ button.SetText(short_text); |
+ EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
+ EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
+ button.SetText(long_text); |
+ EXPECT_GT(button.GetPreferredSize().width(), long_text_width); |
+ button.SetText(short_text); |
+ EXPECT_GT(button.GetPreferredSize().width(), long_text_width); |
+ |
+ // Clamp the size to a maximum value. |
+ button.set_max_size(gfx::Size(long_text_width, 1)); |
+ EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); |
+ |
+ // Clear the monotonically increasing minimum size. |
+ button.set_min_size(gfx::Size()); |
+ EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
+ EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
+} |
+ |
+TEST_F(LabelButtonTest, Image) { |
+ LabelButton button(NULL, string16()); |
+ |
+ const int small_size = 50, large_size = 100; |
+ gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); |
+ gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); |
+ |
+ // The width increases monotonically with image size (it does not shrink). |
+ EXPECT_LT(button.GetPreferredSize().width(), small_size); |
+ EXPECT_LT(button.GetPreferredSize().height(), small_size); |
+ button.SetImage(CustomButton::BS_NORMAL, small_image); |
+ EXPECT_GT(button.GetPreferredSize().width(), small_size); |
+ EXPECT_GT(button.GetPreferredSize().height(), small_size); |
+ EXPECT_LT(button.GetPreferredSize().width(), large_size); |
+ EXPECT_LT(button.GetPreferredSize().height(), large_size); |
+ button.SetImage(CustomButton::BS_NORMAL, large_image); |
+ EXPECT_GT(button.GetPreferredSize().width(), large_size); |
+ EXPECT_GT(button.GetPreferredSize().height(), large_size); |
+ button.SetImage(CustomButton::BS_NORMAL, small_image); |
+ EXPECT_GT(button.GetPreferredSize().width(), large_size); |
+ EXPECT_GT(button.GetPreferredSize().height(), large_size); |
+ |
+ // Clamp the size to a maximum value. |
+ button.set_max_size(gfx::Size(large_size, 1)); |
+ EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); |
+ |
+ // Clear the monotonically increasing minimum size. |
+ button.set_min_size(gfx::Size()); |
+ EXPECT_GT(button.GetPreferredSize().width(), small_size); |
+ EXPECT_LT(button.GetPreferredSize().width(), large_size); |
+} |
+ |
+} // namespace views |