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") {
"test/test_views_delegate.h",
"test/test_widget_observer.cc",
"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.h",
"test/views_test_helper.cc",
......
......@@ -35,6 +35,7 @@
#include "ui/views/controls/link.h"
#include "ui/views/controls/textfield/textfield.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/widget/widget_utils.h"
......@@ -251,7 +252,12 @@ class ButtonTest : public ViewsTestBase {
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) {
event_generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint());
event_generator()->PressLeftButton();
......
......@@ -30,7 +30,9 @@
#include "ui/views/controls/combobox/combobox_listener.h"
#include "ui/views/style/platform_style.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/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_utils.h"
......@@ -42,36 +44,7 @@ using test::ComboboxTestApi;
namespace {
// A wrapper of Combobox to intercept the result of OnKeyPressed() and
// 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);
};
using TestCombobox = Combobox;
// A concrete class is needed to test the combobox.
class TestComboboxModel : public ui::ComboboxModel {
......@@ -222,8 +195,7 @@ class ComboboxTest : public ViewsTestBase {
ComboboxTest() = default;
void TearDown() override {
if (widget_)
widget_->Close();
widget_.reset();
ViewsTestBase::TearDown();
}
......@@ -234,25 +206,25 @@ class ComboboxTest : public ViewsTestBase {
model_->SetSeparators(*separators);
ASSERT_FALSE(combobox_);
combobox_ = new TestCombobox(model_.get());
test_api_ = std::make_unique<ComboboxTestApi>(combobox_);
auto combobox = std::make_unique<TestCombobox>(model_.get());
test_api_ = std::make_unique<ComboboxTestApi>(combobox.get());
test_api_->InstallTestMenuRunner(&menu_show_count_);
combobox_->SetID(1);
combobox->SetID(1);
widget_ = new Widget;
widget_ = std::make_unique<Widget>();
Widget::InitParams params =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.bounds = gfx::Rect(200, 200, 200, 200);
widget_->Init(std::move(params));
View* container = widget_->SetContentsView(std::make_unique<View>());
container->AddChildView(combobox_);
combobox_ = container->AddChildView(std::move(combobox));
widget_->Show();
combobox_->RequestFocus();
combobox_->SizeToPreferredSize();
event_generator_ =
std::make_unique<ui::test::EventGenerator>(GetRootWindow(widget_));
event_generator_ = std::make_unique<ui::test::EventGenerator>(
GetRootWindow(widget_.get()));
event_generator_->set_target(ui::test::EventGenerator::Target::WINDOW);
}
......@@ -290,7 +262,7 @@ class ComboboxTest : public ViewsTestBase {
}
// We need widget to populate wrapper class.
Widget* widget_ = nullptr;
UniqueWidgetPtr widget_;
// |combobox_| will be allocated InitCombobox() and then owned by |widget_|.
TestCombobox* combobox_ = nullptr;
......@@ -355,22 +327,28 @@ TEST_F(ComboboxTest, KeyTestMac) {
}
#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
// native wrapper inherits the disabled state when it gets created.
TEST_F(ComboboxTest, DisabilityTest) {
model_ = std::make_unique<TestComboboxModel>();
ASSERT_FALSE(combobox_);
combobox_ = new TestCombobox(model_.get());
combobox_->SetEnabled(false);
auto combobox = std::make_unique<TestCombobox>(model_.get());
combobox->SetEnabled(false);
widget_ = new Widget;
widget_ = std::make_unique<Widget>();
Widget::InitParams params =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.bounds = gfx::Rect(100, 100, 100, 100);
widget_->Init(std::move(params));
View* container = widget_->SetContentsView(std::make_unique<View>());
container->AddChildView(combobox_);
combobox_ = container->AddChildView(std::move(combobox));
EXPECT_FALSE(combobox_->GetEnabled());
}
......@@ -557,7 +535,7 @@ TEST_F(ComboboxTest, ListenerHandlesDelete) {
// |combobox| will be deleted on change.
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());
ASSERT_NO_FATAL_FAILURE(ComboboxTestApi(combobox).PerformActionAt(2));
EXPECT_TRUE(evil_listener->deleted());
......
......@@ -25,7 +25,9 @@
#include "ui/views/controls/prefix_selector.h"
#include "ui/views/controls/textfield/textfield.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/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"
using ui::TreeModel;
......@@ -143,7 +145,7 @@ class TreeViewTest : public ViewsTestBase {
ui::TreeNodeModel<TestNode> model_;
TreeView* tree_;
Widget* widget_;
UniqueWidgetPtr widget_;
private:
std::string InternalNodeAsString(TreeView::InternalNode* node);
......@@ -159,12 +161,12 @@ class TreeViewTest : public ViewsTestBase {
void TreeViewTest::SetUp() {
ViewsTestBase::SetUp();
widget_ = new Widget;
widget_ = std::make_unique<Widget>();
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
params.bounds = gfx::Rect(0, 0, 200, 200);
widget_->Init(std::move(params));
tree_ = new TreeView();
widget_->GetContentsView()->AddChildView(tree_);
tree_ =
widget_->GetContentsView()->AddChildView(std::make_unique<TreeView>());
tree_->RequestFocus();
ViewAccessibility::AccessibilityEventsCallback accessibility_events_callback =
......@@ -183,8 +185,7 @@ void TreeViewTest::SetUp() {
}
void TreeViewTest::TearDown() {
if (!widget_->IsClosed())
widget_->Close();
widget_.reset();
ViewsTestBase::TearDown();
}
......@@ -369,6 +370,12 @@ std::string TreeViewTest::InternalNodeAsString(TreeView::InternalNode* node) {
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.
TEST_F(TreeViewTest, SetModel) {
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 @@
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/metadata/metadata_types.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/view_observer.h"
#include "ui/views/views_features.h"
......@@ -284,6 +285,15 @@ class TestView : public View {
ax::mojom::Event last_a11y_event_;
};
////////////////////////////////////////////////////////////////////////////////
// Metadata
////////////////////////////////////////////////////////////////////////////////
TEST_F(ViewTest, MetadataTest) {
auto test_view = std::make_unique<TestView>();
test::TestViewMetadata(test_view.get());
}
////////////////////////////////////////////////////////////////////////////////
// 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