Skip to content

Commit

Permalink
Introduce bitset to manage bool flags in mock_layer unit test. (flutt…
Browse files Browse the repository at this point in the history
  • Loading branch information
guoguo338 authored Sep 2, 2022
1 parent 022ebd2 commit 5bf3bf0
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 72 deletions.
4 changes: 2 additions & 2 deletions flow/layers/backdrop_filter_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ TEST_F(BackdropFilterLayerTest, Readback) {
EXPECT_TRUE(preroll_context()->surface_needs_readback);

// BDF with no filter blocks child with readback
auto mock_layer =
std::make_shared<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(SkPath(), SkPaint());
mock_layer->set_fake_reads_surface(true);
layer2->Add(mock_layer);
preroll_context()->surface_needs_readback = false;
layer2->Preroll(preroll_context(), initial_transform);
Expand Down
3 changes: 2 additions & 1 deletion flow/layers/clip_path_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ TEST_F(ClipPathLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;

std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);

// No children, no prior readback -> no readback after
Expand Down
3 changes: 2 additions & 1 deletion flow/layers/clip_rect_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ TEST_F(ClipRectLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;

std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);

// No children, no prior readback -> no readback after
Expand Down
3 changes: 2 additions & 1 deletion flow/layers/clip_rrect_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ TEST_F(ClipRRectLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;

std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);

// No children, no prior readback -> no readback after
Expand Down
4 changes: 2 additions & 2 deletions flow/layers/color_filter_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ TEST_F(ColorFilterLayerTest, Readback) {
EXPECT_FALSE(preroll_context()->surface_needs_readback);

// ColorFilterLayer blocks child with readback
auto mock_layer =
std::make_shared<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(SkPath(), SkPaint());
mock_layer->set_fake_reads_surface(true);
layer->Add(mock_layer);
preroll_context()->surface_needs_readback = false;
layer->Preroll(preroll_context(), initial_transform);
Expand Down
13 changes: 6 additions & 7 deletions flow/layers/container_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ TEST_F(ContainerLayerTest, LayerWithParentHasTextureLayerNeedsResetFlag) {
SkPaint child_paint1(SkColors::kGray);
SkPaint child_paint2(SkColors::kGreen);

auto mock_layer1 = std::make_shared<MockLayer>(
child_path1, child_paint1, false /* fake_has_platform_view */, false,
false, true /* fake_has_texture_layer */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_texture_layer(true);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);

auto root = std::make_shared<ContainerLayer>();
Expand Down Expand Up @@ -126,8 +125,8 @@ TEST_F(ContainerLayerTest, Multiple) {
SkPaint child_paint2(SkColors::kGreen);
SkMatrix initial_transform = SkMatrix::Translate(-0.5f, -0.5f);

auto mock_layer1 = std::make_shared<MockLayer>(
child_path1, child_paint1, true /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_platform_view(true);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
layer->Add(mock_layer1);
Expand Down Expand Up @@ -201,8 +200,8 @@ TEST_F(ContainerLayerTest, NeedsSystemComposite) {
SkPaint child_paint2(SkColors::kGreen);
SkMatrix initial_transform = SkMatrix::Translate(-0.5f, -0.5f);

auto mock_layer1 = std::make_shared<MockLayer>(
child_path1, child_paint1, false /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_platform_view(false);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
layer->Add(mock_layer1);
Expand Down
4 changes: 2 additions & 2 deletions flow/layers/image_filter_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ TEST_F(ImageFilterLayerTest, Readback) {
EXPECT_FALSE(preroll_context()->surface_needs_readback);

// ImageFilterLayer blocks child with readback
auto mock_layer =
std::make_shared<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(SkPath(), SkPaint());
mock_layer->set_fake_reads_surface(true);
layer->Add(mock_layer);
preroll_context()->surface_needs_readback = false;
layer->Preroll(preroll_context(), initial_transform);
Expand Down
7 changes: 3 additions & 4 deletions flow/layers/layer_tree_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ TEST_F(LayerTreeTest, Multiple) {
const SkPath child_path2 = SkPath().addRect(8.0f, 2.0f, 16.5f, 14.5f);
const SkPaint child_paint1(SkColors::kGray);
const SkPaint child_paint2(SkColors::kGreen);
auto mock_layer1 = std::make_shared<MockLayer>(
child_path1, child_paint1, true /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_platform_view(true);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
layer->Add(mock_layer1);
Expand Down Expand Up @@ -163,8 +163,7 @@ TEST_F(LayerTreeTest, NeedsSystemComposite) {
const SkPath child_path2 = SkPath().addRect(8.0f, 2.0f, 16.5f, 14.5f);
const SkPaint child_paint1(SkColors::kGray);
const SkPaint child_paint2(SkColors::kGreen);
auto mock_layer1 = std::make_shared<MockLayer>(
child_path1, child_paint1, false /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
layer->Add(mock_layer1);
Expand Down
4 changes: 2 additions & 2 deletions flow/layers/opacity_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ TEST_F(OpacityLayerTest, Readback) {
EXPECT_FALSE(preroll_context()->surface_needs_readback);

// OpacityLayer blocks child with readback
auto mock_layer =
std::make_shared<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(SkPath(), SkPaint());
mock_layer->set_fake_reads_surface(true);
layer->Add(mock_layer);
preroll_context()->surface_needs_readback = false;
layer->Preroll(preroll_context(), initial_transform);
Expand Down
3 changes: 2 additions & 1 deletion flow/layers/physical_shape_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ TEST_F(PhysicalShapeLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;

std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);

// No children, no prior readback -> no readback after
Expand Down
4 changes: 2 additions & 2 deletions flow/layers/shader_mask_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ TEST_F(ShaderMaskLayerTest, Readback) {
EXPECT_FALSE(preroll_context()->surface_needs_readback);

// ShaderMaskLayer blocks child with readback
auto mock_layer =
std::make_shared<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(SkPath(), SkPaint());
mock_layer->set_fake_reads_surface(true);
layer->Add(mock_layer);
preroll_context()->surface_needs_readback = false;
layer->Preroll(preroll_context(), initial_transform);
Expand Down
27 changes: 9 additions & 18 deletions flow/testing/mock_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@
namespace flutter {
namespace testing {

MockLayer::MockLayer(SkPath path,
SkPaint paint,
bool fake_has_platform_view,
bool fake_reads_surface,
bool fake_opacity_compatible,
bool fake_has_texture_layer)
: fake_paint_path_(path),
fake_paint_(paint),
fake_has_platform_view_(fake_has_platform_view),
fake_reads_surface_(fake_reads_surface),
fake_opacity_compatible_(fake_opacity_compatible),
fake_has_texture_layer_(fake_has_texture_layer) {}
MockLayer::MockLayer(SkPath path, SkPaint paint)
: fake_paint_path_(path), fake_paint_(paint) {}

bool MockLayer::IsReplacing(DiffContext* context, const Layer* layer) const {
// Similar to PictureLayer, only return true for identical mock layers;
Expand All @@ -42,16 +32,17 @@ void MockLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
parent_mutators_ = context->mutators_stack;
parent_matrix_ = matrix;
parent_cull_rect_ = context->cull_rect;
parent_has_platform_view_ = context->has_platform_view;
parent_has_texture_layer_ = context->has_texture_layer;

context->has_platform_view = fake_has_platform_view_;
context->has_texture_layer = fake_has_texture_layer_;
set_parent_has_platform_view(context->has_platform_view);
set_parent_has_texture_layer(context->has_texture_layer);

context->has_platform_view = fake_has_platform_view();
context->has_texture_layer = fake_has_texture_layer();
set_paint_bounds(fake_paint_path_.getBounds());
if (fake_reads_surface_) {
if (fake_reads_surface()) {
context->surface_needs_readback = true;
}
if (fake_opacity_compatible_) {
if (fake_opacity_compatible()) {
context->subtree_can_inherit_opacity = true;
}
}
Expand Down
95 changes: 70 additions & 25 deletions flow/testing/mock_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ namespace testing {
// verify the data against expected values.
class MockLayer : public Layer {
public:
explicit MockLayer(SkPath path,
SkPaint paint = SkPaint(),
bool fake_has_platform_view = false,
bool fake_reads_surface = false,
bool fake_opacity_compatible_ = false,
bool fake_has_texture_layer = false);
explicit MockLayer(SkPath path, SkPaint paint = SkPaint());

static std::shared_ptr<MockLayer> Make(SkPath path,
SkPaint paint = SkPaint()) {
return std::make_shared<MockLayer>(path, paint, false, false, false);
return std::make_shared<MockLayer>(path, paint);
}

static std::shared_ptr<MockLayer> MakeOpacityCompatible(SkPath path) {
return std::make_shared<MockLayer>(path, SkPaint(), false, false, true);
auto mock_layer = std::make_shared<MockLayer>(path, SkPaint());
mock_layer->set_fake_opacity_compatible(true);
return mock_layer;
}

void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
Expand All @@ -46,25 +43,80 @@ class MockLayer : public Layer {
const MutatorsStack& parent_mutators() { return parent_mutators_; }
const SkMatrix& parent_matrix() { return parent_matrix_; }
const SkRect& parent_cull_rect() { return parent_cull_rect_; }
bool parent_has_platform_view() { return parent_has_platform_view_; }
bool parent_has_texture_layer() { return parent_has_texture_layer_; }

bool IsReplacing(DiffContext* context, const Layer* layer) const override;
void Diff(DiffContext* context, const Layer* old_layer) override;
const MockLayer* as_mock_layer() const override { return this; }

bool parent_has_platform_view() {
return mock_flags_ & kParentHasPlatformView;
}

bool parent_has_texture_layer() {
return mock_flags_ & kParentHasTextureLayer;
}

bool fake_has_platform_view() { return mock_flags_ & kFakeHasPlatformView; }

bool fake_reads_surface() { return mock_flags_ & kFakeReadsSurface; }

bool fake_opacity_compatible() {
return mock_flags_ & kFakeOpacityCompatible;
}

bool fake_has_texture_layer() { return mock_flags_ & kFakeHasTextureLayer; }

MockLayer& set_parent_has_platform_view(bool flag) {
flag ? (mock_flags_ |= kParentHasPlatformView)
: (mock_flags_ &= ~(kParentHasPlatformView));
return *this;
}

MockLayer& set_parent_has_texture_layer(bool flag) {
flag ? (mock_flags_ |= kParentHasTextureLayer)
: (mock_flags_ &= ~(kParentHasTextureLayer));
return *this;
}

MockLayer& set_fake_has_platform_view(bool flag) {
flag ? (mock_flags_ |= kFakeHasPlatformView)
: (mock_flags_ &= ~(kFakeHasPlatformView));
return *this;
}

MockLayer& set_fake_reads_surface(bool flag) {
flag ? (mock_flags_ |= kFakeReadsSurface)
: (mock_flags_ &= ~(kFakeReadsSurface));
return *this;
}

MockLayer& set_fake_opacity_compatible(bool flag) {
flag ? (mock_flags_ |= kFakeOpacityCompatible)
: (mock_flags_ &= ~(kFakeOpacityCompatible));
return *this;
}

MockLayer& set_fake_has_texture_layer(bool flag) {
flag ? (mock_flags_ |= kFakeHasTextureLayer)
: (mock_flags_ &= ~(kFakeHasTextureLayer));
return *this;
}

private:
MutatorsStack parent_mutators_;
SkMatrix parent_matrix_;
SkRect parent_cull_rect_ = SkRect::MakeEmpty();
SkPath fake_paint_path_;
SkPaint fake_paint_;
bool parent_has_platform_view_ = false;
bool parent_has_texture_layer_ = false;
bool fake_has_platform_view_ = false;
bool fake_reads_surface_ = false;
bool fake_opacity_compatible_ = false;
bool fake_has_texture_layer_ = false;

static constexpr int kParentHasPlatformView = 1 << 0;
static constexpr int kParentHasTextureLayer = 1 << 1;
static constexpr int kFakeHasPlatformView = 1 << 2;
static constexpr int kFakeReadsSurface = 1 << 3;
static constexpr int kFakeOpacityCompatible = 1 << 4;
static constexpr int kFakeHasTextureLayer = 1 << 5;

int mock_flags_ = 0;

FML_DISALLOW_COPY_AND_ASSIGN(MockLayer);
};
Expand Down Expand Up @@ -97,15 +149,8 @@ class MockCacheableLayer : public MockLayer {
public:
explicit MockCacheableLayer(SkPath path,
SkPaint paint = SkPaint(),
int render_limit = 3,
bool fake_has_platform_view = false,
bool fake_reads_surface = false,
bool fake_opacity_compatible = false)
: MockLayer(path,
paint,
fake_has_platform_view,
fake_reads_surface,
fake_opacity_compatible) {
int render_limit = 3)
: MockLayer(path, paint) {
raster_cache_item_ =
std::make_unique<MockLayerCacheableItem>(this, render_limit);
}
Expand Down
36 changes: 32 additions & 4 deletions flow/testing/mock_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ TEST_F(MockLayerTest, SimpleParams) {
}

TEST_F(MockLayerTest, FakePlatformView) {
auto layer = std::make_shared<MockLayer>(SkPath(), SkPaint(),
true /* fake_has_platform_view */);
auto layer = std::make_shared<MockLayer>(SkPath(), SkPaint());
layer->set_fake_has_platform_view(true);
EXPECT_EQ(preroll_context()->has_platform_view, false);

layer->Preroll(preroll_context(), SkMatrix());
EXPECT_EQ(preroll_context()->has_platform_view, true);
}

TEST_F(MockLayerTest, SaveLayerOnLeafNodesCanvas) {
auto layer = std::make_shared<MockLayer>(SkPath(), SkPaint(),
true /* fake_has_platform_view */);
auto layer = std::make_shared<MockLayer>(SkPath(), SkPaint());
layer->set_fake_has_platform_view(true);
EXPECT_EQ(preroll_context()->has_platform_view, false);

layer->Preroll(preroll_context(), SkMatrix());
Expand All @@ -93,5 +93,33 @@ TEST_F(MockLayerTest, OpacityInheritance) {
EXPECT_TRUE(context->subtree_can_inherit_opacity);
}

TEST_F(MockLayerTest, FlagGetSet) {
auto mock_layer = std::make_shared<MockLayer>(SkPath());

EXPECT_EQ(mock_layer->parent_has_platform_view(), false);
mock_layer->set_parent_has_platform_view(true);
EXPECT_EQ(mock_layer->parent_has_platform_view(), true);

EXPECT_EQ(mock_layer->parent_has_texture_layer(), false);
mock_layer->set_parent_has_texture_layer(true);
EXPECT_EQ(mock_layer->parent_has_texture_layer(), true);

EXPECT_EQ(mock_layer->fake_has_platform_view(), false);
mock_layer->set_fake_has_platform_view(true);
EXPECT_EQ(mock_layer->fake_has_platform_view(), true);

EXPECT_EQ(mock_layer->fake_reads_surface(), false);
mock_layer->set_fake_reads_surface(true);
EXPECT_EQ(mock_layer->fake_reads_surface(), true);

EXPECT_EQ(mock_layer->fake_opacity_compatible(), false);
mock_layer->set_fake_opacity_compatible(true);
EXPECT_EQ(mock_layer->fake_opacity_compatible(), true);

EXPECT_EQ(mock_layer->fake_has_texture_layer(), false);
mock_layer->set_fake_has_texture_layer(true);
EXPECT_EQ(mock_layer->fake_has_texture_layer(), true);
}

} // namespace testing
} // namespace flutter

0 comments on commit 5bf3bf0

Please sign in to comment.