Skip to content

Commit

Permalink
Add first tests for TileRemovalState
Browse files Browse the repository at this point in the history
  • Loading branch information
rorystephenson committed Jul 24, 2023
1 parent fc884d1 commit 028ed3b
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 15 deletions.
134 changes: 134 additions & 0 deletions test/layer/tile_layer/tile_removal_state_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import 'package:flutter/src/scheduler/ticker.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_range.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_removal_state.dart';
import 'package:flutter_map/src/misc/private/bounds.dart';
import 'package:test/test.dart';

import '../../test_utils/test_tile_image.dart';

void main() {
group('tilesToPrune', () {
test('prunes tiles outside of the visible range', () {
final tileImages = [
MockTileImage(
coordinates: const TileCoordinates(1, 1, 1),
loadFinished: true,
readyToDisplay: true,
),
MockTileImage(
coordinates: const TileCoordinates(2, 1, 1),
loadFinished: true,
readyToDisplay: true,
),
];
final removalState = TileRemovalState(
tileImages: tileImages,
visibleRange: DiscreteTileRange(
1,
Bounds(const CustomPoint(2, 1), const CustomPoint(3, 3)),
),
keepRange: DiscreteTileRange(
1,
Bounds(const CustomPoint(2, 1), const CustomPoint(3, 3)),
),
evictStrategy: EvictErrorTileStrategy.none,
);
expect(removalState.tilesToPrune(), [tileImages.first]);
});

test('keeps ancestor tile if a tile has not loaded yet', () {
final tileImages = [
MockTileImage(
coordinates: const TileCoordinates(0, 0, 0),
loadFinished: true,
readyToDisplay: true,
),
MockTileImage(
coordinates: const TileCoordinates(0, 0, 1),
loadFinished: false,
readyToDisplay: false,
),
];
final removalState = TileRemovalState(
tileImages: tileImages,
visibleRange: DiscreteTileRange(
1,
Bounds(const CustomPoint(0, 0), const CustomPoint(0, 0)),
),
keepRange: DiscreteTileRange(
1,
Bounds(const CustomPoint(0, 0), const CustomPoint(0, 0)),
),
evictStrategy: EvictErrorTileStrategy.none,
);
expect(removalState.tilesToPrune(), isNot(contains(tileImages.first)));
});

test('keeps descendant tile if there is no loaded tile obscuring it', () {
final tileImages = [
MockTileImage(
coordinates: const TileCoordinates(0, 0, 0),
loadFinished: false,
readyToDisplay: false,
),
MockTileImage(
coordinates: const TileCoordinates(0, 0, 1),
loadFinished: false,
readyToDisplay: false,
),
MockTileImage(
coordinates: const TileCoordinates(0, 0, 2),
loadFinished: true,
readyToDisplay: true,
),
];
final removalState = TileRemovalState(
tileImages: tileImages,
visibleRange: DiscreteTileRange(
1,
Bounds(const CustomPoint(0, 0), const CustomPoint(0, 0)),
),
keepRange: DiscreteTileRange(
1,
Bounds(const CustomPoint(0, 0), const CustomPoint(0, 0)),
),
evictStrategy: EvictErrorTileStrategy.none,
);
expect(removalState.tilesToPrune(), isNot(contains(tileImages.last)));
});
});
}

class MockTileImage extends TileImage {
@override
final bool readyToDisplay;

MockTileImage({
required super.coordinates,
required this.readyToDisplay,
required bool loadFinished,
void Function(TileCoordinates coordinates)? onLoadComplete,
void Function(TileImage tile, Object error, StackTrace? stackTrace)?
onLoadError,
TileDisplay? tileDisplay,
super.errorImage,
}) : super(
vsync: const MockTickerProvider(),
imageProvider: testWhiteTileImage,
onLoadComplete: onLoadComplete ?? (_) {},
onLoadError: onLoadError ?? (_, __, ___) {},
tileDisplay: const TileDisplay.instantaneous(),
) {
loadFinishedAt = loadFinished ? DateTime.now() : null;
}
}

class MockTickerProvider extends TickerProvider {
const MockTickerProvider();

@override
Ticker createTicker(TickerCallback onTick) {
return Ticker((elapsed) {});
}
}
17 changes: 2 additions & 15 deletions test/test_utils/test_app.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_map/src/layer/circle_layer.dart';
import 'package:flutter_map/src/layer/marker_layer.dart';
import 'package:flutter_map/src/layer/polygon_layer.dart';
import 'package:flutter_map/src/layer/polyline_layer.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_coordinates.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_layer.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_provider/base_tile_provider.dart';
import 'package:flutter_map/src/map/map_controller.dart';
import 'package:flutter_map/src/map/options.dart';
import 'package:flutter_map/src/map/widget.dart';
import 'package:latlong2/latlong.dart';

import 'test_tile_provider.dart';

class TestApp extends StatelessWidget {
const TestApp({
super.key,
Expand Down Expand Up @@ -61,14 +59,3 @@ class TestApp extends StatelessWidget {
);
}
}

class TestTileProvider extends TileProvider {
// Base 64 encoded 256x256 white tile.
static const _whiteTile =
'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRF////p8QbyAAAAB9JREFUeJztwQENAAAAwqD3T20ON6AAAAAAAAAAAL4NIQAAAfFnIe4AAAAASUVORK5CYII=';

@override
ImageProvider<Object> getImage(
TileCoordinates coordinates, TileLayer options) =>
MemoryImage(base64Decode(_whiteTile));
}
8 changes: 8 additions & 0 deletions test/test_utils/test_tile_image.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'dart:convert';

import 'package:flutter/painting.dart';

// Base 64 encoded 256x256 white tile.
const _whiteTile =
'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRF////p8QbyAAAAB9JREFUeJztwQENAAAAwqD3T20ON6AAAAAAAAAAAL4NIQAAAfFnIe4AAAAASUVORK5CYII=';
final testWhiteTileImage = MemoryImage(base64Decode(_whiteTile));
13 changes: 13 additions & 0 deletions test/test_utils/test_tile_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/rendering.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_coordinates.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_layer.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_provider/base_tile_provider.dart';

import 'test_tile_image.dart';

class TestTileProvider extends TileProvider {
@override
ImageProvider<Object> getImage(
TileCoordinates coordinates, TileLayer options) =>
testWhiteTileImage;
}

0 comments on commit 028ed3b

Please sign in to comment.