blob: e321103e490b8d05ebbc68cd6d893a23258db54e [file] [log] [blame]
// Copyright (c) 2012 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/view.h"
#include <stddef.h>
#include <map>
#include <memory>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/i18n/rtl.h"
#include "base/macros.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "cc/paint/display_item_list.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/compositor_switches.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/paint_context.h"
#include "ui/compositor/test/draw_waiter_for_test.h"
#include "ui/compositor/test/test_layers.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/scoped_target_handler.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/transform.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/test_native_theme.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/background.h"
#include "ui/views/controls/native/native_view_host.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/metadata/metadata_types.h"
#include "ui/views/paint_info.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/view_observer.h"
#include "ui/views/widget/native_widget.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/window/dialog_client_view.h"
#include "ui/views/window/dialog_delegate.h"
using base::ASCIIToUTF16;
namespace {
// Returns true if |ancestor| is an ancestor of |layer|.
bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) {
while (layer && layer != ancestor)
layer = layer->parent();
return layer == ancestor;
}
// Convenience functions for walking a View tree.
const views::View* FirstView(const views::View* view) {
const views::View* v = view;
while (!v->children().empty())
v = v->children().front();
return v;
}
const views::View* NextView(const views::View* view) {
const views::View* v = view;
const views::View* parent = v->parent();
if (!parent)
return nullptr;
const auto next = std::next(parent->FindChild(v));
return (next == parent->children().cend()) ? parent : FirstView(*next);
}
// Convenience functions for walking a Layer tree.
const ui::Layer* FirstLayer(const ui::Layer* layer) {
const ui::Layer* l = layer;
while (!l->children().empty())
l = l->children().front();
return l;
}
const ui::Layer* NextLayer(const ui::Layer* layer) {
const ui::Layer* parent = layer->parent();
if (!parent)
return nullptr;
const std::vector<ui::Layer*> children = parent->children();
const auto i = std::find(children.cbegin(), children.cend(), layer) + 1;
return (i == children.cend()) ? parent : FirstLayer(*i);
}
// Given the root nodes of a View tree and a Layer tree, makes sure the two
// trees are in sync.
bool ViewAndLayerTreeAreConsistent(const views::View* view,
const ui::Layer* layer) {
const views::View* v = FirstView(view);
const ui::Layer* l = FirstLayer(layer);
while (v && l) {
// Find the view with a layer.
while (v && !v->layer())
v = NextView(v);
EXPECT_TRUE(v);
if (!v)
return false;
// Check if the View tree and the Layer tree are in sync.
EXPECT_EQ(l, v->layer());
if (v->layer() != l)
return false;
// Check if the visibility states of the View and the Layer are in sync.
EXPECT_EQ(l->IsDrawn(), v->IsDrawn());
if (v->IsDrawn() != l->IsDrawn()) {
for (const views::View* vv = v; vv; vv = vv->parent())
LOG(ERROR) << "V: " << vv << " " << vv->GetVisible() << " "
<< vv->IsDrawn() << " " << vv->layer();
for (const ui::Layer* ll = l; ll; ll = ll->parent())
LOG(ERROR) << "L: " << ll << " " << ll->IsDrawn();
return false;
}
// Check if the size of the View and the Layer are in sync.
EXPECT_EQ(l->bounds(), v->bounds());
if (v->bounds() != l->bounds())
return false;
if (v == view || l == layer)
return v == view && l == layer;
v = NextView(v);
l = NextLayer(l);
}
return false;
}
// Constructs a View tree with the specified depth.
void ConstructTree(views::View* view, int depth) {
if (depth == 0)
return;
int count = base::RandInt(1, 5);
for (int i = 0; i < count; i++) {
views::View* v = new views::View;
view->AddChildView(v);
if (base::RandDouble() > 0.5)
v->SetPaintToLayer();
if (base::RandDouble() < 0.2)
v->SetVisible(false);
ConstructTree(v, depth - 1);
}
}
void ScrambleTree(views::View* view) {
if (view->children().empty())
return;
for (views::View* child : view->children())
ScrambleTree(child);
size_t count = view->children().size();
if (count > 1) {
const uint64_t max = count - 1;
size_t a = size_t{base::RandGenerator(max)};
size_t b = size_t{base::RandGenerator(max)};
if (a != b) {
views::View* view_a = view->children()[a];
views::View* view_b = view->children()[b];
view->ReorderChildView(view_a, b);
view->ReorderChildView(view_b, a);
}
}
if (!view->layer() && base::RandDouble() < 0.1)
view->SetPaintToLayer();
if (base::RandDouble() < 0.1)
view->SetVisible(!view->GetVisible());
}
} // namespace
namespace views {
using ViewTest = ViewsTestBase;
// A derived class for testing purpose.
class TestView : public View {
public:
TestView() = default;
~TestView() override = default;
// Reset all test state
void Reset() {
did_change_bounds_ = false;
did_layout_ = false;
last_mouse_event_type_ = 0;
location_.SetPoint(0, 0);
received_mouse_enter_ = false;
received_mouse_exit_ = false;
did_paint_ = false;
accelerator_count_map_.clear();
can_process_events_within_subtree_ = true;
}
// Exposed as public for testing.
void DoFocus() {
views::View::Focus();
}
void DoBlur() {
views::View::Blur();
}
void set_can_process_events_within_subtree(bool can_process) {
can_process_events_within_subtree_ = can_process;
}
bool CanProcessEventsWithinSubtree() const override {
return can_process_events_within_subtree_;
}
void Layout() override {
did_layout_ = true;
View::Layout();
}
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
bool OnMousePressed(const ui::MouseEvent& event) override;
bool OnMouseDragged(const ui::MouseEvent& event) override;
void OnMouseReleased(const ui::MouseEvent& event) override;
void OnMouseEntered(const ui::MouseEvent& event) override;
void OnMouseExited(const ui::MouseEvent& event) override;
void OnPaint(gfx::Canvas* canvas) override;
void SchedulePaintInRect(const gfx::Rect& rect) override;
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
void OnThemeChanged() override;
void OnAccessibilityEvent(ax::mojom::Event event_type) override;
// OnBoundsChanged.
bool did_change_bounds_;
gfx::Rect new_bounds_;
// Layout.
bool did_layout_ = false;
// MouseEvent.
int last_mouse_event_type_;
gfx::Point location_;
bool received_mouse_enter_;
bool received_mouse_exit_;
bool delete_on_pressed_ = false;
// Painting.
std::vector<gfx::Rect> scheduled_paint_rects_;
bool did_paint_ = false;
// Accelerators.
std::map<ui::Accelerator, int> accelerator_count_map_;
// Native theme.
const ui::NativeTheme* native_theme_ = nullptr;
// Value to return from CanProcessEventsWithinSubtree().
bool can_process_events_within_subtree_ = true;
// Accessibility events
ax::mojom::Event last_a11y_event_;
};
////////////////////////////////////////////////////////////////////////////////
// Layout
////////////////////////////////////////////////////////////////////////////////
TEST_F(ViewTest, LayoutCalledInvalidateAndOriginChanges) {
TestView parent;
TestView* child = new TestView;
gfx::Rect parent_rect(0, 0, 100, 100);
parent.SetBoundsRect(parent_rect);
parent.Reset();
// |AddChildView| invalidates parent's layout.
parent.AddChildView(child);
// Change rect so that only rect's origin is affected.
parent.SetBoundsRect(parent_rect + gfx::Vector2d(10, 0));
EXPECT_TRUE(parent.did_layout_);
// After child layout is invalidated, parent and child must be laid out
// during parent->BoundsChanged(...) call.
parent.Reset();
child->Reset();
child->InvalidateLayout();
parent.SetBoundsRect(parent_rect + gfx::Vector2d(20, 0));
EXPECT_TRUE(parent.did_layout_);
EXPECT_TRUE(child->did_layout_);
}
// Tests that SizeToPreferredSize will trigger a Layout if the size has changed
// or if layout is marked invalid.
TEST_F(ViewTest, SizeToPreferredSizeInducesLayout) {
TestView example_view;
example_view.SetPreferredSize(gfx::Size(101, 102));
example_view.SizeToPreferredSize();
EXPECT_TRUE(example_view.did_layout_);
example_view.Reset();
example_view.SizeToPreferredSize();
EXPECT_FALSE(example_view.did_layout_);
example_view.InvalidateLayout();
example_view.SizeToPreferredSize();
EXPECT_TRUE(example_view.did_layout_);
}
////////////////////////////////////////////////////////////////////////////////
// OnBoundsChanged
////////////////////////////////////////////////////////////////////////////////
void TestView::OnAccessibilityEvent(ax::mojom::Event event_type) {
last_a11y_event_ = event_type;
}
TEST_F(ViewTest, OnBoundsChangedFiresA11yEvent) {
TestView v;
// Should change when scaled or moved.
gfx::Rect initial(0, 0, 200, 200);
gfx::Rect scaled(0, 0, 250, 250);
gfx::Rect moved(100, 100, 250, 250);
v.last_a11y_event_ = ax::mojom::Event::kNone;
v.SetBoundsRect(initial);
EXPECT_EQ(v.last_a11y_event_, ax::mojom::Event::kLocationChanged);
v.last_a11y_event_ = ax::mojom::Event::kNone;
v.SetBoundsRect(scaled);
EXPECT_EQ(v.last_a11y_event_, ax::mojom::Event::kLocationChanged);
v.last_a11y_event_ = ax::mojom::Event::kNone;
v.SetBoundsRect(moved);
EXPECT_EQ(v.last_a11y_event_, ax::mojom::Event::kLocationChanged);
}
void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
did_change_bounds_ = true;
new_bounds_ = bounds();
}
TEST_F(ViewTest, OnBoundsChanged) {
TestView v;
gfx::Rect prev_rect(0, 0, 200, 200);
gfx::Rect new_rect(100, 100, 250, 250);
v.SetBoundsRect(prev_rect);
v.Reset();
v.SetBoundsRect(new_rect);
EXPECT_TRUE(v.did_change_bounds_);
EXPECT_EQ(v.new_bounds_, new_rect);
EXPECT_EQ(v.bounds(), new_rect);
}
////////////////////////////////////////////////////////////////////////////////
// MouseEvent
////////////////////////////////////////////////////////////////////////////////
bool TestView::OnMousePressed(const ui::MouseEvent& event) {
last_mouse_event_type_ = event.type();
location_.SetPoint(event.x(), event.y());
if (delete_on_pressed_)
delete this;
return true;
}
bool TestView::OnMouseDragged(const ui::MouseEvent& event) {
last_mouse_event_type_ = event.type();
location_.SetPoint(event.x(), event.y());
return true;
}
void TestView::OnMouseReleased(const ui::MouseEvent& event) {
last_mouse_event_type_ = event.type();
location_.SetPoint(event.x(), event.y());
}
void TestView::OnMouseEntered(const ui::MouseEvent& event) {
received_mouse_enter_ = true;
}
void TestView::OnMouseExited(const ui::MouseEvent& event) {
received_mouse_exit_ = true;
}
TEST_F(ViewTest, MouseEvent) {
TestView* v1 = new TestView();
v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
TestView* v2 = new TestView();
v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
std::unique_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(50, 50, 650, 650);
widget->Init(params);
internal::RootView* root =
static_cast<internal::RootView*>(widget->GetRootView());
root->AddChildView(v1);
v1->AddChildView(v2);
v1->Reset();
v2->Reset();
gfx::Point p1(110, 120);
ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
root->OnMousePressed(pressed);
EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_PRESSED);
EXPECT_EQ(v2->location_.x(), 10);
EXPECT_EQ(v2->location_.y(), 20);
// Make sure v1 did not receive the event
EXPECT_EQ(v1->last_mouse_event_type_, 0);
// Drag event out of bounds. Should still go to v2
v1->Reset();
v2->Reset();
gfx::Point p2(50, 40);
ui::MouseEvent dragged(ui::ET_MOUSE_DRAGGED, p2, p2, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0);
root->OnMouseDragged(dragged);
EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_DRAGGED);
EXPECT_EQ(v2->location_.x(), -50);
EXPECT_EQ(v2->location_.y(), -60);
// Make sure v1 did not receive the event
EXPECT_EQ(v1->last_mouse_event_type_, 0);
// Releasted event out of bounds. Should still go to v2
v1->Reset();
v2->Reset();
ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), 0, 0);
root->OnMouseDragged(released);
EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_RELEASED);
EXPECT_EQ(v2->location_.x(), -100);
EXPECT_EQ(v2->location_.y(), -100);
// Make sure v1 did not receive the event
EXPECT_EQ(v1->last_mouse_event_type_, 0);
widget->CloseNow();
}
// Confirm that a view can be deleted as part of processing a mouse press.
TEST_F(ViewTest, DeleteOnPressed) {
TestView* v1 = new TestView();
v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
TestView* v2 = new TestView();
v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
v1->Reset();
v2->Reset();
std::unique_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(50, 50, 650, 650);
widget->Init(params);
View* root = widget->GetRootView();
root->AddChildView(v1);
v1->AddChildView(v2);
v2->delete_on_pressed_ = true;
gfx::Point point(110, 120);
ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, point, point,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
root->OnMousePressed(pressed);
EXPECT_TRUE(v1->children().empty());
widget->CloseNow();
}
////////////////////////////////////////////////////////////////////////////////
// Painting
////////////////////////////////////////////////////////////////////////////////
void TestView::OnPaint(gfx::Canvas* canvas) {
did_paint_ = true;
}
namespace {
// Helper class to create a Widget with standard parameters that is closed when
// the helper class goes out of scope.
class ScopedTestPaintWidget {
public:
explicit ScopedTestPaintWidget(const Widget::InitParams& params)
: widget_(new Widget) {
widget_->Init(params);
widget_->GetRootView()->SetBounds(0, 0, 25, 26);
}
~ScopedTestPaintWidget() {
widget_->CloseNow();
}
Widget* operator->() { return widget_; }
private:
Widget* widget_;
DISALLOW_COPY_AND_ASSIGN(ScopedTestPaintWidget);
};
} // namespace
TEST_F(ViewTest, PaintEmptyView) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
// |v1| is empty.
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 0, 1);
root_view->AddChildView(v1);
// |v11| is a child of an empty |v1|.
TestView* v11 = new TestView;
v11->SetBounds(3, 4, 6, 5);
v1->AddChildView(v11);
// |v2| is not.
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
root_view->AddChildView(v2);
// Paint "everything".
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
first_paint.size()));
// The empty view has nothing to paint so it doesn't try build a cache, nor do
// its children which would be clipped by its (empty) self.
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v11->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
TEST_F(ViewTest, PaintWithMovedViewUsesCache) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect pixel_rect = gfx::Rect(1, 1);
float device_scale_factor = 1.f;
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false));
EXPECT_TRUE(v1->did_paint_);
v1->Reset();
// The visual rects for (clip, drawing, transform) should be in layer space.
gfx::Rect expected_visual_rect_in_layer_space(10, 11, 12, 13);
int item_index = 3;
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index));
// If invalidation doesn't intersect v1, we paint with the cache.
list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false));
EXPECT_FALSE(v1->did_paint_);
v1->Reset();
// If invalidation does intersect v1, we don't paint with the cache.
list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, v1->bounds(), false));
EXPECT_TRUE(v1->did_paint_);
v1->Reset();
// Moving the view should still use the cache when the invalidation doesn't
// intersect v1.
list = base::MakeRefCounted<cc::DisplayItemList>();
v1->SetX(9);
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false));
EXPECT_FALSE(v1->did_paint_);
v1->Reset();
item_index = 3;
expected_visual_rect_in_layer_space.SetRect(9, 11, 12, 13);
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index));
// Moving the view should not use the cache when painting without
// invalidation.
list = base::MakeRefCounted<cc::DisplayItemList>();
v1->SetX(8);
root_view->PaintFromPaintRoot(ui::PaintContext(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false),
ui::PaintContext::CLONE_WITHOUT_INVALIDATION));
EXPECT_TRUE(v1->did_paint_);
v1->Reset();
item_index = 3;
expected_visual_rect_in_layer_space.SetRect(8, 11, 12, 13);
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index));
}
TEST_F(ViewTest, PaintWithMovedViewUsesCacheInRTL) {
base::test::ScopedRestoreICUDefaultLocale scoped_locale_("he");
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect pixel_rect = gfx::Rect(1, 1);
float device_scale_factor = 1.f;
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false));
EXPECT_TRUE(v1->did_paint_);
v1->Reset();
// The visual rects for (clip, drawing, transform) should be in layer space.
// x: 25 - 10(x) - 12(width) = 3
gfx::Rect expected_visual_rect_in_layer_space(3, 11, 12, 13);
int item_index = 3;
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index));
// If invalidation doesn't intersect v1, we paint with the cache.
list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false));
EXPECT_FALSE(v1->did_paint_);
v1->Reset();
// If invalidation does intersect v1, we don't paint with the cache.
list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, v1->bounds(), false));
EXPECT_TRUE(v1->did_paint_);
v1->Reset();
// Moving the view should still use the cache when the invalidation doesn't
// intersect v1.
list = base::MakeRefCounted<cc::DisplayItemList>();
v1->SetX(9);
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false));
EXPECT_FALSE(v1->did_paint_);
v1->Reset();
item_index = 3;
// x: 25 - 9(x) - 12(width) = 4
expected_visual_rect_in_layer_space.SetRect(4, 11, 12, 13);
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index));
// Moving the view should not use the cache when painting without
// invalidation.
list = base::MakeRefCounted<cc::DisplayItemList>();
v1->SetX(8);
root_view->PaintFromPaintRoot(ui::PaintContext(
ui::PaintContext(list.get(), device_scale_factor, pixel_rect, false),
ui::PaintContext::CLONE_WITHOUT_INVALIDATION));
EXPECT_TRUE(v1->did_paint_);
v1->Reset();
item_index = 3;
// x: 25 - 8(x) - 12(width) = 5
expected_visual_rect_in_layer_space.SetRect(5, 11, 12, 13);
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index++));
EXPECT_EQ(expected_visual_rect_in_layer_space,
list->VisualRectForTesting(item_index));
}
TEST_F(ViewTest, PaintWithUnknownInvalidation) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), 1.f, first_paint, false));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(1, 1);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
// With a known invalidation, v1 and v2 are not painted.
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->PaintFromPaintRoot(
ui::PaintContext(list.get(), 1.f, paint_area, false));
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
// With unknown invalidation, v1 and v2 are painted.
root_view->PaintFromPaintRoot(
ui::PaintContext(ui::PaintContext(list.get(), 1.f, paint_area, false),
ui::PaintContext::CLONE_WITHOUT_INVALIDATION));
EXPECT_TRUE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
TEST_F(ViewTest, PaintContainsChildren) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(25, 26);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
TEST_F(ViewTest, PaintContainsChildrenInRTL) {
base::test::ScopedRestoreICUDefaultLocale scoped_locale_("he");
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Verify where the layers actually appear.
v1->SetPaintToLayer();
// x: 25 - 10(x) - 12(width) = 3
EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
v1->DestroyLayer();
v2->SetPaintToLayer();
// x: 25 - 10(parent x) - 3(x) - 6(width) = 6
EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
v2->DestroyLayer();
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(25, 26);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsChildren) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(9, 10, 5, 6);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsChildrenInRTL) {
base::test::ScopedRestoreICUDefaultLocale scoped_locale_("he");
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Verify where the layers actually appear.
v1->SetPaintToLayer();
// x: 25 - 10(x) - 12(width) = 3
EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
v1->DestroyLayer();
v2->SetPaintToLayer();
// x: 25 - 10(parent x) - 3(x) - 6(width) = 6
EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
v2->DestroyLayer();
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(2, 10, 5, 6);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsChildButNotGrandChild) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(9, 10, 2, 3);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsChildButNotGrandChildInRTL) {
base::test::ScopedRestoreICUDefaultLocale scoped_locale_("he");
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Verify where the layers actually appear.
v1->SetPaintToLayer();
// x: 25 - 10(x) - 12(width) = 3
EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
v1->DestroyLayer();
v2->SetPaintToLayer();
// x: 25 - 10(parent x) - 3(x) - 6(width) = 6
EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
v2->DestroyLayer();
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(2, 10, 2, 3);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsNoChildren) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(9, 10, 2, 1);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsNoChildrenInRTL) {
base::test::ScopedRestoreICUDefaultLocale scoped_locale_("he");
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Verify where the layers actually appear.
v1->SetPaintToLayer();
// x: 25 - 10(x) - 12(width) = 3
EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
v1->DestroyLayer();
v2->SetPaintToLayer();
// x: 25 - 10(parent x) - 3(x) - 6(width) = 6
EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
v2->DestroyLayer();
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
gfx::Rect paint_area(2, 10, 2, 1);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsOneChild) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
root_view->AddChildView(v2);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
// Intersects with the second child only.
gfx::Rect paint_area(3, 3, 1, 2);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_FALSE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
// Intersects with the first child only.
paint_area = gfx::Rect(20, 10, 1, 2);
v1->Reset();
v2->Reset();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
}
TEST_F(ViewTest, PaintIntersectsOneChildInRTL) {
base::test::ScopedRestoreICUDefaultLocale scoped_locale_("he");
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
root_view->AddChildView(v2);
// Verify where the layers actually appear.
v1->SetPaintToLayer();
// x: 25 - 10(x) - 12(width) = 3
EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
v1->DestroyLayer();
v2->SetPaintToLayer();
// x: 25 - 3(x) - 6(width) = 16
EXPECT_EQ(gfx::Rect(16, 4, 6, 5), v2->layer()->bounds());
v2->DestroyLayer();
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false),
root_view->size()));
v1->Reset();
v2->Reset();
// Intersects with the first child only.
gfx::Rect paint_area(3, 10, 1, 2);
gfx::Rect root_area(root_view->size());
list = base::MakeRefCounted<cc::DisplayItemList>();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
// Intersects with the second child only.
paint_area = gfx::Rect(21, 3, 1, 2);
v1->Reset();
v2->Reset();
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), root_view->size()));
EXPECT_FALSE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
TEST_F(ViewTest, PaintInPromotedToLayer) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
TestView* v1 = new TestView;
v1->SetPaintToLayer();
v1->SetBounds(10, 11, 12, 13);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(3, 4, 6, 5);
v1->AddChildView(v2);
// Paint everything once, since it has to build its cache. Then we can test
// invalidation.
gfx::Rect first_paint(1, 1);
auto list = base::MakeRefCounted<cc::DisplayItemList>();
v1->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, first_paint, false), v1->size()));
v1->Reset();
v2->Reset();
{
gfx::Rect paint_area(25, 26);
gfx::Rect view_area(root_view->size());
auto list = base::MakeRefCounted<cc::DisplayItemList>();
// The promoted views are not painted as they are separate paint roots.
root_view->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false),
root_view->size()));
EXPECT_FALSE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
}
{
gfx::Rect paint_area(1, 1);
gfx::Rect view_area(v1->size());
auto list = base::MakeRefCounted<cc::DisplayItemList>();
// The |v1| view is painted. If it used its offset incorrect, it would think
// its at (10,11) instead of at (0,0) since it is the paint root.
v1->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), v1->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_FALSE(v2->did_paint_);
}
v1->Reset();
{
gfx::Rect paint_area(3, 3, 1, 2);
gfx::Rect view_area(v1->size());
auto list = base::MakeRefCounted<cc::DisplayItemList>();
// The |v2| view is painted also. If it used its offset incorrect, it would
// think its at (13,15) instead of at (3,4) since |v1| is the paint root.
v1->Paint(PaintInfo::CreateRootPaintInfo(
ui::PaintContext(list.get(), 1.f, paint_area, false), v1->size()));
EXPECT_TRUE(v1->did_paint_);
EXPECT_TRUE(v2->did_paint_);
}
}
// A derived class for testing paint.
class TestPaintView : public TestView {
public:
TestPaintView() : canvas_bounds_(gfx::Rect()) {}
~TestPaintView() override = default;
void OnPaint(gfx::Canvas* canvas) override {
did_paint_ = true;
// Get the bounds from the canvas this view paints to.
EXPECT_TRUE(canvas->GetClipBounds(&canvas_bounds_));
}
gfx::Rect canvas_bounds() const { return canvas_bounds_; }
private:
gfx::Rect canvas_bounds_;
};
TEST_F(ViewTest, PaintLocalBounds) {
ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
// Make |root_view|'s bounds larger so |v1|'s visible bounds is not clipped by
// |root_view|.
root_view->SetBounds(0, 0, 200, 200);
TestPaintView* v1 = new TestPaintView;
v1->SetPaintToLayer();
// Set bounds for |v1| such that it has an offset to its parent and only part
// of it is visible. The visible bounds does not intersect with |root_view|'s
// bounds.
v1->SetBounds(0, -1000, 100, 1100);
root_view->AddChildView(v1);
EXPECT_EQ(gfx::Rect(0, 0, 100, 1100), v1->GetLocalBounds());
EXPECT_EQ(gfx::Rect(0, 1000, 100, 100), v1->GetVisibleBounds());
auto list = base::MakeRefCounted<cc::DisplayItemList>();
ui::PaintContext context(list.get(), 1.f, gfx::Rect(), false);
v1->Paint(PaintInfo::CreateRootPaintInfo(context, gfx::Size()));
EXPECT_TRUE(v1->did_paint_);
// Check that the canvas produced by |v1| for paint contains all of |v1|'s
// visible bounds.
EXPECT_TRUE(v1->canvas_bounds().Contains(v1->GetVisibleBounds()));
}
void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
scheduled_paint_rects_.push_back(rect);
View::SchedulePaintInRect(rect);
}
namespace {
void RotateCounterclockwise(gfx::Transform* transform) {
transform->matrix().set3x3(0, -1, 0,
1, 0, 0,
0, 0, 1);
}
void RotateClockwise(gfx::Transform* transform) {
transform->matrix().set3x3( 0, 1, 0, // NOLINT
-1, 0, 0,
0, 0, 1);
}
} // namespace
// Tests the correctness of the rect-based targeting algorithm implemented in
// View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
// of rect-based targeting.
TEST_F(ViewTest, GetEventHandlerForRect) {
Widget* widget = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
widget->Init(params);
View* root_view = widget->GetRootView();
root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
// Have this hierarchy of views (the coordinates here are all in
// the root view's coordinate space):
// v1 (0, 0, 100, 100)
// v2 (150, 0, 250, 100)
// v3 (0, 200, 150, 100)
// v31 (10, 210, 80, 80)
// v32 (110, 210, 30, 80)
// v4 (300, 200, 100, 100)
// v41 (310, 210, 80, 80)
// v411 (370, 275, 10, 5)
// v5 (450, 197, 30, 36)
// v51 (450, 200, 30, 30)
// The coordinates used for SetBounds are in parent coordinates.
TestView* v1 = new TestView;
v1->SetBounds(0, 0, 100, 100);
root_view->AddChildView(v1);
TestView* v2 = new TestView;
v2->SetBounds(150, 0, 250, 100);
root_view->AddChildView(v2);
TestView* v3 = new TestView;
v3->SetBounds(0, 200, 150, 100);
root_view->AddChildView(v3);
TestView* v4 = new TestView;
v4->SetBounds(300, 200, 100, 100);
root_view->AddChildView(v4);
TestView* v31 = new TestView;
v31->SetBounds(10, 10, 80, 80);
v3->AddChildView(v31);
TestView* v32 = new TestView;
v32->SetBounds(110, 10, 30, 80);
v3->AddChildView(v32);
TestView* v41 = new TestView;
v41->SetBounds(10, 10, 80, 80);
v4->AddChildView(v41);
TestView* v411 = new TestView;
v411->SetBounds(60, 65, 10, 5);
v41->AddChildView(v411);
TestView* v5 = new TestView;
v5->SetBounds(450, 197, 30, 36);
root_view->AddChildView(v5);
TestView* v51 = new TestView;
v51->SetBounds(0, 3, 30, 30);
v5->AddChildView(v51);
// |touch_rect| does not intersect any descendant view of |root_view|.
gfx::Rect touch_rect(105, 105, 30, 45);
View* result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
// Covers |v1| by at least 60%.
touch_rect.SetRect(15, 15, 100, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = nullptr;
// Intersects |v1| but does not cover it by at least 60%. The center
// of |touch_rect| is within |v1|.
touch_rect.SetRect(50, 50, 5, 10);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = nullptr;
// Intersects |v1| but does not cover it by at least 60%. The center
// of |touch_rect| is not within |v1|.
touch_rect.SetRect(95, 96, 21, 22);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
// Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
touch_rect.SetRect(95, 10, 300, 120);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v2, result_view);
result_view = nullptr;
// Covers both |v1| and |v2| by at least 60%, but the center point
// of |touch_rect| is closer to the center point of |v2|.
touch_rect.SetRect(20, 20, 400, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v2, result_view);
result_view = nullptr;
// Covers both |v1| and |v2| by at least 60%, but the center point
// of |touch_rect| is closer to the center point of |v1|.
touch_rect.SetRect(-700, -15, 1050, 110);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = nullptr;
// A mouse click within |v1| will target |v1|.
touch_rect.SetRect(15, 15, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = nullptr;
// Intersects |v3| and |v31| by at least 60% and the center point
// of |touch_rect| is closer to the center point of |v31|.
touch_rect.SetRect(0, 200, 110, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v31, result_view);
result_view = nullptr;
// Intersects |v3| and |v31|, but neither by at least 60%. The
// center point of |touch_rect| lies within |v31|.
touch_rect.SetRect(80, 280, 15, 15);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v31, result_view);
result_view = nullptr;
// Covers |v3|, |v31|, and |v32| all by at least 60%, and the
// center point of |touch_rect| is closest to the center point
// of |v32|.
touch_rect.SetRect(0, 200, 200, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = nullptr;
// Intersects all of |v3|, |v31|, and |v32|, but only covers
// |v31| and |v32| by at least 60%. The center point of
// |touch_rect| is closest to the center point of |v32|.
touch_rect.SetRect(30, 225, 180, 115);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = nullptr;
// A mouse click at the corner of |v3| will target |v3|.
touch_rect.SetRect(0, 200, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v3, result_view);
result_view = nullptr;
// A mouse click within |v32| will target |v32|.
touch_rect.SetRect(112, 211, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = nullptr;
// Covers all of |v4|, |v41|, and |v411| by at least 60%.
// The center point of |touch_rect| is equally close to
// the center points of |v4| and |v41|.
touch_rect.SetRect(310, 210, 80, 80);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = nullptr;
// Intersects all of |v4|, |v41|, and |v411| but only covers
// |v411| by at least 60%.
touch_rect.SetRect(370, 275, 7, 5);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v411, result_view);
result_view = nullptr;
// Intersects |v4| and |v41| but covers neither by at least 60%.
// The center point of |touch_rect| is equally close to the center
// points of |v4| and |v41|.
touch_rect.SetRect(345, 245, 7, 7);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = nullptr;
// Intersects all of |v4|, |v41|, and |v411| and covers none of
// them by at least 60%. The center point of |touch_rect| lies
// within |v411|.
touch_rect.SetRect(368, 272, 4, 6);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v411, result_view);
result_view = nullptr;
// Intersects all of |v4|, |v41|, and |v411| and covers none of
// them by at least 60%. The center point of |touch_rect| lies
// within |v41|.
touch_rect.SetRect(365, 270, 7, 7);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = nullptr;
// Intersects all of |v4|, |v41|, and |v411| and covers none of
// them by at least 60%. The center point of |touch_rect| lies
// within |v4|.
touch_rect.SetRect(205, 275, 200, 2);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v4, result_view);
result_view = nullptr;
// Intersects all of |v4|, |v41|, and |v411| but only covers
// |v41| by at least 60%.
touch_rect.SetRect(310, 210, 61, 66);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = nullptr;
// A mouse click within |v411| will target |v411|.
touch_rect.SetRect(372, 275, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v411, result_view);
result_view = nullptr;
// A mouse click within |v41| will target |v41|.
touch_rect.SetRect(350, 215, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = nullptr;
// Covers |v3|, |v4|, and all of their descendants by at
// least 60%. The center point of |touch_rect| is closest
// to the center point of |v32|.
touch_rect.SetRect(0, 200, 400, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = nullptr;
// Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
// Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
// The center point of |touch_rect| is closest to the center
// point of |root_view|.
touch_rect.SetRect(110, 15, 375, 450);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
// Covers all views (except |v5| and |v51|) by at least 60%. The
// center point of |touch_rect| is equally close to the center
// points of |v2| and |v32|. One is not a descendant of the other,
// so in this case the view selected is arbitrary (i.e.,
// it depends only on the ordering of nodes in the views
// hierarchy).
touch_rect.SetRect(0, 0, 400, 300);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = nullptr;
// Covers |v5| and |v51| by at least 60%, and the center point of
// the touch is located within both views. Since both views share
// the same center point, the child view should be selected.
touch_rect.SetRect(440, 190, 40, 40);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = nullptr;
// Covers |v5| and |v51| by at least 60%, but the center point of
// the touch is not located within either view. Since both views
// share the same center point, the child view should be selected.
touch_rect.SetRect(455, 187, 60, 60);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = nullptr;
// Covers neither |v5| nor |v51| by at least 60%, but the center
// of the touch is located within |v51|.
touch_rect.SetRect(450, 197, 10, 10);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = nullptr;
// Covers neither |v5| nor |v51| by at least 60% but intersects both.
// The center point is located outside of both views.
touch_rect.SetRect(433, 180, 24, 24);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
// Only intersects |v5| but does not cover it by at least 60%. The
// center point of the touch region is located within |v5|.
touch_rect.SetRect(449, 196, 3, 3);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v5, result_view);
result_view = nullptr;
// A mouse click within |v5| (but not |v51|) should target |v5|.
touch_rect.SetRect(462, 199, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v5, result_view);
result_view = nullptr;
// A mouse click |v5| and |v51| should target the child view.
touch_rect.SetRect(452, 226, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = nullptr;
// A mouse click on the center of |v5| and |v51| should target
// the child view.
touch_rect.SetRect(465, 215, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = nullptr;
widget->CloseNow();
}
// Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
// as expected when different views in the view hierarchy return false
// when CanProcessEventsWithinSubtree() is called.
TEST_F(ViewTest, CanProcessEventsWithinSubtree) {
Widget* widget = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
widget->Init(params);
View* root_view = widget->GetRootView();
root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
// Have this hierarchy of views (the coords here are in the coordinate
// space of the root view):
// v (0, 0, 100, 100)
// - v_child (0, 0, 20, 30)
// - v_grandchild (5, 5, 5, 15)
TestView* v = new TestView;
v->SetBounds(0, 0, 100, 100);
root_view->AddChildView(v);
v->set_notify_enter_exit_on_child(true);
TestView* v_child = new TestView;
v_child->SetBounds(0, 0, 20, 30);
v->AddChildView(v_child);
TestView* v_grandchild = new TestView;
v_grandchild->SetBounds(5, 5, 5, 15);
v_child->AddChildView(v_grandchild);
v->Reset();
v_child->Reset();
v_grandchild->Reset();
// Define rects and points within the views in the hierarchy.
gfx::Rect rect_in_v_grandchild(7, 7, 3, 3);
gfx::Point point_in_v_grandchild(rect_in_v_grandchild.origin());
gfx::Rect rect_in_v_child(12, 3, 5, 5);
gfx::Point point_in_v_child(rect_in_v_child.origin());
gfx::Rect rect_in_v(50, 50, 25, 30);
gfx::Point point_in_v(rect_in_v.origin());
// When all three views return true when CanProcessEventsWithinSubtree()
// is called, targeting should behave as expected.
View* result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
EXPECT_EQ(v_grandchild, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
EXPECT_EQ(v_grandchild, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
EXPECT_EQ(v_child, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
EXPECT_EQ(v_child, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v);
EXPECT_EQ(v, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
EXPECT_EQ(v, result_view);
result_view = nullptr;
// When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
// is called, then |v_grandchild| cannot be returned as a target.
v_grandchild->set_can_process_events_within_subtree(false);
result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
EXPECT_EQ(v_child, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
EXPECT_EQ(v_child, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
EXPECT_EQ(v_child, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
EXPECT_EQ(v_child, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v);
EXPECT_EQ(v, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
EXPECT_EQ(v, result_view);
// When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
// is called, then NULL should be returned as a target if we call
// GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
// views tree. Note that the location must be in the coordinate space
// of the root view (|v_grandchild| in this case), so use (1, 1).
result_view = v_grandchild;
result_view = v_grandchild->GetTooltipHandlerForPoint(gfx::Point(1, 1));
EXPECT_EQ(nullptr, result_view);
result_view = nullptr;
// When |v_child| returns false when CanProcessEventsWithinSubtree()
// is called, then neither |v_child| nor |v_grandchild| can be returned
// as a target (|v| should be returned as the target for each case).
v_grandchild->Reset();
v_child->set_can_process_events_within_subtree(false);
result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
EXPECT_EQ(v, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
EXPECT_EQ(v, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
EXPECT_EQ(v, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
EXPECT_EQ(v, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v);
EXPECT_EQ(v, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
EXPECT_EQ(v, result_view);
result_view = nullptr;
// When |v| returns false when CanProcessEventsWithinSubtree()
// is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
// as a target (|root_view| should be returned as the target for each case).
v_child->Reset();
v->set_can_process_events_within_subtree(false);
result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
result_view = root_view->GetEventHandlerForRect(rect_in_v);
EXPECT_EQ(root_view, result_view);
result_view = nullptr;
result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
EXPECT_EQ(root_view, result_view);
widget->CloseNow();
}
TEST_F(ViewTest, NotifyEnterExitOnChild) {
Widget* widget = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
widget->Init(params);
View* root_view = widget->GetRootView();
root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
// Have this hierarchy of views (the coords here are in root coord):
// v1 (0, 0, 100, 100)
// - v11 (0, 0, 20, 30)
// - v111 (5, 5, 5, 15)
// - v12 (50, 10, 30, 90)
// - v121 (60, 20, 10, 10)
// v2 (105, 0, 100, 100)
// - v21 (120, 10, 50, 20)
TestView* v1 = new TestView;
v1->SetBounds(0, 0, 100, 100);
root_view->AddChildView(v1);
v1->set_notify_enter_exit_on_child(true);
TestView* v11 = new TestView;
v11->SetBounds(0, 0, 20, 30);
v1->AddChildView(v11);
TestView* v111 = new TestView;
v111->SetBounds(5, 5, 5, 15);
v11->AddChildView(v111);
TestView* v12 = new TestView;
v12->SetBounds(50, 10, 30, 90);
v1->AddChildView(v12);
TestView* v121 = new TestView;
v121->SetBounds(10, 10, 10, 10);
v12->AddChildView(v121);
TestView* v2 = new TestView;
v2->SetBounds(105, 0, 100, 100);
root_view->AddChildView(v2);
TestView* v21 = new TestView;
v21->SetBounds(15, 10, 50, 20);
v2->AddChildView(v21);
v1->Reset();
v11->Reset();
v111->Reset();
v12->Reset();
v121->Reset();
v2->Reset();
v21->Reset();
// Move the mouse in v111.
gfx::Point p1(6, 6);
ui::MouseEvent move1(ui::ET_MOUSE_MOVED, p1, p1, ui::EventTimeForNow(), 0, 0);
root_view->OnMouseMoved(move1);
EXPECT_TRUE(v111->received_mouse_enter_);
EXPECT_FALSE(v11->last_mouse_event_type_);
EXPECT_TRUE(v1->received_mouse_enter_);
v111->Reset();
v1->Reset();
// Now, move into v121.
gfx::Point p2(65, 21);
ui::MouseEvent move2(ui::ET_MOUSE_MOVED, p2, p2, ui::EventTimeForNow(), 0, 0);
root_view->OnMouseMoved(move2);
EXPECT_TRUE(v111->received_mouse_exit_);
EXPECT_TRUE(v121->received_mouse_enter_);
EXPECT_FALSE(v1->last_mouse_event_type_);
v111->Reset();
v121->Reset();
// Now, move into v11.
gfx::Point p3(1, 1);
ui::MouseEvent move3(ui::ET_MOUSE_MOVED, p3, p3, ui::EventTimeForNow(), 0, 0);
root_view->OnMouseMoved(move3);
EXPECT_TRUE(v121->received_mouse_exit_);
EXPECT_TRUE(v11->received_mouse_enter_);
EXPECT_FALSE(v1->last_mouse_event_type_);
v121->Reset();
v11->Reset();
// Move to v21.
gfx::Point p4(121, 15);
ui::MouseEvent move4(ui::ET_MOUSE_MOVED, p4, p4, ui::EventTimeForNow(), 0, 0);
root_view->OnMouseMoved(move4);
EXPECT_TRUE(v21->received_mouse_enter_);
EXPECT_FALSE(v2->last_mouse_event_type_);
EXPECT_TRUE(v11->received_mouse_exit_);
EXPECT_TRUE(v1->received_mouse_exit_);
v21->Reset();
v11->Reset();
v1->Reset();
// Move to v1.
gfx::Point p5(21, 0);
ui::MouseEvent move5(ui::ET_MOUSE_MOVED, p5, p5, ui::EventTimeForNow(), 0, 0);
root_view->OnMouseMoved(move5);
EXPECT_TRUE(v21->received_mouse_exit_);
EXPECT_TRUE(v1->received_mouse_enter_);
v21->Reset();
v1->Reset();
// Now, move into v11.
gfx::Point p6(15, 15);
ui::MouseEvent mouse6(ui::ET_MOUSE_MOVED, p6, p6, ui::EventTimeForNow(), 0,
0);
root_view->OnMouseMoved(mouse6);
EXPECT_TRUE(v11->received_mouse_enter_);
EXPECT_FALSE(v1->last_mouse_event_type_);
v11->Reset();
v1->Reset();
// Move back into v1. Although |v1| had already received an ENTER for mouse6,
// and the mouse remains inside |v1| the whole time, it receives another ENTER
// when the mouse leaves v11.
gfx::Point p7(21, 0);
ui::MouseEvent mouse7(ui::ET_MOUSE_MOVED, p7, p7, ui::EventTimeForNow(), 0,
0);
root_view->OnMouseMoved(mouse7);
EXPECT_TRUE(v11->received_mouse_exit_);
EXPECT_FALSE(v1->received_mouse_enter_);
widget->CloseNow();
}
TEST_F(ViewTest, Textfield) {
const base::string16 kText = ASCIIToUTF16(
"Reality is that which, when you stop believing it, doesn't go away.");
const base::string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!");
Widget* widget = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(0, 0, 100, 100);
widget->Init(params);
View* root_view = widget->GetRootView();
Textfield* textfield = new Textfield();
root_view->AddChildView(textfield);
// Test setting, appending text.
textfield->SetText(kText);
EXPECT_EQ(kText, textfield->text());
textfield->AppendText(kExtraText);
EXPECT_EQ(kText + kExtraText, textfield->text());
textfield->SetText(base::string16());
EXPECT_TRUE(textfield->text().empty());
// Test selection related methods.
textfield->SetText(kText);
EXPECT_TRUE(textfield->GetSelectedText().empty());
textfield->SelectAll(false);
EXPECT_EQ(kText, textfield->text());
textfield->ClearSelection();
EXPECT_TRUE(textfield->GetSelectedText().empty());
widget->CloseNow();
}
// Tests that the Textfield view respond appropiately to cut/copy/paste.
TEST_F(ViewTest, TextfieldCutCopyPaste) {
const base::string16 kNormalText = ASCIIToUTF16("Normal");
const base::string16 kReadOnlyText = ASCIIToUTF16("Read only");
const base::string16 kPasswordText =
ASCIIToUTF16("Password! ** Secret stuff **");
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
Widget* widget = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(0, 0, 100, 100);
widget->Init(params);
View* root_view = widget->GetRootView();
Textfield* normal = new Textfield();
Textfield* read_only = new Textfield();
read_only->SetReadOnly(true);
Textfield* password = new Textfield();
password->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
root_view->AddChildView(normal);
root_view->AddChildView(read_only);
root_view->AddChildView(password);
normal->SetText(kNormalText);
read_only->SetText(kReadOnlyText);
password->SetText(kPasswordText);
//
// Test cut.
//
normal->SelectAll(false);
normal->ExecuteCommand(IDS_APP_CUT, 0);
base::string16 result;
clipboard->ReadText(ui::ClipboardType::kCopyPaste, &result);
EXPECT_EQ(kNormalText, result);
normal->SetText(kNormalText); // Let's revert to the original content.
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_CUT, 0);
result.clear();
clipboard->ReadText(ui::ClipboardType::kCopyPaste, &result);
// Cut should have failed, so the clipboard content should not have changed.
EXPECT_EQ(kNormalText, result);
password->SelectAll(false);
password->ExecuteCommand(IDS_APP_CUT, 0);
result.clear();
clipboard->ReadText(ui::ClipboardType::kCopyPaste, &result);
// Cut should have failed, so the clipboard content should not have changed.
EXPECT_EQ(kNormalText, result);
//
// Test copy.
//
// Start with |read_only| to observe a change in clipboard text.
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_COPY, 0);
result.clear();
clipboard->ReadText(ui::ClipboardType::kCopyPaste, &result);
EXPECT_EQ(kReadOnlyText, result);
normal->SelectAll(false);
normal->ExecuteCommand(IDS_APP_COPY, 0);
result.clear();
clipboard->ReadText(ui::ClipboardType::kCopyPaste, &result);
EXPECT_EQ(kNormalText, result);
password->SelectAll(false);
password->ExecuteCommand(IDS_APP_COPY, 0);
result.clear();
clipboard->ReadText(ui::ClipboardType::kCopyPaste, &result);
// Text cannot be copied from an obscured field; the clipboard won't change.
EXPECT_EQ(kNormalText, result);
//
// Test paste.
//
// Attempting to paste kNormalText in a read-only text-field should fail.
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_PASTE, 0);
EXPECT_EQ(kReadOnlyText, read_only->text());
password->SelectAll(false);
password->ExecuteCommand(IDS_APP_PASTE, 0);
EXPECT_EQ(kNormalText, password->text());
// Copy from |read_only| to observe a change in the normal textfield text.
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_COPY, 0);
normal->SelectAll(false);
normal->ExecuteCommand(IDS_APP_PASTE, 0);
EXPECT_EQ(kReadOnlyText, normal->text());
widget->CloseNow();
}
////////////////////////////////////////////////////////////////////////////////
// Accelerators
////////////////////////////////////////////////////////////////////////////////
bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) {
accelerator_count_map_[accelerator]++;
return true;
}
namespace {
// A Widget with a TestView in the view hierarchy. Used for accelerator tests.
class TestViewWidget {
public:
TestViewWidget(const Widget::InitParams& create_params,
ui::Accelerator* initial_accelerator,
bool show_after_init = true)
: view_(new TestView) {
view_->Reset();
// Register a keyboard accelerator before the view is added to a window.
if (initial_accelerator) {
view_->AddAccelerator(*initial_accelerator);
EXPECT_EQ(view_->accelerator_count_map_[*initial_accelerator], 0);
}
// Create a window and add the view as its child.
Widget::InitParams params = create_params;
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 100, 100);
widget_.Init(params);
View* root = widget_.GetRootView();
root->AddChildView(view_);
if (show_after_init)
widget_.Show();
EXPECT_TRUE(widget_.GetFocusManager());
}
TestView* view() { return view_; }
Widget* widget() { return &widget_; }
private:
TestView* view_;
Widget widget_;
DISALLOW_COPY_AND_ASSIGN(TestViewWidget);
};
} // namespace
// On non-ChromeOS aura there is extra logic to determine whether a view should
// handle accelerators or not (see View::CanHandleAccelerators for details).
// This test targets that extra logic, but should also work on other platforms.
TEST_F(ViewTest, HandleAccelerator) {
ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP),
&return_accelerator);
TestView* view = test_widget.view();
Widget* widget = test_widget.widget();
FocusManager* focus_manager = widget->GetFocusManager();
#if defined(USE_AURA) && !defined(OS_CHROMEOS)
// When a non-child view is not active, it shouldn't handle accelerators.
EXPECT_FALSE(widget->IsActive());
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
#endif
// TYPE_POPUP widgets default to non-activatable, so the Show() above wouldn't
// have activated the Widget. First, allow activation.
widget->widget_delegate()->SetCanActivate(true);
// When a non-child view is active, it should handle accelerators.
view->accelerator_count_map_[return_accelerator] = 0;
widget->Activate();
EXPECT_TRUE(widget->IsActive());
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
// Add a child view associated with a child widget.
TestView* child_view = new TestView();
child_view->Reset();
child_view->AddAccelerator(return_accelerator);
EXPECT_EQ(child_view->accelerator_count_map_[return_accelerator], 0);
view->AddChildView(child_view);
Widget* child_widget = new Widget;
Widget::InitParams child_params =
CreateParams(Widget::InitParams::TYPE_CONTROL);
child_params.parent = widget->GetNativeView();
child_widget->Init(child_params);
child_widget->SetContentsView(child_view);
FocusManager* child_focus_manager = child_widget->GetFocusManager();
ASSERT_TRUE(child_focus_manager);
// When a child view is in focus, it should handle accelerators.
child_view->accelerator_count_map_[return_accelerator] = 0;
view->accelerator_count_map_[return_accelerator] = 0;
child_focus_manager->SetFocusedView(child_view);
EXPECT_FALSE(child_view->GetWidget()->IsActive());
EXPECT_TRUE(child_focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(1, child_view->accelerator_count_map_[return_accelerator]);
EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
#if defined(USE_AURA) && !defined(OS_CHROMEOS)
// When a child view is not in focus, its parent should handle accelerators.
child_view->accelerator_count_map_[return_accelerator] = 0;
view->accelerator_count_map_[return_accelerator] = 0;
child_focus_manager->ClearFocus();
EXPECT_FALSE(child_view->GetWidget()->IsActive());
EXPECT_TRUE(child_focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(0, child_view->accelerator_count_map_[return_accelerator]);
EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
#endif
}
// TODO(themblsha): Bring this up on non-Mac platforms. It currently fails
// because TestView::AcceleratorPressed() is not called. See
// http://crbug.com/667757.
#if defined(OS_MACOSX)
// Test that BridgedContentView correctly handles Accelerator key events when
// subject to OS event dispatch.
TEST_F(ViewTest, ActivateAcceleratorOnMac) {
// Cmd+1 translates to "noop:" command by interpretKeyEvents.
ui::Accelerator command_accelerator(ui::VKEY_1, ui::EF_COMMAND_DOWN);
TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP),
&command_accelerator);
TestView* view = test_widget.view();
ui::test::EventGenerator event_generator(
test_widget.widget()->GetNativeWindow());
// Emulate normal event dispatch through -[NSWindow sendEvent:].
event_generator.set_target(ui::test::EventGenerator::Target::WINDOW);
event_generator.PressKey(command_accelerator.key_code(),
command_accelerator.modifiers());
event_generator.ReleaseKey(command_accelerator.key_code(),
command_accelerator.modifiers());
EXPECT_EQ(view->accelerator_count_map_[command_accelerator], 1);
// Without an _wantsKeyDownForEvent: override we'll only get a keyUp: event
// for this accelerator.
ui::Accelerator key_up_accelerator(ui::VKEY_TAB,
ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN);
view->AddAccelerator(key_up_accelerator);
event_generator.PressKey(key_up_accelerator.key_code(),
key_up_accelerator.modifiers());
event_generator.ReleaseKey(key_up_accelerator.key_code(),
key_up_accelerator.modifiers());
EXPECT_EQ(view->accelerator_count_map_[key_up_accelerator], 1);
// We should handle this accelerator inside keyDown: as it doesn't translate
// to any command by default.
ui::Accelerator key_down_accelerator(
ui::VKEY_L, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN);
view->AddAccelerator(key_down_accelerator);
event_generator.PressKey(key_down_accelerator.key_code(),
key_down_accelerator.modifiers());
event_generator.ReleaseKey(key_down_accelerator.key_code(),
key_down_accelerator.modifiers());
EXPECT_EQ(view->accelerator_count_map_[key_down_accelerator], 1);
}
#endif // OS_MACOSX
// TODO: these tests were initially commented out when getting aura to
// run. Figure out if still valuable and either nuke or fix. crbug.com/667757.
#if defined(OS_MACOSX)
TEST_F(ViewTest, ActivateAccelerator) {
ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP),
&return_accelerator);
TestView* view = test_widget.view();
FocusManager* focus_manager = test_widget.widget()->GetFocusManager();
// Hit the return key and see if it takes effect.
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
// Hit the escape key. Nothing should happen.
ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE);
EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0);
// Now register the escape key and hit it again.
view->AddAccelerator(escape_accelerator);
EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
// Remove the return key accelerator.
view->RemoveAccelerator(return_accelerator);
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
// Add it again. Hit the return key and the escape key.
view->AddAccelerator(return_accelerator);
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
// Remove all the accelerators.
view->ResetAccelerators();
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
}
TEST_F(ViewTest, HiddenViewWithAccelerator) {
ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP),
&return_accelerator);
TestView* view = test_widget.view();
FocusManager* focus_manager = test_widget.widget()->GetFocusManager();
view->SetVisible(false);
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
view->SetVisible(true);
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
}
TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) {
ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
TestViewWidget test_widget(CreateParams(Widget::InitParams::TYPE_POPUP),
&return_accelerator, false);
TestView* view = test_widget.view();
Widget* widget = test_widget.widget();
FocusManager* focus_manager = test_widget.widget()->GetFocusManager();
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
widget->Show();
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
widget->Hide();
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
}
#endif // OS_MACOSX
// TODO: these tests were initially commented out when getting aura to
// run. Figure out if still valuable and either nuke or fix.
#if 0
////////////////////////////////////////////////////////////////////////////////
// Mouse-wheel message rerouting
////////////////////////////////////////////////////////////////////////////////
class ScrollableTestView : public View {
public:
ScrollableTestView() { }
virtual gfx::Size GetPreferredSize() {
return gfx::Size(100, 10000);
}
virtual void Layout() {
SizeToPreferredSize();
}
};
class TestViewWithControls : public View {
public:
TestViewWithControls() {
text_field_ = new Textfield();
AddChildView(text_field_);
}
Textfield* text_field_;
};
class SimpleWidgetDelegate : public WidgetDelegate {
public:
explicit SimpleWidgetDelegate(View* contents) : contents_(contents) { }
virtual void DeleteDelegate() { delete this; }
virtual View* GetContentsView() { return contents_; }
virtual Widget* GetWidget() { return contents_->GetWidget(); }
virtual const Widget* GetWidget() const { return contents_->GetWidget(); }
private:
View* contents_;
};
// Tests that the mouse-wheel messages are correctly rerouted to the window
// under the mouse.
// TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
// bot.
// Note that this fails for a variety of reasons:
// - focused view is apparently reset across window activations and never
// properly restored
// - this test depends on you not having any other window visible open under the
// area that it opens the test windows. --beng
TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) {
TestViewWithControls* view_with_controls = new TestViewWithControls();
Widget* window1 = Widget::CreateWindowWithBounds(
new SimpleWidgetDelegate(view_with_controls),
gfx::Rect(0, 0, 100, 100));
window1->Show();
ScrollView* scroll_view = new ScrollView();
scroll_view->SetContents(new ScrollableTestView());
Widget* window2 = Widget::CreateWindowWithBounds(
new SimpleWidgetDelegate(scroll_view),
gfx::Rect(200, 200, 100, 100));
window2->Show();
EXPECT_EQ(0, scroll_view->GetVisibleRect().y());
// Make the window1 active, as this is what it would be in real-world.
window1->Activate();
// Let's send a mouse-wheel message to the different controls and check that
// it is rerouted to the window under the mouse (effectively scrolling the
// scroll-view).
// First to the Window's HWND.
::SendMessage(view_with_controls->GetWidget()->GetNativeView(),
WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
EXPECT_EQ(20, scroll_view->GetVisibleRect().y());
window1->CloseNow();
window2->CloseNow();
}
#endif // 0
////////////////////////////////////////////////////////////////////////////////
// Native view hierachy
////////////////////////////////////////////////////////////////////////////////
class ToplevelWidgetObserverView : public View {
public:
ToplevelWidgetObserverView() = default;
~ToplevelWidgetObserverView() override = default;
// View overrides:
void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) override {
if (details.is_add) {
toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : nullptr;
} else {
toplevel_ = nullptr;
}
}
void NativeViewHierarchyChanged() override {
toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : nullptr;
}
Widget* toplevel() { return toplevel_; }
private:
Widget* toplevel_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView);
};
// Test that a view can track the current top level widget by overriding
// View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
TEST_F(ViewTest, NativeViewHierarchyChanged) {
std::unique_ptr<Widget> toplevel1(new Widget);
Widget::InitParams toplevel1_params =
CreateParams(Widget::InitParams::TYPE_POPUP);
toplevel1_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
toplevel1->Init(toplevel1_params);
std::unique_ptr<Widget> toplevel2(new Widget);
Widget::InitParams toplevel2_params =
CreateParams(Widget::InitParams::TYPE_POPUP);
toplevel2_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
toplevel2->Init(toplevel2_params);
Widget* child = new Widget;
Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
child_params.parent = toplevel1->GetNativeView();
child->Init(child_params);
ToplevelWidgetObserverView* observer_view =
new ToplevelWidgetObserverView();
EXPECT_EQ(nullptr, observer_view->toplevel());
child->SetContentsView(observer_view);
EXPECT_EQ(toplevel1.get(), observer_view->toplevel());
Widget::ReparentNativeView(child->GetNativeView(),
toplevel2->GetNativeView());
EXPECT_EQ(toplevel2.get(), observer_view->toplevel());
observer_view->parent()->RemoveChildView(observer_view);
EXPECT_EQ(nullptr, observer_view->toplevel());
// Make |observer_view| |child|'s contents view again so that it gets deleted
// with the widget.
child->SetContentsView(observer_view);
}
////////////////////////////////////////////////////////////////////////////////
// Transformations
////////////////////////////////////////////////////////////////////////////////
class TransformPaintView : public TestView {
public:
TransformPaintView() = default;
~TransformPaintView() override = default;
void ClearScheduledPaintRect() {
scheduled_paint_rect_ = gfx::Rect();
}
gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
// Overridden from View:
void SchedulePaintInRect(const gfx::Rect& rect) override {
gfx::Rect xrect = ConvertRectToParent(rect);
scheduled_paint_rect_.Union(xrect);
}
private:
gfx::Rect scheduled_paint_rect_;
DISALLOW_COPY_AND_ASSIGN(TransformPaintView);
};
TEST_F(ViewTest, TransformPaint) {
TransformPaintView* v1 = new TransformPaintView();
v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
TestView* v2 = new TestView();
v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
Widget* widget = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(50, 50, 650, 650);
widget->Init(params);
widget->Show();
View* root = widget->GetRootView();
root->AddChildView(v1);
v1->AddChildView(v2);
// At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
v1->ClearScheduledPaintRect();
v2->SchedulePaint();
EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
// Rotate |v1| counter-clockwise.
gfx::Transform transform;
RotateCounterclockwise(&transform);
transform.matrix().set(1, 3, 500.0);
v1->SetTransform(transform);
// |v2| now occupies (100, 200) to (200, 400) in |root|.
v1->ClearScheduledPaintRect();
v2->SchedulePaint();
EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect());
widget->CloseNow();
}
TEST_F(ViewTest, TransformEvent) {
TestView* v1 = new TestView();
v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
TestView* v2 = new TestView();
v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
Widget* widget = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(50, 50, 650, 650);
widget->Init(params);
View* root = widget->GetRootView();
root->AddChildView(v1);
v1->AddChildView(v2);
// At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
// Rotate |v1| counter-clockwise.
gfx::Transform transform(v1->GetTransform());
RotateCounterclockwise(&transform);