Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix memory music startup behavior

ashilknd485da139812
diff --git a/mobile/apps/photos/lib/services/memories/memory_music_service.dart b/mobile/apps/photos/lib/services/memories/memory_music_service.dart
index 3ebfeb24be..3b2658d153 100644
--- a/mobile/apps/photos/lib/services/memories/memory_music_service.dart
+++ b/mobile/apps/photos/lib/services/memories/memory_music_service.dart
@@ -20,9 +20,12 @@ class MemoryMusicService {
final SharedPreferences _preferences;
final Future<Object?> Function() _fetchManifest;
+ final Future<bool> Function(String url) _hasAsset;
final Future<void> Function(String url) _cacheAsset;
final Future<void> Function(String url) _deleteAsset;
+ final Set<String> _availableURLs = {};
+ final Map<String, Future<bool>> _assetDownloads = {};
List<MemoryMusicTrack>? _tracks;
Future<List<MemoryMusicTrack>>? _preparation;
bool _manifestRefreshed = false;
@@ -30,10 +33,12 @@ class MemoryMusicService {
MemoryMusicService({
required SharedPreferences preferences,
required Future<Object?> Function() fetchManifest,
+ required Future<bool> Function(String url) hasAsset,
required Future<void> Function(String url) cacheAsset,
required Future<void> Function(String url) deleteAsset,
}) : _preferences = preferences,
_fetchManifest = fetchManifest,
+ _hasAsset = hasAsset,
_cacheAsset = cacheAsset,
_deleteAsset = deleteAsset;
@@ -45,6 +50,10 @@ class MemoryMusicService {
);
return response.data;
},
+ hasAsset: (url) => RemoteAssetsService.instance.hasAsset(
+ url,
+ cacheFileName: memoryMusicCacheFileName(url),
+ ),
cacheAsset: (url) async {
await RemoteAssetsService.instance.getAsset(
url,
@@ -153,26 +162,92 @@ class MemoryMusicService {
Future<List<MemoryMusicTrack>> _availableTracks(
List<MemoryMusicTrack> tracks,
) async {
- final availableURLs = <String>{};
+ final currentURLs = tracks.map((track) => track.url).toSet();
+ _availableURLs.retainAll(currentURLs);
await Future.wait(
- tracks.map((track) => track.url).toSet().map((url) async {
- try {
- await _cacheAsset(url);
- availableURLs.add(url);
- } catch (error, stackTrace) {
- _logger.warning(
- "Failed to cache memory music asset $url",
- error,
- stackTrace,
- );
- }
- }),
+ currentURLs
+ .where(
+ (url) =>
+ !_availableURLs.contains(url) &&
+ !_assetDownloads.containsKey(url),
+ )
+ .map(_findCachedAsset),
);
+
+ final downloads = <Future<bool>>[
+ for (final url in currentURLs)
+ if (!_availableURLs.contains(url)) _downloadAsset(url),
+ ];
+ if (_availableURLs.isEmpty && downloads.isNotEmpty) {
+ await _waitForAvailableAsset(downloads);
+ }
+
return tracks
- .where((track) => availableURLs.contains(track.url))
+ .where((track) => _availableURLs.contains(track.url))
.toList(growable: false);
}
+ Future<void> _findCachedAsset(String url) async {
+ try {
+ if (await _hasAsset(url)) _availableURLs.add(url);
+ } catch (error, stackTrace) {
+ _logger.warning(
+ "Failed to check memory music asset $url",
+ error,
+ stackTrace,
+ );
+ }
+ }
+
+ Future<bool> _downloadAsset(String url) {
+ final download = _assetDownloads[url];
+ if (download != null) return download;
+
+ late final Future<bool> nextDownload;
+ nextDownload = _cacheAssetURL(url).whenComplete(() {
+ if (identical(_assetDownloads[url], nextDownload)) {
+ _assetDownloads.remove(url);
+ }
+ });
+ _assetDownloads[url] = nextDownload;
+ return nextDownload;
+ }
+
+ Future<bool> _cacheAssetURL(String url) async {
+ try {
+ await _cacheAsset(url);
+ } catch (error, stackTrace) {
+ _logger.warning(
+ "Failed to cache memory music asset $url",
+ error,
+ stackTrace,
+ );
+ return false;
+ }
+
+ if (!_tracks!.any((track) => track.url == url)) {
+ return false;
+ }
+ _availableURLs.add(url);
+ return true;
+ }
+
+ Future<void> _waitForAvailableAsset(List<Future<bool>> downloads) {
+ final available = Completer<void>();
+ var remaining = downloads.length;
+ for (final download in downloads) {
+ unawaited(
+ download.then((didDownload) {
+ remaining--;
+ if (!available.isCompleted && (didDownload || remaining == 0)) {
+ available.complete();
+ }
+ }),
+ );
+ }
+ return available.future;
+ }
+
List<MemoryMusicTrack> _parseManifest(Object? manifest) {
if (manifest is! Map<String, dynamic>) {
throw const FormatException("Memory music manifest must be an object");
diff --git a/mobile/apps/photos/lib/services/remote_assets_service.dart b/mobile/apps/photos/lib/services/remote_assets_service.dart
index 195b46d623..759d2a8932 100644
--- a/mobile/apps/photos/lib/services/remote_assets_service.dart
+++ b/mobile/apps/photos/lib/services/remote_assets_service.dart
@@ -124,9 +124,12 @@ class RemoteAssetsService {
});
}
- Future<bool> hasAsset(String remotePath) async {
+ Future<bool> hasAsset(String remotePath, {String? cacheFileName}) async {
return _lockFor(remotePath).synchronized(() async {
- final path = await _getLocalPath(remotePath);
+ final path = await _getLocalPath(
+ remotePath,
+ cacheFileName: cacheFileName,
+ );
return File(path).exists();
});
}
diff --git a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart
index a7de5231d6..147efc3d60 100644
--- a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart
@@ -26,7 +26,6 @@ import "package:photos/models/selected_files.dart";
import "package:photos/module/download/file.dart";
import "package:photos/module/download/thumbnail.dart";
import "package:photos/service_locator.dart";
-import "package:photos/services/memories/memory_music_controller.dart";
import "package:photos/services/memory_share_service.dart";
import "package:photos/services/smart_memories_service.dart";
import "package:photos/ui/actions/file/file_actions.dart";
@@ -463,7 +462,10 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
);
if (index == null) return;
final file = inheritedData.memories[index].file;
- final controller = MemoryMusicScope.maybeOf(context, listen: false);
+ final controller = MemoryMusicScope.maybeOf(
+ context,
+ listen: false,
+ )?.controller;
if (controller == null) return;
unawaited(controller.setViewerActionPaused(_isMusicViewerActionPaused));
unawaited(
@@ -676,7 +678,10 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
void _pauseViewer() {
if (!mounted) return;
_isMusicViewerActionPaused = true;
- final controller = MemoryMusicScope.maybeOf(context, listen: false);
+ final controller = MemoryMusicScope.maybeOf(
+ context,
+ listen: false,
+ )?.controller;
if (controller != null) {
unawaited(controller.setViewerActionPaused(true));
}
@@ -689,7 +694,10 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
_isMusicViewerActionPaused = false;
Bus.instance.fire(ResumeVideoEvent());
_toggleAnimation(pause: false);
- final controller = MemoryMusicScope.maybeOf(context, listen: false);
+ final controller = MemoryMusicScope.maybeOf(
+ context,
+ listen: false,
+ )?.controller;
if (controller != null) {
unawaited(controller.setViewerActionPaused(false));
}
@@ -698,7 +706,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
@override
Widget build(BuildContext context) {
final inheritedData = FullScreenMemoryData.of(context);
- final memoryMusicController = MemoryMusicScope.maybeOf(context);
+ final memoryMusic = MemoryMusicScope.maybeOf(context);
if (inheritedData == null || inheritedData.memories.isEmpty) {
return const SizedBox.shrink();
}
@@ -798,7 +806,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
color: Colors.transparent,
),
isFromMemories: true,
- isAudioMutedOverride: memoryMusicController?.isMuted,
+ isAudioMutedOverride: memoryMusic?.isMuted,
shouldDisableScroll: (isLocked) {
_isMediaInteractionLocked = isLocked;
widget.onMediaInteractionLockChanged?.call(
@@ -839,7 +847,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
_MemoryViewerScrimsAndCaption(
socialControlsVisible: _socialControlsVisible,
),
- if (memoryMusicController != null)
+ if (memoryMusic != null)
Positioned(
left:
MediaQuery.paddingOf(context).left +
@@ -848,7 +856,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
MediaQuery.paddingOf(context).bottom +
kMemoryBottomActionBarHeight +
_socialToActionBarGap,
- child: _MemoryMusicMuteButton(memoryMusicController),
+ child: _MemoryMusicMuteButton(memoryMusic),
),
ValueListenableBuilder<int>(
valueListenable: inheritedData.indexNotifier,
@@ -1070,13 +1078,13 @@ class _MemoryActionButton extends StatelessWidget {
}
class _MemoryMusicMuteButton extends StatelessWidget {
- final MemoryMusicController controller;
+ final MemoryMusicScope memoryMusic;
- const _MemoryMusicMuteButton(this.controller);
+ const _MemoryMusicMuteButton(this.memoryMusic);
@override
Widget build(BuildContext context) {
- final isMuted = controller.isMuted;
+ final isMuted = memoryMusic.isMuted;
return SizedBox.square(
dimension: 48,
child: IconButton(
@@ -1091,7 +1099,7 @@ class _MemoryMusicMuteButton extends StatelessWidget {
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
overlayColor: Colors.transparent,
),
- onPressed: () => unawaited(controller.toggleMuted()),
+ onPressed: () => unawaited(memoryMusic.toggleMuted()),
icon: DecoratedBox(
decoration: const BoxDecoration(
color: Color(0x66000000),
diff --git a/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart b/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart
index 7149ced285..e7ba022837 100644
--- a/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart
@@ -1,6 +1,7 @@
import "dart:async";
import "package:flutter/material.dart";
+import "package:logging/logging.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/memories/just_audio_memory_music_player.dart";
import "package:photos/services/memories/memory_music_controller.dart";
@@ -23,12 +24,16 @@ class MemoryMusicSession extends StatefulWidget {
class _MemoryMusicSessionState extends State<MemoryMusicSession>
with WidgetsBindingObserver {
+ static final _logger = Logger("MemoryMusicSession");
+
MemoryMusicController? _controller;
bool _isAppActive = true;
+ late bool _isMuted;
@override
void initState() {
super.initState();
+ _isMuted = localSettings.isMemoriesAudioMuted();
WidgetsBinding.instance.addObserver(this);
final lifecycleState = WidgetsBinding.instance.lifecycleState;
_isAppActive =
@@ -45,7 +50,7 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
);
final controller = MemoryMusicController(
assignments: assignments,
- initiallyMuted: localSettings.isMemoriesAudioMuted(),
+ initiallyMuted: _isMuted,
persistMuted: localSettings.setMemoriesAudioMuted,
player: JustAudioMemoryMusicPlayer(),
tracks: tracks,
@@ -54,6 +59,26 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
setState(() => _controller = controller);
}
+ Future<void> _toggleMuted() async {
+ final controller = _controller;
+ if (controller != null) {
+ await controller.toggleMuted();
+ return;
+ }
+
+ final isMuted = !_isMuted;
+ setState(() => _isMuted = isMuted);
+ try {
+ await localSettings.setMemoriesAudioMuted(isMuted);
+ } catch (error, stackTrace) {
+ _logger.warning(
+ "Failed to persist memories audio mute state",
+ error,
+ stackTrace,
+ );
+ }
+ }
+
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_isAppActive = state == AppLifecycleState.resumed;
@@ -72,24 +97,42 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
@override
Widget build(BuildContext context) {
- return MemoryMusicScope(controller: _controller, child: widget.child);
+ return MemoryMusicScope(
+ controller: _controller,
+ isMuted: _isMuted,
+ toggleMuted: _toggleMuted,
+ child: widget.child,
+ );
}
}
class MemoryMusicScope extends InheritedNotifier<MemoryMusicController> {
+ final bool _isMuted;
+ final Future<void> Function() _toggleMuted;
+
const MemoryMusicScope({
required MemoryMusicController? controller,
+ required bool isMuted,
+ required Future<void> Function() toggleMuted,
required super.child,
super.key,
- }) : super(notifier: controller);
+ }) : _isMuted = isMuted,
+ _toggleMuted = toggleMuted,
+ super(notifier: controller);
+
+ MemoryMusicController? get controller => notifier;
+
+ bool get isMuted => notifier?.isMuted ?? _isMuted;
- static MemoryMusicController? maybeOf(
- BuildContext context, {
- bool listen = true,
- }) {
- final scope = listen
+ Future<void> toggleMuted() => _toggleMuted();
+
+ static MemoryMusicScope? maybeOf(BuildContext context, {bool listen = true}) {
+ return listen
? context.dependOnInheritedWidgetOfExactType<MemoryMusicScope>()
: context.getInheritedWidgetOfExactType<MemoryMusicScope>();
- return scope?.notifier;
}
+
+ @override
+ bool updateShouldNotify(MemoryMusicScope oldWidget) =>
+ _isMuted != oldWidget._isMuted || super.updateShouldNotify(oldWidget);
}
diff --git a/mobile/apps/photos/test/services/memories/memory_music_service_test.dart b/mobile/apps/photos/test/services/memories/memory_music_service_test.dart
index 11dad05178..b6195a960b 100644
--- a/mobile/apps/photos/test/services/memories/memory_music_service_test.dart
+++ b/mobile/apps/photos/test/services/memories/memory_music_service_test.dart
@@ -18,17 +18,20 @@ void main() {
preferences = await SharedPreferences.getInstance();
});
- test("sorts a valid manifest and caches each URL once", () async {
+ test("sorts a valid manifest and checks each cached URL once", () async {
final manifest = _manifest([
_track("z-track", _sharedURL, title: "Zed"),
_track("a-track", _sharedURL, artist: "Artist"),
_track("middle-track", _newURL),
]);
- final cachedURLs = <String>[];
+ final checkedURLs = <String>[];
final service = _service(
preferences,
fetchManifest: () async => manifest,
- cacheAsset: (url) async => cachedURLs.add(url),
+ hasAsset: (url) async {
+ checkedURLs.add(url);
+ return true;
+ },
);
final tracks = await service.prepare();
@@ -40,8 +43,8 @@ void main() {
]);
expect(tracks.first.artist, "Artist");
expect(tracks.last.title, "Zed");
- expect(cachedURLs, unorderedEquals([_sharedURL, _newURL]));
- expect(cachedURLs.where((url) => url == _sharedURL), hasLength(1));
+ expect(checkedURLs, unorderedEquals([_sharedURL, _newURL]));
+ expect(checkedURLs.where((url) => url == _sharedURL), hasLength(1));
expect(
jsonDecode(preferences.getString(_manifestPreferenceKey)!),
manifest,
@@ -127,20 +130,23 @@ void main() {
]),
),
);
- final cachedURLs = <String>[];
+ final availableURLs = <String>[];
final deletedURLs = <String>[];
final emptyManifest = _manifest([]);
final service = _service(
preferences,
fetchManifest: () async => emptyManifest,
- cacheAsset: (url) async => cachedURLs.add(url),
+ hasAsset: (url) async {
+ availableURLs.add(url);
+ return true;
+ },
deleteAsset: (url) async => deletedURLs.add(url),
);
final tracks = await service.prepare();
expect(tracks, isEmpty);
- expect(cachedURLs, isEmpty);
+ expect(availableURLs, isEmpty);
expect(deletedURLs, unorderedEquals([_cachedURL, _sharedURL]));
expect(deletedURLs.where((url) => url == _cachedURL), hasLength(1));
expect(
@@ -159,7 +165,7 @@ void main() {
]),
),
);
- final cachedURLs = <String>[];
+ final availableURLs = <String>[];
final deletedURLs = <String>[];
final service = _service(
preferences,
@@ -167,51 +173,107 @@ void main() {
_track("new-shared-id", _sharedURL),
_track("new", _newURL),
]),
- cacheAsset: (url) async => cachedURLs.add(url),
+ hasAsset: (url) async {
+ availableURLs.add(url);
+ return true;
+ },
deleteAsset: (url) async => deletedURLs.add(url),
);
final tracks = await service.prepare();
expect(tracks.map((track) => track.id), ["new", "new-shared-id"]);
- expect(cachedURLs, unorderedEquals([_newURL, _sharedURL]));
+ expect(availableURLs, unorderedEquals([_newURL, _sharedURL]));
expect(deletedURLs, [_cachedURL]);
});
- test(
- "filters an audio failure and retries it without refetching the manifest",
- () async {
- const firstURL = "https://music.ente.com/first.mp3";
- const secondURL = "https://music.ente.com/second.mp3";
- var fetchCalls = 0;
- final cacheAttempts = <String, int>{};
- final service = _service(
- preferences,
- fetchManifest: () async {
- fetchCalls++;
- return _manifest([
- _track("first", firstURL),
- _track("second", secondURL),
- ]);
- },
- cacheAsset: (url) async {
- final attempt = (cacheAttempts[url] ?? 0) + 1;
- cacheAttempts[url] = attempt;
- if (url == firstURL && attempt == 1) {
- throw StateError("download failed");
- }
- },
- );
+ test("returns cached tracks without waiting for a download", () async {
+ final downloadStarted = Completer<void>();
+ final finishDownload = Completer<void>();
+ addTearDown(() {
+ if (!finishDownload.isCompleted) finishDownload.complete();
+ });
+ final service = _service(
+ preferences,
+ fetchManifest: () async =>
+ _manifest([_track("cached", _cachedURL), _track("new", _newURL)]),
+ hasAsset: (url) async => url == _cachedURL,
+ cacheAsset: (url) async {
+ expect(url, _newURL);
+ downloadStarted.complete();
+ await finishDownload.future;
+ },
+ );
- final firstTracks = await service.prepare();
- final retriedTracks = await service.prepare();
+ final preparation = service.prepare();
+ await downloadStarted.future.timeout(const Duration(seconds: 1));
- expect(firstTracks.map((track) => track.id), ["second"]);
- expect(retriedTracks.map((track) => track.id), ["first", "second"]);
- expect(fetchCalls, 1);
- expect(cacheAttempts, <String, int>{firstURL: 2, secondURL: 2});
- },
- );
+ expect(
+ (await preparation.timeout(
+ const Duration(seconds: 1),
+ )).map((track) => track.id),
+ ["cached"],
+ );
+
+ finishDownload.complete();
+ await Future<void>.delayed(Duration.zero);
+ });
+
+ test("returns after the first download succeeds", () async {
+ const slowURL = "https://music.ente.com/slow.mp3";
+ final slowDownloadStarted = Completer<void>();
+ final finishSlowDownload = Completer<void>();
+ addTearDown(() {
+ if (!finishSlowDownload.isCompleted) finishSlowDownload.complete();
+ });
+ final service = _service(
+ preferences,
+ fetchManifest: () async =>
+ _manifest([_track("ready", _newURL), _track("slow", slowURL)]),
+ hasAsset: (_) async => false,
+ cacheAsset: (url) async {
+ if (url == slowURL) {
+ slowDownloadStarted.complete();
+ await finishSlowDownload.future;
+ }
+ },
+ );
+
+ final preparation = service.prepare();
+ await slowDownloadStarted.future.timeout(const Duration(seconds: 1));
+
+ expect(
+ (await preparation.timeout(
+ const Duration(seconds: 1),
+ )).map((track) => track.id),
+ ["ready"],
+ );
+
+ finishSlowDownload.complete();
+ await Future<void>.delayed(Duration.zero);
+ });
+
+ test("retries failed downloads without refetching the manifest", () async {
+ var fetchCalls = 0;
+ var cacheAttempts = 0;
+ final service = _service(
+ preferences,
+ fetchManifest: () async {
+ fetchCalls++;
+ return _manifest([_track("track", _newURL)]);
+ },
+ hasAsset: (_) async => false,
+ cacheAsset: (_) async {
+ cacheAttempts++;
+ if (cacheAttempts == 1) throw StateError("download failed");
+ },
+ );
+
+ expect(await service.prepare(), isEmpty);
+ expect((await service.prepare()).map((track) => track.id), ["track"]);
+ expect(fetchCalls, 1);
+ expect(cacheAttempts, 2);
+ });
test("concurrent preparation shares one manifest fetch", () async {
final fetchStarted = Completer<void>();
@@ -227,6 +289,7 @@ void main() {
return _manifest([_track("track", _newURL)]);
},
cacheAsset: (_) async => cacheCalls++,
+ hasAsset: (_) async => false,
);
final firstPreparation = service.prepare();
@@ -248,12 +311,14 @@ void main() {
MemoryMusicService _service(
SharedPreferences preferences, {
required Future<Object?> Function() fetchManifest,
+ Future<bool> Function(String url)? hasAsset,
Future<void> Function(String url)? cacheAsset,
Future<void> Function(String url)? deleteAsset,
}) {
return MemoryMusicService(
preferences: preferences,
fetchManifest: fetchManifest,
+ hasAsset: hasAsset ?? (_) async => true,
cacheAsset: cacheAsset ?? (_) async {},
deleteAsset: deleteAsset ?? (_) async {},
);
diff --git a/mobile/apps/photos/test/services/remote_assets_service_test.dart b/mobile/apps/photos/test/services/remote_assets_service_test.dart
index ba6d455c7a..539c35e185 100644
--- a/mobile/apps/photos/test/services/remote_assets_service_test.dart
+++ b/mobile/apps/photos/test/services/remote_assets_service_test.dart
@@ -26,6 +26,7 @@ void main() {
test("deleteAsset removes every custom-named cache artifact", () async {
const cacheFileName = "memory-music-track.mp3";
+ const remotePath = "https://music.ente.com/track.mp3";
final localPath = p.join(tempDirectory.path, "assets", cacheFileName);
final artifacts = await _createAssetArtifacts(localPath);
final unrelatedFile = File(
@@ -33,8 +34,16 @@ void main() {
);
await unrelatedFile.writeAsString("unrelated");
+ expect(
+ await RemoteAssetsService.instance.hasAsset(
+ remotePath,
+ cacheFileName: cacheFileName,
+ ),
+ isTrue,
+ );
+
await RemoteAssetsService.instance.deleteAsset(
- "https://music.ente.com/track.mp3",
+ remotePath,
cacheFileName: cacheFileName,
);
@@ -42,9 +51,16 @@ void main() {
expect(await artifact.exists(), isFalse);
}
expect(await unrelatedFile.exists(), isTrue);
+ expect(
+ await RemoteAssetsService.instance.hasAsset(
+ remotePath,
+ cacheFileName: cacheFileName,
+ ),
+ isFalse,
+ );
await RemoteAssetsService.instance.deleteAsset(
- "https://music.ente.com/track.mp3",
+ remotePath,
cacheFileName: cacheFileName,
);
});
diff --git a/mobile/apps/photos/test/ui/home/memories/memory_music_session_test.dart b/mobile/apps/photos/test/ui/home/memories/memory_music_session_test.dart
new file mode 100644
index 0000000000..027aa136d8
--- /dev/null
+++ b/mobile/apps/photos/test/ui/home/memories/memory_music_session_test.dart
@@ -0,0 +1,48 @@
+import "package:flutter/material.dart";
+import "package:flutter_test/flutter_test.dart";
+import "package:photos/ui/home/memories/memory_music_session.dart";
+
+void main() {
+ testWidgets("updates mute state before the controller is ready", (
+ tester,
+ ) async {
+ var isMuted = true;
+
+ await tester.pumpWidget(
+ MaterialApp(
+ home: StatefulBuilder(
+ builder: (context, setState) {
+ return MemoryMusicScope(
+ controller: null,
+ isMuted: isMuted,
+ toggleMuted: () async {
+ setState(() => isMuted = !isMuted);
+ },
+ child: const _MuteButton(),
+ );
+ },
+ ),
+ ),
+ );
+
+ expect(find.text("muted"), findsOneWidget);
+
+ await tester.tap(find.byType(TextButton));
+ await tester.pump();
+
+ expect(find.text("unmuted"), findsOneWidget);
+ });
+}
+
+class _MuteButton extends StatelessWidget {
+ const _MuteButton();
+
+ @override
+ Widget build(BuildContext context) {
+ final memoryMusic = MemoryMusicScope.maybeOf(context)!;
+ return TextButton(
+ onPressed: memoryMusic.toggleMuted,
+ child: Text(memoryMusic.isMuted ? "muted" : "unmuted"),
+ );
+ }
+}