Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: ui/views/widget/native_widget_layer_reorderer_aura_unittest.cc

Issue 15114002: Reorder the NativeViews attached to a view via kViewHostKey according to the position of the view (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes as requested Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/aura/root_window.h"
6 #include "ui/aura/test/aura_test_base.h"
7 #include "ui/aura/test/test_windows.h"
8 #include "ui/aura/window.h"
9 #include "ui/compositor/layer.h"
10 #include "ui/compositor/test/test_layers.h"
11 #include "ui/views/view.h"
12 #include "ui/views/view_constants_aura.h"
13 #include "ui/views/widget/widget.h"
14
15 namespace views {
16 namespace {
17
18 // Creates a control widget with the passed in parameters.
19 // The caller takes ownership of the returned widget.
20 Widget* CreateControlWidget(aura::Window* parent, const gfx::Rect& bounds) {
21 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
22 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
23 params.parent = parent;
24 params.bounds = bounds;
25 Widget* widget = new Widget();
26 widget->Init(params);
27 return widget;
28 }
29
30 // Sets the name of |window| and |window|'s layer to |name|.
31 void SetWindowAndLayerName(aura::Window* window, const std::string& name) {
32 window->SetName(name);
33 window->layer()->set_name(name);
34 }
35
36 // Returns a string containing the name of each of the child windows (bottommost
37 // first) of |parent|. The format of the string is "name1 name2 name3 ...".
38 std::string ChildWindowNamesAsString(const aura::Window& parent) {
39 std::string names;
40 typedef std::vector<aura::Window*> Windows;
41 for (Windows::const_iterator it = parent.children().begin();
42 it != parent.children().end(); ++it) {
43 if (!names.empty())
44 names += " ";
45 names += (*it)->name();
46 }
47 return names;
48 }
49
50 typedef aura::test::AuraTestBase NativeWidgetLayerReordererTest;
51
52 // Test that views with layers and views with attached windows are reordered
53 // according to the view hierarchy.
54 TEST_F(NativeWidgetLayerReordererTest, Basic) {
55 scoped_ptr<Widget> parent(CreateControlWidget(root_window(),
56 gfx::Rect(0, 0, 100, 100)));
57 parent->Show();
58 aura::Window* parent_window = parent->GetNativeWindow();
59
60 View* contents_view = new View();
61 parent->SetContentsView(contents_view);
62
63 // 1) Test that layers for views and layers for windows attached to a host
64 // view are stacked below the layers for any windows not attached to a host
65 // view.
66 View* v = new View();
67 v->SetPaintToLayer(true);
68 v->layer()->set_name("v");
69 contents_view->AddChildView(v);
70
71 scoped_ptr<Widget> w1(CreateControlWidget(parent_window,
72 gfx::Rect(0, 1, 100, 101)));
73 SetWindowAndLayerName(w1->GetNativeView(), "w1");
74 w1->Show();
75 scoped_ptr<Widget> w2(CreateControlWidget(parent_window,
76 gfx::Rect(0, 2, 100, 102)));
77 SetWindowAndLayerName(w2->GetNativeView(), "w2");
78 w2->Show();
79
80 EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
81 EXPECT_EQ("v w1 w2",
82 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
83
84 View* host_view2 = new View();
85 contents_view->AddChildView(host_view2);
86 w2->GetNativeView()->SetProperty(kHostViewKey, host_view2);
87 EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
88 EXPECT_EQ("v w2 w1",
89 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
90
91 View* host_view1 = new View();
92 w1->GetNativeView()->SetProperty(kHostViewKey, host_view1);
93 contents_view->AddChildViewAt(host_view1, 0);
94 EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
95 EXPECT_EQ("w1 v w2",
96 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
97
98 // 2) Test the z-order of the windows and layers as a result of reordering the
99 // views.
100 contents_view->RemoveChildView(host_view1);
101 contents_view->AddChildView(host_view1);
102 EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
103 EXPECT_EQ("v w2 w1",
104 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
105
106 contents_view->ReorderChildView(host_view2, -1);
107 EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
108 EXPECT_EQ("v w1 w2",
109 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
110
111 // 3) Test the z-order of the windows and layers as a result of reordering the
112 // views in situations where the window order remains unchanged.
113 contents_view->ReorderChildView(v, -1);
114 EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
115 EXPECT_EQ("w1 w2 v",
116 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
117
118 contents_view->ReorderChildView(host_view2, -1);
119 EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
120 EXPECT_EQ("w1 v w2",
121 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
122
123 // Work around for bug in NativeWidgetAura.
124 // TODO: fix bug and remove this.
125 parent->Close();
126 }
127
128 // It is possible to attach a window to a view which has a parent layer (other
129 // than the widget layer). In this case, the parent layer of the host view and
130 // the parent layer of the attached window are different. Test that the layers
131 // and windows are properly reordered in this case.
132 TEST_F(NativeWidgetLayerReordererTest, HostViewParentHasLayer) {
133 scoped_ptr<Widget> parent(CreateControlWidget(root_window(),
134 gfx::Rect(0, 0, 100, 100)));
135 parent->Show();
136 aura::Window* parent_window = parent->GetNativeWindow();
137
138 View* contents_view = new View();
139 parent->SetContentsView(contents_view);
140
141 // Create the following view hierarchy. (*) denotes views which paint to a
142 // layer.
143 //
144 // contents_view
145 // +-- v1
146 // +-- v11*
147 // +-- v12 (attached window)
148 // +-- v13*
149 // +--v2*
150
151 View* v1 = new View();
152 contents_view->AddChildView(v1);
153
154 View* v11 = new View();
155 v11->SetPaintToLayer(true);
156 v11->layer()->set_name("v11");
157 v1->AddChildView(v11);
158
159 scoped_ptr<Widget> w(CreateControlWidget(parent_window,
160 gfx::Rect(0, 1, 100, 101)));
161 SetWindowAndLayerName(w->GetNativeView(), "w");
162 w->Show();
163
164 View* v12 = new View();
165 v1->AddChildView(v12);
166 w->GetNativeView()->SetProperty(kHostViewKey, v12);
167
168 View* v13 = new View();
169 v13->SetPaintToLayer(true);
170 v13->layer()->set_name("v13");
171 v1->AddChildView(v13);
172
173 View* v2 = new View();
174 v2->SetPaintToLayer(true);
175 v2->layer()->set_name("v2");
176 contents_view->AddChildView(v2);
177
178 // Test intial state.
179 EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
180 EXPECT_EQ("v11 w v13 v2",
181 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
182
183 // |w|'s layer should be stacked above |v1|'s layer.
184 v1->SetPaintToLayer(true);
185 v1->layer()->set_name("v1");
186 EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
187 EXPECT_EQ("v1 w v2",
188 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
189
190 // Test moving the host view from one view with a layer to another.
191 v2->AddChildView(v12);
192 EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
193 EXPECT_EQ("v1 v2 w",
194 ui::test::ChildLayerNamesAsString(*parent_window->layer()));
195
196 // Work around for bug in NativeWidgetAura.
197 // TODO: fix bug and remove this.
198 parent->Close();
199 }
200
201 } // namespace
202 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698