|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include <unordered_map> |
| 6 | +#include "display_list/dl_builder.h" |
| 7 | +#include "flow/embedded_views.h" |
| 8 | +#include "flutter/flow/view_slicer.h" |
| 9 | +#include "gtest/gtest.h" |
| 10 | + |
| 11 | +namespace flutter { |
| 12 | +namespace testing { |
| 13 | + |
| 14 | +namespace { |
| 15 | +void AddSliceOfSize( |
| 16 | + std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>>& slices, |
| 17 | + int64_t id, |
| 18 | + SkRect rect) { |
| 19 | + slices[id] = std::make_unique<DisplayListEmbedderViewSlice>(rect); |
| 20 | + DlPaint paint; |
| 21 | + paint.setColor(DlColor::kBlack()); |
| 22 | + slices[id]->canvas()->DrawRect(rect, paint); |
| 23 | +} |
| 24 | +} // namespace |
| 25 | + |
| 26 | +TEST(ViewSlicerTest, CanSlicerNonOverlappingViews) { |
| 27 | + DisplayListBuilder builder(SkRect::MakeLTRB(0, 0, 100, 100)); |
| 28 | + |
| 29 | + std::vector<int64_t> composition_order = {1}; |
| 30 | + std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices; |
| 31 | + AddSliceOfSize(slices, 1, SkRect::MakeLTRB(99, 99, 100, 100)); |
| 32 | + |
| 33 | + std::unordered_map<int64_t, SkRect> view_rects = { |
| 34 | + {1, SkRect::MakeLTRB(50, 50, 60, 60)}}; |
| 35 | + |
| 36 | + auto computed_overlays = |
| 37 | + SliceViews(&builder, composition_order, slices, view_rects); |
| 38 | + |
| 39 | + EXPECT_TRUE(computed_overlays.empty()); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(ViewSlicerTest, IgnoresFractionalOverlaps) { |
| 43 | + DisplayListBuilder builder(SkRect::MakeLTRB(0, 0, 100, 100)); |
| 44 | + |
| 45 | + std::vector<int64_t> composition_order = {1}; |
| 46 | + std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices; |
| 47 | + AddSliceOfSize(slices, 1, SkRect::MakeLTRB(0, 0, 50.49, 50.49)); |
| 48 | + |
| 49 | + std::unordered_map<int64_t, SkRect> view_rects = { |
| 50 | + {1, SkRect::MakeLTRB(50.5, 50.5, 100, 100)}}; |
| 51 | + |
| 52 | + auto computed_overlays = |
| 53 | + SliceViews(&builder, composition_order, slices, view_rects); |
| 54 | + |
| 55 | + EXPECT_TRUE(computed_overlays.empty()); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(ViewSlicerTest, ComputesOverlapWith1PV) { |
| 59 | + DisplayListBuilder builder(SkRect::MakeLTRB(0, 0, 100, 100)); |
| 60 | + |
| 61 | + std::vector<int64_t> composition_order = {1}; |
| 62 | + std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices; |
| 63 | + AddSliceOfSize(slices, 1, SkRect::MakeLTRB(0, 0, 50, 50)); |
| 64 | + |
| 65 | + std::unordered_map<int64_t, SkRect> view_rects = { |
| 66 | + {1, SkRect::MakeLTRB(0, 0, 100, 100)}}; |
| 67 | + |
| 68 | + auto computed_overlays = |
| 69 | + SliceViews(&builder, composition_order, slices, view_rects); |
| 70 | + |
| 71 | + EXPECT_EQ(computed_overlays.size(), 1u); |
| 72 | + auto overlay = computed_overlays.find(1); |
| 73 | + ASSERT_NE(overlay, computed_overlays.end()); |
| 74 | + |
| 75 | + EXPECT_EQ(overlay->second, SkRect::MakeLTRB(0, 0, 50, 50)); |
| 76 | +} |
| 77 | + |
| 78 | +TEST(ViewSlicerTest, ComputesOverlapWith2PV) { |
| 79 | + DisplayListBuilder builder(SkRect::MakeLTRB(0, 0, 100, 100)); |
| 80 | + |
| 81 | + std::vector<int64_t> composition_order = {1, 2}; |
| 82 | + std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices; |
| 83 | + AddSliceOfSize(slices, 1, SkRect::MakeLTRB(0, 0, 50, 50)); |
| 84 | + AddSliceOfSize(slices, 2, SkRect::MakeLTRB(50, 50, 100, 100)); |
| 85 | + |
| 86 | + std::unordered_map<int64_t, SkRect> view_rects = { |
| 87 | + {1, SkRect::MakeLTRB(0, 0, 50, 50)}, // |
| 88 | + {2, SkRect::MakeLTRB(50, 50, 100, 100)}, // |
| 89 | + }; |
| 90 | + |
| 91 | + auto computed_overlays = |
| 92 | + SliceViews(&builder, composition_order, slices, view_rects); |
| 93 | + |
| 94 | + EXPECT_EQ(computed_overlays.size(), 2u); |
| 95 | + |
| 96 | + auto overlay = computed_overlays.find(1); |
| 97 | + ASSERT_NE(overlay, computed_overlays.end()); |
| 98 | + |
| 99 | + EXPECT_EQ(overlay->second, SkRect::MakeLTRB(0, 0, 50, 50)); |
| 100 | + |
| 101 | + overlay = computed_overlays.find(2); |
| 102 | + ASSERT_NE(overlay, computed_overlays.end()); |
| 103 | + EXPECT_EQ(overlay->second, SkRect::MakeLTRB(50, 50, 100, 100)); |
| 104 | +} |
| 105 | + |
| 106 | +TEST(ViewSlicerTest, OverlappingTwoPVs) { |
| 107 | + DisplayListBuilder builder(SkRect::MakeLTRB(0, 0, 100, 100)); |
| 108 | + |
| 109 | + std::vector<int64_t> composition_order = {1, 2}; |
| 110 | + std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices; |
| 111 | + // This embeded view overlaps both platform views: |
| 112 | + // |
| 113 | + // [ A [ ]] |
| 114 | + // [_____[ C ]] |
| 115 | + // [ B [ ]] |
| 116 | + // [ ] |
| 117 | + AddSliceOfSize(slices, 1, SkRect::MakeLTRB(0, 0, 0, 0)); |
| 118 | + AddSliceOfSize(slices, 2, SkRect::MakeLTRB(0, 0, 100, 100)); |
| 119 | + |
| 120 | + std::unordered_map<int64_t, SkRect> view_rects = { |
| 121 | + {1, SkRect::MakeLTRB(0, 0, 50, 50)}, // |
| 122 | + {2, SkRect::MakeLTRB(50, 50, 100, 100)}, // |
| 123 | + }; |
| 124 | + |
| 125 | + auto computed_overlays = |
| 126 | + SliceViews(&builder, composition_order, slices, view_rects); |
| 127 | + |
| 128 | + EXPECT_EQ(computed_overlays.size(), 1u); |
| 129 | + |
| 130 | + auto overlay = computed_overlays.find(2); |
| 131 | + ASSERT_NE(overlay, computed_overlays.end()); |
| 132 | + |
| 133 | + // We create a single overlay for both overlapping sections. |
| 134 | + EXPECT_EQ(overlay->second, SkRect::MakeLTRB(0, 0, 100, 100)); |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace testing |
| 138 | +} // namespace flutter |
0 commit comments