File: sub_surface_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (298 lines) | stat: -rw-r--r-- 12,023 bytes parent folder | download | duplicates (5)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/sub_surface.h"

#include "components/exo/buffer.h"
#include "components/exo/shell_surface.h"
#include "components/exo/surface.h"
#include "components/exo/test/exo_test_base.h"
#include "components/exo/test/exo_test_helper.h"
#include "components/viz/common/quads/texture_draw_quad.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/gfx/geometry/point_conversions.h"

namespace exo {
namespace {

using SubSurfaceTest = test::ExoTestBase;

TEST_F(SubSurfaceTest, SetPosition) {
  auto parent = std::make_unique<Surface>();
  auto shell_surface = std::make_unique<ShellSurface>(parent.get());
  auto surface = std::make_unique<Surface>();
  auto sub_surface = std::make_unique<SubSurface>(surface.get(), parent.get());

  // Initial position is at the origin.
  EXPECT_EQ(gfx::Point().ToString(),
            surface->window()->bounds().origin().ToString());

  // Set position to 10, 10.
  gfx::PointF position(10, 10);
  sub_surface->SetPosition(position);

  // A call to Commit() is required for position to take effect.
  EXPECT_EQ(gfx::Point().ToString(),
            surface->window()->bounds().origin().ToString());

  // Check that position is updated when Commit() is called.
  parent->Commit();
  EXPECT_EQ(gfx::ToRoundedPoint(position).ToString(),
            surface->window()->bounds().origin().ToString());

  // Create and commit a new sub-surface using the same surface.
  sub_surface.reset();
  sub_surface = std::make_unique<SubSurface>(surface.get(), parent.get());
  parent->Commit();

  // Initial position should be reset to origin.
  EXPECT_EQ(gfx::Point().ToString(),
            surface->window()->bounds().origin().ToString());
}

TEST_F(SubSurfaceTest, PlaceAbove) {
  auto parent = std::make_unique<Surface>();
  auto shell_surface = std::make_unique<ShellSurface>(parent.get());
  auto surface1 = std::make_unique<Surface>();
  auto surface2 = std::make_unique<Surface>();
  auto non_sibling_surface = std::make_unique<Surface>();
  auto sub_surface1 =
      std::make_unique<SubSurface>(surface1.get(), parent.get());
  auto sub_surface2 =
      std::make_unique<SubSurface>(surface2.get(), parent.get());

  ASSERT_EQ(2u, parent->window()->children().size());
  EXPECT_EQ(surface1->window(), parent->window()->children()[0]);
  EXPECT_EQ(surface2->window(), parent->window()->children()[1]);
  EXPECT_EQ(surface1.get(), parent->sub_surfaces().front().first);
  EXPECT_EQ(surface2.get(), parent->sub_surfaces().back().first);

  sub_surface2->PlaceAbove(parent.get());
  sub_surface1->PlaceAbove(non_sibling_surface.get());  // Invalid
  sub_surface1->PlaceAbove(surface1.get());             // Invalid
  sub_surface1->PlaceAbove(surface2.get());

  // Nothing should have changed as Commit() is required for new stacking
  // order to take effect.
  EXPECT_EQ(surface1->window(), parent->window()->children()[0]);
  EXPECT_EQ(surface2->window(), parent->window()->children()[1]);
  EXPECT_EQ(surface1.get(), parent->sub_surfaces().front().first);
  EXPECT_EQ(surface2.get(), parent->sub_surfaces().back().first);

  parent->Commit();

  // surface1 should now be stacked above surface2.
  EXPECT_EQ(surface2->window(), parent->window()->children()[0]);
  EXPECT_EQ(surface1->window(), parent->window()->children()[1]);
  EXPECT_EQ(surface2.get(), parent->sub_surfaces().front().first);
  EXPECT_EQ(surface1.get(), parent->sub_surfaces().back().first);

  sub_surface1->PlaceAbove(parent.get());
  parent->Commit();

  // surface2 should now be stacked above surface1.
  EXPECT_EQ(surface1->window(), parent->window()->children()[0]);
  EXPECT_EQ(surface2->window(), parent->window()->children()[1]);
  EXPECT_EQ(surface1.get(), parent->sub_surfaces().front().first);
  EXPECT_EQ(surface2.get(), parent->sub_surfaces().back().first);
}

TEST_F(SubSurfaceTest, PlaceBelow) {
  auto parent = std::make_unique<Surface>();
  auto shell_surface = std::make_unique<ShellSurface>(parent.get());
  auto surface1 = std::make_unique<Surface>();
  auto surface2 = std::make_unique<Surface>();
  auto non_sibling_surface = std::make_unique<Surface>();
  auto sub_surface1 =
      std::make_unique<SubSurface>(surface1.get(), parent.get());
  auto sub_surface2 =
      std::make_unique<SubSurface>(surface2.get(), parent.get());

  ASSERT_EQ(2u, parent->window()->children().size());
  EXPECT_EQ(surface1->window(), parent->window()->children()[0]);
  EXPECT_EQ(surface2->window(), parent->window()->children()[1]);
  EXPECT_EQ(surface1.get(), parent->sub_surfaces().front().first);
  EXPECT_EQ(surface2.get(), parent->sub_surfaces().back().first);

  sub_surface2->PlaceBelow(parent.get());               // Invalid
  sub_surface2->PlaceBelow(non_sibling_surface.get());  // Invalid
  sub_surface1->PlaceBelow(surface2.get());
  sub_surface2->PlaceBelow(surface1.get());

  // Nothing should have changed as Commit() is required for new stacking
  // order to take effect.
  EXPECT_EQ(surface1->window(), parent->window()->children()[0]);
  EXPECT_EQ(surface2->window(), parent->window()->children()[1]);
  EXPECT_EQ(surface1.get(), parent->sub_surfaces().front().first);
  EXPECT_EQ(surface2.get(), parent->sub_surfaces().back().first);

  parent->Commit();

  // surface1 should now be stacked above surface2.
  EXPECT_EQ(surface2->window(), parent->window()->children()[0]);
  EXPECT_EQ(surface1->window(), parent->window()->children()[1]);
  EXPECT_EQ(surface2.get(), parent->sub_surfaces().front().first);
  EXPECT_EQ(surface1.get(), parent->sub_surfaces().back().first);
}

TEST_F(SubSurfaceTest, ParentDamageOnReorder) {
  gfx::Size buffer_size(800, 600);
  auto buffer = test::ExoTestHelper::CreateBuffer(buffer_size);
  auto surface_tree_host = std::make_unique<SurfaceTreeHost>("SubSurfaceTest");
  LayerTreeFrameSinkHolder* frame_sink_holder =
      surface_tree_host->layer_tree_frame_sink_holder();

  auto parent = std::make_unique<Surface>();
  parent->Attach(buffer.get());
  auto surface1 = std::make_unique<Surface>();
  auto surface2 = std::make_unique<Surface>();
  auto non_sibling_surface = std::make_unique<Surface>();
  auto sub_surface1 =
      std::make_unique<SubSurface>(surface1.get(), parent.get());
  auto sub_surface2 =
      std::make_unique<SubSurface>(surface2.get(), parent.get());

  sub_surface2->PlaceBelow(surface1.get());
  parent->Commit();

  viz::CompositorFrame frame1;
  frame1.render_pass_list.push_back(viz::CompositorRenderPass::Create());
  parent->AppendSurfaceHierarchyContentsToFrame(
      gfx::PointF{}, gfx::PointF{},
      /*needs_full_damage=*/false, frame_sink_holder->resource_manager(),
      /*device_scale_factor=*/std::nullopt, &frame1);

  const viz::TextureDrawQuad* parent_quad1 = viz::TextureDrawQuad::MaterialCast(
      frame1.render_pass_list.back()->quad_list.back());

  // Parent surface damage is extended when sub_surface stacking order changes.
  EXPECT_FALSE(frame1.render_pass_list.back()->damage_rect.IsEmpty() &&
               parent_quad1->damage_rect->IsEmpty());

  sub_surface1->PlaceAbove(surface2.get());  // no-op
  sub_surface2->PlaceBelow(surface1.get());  // no-op
  parent->Commit();

  viz::CompositorFrame frame2;
  frame2.render_pass_list.push_back(viz::CompositorRenderPass::Create());
  parent->AppendSurfaceHierarchyContentsToFrame(
      gfx::PointF{}, gfx::PointF{},
      /*needs_full_damage=*/false, frame_sink_holder->resource_manager(),
      /*device_scale_factor=*/std::nullopt, &frame2);

  const viz::TextureDrawQuad* parent_quad2 = viz::TextureDrawQuad::MaterialCast(
      frame2.render_pass_list.back()->quad_list.back());

  gfx::Rect parent_quad2_damage_rect =
      parent_quad2->damage_rect.value_or(gfx::Rect());

  // Parent surface damage is unaffected.
  EXPECT_TRUE(frame2.render_pass_list.back()->damage_rect.IsEmpty() &&
              parent_quad2_damage_rect.IsEmpty());
}

TEST_F(SubSurfaceTest, SetCommitBehavior) {
  auto parent = std::make_unique<Surface>();
  auto shell_surface = std::make_unique<ShellSurface>(parent.get());
  auto child = std::make_unique<Surface>();
  auto grandchild = std::make_unique<Surface>();
  auto child_sub_surface =
      std::make_unique<SubSurface>(child.get(), parent.get());
  auto grandchild_sub_surface =
      std::make_unique<SubSurface>(grandchild.get(), child.get());

  // Initial position is at the origin.
  EXPECT_EQ(gfx::Point().ToString(),
            grandchild->window()->bounds().origin().ToString());

  // Set position to 10, 10.
  gfx::PointF position1(10, 10);
  grandchild_sub_surface->SetPosition(position1);
  child->Commit();

  // Initial commit behavior is synchronous and the effect of the child
  // Commit() call will not take effect until Commit() is called on the
  // parent.
  EXPECT_EQ(gfx::Point().ToString(),
            grandchild->window()->bounds().origin().ToString());

  parent->Commit();

  // Position should have been updated when Commit() has been called on both
  // child and parent.
  EXPECT_EQ(gfx::ToRoundedPoint(position1).ToString(),
            grandchild->window()->bounds().origin().ToString());

  bool synchronized = false;
  child_sub_surface->SetCommitBehavior(synchronized);

  // Set position to 20, 20.
  gfx::PointF position2(20, 20);
  grandchild_sub_surface->SetPosition(position2);
  child->Commit();

  // A Commit() call on child should be sufficient for the position of
  // grandchild to take effect when synchronous is disabled.
  EXPECT_EQ(gfx::ToRoundedPoint(position2).ToString(),
            grandchild->window()->bounds().origin().ToString());
}

TEST_F(SubSurfaceTest, SetOnParent) {
  gfx::Size buffer_size(32, 32);
  auto buffer = test::ExoTestHelper::CreateBuffer(buffer_size);
  auto parent = std::make_unique<Surface>();
  auto shell_surface = std::make_unique<ShellSurface>(parent.get());
  parent->Attach(buffer.get());
  parent->Commit();

  shell_surface->GetWidget()->GetNativeWindow()->SetProperty(
      aura::client::kSkipImeProcessing, true);
  ASSERT_TRUE(parent->window()->GetProperty(aura::client::kSkipImeProcessing));

  // SkipImeProcessing property is propagated to SubSurface.
  auto surface = std::make_unique<Surface>();
  auto sub_surface = std::make_unique<SubSurface>(surface.get(), parent.get());
  surface->SetParent(parent.get(), gfx::Point(10, 10));
  EXPECT_TRUE(surface->window()->GetProperty(aura::client::kSkipImeProcessing));
}

TEST_F(SubSurfaceTest, AugmentedSurfaceDoesNotExpandHierarchy) {
  auto parent = std::make_unique<Surface>();
  auto shell_surface = std::make_unique<ShellSurface>(parent.get());

  auto sub_layer = std::make_unique<Surface>();
  sub_layer->set_is_augmented(true);
  auto sub_surface = std::make_unique<Surface>();
  auto sub_layer_role =
      std::make_unique<SubSurface>(sub_layer.get(), parent.get());
  auto sub_surface_role =
      std::make_unique<SubSurface>(sub_surface.get(), parent.get());

  auto parent_buffer = exo_test_helper()->CreateBuffer(gfx::Size(800, 600));
  auto sub_surface_buffer = exo_test_helper()->CreateBuffer(gfx::Size(10, 10));
  auto sub_layer_buffer = exo_test_helper()->CreateBuffer(gfx::Size(10, 10));

  // Set position to be outside parent surface.
  sub_layer_role->SetPosition(gfx::PointF(-10, 0));
  sub_surface_role->SetPosition(gfx::PointF(0, -10));

  parent->Attach(parent_buffer.get());
  sub_layer->Attach(sub_layer_buffer.get());
  sub_surface->Attach(sub_surface_buffer.get());

  sub_layer->Commit();
  sub_surface->Commit();
  parent->Commit();

  // Only sub_surface affects the surface hierarchy bounds
  EXPECT_EQ(parent->surface_hierarchy_content_bounds(),
            gfx::Rect(0, -10, 800, 610));
  // sub_layer is not in parent aura window's children list.
  EXPECT_EQ(1u, parent->window()->children().size());
  EXPECT_EQ(sub_surface->window(), parent->window()->children()[0]);
}

}  // namespace
}  // namespace exo