summaryrefslogtreecommitdiff
path: root/player-android/test/screens/folder_browser_screen_test.dart
blob: b6452534c9f7c511d4036c9250849a8b7c76141d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Widget tests for FolderBrowserScreen (folder_browser_screen.dart).
//
// Tests cover:
//   1. Shows loading indicator while browseSet is in flight.
//   2. Renders folder tiles and media tiles after a successful load.
//   3. Breadcrumb bar reflects the current path (one crumb for root, multiple
//      for nested paths).
//   4. Tapping a folder tile navigates deeper (pushes a new route with the
//      updated path).
//   5. Tapping a media tile navigates to the media-detail route.
//   6. Shows an empty-state view when browseSet returns no folders/media.
//   7. Shows an error view when browseSet throws, with a retry button.
//   8. Pull-to-refresh calls browseSet a second time.
//   9. Breadcrumb tap navigates to the tapped ancestor path.
//
// Riverpod providers are overridden with fakes so tests run without a real
// server or OS keychain.
//
// Run with: flutter test test/screens/folder_browser_screen_test.dart

import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'package:player_android/api/dio_client.dart';
import 'package:player_android/api/player_api_client.dart';
import 'package:player_android/providers/api_client_provider.dart';
import 'package:player_android/screens/folder_browser_screen.dart';

// ---------------------------------------------------------------------------
// Fakes
// ---------------------------------------------------------------------------

/// In-memory [TokenStorage] that returns a fixed test token.
///
/// Avoids the platform-specific OS keychain in widget tests.
class _FakeTokenStorage implements TokenStorage {
  const _FakeTokenStorage();

  @override
  Future<String?> readToken() async => 'test-token';

  @override
  Future<void> writeToken(String token) async {}

  @override
  Future<void> deleteToken() async {}
}

/// Controllable [PlayerApiClient] stub for [FolderBrowserScreen] tests.
///
/// Only [browseSet], [thumbnailUrl], and [setFolderCoverUrl] are implemented;
/// all other methods remain [UnimplementedError] — the screen calls only these.
class _FakeApiClient extends PlayerApiClient {
  _FakeApiClient() : super(dio: Dio());

  /// When non-null, [browseSet] returns this map.
  Map<String, dynamic>? browseResult;

  /// When non-null, [browseSet] throws this instead of returning.
  Object? browseError;

  /// Records the number of [browseSet] calls for refresh tests.
  int browseCallCount = 0;

  /// The [parent] argument passed to the most recent [browseSet] call.
  String? lastParent;

  @override
  Future<Map<String, dynamic>> browseSet(int setId, {String? parent}) async {
    browseCallCount++;
    lastParent = parent;
    if (browseError != null) throw browseError!;
    return browseResult!;
  }

  /// Returns an empty string so thumbnail widgets show the placeholder instead
  /// of making a network request — keeps tests hermetic.
  @override
  String thumbnailUrl(int mediaId) => '';

  /// Returns an empty string so folder-cover images show the placeholder
  /// instead of making a network request — keeps tests hermetic.
  @override
  String setFolderCoverUrl(int setId, {String? folder}) => '';
}

/// [PlayerApiClient] stub that delays [browseSet] until [complete] is called.
///
/// Used to inspect mid-flight loading state before the response arrives.
class _DelayedFakeApiClient extends PlayerApiClient {
  _DelayedFakeApiClient() : super(dio: Dio());

  final _completer = Completer<Map<String, dynamic>>();

  /// Resolves the pending [browseSet] call with [result].
  void complete(Map<String, dynamic> result) => _completer.complete(result);

  @override
  Future<Map<String, dynamic>> browseSet(int setId, {String? parent}) =>
      _completer.future;

  /// Returns an empty string so thumbnail widgets show the placeholder instead
  /// of making a network request — keeps tests hermetic.
  @override
  String thumbnailUrl(int mediaId) => '';

  /// Returns an empty string so folder-cover images show the placeholder
  /// instead of making a network request — keeps tests hermetic.
  @override
  String setFolderCoverUrl(int setId, {String? folder}) => '';
}

// ---------------------------------------------------------------------------
// Sample data
// ---------------------------------------------------------------------------

/// A browseSet response at the root with two subfolders and one media item.
///
/// Both folders set [has_cover] to false so tests stay hermetic — no
/// [CachedNetworkImage] network requests are made during widget pumps.
const _kRootBrowseResult = {
  'current_path': '',
  'folders': [
    {'name': 'FolderA', 'has_cover': false},
    {'name': 'FolderB', 'has_cover': false},
  ],
  'media': [
    {
      'id': 1,
      'set_id': 10,
      'rel_path': 'root_video.mp4',
      'file_name': 'root_video.mp4',
      'abs_path': '/media/set/root_video.mp4',
      'type': 'video',
      'duration': 120.0,
      'codec': 'h264',
      'resolution': '1920x1080',
      'bitrate': 4000,
      'file_size_bytes': 15000000,
      'width': 1920,
      'height': 1080,
      'thumbnail_path': '',
      'play_count': 0,
    }
  ],
};

/// A browseSet response for FolderA containing one subfolder and two media items.
const _kFolderABrowseResult = {
  'current_path': 'FolderA',
  'folders': [
    {'name': 'SubFolderA1', 'has_cover': false},
  ],
  'media': [
    {
      'id': 2,
      'set_id': 10,
      'rel_path': 'FolderA/audio.mp3',
      'file_name': 'audio.mp3',
      'abs_path': '/media/set/FolderA/audio.mp3',
      'type': 'audio',
      'duration': 210.0,
      'codec': 'mp3',
      'resolution': '',
      'bitrate': 320,
      'file_size_bytes': 8000000,
      'width': 0,
      'height': 0,
      'thumbnail_path': '',
      'play_count': 0,
    },
    {
      'id': 3,
      'set_id': 10,
      'rel_path': 'FolderA/clip.mp4',
      'file_name': 'clip.mp4',
      'abs_path': '/media/set/FolderA/clip.mp4',
      'type': 'video',
      'duration': 30.0,
      'codec': 'h264',
      'resolution': '1280x720',
      'bitrate': 2000,
      'file_size_bytes': 3000000,
      'width': 1280,
      'height': 720,
      'thumbnail_path': '',
      'play_count': 0,
    },
  ],
};

/// A browseSet response with no folders and no media (empty folder).
const _kEmptyBrowseResult = {
  'current_path': '',
  'folders': <Map<String, dynamic>>[],
  'media': <Map<String, dynamic>>[],
};

// ---------------------------------------------------------------------------
// Helper: pump FolderBrowserScreen inside a minimal ProviderScope.
// ---------------------------------------------------------------------------

/// Stub route shown after navigating to the media-detail screen.
const _kMediaDetailKey = Key('nav_media_detail');

/// Builds a [GoRouter] with [FolderBrowserScreen] and stub detail + child routes
/// so navigation tests can verify that the correct destination is reached.
GoRouter _buildRouter(
  PlayerApiClient fakeClient, {
  String? path,
  String? setName,
}) {
  return GoRouter(
    initialLocation: path != null
        ? '/browse/10?path=${Uri.encodeComponent(path)}'
        : '/browse/10',
    routes: [
      GoRoute(
        path: '/browse/:setId',
        builder: (context, state) {
          final setId = int.tryParse(state.pathParameters['setId']!) ?? 0;
          final p = state.uri.queryParameters['path'];
          final name =
              state.extra is String ? state.extra as String : setName;
          return FolderBrowserScreen(setId: setId, path: p, setName: name);
        },
      ),
      GoRoute(
        // Stub for media-detail navigation.
        path: '/media/:id',
        builder: (context, state) => Scaffold(
          body: Text(
            'Media ${state.pathParameters['id']}',
            key: _kMediaDetailKey,
          ),
        ),
      ),
    ],
  );
}

/// Pumps [FolderBrowserScreen] (set 10, optional [path]) inside a
/// [ProviderScope] that overrides [apiClientProvider] and
/// [tokenStorageProvider] with fakes.
Future<void> _pumpScreen(
  WidgetTester tester,
  PlayerApiClient fakeClient, {
  String? path,
  String? setName,
}) async {
  final router = _buildRouter(fakeClient, path: path, setName: setName);
  await tester.pumpWidget(
    ProviderScope(
      overrides: [
        tokenStorageProvider.overrideWithValue(const _FakeTokenStorage()),
        apiClientProvider.overrideWithValue(fakeClient),
      ],
      child: MaterialApp.router(
        routerConfig: router,
      ),
    ),
  );
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

void main() {
  // --------------------------------------------------------------------------
  // Loading state
  // --------------------------------------------------------------------------

  group('loading state', () {
    testWidgets('shows loading indicator while browseSet is in flight',
        (tester) async {
      final fakeClient = _DelayedFakeApiClient();

      await _pumpScreen(tester, fakeClient);

      // One frame: initState fires, addPostFrameCallback enqueues the load,
      // but the Future has not yet resolved.
      await tester.pump();

      expect(find.byKey(const Key('folder_loading')), findsOneWidget);
      expect(find.byType(CircularProgressIndicator), findsOneWidget);

      // Resolve to prevent "pending async work" warnings.
      fakeClient.complete(_kRootBrowseResult);
      await tester.pumpAndSettle();
    });
  });

  // --------------------------------------------------------------------------
  // Renders content
  // --------------------------------------------------------------------------

  group('renders folders and media', () {
    testWidgets('shows folder tiles for each folder returned by browseSet',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(
        find.byKey(const Key('folder_tile_FolderA')),
        findsOneWidget,
      );
      expect(
        find.byKey(const Key('folder_tile_FolderB')),
        findsOneWidget,
      );
      expect(find.byKey(const Key('folder_name_FolderA')), findsOneWidget);
      expect(find.byKey(const Key('folder_name_FolderB')), findsOneWidget);
    });

    testWidgets('shows media tiles for each media item returned by browseSet',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(find.byKey(const Key('media_tile_1')), findsOneWidget);
      expect(
        find.byKey(const Key('media_tile_name_1')),
        findsOneWidget,
      );
      expect(find.text('root_video.mp4'), findsOneWidget);
    });

    testWidgets('shows section headers for folders and media', (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(
        find.byKey(const Key('folder_section_header')),
        findsOneWidget,
      );
      expect(
        find.byKey(const Key('media_section_header')),
        findsOneWidget,
      );
    });

    testWidgets('renders duration formatted correctly', (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kFolderABrowseResult);

      await _pumpScreen(tester, fakeClient, path: 'FolderA');
      await tester.pumpAndSettle();

      // audio.mp3: 210s = 3:30
      expect(find.text('3:30'), findsOneWidget);
      // clip.mp4: 30s = 0:30
      expect(find.text('0:30'), findsOneWidget);
    });
  });

  // --------------------------------------------------------------------------
  // Breadcrumb navigation
  // --------------------------------------------------------------------------

  group('breadcrumb navigation', () {
    testWidgets('shows single "Home" crumb at root path', (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      // At root the only crumb is "Home" (as a non-tappable Text).
      expect(
        find.byKey(const Key('breadcrumb_current_Home')),
        findsOneWidget,
      );
      expect(find.byKey(const Key('breadcrumb_bar')), findsOneWidget);
    });

    testWidgets('shows parent crumbs and current crumb for nested path',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kFolderABrowseResult);

      await _pumpScreen(tester, fakeClient, path: 'FolderA');
      await tester.pumpAndSettle();

      // "Home" is a tappable ancestor crumb.
      expect(find.byKey(const Key('breadcrumb_Home')), findsOneWidget);
      // "FolderA" is the current (non-tappable) crumb.
      expect(
        find.byKey(const Key('breadcrumb_current_FolderA')),
        findsOneWidget,
      );
    });

    testWidgets('shows three crumbs for a two-level nested path',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = {
          'current_path': 'FolderA/SubFolderA1',
          'folders': <Map<String, dynamic>>[],
          'media': <Map<String, dynamic>>[],
        };

      await _pumpScreen(tester, fakeClient, path: 'FolderA/SubFolderA1');
      await tester.pumpAndSettle();

      expect(find.byKey(const Key('breadcrumb_Home')), findsOneWidget);
      expect(find.byKey(const Key('breadcrumb_FolderA')), findsOneWidget);
      expect(
        find.byKey(const Key('breadcrumb_current_SubFolderA1')),
        findsOneWidget,
      );
    });
  });

  // --------------------------------------------------------------------------
  // Folder tap navigates deeper
  // --------------------------------------------------------------------------

  group('folder tap navigates deeper', () {
    testWidgets(
        'tapping a folder tile pushes a new route with the updated path',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      // Tap FolderA — should push /browse/10?path=FolderA.
      await tester.tap(find.byKey(const Key('folder_tile_FolderA')));

      // Prepare the client for the child browse call.
      fakeClient.browseResult =
          Map<String, dynamic>.from(_kFolderABrowseResult);
      await tester.pumpAndSettle();

      // After navigation the new screen's breadcrumb should show FolderA as
      // the current crumb.
      expect(
        find.byKey(const Key('breadcrumb_current_FolderA')),
        findsOneWidget,
      );
    });

    testWidgets('browseSet is called with the child path after folder tap',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      // First call is for the root (parent == null).
      expect(fakeClient.browseCallCount, equals(1));
      expect(fakeClient.lastParent, isNull);

      // Prepare for the child browse.
      fakeClient.browseResult =
          Map<String, dynamic>.from(_kFolderABrowseResult);

      await tester.tap(find.byKey(const Key('folder_tile_FolderA')));
      await tester.pumpAndSettle();

      // A second browseSet call should have been made with parent='FolderA'.
      expect(fakeClient.browseCallCount, greaterThanOrEqualTo(2));
      expect(fakeClient.lastParent, equals('FolderA'));
    });
  });

  // --------------------------------------------------------------------------
  // Media tap navigates to detail
  // --------------------------------------------------------------------------

  group('media tap navigates to detail', () {
    testWidgets('tapping a media tile navigates to /media/:id', (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      await tester.tap(find.byKey(const Key('media_tile_1')));
      await tester.pumpAndSettle();

      // Stub route at /media/:id should be on screen.
      expect(find.byKey(_kMediaDetailKey), findsOneWidget);
      expect(find.text('Media 1'), findsOneWidget);
    });
  });

  // --------------------------------------------------------------------------
  // Empty state
  // --------------------------------------------------------------------------

  group('empty state', () {
    testWidgets('shows empty-state view when browseSet returns no content',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult =
            Map<String, dynamic>.from(_kEmptyBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(find.byKey(const Key('folder_empty')), findsOneWidget);
      expect(
        find.byKey(const Key('folder_section_header')),
        findsNothing,
      );
      expect(
        find.byKey(const Key('media_section_header')),
        findsNothing,
      );
    });
  });

  // --------------------------------------------------------------------------
  // Error state
  // --------------------------------------------------------------------------

  group('error state', () {
    testWidgets('shows error message when browseSet throws a network error',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseError = DioException(
          requestOptions: RequestOptions(path: '/api/v1/sets/10/browse'),
          type: DioExceptionType.connectionError,
        );

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(find.byKey(const Key('folder_error')), findsOneWidget);
      expect(
        find.textContaining('Could not reach the server'),
        findsOneWidget,
      );
    });

    testWidgets('retry button re-calls browseSet and shows content on success',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseError = DioException(
          requestOptions: RequestOptions(path: '/api/v1/sets/10/browse'),
          type: DioExceptionType.connectionError,
        );

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(find.byKey(const Key('folder_retry')), findsOneWidget);

      // Fix the error before tapping retry.
      fakeClient
        ..browseError = null
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await tester.tap(find.byKey(const Key('folder_retry')));
      await tester.pumpAndSettle();

      // Grid content is now visible.
      expect(find.byKey(const Key('folder_tile_FolderA')), findsOneWidget);
      expect(fakeClient.browseCallCount, equals(2));
    });

    testWidgets('shows 403 permission message for forbidden error',
        (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseError = DioException(
          requestOptions: RequestOptions(path: '/api/v1/sets/10/browse'),
          type: DioExceptionType.badResponse,
          response: Response(
            requestOptions: RequestOptions(path: '/api/v1/sets/10/browse'),
            statusCode: 403,
          ),
        );

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(
        find.textContaining('do not have permission'),
        findsOneWidget,
      );
    });
  });

  // --------------------------------------------------------------------------
  // Pull-to-refresh
  // --------------------------------------------------------------------------

  group('pull-to-refresh', () {
    testWidgets('pull-to-refresh calls browseSet a second time', (tester) async {
      final fakeClient = _FakeApiClient()
        ..browseResult = Map<String, dynamic>.from(_kRootBrowseResult);

      await _pumpScreen(tester, fakeClient);
      await tester.pumpAndSettle();

      expect(fakeClient.browseCallCount, equals(1));

      await tester.drag(
        find.byKey(const Key('folder_browser_scroll')),
        const Offset(0, 300),
      );
      await tester.pumpAndSettle();

      expect(fakeClient.browseCallCount, equals(2));
    });
  });
}