Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions lib/src/layer/shared/feature_layer_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,24 @@ mixin FeatureLayerUtils on CustomPainter {

if (worldWidth == 0) return false;

negativeWorldsLoop:
for (double shift = -worldWidth;; shift -= worldWidth) {
protectInfiniteLoop();
switch (work(shift)) {
case WorldWorkControl.hit:
return true;
case WorldWorkControl.invisible:
break negativeWorldsLoop;
case WorldWorkControl.visible:
// Each direction leaves its loop with a `return`, never with a labeled
// `break` from inside the `switch`: DDC (Dart 3.14) compiles the latter to
// a `break` of the `switch` only, so the loop never ends.
// See https://github.com/dart-lang/sdk/issues/64941.
bool workInDirection(double worldShift) {
for (double shift = worldShift;; shift += worldShift) {
protectInfiniteLoop();
switch (work(shift)) {
case WorldWorkControl.hit:
return true;
case WorldWorkControl.invisible:
return false;
case WorldWorkControl.visible:
}
}
}

for (double shift = worldWidth;; shift += worldWidth) {
protectInfiniteLoop();
switch (work(shift)) {
case WorldWorkControl.hit:
return true;
case WorldWorkControl.invisible:
return false;
case WorldWorkControl.visible:
}
}
return workInDirection(-worldWidth) || workInDirection(worldWidth);
}

/// Returns the world size in pixels.
Expand Down
102 changes: 102 additions & 0 deletions test/layer/shared/feature_layer_utils_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/layer/shared/feature_layer_utils.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:latlong2/latlong.dart';

class _TestPainter extends CustomPainter with FeatureLayerUtils {
_TestPainter(this.camera);

@override
final MapCamera camera;

@override
// Required by `@mustBeOverridden`.
// ignore: unnecessary_overrides
void paint(Canvas canvas, Size size) => super.paint(canvas, size);

@override
bool shouldRepaint(_TestPainter oldDelegate) => false;
}

_TestPainter _painter({Crs crs = const Epsg3857()}) => _TestPainter(
MapCamera(
crs: crs,
center: const LatLng(0, 0),
zoom: 3,
rotation: 0,
nonRotatedSize: const Size(200, 200),
),
);

void main() {
group('FeatureLayerUtils.workAcrossWorlds', () {
test('stops in each direction at the first invisible world', () {
final painter = _painter();
final worldWidth = painter.worldWidth;
final shifts = <double>[];

final result = painter.workAcrossWorlds((shift) {
shifts.add(shift);
return shift == 0
? WorldWorkControl.visible
: WorldWorkControl.invisible;
});

expect(result, isFalse);
expect(shifts, [0, -worldWidth, worldWidth]);
});

test('keeps going in each direction while worlds are visible', () {
final painter = _painter();
final worldWidth = painter.worldWidth;
final shifts = <double>[];

final result = painter.workAcrossWorlds((shift) {
shifts.add(shift);
return shift.abs() <= worldWidth
? WorldWorkControl.visible
: WorldWorkControl.invisible;
});

expect(result, isFalse);
expect(
shifts,
[0, -worldWidth, -2 * worldWidth, worldWidth, 2 * worldWidth],
);
});

test('returns true as soon as a world is hit', () {
final painter = _painter();
final worldWidth = painter.worldWidth;

for (final hitShift in [0.0, -worldWidth, worldWidth]) {
final shifts = <double>[];

final result = painter.workAcrossWorlds((shift) {
shifts.add(shift);
if (shift == hitShift) return WorldWorkControl.hit;
return shift == 0
? WorldWorkControl.visible
: WorldWorkControl.invisible;
});

expect(result, isTrue);
expect(shifts.last, hitShift);
}
});

test('only works in the primary world if the CRS is not replicated', () {
final painter = _painter(crs: const Epsg4326());
final shifts = <double>[];

final result = painter.workAcrossWorlds((shift) {
shifts.add(shift);
return WorldWorkControl.visible;
});

expect(result, isFalse);
expect(shifts, [0]);
});
});
}