This source file includes following definitions.
- Init
- SetUp
- root_window
- TEST_F
- TEST_F
- TEST_F
- OnWindowResized
- OnWindowAddedToLayout
- OnWillRemoveWindowFromLayout
- OnWindowRemovedFromLayout
- OnChildWindowVisibilityChanged
- SetChildBounds
- did_size_change_more_than_once
- OnNativeWidgetSizeChanged
- TEST_F
- TEST_F
- consume_gesture_event_
- set_consume_gesture_event
- clear_got_gesture_event
- got_gesture_event
- OnGestureEvent
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- ClearGotMove
- got_move
- OnWidgetMove
- TEST_F
#include "ui/views/widget/native_widget_aura.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/env.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/events/event.h"
#include "ui/gfx/screen.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/widget_delegate.h"
namespace views {
namespace {
NativeWidgetAura* Init(aura::Window* parent, Widget* widget) {
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = parent;
widget->Init(params);
return static_cast<NativeWidgetAura*>(widget->native_widget());
}
class NativeWidgetAuraTest : public ViewsTestBase {
public:
NativeWidgetAuraTest() {}
virtual ~NativeWidgetAuraTest() {}
virtual void SetUp() OVERRIDE {
ViewsTestBase::SetUp();
host()->SetBounds(gfx::Rect(640, 480));
}
protected:
aura::Window* root_window() { return GetContext(); }
private:
DISALLOW_COPY_AND_ASSIGN(NativeWidgetAuraTest);
};
TEST_F(NativeWidgetAuraTest, CenterWindowLargeParent) {
scoped_ptr<aura::Window> parent(new aura::Window(NULL));
parent->Init(aura::WINDOW_LAYER_NOT_DRAWN);
parent->SetBounds(gfx::Rect(0, 0, 1024, 800));
scoped_ptr<Widget> widget(new Widget());
NativeWidgetAura* window = Init(parent.get(), widget.get());
window->CenterWindow(gfx::Size(100, 100));
EXPECT_EQ(gfx::Rect( (640 - 100) / 2,
(480 - 100) / 2,
100, 100),
window->GetNativeWindow()->bounds());
widget->CloseNow();
}
TEST_F(NativeWidgetAuraTest, CenterWindowSmallParent) {
scoped_ptr<aura::Window> parent(new aura::Window(NULL));
parent->Init(aura::WINDOW_LAYER_NOT_DRAWN);
parent->SetBounds(gfx::Rect(0, 0, 480, 320));
scoped_ptr<Widget> widget(new Widget());
NativeWidgetAura* window = Init(parent.get(), widget.get());
window->CenterWindow(gfx::Size(100, 100));
EXPECT_EQ(gfx::Rect( (480 - 100) / 2,
(320 - 100) / 2,
100, 100),
window->GetNativeWindow()->bounds());
widget->CloseNow();
}
TEST_F(NativeWidgetAuraTest, CenterWindowSmallParentNotAtOrigin) {
scoped_ptr<aura::Window> parent(new aura::Window(NULL));
parent->Init(aura::WINDOW_LAYER_NOT_DRAWN);
parent->SetBounds(gfx::Rect(20, 40, 480, 320));
scoped_ptr<Widget> widget(new Widget());
NativeWidgetAura* window = Init(parent.get(), widget.get());
window->CenterWindow(gfx::Size(500, 600));
EXPECT_EQ("20,40 480x320", window->GetNativeWindow()->bounds().ToString());
widget->CloseNow();
}
class TestLayoutManager : public aura::LayoutManager {
public:
TestLayoutManager() {}
virtual void OnWindowResized() OVERRIDE {
}
virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
SetChildBoundsDirect(child, gfx::Rect(0, 0, 300, 300));
}
virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {
}
virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {
}
virtual void OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) OVERRIDE {
}
virtual void SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) OVERRIDE {
}
private:
DISALLOW_COPY_AND_ASSIGN(TestLayoutManager);
};
class TestWidget : public views::Widget {
public:
TestWidget() : did_size_change_more_than_once_(false) {
}
bool did_size_change_more_than_once() const {
return did_size_change_more_than_once_;
}
virtual void OnNativeWidgetSizeChanged(const gfx::Size& new_size) OVERRIDE {
if (last_size_.IsEmpty())
last_size_ = new_size;
else if (!did_size_change_more_than_once_ && new_size != last_size_)
did_size_change_more_than_once_ = true;
Widget::OnNativeWidgetSizeChanged(new_size);
}
private:
bool did_size_change_more_than_once_;
gfx::Size last_size_;
DISALLOW_COPY_AND_ASSIGN(TestWidget);
};
TEST_F(NativeWidgetAuraTest, ShowMaximizedDoesntBounceAround) {
root_window()->SetBounds(gfx::Rect(0, 0, 640, 480));
root_window()->SetLayoutManager(new TestLayoutManager);
scoped_ptr<TestWidget> widget(new TestWidget());
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = NULL;
params.context = root_window();
params.show_state = ui::SHOW_STATE_MAXIMIZED;
params.bounds = gfx::Rect(10, 10, 100, 200);
widget->Init(params);
EXPECT_FALSE(widget->did_size_change_more_than_once());
widget->CloseNow();
}
TEST_F(NativeWidgetAuraTest, GetClientAreaScreenBounds) {
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.context = root_window();
params.bounds.SetRect(10, 20, 300, 400);
scoped_ptr<Widget> widget(new Widget());
widget->Init(params);
gfx::Rect client_bounds = widget->GetClientAreaBoundsInScreen();
EXPECT_EQ(10, client_bounds.x());
EXPECT_EQ(20, client_bounds.y());
EXPECT_EQ(300, client_bounds.width());
EXPECT_EQ(400, client_bounds.height());
}
namespace {
class GestureTrackingView : public views::View {
public:
GestureTrackingView()
: got_gesture_event_(false),
consume_gesture_event_(true) {}
void set_consume_gesture_event(bool value) {
consume_gesture_event_ = value;
}
void clear_got_gesture_event() {
got_gesture_event_ = false;
}
bool got_gesture_event() const {
return got_gesture_event_;
}
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
got_gesture_event_ = true;
if (consume_gesture_event_)
event->StopPropagation();
}
private:
bool got_gesture_event_;
bool consume_gesture_event_;
DISALLOW_COPY_AND_ASSIGN(GestureTrackingView);
};
}
TEST_F(NativeWidgetAuraTest, DontCaptureOnGesture) {
GestureTrackingView* view = new GestureTrackingView();
GestureTrackingView* child = new GestureTrackingView();
child->set_consume_gesture_event(false);
view->SetLayoutManager(new FillLayout);
view->AddChildView(child);
scoped_ptr<TestWidget> widget(new TestWidget());
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.context = root_window();
params.bounds = gfx::Rect(0, 0, 100, 200);
widget->Init(params);
widget->SetContentsView(view);
widget->Show();
ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(41, 51), 1,
base::TimeDelta());
ui::EventDispatchDetails details =
event_processor()->OnEventFromSource(&press);
ASSERT_FALSE(details.dispatcher_destroyed);
EXPECT_TRUE(view->got_gesture_event());
EXPECT_TRUE(child->got_gesture_event());
view->clear_got_gesture_event();
child->clear_got_gesture_event();
EXPECT_FALSE(widget->HasCapture());
ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(250, 251), 1,
base::TimeDelta());
details = event_processor()->OnEventFromSource(&release);
ASSERT_FALSE(details.dispatcher_destroyed);
EXPECT_TRUE(view->got_gesture_event());
EXPECT_FALSE(child->got_gesture_event());
view->clear_got_gesture_event();
widget->Close();
}
TEST_F(NativeWidgetAuraTest, PreferViewLayersToChildWindows) {
views::View* parent_root = new views::View;
scoped_ptr<Widget> parent(new Widget());
Widget::InitParams parent_params(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
parent_params.ownership =
views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
parent_params.context = root_window();
parent->Init(parent_params);
parent->SetContentsView(parent_root);
parent->SetBounds(gfx::Rect(0, 0, 400, 400));
parent->Show();
scoped_ptr<Widget> child(new Widget());
Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
child_params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
child_params.parent = parent->GetNativeWindow();
child->Init(child_params);
child->SetBounds(gfx::Rect(0, 0, 200, 200));
child->Show();
EXPECT_EQ(child->GetNativeWindow(),
parent->GetNativeWindow()->GetEventHandlerForPoint(
gfx::Point(50, 50)));
views::View* view_with_layer = new views::View;
parent_root->AddChildView(view_with_layer);
view_with_layer->SetBounds(0, 0, 50, 50);
view_with_layer->SetPaintToLayer(true);
EXPECT_EQ(child->GetNativeWindow(),
parent->GetNativeWindow()->GetEventHandlerForPoint(
gfx::Point(20, 20)));
view_with_layer->layer()->parent()->StackAtTop(
view_with_layer->layer());
EXPECT_EQ(parent->GetNativeWindow(),
parent->GetNativeWindow()->GetEventHandlerForPoint(
gfx::Point(20, 20)));
EXPECT_EQ(child->GetNativeWindow(),
parent->GetNativeWindow()->GetEventHandlerForPoint(
gfx::Point(70, 70)));
delete view_with_layer;
view_with_layer = NULL;
EXPECT_EQ(child->GetNativeWindow(),
parent->GetNativeWindow()->GetEventHandlerForPoint(
gfx::Point(20, 20)));
parent->Close();
}
TEST_F(NativeWidgetAuraTest, FlashFrame) {
scoped_ptr<Widget> widget(new Widget());
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.context = root_window();
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
aura::Window* window = widget->GetNativeWindow();
EXPECT_FALSE(window->GetProperty(aura::client::kDrawAttentionKey));
widget->FlashFrame(true);
EXPECT_TRUE(window->GetProperty(aura::client::kDrawAttentionKey));
widget->FlashFrame(false);
EXPECT_FALSE(window->GetProperty(aura::client::kDrawAttentionKey));
widget->FlashFrame(true);
EXPECT_TRUE(window->GetProperty(aura::client::kDrawAttentionKey));
widget->Activate();
EXPECT_FALSE(window->GetProperty(aura::client::kDrawAttentionKey));
}
TEST_F(NativeWidgetAuraTest, NoCrashOnThemeAfterClose) {
scoped_ptr<aura::Window> parent(new aura::Window(NULL));
parent->Init(aura::WINDOW_LAYER_NOT_DRAWN);
parent->SetBounds(gfx::Rect(0, 0, 480, 320));
scoped_ptr<Widget> widget(new Widget());
NativeWidgetAura* window = Init(parent.get(), widget.get());
window->Show();
window->Close();
base::MessageLoop::current()->RunUntilIdle();
widget->GetNativeTheme();
}
class MoveTestWidgetDelegate : public WidgetDelegateView {
public:
MoveTestWidgetDelegate() : got_move_(false) {}
virtual ~MoveTestWidgetDelegate() {}
void ClearGotMove() { got_move_ = false; }
bool got_move() const { return got_move_; }
virtual void OnWidgetMove() OVERRIDE { got_move_ = true; }
private:
bool got_move_;
DISALLOW_COPY_AND_ASSIGN(MoveTestWidgetDelegate);
};
TEST_F(NativeWidgetAuraTest, OnWidgetMovedInvokedAfterAcquireLayer) {
MoveTestWidgetDelegate* delegate = new MoveTestWidgetDelegate;
Widget* widget =
Widget::CreateWindowWithContextAndBounds(delegate,
root_window(),
gfx::Rect(10, 10, 100, 200));
widget->Show();
delegate->ClearGotMove();
delete widget->GetNativeView()->RecreateLayer().release();
widget->SetBounds(gfx::Rect(0, 0, 500, 500));
EXPECT_TRUE(delegate->got_move());
widget->CloseNow();
}
}
}