1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/aura/test/aura_test_base.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/aura/test/test_windows.h"
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/aura/window.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/compositor/layer.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/compositor/test/test_layers.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/views/view.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/views/view_constants_aura.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/views/widget/widget.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace views {
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Creates a control widget with the passed in parameters.
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The caller takes ownership of the returned widget.
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Widget* CreateControlWidget(aura::Window* parent, const gfx::Rect& bounds) {
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  params.parent = parent;
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  params.bounds = bounds;
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Widget* widget = new Widget();
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  widget->Init(params);
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return widget;
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Sets the name of |window| and |window|'s layer to |name|.
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void SetWindowAndLayerName(aura::Window* window, const std::string& name) {
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  window->SetName(name);
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  window->layer()->set_name(name);
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns a string containing the name of each of the child windows (bottommost
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// first) of |parent|. The format of the string is "name1 name2 name3 ...".
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::string ChildWindowNamesAsString(const aura::Window& parent) {
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string names;
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef std::vector<aura::Window*> Windows;
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (Windows::const_iterator it = parent.children().begin();
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)       it != parent.children().end(); ++it) {
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (!names.empty())
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      names += " ";
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    names += (*it)->name();
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return names;
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef aura::test::AuraTestBase WindowReordererTest;
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test that views with layers and views with associated windows are reordered
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// according to the view hierarchy.
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(WindowReordererTest, Basic) {
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<Widget> parent(CreateControlWidget(root_window(),
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                gfx::Rect(0, 0, 100, 100)));
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->Show();
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  aura::Window* parent_window = parent->GetNativeWindow();
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* contents_view = new View();
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->SetContentsView(contents_view);
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // 1) Test that layers for views and layers for windows associated to a host
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // view are stacked below the layers for any windows not associated to a host
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // view.
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* v = new View();
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v->SetPaintToLayer(true);
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v->layer()->set_name("v");
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildView(v);
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<Widget> w1(CreateControlWidget(parent_window,
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                            gfx::Rect(0, 1, 100, 101)));
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetWindowAndLayerName(w1->GetNativeView(), "w1");
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w1->Show();
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<Widget> w2(CreateControlWidget(parent_window,
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                            gfx::Rect(0, 2, 100, 102)));
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetWindowAndLayerName(w2->GetNativeView(), "w2");
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w2->Show();
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("v w1 w2",
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* host_view2 = new View();
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildView(host_view2);
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w2->GetNativeView()->SetProperty(kHostViewKey, host_view2);
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("v w2 w1",
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* host_view1 = new View();
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w1->GetNativeView()->SetProperty(kHostViewKey, host_view1);
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildViewAt(host_view1, 0);
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 v w2",
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // 2) Test the z-order of the windows and layers as a result of reordering the
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // views.
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->ReorderChildView(host_view1, -1);
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("v w2 w1",
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->ReorderChildView(host_view2, -1);
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("v w1 w2",
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // 3) Test the z-order of the windows and layers as a result of reordering the
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // views in situations where the window order remains unchanged.
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->ReorderChildView(v, -1);
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2 v",
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->ReorderChildView(host_view2, -1);
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 v w2",
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Work around for bug in NativeWidgetAura.
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO: fix bug and remove this.
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->Close();
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test that different orderings of:
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// - adding a window to a parent widget
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// - adding a "host" view to a parent widget
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// - associating the "host" view and window
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// all correctly reorder the child windows and layers.
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(WindowReordererTest, Association) {
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<Widget> parent(CreateControlWidget(root_window(),
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                gfx::Rect(0, 0, 100, 100)));
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->Show();
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  aura::Window* parent_window = parent->GetNativeWindow();
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* contents_view = new View();
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->SetContentsView(contents_view);
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  aura::Window* w1 = aura::test::CreateTestWindowWithId(0,
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      parent->GetNativeWindow());
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetWindowAndLayerName(w1, "w1");
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  aura::Window* w2 = aura::test::CreateTestWindowWithId(0, NULL);
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetWindowAndLayerName(w2, "w2");
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* host_view2 = new View();
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // 1) Test that parenting the window to the parent widget last results in a
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //    correct ordering of child windows and layers.
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildView(host_view2);
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w2->SetProperty(views::kHostViewKey, host_view2);
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1", ChildWindowNamesAsString(*parent_window));
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1",
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent_window->AddChild(w2);
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1",
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // 2) Test that associating the window and "host" view last results in a
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // correct ordering of child windows and layers.
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* host_view1 = new View();
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildViewAt(host_view1, 0);
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1",
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w1->SetProperty(views::kHostViewKey, host_view1);
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w1 w2",
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // 3) Test that parenting the "host" view to the parent widget last results
177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // in a correct ordering of child windows and layers.
178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->RemoveChildView(host_view2);
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildViewAt(host_view2, 0);
180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w2 w1",
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Work around for bug in NativeWidgetAura.
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO: fix bug and remove this.
186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->Close();
187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// It is possible to associate a window to a view which has a parent layer
190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// (other than the widget layer). In this case, the parent layer of the host
191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// view and the parent layer of the associated window are different. Test that
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// the layers and windows are properly reordered in this case.
193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(WindowReordererTest, HostViewParentHasLayer) {
194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<Widget> parent(CreateControlWidget(root_window(),
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                gfx::Rect(0, 0, 100, 100)));
196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->Show();
197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  aura::Window* parent_window = parent->GetNativeWindow();
198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* contents_view = new View();
200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->SetContentsView(contents_view);
201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Create the following view hierarchy. (*) denotes views which paint to a
203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // layer.
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // contents_view
206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // +-- v1
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //     +-- v11*
208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //     +-- v12 (attached window)
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //     +-- v13*
210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // +--v2*
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* v1 = new View();
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildView(v1);
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* v11 = new View();
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v11->SetPaintToLayer(true);
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v11->layer()->set_name("v11");
218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v1->AddChildView(v11);
219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<Widget> w(CreateControlWidget(parent_window,
221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                           gfx::Rect(0, 1, 100, 101)));
222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetWindowAndLayerName(w->GetNativeView(), "w");
223868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w->Show();
224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* v12 = new View();
226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v1->AddChildView(v12);
227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  w->GetNativeView()->SetProperty(kHostViewKey, v12);
228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* v13 = new View();
230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v13->SetPaintToLayer(true);
231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v13->layer()->set_name("v13");
232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v1->AddChildView(v13);
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  View* v2 = new View();
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v2->SetPaintToLayer(true);
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v2->layer()->set_name("v2");
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  contents_view->AddChildView(v2);
238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test intial state.
240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("v11 w v13 v2",
242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |w|'s layer should be stacked above |v1|'s layer.
245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v1->SetPaintToLayer(true);
246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v1->layer()->set_name("v1");
247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("v1 w v2",
249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test moving the host view from one view with a layer to another.
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  v2->AddChildView(v12);
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("v1 v2 w",
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Work around for bug in NativeWidgetAura.
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO: fix bug and remove this.
259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  parent->Close();
260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
263868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace views
264