Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 04a1693

Browse files
author
Emmanuel Garcia
committed
Add more tests for the searchRect method
1 parent 0c791bd commit 04a1693

File tree

1 file changed

+83
-27
lines changed

1 file changed

+83
-27
lines changed

third_party/skia/platform_view_rtree_unittests.cc

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,141 @@
44
* Use of this source code is governed by a BSD-style license that can be
55
* found in the LICENSE file.
66
*/
7+
78
#include "flutter/testing/testing.h"
89
#include "flutter/testing/thread_test.h"
10+
#include "skia/flutter_rtree.h"
911

10-
#include "skia/platform_view_rtree.h"
1112
#include "third_party/skia/include/core/SkCanvas.h"
1213
#include "third_party/skia/include/core/SkPictureRecorder.h"
1314

1415
namespace flutter {
1516
namespace testing {
1617

17-
using PlatformViewRTreeTest = ThreadTest;
18+
using PlatformViewRTree = ThreadTest;
1819

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);
2223
auto recorder = std::make_unique<SkPictureRecorder>();
2324
auto recording_canvas = recorder->beginRecording(SkRect::MakeIWH(1000, 1000), &rtree_factory);
2425

2526
auto rect_paint = SkPaint();
2627
rect_paint.setColor(SkColors::kCyan);
2728
rect_paint.setStyle(SkPaint::Style::kFill_Style);
2829

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);
3032
recorder->finishRecordingAsPicture();
3133

3234
auto hits = std::vector<SkRect*>();
33-
auto unobstructed = SkRect::MakeXYWH(40, 40, 20, 20);
35+
auto query = SkRect::MakeLTRB(40, 40, 80, 80);
3436

35-
r_tree->searchRects(unobstructed, &hits);
37+
r_tree->searchRects(query, &hits);
3638
ASSERT_TRUE(hits.empty());
3739
}
3840

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);
4268
auto recorder = std::make_unique<SkPictureRecorder>();
4369
auto recording_canvas = recorder->beginRecording(SkRect::MakeIWH(1000, 1000), &rtree_factory);
4470

4571
auto rect_paint = SkPaint();
4672
rect_paint.setColor(SkColors::kCyan);
4773
rect_paint.setStyle(SkPaint::Style::kFill_Style);
4874

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

5191
recorder->finishRecordingAsPicture();
5292

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);
5694
auto hits = std::vector<SkRect*>();
5795

58-
r_tree->searchRects(one_hit, &hits);
96+
r_tree->searchRects(query, &hits);
5997
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));
6199
}
62100

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);
66104
auto recorder = std::make_unique<SkPictureRecorder>();
67105
auto recording_canvas = recorder->beginRecording(SkRect::MakeIWH(1000, 1000), &rtree_factory);
68106

69107
auto rect_paint = SkPaint();
70108
rect_paint.setColor(SkColors::kCyan);
71109
rect_paint.setStyle(SkPaint::Style::kFill_Style);
72110

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

76134
recorder->finishRecordingAsPicture();
77135

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);
81137
auto hits = std::vector<SkRect*>();
82138

83-
r_tree->searchRects(one_hit, &hits);
139+
r_tree->searchRects(query, &hits);
84140
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));
86142
}
87143

88144
} // namespace testing

0 commit comments

Comments
 (0)