diff --git a/lib/services/platform_bridge.dart b/lib/services/platform_bridge.dart index 7f46998f..f85af4ae 100644 --- a/lib/services/platform_bridge.dart +++ b/lib/services/platform_bridge.dart @@ -145,7 +145,9 @@ class PlatformBridge { static const _bridgeCacheMaxBytes = 8 * 1024 * 1024; static const _persistentCacheMaxEntryBytes = 1024 * 1024; static const _persistentCacheMaxBytes = 2 * 1024 * 1024; - static const _lookupCachePersistDebounce = Duration(milliseconds: 500); + static const _defaultLookupCachePersistDebounce = Duration(milliseconds: 500); + static Duration _lookupCachePersistDebounce = + _defaultLookupCachePersistDebounce; static const _metadataPersistentCacheKey = 'bridge_metadata_lookup_cache_v1'; static const _downloadProgressEvents = EventChannel( 'com.zarz.spotiflac/download_progress_stream', @@ -574,6 +576,13 @@ class PlatformBridge { } } + /// Shortens the lookup cache persist debounce so tests need not wait the + /// production delay. Null restores the default. + @visibleForTesting + static set lookupCachePersistDebounceForTesting(Duration? value) { + _lookupCachePersistDebounce = value ?? _defaultLookupCachePersistDebounce; + } + static void _scheduleLookupCachePersist( Map>> cache, String prefsKey, diff --git a/test/platform_bridge_lookup_cache_test.dart b/test/platform_bridge_lookup_cache_test.dart index ca953bb0..79a2c481 100644 --- a/test/platform_bridge_lookup_cache_test.dart +++ b/test/platform_bridge_lookup_cache_test.dart @@ -13,16 +13,22 @@ void main() { final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; + // The waits below keep the original margin after the debounce fires (50 to + // 250 ms for the background encode) while skipping the 500 ms default. + const debounce = Duration(milliseconds: 10); + setUp(() async { SharedPreferences.setMockInitialValues({}); messenger.setMockMethodCallHandler(channel, (_) async => null); await PlatformBridge.clearTrackCache(); + PlatformBridge.lookupCachePersistDebounceForTesting = debounce; }); tearDown(() async { messenger.setMockMethodCallHandler(channel, (_) async => null); await PlatformBridge.clearTrackCache(); messenger.setMockMethodCallHandler(channel, null); + PlatformBridge.lookupCachePersistDebounceForTesting = null; }); Map collection([int count = 2]) => { @@ -57,7 +63,7 @@ void main() { expect(results.last['text'], isNot('changed')); expect((await load())['text'], isNot('changed')); expect(calls, 2); - await Future.delayed(const Duration(milliseconds: 550)); + await Future.delayed(debounce + const Duration(milliseconds: 50)); final prefs = await SharedPreferences.getInstance(); final stored = prefs.getString('bridge_metadata_lookup_cache_v1'); expect(stored == null || jsonDecode(stored) is Map, true); @@ -95,7 +101,7 @@ void main() { }); await PlatformBridge.getProviderMetadata('example', 'album', 'large'); await PlatformBridge.getProviderMetadata('example', 'track', 'small'); - await Future.delayed(const Duration(milliseconds: 650)); + await Future.delayed(debounce + const Duration(milliseconds: 150)); final prefs = await SharedPreferences.getInstance(); final stored = prefs.getString('bridge_metadata_lookup_cache_v1')!; expect(utf8.encode(stored).length, lessThanOrEqualTo(2 * 1024 * 1024)); @@ -120,7 +126,7 @@ void main() { for (final id in ['escaped', 'multibyte', 'small']) { await PlatformBridge.getProviderMetadata('example', 'album', id); } - await Future.delayed(const Duration(milliseconds: 750)); + await Future.delayed(debounce + const Duration(milliseconds: 250)); final prefs = await SharedPreferences.getInstance(); final stored = jsonDecode(prefs.getString('bridge_metadata_lookup_cache_v1')!) @@ -137,7 +143,7 @@ void main() { for (var index = 0; index < 3; index++) { await PlatformBridge.getProviderMetadata('example', 'album', '$index'); } - await Future.delayed(const Duration(milliseconds: 750)); + await Future.delayed(debounce + const Duration(milliseconds: 250)); final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString('bridge_metadata_lookup_cache_v1')!; expect(utf8.encode(raw).length, lessThanOrEqualTo(2 * 1024 * 1024));