Three pure modules: tile_math.dart (tilesForBounds/tilesAlongRoute, hard cap enforced by throwing), tile_cache.dart (FileTileCache with LRU eviction before any write that would exceed the cap), tile_downloader.dart (sequential, rate-limited, cooperatively cancellable, one bad tile doesn't abort the rest). CachedTileProvider wires the cache into flutter_map via a custom ImageProvider and now backs RideMap and RoutePlannerScreen's tile layers, so ordinary viewing write-throughs into the same capped cache. RoutePlannerScreen gained a route-corridor download action (no rectangle-selection UI -- the ticket names the corridor as strictly better and V3-07 already exists to hang it off of), with a count/size confirmation before any request and a cancellable progress dialog. Fixed a real bug before shipping: a StatefulBuilder-based progress dialog would have started a new overlapping download subscription on every single progress tick; moved to a dedicated StatefulWidget that subscribes once in initState. Marked partially done: aeroplane-mode verification on a real device is the ticket's own acceptance criterion and needs a phone this environment doesn't have.
93 lines
3.2 KiB
Dart
93 lines
3.2 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:rippr/src/tiles/tile_cache.dart';
|
|
import 'package:rippr/src/tiles/tile_math.dart';
|
|
|
|
void main() {
|
|
late Directory tempDir;
|
|
late FileTileCache cache;
|
|
|
|
setUp(() async {
|
|
tempDir = await Directory.systemTemp.createTemp('rippr_tile_cache_test_');
|
|
});
|
|
|
|
tearDown(() async {
|
|
if (tempDir.existsSync()) tempDir.deleteSync(recursive: true);
|
|
});
|
|
|
|
Uint8List bytesOfSize(int n) => Uint8List.fromList(List.filled(n, 1));
|
|
|
|
test('a stored tile round-trips', () async {
|
|
cache = FileTileCache(directory: tempDir, maxBytes: 1024 * 1024);
|
|
const key = TileKey(12, 100, 200);
|
|
|
|
await cache.put(key, bytesOfSize(50));
|
|
final read = await cache.get(key);
|
|
|
|
expect(read, isNotNull);
|
|
expect(read!.length, 50);
|
|
});
|
|
|
|
test('a tile never written is a miss, not an error', () async {
|
|
cache = FileTileCache(directory: tempDir, maxBytes: 1024 * 1024);
|
|
expect(await cache.get(const TileKey(1, 1, 1)), isNull);
|
|
});
|
|
|
|
test('sizeBytes reflects everything stored', () async {
|
|
cache = FileTileCache(directory: tempDir, maxBytes: 1024 * 1024);
|
|
await cache.put(const TileKey(1, 0, 0), bytesOfSize(100));
|
|
await cache.put(const TileKey(1, 0, 1), bytesOfSize(200));
|
|
|
|
expect(await cache.sizeBytes(), 300);
|
|
});
|
|
|
|
test('eviction at the cap removes the least-recently-used tile first', () async {
|
|
// Room for two 100-byte tiles at a time.
|
|
cache = FileTileCache(directory: tempDir, maxBytes: 200);
|
|
const a = TileKey(1, 0, 0);
|
|
const b = TileKey(1, 0, 1);
|
|
const c = TileKey(1, 0, 2);
|
|
|
|
await cache.put(a, bytesOfSize(100));
|
|
await cache.put(b, bytesOfSize(100));
|
|
// Touch `a` so `b` becomes the least-recently-used.
|
|
await cache.get(a);
|
|
await cache.put(c, bytesOfSize(100));
|
|
|
|
expect(await cache.get(a), isNotNull, reason: 'recently touched, must survive');
|
|
expect(await cache.get(b), isNull, reason: 'least-recently-used, must be evicted');
|
|
expect(await cache.get(c), isNotNull, reason: 'just written, must survive');
|
|
expect(await cache.sizeBytes(), lessThanOrEqualTo(200));
|
|
});
|
|
|
|
test('the cache never exceeds its cap after many writes', () async {
|
|
cache = FileTileCache(directory: tempDir, maxBytes: 500);
|
|
for (var i = 0; i < 20; i++) {
|
|
await cache.put(TileKey(1, i, 0), bytesOfSize(100));
|
|
}
|
|
expect(await cache.sizeBytes(), lessThanOrEqualTo(500));
|
|
});
|
|
|
|
test('clear removes every tile and resets size to zero', () async {
|
|
cache = FileTileCache(directory: tempDir, maxBytes: 1024 * 1024);
|
|
await cache.put(const TileKey(1, 0, 0), bytesOfSize(100));
|
|
await cache.put(const TileKey(1, 0, 1), bytesOfSize(100));
|
|
|
|
await cache.clear();
|
|
|
|
expect(await cache.sizeBytes(), 0);
|
|
expect(await cache.get(const TileKey(1, 0, 0)), isNull);
|
|
});
|
|
|
|
test('a cache re-opened over the same directory sees what was stored', () async {
|
|
cache = FileTileCache(directory: tempDir, maxBytes: 1024 * 1024);
|
|
await cache.put(const TileKey(5, 10, 10), bytesOfSize(64));
|
|
|
|
final reopened = FileTileCache(directory: tempDir, maxBytes: 1024 * 1024);
|
|
expect(await reopened.get(const TileKey(5, 10, 10)), isNotNull);
|
|
expect(await reopened.sizeBytes(), 64);
|
|
});
|
|
}
|