Commit 1de1b8f4 authored by Allen Bauer's avatar Allen Bauer Committed by Commit Bot

Testability: Added function to iterate over all metadata for a View.

views: :View - added TestMetadata test.
views: :Button - added TestMetadata test.
Change-Id: I7fc797f59e837d1c9eacc9004a39a882ac3afd93
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2258340
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781996}
parent 22b39c46
...@@ -925,6 +925,8 @@ jumbo_source_set("test_support") { ...@@ -925,6 +925,8 @@ jumbo_source_set("test_support") {
"test/test_views_delegate.h", "test/test_views_delegate.h",
"test/test_widget_observer.cc", "test/test_widget_observer.cc",
"test/test_widget_observer.h", "test/test_widget_observer.h",
"test/view_metadata_test_utils.cc",
"test/view_metadata_test_utils.h",
"test/views_test_base.cc", "test/views_test_base.cc",
"test/views_test_base.h", "test/views_test_base.h",
"test/views_test_helper.cc", "test/views_test_helper.cc",
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "ui/views/controls/link.h" #include "ui/views/controls/link.h"
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
#include "ui/views/style/platform_style.h" #include "ui/views/style/platform_style.h"
#include "ui/views/test/view_metadata_test_utils.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget_utils.h" #include "ui/views/widget/widget_utils.h"
...@@ -251,7 +252,12 @@ class ButtonTest : public ViewsTestBase { ...@@ -251,7 +252,12 @@ class ButtonTest : public ViewsTestBase {
DISALLOW_COPY_AND_ASSIGN(ButtonTest); DISALLOW_COPY_AND_ASSIGN(ButtonTest);
}; };
// Tests that hover state changes correctly when visiblity/enableness changes. // Iterate through the metadata for Button to ensure it all works.
TEST_F(ButtonTest, MetadataTest) {
test::TestViewMetadata(button());
}
// Tests that hover state changes correctly when visibility/enableness changes.
TEST_F(ButtonTest, HoverStateOnVisibilityChange) { TEST_F(ButtonTest, HoverStateOnVisibilityChange) {
event_generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint()); event_generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint());
event_generator()->PressLeftButton(); event_generator()->PressLeftButton();
......
...@@ -30,7 +30,9 @@ ...@@ -30,7 +30,9 @@
#include "ui/views/controls/combobox/combobox_listener.h" #include "ui/views/controls/combobox/combobox_listener.h"
#include "ui/views/style/platform_style.h" #include "ui/views/style/platform_style.h"
#include "ui/views/test/combobox_test_api.h" #include "ui/views/test/combobox_test_api.h"
#include "ui/views/test/view_metadata_test_utils.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_utils.h" #include "ui/views/widget/widget_utils.h"
...@@ -42,36 +44,7 @@ using test::ComboboxTestApi; ...@@ -42,36 +44,7 @@ using test::ComboboxTestApi;
namespace { namespace {
// A wrapper of Combobox to intercept the result of OnKeyPressed() and using TestCombobox = Combobox;
// OnKeyReleased() methods.
class TestCombobox : public Combobox {
public:
explicit TestCombobox(ui::ComboboxModel* model)
: Combobox(model), key_handled_(false), key_received_(false) {}
bool OnKeyPressed(const ui::KeyEvent& e) override {
key_received_ = true;
key_handled_ = Combobox::OnKeyPressed(e);
return key_handled_;
}
bool OnKeyReleased(const ui::KeyEvent& e) override {
key_received_ = true;
key_handled_ = Combobox::OnKeyReleased(e);
return key_handled_;
}
bool key_handled() const { return key_handled_; }
bool key_received() const { return key_received_; }
void clear() { key_received_ = key_handled_ = false; }
private:
bool key_handled_;
bool key_received_;
DISALLOW_COPY_AND_ASSIGN(TestCombobox);
};
// A concrete class is needed to test the combobox. // A concrete class is needed to test the combobox.
class TestComboboxModel : public ui::ComboboxModel { class TestComboboxModel : public ui::ComboboxModel {
...@@ -222,8 +195,7 @@ class ComboboxTest : public ViewsTestBase { ...@@ -222,8 +195,7 @@ class ComboboxTest : public ViewsTestBase {
ComboboxTest() = default; ComboboxTest() = default;
void TearDown() override { void TearDown() override {
if (widget_) widget_.reset();
widget_->Close();
ViewsTestBase::TearDown(); ViewsTestBase::TearDown();
} }
...@@ -234,25 +206,25 @@ class ComboboxTest : public ViewsTestBase { ...@@ -234,25 +206,25 @@ class ComboboxTest : public ViewsTestBase {
model_->SetSeparators(*separators); model_->SetSeparators(*separators);
ASSERT_FALSE(combobox_); ASSERT_FALSE(combobox_);
combobox_ = new TestCombobox(model_.get()); auto combobox = std::make_unique<TestCombobox>(model_.get());
test_api_ = std::make_unique<ComboboxTestApi>(combobox_); test_api_ = std::make_unique<ComboboxTestApi>(combobox.get());
test_api_->InstallTestMenuRunner(&menu_show_count_); test_api_->InstallTestMenuRunner(&menu_show_count_);
combobox_->SetID(1); combobox->SetID(1);
widget_ = new Widget; widget_ = std::make_unique<Widget>();
Widget::InitParams params = Widget::InitParams params =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.bounds = gfx::Rect(200, 200, 200, 200); params.bounds = gfx::Rect(200, 200, 200, 200);
widget_->Init(std::move(params)); widget_->Init(std::move(params));
View* container = widget_->SetContentsView(std::make_unique<View>()); View* container = widget_->SetContentsView(std::make_unique<View>());
container->AddChildView(combobox_); combobox_ = container->AddChildView(std::move(combobox));
widget_->Show(); widget_->Show();
combobox_->RequestFocus(); combobox_->RequestFocus();
combobox_->SizeToPreferredSize(); combobox_->SizeToPreferredSize();
event_generator_ = event_generator_ = std::make_unique<ui::test::EventGenerator>(
std::make_unique<ui::test::EventGenerator>(GetRootWindow(widget_)); GetRootWindow(widget_.get()));
event_generator_->set_target(ui::test::EventGenerator::Target::WINDOW); event_generator_->set_target(ui::test::EventGenerator::Target::WINDOW);
} }
...@@ -290,7 +262,7 @@ class ComboboxTest : public ViewsTestBase { ...@@ -290,7 +262,7 @@ class ComboboxTest : public ViewsTestBase {
} }
// We need widget to populate wrapper class. // We need widget to populate wrapper class.
Widget* widget_ = nullptr; UniqueWidgetPtr widget_;
// |combobox_| will be allocated InitCombobox() and then owned by |widget_|. // |combobox_| will be allocated InitCombobox() and then owned by |widget_|.
TestCombobox* combobox_ = nullptr; TestCombobox* combobox_ = nullptr;
...@@ -355,22 +327,28 @@ TEST_F(ComboboxTest, KeyTestMac) { ...@@ -355,22 +327,28 @@ TEST_F(ComboboxTest, KeyTestMac) {
} }
#endif #endif
// Iterate through all the metadata and test each property.
TEST_F(ComboboxTest, MetadataTest) {
InitCombobox(nullptr);
test::TestViewMetadata(combobox_);
}
// Check that if a combobox is disabled before it has a native wrapper, then the // Check that if a combobox is disabled before it has a native wrapper, then the
// native wrapper inherits the disabled state when it gets created. // native wrapper inherits the disabled state when it gets created.
TEST_F(ComboboxTest, DisabilityTest) { TEST_F(ComboboxTest, DisabilityTest) {
model_ = std::make_unique<TestComboboxModel>(); model_ = std::make_unique<TestComboboxModel>();
ASSERT_FALSE(combobox_); ASSERT_FALSE(combobox_);
combobox_ = new TestCombobox(model_.get()); auto combobox = std::make_unique<TestCombobox>(model_.get());
combobox_->SetEnabled(false); combobox->SetEnabled(false);
widget_ = new Widget; widget_ = std::make_unique<Widget>();
Widget::InitParams params = Widget::InitParams params =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.bounds = gfx::Rect(100, 100, 100, 100); params.bounds = gfx::Rect(100, 100, 100, 100);
widget_->Init(std::move(params)); widget_->Init(std::move(params));
View* container = widget_->SetContentsView(std::make_unique<View>()); View* container = widget_->SetContentsView(std::make_unique<View>());
container->AddChildView(combobox_); combobox_ = container->AddChildView(std::move(combobox));
EXPECT_FALSE(combobox_->GetEnabled()); EXPECT_FALSE(combobox_->GetEnabled());
} }
...@@ -557,7 +535,7 @@ TEST_F(ComboboxTest, ListenerHandlesDelete) { ...@@ -557,7 +535,7 @@ TEST_F(ComboboxTest, ListenerHandlesDelete) {
// |combobox| will be deleted on change. // |combobox| will be deleted on change.
TestCombobox* combobox = new TestCombobox(&model); TestCombobox* combobox = new TestCombobox(&model);
std::unique_ptr<EvilListener> evil_listener(new EvilListener()); auto evil_listener = std::make_unique<EvilListener>();
combobox->set_listener(evil_listener.get()); combobox->set_listener(evil_listener.get());
ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2)); ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2));
EXPECT_TRUE(evil_listener->deleted()); EXPECT_TRUE(evil_listener->deleted());
......
...@@ -25,7 +25,9 @@ ...@@ -25,7 +25,9 @@
#include "ui/views/controls/prefix_selector.h" #include "ui/views/controls/prefix_selector.h"
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/tree/tree_view_controller.h" #include "ui/views/controls/tree/tree_view_controller.h"
#include "ui/views/test/view_metadata_test_utils.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
using ui::TreeModel; using ui::TreeModel;
...@@ -143,7 +145,7 @@ class TreeViewTest : public ViewsTestBase { ...@@ -143,7 +145,7 @@ class TreeViewTest : public ViewsTestBase {
ui::TreeNodeModel<TestNode> model_; ui::TreeNodeModel<TestNode> model_;
TreeView* tree_; TreeView* tree_;
Widget* widget_; UniqueWidgetPtr widget_;
private: private:
std::string InternalNodeAsString(TreeView::InternalNode* node); std::string InternalNodeAsString(TreeView::InternalNode* node);
...@@ -159,12 +161,12 @@ class TreeViewTest : public ViewsTestBase { ...@@ -159,12 +161,12 @@ class TreeViewTest : public ViewsTestBase {
void TreeViewTest::SetUp() { void TreeViewTest::SetUp() {
ViewsTestBase::SetUp(); ViewsTestBase::SetUp();
widget_ = new Widget; widget_ = std::make_unique<Widget>();
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
params.bounds = gfx::Rect(0, 0, 200, 200); params.bounds = gfx::Rect(0, 0, 200, 200);
widget_->Init(std::move(params)); widget_->Init(std::move(params));
tree_ = new TreeView(); tree_ =
widget_->GetContentsView()->AddChildView(tree_); widget_->GetContentsView()->AddChildView(std::make_unique<TreeView>());
tree_->RequestFocus(); tree_->RequestFocus();
ViewAccessibility::AccessibilityEventsCallback accessibility_events_callback = ViewAccessibility::AccessibilityEventsCallback accessibility_events_callback =
...@@ -183,8 +185,7 @@ void TreeViewTest::SetUp() { ...@@ -183,8 +185,7 @@ void TreeViewTest::SetUp() {
} }
void TreeViewTest::TearDown() { void TreeViewTest::TearDown() {
if (!widget_->IsClosed()) widget_.reset();
widget_->Close();
ViewsTestBase::TearDown(); ViewsTestBase::TearDown();
} }
...@@ -369,6 +370,12 @@ std::string TreeViewTest::InternalNodeAsString(TreeView::InternalNode* node) { ...@@ -369,6 +370,12 @@ std::string TreeViewTest::InternalNodeAsString(TreeView::InternalNode* node) {
return result; return result;
} }
// Verify properties are accessible via metadata.
TEST_F(TreeViewTest, MetadataTest) {
tree_->SetModel(&model_);
test::TestViewMetadata(tree_);
}
// Verifies setting model correctly updates internal state. // Verifies setting model correctly updates internal state.
TEST_F(TreeViewTest, SetModel) { TEST_F(TreeViewTest, SetModel) {
tree_->SetModel(&model_); tree_->SetModel(&model_);
......
// Copyright 2020 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 "ui/views/test/view_metadata_test_utils.h"
#include "base/strings/string16.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/metadata/metadata_types.h"
namespace views {
namespace test {
void TestViewMetadata(View* view) {
metadata::ClassMetaData* meta_data = view->GetClassMetaData();
EXPECT_NE(meta_data, nullptr);
for (auto* property : *meta_data) {
base::string16 value = property->GetValueAsString(view);
if (property->GetPropertyFlags() != metadata::PropertyFlags::kReadOnly)
property->SetValueAsString(view, value);
}
}
} // namespace test
} // namespace views
// Copyright 2020 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.
#ifndef UI_VIEWS_TEST_VIEW_METADATA_TEST_UTILS_H_
#define UI_VIEWS_TEST_VIEW_METADATA_TEST_UTILS_H_
#include "ui/views/view.h"
namespace views {
namespace test {
// Iterate through all the metadata for the given instance, reading each
// property and then setting the property. Exercises the getters, setters, and
// property type-converters associated with each.
void TestViewMetadata(View* view);
} // namespace test
} // namespace views
#endif // UI_VIEWS_TEST_VIEW_METADATA_TEST_UTILS_H_
...@@ -53,6 +53,7 @@ ...@@ -53,6 +53,7 @@
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
#include "ui/views/metadata/metadata_types.h" #include "ui/views/metadata/metadata_types.h"
#include "ui/views/paint_info.h" #include "ui/views/paint_info.h"
#include "ui/views/test/view_metadata_test_utils.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#include "ui/views/view_observer.h" #include "ui/views/view_observer.h"
#include "ui/views/views_features.h" #include "ui/views/views_features.h"
...@@ -284,6 +285,15 @@ class TestView : public View { ...@@ -284,6 +285,15 @@ class TestView : public View {
ax::mojom::Event last_a11y_event_; ax::mojom::Event last_a11y_event_;
}; };
////////////////////////////////////////////////////////////////////////////////
// Metadata
////////////////////////////////////////////////////////////////////////////////
TEST_F(ViewTest, MetadataTest) {
auto test_view = std::make_unique<TestView>();
test::TestViewMetadata(test_view.get());
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Layout // Layout
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
......
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