File: window_reorderer_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (265 lines) | stat: -rw-r--r-- 10,421 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/memory/raw_ptr.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/test/test_layers.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/view.h"
#include "ui/views/view_constants_aura.h"
#include "ui/views/widget/widget.h"

namespace views {
namespace {

// Sets the name of |window| and |window|'s layer to |name|.
void SetWindowAndLayerName(aura::Window* window, const std::string& name) {
  window->SetName(name);
  window->layer()->SetName(name);
}

// Returns a string containing the name of each of the child windows (bottommost
// first) of |parent|. The format of the string is "name1 name2 name3 ...".
std::string ChildWindowNamesAsString(const aura::Window& parent) {
  std::string names;
  for (const aura::Window* child : parent.children()) {
    if (!names.empty()) {
      names += " ";
    }
    names += child->GetName();
  }
  return names;
}

class WindowReordererTest : public ViewsTestBase {
 protected:
  std::unique_ptr<Widget> CreateControlWidget(aura::Window* parent) {
    Widget::InitParams params =
        CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
                                  Widget::InitParams::TYPE_CONTROL);
    params.parent = parent;
    return CreateTestWidget(std::move(params));
  }
};

// Test that views with layers and views with associated windows are reordered
// according to the view hierarchy.
TEST_F(WindowReordererTest, Basic) {
  std::unique_ptr<Widget> parent = CreateControlWidget(root_window());
  parent->Show();
  aura::Window* parent_window = parent->GetNativeWindow();

  View* contents_view = parent->SetContentsView(std::make_unique<View>());

  // 1) Test that layers for views and layers for windows associated to a host
  // view are stacked below the layers for any windows not associated to a host
  // view.
  auto* v = contents_view->AddChildView(std::make_unique<View>());
  v->SetPaintToLayer();
  v->layer()->SetName("v");

  std::unique_ptr<Widget> w1 = CreateControlWidget(parent_window);
  SetWindowAndLayerName(w1->GetNativeView(), "w1");
  w1->Show();
  std::unique_ptr<Widget> w2 = CreateControlWidget(parent_window);
  SetWindowAndLayerName(w2->GetNativeView(), "w2");
  w2->Show();

  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("v w1 w2",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  auto* host_view2 = contents_view->AddChildView(std::make_unique<View>());
  w2->GetNativeView()->SetProperty(kHostViewKey, host_view2);
  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("v w2 w1",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  auto* host_view1 = contents_view->AddChildViewAt(std::make_unique<View>(), 0);
  w1->GetNativeView()->SetProperty(kHostViewKey, host_view1);
  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w1 v w2",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  // 2) Test the z-order of the windows and layers as a result of reordering the
  // views.
  contents_view->ReorderChildView(host_view1, contents_view->children().size());
  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("v w2 w1",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  contents_view->ReorderChildView(host_view2, contents_view->children().size());
  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("v w1 w2",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  // 3) Test the z-order of the windows and layers as a result of reordering the
  // views in situations where the window order remains unchanged.
  contents_view->ReorderChildView(v, contents_view->children().size());
  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w1 w2 v",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  contents_view->ReorderChildView(host_view2, contents_view->children().size());
  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w1 v w2",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
}

// Test that different orderings of:
// - adding a window to a parent widget
// - adding a "host" view to a parent widget
// - associating the "host" view and window
// all correctly reorder the child windows and layers.
TEST_F(WindowReordererTest, Association) {
  std::unique_ptr<Widget> parent = CreateControlWidget(root_window());
  parent->Show();
  aura::Window* parent_window = parent->GetNativeWindow();

  View* contents_view = parent->SetContentsView(std::make_unique<View>());

  aura::Window* w1 =
      aura::test::CreateTestWindowWithId(0, parent->GetNativeWindow());
  SetWindowAndLayerName(w1, "w1");

  aura::Window* w2 = aura::test::CreateTestWindowWithId(0, nullptr);
  SetWindowAndLayerName(w2, "w2");

  // 1) Test that parenting the window to the parent widget last results in a
  //    correct ordering of child windows and layers.
  auto* host_view2 = contents_view->AddChildView(std::make_unique<View>());
  w2->SetProperty(views::kHostViewKey, host_view2);
  EXPECT_EQ("w1", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w1", ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  parent_window->AddChild(w2);
  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w2 w1",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  // 2) Test that associating the window and "host" view last results in a
  // correct ordering of child windows and layers.
  auto* host_view1 = contents_view->AddChildViewAt(std::make_unique<View>(), 0);
  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w2 w1",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  w1->SetProperty(views::kHostViewKey, host_view1);
  EXPECT_EQ("w1 w2", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w1 w2",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  // 3) Test that parenting the "host" view to the parent widget last results
  // in a correct ordering of child windows and layers.
  contents_view->RemoveChildView(host_view2);
  contents_view->AddChildViewAt(host_view2, 0);
  EXPECT_EQ("w2 w1", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("w2 w1",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
}

// It is possible to associate a window to a view which has a parent layer
// (other than the widget layer). In this case, the parent layer of the host
// view and the parent layer of the associated window are different. Test that
// the layers and windows are properly reordered in this case.
TEST_F(WindowReordererTest, HostViewParentHasLayer) {
  std::unique_ptr<Widget> parent = CreateControlWidget(root_window());
  parent->Show();
  aura::Window* parent_window = parent->GetNativeWindow();

  View* contents_view = parent->SetContentsView(std::make_unique<View>());

  // Create the following view hierarchy. (*) denotes views which paint to a
  // layer.
  //
  // contents_view
  // +-- v1
  //     +-- v11*
  //     +-- v12 (attached window)
  //     +-- v13*
  // +--v2*

  View* v1 = contents_view->AddChildView(std::make_unique<View>());

  View* v11 = v1->AddChildView(std::make_unique<View>());
  v11->SetPaintToLayer();
  v11->layer()->SetName("v11");

  std::unique_ptr<Widget> w = CreateControlWidget(parent_window);
  SetWindowAndLayerName(w->GetNativeView(), "w");
  w->Show();

  View* v12 = v1->AddChildView(std::make_unique<View>());
  w->GetNativeView()->SetProperty(kHostViewKey, v12);

  View* v13 = v1->AddChildView(std::make_unique<View>());
  v13->SetPaintToLayer();
  v13->layer()->SetName("v13");

  View* v2 = contents_view->AddChildView(std::make_unique<View>());
  v2->SetPaintToLayer();
  v2->layer()->SetName("v2");

  // Test intial state.
  EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("v11 w v13 v2",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  // |w|'s layer should be stacked above |v1|'s layer.
  v1->SetPaintToLayer();
  v1->layer()->SetName("v1");
  EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("v1 w v2",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  // Test moving the host view from one view with a layer to another.
  v2->AddChildView(v1->RemoveChildViewT(v12));
  EXPECT_EQ("w", ChildWindowNamesAsString(*parent_window));
  EXPECT_EQ("v1 v2 w",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
}

// Test that a layer added beneath a view is restacked correctly.
TEST_F(WindowReordererTest, ViewWithLayerBeneath) {
  std::unique_ptr<Widget> parent = CreateControlWidget(root_window());
  parent->Show();

  aura::Window* parent_window = parent->GetNativeWindow();

  View* contents_view = parent->SetContentsView(std::make_unique<View>());

  View* view_with_layer_beneath =
      contents_view->AddChildView(std::make_unique<View>());
  ui::Layer layer_beneath;
  view_with_layer_beneath->AddLayerToRegion(&layer_beneath,
                                            LayerRegion::kBelow);

  ASSERT_NE(nullptr, view_with_layer_beneath->layer());
  view_with_layer_beneath->layer()->SetName("view");
  layer_beneath.SetName("beneath");

  // Verify that the initial ordering is correct.
  EXPECT_EQ("beneath view",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));

  // Add a hosted window to make WindowReorderer::ReorderChildWindows() restack
  // layers.
  std::unique_ptr<Widget> child_widget = CreateControlWidget(parent_window);
  SetWindowAndLayerName(child_widget->GetNativeView(), "child_widget");
  child_widget->Show();
  View* host_view = contents_view->AddChildView(std::make_unique<View>());
  child_widget->GetNativeView()->SetProperty(kHostViewKey, host_view);

  // Verify the new order is correct.
  EXPECT_EQ("beneath view child_widget",
            ui::test::ChildLayerNamesAsString(*parent_window->layer()));
}

}  // namespace
}  // namespace views