Commit feaca92a authored by Keren Zhu's avatar Keren Zhu Committed by Commit Bot

views: Make ImageButton and ToggleButton default constructible

This CL is part of the effort to make Views default
constructible, allowing UI to be built in a declarative fashion.

Bug: 1108460
Change-Id: I4f6d6a9e271990ead0e9e28fe7ebc3b2da401ad2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2330632
Commit-Queue: Keren Zhu <kerenzhu@chromium.org>
Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Auto-Submit: Keren Zhu <kerenzhu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#793517}
parent 26256e92
...@@ -151,10 +151,9 @@ class DesktopLinuxBrowserFrameViewLayoutTest : public ChromeViewsTestBase { ...@@ -151,10 +151,9 @@ class DesktopLinuxBrowserFrameViewLayoutTest : public ChromeViewsTestBase {
protected: protected:
views::ImageButton* InitWindowCaptionButton(ViewID view_id) { views::ImageButton* InitWindowCaptionButton(ViewID view_id) {
views::ImageButton* button = new views::ImageButton(nullptr); auto button = std::make_unique<views::ImageButton>();
button->SetID(view_id); button->SetID(view_id);
root_view_->AddChildView(button); return root_view_->AddChildView(std::move(button));
return button;
} }
void ResetNativeNavButtonImagesFromButtonProvider() { void ResetNativeNavButtonImagesFromButtonProvider() {
......
...@@ -138,13 +138,15 @@ class OpaqueBrowserFrameViewLayoutTest ...@@ -138,13 +138,15 @@ class OpaqueBrowserFrameViewLayoutTest
protected: protected:
views::ImageButton* InitWindowCaptionButton(ViewID view_id, views::ImageButton* InitWindowCaptionButton(ViewID view_id,
const gfx::Size& size) { const gfx::Size& size) {
views::ImageButton* button = new views::ImageButton(nullptr); auto button = std::make_unique<views::ImageButton>();
gfx::ImageSkiaRep rep(size, 1.0f); gfx::ImageSkiaRep rep(size, 1.0f);
gfx::ImageSkia image(rep); gfx::ImageSkia image(rep);
button->SetImage(views::Button::STATE_NORMAL, &image); button->SetImage(views::Button::STATE_NORMAL, &image);
button->SetID(view_id); button->SetID(view_id);
root_view_->AddChildView(button);
return button; // OpaqueBrowserFrameViewLayout requires the id of a view is set before
// attaching it to a parent.
return root_view_->AddChildView(std::move(button));
} }
void AddWindowTitleIcons() { void AddWindowTitleIcons() {
......
...@@ -27,8 +27,8 @@ TEST_F(AccessibilityCheckerTest, VerifyAccessibilityCheckerFailAndPass) { ...@@ -27,8 +27,8 @@ TEST_F(AccessibilityCheckerTest, VerifyAccessibilityCheckerFailAndPass) {
widget.Show(); widget.Show();
// Add the button. // Add the button.
views::ImageButton* button = new views::ImageButton(nullptr); auto* button = widget.GetContentsView()->AddChildView(
widget.GetContentsView()->AddChildView(button); std::make_unique<views::ImageButton>());
// Accessibility test should pass as it is focusable but has a name. // Accessibility test should pass as it is focusable but has a name.
button->SetFocusBehavior(views::View::FocusBehavior::ALWAYS); button->SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
......
...@@ -474,7 +474,7 @@ TEST_F(ButtonTest, AsButton) { ...@@ -474,7 +474,7 @@ TEST_F(ButtonTest, AsButton) {
LabelButton label_button(nullptr, text); LabelButton label_button(nullptr, text);
EXPECT_TRUE(Button::AsButton(&label_button)); EXPECT_TRUE(Button::AsButton(&label_button));
ImageButton image_button(nullptr); ImageButton image_button;
EXPECT_TRUE(Button::AsButton(&image_button)); EXPECT_TRUE(Button::AsButton(&image_button));
Checkbox checkbox(text); Checkbox checkbox(text);
...@@ -486,7 +486,7 @@ TEST_F(ButtonTest, AsButton) { ...@@ -486,7 +486,7 @@ TEST_F(ButtonTest, AsButton) {
MenuButton menu_button(text, nullptr); MenuButton menu_button(text, nullptr);
EXPECT_TRUE(Button::AsButton(&menu_button)); EXPECT_TRUE(Button::AsButton(&menu_button));
ToggleButton toggle_button(nullptr); ToggleButton toggle_button;
EXPECT_TRUE(Button::AsButton(&toggle_button)); EXPECT_TRUE(Button::AsButton(&toggle_button));
Label label; Label label;
......
...@@ -29,7 +29,7 @@ class VIEWS_EXPORT ImageButton : public Button { ...@@ -29,7 +29,7 @@ class VIEWS_EXPORT ImageButton : public Button {
// An enum describing the vertical alignment of images on Buttons. // An enum describing the vertical alignment of images on Buttons.
enum VerticalAlignment { ALIGN_TOP = 0, ALIGN_MIDDLE, ALIGN_BOTTOM }; enum VerticalAlignment { ALIGN_TOP = 0, ALIGN_MIDDLE, ALIGN_BOTTOM };
explicit ImageButton(ButtonListener* listener); explicit ImageButton(ButtonListener* listener = nullptr);
~ImageButton() override; ~ImageButton() override;
// Returns the image for a given |state|. // Returns the image for a given |state|.
......
...@@ -40,7 +40,7 @@ namespace views { ...@@ -40,7 +40,7 @@ namespace views {
using ImageButtonTest = ViewsTestBase; using ImageButtonTest = ViewsTestBase;
TEST_F(ImageButtonTest, Basics) { TEST_F(ImageButtonTest, Basics) {
ImageButton button(nullptr); ImageButton button;
// Our image to paint starts empty. // Our image to paint starts empty.
EXPECT_TRUE(button.GetImageToPaint().isNull()); EXPECT_TRUE(button.GetImageToPaint().isNull());
...@@ -88,7 +88,7 @@ TEST_F(ImageButtonTest, Basics) { ...@@ -88,7 +88,7 @@ TEST_F(ImageButtonTest, Basics) {
} }
TEST_F(ImageButtonTest, SetAndGetImage) { TEST_F(ImageButtonTest, SetAndGetImage) {
ImageButton button(nullptr); ImageButton button;
// Images start as null. // Images start as null.
EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull());
...@@ -114,7 +114,7 @@ TEST_F(ImageButtonTest, SetAndGetImage) { ...@@ -114,7 +114,7 @@ TEST_F(ImageButtonTest, SetAndGetImage) {
} }
TEST_F(ImageButtonTest, ImagePositionWithBorder) { TEST_F(ImageButtonTest, ImagePositionWithBorder) {
ImageButton button(nullptr); ImageButton button;
gfx::ImageSkia image = CreateTestImage(20, 30); gfx::ImageSkia image = CreateTestImage(20, 30);
button.SetImage(Button::STATE_NORMAL, &image); button.SetImage(Button::STATE_NORMAL, &image);
...@@ -143,7 +143,7 @@ TEST_F(ImageButtonTest, ImagePositionWithBorder) { ...@@ -143,7 +143,7 @@ TEST_F(ImageButtonTest, ImagePositionWithBorder) {
} }
TEST_F(ImageButtonTest, LeftAlignedMirrored) { TEST_F(ImageButtonTest, LeftAlignedMirrored) {
ImageButton button(nullptr); ImageButton button;
gfx::ImageSkia image = CreateTestImage(20, 30); gfx::ImageSkia image = CreateTestImage(20, 30);
button.SetImage(Button::STATE_NORMAL, &image); button.SetImage(Button::STATE_NORMAL, &image);
button.SetBounds(0, 0, 50, 30); button.SetBounds(0, 0, 50, 30);
...@@ -156,7 +156,7 @@ TEST_F(ImageButtonTest, LeftAlignedMirrored) { ...@@ -156,7 +156,7 @@ TEST_F(ImageButtonTest, LeftAlignedMirrored) {
} }
TEST_F(ImageButtonTest, RightAlignedMirrored) { TEST_F(ImageButtonTest, RightAlignedMirrored) {
ImageButton button(nullptr); ImageButton button;
gfx::ImageSkia image = CreateTestImage(20, 30); gfx::ImageSkia image = CreateTestImage(20, 30);
button.SetImage(Button::STATE_NORMAL, &image); button.SetImage(Button::STATE_NORMAL, &image);
button.SetBounds(0, 0, 50, 30); button.SetBounds(0, 0, 50, 30);
...@@ -171,7 +171,7 @@ TEST_F(ImageButtonTest, RightAlignedMirrored) { ...@@ -171,7 +171,7 @@ TEST_F(ImageButtonTest, RightAlignedMirrored) {
TEST_F(ImageButtonTest, PreferredSizeInvalidation) { TEST_F(ImageButtonTest, PreferredSizeInvalidation) {
Parent parent; Parent parent;
ImageButton button(nullptr); ImageButton button;
gfx::ImageSkia first_image = CreateTestImage(20, 30); gfx::ImageSkia first_image = CreateTestImage(20, 30);
gfx::ImageSkia second_image = CreateTestImage(50, 50); gfx::ImageSkia second_image = CreateTestImage(50, 50);
button.SetImage(Button::STATE_NORMAL, &first_image); button.SetImage(Button::STATE_NORMAL, &first_image);
......
...@@ -20,7 +20,7 @@ class VIEWS_EXPORT ToggleButton : public Button { ...@@ -20,7 +20,7 @@ class VIEWS_EXPORT ToggleButton : public Button {
public: public:
METADATA_HEADER(ToggleButton); METADATA_HEADER(ToggleButton);
explicit ToggleButton(ButtonListener* listener); explicit ToggleButton(ButtonListener* listener = nullptr);
~ToggleButton() override; ~ToggleButton() override;
// AnimateIsOn() animates the state change to |is_on|; SetIsOn() doesn't. // AnimateIsOn() animates the state change to |is_on|; SetIsOn() doesn't.
......
...@@ -19,8 +19,7 @@ namespace views { ...@@ -19,8 +19,7 @@ namespace views {
class TestToggleButton : public ToggleButton { class TestToggleButton : public ToggleButton {
public: public:
explicit TestToggleButton(int* counter) explicit TestToggleButton(int* counter) : counter_(counter) {}
: ToggleButton(nullptr), counter_(counter) {}
~TestToggleButton() override { ~TestToggleButton() override {
// Calling SetInkDropMode() in this subclass allows this class's // Calling SetInkDropMode() in this subclass allows this class's
// implementation of RemoveInkDropLayer() to be called. The same // implementation of RemoveInkDropLayer() to be called. The same
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment