为什么:上一批归档只覆盖了「已跟踪文件的修改」,原型阶段新增的文件(app_config、theme、 home_feed/play_ticket 模型、catalog_api、测试目录等)还是未入库状态 —— 而已入库的代码会 import 它们, 留在工作区会导致 HEAD 处的代码编译不过。一并归档,让 HEAD 自洽。 纯归档,无行为改动。
347 lines
10 KiB
Dart
347 lines
10 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:nontonki/core/data/models/drama.dart';
|
|
import 'package:nontonki/l10n/app_localizations.dart';
|
|
import 'package:nontonki/pages/discover/discover_logic.dart';
|
|
|
|
import '../support/fake_watch_history.dart';
|
|
|
|
Drama _drama(String id, {List<int> numbers = const [1, 2, 3]}) => Drama(
|
|
id: id,
|
|
title: id,
|
|
synopsis: 'synopsis',
|
|
posterUrl: '/files/poster.png',
|
|
episodes: [
|
|
for (final number in numbers)
|
|
Episode(
|
|
id: '$id-$number',
|
|
number: number,
|
|
title: 'Episode $number',
|
|
videoUrl: 'https://example.test/clip.mp4',
|
|
isFree: true,
|
|
),
|
|
],
|
|
);
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
test('the constructor resolves the resume position of episode one', () async {
|
|
final history = FakeWatchHistory()..saved = const Duration(seconds: 4);
|
|
final logic = DiscoverLogic(history: history, drama: _drama('signal'));
|
|
|
|
expect(await logic.position, const Duration(seconds: 4));
|
|
expect(history.reads, ['demo-local-v1|signal|signal-1']);
|
|
expect(logic.category, 0);
|
|
expect(logic.searchOpen, isFalse);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('refreshPosition re-reads after the drama is swapped', () async {
|
|
final history = FakeWatchHistory();
|
|
final logic = DiscoverLogic(history: history, drama: _drama('signal'));
|
|
|
|
logic.drama = _drama('city');
|
|
logic.refreshPosition();
|
|
await logic.position;
|
|
|
|
expect(history.reads, [
|
|
'demo-local-v1|signal|signal-1',
|
|
'demo-local-v1|city|city-1',
|
|
]);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('a list card without episodes resumes from the last watch', () async {
|
|
final history = FakeWatchHistory()
|
|
..saved = const Duration(seconds: 9)
|
|
..lastEpisode = 'signal-2';
|
|
final logic = DiscoverLogic(
|
|
history: history,
|
|
drama: _drama('signal', numbers: const []),
|
|
);
|
|
|
|
expect(await logic.position, const Duration(seconds: 9));
|
|
expect(history.reads, ['demo-local-v1|signal|signal-2']);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('a list card without history has no resume position', () async {
|
|
final history = FakeWatchHistory();
|
|
final logic = DiscoverLogic(
|
|
history: history,
|
|
drama: _drama('signal', numbers: const []),
|
|
);
|
|
|
|
expect(await logic.position, isNull);
|
|
expect(history.reads, isEmpty);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('selectCategory stores the index and notifies once', () {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
var notifications = 0;
|
|
logic.addListener(() => notifications++);
|
|
|
|
logic.selectCategory(3);
|
|
|
|
expect(logic.category, 3);
|
|
expect(notifications, 1);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('search visibility follows the flag or the typed text', () {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
|
|
expect(logic.showSearch, isFalse);
|
|
|
|
logic.search.text = 'vengeance';
|
|
logic.onSearchChanged();
|
|
expect(logic.showSearch, isTrue);
|
|
|
|
logic.search.clear();
|
|
expect(logic.showSearch, isFalse);
|
|
|
|
logic.searchOpen = true;
|
|
expect(logic.showSearch, isTrue);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('dramaFor matches by id and tolerates a missing id', () {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
final catalog = [_drama('signal'), _drama('city')];
|
|
|
|
expect(logic.dramaFor('city', catalog)?.id, 'city');
|
|
expect(logic.dramaFor('missing', catalog), isNull);
|
|
expect(logic.dramaFor(null, catalog), isNull);
|
|
expect(logic.dramaFor('city', const []), isNull);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('syncDrama only adopts a drama while the page is active', () {
|
|
final history = FakeWatchHistory();
|
|
final logic = DiscoverLogic(history: history, drama: _drama('signal'));
|
|
|
|
void sync(
|
|
Drama next, {
|
|
required bool active,
|
|
required bool wasActive,
|
|
required bool dramaChanged,
|
|
int searchRequest = 0,
|
|
int previousSearchRequest = 0,
|
|
}) => logic.syncDrama(
|
|
active: active,
|
|
wasActive: wasActive,
|
|
nextDrama: next,
|
|
dramaChanged: dramaChanged,
|
|
searchRequest: searchRequest,
|
|
previousSearchRequest: previousSearchRequest,
|
|
isActive: () => active,
|
|
);
|
|
|
|
// An inactive page keeps showing its own drama.
|
|
sync(_drama('city'), active: false, wasActive: true, dramaChanged: true);
|
|
expect(logic.drama.id, 'signal');
|
|
|
|
// Becoming active adopts the incoming drama.
|
|
sync(_drama('city'), active: true, wasActive: false, dramaChanged: false);
|
|
expect(logic.drama.id, 'city');
|
|
|
|
// Staying active with a changed drama re-reads history.
|
|
history.reads.clear();
|
|
sync(_drama('vengeance'), active: true, wasActive: true, dramaChanged: true);
|
|
expect(logic.drama.id, 'vengeance');
|
|
expect(history.reads, ['demo-local-v1|vengeance|vengeance-1']);
|
|
|
|
// Staying active with no change leaves both drama and history alone.
|
|
history.reads.clear();
|
|
sync(_drama('other'), active: true, wasActive: true, dramaChanged: false);
|
|
expect(logic.drama.id, 'vengeance');
|
|
expect(history.reads, isEmpty);
|
|
logic.dispose();
|
|
});
|
|
|
|
testWidgets('a search request opens and focuses the field when active', (
|
|
tester,
|
|
) async {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: TextField(
|
|
controller: logic.search,
|
|
focusNode: logic.searchFocus,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
logic.syncDrama(
|
|
active: true,
|
|
wasActive: true,
|
|
nextDrama: logic.drama,
|
|
dramaChanged: false,
|
|
searchRequest: 1,
|
|
previousSearchRequest: 0,
|
|
isActive: () => true,
|
|
);
|
|
|
|
expect(logic.searchOpen, isTrue);
|
|
await tester.pumpAndSettle();
|
|
expect(logic.searchFocus.hasFocus, isTrue);
|
|
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
logic.dispose();
|
|
});
|
|
|
|
testWidgets('a hidden page never opens or focuses the search field', (
|
|
tester,
|
|
) async {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: TextField(
|
|
controller: logic.search,
|
|
focusNode: logic.searchFocus,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
// The search request branch is gated on `active`, so a page that is not on
|
|
// screen must neither reveal the field nor pull focus away from the visible tab.
|
|
logic.syncDrama(
|
|
active: false,
|
|
wasActive: false,
|
|
nextDrama: logic.drama,
|
|
dramaChanged: false,
|
|
searchRequest: 1,
|
|
previousSearchRequest: 0,
|
|
isActive: () => false,
|
|
);
|
|
|
|
expect(logic.searchOpen, isFalse);
|
|
await tester.pumpAndSettle();
|
|
expect(logic.searchFocus.hasFocus, isFalse);
|
|
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
logic.dispose();
|
|
});
|
|
|
|
testWidgets('act returns the choice for a page and forwards it when embedded', (
|
|
tester,
|
|
) async {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
final forwarded = <DiscoverPageAction>[];
|
|
final popped = Completer<DiscoverPageAction?>();
|
|
late BuildContext pageContext;
|
|
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Builder(
|
|
builder: (context) => TextButton(
|
|
onPressed: () => Navigator.of(context)
|
|
.push<DiscoverPageAction>(
|
|
MaterialPageRoute<DiscoverPageAction>(
|
|
builder: (inner) {
|
|
pageContext = inner;
|
|
return const SizedBox.shrink();
|
|
},
|
|
),
|
|
)
|
|
.then(popped.complete),
|
|
child: const Text('open'),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
await tester.tap(find.text('open'));
|
|
await tester.pumpAndSettle();
|
|
|
|
logic.act(
|
|
pageContext,
|
|
DiscoverPageAction.home,
|
|
embedded: true,
|
|
onAction: forwarded.add,
|
|
);
|
|
expect(forwarded, [DiscoverPageAction.home]);
|
|
expect(Navigator.of(pageContext).canPop(), isTrue);
|
|
|
|
logic.act(pageContext, DiscoverPageAction.watchlist, embedded: false);
|
|
await tester.pumpAndSettle();
|
|
|
|
await expectLater(popped.future, completion(DiscoverPageAction.watchlist));
|
|
logic.dispose();
|
|
});
|
|
|
|
testWidgets('a card without playable media shows the artwork notice', (
|
|
tester,
|
|
) async {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
late BuildContext context;
|
|
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
locale: const Locale('en'),
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
home: Builder(
|
|
builder: (inner) {
|
|
context = inner;
|
|
return const Scaffold(body: SizedBox.shrink());
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
logic.openCard(context, null, embedded: false);
|
|
await tester.pumpAndSettle();
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
expect(find.text(l10n.artworkOnlyNotice), findsOneWidget);
|
|
logic.dispose();
|
|
});
|
|
|
|
testWidgets('an embedded card forwards the chosen drama', (tester) async {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
late BuildContext context;
|
|
Drama? selected;
|
|
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Builder(
|
|
builder: (inner) {
|
|
context = inner;
|
|
return const SizedBox.shrink();
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
logic.openCard(
|
|
context,
|
|
_drama('city'),
|
|
embedded: true,
|
|
onDramaSelected: (drama) => selected = drama,
|
|
);
|
|
|
|
expect(selected?.id, 'city');
|
|
logic.dispose();
|
|
});
|
|
|
|
test('notify after dispose is a no-op', () {
|
|
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
|
|
var notifications = 0;
|
|
logic.addListener(() => notifications++);
|
|
|
|
logic.dispose();
|
|
logic.notify();
|
|
|
|
expect(notifications, 0);
|
|
});
|
|
}
|