This source file includes following definitions.
- SetUp
- CreateChildForHost
- toplevel
- ResetCount
- notification_count
- NativeViewHierarchyChanged
- GetNativeParent
- ResetParentChanges
- num_parent_changes
- ViewHierarchyChanged
- TEST_F
- TEST_F
- TEST_F
#include "ui/views/controls/native/native_view_host.h"
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "ui/aura/window.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
namespace views {
class NativeViewHostTest : public ViewsTestBase {
public:
NativeViewHostTest() {
}
virtual void SetUp() OVERRIDE {
ViewsTestBase::SetUp();
toplevel_.reset(new Widget);
Widget::InitParams toplevel_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
toplevel_->Init(toplevel_params);
}
Widget* CreateChildForHost(gfx::NativeView native_parent_view,
View* parent_view,
View* contents_view,
NativeViewHost* host) {
Widget* child = new Widget;
Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
child_params.parent = native_parent_view;
child->Init(child_params);
child->SetContentsView(contents_view);
parent_view->AddChildView(host);
host->Attach(child->GetNativeView());
return child;
}
Widget* toplevel() {
return toplevel_.get();
}
private:
scoped_ptr<Widget> toplevel_;
DISALLOW_COPY_AND_ASSIGN(NativeViewHostTest);
};
namespace {
class NativeViewHierarchyChangedTestView : public View {
public:
NativeViewHierarchyChangedTestView() : notification_count_(0) {
}
void ResetCount() {
notification_count_ = 0;
}
int notification_count() const { return notification_count_; }
virtual void NativeViewHierarchyChanged() OVERRIDE {
++notification_count_;
View::NativeViewHierarchyChanged();
}
private:
int notification_count_;
DISALLOW_COPY_AND_ASSIGN(NativeViewHierarchyChangedTestView);
};
aura::Window* GetNativeParent(aura::Window* window) {
return window->parent();
}
class ViewHierarchyChangedTestHost : public NativeViewHost {
public:
ViewHierarchyChangedTestHost()
: num_parent_changes_(0) {
}
void ResetParentChanges() {
num_parent_changes_ = 0;
}
int num_parent_changes() const {
return num_parent_changes_;
}
virtual void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) OVERRIDE {
gfx::NativeView parent_before = native_view() ?
GetNativeParent(native_view()) : NULL;
NativeViewHost::ViewHierarchyChanged(details);
gfx::NativeView parent_after = native_view() ?
GetNativeParent(native_view()) : NULL;
if (parent_before != parent_after)
++num_parent_changes_;
}
private:
int num_parent_changes_;
DISALLOW_COPY_AND_ASSIGN(ViewHierarchyChangedTestHost);
};
}
TEST_F(NativeViewHostTest, NativeViewHierarchyChanged) {
NativeViewHierarchyChangedTestView* test_view =
new NativeViewHierarchyChangedTestView;
NativeViewHost* host = new NativeViewHost;
scoped_ptr<Widget> child(CreateChildForHost(toplevel()->GetNativeView(),
toplevel()->GetRootView(),
test_view,
host));
EXPECT_EQ(0, test_view->notification_count());
test_view->ResetCount();
host->Detach();
EXPECT_EQ(1, test_view->notification_count());
EXPECT_NE(toplevel()->GetNativeView(),
GetNativeParent(child->GetNativeView()));
test_view->ResetCount();
host->Attach(child->GetNativeView());
EXPECT_EQ(1, test_view->notification_count());
EXPECT_EQ(toplevel()->GetNativeView(),
GetNativeParent(child->GetNativeView()));
}
TEST_F(NativeViewHostTest, ViewHierarchyChangedForHost) {
NativeViewHost* host0 = new NativeViewHost;
scoped_ptr<Widget> child0(CreateChildForHost(toplevel()->GetNativeView(),
toplevel()->GetRootView(),
new View,
host0));
NativeViewHost* host1 = new NativeViewHost;
scoped_ptr<Widget> child1(CreateChildForHost(toplevel()->GetNativeView(),
toplevel()->GetRootView(),
new View,
host1));
ViewHierarchyChangedTestHost* test_host = new ViewHierarchyChangedTestHost;
scoped_ptr<Widget> test_child(CreateChildForHost(host0->native_view(),
host0,
new View,
test_host));
test_host->ResetParentChanges();
EXPECT_EQ(0, test_host->num_parent_changes());
host0->RemoveChildView(test_host);
EXPECT_EQ(1, test_host->num_parent_changes());
test_host->ResetParentChanges();
EXPECT_EQ(0, test_host->num_parent_changes());
host0->AddChildView(test_host);
EXPECT_EQ(1, test_host->num_parent_changes());
test_host->ResetParentChanges();
EXPECT_EQ(0, test_host->num_parent_changes());
host1->AddChildView(test_host);
EXPECT_EQ(0, test_host->num_parent_changes());
test_host->ResetParentChanges();
EXPECT_EQ(0, test_host->num_parent_changes());
child0->GetContentsView()->AddChildView(test_host);
EXPECT_EQ(2, test_host->num_parent_changes());
}
TEST_F(NativeViewHostTest, ViewHierarchyChangedForHostParent) {
View* view0 = new View;
toplevel()->GetRootView()->AddChildView(view0);
View* view1 = new View;
toplevel()->GetRootView()->AddChildView(view1);
ViewHierarchyChangedTestHost* host0 = new ViewHierarchyChangedTestHost;
scoped_ptr<Widget> child0(CreateChildForHost(toplevel()->GetNativeView(),
view0,
new View,
host0));
ViewHierarchyChangedTestHost* host1 = new ViewHierarchyChangedTestHost;
scoped_ptr<Widget> child1(CreateChildForHost(toplevel()->GetNativeView(),
view1,
new View,
host1));
host0->ResetParentChanges();
EXPECT_EQ(0, host0->num_parent_changes());
toplevel()->GetRootView()->RemoveChildView(view0);
EXPECT_EQ(1, host0->num_parent_changes());
host0->ResetParentChanges();
EXPECT_EQ(0, host0->num_parent_changes());
toplevel()->GetRootView()->AddChildView(view0);
EXPECT_EQ(1, host0->num_parent_changes());
host0->ResetParentChanges();
host1->ResetParentChanges();
EXPECT_EQ(0, host0->num_parent_changes());
EXPECT_EQ(0, host1->num_parent_changes());
view1->AddChildView(view0);
EXPECT_EQ(0, host0->num_parent_changes());
EXPECT_EQ(0, host1->num_parent_changes());
toplevel()->GetRootView()->AddChildView(view0);
host0->ResetParentChanges();
host1->ResetParentChanges();
EXPECT_EQ(0, host0->num_parent_changes());
EXPECT_EQ(0, host1->num_parent_changes());
child0->GetContentsView()->AddChildView(view1);
EXPECT_EQ(0, host0->num_parent_changes());
EXPECT_EQ(2, host1->num_parent_changes());
}
}