Skip to content

Commit

Permalink
Add a unit test for PhysicalShapeLayer (flutter#8616)
Browse files Browse the repository at this point in the history
An unnecessary PrerollContext copy is also removed. The added unit test will catch the error if we forget to subtract the elevation after the copy removal.

This change has been tested with the framework (`flutter test --local-engine=host_debug_unopt`).
  • Loading branch information
liyuqian authored Apr 18, 2019
1 parent 876c4c3 commit 8b667b0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ FILE: ../../../flutter/flow/layers/performance_overlay_layer.h
FILE: ../../../flutter/flow/layers/performance_overlay_layer_unittests.cc
FILE: ../../../flutter/flow/layers/physical_shape_layer.cc
FILE: ../../../flutter/flow/layers/physical_shape_layer.h
FILE: ../../../flutter/flow/layers/physical_shape_layer_unittests.cc
FILE: ../../../flutter/flow/layers/picture_layer.cc
FILE: ../../../flutter/flow/layers/picture_layer.h
FILE: ../../../flutter/flow/layers/platform_view_layer.cc
Expand Down
1 change: 1 addition & 0 deletions flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ executable("flow_unittests") {
"flow_test_utils.cc",
"flow_test_utils.h",
"layers/performance_overlay_layer_unittests.cc",
"layers/physical_shape_layer_unittests.cc",
"matrix_decomposition_unittests.cc",
"raster_cache_unittests.cc",
]
Expand Down
3 changes: 1 addition & 2 deletions flow/layers/container_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ void ContainerLayer::PrerollChildren(PrerollContext* context,
const SkMatrix& child_matrix,
SkRect* child_paint_bounds) {
for (auto& layer : layers_) {
PrerollContext child_context = *context;
layer->Preroll(&child_context, child_matrix);
layer->Preroll(context, child_matrix);

if (layer->needs_system_composite()) {
set_needs_system_composite(true);
Expand Down
1 change: 1 addition & 0 deletions flow/layers/physical_shape_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void PhysicalShapeLayer::Preroll(PrerollContext* context,
total_elevation_ = context->total_elevation;
SkRect child_paint_bounds;
PrerollChildren(context, matrix, &child_paint_bounds);
context->total_elevation -= elevation_;

if (elevation_ == 0) {
set_paint_bounds(path_.getBounds());
Expand Down
2 changes: 2 additions & 0 deletions flow/layers/physical_shape_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class PhysicalShapeLayer : public ContainerLayer {
bool isRect_;
SkRRect frameRRect_;
Clip clip_behavior_;

friend class PhysicalShapeLayer_TotalElevation_Test;
};

} // namespace flutter
Expand Down
60 changes: 60 additions & 0 deletions flow/layers/physical_shape_layer_unittests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/flow/layers/physical_shape_layer.h"

#include "gtest/gtest.h"

namespace flow {

TEST(PhysicalShapeLayer, TotalElevation) {
std::shared_ptr<PhysicalShapeLayer> layers[4];

for (int i = 0; i < 4; i += 1) {
layers[i] = std::make_shared<PhysicalShapeLayer>(Clip::none);
layers[i]->set_elevation((float)(i + 1));
}

layers[0]->Add(layers[1]);
layers[0]->Add(layers[2]);
layers[2]->Add(layers[3]);

const Stopwatch unused_stopwatch;
TextureRegistry unused_texture_registry;
PrerollContext preroll_context{
nullptr, // raster_cache (don't consult the cache)
nullptr, // gr_context (used for the raster cache)
nullptr, // external view embedder
nullptr, // SkColorSpace* dst_color_space
kGiantRect, // SkRect cull_rect
unused_stopwatch, // frame time (dont care)
unused_stopwatch, // engine time (dont care)
unused_texture_registry, // texture registry (not supported)
false, // checkerboard_offscreen_layers
0.0f, // total elevation
};

SkMatrix identity;
identity.setIdentity();

layers[0]->Preroll(&preroll_context, identity);

// It should look like this:
// layers[0] +1.0f
// | \
// | \
// | \
// | layers[2] +3.0f
// | |
// | layers[3] +4.0f
// |
// |
// layers[1] + 2.0f
EXPECT_EQ(layers[0]->total_elevation_, 1.0f);
EXPECT_EQ(layers[1]->total_elevation_, 3.0f);
EXPECT_EQ(layers[2]->total_elevation_, 4.0f);
EXPECT_EQ(layers[3]->total_elevation_, 8.0f);
}

} // namespace flow

0 comments on commit 8b667b0

Please sign in to comment.