Add a unit test for PhysicalShapeLayer (flutter/engine#8616)

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`).
This commit is contained in:
liyuqian
2019-04-17 17:34:11 -07:00
committed by GitHub
parent 22a0517fb6
commit 6f4740c284
6 changed files with 66 additions and 2 deletions

View File

@@ -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

View File

@@ -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",
]

View File

@@ -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);

View File

@@ -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());

View File

@@ -52,6 +52,8 @@ class PhysicalShapeLayer : public ContainerLayer {
bool isRect_;
SkRRect frameRRect_;
Clip clip_behavior_;
friend class PhysicalShapeLayer_TotalElevation_Test;
};
} // namespace flutter

View File

@@ -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