Commit e228dffd authored by Dana Fried's avatar Dana Fried Committed by Commit Bot

Improve debugging/testing features for layout types.

Adds better output for EXPECT/DCHECK/ASSERT around common layout types
including SizeBounds, as well as providing ToString() methods for
ProposedLayout and ChildLayout used by all LayoutManagerBase-derived
layout managers.

Change-Id: If9c5c99ac3aec2db4969038739fea6118e893191
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1958695
Commit-Queue: Dana Fried <dfried@chromium.org>
Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Auto-Submit: Dana Fried <dfried@chromium.org>
Cr-Commit-Position: refs/heads/master@{#723657}
parent 93eb9765
...@@ -915,6 +915,7 @@ jumbo_source_set("test_support") { ...@@ -915,6 +915,7 @@ jumbo_source_set("test_support") {
"test/desktop_test_views_delegate.h", "test/desktop_test_views_delegate.h",
"test/focus_manager_test.cc", "test/focus_manager_test.cc",
"test/focus_manager_test.h", "test/focus_manager_test.h",
"test/layout_test_utils.cc",
"test/menu_runner_test_api.cc", "test/menu_runner_test_api.cc",
"test/menu_runner_test_api.h", "test/menu_runner_test_api.h",
"test/menu_test_utils.cc", "test/menu_test_utils.cc",
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef UI_VIEWS_LAYOUT_LAYOUT_TYPES_H_ #ifndef UI_VIEWS_LAYOUT_LAYOUT_TYPES_H_
#define UI_VIEWS_LAYOUT_LAYOUT_TYPES_H_ #define UI_VIEWS_LAYOUT_LAYOUT_TYPES_H_
#include <ostream>
#include <string> #include <string>
#include "base/optional.h" #include "base/optional.h"
...@@ -53,6 +54,12 @@ class VIEWS_EXPORT SizeBounds { ...@@ -53,6 +54,12 @@ class VIEWS_EXPORT SizeBounds {
base::Optional<int> height_; base::Optional<int> height_;
}; };
// These are declared here for use in gtest-based unit tests but is defined in
// the views_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const SizeBounds& size_bounds, ::std::ostream* os);
void PrintTo(LayoutOrientation layout_orientation, ::std::ostream* os);
} // namespace views } // namespace views
#endif // UI_VIEWS_LAYOUT_LAYOUT_TYPES_H_ #endif // UI_VIEWS_LAYOUT_LAYOUT_TYPES_H_
...@@ -17,6 +17,13 @@ bool ChildLayout::operator==(const ChildLayout& other) const { ...@@ -17,6 +17,13 @@ bool ChildLayout::operator==(const ChildLayout& other) const {
(!visible || bounds == other.bounds); (!visible || bounds == other.bounds);
} }
std::string ChildLayout::ToString() const {
std::ostringstream oss;
oss << "{" << child_view << (visible ? " visible " : " not visible ")
<< bounds.ToString() << "}";
return oss.str();
}
ProposedLayout::ProposedLayout() = default; ProposedLayout::ProposedLayout() = default;
ProposedLayout::ProposedLayout(const ProposedLayout& other) = default; ProposedLayout::ProposedLayout(const ProposedLayout& other) = default;
ProposedLayout::ProposedLayout(ProposedLayout&& other) = default; ProposedLayout::ProposedLayout(ProposedLayout&& other) = default;
...@@ -33,6 +40,21 @@ bool ProposedLayout::operator==(const ProposedLayout& other) const { ...@@ -33,6 +40,21 @@ bool ProposedLayout::operator==(const ProposedLayout& other) const {
return host_size == other.host_size && child_layouts == other.child_layouts; return host_size == other.host_size && child_layouts == other.child_layouts;
} }
std::string ProposedLayout::ToString() const {
std::ostringstream oss;
oss << "{" << host_size.ToString() << " {";
bool first = true;
for (const auto& child_layout : child_layouts) {
if (first)
first = false;
else
oss << ", ";
oss << child_layout.ToString();
}
oss << "}}";
return oss.str();
}
ProposedLayout ProposedLayoutBetween(double value, ProposedLayout ProposedLayoutBetween(double value,
const ProposedLayout& start, const ProposedLayout& start,
const ProposedLayout& target) { const ProposedLayout& target) {
......
...@@ -18,6 +18,8 @@ struct VIEWS_EXPORT ChildLayout { ...@@ -18,6 +18,8 @@ struct VIEWS_EXPORT ChildLayout {
bool operator==(const ChildLayout& other) const; bool operator==(const ChildLayout& other) const;
bool operator!=(const ChildLayout& other) const { return !(*this == other); } bool operator!=(const ChildLayout& other) const { return !(*this == other); }
std::string ToString() const;
View* child_view = nullptr; View* child_view = nullptr;
bool visible = false; bool visible = false;
gfx::Rect bounds; gfx::Rect bounds;
...@@ -39,6 +41,8 @@ struct VIEWS_EXPORT ProposedLayout { ...@@ -39,6 +41,8 @@ struct VIEWS_EXPORT ProposedLayout {
return !(*this == other); return !(*this == other);
} }
std::string ToString() const;
// The size of the host view given the size bounds for this layout. If both // The size of the host view given the size bounds for this layout. If both
// dimensions of the size bounds are specified, this will be the same size. // dimensions of the size bounds are specified, this will be the same size.
gfx::Size host_size; gfx::Size host_size;
......
// Copyright 2019 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 <ostream>
#include "ui/views/layout/layout_types.h"
namespace views {
void PrintTo(const SizeBounds& size_bounds, ::std::ostream* os) {
*os << size_bounds.ToString();
}
void PrintTo(LayoutOrientation layout_orientation, ::std::ostream* os) {
switch (layout_orientation) {
case LayoutOrientation::kHorizontal:
*os << "LayoutOrientation::kHorizontal";
break;
case LayoutOrientation::kVertical:
*os << "LayoutOrientation::kVertical";
break;
}
}
} // namespace views
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