Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Unified Diff: ui/views/controls/button/label_button_unittest.cc

Issue 1228213003: Just set borders once when creating a views::LabelButton (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
index 05a81a77fa16feceaf5c0a8ff195af91733673c7..30f78a46324b9c78a6ac2e28816082acf2e3fb9a 100644
--- a/ui/views/controls/button/label_button_unittest.cc
+++ b/ui/views/controls/button/label_button_unittest.cc
@@ -6,11 +6,12 @@
#include "base/strings/utf_string_conversions.h"
#include "third_party/skia/include/core/SkBitmap.h"
+#include "ui/accessibility/ax_view_state.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/text_utils.h"
-#include "ui/views/test/views_test_base.h"
+#include "ui/views/test/widget_test.h"
using base::ASCIIToUTF16;
@@ -26,7 +27,34 @@ gfx::ImageSkia CreateTestImage(int width, int height) {
namespace views {
-typedef ViewsTestBase LabelButtonTest;
+class LabelButtonTest : public test::WidgetTest {
+ public:
+ LabelButtonTest() {}
+
+ // testing::Test:
+ void SetUp() override {
+ WidgetTest::SetUp();
+ // Make a Widget to host the button. This ensures appropriate borders are
+ // used (which could be derived from the Widget's NativeTheme).
+ test_widget_ = CreateTopLevelPlatformWidget();
+
+ button_ = new LabelButton(nullptr, base::string16());
+ test_widget_->GetContentsView()->AddChildView(button_);
+ }
+
+ void TearDown() override {
+ test_widget_->CloseNow();
+ WidgetTest::TearDown();
+ }
+
+ protected:
+ LabelButton* button_ = nullptr;
+
+ private:
+ Widget* test_widget_ = nullptr;
+
+ DISALLOW_COPY_AND_ASSIGN(LabelButtonTest);
+};
TEST_F(LabelButtonTest, Init) {
const base::string16 text(ASCIIToUTF16("abc"));
@@ -38,6 +66,12 @@ TEST_F(LabelButtonTest, Init) {
EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull());
EXPECT_EQ(text, button.GetText());
+
+ ui::AXViewState accessible_state;
+ button.GetAccessibleState(&accessible_state);
+ EXPECT_EQ(ui::AX_ROLE_BUTTON, accessible_state.role);
+ EXPECT_EQ(text, accessible_state.name);
+
EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment());
EXPECT_FALSE(button.is_default());
EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON);
@@ -48,8 +82,7 @@ TEST_F(LabelButtonTest, Init) {
}
TEST_F(LabelButtonTest, Label) {
- LabelButton button(NULL, base::string16());
- EXPECT_TRUE(button.GetText().empty());
+ EXPECT_TRUE(button_->GetText().empty());
const gfx::FontList font_list;
const base::string16 short_text(ASCIIToUTF16("abcdefghijklm"));
@@ -58,61 +91,85 @@ TEST_F(LabelButtonTest, Label) {
const int long_text_width = gfx::GetStringWidth(long_text, font_list);
// 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().height(), font_list.GetHeight());
- 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);
+ EXPECT_LT(button_->GetPreferredSize().width(), short_text_width);
+ button_->SetText(short_text);
+ EXPECT_GT(button_->GetPreferredSize().height(), font_list.GetHeight());
+ 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.SetMaxSize(gfx::Size(long_text_width, 1));
- EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1));
+ button_->SetMaxSize(gfx::Size(long_text_width, 1));
+ EXPECT_EQ(button_->GetPreferredSize(), gfx::Size(long_text_width, 1));
// Clear the monotonically increasing minimum size.
- button.SetMinSize(gfx::Size());
- EXPECT_GT(button.GetPreferredSize().width(), short_text_width);
- EXPECT_LT(button.GetPreferredSize().width(), long_text_width);
+ button_->SetMinSize(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, base::string16());
+// Test behavior of View::GetAccessibleState() for buttons when setting a label.
+TEST_F(LabelButtonTest, AccessibleState) {
+ ui::AXViewState accessible_state;
+
+ button_->GetAccessibleState(&accessible_state);
+ EXPECT_EQ(ui::AX_ROLE_BUTTON, accessible_state.role);
+ EXPECT_EQ(base::string16(), accessible_state.name);
+
+ // Without a label (e.g. image-only), the accessible name should automatically
+ // be set from the tooltip.
+ const base::string16 tooltip_text = ASCIIToUTF16("abc");
+ button_->SetTooltipText(tooltip_text);
+ button_->GetAccessibleState(&accessible_state);
+ EXPECT_EQ(tooltip_text, accessible_state.name);
+ EXPECT_EQ(base::string16(), button_->GetText());
+
+ // Setting a label overrides the tooltip text.
+ const base::string16 label_text = ASCIIToUTF16("def");
+ button_->SetText(label_text);
+ button_->GetAccessibleState(&accessible_state);
+ EXPECT_EQ(label_text, accessible_state.name);
+ EXPECT_EQ(label_text, button_->GetText());
+
+ base::string16 tooltip;
+ EXPECT_TRUE(button_->GetTooltipText(gfx::Point(), &tooltip));
+ EXPECT_EQ(tooltip_text, tooltip);
+}
+TEST_F(LabelButtonTest, Image) {
const int small_size = 50, large_size = 100;
const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size);
const 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(Button::STATE_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(Button::STATE_NORMAL, large_image);
- EXPECT_GT(button.GetPreferredSize().width(), large_size);
- EXPECT_GT(button.GetPreferredSize().height(), large_size);
- button.SetImage(Button::STATE_NORMAL, small_image);
- EXPECT_GT(button.GetPreferredSize().width(), large_size);
- EXPECT_GT(button.GetPreferredSize().height(), large_size);
+ EXPECT_LT(button_->GetPreferredSize().width(), small_size);
+ EXPECT_LT(button_->GetPreferredSize().height(), small_size);
+ button_->SetImage(Button::STATE_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(Button::STATE_NORMAL, large_image);
+ EXPECT_GT(button_->GetPreferredSize().width(), large_size);
+ EXPECT_GT(button_->GetPreferredSize().height(), large_size);
+ button_->SetImage(Button::STATE_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.SetMaxSize(gfx::Size(large_size, 1));
- EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1));
+ button_->SetMaxSize(gfx::Size(large_size, 1));
+ EXPECT_EQ(button_->GetPreferredSize(), gfx::Size(large_size, 1));
// Clear the monotonically increasing minimum size.
- button.SetMinSize(gfx::Size());
- EXPECT_GT(button.GetPreferredSize().width(), small_size);
- EXPECT_LT(button.GetPreferredSize().width(), large_size);
+ button_->SetMinSize(gfx::Size());
+ EXPECT_GT(button_->GetPreferredSize().width(), small_size);
+ EXPECT_LT(button_->GetPreferredSize().width(), large_size);
}
TEST_F(LabelButtonTest, LabelAndImage) {
- LabelButton button(NULL, base::string16());
-
const gfx::FontList font_list;
const base::string16 text(ASCIIToUTF16("abcdefghijklm"));
const int text_width = gfx::GetStringWidth(text, font_list);
@@ -122,107 +179,106 @@ TEST_F(LabelButtonTest, LabelAndImage) {
ASSERT_LT(font_list.GetHeight(), image_size);
// The width increases monotonically with content size (it does not shrink).
- EXPECT_LT(button.GetPreferredSize().width(), text_width);
- EXPECT_LT(button.GetPreferredSize().width(), image_size);
- EXPECT_LT(button.GetPreferredSize().height(), image_size);
- button.SetText(text);
- EXPECT_GT(button.GetPreferredSize().width(), text_width);
- EXPECT_GT(button.GetPreferredSize().height(), font_list.GetHeight());
- EXPECT_LT(button.GetPreferredSize().width(), text_width + image_size);
- EXPECT_LT(button.GetPreferredSize().height(), image_size);
- button.SetImage(Button::STATE_NORMAL, image);
- EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
- EXPECT_GT(button.GetPreferredSize().height(), image_size);
+ EXPECT_LT(button_->GetPreferredSize().width(), text_width);
+ EXPECT_LT(button_->GetPreferredSize().width(), image_size);
+ EXPECT_LT(button_->GetPreferredSize().height(), image_size);
+ button_->SetText(text);
+ EXPECT_GT(button_->GetPreferredSize().width(), text_width);
+ EXPECT_GT(button_->GetPreferredSize().height(), font_list.GetHeight());
+ EXPECT_LT(button_->GetPreferredSize().width(), text_width + image_size);
+ EXPECT_LT(button_->GetPreferredSize().height(), image_size);
+ button_->SetImage(Button::STATE_NORMAL, image);
+ EXPECT_GT(button_->GetPreferredSize().width(), text_width + image_size);
+ EXPECT_GT(button_->GetPreferredSize().height(), image_size);
// Layout and ensure the image is left of the label except for ALIGN_RIGHT.
// (A proper parent view or layout manager would Layout on its invalidations).
- button.SetSize(button.GetPreferredSize());
- button.Layout();
- EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment());
- EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x());
- button.SetHorizontalAlignment(gfx::ALIGN_CENTER);
- button.Layout();
- EXPECT_EQ(gfx::ALIGN_CENTER, button.GetHorizontalAlignment());
- EXPECT_LT(button.image_->bounds().right(), button.label_->bounds().x());
- button.SetHorizontalAlignment(gfx::ALIGN_RIGHT);
- button.Layout();
- EXPECT_EQ(gfx::ALIGN_RIGHT, button.GetHorizontalAlignment());
- EXPECT_LT(button.label_->bounds().right(), button.image_->bounds().x());
-
- button.SetText(base::string16());
- EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
- EXPECT_GT(button.GetPreferredSize().height(), image_size);
- button.SetImage(Button::STATE_NORMAL, gfx::ImageSkia());
- EXPECT_GT(button.GetPreferredSize().width(), text_width + image_size);
- EXPECT_GT(button.GetPreferredSize().height(), image_size);
+ button_->SetSize(button_->GetPreferredSize());
+ button_->Layout();
+ EXPECT_EQ(gfx::ALIGN_LEFT, button_->GetHorizontalAlignment());
+ EXPECT_LT(button_->image_->bounds().right(), button_->label_->bounds().x());
+ button_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
+ button_->Layout();
+ EXPECT_EQ(gfx::ALIGN_CENTER, button_->GetHorizontalAlignment());
+ EXPECT_LT(button_->image_->bounds().right(), button_->label_->bounds().x());
+ button_->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
+ button_->Layout();
+ EXPECT_EQ(gfx::ALIGN_RIGHT, button_->GetHorizontalAlignment());
+ EXPECT_LT(button_->label_->bounds().right(), button_->image_->bounds().x());
+
+ button_->SetText(base::string16());
+ EXPECT_GT(button_->GetPreferredSize().width(), text_width + image_size);
+ EXPECT_GT(button_->GetPreferredSize().height(), image_size);
+ button_->SetImage(Button::STATE_NORMAL, gfx::ImageSkia());
+ EXPECT_GT(button_->GetPreferredSize().width(), text_width + image_size);
+ EXPECT_GT(button_->GetPreferredSize().height(), image_size);
// Clamp the size to a maximum value.
- button.SetMaxSize(gfx::Size(image_size, 1));
- EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1));
+ button_->SetMaxSize(gfx::Size(image_size, 1));
+ EXPECT_EQ(button_->GetPreferredSize(), gfx::Size(image_size, 1));
// Clear the monotonically increasing minimum size.
- button.SetMinSize(gfx::Size());
- EXPECT_LT(button.GetPreferredSize().width(), text_width);
- EXPECT_LT(button.GetPreferredSize().width(), image_size);
- EXPECT_LT(button.GetPreferredSize().height(), image_size);
+ button_->SetMinSize(gfx::Size());
+ EXPECT_LT(button_->GetPreferredSize().width(), text_width);
+ EXPECT_LT(button_->GetPreferredSize().width(), image_size);
+ EXPECT_LT(button_->GetPreferredSize().height(), image_size);
}
TEST_F(LabelButtonTest, FontList) {
- const base::string16 text(ASCIIToUTF16("abc"));
- LabelButton button(NULL, text);
+ button_->SetText(base::ASCIIToUTF16("abc"));
- const gfx::FontList original_font_list = button.GetFontList();
+ const gfx::FontList original_font_list = button_->GetFontList();
const gfx::FontList large_font_list =
original_font_list.DeriveWithSizeDelta(100);
- const int original_width = button.GetPreferredSize().width();
- const int original_height = button.GetPreferredSize().height();
+ const int original_width = button_->GetPreferredSize().width();
+ const int original_height = button_->GetPreferredSize().height();
// The button size increases when the font size is increased.
- button.SetFontList(large_font_list);
- EXPECT_GT(button.GetPreferredSize().width(), original_width);
- EXPECT_GT(button.GetPreferredSize().height(), original_height);
+ button_->SetFontList(large_font_list);
+ EXPECT_GT(button_->GetPreferredSize().width(), original_width);
+ EXPECT_GT(button_->GetPreferredSize().height(), original_height);
// The button returns to its original size when the minimal size is cleared
// and the original font size is restored.
- button.SetMinSize(gfx::Size());
- button.SetFontList(original_font_list);
- EXPECT_EQ(original_width, button.GetPreferredSize().width());
- EXPECT_EQ(original_height, button.GetPreferredSize().height());
+ button_->SetMinSize(gfx::Size());
+ button_->SetFontList(original_font_list);
+ EXPECT_EQ(original_width, button_->GetPreferredSize().width());
+ EXPECT_EQ(original_height, button_->GetPreferredSize().height());
}
TEST_F(LabelButtonTest, ChangeTextSize) {
const base::string16 text(ASCIIToUTF16("abc"));
const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm"));
- LabelButton button(NULL, text);
+ button_->SetText(text);
- const int original_width = button.GetPreferredSize().width();
+ const int original_width = button_->GetPreferredSize().width();
// The button size increases when the text size is increased.
- button.SetText(longer_text);
- EXPECT_GT(button.GetPreferredSize().width(), original_width);
+ button_->SetText(longer_text);
+ EXPECT_GT(button_->GetPreferredSize().width(), original_width);
// The button returns to its original size when the original text is restored.
- button.SetMinSize(gfx::Size());
- button.SetText(text);
- EXPECT_EQ(original_width, button.GetPreferredSize().width());
+ button_->SetMinSize(gfx::Size());
+ button_->SetText(text);
+ EXPECT_EQ(original_width, button_->GetPreferredSize().width());
}
TEST_F(LabelButtonTest, ChangeLabelImageSpacing) {
- LabelButton button(NULL, ASCIIToUTF16("abc"));
- button.SetImage(Button::STATE_NORMAL, CreateTestImage(50, 50));
+ button_->SetText(ASCIIToUTF16("abc"));
+ button_->SetImage(Button::STATE_NORMAL, CreateTestImage(50, 50));
const int kOriginalSpacing = 5;
- button.SetImageLabelSpacing(kOriginalSpacing);
- const int original_width = button.GetPreferredSize().width();
+ button_->SetImageLabelSpacing(kOriginalSpacing);
+ const int original_width = button_->GetPreferredSize().width();
// Increasing the spacing between the text and label should increase the size.
- button.SetImageLabelSpacing(2 * kOriginalSpacing);
- EXPECT_GT(button.GetPreferredSize().width(), original_width);
+ button_->SetImageLabelSpacing(2 * kOriginalSpacing);
+ EXPECT_GT(button_->GetPreferredSize().width(), original_width);
// The button shrinks if the original spacing is restored.
- button.SetMinSize(gfx::Size());
- button.SetImageLabelSpacing(kOriginalSpacing);
- EXPECT_EQ(original_width, button.GetPreferredSize().width());
+ button_->SetMinSize(gfx::Size());
+ button_->SetImageLabelSpacing(kOriginalSpacing);
+ EXPECT_EQ(original_width, button_->GetPreferredSize().width());
}
} // namespace views
« ui/views/controls/button/label_button.cc ('K') | « ui/views/controls/button/label_button.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698