Commit 866e9f8f authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

views: support setting Widget internal name via delegate

This will make using AnyWidgetWaiter a lot more convenient for tests
that cover code which does not subclass View for the widget delegate's
contents view (which is current best practice).

Bug: 1075649
Change-Id: Ie1c8d2917f525176d1b17bcbeddd7b6f93b19644
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2492891
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821285}
parent 655d04d4
......@@ -292,11 +292,13 @@ bool Widget::RequiresNonClientView(InitParams::Type type) {
void Widget::Init(InitParams params) {
TRACE_EVENT0("views", "Widget::Init");
// If an internal name was not provided the class name of the contents view
// is a reasonable default.
if (params.name.empty() && params.delegate &&
params.delegate->GetContentsView())
params.name = params.delegate->GetContentsView()->GetClassName();
if (params.name.empty() && params.delegate) {
params.name = params.delegate->internal_name();
// If an internal name was not provided the class name of the contents view
// is a reasonable default.
if (params.name.empty() && params.delegate->GetContentsView())
params.name = params.delegate->GetContentsView()->GetClassName();
}
params.child |= (params.type == InitParams::TYPE_CONTROL);
is_top_level_ = !params.child;
......
......@@ -68,6 +68,11 @@ class VIEWS_EXPORT WidgetDelegate {
// this WidgetDelegate is used to initialize a Widget.
base::Optional<View*> initially_focused_view;
// The widget's internal name, used to identify it in window-state
// restoration (if this widget participates in that) and in debugging
// contexts. Never displayed to the user, and not translated.
std::string internal_name;
// The widget's modality type. Note that MODAL_TYPE_SYSTEM does not work at
// all on Mac.
ui::ModalType modal_type = ui::MODAL_TYPE_NONE;
......@@ -350,6 +355,9 @@ class VIEWS_EXPORT WidgetDelegate {
bool focus_traverses_out() const { return params_.focus_traverses_out; }
bool owned_by_widget() const { return params_.owned_by_widget; }
void set_internal_name(std::string name) { params_.internal_name = name; }
std::string internal_name() const { return params_.internal_name; }
private:
friend class Widget;
......
......@@ -173,22 +173,62 @@ TEST_F(WidgetTest, WidgetInitParams) {
// Tests that the internal name is propagated through widget initialization to
// the native widget and back.
class WidgetNameTest : public WidgetTest {
class WidgetWithCustomParamsTest : public WidgetTest {
public:
using InitFunction = base::RepeatingCallback<void(Widget::InitParams*)>;
void SetInitFunction(const InitFunction& init) { init_ = std::move(init); }
Widget::InitParams CreateParams(Widget::InitParams::Type type) override {
Widget::InitParams params = WidgetTest::CreateParams(type);
params.name = "MyWidget";
DCHECK(init_) << "If you don't need an init function, use WidgetTest";
init_.Run(&params);
return params;
}
private:
InitFunction init_;
};
TEST_F(WidgetNameTest, GetName) {
TEST_F(WidgetWithCustomParamsTest, NamePropagatedFromParams) {
SetInitFunction(base::BindLambdaForTesting(
[](Widget::InitParams* params) { params->name = "MyWidget"; }));
std::unique_ptr<Widget> widget = CreateTestWidget();
EXPECT_EQ("MyWidget", widget->native_widget_private()->GetName());
EXPECT_EQ("MyWidget", widget->GetName());
}
TEST_F(WidgetWithCustomParamsTest, NamePropagatedFromDelegate) {
WidgetDelegate delegate;
delegate.set_internal_name("Foobar");
SetInitFunction(base::BindLambdaForTesting(
[&](Widget::InitParams* params) { params->delegate = &delegate; }));
std::unique_ptr<Widget> widget = CreateTestWidget();
EXPECT_EQ(delegate.internal_name(),
widget->native_widget_private()->GetName());
EXPECT_EQ(delegate.internal_name(), widget->GetName());
}
TEST_F(WidgetWithCustomParamsTest, NamePropagatedFromContentsViewClassName) {
class ViewWithClassName : public View {
public:
const char* GetClassName() const override { return "ViewWithClassName"; }
};
WidgetDelegate delegate;
auto view = std::make_unique<ViewWithClassName>();
auto* contents = delegate.SetContentsView(std::move(view));
SetInitFunction(base::BindLambdaForTesting(
[&](Widget::InitParams* params) { params->delegate = &delegate; }));
std::unique_ptr<Widget> widget = CreateTestWidget();
EXPECT_EQ(contents->GetClassName(),
widget->native_widget_private()->GetName());
EXPECT_EQ(contents->GetClassName(), widget->GetName());
}
TEST_F(WidgetTest, NativeWindowProperty) {
const char* key = "foo";
int value = 3;
......
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