|
4 | 4 | * Use of this source code is governed by a BSD-style license that can be |
5 | 5 | * found in the LICENSE file. |
6 | 6 | */ |
| 7 | + |
7 | 8 | #include "flutter/testing/testing.h" |
8 | 9 | #include "flutter/testing/thread_test.h" |
| 10 | +#include "skia/flutter_rtree.h" |
9 | 11 |
|
10 | | -#include "skia/platform_view_rtree.h" |
11 | 12 | #include "third_party/skia/include/core/SkCanvas.h" |
12 | 13 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
13 | 14 |
|
14 | 15 | namespace flutter { |
15 | 16 | namespace testing { |
16 | 17 |
|
17 | | -using PlatformViewRTreeTest = ThreadTest; |
| 18 | +using PlatformViewRTree = ThreadTest; |
18 | 19 |
|
19 | | -TEST_F(PlatformViewRTreeTest, NoIntersection) { |
20 | | - auto r_tree = sk_make_sp<PlatformViewRTree>(); |
21 | | - auto rtree_factory = PlatformViewRTreeFactory(r_tree); |
| 20 | +TEST_F(PlatformViewRTree, NoIntersection) { |
| 21 | + auto r_tree = sk_make_sp<FlutterRTree>(); |
| 22 | + auto rtree_factory = FlutterRTreeFactory(r_tree); |
22 | 23 | auto recorder = std::make_unique<SkPictureRecorder>(); |
23 | 24 | auto recording_canvas = recorder->beginRecording(SkRect::MakeIWH(1000, 1000), &rtree_factory); |
24 | 25 |
|
25 | 26 | auto rect_paint = SkPaint(); |
26 | 27 | rect_paint.setColor(SkColors::kCyan); |
27 | 28 | rect_paint.setStyle(SkPaint::Style::kFill_Style); |
28 | 29 |
|
29 | | - recording_canvas->drawRect(SkRect::MakeXYWH(20, 20, 20, 20), rect_paint); |
| 30 | + // If no rect is intersected with the query rect, then the result vector is empty. |
| 31 | + recording_canvas->drawRect(SkRect::MakeLTRB(20, 20, 40, 40), rect_paint); |
30 | 32 | recorder->finishRecordingAsPicture(); |
31 | 33 |
|
32 | 34 | auto hits = std::vector<SkRect*>(); |
33 | | - auto unobstructed = SkRect::MakeXYWH(40, 40, 20, 20); |
| 35 | + auto query = SkRect::MakeLTRB(40, 40, 80, 80); |
34 | 36 |
|
35 | | - r_tree->searchRects(unobstructed, &hits); |
| 37 | + r_tree->searchRects(query, &hits); |
36 | 38 | ASSERT_TRUE(hits.empty()); |
37 | 39 | } |
38 | 40 |
|
39 | | -TEST_F(PlatformViewRTreeTest, Intersection) { |
40 | | - auto r_tree = sk_make_sp<PlatformViewRTree>(); |
41 | | - auto rtree_factory = PlatformViewRTreeFactory(r_tree); |
| 41 | +TEST_F(PlatformViewRTree, Intersection) { |
| 42 | + auto r_tree = sk_make_sp<FlutterRTree>(); |
| 43 | + auto rtree_factory = FlutterRTreeFactory(r_tree); |
| 44 | + auto recorder = std::make_unique<SkPictureRecorder>(); |
| 45 | + auto recording_canvas = recorder->beginRecording(SkRect::MakeIWH(1000, 1000), &rtree_factory); |
| 46 | + |
| 47 | + auto rect_paint = SkPaint(); |
| 48 | + rect_paint.setColor(SkColors::kCyan); |
| 49 | + rect_paint.setStyle(SkPaint::Style::kFill_Style); |
| 50 | + |
| 51 | + // Given a single rect A that intersects with the query rect, |
| 52 | + // the result vector contains this rect. |
| 53 | + recording_canvas->drawRect(SkRect::MakeLTRB(120, 120, 160, 160), rect_paint); |
| 54 | + |
| 55 | + recorder->finishRecordingAsPicture(); |
| 56 | + |
| 57 | + auto query = SkRect::MakeLTRB(140, 140, 150, 150); |
| 58 | + auto hits = std::vector<SkRect*>(); |
| 59 | + |
| 60 | + r_tree->searchRects(query, &hits); |
| 61 | + ASSERT_EQ(1UL, hits.size()); |
| 62 | + ASSERT_EQ(*hits[0], SkRect::MakeLTRB(120, 120, 160, 160)); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(PlatformViewRTree, JoinRectsWhenIntersectedCase1) { |
| 66 | + auto r_tree = sk_make_sp<FlutterRTree>(); |
| 67 | + auto rtree_factory = FlutterRTreeFactory(r_tree); |
42 | 68 | auto recorder = std::make_unique<SkPictureRecorder>(); |
43 | 69 | auto recording_canvas = recorder->beginRecording(SkRect::MakeIWH(1000, 1000), &rtree_factory); |
44 | 70 |
|
45 | 71 | auto rect_paint = SkPaint(); |
46 | 72 | rect_paint.setColor(SkColors::kCyan); |
47 | 73 | rect_paint.setStyle(SkPaint::Style::kFill_Style); |
48 | 74 |
|
49 | | - recording_canvas->drawRect(SkRect::MakeXYWH(120, 120, 40, 40), rect_paint); |
| 75 | + // Given the A, and B rects, which intersect with the query rect, |
| 76 | + // the result vector contains the rect resulting from the union of A and B. |
| 77 | + // |
| 78 | + // +-----+ |
| 79 | + // | A | |
| 80 | + // | +-----+ |
| 81 | + // | | C | |
| 82 | + // | +-----+ |
| 83 | + // | | |
| 84 | + // +-----+ |
| 85 | + |
| 86 | + // A |
| 87 | + recording_canvas->drawRect(SkRect::MakeLTRB(100, 100, 150, 150), rect_paint); |
| 88 | + // B |
| 89 | + recording_canvas->drawRect(SkRect::MakeLTRB(125, 125, 175, 175), rect_paint); |
50 | 90 |
|
51 | 91 | recorder->finishRecordingAsPicture(); |
52 | 92 |
|
53 | | - // Hits that partially overlap with a drawn area return bboxes describing the |
54 | | - // intersection of the query and the drawn area. |
55 | | - auto one_hit = SkRect::MakeXYWH(140, 140, 40, 40); |
| 93 | + auto query = SkRect::MakeXYWH(120, 120, 126, 126); |
56 | 94 | auto hits = std::vector<SkRect*>(); |
57 | 95 |
|
58 | | - r_tree->searchRects(one_hit, &hits); |
| 96 | + r_tree->searchRects(query, &hits); |
59 | 97 | ASSERT_EQ(1UL, hits.size()); |
60 | | - ASSERT_EQ(*hits[0], SkRect::MakeXYWH(120, 120, 40, 40)); |
| 98 | + ASSERT_EQ(*hits[0], SkRect::MakeLTRB(100, 100, 175, 175)); |
61 | 99 | } |
62 | 100 |
|
63 | | -TEST_F(PlatformViewRTreeTest, JoinRectsWhenIntersected) { |
64 | | - auto r_tree = sk_make_sp<PlatformViewRTree>(); |
65 | | - auto rtree_factory = PlatformViewRTreeFactory(r_tree); |
| 101 | +TEST_F(PlatformViewRTree, JoinRectsWhenIntersectedCase2) { |
| 102 | + auto r_tree = sk_make_sp<FlutterRTree>(); |
| 103 | + auto rtree_factory = FlutterRTreeFactory(r_tree); |
66 | 104 | auto recorder = std::make_unique<SkPictureRecorder>(); |
67 | 105 | auto recording_canvas = recorder->beginRecording(SkRect::MakeIWH(1000, 1000), &rtree_factory); |
68 | 106 |
|
69 | 107 | auto rect_paint = SkPaint(); |
70 | 108 | rect_paint.setColor(SkColors::kCyan); |
71 | 109 | rect_paint.setStyle(SkPaint::Style::kFill_Style); |
72 | 110 |
|
73 | | - recording_canvas->drawRect(SkRect::MakeXYWH(120, 120, 40, 40), rect_paint); |
74 | | - recording_canvas->drawRect(SkRect::MakeXYWH(140, 140, 40, 40), rect_paint); |
| 111 | + // Given the A, B, and C rects that intersect with the query rect, |
| 112 | + // there should be only C in the result vector, |
| 113 | + // since A and B are contained in C. |
| 114 | + // |
| 115 | + // +---------------------+ |
| 116 | + // | C | |
| 117 | + // | +-----+ +-----+ | |
| 118 | + // | | A | | B | | |
| 119 | + // | +-----+ +-----+ | |
| 120 | + // +---------------------+ |
| 121 | + // +-----+ |
| 122 | + // | D | |
| 123 | + // +-----+ |
| 124 | + |
| 125 | + // A |
| 126 | + recording_canvas->drawRect(SkRect::MakeLTRB(100, 100, 200, 200), rect_paint); |
| 127 | + // B |
| 128 | + recording_canvas->drawRect(SkRect::MakeLTRB(300, 100, 400, 200), rect_paint); |
| 129 | + // C |
| 130 | + recording_canvas->drawRect(SkRect::MakeLTRB(50, 50, 500, 250), rect_paint); |
| 131 | + // D |
| 132 | + recording_canvas->drawRect(SkRect::MakeLTRB(280, 100, 280, 320), rect_paint); |
75 | 133 |
|
76 | 134 | recorder->finishRecordingAsPicture(); |
77 | 135 |
|
78 | | - // Hits that partially overlap with a drawn area return bboxes describing the |
79 | | - // intersection of the query and the drawn area. |
80 | | - auto one_hit = SkRect::MakeXYWH(142, 142, 10, 10); |
| 136 | + auto query = SkRect::MakeLTRB(30, 30, 550, 270); |
81 | 137 | auto hits = std::vector<SkRect*>(); |
82 | 138 |
|
83 | | - r_tree->searchRects(one_hit, &hits); |
| 139 | + r_tree->searchRects(query, &hits); |
84 | 140 | ASSERT_EQ(1UL, hits.size()); |
85 | | - ASSERT_EQ(*hits[0], SkRect::MakeXYWH(120, 120, 60, 60)); |
| 141 | + ASSERT_EQ(*hits[0], SkRect::MakeLTRB(50, 50, 500, 250)); |
86 | 142 | } |
87 | 143 |
|
88 | 144 | } // namespace testing |
|
0 commit comments