π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] add video seek tests
r4khul8ebd5ebb833b
diff --git a/mobile/apps/photos/changes/video-double-tap-seek.md b/mobile/apps/photos/changes/video-double-tap-seek.mdindex 430ac3336a..d71c440d3b 100644--- a/mobile/apps/photos/changes/video-double-tap-seek.md+++ b/mobile/apps/photos/changes/video-double-tap-seek.md@@ -1,2 +1 @@-- Added double-tap video seek. (@r4khul)-- Fixed video seek bar snapping glitch during manual drag. (@r4khul)+- Added five-second double-tap seeking for videos. (@r4khul)diff --git a/mobile/apps/photos/lib/ui/viewer/file/native_video_player_controls/seek_bar.dart b/mobile/apps/photos/lib/ui/viewer/file/native_video_player_controls/seek_bar.dartindex 5e606fdb9a..c4f0cea692 100644--- a/mobile/apps/photos/lib/ui/viewer/file/native_video_player_controls/seek_bar.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/native_video_player_controls/seek_bar.dart@@ -172,6 +172,10 @@ class _NativeVideoProgressControlsStatevoid _onPlaybackPositionChanged() {final target = widget.controller.playbackPosition.inMilliseconds;+ if (widget.controller.playbackStatus == PlaybackStatus.stopped &&+ target != 0) {+ return;+ }final duration = _effectiveDurationInMilliseconds();widget.seekController.onPlayerPosition(Duration(milliseconds: target),diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_double_tap_seek.dart b/mobile/apps/photos/lib/ui/viewer/file/video_double_tap_seek.dartindex fdadf23486..839e9c8a5e 100644--- a/mobile/apps/photos/lib/ui/viewer/file/video_double_tap_seek.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_double_tap_seek.dart@@ -41,6 +41,7 @@ class _DoubleTapSeekOverlayState extends State<DoubleTapSeekOverlay>int _accumulatedSeconds = 0;DateTime? _lastDoubleTapTime;bool? _lastDirection;+ Duration? _lastSeekTarget;TapDownDetails? _doubleTapDetails;int _seekGeneration = 0;late final AnimationController _badgeAnimation;@@ -80,17 +81,28 @@ class _DoubleTapSeekOverlayState extends State<DoubleTapSeekOverlay>final target = widget.seekBy(isForward ? kDefaultSeekDuration : -kDefaultSeekDuration,);- final applied = (target - position).abs();- if (applied == Duration.zero) return;- final appliedSeconds = (applied.inMilliseconds / 1000).ceil();+ if (isFastSequence && target == _lastSeekTarget) {+ _lastDoubleTapTime = now;+ _showSeekBadge(isForward);+ return;+ }+ if (target == position) {+ if (_showBadge) {+ _lastDoubleTapTime = now;+ _showSeekBadge(isForward);+ }+ return;+ }+ final requestedSeconds = kDefaultSeekDuration.inSeconds;if (isFastSequence) {- _accumulatedSeconds += appliedSeconds;+ _accumulatedSeconds += requestedSeconds;} else {- _accumulatedSeconds = appliedSeconds;+ _accumulatedSeconds = requestedSeconds;_lastDirection = isForward;}_lastDoubleTapTime = now;+ _lastSeekTarget = target;_seekGeneration++;widget.onSeekInteraction?.call();@@ -110,6 +122,7 @@ class _DoubleTapSeekOverlayState extends State<DoubleTapSeekOverlay>setState(() {_showBadge = false;_lastDoubleTapTime = null;+ _lastSeekTarget = null;});});}diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_seek_controller.dart b/mobile/apps/photos/lib/ui/viewer/file/video_seek_controller.dartindex 45bed8f2cf..c26a20e9c0 100644--- a/mobile/apps/photos/lib/ui/viewer/file/video_seek_controller.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_seek_controller.dart@@ -46,7 +46,6 @@ class VideoSeekState {}class VideoSeekController extends ChangeNotifier {- static const _seekCommandTimeout = Duration(seconds: 2);static const _positionTolerance = Duration(milliseconds: 750);static const _reconciliationTimeout = Duration(seconds: 2);static const _sliderSeekInterval = Duration(milliseconds: 300);@@ -165,7 +164,6 @@ class VideoSeekController extends ChangeNotifier {_sliderSeekTimer = null;_queuedTarget = null;_clearReconciliation();- _commandInFlight = false;_confirmedPosition = position;_setState(VideoSeekState(@@ -218,7 +216,7 @@ class VideoSeekController extends ChangeNotifier {final requestId = _state.requestId;_queuedTarget = null;try {- await _seek(target).timeout(_seekCommandTimeout);+ await _seek(target);if (!_disposed &&sessionId == _sessionId &&requestId == _state.requestId &&@@ -244,11 +242,12 @@ class VideoSeekController extends ChangeNotifier {}}} finally {- if (!_disposed && sessionId == _sessionId) {- _commandInFlight = false;+ _commandInFlight = false;+ if (!_disposed) {if (_queuedTarget != null) {_startCommandDrain();- } else if (_expectedPosition == null &&+ } else if (sessionId == _sessionId &&+ _expectedPosition == null &&_state.phase != VideoSeekPhase.dragging) {_setState(_state.copyWith(phase: VideoSeekPhase.idle));}diff --git a/mobile/apps/photos/test/ui/viewer/file/video_double_tap_seek_test.dart b/mobile/apps/photos/test/ui/viewer/file/video_double_tap_seek_test.dartnew file mode 100644index 0000000000..8ce8c150cf--- /dev/null+++ b/mobile/apps/photos/test/ui/viewer/file/video_double_tap_seek_test.dart@@ -0,0 +1,188 @@+import "package:flutter/material.dart";+import "package:flutter_test/flutter_test.dart";+import "package:photos/ui/viewer/file/video_double_tap_seek.dart";++void main() {+ testWidgets("opposite-direction double taps reset the accumulated badge", (+ tester,+ ) async {+ final seeks = <Duration>[];+ var position = const Duration(seconds: 30);++ await tester.pumpWidget(+ MaterialApp(+ home: SizedBox(+ width: 400,+ height: 300,+ child: DoubleTapSeekOverlay(+ enabled: () => true,+ position: () => position,+ duration: () => const Duration(seconds: 60),+ seekBy: (delta) {+ position += delta;+ if (position < Duration.zero) position = Duration.zero;+ if (position > const Duration(seconds: 60)) {+ position = const Duration(seconds: 60);+ }+ seeks.add(position);+ return position;+ },+ ),+ ),+ ),+ );++ await _doubleTap(tester, forward: true);+ await _doubleTap(tester, forward: false);++ expect(seeks, [const Duration(seconds: 35), const Duration(seconds: 30)]);+ expect(find.text("5s"), findsOneWidget);+ });++ testWidgets("rapid same-direction double taps accumulate the badge", (+ tester,+ ) async {+ final seeks = <Duration>[];+ var position = Duration.zero;++ await tester.pumpWidget(+ MaterialApp(+ home: SizedBox(+ width: 400,+ height: 300,+ child: DoubleTapSeekOverlay(+ enabled: () => true,+ position: () => position,+ duration: () => const Duration(seconds: 40),+ seekBy: (delta) {+ position += delta;+ if (position > const Duration(seconds: 40)) {+ position = const Duration(seconds: 40);+ }+ seeks.add(position);+ return position;+ },+ ),+ ),+ ),+ );++ for (var i = 0; i < 8; i++) {+ await _doubleTap(tester, forward: true);+ }++ expect(seeks, [+ for (var seconds = 5; seconds <= 40; seconds += 5)+ Duration(seconds: seconds),+ ]);+ expect(find.text("40s"), findsOneWidget);+ });++ testWidgets(+ "keeps the requested badge when position updates are stale at a boundary",+ (tester) async {+ final seeks = <Duration>[];+ const position = Duration(seconds: 2);++ await tester.pumpWidget(+ MaterialApp(+ home: SizedBox(+ width: 400,+ height: 300,+ child: DoubleTapSeekOverlay(+ enabled: () => true,+ position: () => position,+ duration: () => const Duration(seconds: 60),+ seekBy: (delta) {+ final unclampedTarget = position + delta;+ final target = unclampedTarget < Duration.zero+ ? Duration.zero+ : unclampedTarget > const Duration(seconds: 60)+ ? const Duration(seconds: 60)+ : unclampedTarget;+ seeks.add(target);+ return target;+ },+ ),+ ),+ ),+ );++ await _doubleTap(tester, forward: false);+ await _doubleTap(tester, forward: false);++ expect(seeks, [Duration.zero, Duration.zero]);+ expect(find.text("5s"), findsOneWidget);+ },+ );++ testWidgets("reports the requested seek at a boundary", (tester) async {+ var position = const Duration(seconds: 57);++ await tester.pumpWidget(+ MaterialApp(+ home: SizedBox(+ width: 400,+ height: 300,+ child: DoubleTapSeekOverlay(+ enabled: () => true,+ position: () => position,+ duration: () => const Duration(seconds: 60),+ seekBy: (delta) {+ position += delta;+ if (position > const Duration(seconds: 60)) {+ position = const Duration(seconds: 60);+ }+ return position;+ },+ ),+ ),+ ),+ );++ await _doubleTap(tester, forward: true);++ expect(find.text("5s"), findsOneWidget);+ });++ testWidgets("does not seek while the overlay is disabled", (tester) async {+ var seekCount = 0;++ await tester.pumpWidget(+ MaterialApp(+ home: SizedBox(+ width: 400,+ height: 300,+ child: DoubleTapSeekOverlay(+ enabled: () => false,+ position: () => const Duration(seconds: 30),+ duration: () => const Duration(seconds: 60),+ seekBy: (_) {+ seekCount++;+ return const Duration(seconds: 30);+ },+ ),+ ),+ ),+ );++ await _doubleTap(tester, forward: true);++ expect(seekCount, 0);+ });+}++Future<void> _doubleTap(WidgetTester tester, {required bool forward}) async {+ final rect = tester.getRect(find.byType(DoubleTapSeekOverlay));+ final position = Offset(+ rect.left + rect.width * (forward ? 0.75 : 0.25),+ rect.center.dy,+ );+ final firstTap = await tester.startGesture(position);+ await firstTap.up();+ await tester.pump(const Duration(milliseconds: 50));++ final secondTap = await tester.startGesture(position);+ await secondTap.up();+ await tester.pump(const Duration(milliseconds: 400));+}diff --git a/mobile/apps/photos/test/ui/viewer/file/video_seek_controller_test.dart b/mobile/apps/photos/test/ui/viewer/file/video_seek_controller_test.dartnew file mode 100644index 0000000000..0e0a3e7be5--- /dev/null+++ b/mobile/apps/photos/test/ui/viewer/file/video_seek_controller_test.dart@@ -0,0 +1,225 @@+import "dart:async";++import "package:flutter_test/flutter_test.dart";+import "package:photos/ui/viewer/file/video_seek_controller.dart";++void main() {+ test(+ "retains the latest user intent while an earlier seek is in flight",+ () async {+ final firstSeek = Completer<void>();+ final seeks = <Duration>[];+ final controller = VideoSeekController(+ seek: (target) {+ seeks.add(target);+ return seeks.length == 1 ? firstSeek.future : Future.value();+ },+ readPosition: () => const Duration(seconds: 40),+ readDuration: () => const Duration(seconds: 60),+ );+ controller.onPlayerPosition(const Duration(seconds: 30));++ controller.seekBy(const Duration(seconds: 5));+ await _flushMicrotasks();+ controller.seekBy(const Duration(seconds: 5));++ expect(controller.position, const Duration(seconds: 40));+ expect(seeks, [const Duration(seconds: 35)]);++ firstSeek.complete();+ await _flushMicrotasks();++ expect(seeks, [const Duration(seconds: 35), const Duration(seconds: 40)]);+ controller.onPlayerPosition(const Duration(seconds: 40));+ expect(controller.state.phase, VideoSeekPhase.idle);+ controller.dispose();+ },+ );++ test(+ "rejects a stale player event until the requested target is observed",+ () async {+ final controller = VideoSeekController(+ seek: (_) async {},+ readPosition: () => const Duration(seconds: 35),+ readDuration: () => const Duration(seconds: 60),+ );+ controller.onPlayerPosition(const Duration(seconds: 30));++ controller.seekBy(const Duration(seconds: 5));+ await _flushMicrotasks();+ controller.onPlayerPosition(const Duration(seconds: 10));++ expect(controller.position, const Duration(seconds: 35));++ controller.onPlayerPosition(const Duration(seconds: 35));+ expect(controller.position, const Duration(seconds: 35));+ expect(controller.state.phase, VideoSeekPhase.idle);+ controller.dispose();+ },+ );++ test("a source reset keeps an in-flight seek serialized", () async {+ final firstSeek = Completer<void>();+ final seeks = <Duration>[];+ final controller = VideoSeekController(+ seek: (target) {+ seeks.add(target);+ return seeks.length == 1 ? firstSeek.future : Future.value();+ },+ readPosition: () => const Duration(seconds: 12),+ readDuration: () => const Duration(seconds: 60),+ );++ controller.seekBy(const Duration(seconds: 5));+ await _flushMicrotasks();+ controller.reset(+ position: const Duration(seconds: 12),+ duration: const Duration(seconds: 60),+ );+ controller.seekBy(const Duration(seconds: 5));+ await _flushMicrotasks();++ expect(seeks, [const Duration(seconds: 5)]);++ firstSeek.complete();+ await _flushMicrotasks();++ expect(seeks, [const Duration(seconds: 5), const Duration(seconds: 17)]);+ controller.onPlayerPosition(const Duration(seconds: 17));+ expect(controller.state.phase, VideoSeekPhase.idle);+ controller.dispose();+ });++ testWidgets("reconciles a seek when no matching event arrives", (+ tester,+ ) async {+ var playerPosition = const Duration(seconds: 30);+ final controller = VideoSeekController(+ seek: (target) async {+ playerPosition = target - const Duration(seconds: 1);+ },+ readPosition: () => playerPosition,+ readDuration: () => const Duration(seconds: 60),+ );+ controller.onPlayerPosition(playerPosition);++ controller.seekBy(const Duration(seconds: 5));+ await tester.pump();+ expect(controller.state.phase, VideoSeekPhase.settling);++ await tester.pump(const Duration(seconds: 2));+ expect(controller.position, const Duration(seconds: 34));+ expect(controller.state.phase, VideoSeekPhase.idle);+ controller.dispose();+ });++ test("does not notify listeners for an unchanged player position", () {+ final controller = VideoSeekController(+ seek: (_) async {},+ readPosition: () => const Duration(seconds: 30),+ readDuration: () => const Duration(seconds: 60),+ );+ var notifications = 0;+ controller.addListener(() => notifications++);++ controller.onPlayerPosition(const Duration(seconds: 30));+ notifications = 0;+ controller.onPlayerPosition(const Duration(seconds: 30));++ expect(notifications, 0);+ controller.dispose();+ });++ test("does not enter dragging until a duration is available", () {+ final controller = VideoSeekController(+ seek: (_) async {},+ readPosition: () => Duration.zero,+ readDuration: () => null,+ );++ controller.beginSliderInteraction();+ controller.endSliderInteraction(Duration.zero);++ expect(controller.state.phase, VideoSeekPhase.idle);+ controller.dispose();+ });++ testWidgets("a slider release seeks to its final target", (tester) async {+ final seeks = <Duration>[];+ final controller = VideoSeekController(+ seek: (target) async {+ seeks.add(target);+ },+ readPosition: () => const Duration(seconds: 10),+ readDuration: () => const Duration(seconds: 60),+ );++ controller.beginSliderInteraction();+ controller.updateSliderTarget(const Duration(seconds: 20));+ controller.endSliderInteraction(const Duration(seconds: 40));+ await tester.pump();++ expect(seeks, [const Duration(seconds: 40)]);+ controller.dispose();+ });++ test("relative seeks clamp to bounds and skip no-op commands", () async {+ final seeks = <Duration>[];+ final controller = VideoSeekController(+ seek: (target) async {+ seeks.add(target);+ },+ readPosition: () => const Duration(seconds: 3),+ readDuration: () => const Duration(seconds: 60),+ );+ controller.onPlayerPosition(const Duration(seconds: 3));++ controller.seekBy(const Duration(seconds: -5));+ await _flushMicrotasks();+ controller.onPlayerPosition(Duration.zero);+ controller.seekBy(const Duration(seconds: -5));+ controller.seekBy(const Duration(seconds: 70));+ await _flushMicrotasks();++ expect(seeks, [Duration.zero, const Duration(seconds: 60)]);+ controller.dispose();+ });++ testWidgets("does not reconcile while a slider interaction is active", (+ tester,+ ) async {+ final controller = VideoSeekController(+ seek: (_) async {},+ readPosition: () => const Duration(seconds: 10),+ readDuration: () => const Duration(seconds: 60),+ );++ controller.beginSliderInteraction();+ controller.updateSliderTarget(const Duration(seconds: 30));+ await tester.pump(const Duration(milliseconds: 300));+ await tester.pump(const Duration(seconds: 2));++ expect(controller.position, const Duration(seconds: 30));+ expect(controller.state.phase, VideoSeekPhase.dragging);+ controller.dispose();+ });++ test("recovers from a failed seek command", () async {+ final controller = VideoSeekController(+ seek: (_) => Future.error(StateError("seek failed")),+ readPosition: () => const Duration(seconds: 10),+ readDuration: () => const Duration(seconds: 60),+ );+ controller.onPlayerPosition(const Duration(seconds: 10));++ controller.seekBy(const Duration(seconds: 5));+ await _flushMicrotasks();++ expect(controller.position, const Duration(seconds: 10));+ expect(controller.state.phase, VideoSeekPhase.idle);+ controller.dispose();+ });+}++Future<void> _flushMicrotasks() => Future<void>.delayed(Duration.zero);