This source file includes following definitions.
- LayerIsAncestor
- FirstView
- NextView
- FirstLayer
- NextLayer
- ViewAndLayerTreeAreConsistent
- ConstructTree
- ScrambleTree
- delete_on_pressed_
- Reset
- DoFocus
- DoBlur
- focusable
- OnGestureEvent
- OnGestureEvent
- OnGestureEvent
- OnBoundsChanged
- TEST_F
- OnMousePressed
- OnMouseDragged
- OnMouseReleased
- OnMouseEntered
- OnMouseExited
- TEST_F
- TEST_F
- OnGestureEvent
- TEST_F
- TEST_F
- Paint
- SchedulePaintInRect
- CheckRect
- TEST_F
- HasHitTestMask
- GetHitTestMask
- ConvertPointToView
- ConvertRectToView
- RotateCounterclockwise
- RotateClockwise
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- AcceleratorPressed
- TEST_F
- TEST_F
- TEST_F
- GetPreferredSize
- Layout
- DeleteDelegate
- GetContentsView
- GetWidget
- GetWidget
- TEST_F
- ViewHierarchyChanged
- NativeViewHierarchyChanged
- toplevel
- TEST_F
- ClearScheduledPaintRect
- scheduled_paint_rect
- SchedulePaintInRect
- TEST_F
- TEST_F
- TEST_F
- received_notification
- set_received_notification
- NeedsNotificationWhenVisibleBoundsChange
- OnVisibleBoundsChanged
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- has_add_details
- has_remove_details
- add_details
- remove_details
- has_remove_details_
- ResetTestState
- ViewHierarchyChanged
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- last_bounds
- SetBounds
- GetRootLayer
- SetUp
- TearDown
- widget
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- painted
- set_painted
- OnPaint
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- CreateControlWidget
- CreateViewWithLayer
- TEST_F
- TEST_F
#include <map>
#include "base/memory/scoped_ptr.h"
#include "base/rand_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/ui_strings.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.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/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/layer_tree_owner.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/gestures/gesture_recognizer.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/path.h"
#include "ui/gfx/transform.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/focus/view_storage.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/view.h"
#include "ui/views/view_constants_aura.h"
#include "ui/views/views_delegate.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"
#include "ui/wm/core/window_util.h"
using base::ASCIIToUTF16;
namespace {
bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) {
while (layer && layer != ancestor)
layer = layer->parent();
return layer == ancestor;
}
const views::View* FirstView(const views::View* view) {
const views::View* v = view;
while (v->has_children())
v = v->child_at(0);
return v;
}
const views::View* NextView(const views::View* view) {
const views::View* v = view;
const views::View* parent = v->parent();
if (!parent)
return NULL;
int next = parent->GetIndexOf(v) + 1;
if (next != parent->child_count())
return FirstView(parent->child_at(next));
return parent;
}
const ui::Layer* FirstLayer(const ui::Layer* layer) {
const ui::Layer* l = layer;
while (l->children().size() > 0)
l = l->children()[0];
return l;
}
const ui::Layer* NextLayer(const ui::Layer* layer) {
const ui::Layer* parent = layer->parent();
if (!parent)
return NULL;
const std::vector<ui::Layer*> children = parent->children();
size_t index;
for (index = 0; index < children.size(); index++) {
if (children[index] == layer)
break;
}
size_t next = index + 1;
if (next < children.size())
return FirstLayer(children[next]);
return parent;
}
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) {
while (v && !v->layer())
v = NextView(v);
EXPECT_TRUE(v);
if (!v)
return false;
EXPECT_EQ(l, v->layer());
if (v->layer() != l)
return false;
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->visible() << " "
<< vv->IsDrawn() << " " << vv->layer();
for (const ui::Layer* ll = l; ll; ll = ll->parent())
LOG(ERROR) << "L: " << ll << " " << ll->IsDrawn();
return false;
}
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;
}
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(true);
if (base::RandDouble() < 0.2)
v->SetVisible(false);
ConstructTree(v, depth - 1);
}
}
void ScrambleTree(views::View* view) {
int count = view->child_count();
if (count == 0)
return;
for (int i = 0; i < count; i++) {
ScrambleTree(view->child_at(i));
}
if (count > 1) {
int a = base::RandInt(0, count - 1);
int b = base::RandInt(0, count - 1);
views::View* view_a = view->child_at(a);
views::View* view_b = view->child_at(b);
view->ReorderChildView(view_a, b);
view->ReorderChildView(view_b, a);
}
if (!view->layer() && base::RandDouble() < 0.1)
view->SetPaintToLayer(true);
if (base::RandDouble() < 0.1)
view->SetVisible(!view->visible());
}
class GestureEventForTest : public ui::GestureEvent {
public:
GestureEventForTest(ui::EventType type, int x, int y, int flags)
: GestureEvent(type, x, y, flags, base::TimeDelta(),
ui::GestureEventDetails(type, 0.0f, 0.0f), 0) {
}
private:
DISALLOW_COPY_AND_ASSIGN(GestureEventForTest);
};
}
namespace views {
typedef ViewsTestBase ViewTest;
class TestView : public View {
public:
TestView() : View(), delete_on_pressed_(false) {}
virtual ~TestView() {}
void Reset() {
did_change_bounds_ = false;
last_mouse_event_type_ = 0;
location_.SetPoint(0, 0);
received_mouse_enter_ = false;
received_mouse_exit_ = false;
last_gesture_event_type_ = 0;
last_gesture_event_was_handled_ = false;
last_clip_.setEmpty();
accelerator_count_map_.clear();
}
void DoFocus() {
views::View::Focus();
}
void DoBlur() {
views::View::Blur();
}
bool focusable() const { return View::focusable(); }
virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
virtual void Paint(gfx::Canvas* canvas) OVERRIDE;
virtual void SchedulePaintInRect(const gfx::Rect& rect) OVERRIDE;
virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
bool did_change_bounds_;
gfx::Rect new_bounds_;
int last_mouse_event_type_;
gfx::Point location_;
bool received_mouse_enter_;
bool received_mouse_exit_;
bool delete_on_pressed_;
std::vector<gfx::Rect> scheduled_paint_rects_;
int last_gesture_event_type_;
bool last_gesture_event_was_handled_;
SkRect last_clip_;
std::map<ui::Accelerator, int> accelerator_count_map_;
};
class TestViewConsumeGesture : public TestView {
public:
TestViewConsumeGesture() : TestView() {}
virtual ~TestViewConsumeGesture() {}
protected:
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
last_gesture_event_type_ = event->type();
location_.SetPoint(event->x(), event->y());
event->StopPropagation();
}
private:
DISALLOW_COPY_AND_ASSIGN(TestViewConsumeGesture);
};
class TestViewIgnoreGesture: public TestView {
public:
TestViewIgnoreGesture() : TestView() {}
virtual ~TestViewIgnoreGesture() {}
private:
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
}
DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreGesture);
};
class TestViewIgnoreScrollGestures : public TestViewConsumeGesture {
public:
TestViewIgnoreScrollGestures() {}
virtual ~TestViewIgnoreScrollGestures() {}
private:
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
if (event->IsScrollGestureEvent())
return;
TestViewConsumeGesture::OnGestureEvent(event);
}
DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreScrollGestures);
};
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);
}
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));
scoped_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::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);
EXPECT_EQ(v1->last_mouse_event_type_, 0);
v1->Reset();
v2->Reset();
gfx::Point p2(50, 40);
ui::MouseEvent dragged(ui::ET_MOUSE_DRAGGED, p2, p2,
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);
EXPECT_EQ(v1->last_mouse_event_type_, 0);
v1->Reset();
v2->Reset();
ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), 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);
EXPECT_EQ(v1->last_mouse_event_type_, 0);
widget->CloseNow();
}
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();
scoped_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::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
root->OnMousePressed(pressed);
EXPECT_EQ(0, v1->child_count());
widget->CloseNow();
}
void TestView::OnGestureEvent(ui::GestureEvent* event) {
}
TEST_F(ViewTest, GestureEvent) {
TestView* v1 = new TestViewConsumeGesture();
v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
TestView* v2 = new TestViewConsumeGesture();
v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
TestView* v3 = new TestViewIgnoreGesture();
v3->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
scoped_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());
ui::EventDispatchDetails details;
root->AddChildView(v1);
v1->AddChildView(v2);
v2->AddChildView(v3);
v1->Reset();
v2->Reset();
v3->Reset();
GestureEventForTest g1(ui::ET_GESTURE_TAP, 110, 110, 0);
details = root->OnEventFromSource(&g1);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_TAP, v2->last_gesture_event_type_);
EXPECT_EQ(gfx::Point(10, 10), v2->location_);
EXPECT_EQ(ui::ET_UNKNOWN, v1->last_gesture_event_type_);
GestureEventForTest g1_up(ui::ET_GESTURE_END, 110, 110, 0);
details = root->OnEventFromSource(&g1_up);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
v1->Reset();
v2->Reset();
v3->Reset();
GestureEventForTest g2(ui::ET_GESTURE_TAP, 80, 80, 0);
details = root->OnEventFromSource(&g2);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_TAP, v1->last_gesture_event_type_);
EXPECT_EQ(gfx::Point(80, 80), v1->location_);
EXPECT_EQ(ui::ET_UNKNOWN, v2->last_gesture_event_type_);
v1->last_gesture_event_type_ = ui::ET_UNKNOWN;
v3->last_gesture_event_type_ = ui::ET_UNKNOWN;
details = root->OnEventFromSource(&g1);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_TAP, v1->last_gesture_event_type_);
EXPECT_EQ(ui::ET_UNKNOWN, v3->last_gesture_event_type_);
EXPECT_EQ("110,110", v1->location_.ToString());
widget->CloseNow();
}
TEST_F(ViewTest, ScrollGestureEvent) {
TestView* v1 = new TestViewConsumeGesture();
v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
TestView* v2 = new TestViewIgnoreScrollGestures();
v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
TestView* v3 = new TestViewIgnoreGesture();
v3->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
scoped_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());
ui::EventDispatchDetails details;
root->AddChildView(v1);
v1->AddChildView(v2);
v2->AddChildView(v3);
v1->Reset();
v2->Reset();
v3->Reset();
GestureEventForTest g1(ui::ET_GESTURE_TAP, 110, 110, 0);
details = root->OnEventFromSource(&g1);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_TAP, v2->last_gesture_event_type_);
EXPECT_EQ(gfx::Point(10, 10), v2->location_);
EXPECT_EQ(ui::ET_UNKNOWN, v1->last_gesture_event_type_);
v2->Reset();
GestureEventForTest gscroll_begin(ui::ET_GESTURE_SCROLL_BEGIN, 115, 115, 0);
details = root->OnEventFromSource(&gscroll_begin);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_UNKNOWN, v2->last_gesture_event_type_);
EXPECT_EQ(ui::ET_GESTURE_SCROLL_BEGIN, v1->last_gesture_event_type_);
v1->Reset();
GestureEventForTest second_tap(ui::ET_GESTURE_TAP, 70, 70, 0);
details = root->OnEventFromSource(&second_tap);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_TAP, v2->last_gesture_event_type_);
EXPECT_EQ(ui::ET_UNKNOWN, v1->last_gesture_event_type_);
v2->Reset();
GestureEventForTest gscroll_end(ui::ET_GESTURE_SCROLL_END, 50, 50, 0);
details = root->OnEventFromSource(&gscroll_end);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_SCROLL_END, v1->last_gesture_event_type_);
v1->Reset();
GestureEventForTest g1_up(ui::ET_GESTURE_END, 110, 110, 0);
details = root->OnEventFromSource(&g1_up);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_END, v2->last_gesture_event_type_);
v1->Reset();
v2->Reset();
v3->Reset();
GestureEventForTest g2(ui::ET_GESTURE_TAP, 80, 80, 0);
details = root->OnEventFromSource(&g2);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_TAP, v1->last_gesture_event_type_);
EXPECT_EQ(gfx::Point(80, 80), v1->location_);
EXPECT_EQ(ui::ET_UNKNOWN, v2->last_gesture_event_type_);
v1->last_gesture_event_type_ = ui::ET_UNKNOWN;
v3->last_gesture_event_type_ = ui::ET_UNKNOWN;
details = root->OnEventFromSource(&g1);
EXPECT_FALSE(details.dispatcher_destroyed);
EXPECT_FALSE(details.target_destroyed);
EXPECT_EQ(ui::ET_GESTURE_TAP, v1->last_gesture_event_type_);
EXPECT_EQ(ui::ET_UNKNOWN, v3->last_gesture_event_type_);
EXPECT_EQ("110,110", v1->location_.ToString());
widget->CloseNow();
}
void TestView::Paint(gfx::Canvas* canvas) {
canvas->sk_canvas()->getClipBounds(&last_clip_);
}
void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
scheduled_paint_rects_.push_back(rect);
View::SchedulePaintInRect(rect);
}
void CheckRect(const SkRect& check_rect, const SkRect& target_rect) {
EXPECT_EQ(target_rect.fLeft, check_rect.fLeft);
EXPECT_EQ(target_rect.fRight, check_rect.fRight);
EXPECT_EQ(target_rect.fTop, check_rect.fTop);
EXPECT_EQ(target_rect.fBottom, check_rect.fBottom);
}
TEST_F(ViewTest, RemoveNotification) {
ViewStorage* vs = ViewStorage::GetInstance();
Widget* widget = new Widget;
widget->Init(CreateParams(Widget::InitParams::TYPE_POPUP));
View* root_view = widget->GetRootView();
View* v1 = new View;
int s1 = vs->CreateStorageID();
vs->StoreView(s1, v1);
root_view->AddChildView(v1);
View* v11 = new View;
int s11 = vs->CreateStorageID();
vs->StoreView(s11, v11);
v1->AddChildView(v11);
View* v111 = new View;
int s111 = vs->CreateStorageID();
vs->StoreView(s111, v111);
v11->AddChildView(v111);
View* v112 = new View;
int s112 = vs->CreateStorageID();
vs->StoreView(s112, v112);
v11->AddChildView(v112);
View* v113 = new View;
int s113 = vs->CreateStorageID();
vs->StoreView(s113, v113);
v11->AddChildView(v113);
View* v1131 = new View;
int s1131 = vs->CreateStorageID();
vs->StoreView(s1131, v1131);
v113->AddChildView(v1131);
View* v12 = new View;
int s12 = vs->CreateStorageID();
vs->StoreView(s12, v12);
v1->AddChildView(v12);
View* v2 = new View;
int s2 = vs->CreateStorageID();
vs->StoreView(s2, v2);
root_view->AddChildView(v2);
View* v21 = new View;
int s21 = vs->CreateStorageID();
vs->StoreView(s21, v21);
v2->AddChildView(v21);
View* v211 = new View;
int s211 = vs->CreateStorageID();
vs->StoreView(s211, v211);
v21->AddChildView(v211);
size_t stored_views = vs->view_count();
v21->RemoveChildView(v211);
EXPECT_EQ(stored_views - 1, vs->view_count());
EXPECT_EQ(NULL, vs->RetrieveView(s211));
delete v211;
v11->RemoveChildView(v113);
EXPECT_EQ(stored_views - 3, vs->view_count());
EXPECT_EQ(NULL, vs->RetrieveView(s113));
EXPECT_EQ(NULL, vs->RetrieveView(s1131));
delete v113;
root_view->RemoveChildView(v1);
EXPECT_EQ(NULL, vs->RetrieveView(s1));
EXPECT_EQ(NULL, vs->RetrieveView(s11));
EXPECT_EQ(NULL, vs->RetrieveView(s12));
EXPECT_EQ(NULL, vs->RetrieveView(s111));
EXPECT_EQ(NULL, vs->RetrieveView(s112));
root_view->AddChildView(v1);
vs->StoreView(s1, v1);
widget->CloseNow();
EXPECT_EQ(stored_views - 10, vs->view_count());
EXPECT_EQ(NULL, vs->RetrieveView(s1));
EXPECT_EQ(NULL, vs->RetrieveView(s12));
EXPECT_EQ(NULL, vs->RetrieveView(s11));
EXPECT_EQ(NULL, vs->RetrieveView(s12));
EXPECT_EQ(NULL, vs->RetrieveView(s21));
EXPECT_EQ(NULL, vs->RetrieveView(s111));
EXPECT_EQ(NULL, vs->RetrieveView(s112));
}
namespace {
class HitTestView : public View {
public:
explicit HitTestView(bool has_hittest_mask)
: has_hittest_mask_(has_hittest_mask) {
}
virtual ~HitTestView() {}
protected:
virtual bool HasHitTestMask() const OVERRIDE {
return has_hittest_mask_;
}
virtual void GetHitTestMask(HitTestSource source,
gfx::Path* mask) const OVERRIDE {
DCHECK(has_hittest_mask_);
DCHECK(mask);
SkScalar w = SkIntToScalar(width());
SkScalar h = SkIntToScalar(height());
mask->moveTo(w / 2, 0);
mask->lineTo(w, h);
mask->lineTo(0, h);
mask->close();
}
private:
bool has_hittest_mask_;
DISALLOW_COPY_AND_ASSIGN(HitTestView);
};
gfx::Point ConvertPointToView(View* view, const gfx::Point& p) {
gfx::Point tmp(p);
View::ConvertPointToTarget(view->GetWidget()->GetRootView(), view, &tmp);
return tmp;
}
gfx::Rect ConvertRectToView(View* view, const gfx::Rect& r) {
gfx::Rect tmp(r);
tmp.set_origin(ConvertPointToView(view, r.origin()));
return tmp;
}
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,
-1, 0, 0,
0, 0, 1);
}
}
TEST_F(ViewTest, HitTestMasks) {
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));
gfx::Rect v1_bounds = gfx::Rect(0, 0, 100, 100);
HitTestView* v1 = new HitTestView(false);
v1->SetBoundsRect(v1_bounds);
root_view->AddChildView(v1);
gfx::Rect v2_bounds = gfx::Rect(105, 0, 100, 100);
HitTestView* v2 = new HitTestView(true);
v2->SetBoundsRect(v2_bounds);
root_view->AddChildView(v2);
gfx::Point v1_centerpoint = v1_bounds.CenterPoint();
gfx::Point v2_centerpoint = v2_bounds.CenterPoint();
gfx::Point v1_origin = v1_bounds.origin();
gfx::Point v2_origin = v2_bounds.origin();
gfx::Rect r1(10, 10, 110, 15);
gfx::Rect r2(106, 1, 98, 98);
gfx::Rect r3(0, 0, 300, 300);
gfx::Rect r4(115, 342, 200, 10);
EXPECT_TRUE(v1->HitTestPoint(ConvertPointToView(v1, v1_centerpoint)));
EXPECT_TRUE(v2->HitTestPoint(ConvertPointToView(v2, v2_centerpoint)));
EXPECT_TRUE(v1->HitTestPoint(ConvertPointToView(v1, v1_origin)));
EXPECT_FALSE(v2->HitTestPoint(ConvertPointToView(v2, v2_origin)));
EXPECT_TRUE(v1->HitTestRect(ConvertRectToView(v1, r1)));
EXPECT_FALSE(v2->HitTestRect(ConvertRectToView(v2, r1)));
EXPECT_FALSE(v1->HitTestRect(ConvertRectToView(v1, r2)));
EXPECT_TRUE(v2->HitTestRect(ConvertRectToView(v2, r2)));
EXPECT_TRUE(v1->HitTestRect(ConvertRectToView(v1, r3)));
EXPECT_TRUE(v2->HitTestRect(ConvertRectToView(v2, r3)));
EXPECT_FALSE(v1->HitTestRect(ConvertRectToView(v1, r4)));
EXPECT_FALSE(v2->HitTestRect(ConvertRectToView(v2, r4)));
EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_centerpoint));
EXPECT_EQ(v2, root_view->GetEventHandlerForPoint(v2_centerpoint));
EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_origin));
EXPECT_EQ(root_view, root_view->GetEventHandlerForPoint(v2_origin));
EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_centerpoint));
EXPECT_EQ(v2, root_view->GetTooltipHandlerForPoint(v2_centerpoint));
EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin));
EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin));
EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin));
widget->CloseNow();
}
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));
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);
gfx::Rect touch_rect(105, 105, 30, 45);
View* result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = NULL;
touch_rect.SetRect(15, 15, 100, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = NULL;
touch_rect.SetRect(50, 50, 5, 10);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = NULL;
touch_rect.SetRect(95, 96, 21, 22);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = NULL;
touch_rect.SetRect(95, 10, 300, 120);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v2, result_view);
result_view = NULL;
touch_rect.SetRect(20, 20, 400, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v2, result_view);
result_view = NULL;
touch_rect.SetRect(-700, -15, 1050, 110);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = NULL;
touch_rect.SetRect(15, 15, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v1, result_view);
result_view = NULL;
touch_rect.SetRect(0, 200, 110, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v31, result_view);
result_view = NULL;
touch_rect.SetRect(80, 280, 15, 15);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v31, result_view);
result_view = NULL;
touch_rect.SetRect(0, 200, 200, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = NULL;
touch_rect.SetRect(30, 225, 180, 115);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = NULL;
touch_rect.SetRect(0, 200, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v3, result_view);
result_view = NULL;
touch_rect.SetRect(112, 211, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = NULL;
touch_rect.SetRect(310, 210, 80, 80);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = NULL;
touch_rect.SetRect(370, 275, 7, 5);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v411, result_view);
result_view = NULL;
touch_rect.SetRect(345, 245, 7, 7);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = NULL;
touch_rect.SetRect(368, 272, 4, 6);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v411, result_view);
result_view = NULL;
touch_rect.SetRect(365, 270, 7, 7);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = NULL;
touch_rect.SetRect(205, 275, 200, 2);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v4, result_view);
result_view = NULL;
touch_rect.SetRect(310, 210, 61, 66);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = NULL;
touch_rect.SetRect(372, 275, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v411, result_view);
result_view = NULL;
touch_rect.SetRect(350, 215, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v41, result_view);
result_view = NULL;
touch_rect.SetRect(0, 200, 400, 100);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = NULL;
touch_rect.SetRect(110, 15, 375, 450);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = NULL;
touch_rect.SetRect(0, 0, 400, 300);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v32, result_view);
result_view = NULL;
touch_rect.SetRect(440, 190, 40, 40);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = NULL;
touch_rect.SetRect(455, 187, 60, 60);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = NULL;
touch_rect.SetRect(450, 197, 10, 10);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = NULL;
touch_rect.SetRect(433, 180, 24, 24);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(root_view, result_view);
result_view = NULL;
touch_rect.SetRect(449, 196, 3, 3);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v5, result_view);
result_view = NULL;
touch_rect.SetRect(462, 199, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v5, result_view);
result_view = NULL;
touch_rect.SetRect(452, 226, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = NULL;
touch_rect.SetRect(465, 215, 1, 1);
result_view = root_view->GetEventHandlerForRect(touch_rect);
EXPECT_EQ(v51, result_view);
result_view = NULL;
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));
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();
gfx::Point p1(6, 6);
ui::MouseEvent move1(ui::ET_MOUSE_MOVED, p1, p1, 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();
gfx::Point p2(65, 21);
ui::MouseEvent move2(ui::ET_MOUSE_MOVED, p2, p2, 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();
gfx::Point p3(1, 1);
ui::MouseEvent move3(ui::ET_MOUSE_MOVED, p3, p3, 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();
gfx::Point p4(121, 15);
ui::MouseEvent move4(ui::ET_MOUSE_MOVED, p4, p4, 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();
gfx::Point p5(21, 0);
ui::MouseEvent move5(ui::ET_MOUSE_MOVED, p5, p5, 0, 0);
root_view->OnMouseMoved(move5);
EXPECT_TRUE(v21->received_mouse_exit_);
EXPECT_TRUE(v1->received_mouse_enter_);
v21->Reset();
v1->Reset();
gfx::Point p6(15, 15);
ui::MouseEvent mouse6(ui::ET_MOUSE_MOVED, p6, p6, 0, 0);
root_view->OnMouseMoved(mouse6);
EXPECT_TRUE(v11->received_mouse_enter_);
EXPECT_FALSE(v1->last_mouse_event_type_);
v11->Reset();
v1->Reset();
gfx::Point p7(21, 0);
ui::MouseEvent mouse7(ui::ET_MOUSE_MOVED, p7, p7, 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!");
const base::string16 kEmptyString;
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);
textfield->SetText(kText);
EXPECT_EQ(kText, textfield->text());
textfield->AppendText(kExtraText);
EXPECT_EQ(kText + kExtraText, textfield->text());
textfield->SetText(base::string16());
EXPECT_EQ(kEmptyString, textfield->text());
textfield->SetText(kText);
EXPECT_EQ(kEmptyString, textfield->GetSelectedText());
textfield->SelectAll(false);
EXPECT_EQ(kText, textfield->text());
textfield->ClearSelection();
EXPECT_EQ(kEmptyString, textfield->GetSelectedText());
widget->CloseNow();
}
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);
normal->SelectAll(false);
normal->ExecuteCommand(IDS_APP_CUT);
base::string16 result;
clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
EXPECT_EQ(kNormalText, result);
normal->SetText(kNormalText);
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_CUT);
result.clear();
clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
EXPECT_EQ(kNormalText, result);
password->SelectAll(false);
password->ExecuteCommand(IDS_APP_CUT);
result.clear();
clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
EXPECT_EQ(kNormalText, result);
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_COPY);
result.clear();
clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
EXPECT_EQ(kReadOnlyText, result);
normal->SelectAll(false);
normal->ExecuteCommand(IDS_APP_COPY);
result.clear();
clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
EXPECT_EQ(kNormalText, result);
password->SelectAll(false);
password->ExecuteCommand(IDS_APP_COPY);
result.clear();
clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
EXPECT_EQ(kNormalText, result);
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_PASTE);
EXPECT_EQ(kReadOnlyText, read_only->text());
password->SelectAll(false);
password->ExecuteCommand(IDS_APP_PASTE);
EXPECT_EQ(kNormalText, password->text());
read_only->SelectAll(false);
read_only->ExecuteCommand(IDS_APP_COPY);
normal->SelectAll(false);
normal->ExecuteCommand(IDS_APP_PASTE);
EXPECT_EQ(kReadOnlyText, normal->text());
widget->CloseNow();
}
bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) {
accelerator_count_map_[accelerator]++;
return true;
}
#if defined(false)
TEST_F(ViewTest, ActivateAccelerator) {
ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
TestView* view = new TestView();
view->Reset();
view->AddAccelerator(return_accelerator);
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
scoped_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(0, 0, 100, 100);
widget->Init(params);
View* root = widget->GetRootView();
root->AddChildView(view);
widget->Show();
FocusManager* focus_manager = widget->GetFocusManager();
ASSERT_TRUE(focus_manager);
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
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);
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);
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);
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);
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);
widget->CloseNow();
}
TEST_F(ViewTest, HiddenViewWithAccelerator) {
ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
TestView* view = new TestView();
view->Reset();
view->AddAccelerator(return_accelerator);
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
scoped_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(0, 0, 100, 100);
widget->Init(params);
View* root = widget->GetRootView();
root->AddChildView(view);
widget->Show();
FocusManager* focus_manager = widget->GetFocusManager();
ASSERT_TRUE(focus_manager);
view->SetVisible(false);
EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
view->SetVisible(true);
EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
widget->CloseNow();
}
TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) {
ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
TestView* view = new TestView();
view->Reset();
view->AddAccelerator(return_accelerator);
EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
scoped_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(0, 0, 100, 100);
widget->Init(params);
View* root = widget->GetRootView();
root->AddChildView(view);
FocusManager* focus_manager = widget->GetFocusManager();
ASSERT_TRUE(focus_manager);
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]);
widget->CloseNow();
}
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_;
};
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());
window1->Activate();
::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
class ToplevelWidgetObserverView : public View {
public:
ToplevelWidgetObserverView() : toplevel_(NULL) {
}
virtual ~ToplevelWidgetObserverView() {
}
virtual void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) OVERRIDE {
if (details.is_add) {
toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
} else {
toplevel_ = NULL;
}
}
virtual void NativeViewHierarchyChanged() OVERRIDE {
toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
}
Widget* toplevel() { return toplevel_; }
private:
Widget* toplevel_;
DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView);
};
TEST_F(ViewTest, NativeViewHierarchyChanged) {
scoped_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);
scoped_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(NULL, observer_view->toplevel());
child->SetContentsView(observer_view);
EXPECT_EQ(toplevel1, observer_view->toplevel());
Widget::ReparentNativeView(child->GetNativeView(),
toplevel2->GetNativeView());
EXPECT_EQ(toplevel2, observer_view->toplevel());
observer_view->parent()->RemoveChildView(observer_view);
EXPECT_EQ(NULL, observer_view->toplevel());
child->SetContentsView(observer_view);
}
class TransformPaintView : public TestView {
public:
TransformPaintView() {}
virtual ~TransformPaintView() {}
void ClearScheduledPaintRect() {
scheduled_paint_rect_ = gfx::Rect();
}
gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
virtual 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);
v1->ClearScheduledPaintRect();
v2->SchedulePaint();
EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
gfx::Transform transform;
RotateCounterclockwise(&transform);
transform.matrix().set(1, 3, 500.0);
v1->SetTransform(transform);
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);
gfx::Transform transform(v1->GetTransform());
RotateCounterclockwise(&transform);
transform.matrix().set(1, 3, 500.0);
v1->SetTransform(transform);
v1->Reset();
v2->Reset();
gfx::Point p1(110, 210);
ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1,
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
root->OnMousePressed(pressed);
EXPECT_EQ(0, v1->last_mouse_event_type_);
EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
EXPECT_EQ(190, v2->location_.x());
EXPECT_EQ(10, v2->location_.y());
ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), 0,
0);
root->OnMouseReleased(released);
transform = v2->GetTransform();
RotateClockwise(&transform);
transform.matrix().set(0, 3, 100.f);
v2->SetTransform(transform);
v1->Reset();
v2->Reset();
gfx::Point point2(110, 320);
ui::MouseEvent p2(ui::ET_MOUSE_PRESSED, point2, point2,
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
root->OnMousePressed(p2);
EXPECT_EQ(0, v1->last_mouse_event_type_);
EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
EXPECT_EQ(10, v2->location_.x());
EXPECT_EQ(20, v2->location_.y());
root->OnMouseReleased(released);
v1->SetTransform(gfx::Transform());
v2->SetTransform(gfx::Transform());
TestView* v3 = new TestView();
v3->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
v2->AddChildView(v3);
transform = v1->GetTransform();
RotateClockwise(&transform);
transform.matrix().set(0, 3, 30.f);
v3->SetTransform(transform);
transform = v2->GetTransform();
transform.matrix().set(0, 0, 0.8f);
transform.matrix().set(1, 1, 0.5f);
v2->SetTransform(transform);
v1->Reset();
v2->Reset();
v3->Reset();
gfx::Point point(112, 110);
ui::MouseEvent p3(ui::ET_MOUSE_PRESSED, point, point,
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
root->OnMousePressed(p3);
EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
EXPECT_EQ(10, v3->location_.x());
EXPECT_EQ(25, v3->location_.y());
root->OnMouseReleased(released);
v1->SetTransform(gfx::Transform());
v2->SetTransform(gfx::Transform());
v3->SetTransform(gfx::Transform());
v1->Reset();
v2->Reset();
v3->Reset();
transform = v3->GetTransform();
RotateClockwise(&transform);
transform.matrix().set(0, 3, 30.f);
gfx::Transform scale;
scale.Scale(0.8f, 0.5f);
transform.ConcatTransform(scale);
v3->SetTransform(transform);
transform = v2->GetTransform();
transform.matrix().set(0, 3, 10.f);
transform.matrix().set(1, 3, 10.f);
v2->SetTransform(transform);
gfx::Point point3(124, 125);
ui::MouseEvent p4(ui::ET_MOUSE_PRESSED, point3, point3,
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
root->OnMousePressed(p4);
EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
EXPECT_EQ(10, v3->location_.x());
EXPECT_EQ(25, v3->location_.y());
root->OnMouseReleased(released);
widget->CloseNow();
}
TEST_F(ViewTest, TransformVisibleBound) {
gfx::Rect viewport_bounds(0, 0, 100, 100);
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = viewport_bounds;
widget->Init(params);
widget->GetRootView()->SetBoundsRect(viewport_bounds);
View* viewport = new View;
widget->SetContentsView(viewport);
View* contents = new View;
viewport->AddChildView(contents);
viewport->SetBoundsRect(viewport_bounds);
contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
View* child = new View;
contents->AddChildView(child);
child->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds());
gfx::Transform transform;
RotateCounterclockwise(&transform);
transform.matrix().set(1, 3, 50.f);
child->SetTransform(transform);
EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds());
widget->CloseNow();
}
class VisibleBoundsView : public View {
public:
VisibleBoundsView() : received_notification_(false) {}
virtual ~VisibleBoundsView() {}
bool received_notification() const { return received_notification_; }
void set_received_notification(bool received) {
received_notification_ = received;
}
private:
virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE {
return true;
}
virtual void OnVisibleBoundsChanged() OVERRIDE {
received_notification_ = true;
}
bool received_notification_;
DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView);
};
TEST_F(ViewTest, OnVisibleBoundsChanged) {
gfx::Rect viewport_bounds(0, 0, 100, 100);
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = viewport_bounds;
widget->Init(params);
widget->GetRootView()->SetBoundsRect(viewport_bounds);
View* viewport = new View;
widget->SetContentsView(viewport);
View* contents = new View;
viewport->AddChildView(contents);
viewport->SetBoundsRect(viewport_bounds);
contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
VisibleBoundsView* child = new VisibleBoundsView;
contents->AddChildView(child);
child->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
contents->SetY(contents->y() - 1);
EXPECT_TRUE(child->received_notification());
EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
child->set_received_notification(false);
contents->SetY(contents->y() - 10);
EXPECT_TRUE(child->received_notification());
EXPECT_EQ(1, child->GetVisibleBounds().height());
child->set_received_notification(false);
widget->CloseNow();
}
TEST_F(ViewTest, SetBoundsPaint) {
TestView top_view;
TestView* child_view = new TestView;
top_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
top_view.scheduled_paint_rects_.clear();
child_view->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
top_view.AddChildView(child_view);
top_view.scheduled_paint_rects_.clear();
child_view->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
EXPECT_EQ(2U, top_view.scheduled_paint_rects_.size());
gfx::Rect paint_rect = top_view.scheduled_paint_rects_[0];
paint_rect.Union(top_view.scheduled_paint_rects_[1]);
EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect);
}
TEST_F(ViewTest, FocusBlurPaints) {
TestView parent_view;
TestView* child_view1 = new TestView;
parent_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
child_view1->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
parent_view.AddChildView(child_view1);
parent_view.scheduled_paint_rects_.clear();
child_view1->scheduled_paint_rects_.clear();
child_view1->DoFocus();
EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
child_view1->DoBlur();
EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
}
TEST_F(ViewTest, SetBoundsSameBoundsDoesntSchedulePaint) {
TestView view;
view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
view.InvalidateLayout();
view.scheduled_paint_rects_.clear();
view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
EXPECT_TRUE(view.scheduled_paint_rects_.empty());
}
TEST_F(ViewTest, AddAndRemoveSchedulePaints) {
gfx::Rect viewport_bounds(0, 0, 100, 100);
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = viewport_bounds;
widget->Init(params);
widget->GetRootView()->SetBoundsRect(viewport_bounds);
TestView* parent_view = new TestView;
widget->SetContentsView(parent_view);
parent_view->SetBoundsRect(viewport_bounds);
parent_view->scheduled_paint_rects_.clear();
View* child_view = new View;
child_view->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
parent_view->AddChildView(child_view);
ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
parent_view->scheduled_paint_rects_.clear();
parent_view->RemoveChildView(child_view);
scoped_ptr<View> child_deleter(child_view);
ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
widget->CloseNow();
}
TEST_F(ViewTest, ConversionsWithTransform) {
TestView top_view;
TestView* child = new TestView;
TestView* child_child = new TestView;
TestView* child_2 = new TestView;
top_view.AddChildView(child);
child->AddChildView(child_child);
top_view.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
child->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
gfx::Transform transform;
transform.Scale(3.0, 4.0);
child->SetTransform(transform);
child_child->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
transform.MakeIdentity();
transform.Scale(5.0, 7.0);
child_child->SetTransform(transform);
top_view.AddChildView(child_2);
child_2->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
transform.MakeIdentity();
RotateClockwise(&transform);
child_2->SetTransform(transform);
{
gfx::Transform transform;
transform.Translate(110.0, -110.0);
transform.Scale(100.0, 55.0);
transform.Translate(1.0, 1.0);
const SkMatrix& matrix = transform.matrix();
EXPECT_EQ(210, matrix.getTranslateX());
EXPECT_EQ(-55, matrix.getTranslateY());
EXPECT_EQ(100, matrix.getScaleX());
EXPECT_EQ(55, matrix.getScaleY());
EXPECT_EQ(0, matrix.getSkewX());
EXPECT_EQ(0, matrix.getSkewY());
}
{
gfx::Transform transform;
transform.Translate(1.0, 1.0);
gfx::Transform t2;
t2.Scale(100.0, 55.0);
gfx::Transform t3;
t3.Translate(110.0, -110.0);
transform.ConcatTransform(t2);
transform.ConcatTransform(t3);
const SkMatrix& matrix = transform.matrix();
EXPECT_EQ(210, matrix.getTranslateX());
EXPECT_EQ(-55, matrix.getTranslateY());
EXPECT_EQ(100, matrix.getScaleX());
EXPECT_EQ(55, matrix.getScaleY());
EXPECT_EQ(0, matrix.getSkewX());
EXPECT_EQ(0, matrix.getSkewY());
}
{
gfx::Point point(5, 5);
View::ConvertPointToTarget(child, &top_view, &point);
EXPECT_EQ(22, point.x());
EXPECT_EQ(39, point.y());
gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
View::ConvertRectToTarget(child, &top_view, &rect);
EXPECT_FLOAT_EQ(22.0f, rect.x());
EXPECT_FLOAT_EQ(39.0f, rect.y());
EXPECT_FLOAT_EQ(30.0f, rect.width());
EXPECT_FLOAT_EQ(80.0f, rect.height());
point.SetPoint(22, 39);
View::ConvertPointToTarget(&top_view, child, &point);
EXPECT_EQ(5, point.x());
EXPECT_EQ(5, point.y());
rect.SetRect(22.0f, 39.0f, 30.0f, 80.0f);
View::ConvertRectToTarget(&top_view, child, &rect);
EXPECT_FLOAT_EQ(5.0f, rect.x());
EXPECT_FLOAT_EQ(5.0f, rect.y());
EXPECT_FLOAT_EQ(10.0f, rect.width());
EXPECT_FLOAT_EQ(20.0f, rect.height());
}
{
gfx::Point point(5, 5);
View::ConvertPointToTarget(child_child, &top_view, &point);
EXPECT_EQ(133, point.x());
EXPECT_EQ(211, point.y());
gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
View::ConvertRectToTarget(child_child, &top_view, &rect);
EXPECT_FLOAT_EQ(133.0f, rect.x());
EXPECT_FLOAT_EQ(211.0f, rect.y());
EXPECT_FLOAT_EQ(150.0f, rect.width());
EXPECT_FLOAT_EQ(560.0f, rect.height());
point.SetPoint(133, 211);
View::ConvertPointToTarget(&top_view, child_child, &point);
EXPECT_EQ(5, point.x());
EXPECT_EQ(5, point.y());
rect.SetRect(133.0f, 211.0f, 150.0f, 560.0f);
View::ConvertRectToTarget(&top_view, child_child, &rect);
EXPECT_FLOAT_EQ(5.0f, rect.x());
EXPECT_FLOAT_EQ(5.0f, rect.y());
EXPECT_FLOAT_EQ(10.0f, rect.width());
EXPECT_FLOAT_EQ(20.0f, rect.height());
}
{
gfx::Point point(5, 5);
View::ConvertPointToTarget(child_child, child, &point);
EXPECT_EQ(42, point.x());
EXPECT_EQ(48, point.y());
gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
View::ConvertRectToTarget(child_child, child, &rect);
EXPECT_FLOAT_EQ(42.0f, rect.x());
EXPECT_FLOAT_EQ(48.0f, rect.y());
EXPECT_FLOAT_EQ(50.0f, rect.width());
EXPECT_FLOAT_EQ(140.0f, rect.height());
point.SetPoint(42, 48);
View::ConvertPointToTarget(child, child_child, &point);
EXPECT_EQ(5, point.x());
EXPECT_EQ(5, point.y());
rect.SetRect(42.0f, 48.0f, 50.0f, 140.0f);
View::ConvertRectToTarget(child, child_child, &rect);
EXPECT_FLOAT_EQ(5.0f, rect.x());
EXPECT_FLOAT_EQ(5.0f, rect.y());
EXPECT_FLOAT_EQ(10.0f, rect.width());
EXPECT_FLOAT_EQ(20.0f, rect.height());
}
{
gfx::Point point(6, 18);
View::ConvertPointToTarget(&top_view, child, &point);
EXPECT_EQ(-1, point.x());
EXPECT_EQ(-1, point.y());
float error = 0.01f;
gfx::RectF rect(6.0f, 18.0f, 10.0f, 39.0f);
View::ConvertRectToTarget(&top_view, child, &rect);
EXPECT_NEAR(-0.33f, rect.x(), error);
EXPECT_NEAR(-0.25f, rect.y(), error);
EXPECT_NEAR(3.33f, rect.width(), error);
EXPECT_NEAR(9.75f, rect.height(), error);
}
{
gfx::RectF rect(50.0f, 55.0f, 20.0f, 30.0f);
View::ConvertRectToTarget(child_2, &top_view, &rect);
EXPECT_FLOAT_EQ(615.0f, rect.x());
EXPECT_FLOAT_EQ(775.0f, rect.y());
EXPECT_FLOAT_EQ(30.0f, rect.width());
EXPECT_FLOAT_EQ(20.0f, rect.height());
rect.SetRect(615.0f, 775.0f, 30.0f, 20.0f);
View::ConvertRectToTarget(&top_view, child_2, &rect);
EXPECT_FLOAT_EQ(50.0f, rect.x());
EXPECT_FLOAT_EQ(55.0f, rect.y());
EXPECT_FLOAT_EQ(20.0f, rect.width());
EXPECT_FLOAT_EQ(30.0f, rect.height());
}
}
TEST_F(ViewTest, ConversionsToFromScreen) {
scoped_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* child = new View;
widget->GetRootView()->AddChildView(child);
child->SetBounds(10, 10, 100, 200);
gfx::Transform t;
t.Scale(0.5, 0.5);
child->SetTransform(t);
gfx::Point point_in_screen(100, 90);
gfx::Point point_in_child(80,60);
gfx::Point point = point_in_screen;
View::ConvertPointFromScreen(child, &point);
EXPECT_EQ(point_in_child.ToString(), point.ToString());
View::ConvertPointToScreen(child, &point);
EXPECT_EQ(point_in_screen.ToString(), point.ToString());
}
TEST_F(ViewTest, ConvertRectWithTransform) {
scoped_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();
TestView* v1 = new TestView;
TestView* v2 = new TestView;
root->AddChildView(v1);
v1->AddChildView(v2);
v1->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
v2->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
gfx::Rect rect(5, 5, 15, 40);
EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2->ConvertRectToParent(rect));
EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2->ConvertRectToWidget(rect));
gfx::Transform t2;
RotateCounterclockwise(&t2);
t2.matrix().set(1, 3, 100.f);
v2->SetTransform(t2);
EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2->ConvertRectToWidget(rect));
gfx::Transform t1;
t1.Scale(0.5, 0.5);
v1->SetTransform(t1);
EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
v2->ConvertRectToWidget(rect).ToString());
widget->CloseNow();
}
class ObserverView : public View {
public:
ObserverView();
virtual ~ObserverView();
void ResetTestState();
bool has_add_details() const { return has_add_details_; }
bool has_remove_details() const { return has_remove_details_; }
const ViewHierarchyChangedDetails& add_details() const {
return add_details_;
}
const ViewHierarchyChangedDetails& remove_details() const {
return remove_details_;
}
private:
virtual void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) OVERRIDE;
bool has_add_details_;
bool has_remove_details_;
ViewHierarchyChangedDetails add_details_;
ViewHierarchyChangedDetails remove_details_;
DISALLOW_COPY_AND_ASSIGN(ObserverView);
};
ObserverView::ObserverView()
: has_add_details_(false),
has_remove_details_(false) {
}
ObserverView::~ObserverView() {}
void ObserverView::ResetTestState() {
has_add_details_ = false;
has_remove_details_ = false;
add_details_ = ViewHierarchyChangedDetails();
remove_details_ = ViewHierarchyChangedDetails();
}
void ObserverView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
if (details.is_add) {
has_add_details_ = true;
add_details_ = details;
} else {
has_remove_details_ = true;
remove_details_ = details;
}
}
TEST_F(ViewTest, ViewHierarchyChanged) {
ObserverView v1;
ObserverView* v3 = new ObserverView();
scoped_ptr<ObserverView> v2(new ObserverView());
v2->AddChildView(v3);
EXPECT_TRUE(v2->has_add_details());
EXPECT_FALSE(v2->has_remove_details());
EXPECT_EQ(v2.get(), v2->add_details().parent);
EXPECT_EQ(v3, v2->add_details().child);
EXPECT_EQ(NULL, v2->add_details().move_view);
EXPECT_TRUE(v3->has_add_details());
EXPECT_FALSE(v3->has_remove_details());
EXPECT_EQ(v2.get(), v3->add_details().parent);
EXPECT_EQ(v3, v3->add_details().child);
EXPECT_EQ(NULL, v3->add_details().move_view);
v2->ResetTestState();
v3->ResetTestState();
v1.AddChildView(v2.get());
EXPECT_TRUE(v1.has_add_details());
EXPECT_FALSE(v1.has_remove_details());
EXPECT_EQ(&v1, v1.add_details().parent);
EXPECT_EQ(v2.get(), v1.add_details().child);
EXPECT_EQ(NULL, v1.add_details().move_view);
EXPECT_TRUE(v2->has_add_details());
EXPECT_FALSE(v2->has_remove_details());
EXPECT_EQ(&v1, v2->add_details().parent);
EXPECT_EQ(v2.get(), v2->add_details().child);
EXPECT_EQ(NULL, v2->add_details().move_view);
EXPECT_TRUE(v3->has_add_details());
EXPECT_FALSE(v3->has_remove_details());
EXPECT_EQ(&v1, v3->add_details().parent);
EXPECT_EQ(v2.get(), v3->add_details().child);
EXPECT_EQ(NULL, v3->add_details().move_view);
v1.ResetTestState();
v2->ResetTestState();
v3->ResetTestState();
v1.RemoveChildView(v2.get());
EXPECT_FALSE(v1.has_add_details());
EXPECT_TRUE(v1.has_remove_details());
EXPECT_EQ(&v1, v1.remove_details().parent);
EXPECT_EQ(v2.get(), v1.remove_details().child);
EXPECT_EQ(NULL, v1.remove_details().move_view);
EXPECT_FALSE(v2->has_add_details());
EXPECT_TRUE(v2->has_remove_details());
EXPECT_EQ(&v1, v2->remove_details().parent);
EXPECT_EQ(v2.get(), v2->remove_details().child);
EXPECT_EQ(NULL, v2->remove_details().move_view);
EXPECT_FALSE(v3->has_add_details());
EXPECT_TRUE(v3->has_remove_details());
EXPECT_EQ(&v1, v3->remove_details().parent);
EXPECT_EQ(v3, v3->remove_details().child);
EXPECT_EQ(NULL, v3->remove_details().move_view);
ObserverView* v4 = new ObserverView();
v2->AddChildView(v4);
v1.ResetTestState();
v2->ResetTestState();
v3->ResetTestState();
v4->ResetTestState();
v1.AddChildView(v4);
EXPECT_TRUE(v1.has_add_details());
EXPECT_FALSE(v1.has_remove_details());
EXPECT_EQ(&v1, v1.add_details().parent);
EXPECT_EQ(v4, v1.add_details().child);
EXPECT_EQ(v2.get(), v1.add_details().move_view);
EXPECT_FALSE(v2->has_add_details());
EXPECT_TRUE(v2->has_remove_details());
EXPECT_EQ(v2.get(), v2->remove_details().parent);
EXPECT_EQ(v4, v2->remove_details().child);
EXPECT_EQ(&v1, v2->remove_details().move_view);
EXPECT_FALSE(v3->has_add_details());
EXPECT_FALSE(v3->has_remove_details());
EXPECT_TRUE(v4->has_remove_details());
EXPECT_TRUE(v4->has_add_details());
EXPECT_EQ(v2.get(), v4->remove_details().parent);
EXPECT_EQ(&v1, v4->add_details().parent);
EXPECT_EQ(v4, v4->add_details().child);
EXPECT_EQ(v4, v4->remove_details().child);
EXPECT_EQ(&v1, v4->remove_details().move_view);
EXPECT_EQ(v2.get(), v4->add_details().move_view);
}
TEST_F(ViewTest, RemoveAllChildViews) {
View root;
View* child1 = new View;
root.AddChildView(child1);
for (int i = 0; i < 2; ++i)
root.AddChildView(new View);
View* foo = new View;
child1->AddChildView(foo);
for (int i = 0; i < 3; ++i)
foo->AddChildView(new View);
EXPECT_EQ(3, root.child_count());
EXPECT_EQ(1, child1->child_count());
EXPECT_EQ(3, foo->child_count());
root.RemoveAllChildViews(true);
EXPECT_EQ(0, root.child_count());
EXPECT_FALSE(root.has_children());
}
TEST_F(ViewTest, Contains) {
View v1;
View* v2 = new View;
View* v3 = new View;
v1.AddChildView(v2);
v2->AddChildView(v3);
EXPECT_FALSE(v1.Contains(NULL));
EXPECT_TRUE(v1.Contains(&v1));
EXPECT_TRUE(v1.Contains(v2));
EXPECT_TRUE(v1.Contains(v3));
EXPECT_FALSE(v2->Contains(NULL));
EXPECT_TRUE(v2->Contains(v2));
EXPECT_FALSE(v2->Contains(&v1));
EXPECT_TRUE(v2->Contains(v3));
EXPECT_FALSE(v3->Contains(NULL));
EXPECT_TRUE(v3->Contains(v3));
EXPECT_FALSE(v3->Contains(&v1));
EXPECT_FALSE(v3->Contains(v2));
}
TEST_F(ViewTest, GetIndexOf) {
View root;
View* child1 = new View;
root.AddChildView(child1);
View* child2 = new View;
root.AddChildView(child2);
View* foo1 = new View;
child1->AddChildView(foo1);
EXPECT_EQ(-1, root.GetIndexOf(NULL));
EXPECT_EQ(-1, root.GetIndexOf(&root));
EXPECT_EQ(0, root.GetIndexOf(child1));
EXPECT_EQ(1, root.GetIndexOf(child2));
EXPECT_EQ(-1, root.GetIndexOf(foo1));
EXPECT_EQ(-1, child1->GetIndexOf(NULL));
EXPECT_EQ(-1, child1->GetIndexOf(&root));
EXPECT_EQ(-1, child1->GetIndexOf(child1));
EXPECT_EQ(-1, child1->GetIndexOf(child2));
EXPECT_EQ(0, child1->GetIndexOf(foo1));
EXPECT_EQ(-1, child2->GetIndexOf(NULL));
EXPECT_EQ(-1, child2->GetIndexOf(&root));
EXPECT_EQ(-1, child2->GetIndexOf(child2));
EXPECT_EQ(-1, child2->GetIndexOf(child1));
EXPECT_EQ(-1, child2->GetIndexOf(foo1));
}
TEST_F(ViewTest, ReorderChildren) {
View root;
View* child = new View();
root.AddChildView(child);
View* foo1 = new View();
child->AddChildView(foo1);
View* foo2 = new View();
child->AddChildView(foo2);
View* foo3 = new View();
child->AddChildView(foo3);
foo1->SetFocusable(true);
foo2->SetFocusable(true);
foo3->SetFocusable(true);
ASSERT_EQ(0, child->GetIndexOf(foo1));
ASSERT_EQ(1, child->GetIndexOf(foo2));
ASSERT_EQ(2, child->GetIndexOf(foo3));
ASSERT_EQ(foo2, foo1->GetNextFocusableView());
ASSERT_EQ(foo3, foo2->GetNextFocusableView());
ASSERT_EQ(NULL, foo3->GetNextFocusableView());
child->ReorderChildView(foo2, -1);
ASSERT_EQ(0, child->GetIndexOf(foo1));
ASSERT_EQ(1, child->GetIndexOf(foo3));
ASSERT_EQ(2, child->GetIndexOf(foo2));
ASSERT_EQ(foo3, foo1->GetNextFocusableView());
ASSERT_EQ(foo2, foo3->GetNextFocusableView());
ASSERT_EQ(NULL, foo2->GetNextFocusableView());
child->ReorderChildView(foo1, -1);
ASSERT_EQ(0, child->GetIndexOf(foo3));
ASSERT_EQ(1, child->GetIndexOf(foo2));
ASSERT_EQ(2, child->GetIndexOf(foo1));
ASSERT_EQ(NULL, foo1->GetNextFocusableView());
ASSERT_EQ(foo2, foo1->GetPreviousFocusableView());
ASSERT_EQ(foo2, foo3->GetNextFocusableView());
ASSERT_EQ(foo1, foo2->GetNextFocusableView());
child->ReorderChildView(foo2, 0);
ASSERT_EQ(0, child->GetIndexOf(foo2));
ASSERT_EQ(1, child->GetIndexOf(foo3));
ASSERT_EQ(2, child->GetIndexOf(foo1));
ASSERT_EQ(NULL, foo1->GetNextFocusableView());
ASSERT_EQ(foo3, foo1->GetPreviousFocusableView());
ASSERT_EQ(foo3, foo2->GetNextFocusableView());
ASSERT_EQ(foo1, foo3->GetNextFocusableView());
}
TEST_F(ViewTest, GetViewByID) {
View v1;
const int kV1ID = 1;
v1.set_id(kV1ID);
View v2;
const int kV2ID = 2;
v2.set_id(kV2ID);
View v3;
const int kV3ID = 3;
v3.set_id(kV3ID);
View v4;
const int kV4ID = 4;
v4.set_id(kV4ID);
const int kV5ID = 5;
v1.AddChildView(&v2);
v2.AddChildView(&v3);
v2.AddChildView(&v4);
EXPECT_EQ(&v1, v1.GetViewByID(kV1ID));
EXPECT_EQ(&v2, v1.GetViewByID(kV2ID));
EXPECT_EQ(&v4, v1.GetViewByID(kV4ID));
EXPECT_EQ(NULL, v1.GetViewByID(kV5ID));
EXPECT_EQ(NULL, v2.GetViewByID(kV1ID));
const int kGroup = 1;
v3.SetGroup(kGroup);
v4.SetGroup(kGroup);
View::Views views;
v1.GetViewsInGroup(kGroup, &views);
EXPECT_EQ(2U, views.size());
View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3));
EXPECT_NE(views.end(), i);
i = std::find(views.begin(), views.end(), &v4);
EXPECT_NE(views.end(), i);
}
TEST_F(ViewTest, AddExistingChild) {
View v1, v2, v3;
v1.AddChildView(&v2);
v1.AddChildView(&v3);
EXPECT_EQ(0, v1.GetIndexOf(&v2));
EXPECT_EQ(1, v1.GetIndexOf(&v3));
v1.AddChildViewAt(&v2, 0);
EXPECT_EQ(0, v1.GetIndexOf(&v2));
EXPECT_EQ(1, v1.GetIndexOf(&v3));
v1.AddChildViewAt(&v3, 1);
EXPECT_EQ(0, v1.GetIndexOf(&v2));
EXPECT_EQ(1, v1.GetIndexOf(&v3));
v1.AddChildViewAt(&v2, 1);
EXPECT_EQ(1, v1.GetIndexOf(&v2));
EXPECT_EQ(0, v1.GetIndexOf(&v3));
v1.AddChildViewAt(&v2, 0);
EXPECT_EQ(0, v1.GetIndexOf(&v2));
EXPECT_EQ(1, v1.GetIndexOf(&v3));
v1.AddChildView(&v2);
EXPECT_EQ(0, v1.GetIndexOf(&v2));
EXPECT_EQ(1, v1.GetIndexOf(&v3));
v1.AddChildView(&v3);
EXPECT_EQ(0, v1.GetIndexOf(&v2));
EXPECT_EQ(1, v1.GetIndexOf(&v3));
}
namespace {
class TestLayerAnimator : public ui::LayerAnimator {
public:
TestLayerAnimator();
const gfx::Rect& last_bounds() const { return last_bounds_; }
virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
protected:
virtual ~TestLayerAnimator() { }
private:
gfx::Rect last_bounds_;
DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
};
TestLayerAnimator::TestLayerAnimator()
: ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
}
void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) {
last_bounds_ = bounds;
}
}
class ViewLayerTest : public ViewsTestBase {
public:
ViewLayerTest() : widget_(NULL) {}
virtual ~ViewLayerTest() {
}
ui::Layer* GetRootLayer() {
return widget()->GetLayer();
}
virtual void SetUp() OVERRIDE {
ViewTest::SetUp();
widget_ = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(50, 50, 200, 200);
widget_->Init(params);
widget_->Show();
widget_->GetRootView()->SetBounds(0, 0, 200, 200);
}
virtual void TearDown() OVERRIDE {
widget_->CloseNow();
ViewsTestBase::TearDown();
}
Widget* widget() { return widget_; }
private:
Widget* widget_;
};
TEST_F(ViewLayerTest, LayerToggling) {
ui::Layer* root_layer = widget()->GetLayer();
View* content_view = new View;
widget()->SetContentsView(content_view);
View* v1 = new View;
v1->SetPaintToLayer(true);
EXPECT_TRUE(v1->layer() != NULL);
v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
content_view->AddChildView(v1);
ASSERT_TRUE(v1->layer() != NULL);
EXPECT_EQ(root_layer, v1->layer()->parent());
EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds());
View* v2 = new View;
v1->AddChildView(v2);
EXPECT_TRUE(v2->layer() == NULL);
v2->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
v2->SetPaintToLayer(true);
ASSERT_TRUE(v2->layer() != NULL);
EXPECT_EQ(v1->layer(), v2->layer()->parent());
EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
v1->SetPaintToLayer(false);
EXPECT_TRUE(v1->layer() == NULL);
EXPECT_TRUE(v2->layer() != NULL);
EXPECT_EQ(root_layer, v2->layer()->parent());
ASSERT_EQ(1u, root_layer->children().size());
EXPECT_EQ(root_layer->children()[0], v2->layer());
EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds());
gfx::Transform transform;
transform.Scale(2.0, 2.0);
v1->SetTransform(transform);
EXPECT_TRUE(v1->layer() != NULL);
EXPECT_TRUE(v2->layer() != NULL);
EXPECT_EQ(root_layer, v1->layer()->parent());
EXPECT_EQ(v1->layer(), v2->layer()->parent());
ASSERT_EQ(1u, root_layer->children().size());
EXPECT_EQ(root_layer->children()[0], v1->layer());
ASSERT_EQ(1u, v1->layer()->children().size());
EXPECT_EQ(v1->layer()->children()[0], v2->layer());
EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
}
TEST_F(ViewLayerTest, NestedLayerToggling) {
View* content_view = new View;
widget()->SetContentsView(content_view);
View* v1 = new View;
content_view->AddChildView(v1);
v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
View* v2 = new View;
v1->AddChildView(v2);
View* v3 = new View;
v3->SetPaintToLayer(true);
v2->AddChildView(v3);
ASSERT_TRUE(v3->layer() != NULL);
v1->SetPaintToLayer(true);
EXPECT_EQ(v1->layer(), v3->layer()->parent());
}
TEST_F(ViewLayerTest, LayerAnimator) {
View* content_view = new View;
widget()->SetContentsView(content_view);
View* v1 = new View;
content_view->AddChildView(v1);
v1->SetPaintToLayer(true);
EXPECT_TRUE(v1->layer() != NULL);
TestLayerAnimator* animator = new TestLayerAnimator();
v1->layer()->SetAnimator(animator);
gfx::Rect bounds(1, 2, 3, 4);
v1->SetBoundsRect(bounds);
EXPECT_EQ(bounds, animator->last_bounds());
EXPECT_NE(bounds, v1->layer()->bounds());
}
TEST_F(ViewLayerTest, BoundsChangeWithLayer) {
View* content_view = new View;
widget()->SetContentsView(content_view);
View* v1 = new View;
content_view->AddChildView(v1);
v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
View* v2 = new View;
v2->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
v1->AddChildView(v2);
v2->SetPaintToLayer(true);
ASSERT_TRUE(v2->layer() != NULL);
EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
v1->SetPosition(gfx::Point(25, 36));
EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
v2->SetPosition(gfx::Point(11, 12));
EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
v1->SetVisible(false);
v1->SetPosition(gfx::Point(20, 30));
EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
v2->SetVisible(false);
v2->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
}
TEST_F(ViewLayerTest, BoundInRTL) {
std::string locale = l10n_util::GetApplicationLocale(std::string());
base::i18n::SetICUDefaultLocale("he");
View* view = new View;
widget()->SetContentsView(view);
int content_width = view->width();
View* v1 = new View;
v1->SetPaintToLayer(true);
v1->SetBounds(10, 10, 20, 10);
EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
v1->layer()->bounds());
view->AddChildView(v1);
EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
v1->layer()->bounds());
gfx::Rect l1bounds = v1->layer()->bounds();
View* v2 = new View;
v2->SetBounds(50, 10, 30, 10);
EXPECT_FALSE(v2->layer());
view->AddChildView(v2);
v2->SetPaintToLayer(true);
EXPECT_EQ(gfx::Rect(content_width - 80, 10, 30, 10),
v2->layer()->bounds());
gfx::Rect l2bounds = v2->layer()->bounds();
view->SetPaintToLayer(true);
EXPECT_EQ(l1bounds, v1->layer()->bounds());
EXPECT_EQ(l2bounds, v2->layer()->bounds());
v1->SetBounds(v1->x() - 5, v1->y(), v1->width(), v1->height());
l1bounds.set_x(l1bounds.x() + 5);
EXPECT_EQ(l1bounds, v1->layer()->bounds());
view->SetPaintToLayer(false);
EXPECT_EQ(l1bounds, v1->layer()->bounds());
EXPECT_EQ(l2bounds, v2->layer()->bounds());
v2->SetBounds(v2->x() + 5, v2->y(), v2->width(), v2->height());
l2bounds.set_x(l2bounds.x() - 5);
EXPECT_EQ(l2bounds, v2->layer()->bounds());
base::i18n::SetICUDefaultLocale(locale);
}
TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
View* view = new View;
gfx::Transform transform;
transform.Scale(2.0, 2.0);
view->SetTransform(transform);
widget()->SetContentsView(view);
EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
view->SetVisible(false);
EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
view->SetVisible(true);
EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
}
TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) {
View* view = new View;
gfx::Transform transform;
transform.Scale(2.0, 2.0);
view->SetTransform(transform);
widget()->SetContentsView(view);
EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
ASSERT_TRUE(view->layer() != NULL);
EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
View* parent = view->parent();
parent->RemoveChildView(view);
parent->AddChildView(view);
EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
ASSERT_TRUE(view->layer() != NULL);
EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
}
TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
View* content_view = new View;
widget()->SetContentsView(content_view);
View* v1 = new View;
v1->SetPaintToLayer(true);
EXPECT_TRUE(v1->layer());
EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
v1->layer()));
content_view->AddChildView(v1);
EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
v1->layer()));
EXPECT_TRUE(v1->layer()->IsDrawn());
v1->SetVisible(false);
EXPECT_FALSE(v1->layer()->IsDrawn());
v1->SetVisible(true);
EXPECT_TRUE(v1->layer()->IsDrawn());
widget()->Hide();
EXPECT_FALSE(v1->layer()->IsDrawn());
widget()->Show();
EXPECT_TRUE(v1->layer()->IsDrawn());
}
TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
View* content_view = new View;
widget()->SetContentsView(content_view);
View* v1 = new View;
content_view->AddChildView(v1);
View* v2 = new View;
v1->AddChildView(v2);
v2->SetPaintToLayer(true);
EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
v2->layer()));
EXPECT_TRUE(v2->layer()->IsDrawn());
content_view->RemoveChildView(v1);
EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
v2->layer()));
content_view->AddChildView(v2);
delete v1;
v1 = NULL;
EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
v2->layer()));
EXPECT_TRUE(v2->layer()->IsDrawn());
}
class PaintTrackingView : public View {
public:
PaintTrackingView() : painted_(false) {
}
bool painted() const { return painted_; }
void set_painted(bool value) { painted_ = value; }
virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
painted_ = true;
}
private:
bool painted_;
DISALLOW_COPY_AND_ASSIGN(PaintTrackingView);
};
TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) {
PaintTrackingView* content_view = new PaintTrackingView;
widget()->SetContentsView(content_view);
content_view->SetPaintToLayer(true);
GetRootLayer()->GetCompositor()->ScheduleDraw();
ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
content_view->set_painted(false);
GetRootLayer()->GetCompositor()->ScheduleDraw();
ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
EXPECT_FALSE(content_view->painted());
content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
GetRootLayer()->GetCompositor()->ScheduleDraw();
ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
EXPECT_TRUE(content_view->painted());
}
TEST_F(ViewLayerTest, VisibilityChildLayers) {
View* v1 = new View;
v1->SetPaintToLayer(true);
widget()->SetContentsView(v1);
View* v2 = new View;
v1->AddChildView(v2);
View* v3 = new View;
v2->AddChildView(v3);
v3->SetVisible(false);
View* v4 = new View;
v4->SetPaintToLayer(true);
v3->AddChildView(v4);
EXPECT_TRUE(v1->layer()->IsDrawn());
EXPECT_FALSE(v4->layer()->IsDrawn());
v2->SetVisible(false);
EXPECT_TRUE(v1->layer()->IsDrawn());
EXPECT_FALSE(v4->layer()->IsDrawn());
v2->SetVisible(true);
EXPECT_TRUE(v1->layer()->IsDrawn());
EXPECT_FALSE(v4->layer()->IsDrawn());
v2->SetVisible(false);
EXPECT_TRUE(v1->layer()->IsDrawn());
EXPECT_FALSE(v4->layer()->IsDrawn());
EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
v3->SetVisible(true);
EXPECT_TRUE(v1->layer()->IsDrawn());
EXPECT_FALSE(v4->layer()->IsDrawn());
EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
v1->AddChildView(v3);
EXPECT_TRUE(v1->layer()->IsDrawn());
EXPECT_TRUE(v4->layer()->IsDrawn());
EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
}
TEST_F(ViewLayerTest, DISABLED_ViewLayerTreesInSync) {
View* content = new View;
content->SetPaintToLayer(true);
widget()->SetContentsView(content);
widget()->Show();
ConstructTree(content, 5);
EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
ScrambleTree(content);
EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
ScrambleTree(content);
EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
ScrambleTree(content);
EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
}
TEST_F(ViewLayerTest, ReorderUnderWidget) {
View* content = new View;
widget()->SetContentsView(content);
View* c1 = new View;
c1->SetPaintToLayer(true);
content->AddChildView(c1);
View* c2 = new View;
c2->SetPaintToLayer(true);
content->AddChildView(c2);
ui::Layer* parent_layer = c1->layer()->parent();
ASSERT_TRUE(parent_layer);
ASSERT_EQ(2u, parent_layer->children().size());
EXPECT_EQ(c1->layer(), parent_layer->children()[0]);
EXPECT_EQ(c2->layer(), parent_layer->children()[1]);
content->ReorderChildView(c1, -1);
EXPECT_EQ(c1->layer(), parent_layer->children()[1]);
EXPECT_EQ(c2->layer(), parent_layer->children()[0]);
}
TEST_F(ViewLayerTest, AcquireLayer) {
View* content = new View;
widget()->SetContentsView(content);
scoped_ptr<View> c1(new View);
c1->SetPaintToLayer(true);
EXPECT_TRUE(c1->layer());
content->AddChildView(c1.get());
scoped_ptr<ui::Layer> layer(c1->AcquireLayer());
EXPECT_EQ(layer.get(), c1->layer());
scoped_ptr<ui::Layer> layer2(c1->RecreateLayer());
EXPECT_NE(c1->layer(), layer2.get());
c1.reset();
}
TEST_F(ViewLayerTest, RecreateLayerScaling) {
scoped_ptr<View> v(new View());
v->SetPaintToLayer(true);
v->layer()->set_scale_content(false);
scoped_ptr<ui::Layer> old_layer(v->RecreateLayer());
ui::Layer* new_layer = v->layer();
EXPECT_FALSE(new_layer->scale_content());
}
TEST_F(ViewLayerTest, RecreateLayerZOrder) {
scoped_ptr<View> v(new View());
v->SetPaintToLayer(true);
View* v1 = new View();
v1->SetPaintToLayer(true);
v->AddChildView(v1);
View* v2 = new View();
v2->SetPaintToLayer(true);
v->AddChildView(v2);
const std::vector<ui::Layer*>& child_layers_pre = v->layer()->children();
ASSERT_EQ(2u, child_layers_pre.size());
EXPECT_EQ(v1->layer(), child_layers_pre[0]);
EXPECT_EQ(v2->layer(), child_layers_pre[1]);
scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
const std::vector<ui::Layer*>& child_layers_post = v->layer()->children();
ASSERT_EQ(3u, child_layers_post.size());
EXPECT_EQ(v1->layer(), child_layers_post[0]);
EXPECT_EQ(v1_old_layer, child_layers_post[1]);
EXPECT_EQ(v2->layer(), child_layers_post[2]);
}
TEST_F(ViewLayerTest, RecreateLayerZOrderWidgetParent) {
View* v = new View();
widget()->SetContentsView(v);
View* v1 = new View();
v1->SetPaintToLayer(true);
v->AddChildView(v1);
View* v2 = new View();
v2->SetPaintToLayer(true);
v->AddChildView(v2);
ui::Layer* root_layer = GetRootLayer();
const std::vector<ui::Layer*>& child_layers_pre = root_layer->children();
ASSERT_EQ(2u, child_layers_pre.size());
EXPECT_EQ(v1->layer(), child_layers_pre[0]);
EXPECT_EQ(v2->layer(), child_layers_pre[1]);
scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
ASSERT_EQ(3u, child_layers_post.size());
EXPECT_EQ(v1->layer(), child_layers_post[0]);
EXPECT_EQ(v1_old_layer, child_layers_post[1]);
EXPECT_EQ(v2->layer(), child_layers_post[2]);
}
TEST_F(ViewLayerTest, RecreateLayerMovesNonViewChildren) {
View v;
v.SetPaintToLayer(true);
View child;
child.SetPaintToLayer(true);
v.AddChildView(&child);
ASSERT_TRUE(v.layer() != NULL);
ASSERT_EQ(1u, v.layer()->children().size());
EXPECT_EQ(v.layer()->children()[0], child.layer());
ui::Layer layer(ui::LAYER_NOT_DRAWN);
v.layer()->Add(&layer);
v.layer()->StackAtBottom(&layer);
scoped_ptr<ui::Layer> old_layer(v.RecreateLayer());
ASSERT_TRUE(old_layer.get() != NULL);
EXPECT_TRUE(old_layer->children().empty());
ASSERT_TRUE(v.layer() != NULL);
ASSERT_EQ(2u, v.layer()->children().size());
EXPECT_EQ(v.layer()->children()[0], &layer);
EXPECT_EQ(v.layer()->children()[1], child.layer());
}
TEST_F(ViewTest, FocusableAssertions) {
TestView view;
view.SetFocusable(true);
EXPECT_TRUE(view.focusable());
view.SetEnabled(false);
EXPECT_TRUE(view.focusable());
view.SetFocusable(false);
EXPECT_FALSE(view.focusable());
}
Widget* CreateControlWidget(aura::Window* parent, const gfx::Rect& bounds) {
Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
params.parent = parent;
params.bounds = bounds;
Widget* widget = new Widget();
widget->Init(params);
return widget;
}
View* CreateViewWithLayer(const gfx::Rect& bounds,
const char* layer_name) {
View* view = new View();
view->SetBoundsRect(bounds);
view->SetPaintToLayer(true);
view->layer()->set_name(layer_name);
return view;
}
TEST_F(ViewTest, RecreateLayers) {
Widget* w1 = CreateControlWidget(GetContext(), gfx::Rect(0, 0, 100, 100));
w1->GetNativeView()->layer()->set_name("w1");
View* v2 = new View();
v2->SetBounds(0, 1, 100, 101);
View* v3 = new View();
v3->SetBounds(0, 2, 100, 102);
View* w2_host_view = new View();
View* v1 = CreateViewWithLayer(gfx::Rect(0, 3, 100, 103), "v1");
ui::Layer* v1_layer = v1->layer();
w1->GetRootView()->AddChildView(v1);
w1->GetRootView()->AddChildView(v2);
v2->AddChildView(v3);
View* v4 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v4");
ui::Layer* v4_layer = v4->layer();
v2->AddChildView(v4);
w1->GetRootView()->AddChildView(w2_host_view);
View* v7 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v7");
ui::Layer* v7_layer = v7->layer();
w1->GetRootView()->AddChildView(v7);
View* v8 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v8");
ui::Layer* v8_layer = v8->layer();
v7->AddChildView(v8);
View* v9 = CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v9");
ui::Layer* v9_layer = v9->layer();
v7->AddChildView(v9);
Widget* w2 = CreateControlWidget(w1->GetNativeView(),
gfx::Rect(0, 5, 100, 105));
w2->GetNativeView()->layer()->set_name("w2");
w2->GetNativeView()->SetProperty(kHostViewKey, w2_host_view);
View* v5 = CreateViewWithLayer(gfx::Rect(0, 6, 100, 106), "v5");
w2->GetRootView()->AddChildView(v5);
View* v6 = CreateViewWithLayer(gfx::Rect(0, 7, 100, 107), "v6");
ui::Layer* v6_layer = v6->layer();
v5->AddChildView(v6);
ui::Layer* w1_layer = w1->GetNativeView()->layer();
ASSERT_EQ("w1", w1_layer->name());
ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer));
ui::Layer* w2_layer = w1_layer->children()[2];
ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer));
ui::Layer* v5_layer = w2_layer->children()[0];
ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer));
{
scoped_ptr<ui::LayerTreeOwner> cloned_owner(
wm::RecreateLayers(w1->GetNativeView()));
EXPECT_EQ(w1_layer, cloned_owner->root());
EXPECT_NE(w1_layer, w1->GetNativeView()->layer());
ASSERT_EQ("w1", w1_layer->name());
ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer));
ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer));
ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer));
EXPECT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_layer));
ASSERT_EQ(4u, w1_layer->children().size());
EXPECT_EQ(v1_layer, w1_layer->children()[0]);
EXPECT_EQ(v4_layer, w1_layer->children()[1]);
EXPECT_EQ(w2_layer, w1_layer->children()[2]);
EXPECT_EQ(v7_layer, w1_layer->children()[3]);
ASSERT_EQ(1u, w2_layer->children().size());
EXPECT_EQ(v5_layer, w2_layer->children()[0]);
ASSERT_EQ(1u, v5_layer->children().size());
EXPECT_EQ(v6_layer, v5_layer->children()[0]);
ASSERT_EQ(0u, v6_layer->children().size());
EXPECT_EQ(2u, v7_layer->children().size());
EXPECT_EQ(v8_layer, v7_layer->children()[0]);
EXPECT_EQ(v9_layer, v7_layer->children()[1]);
ui::Layer* w1_new_layer = w1->GetNativeView()->layer();
EXPECT_EQ("w1", w1_new_layer->name());
ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_new_layer));
ui::Layer* w2_new_layer = w1_new_layer->children()[2];
ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_new_layer));
ui::Layer* v5_new_layer = w2_new_layer->children()[0];
ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_new_layer));
ui::Layer* v7_new_layer = w1_new_layer->children()[3];
ASSERT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_new_layer));
}
}
TEST_F(ViewTest, UpdateViewStorageOnDelete) {
ViewStorage* view_storage = ViewStorage::GetInstance();
const int storage_id = view_storage->CreateStorageID();
{
View view;
view_storage->StoreView(storage_id, &view);
}
EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL);
}
}