336 lines
12 KiB
Dart
336 lines
12 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:fruitshorts/app.dart';
|
|
import 'package:fruitshorts/core/data/models/comment.dart';
|
|
import 'package:fruitshorts/core/data/repositories/comment_repository.dart';
|
|
import 'package:fruitshorts/core/data/repositories/favorite_repository.dart';
|
|
import 'package:fruitshorts/core/data/repositories/interaction_repository.dart';
|
|
import 'package:fruitshorts/core/data/repositories/playback_repository.dart';
|
|
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
|
|
import 'package:fruitshorts/core/data/services/playback_session.dart';
|
|
import 'package:fruitshorts/pages/player/player_view.dart';
|
|
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
|
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
|
|
|
import 'support/fake_catalog.dart';
|
|
import 'support/fake_category.dart';
|
|
import 'support/fake_interaction_repositories.dart';
|
|
import 'support/widget_player.dart';
|
|
|
|
void main() {
|
|
void seedSignedInSession() {
|
|
SharedPreferencesAsyncPlatform
|
|
.instance = InMemorySharedPreferencesAsync.withData({
|
|
'auth.accessToken': 'test-access',
|
|
'auth.refreshToken': 'test-refresh',
|
|
'auth.user': '{"uid":"M1CZWVYE","nickname":"Vicky sheng","avatarUrl":""}',
|
|
});
|
|
}
|
|
|
|
setUp(seedSignedInSession);
|
|
|
|
Widget appUnderTest({
|
|
PlayerFactory? playerFactory,
|
|
CommentRepository? commentRepository,
|
|
InteractionRepository? interactionRepository,
|
|
}) {
|
|
final catalog = FakeCatalogRepository();
|
|
return LumaDramaApp(
|
|
playerFactory: playerFactory ?? createWidgetPlayer,
|
|
catalogRepository: catalog,
|
|
categoryRepository: FakeCategoryRepository(catalog: catalog),
|
|
watchHistoryRepository: InMemoryWatchHistoryRepository(),
|
|
favoriteRepository: InMemoryFavoriteRepository(cards: catalog.dramas),
|
|
playbackRepository: FakePlaybackRepository(),
|
|
commentRepository: commentRepository ?? FakeCommentRepository(),
|
|
interactionRepository:
|
|
interactionRepository ?? FakeInteractionRepository(),
|
|
);
|
|
}
|
|
|
|
testWidgets('should_preload_next_episode_when_fullscreen_player_opens', (
|
|
tester,
|
|
) async {
|
|
final created = <String>[];
|
|
await tester.pumpWidget(
|
|
appUnderTest(
|
|
playerFactory: (episode) {
|
|
created.add(episode.id);
|
|
return WidgetPlayer();
|
|
},
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
// 首页 feed 启动会创建首个播放器,先清掉再统计剧场播放器。
|
|
created.clear();
|
|
await _openTheaterPlayer(tester);
|
|
|
|
expect(created, hasLength(2));
|
|
expect(find.byType(CircularProgressIndicator), findsNothing);
|
|
await tester.drag(find.byType(PageView).last, const Offset(0, -550));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(
|
|
tester.widget<Text>(find.byKey(const Key('player-episode-heading'))).data,
|
|
contains('Episode 2'),
|
|
);
|
|
expect(created, hasLength(3));
|
|
expect(created.toSet(), hasLength(3));
|
|
});
|
|
|
|
testWidgets('should_open_fullscreen_player_when_theater_card_is_tapped', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(appUnderTest());
|
|
await tester.pumpAndSettle();
|
|
await _openTheaterPlayer(tester);
|
|
|
|
final player = tester.widget<PlayerPage>(find.byType(PlayerPage).last);
|
|
expect(player.homeMode, isFalse);
|
|
expect(find.byKey(const Key('player-controls-top')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('should_keep_four_root_tabs_on_one_route_and_return_home', (
|
|
tester,
|
|
) async {
|
|
tester.view.physicalSize = const Size(390, 844);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
|
|
await tester.pumpWidget(appUnderTest());
|
|
await tester.pumpAndSettle();
|
|
await _openTheaterPlayer(tester);
|
|
expect(find.byType(PlayerPage), findsOneWidget);
|
|
|
|
await tester.tap(find.byKey(const Key('player-back')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('home-all-dramas-grid')), findsOneWidget);
|
|
expect(
|
|
Navigator.of(
|
|
tester.element(find.byKey(const Key('home-all-dramas-grid'))),
|
|
).canPop(),
|
|
isFalse,
|
|
);
|
|
|
|
await tester.binding.handlePopRoute();
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('home-feed-title')), findsOneWidget);
|
|
|
|
await tester.tap(find.byKey(const Key('tab-create')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('create-page')), findsOneWidget);
|
|
await tester.tap(find.byKey(const Key('tab-mine')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('mine-menu')), findsOneWidget);
|
|
expect(
|
|
Navigator.of(tester.element(find.byKey(const Key('mine-menu')))).canPop(),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
testWidgets('should_show_language_entry_on_mine_page', (tester) async {
|
|
await tester.pumpWidget(appUnderTest());
|
|
await tester.pumpAndSettle();
|
|
expect(find.byTooltip('Captions'), findsNothing);
|
|
|
|
await tester.tap(find.byKey(const Key('tab-mine')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Vicky sheng'), findsOneWidget);
|
|
expect(find.text('Coins'), findsOneWidget);
|
|
expect(find.byKey(const Key('mine-language')), findsOneWidget);
|
|
expect(find.byKey(const Key('mine-wallet')), findsOneWidget);
|
|
|
|
await tester.tap(find.byKey(const Key('tab-theater')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Theater'), findsWidgets);
|
|
});
|
|
|
|
testWidgets('should_switch_app_language_from_mine_menu', (tester) async {
|
|
await tester.pumpWidget(appUnderTest());
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('tab-mine')));
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.drag(
|
|
find.byKey(const Key('mine-page-scroll')),
|
|
const Offset(0, -320),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('mine-language')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('mine-language-sheet')), findsOneWidget);
|
|
await tester.tap(find.byKey(const Key('mine-language-zh')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('我的'), findsWidgets);
|
|
expect(find.text('语言'), findsOneWidget);
|
|
await tester.tap(find.byKey(const Key('tab-theater')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('剧场'), findsWidgets);
|
|
});
|
|
|
|
testWidgets('should_fit_compact_viewport_when_player_is_opened', (
|
|
tester,
|
|
) async {
|
|
tester.view.physicalSize = const Size(360, 720);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
|
|
await tester.pumpWidget(appUnderTest());
|
|
await tester.pumpAndSettle();
|
|
await _openTheaterPlayer(tester);
|
|
|
|
expect(find.byKey(const Key('progress-loading')), findsOneWidget);
|
|
expect(find.byType(Slider), findsNothing);
|
|
await tester.drag(find.byType(PageView), const Offset(0, -500));
|
|
await tester.pumpAndSettle();
|
|
expect(
|
|
tester.widget<Text>(find.byKey(const Key('player-episode-heading'))).data,
|
|
contains('Episode 2'),
|
|
);
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
|
|
testWidgets('should_render_theater_filters_and_grid_on_same_route_tab', (
|
|
tester,
|
|
) async {
|
|
tester.view.physicalSize = const Size(360, 720);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
|
|
await tester.pumpWidget(appUnderTest());
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('tab-theater')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.byKey(const Key('discover-search')), findsOneWidget);
|
|
expect(find.byKey(const Key('theater-category-chips')), findsOneWidget);
|
|
expect(find.byKey(const Key('theater-language-chips')), findsNothing);
|
|
expect(find.byKey(const Key('home-all-dramas-grid')), findsOneWidget);
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
|
|
testWidgets('should_save_comment_like_and_share_from_player_more', (
|
|
tester,
|
|
) async {
|
|
String? sharedText;
|
|
final interactions = FakeInteractionRepository();
|
|
final comments = FakeCommentRepository(pages: [const CommentPage()]);
|
|
const channel = MethodChannel('com.stormg.lumadrama/share');
|
|
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
|
|
call,
|
|
) async {
|
|
sharedText = (call.arguments as Map)['text'] as String;
|
|
return null;
|
|
});
|
|
addTearDown(
|
|
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
|
channel,
|
|
null,
|
|
),
|
|
);
|
|
|
|
await tester.pumpWidget(
|
|
appUnderTest(
|
|
commentRepository: comments,
|
|
interactionRepository: interactions,
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
await _openTheaterPlayer(tester);
|
|
|
|
await tester.tap(find.byKey(const Key('player-rail-comments')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('No comments yet.'), findsOneWidget);
|
|
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
|
|
await tester.pumpAndSettle();
|
|
await tester.enterText(
|
|
find.byKey(const Key('comment-composer-input')),
|
|
'Nice demo',
|
|
);
|
|
await tester.pump();
|
|
await tester.tap(find.byKey(const Key('comment-composer-post')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Nice demo'), findsOneWidget);
|
|
await tester.binding.handlePopRoute();
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.tap(find.byKey(const Key('player-rail-like')));
|
|
await tester.pumpAndSettle();
|
|
expect(interactions.likeTargets, contains('EPISODE:city-lights-demo-1'));
|
|
|
|
await tester.tap(find.byKey(const Key('player-rail-save')));
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('player-rail-share')));
|
|
await tester.pumpAndSettle();
|
|
expect(sharedText, contains('City Lights & Velvet Nights'));
|
|
expect(sharedText, contains('FruitShorts'));
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
|
|
testWidgets('should_open_auth_gate_without_liking_when_signed_out', (
|
|
tester,
|
|
) async {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.empty();
|
|
final interactions = FakeInteractionRepository();
|
|
|
|
await tester.pumpWidget(appUnderTest(interactionRepository: interactions));
|
|
await tester.pumpAndSettle();
|
|
await _openTheaterPlayer(tester);
|
|
await tester.tap(find.byKey(const Key('player-rail-like')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Sign in to continue'), findsOneWidget);
|
|
expect(interactions.likeTargets, isEmpty);
|
|
});
|
|
|
|
testWidgets('should_show_saved_player_drama_in_favorites', (tester) async {
|
|
tester.view.physicalSize = const Size(390, 844);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
|
|
await tester.pumpWidget(appUnderTest());
|
|
await tester.pumpAndSettle();
|
|
await _openTheaterPlayer(tester);
|
|
await tester.tap(find.byKey(const Key('player-rail-save')));
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.tap(find.byKey(const Key('player-back')));
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('tab-mine')));
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('mine-watchlist')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('City Lights & Velvet Nights'), findsWidgets);
|
|
});
|
|
}
|
|
|
|
Future<void> _openTheaterPlayer(WidgetTester tester) async {
|
|
await tester.tap(find.byKey(const Key('tab-theater')));
|
|
await tester.pumpAndSettle();
|
|
final card = find.byKey(const Key('home-drama-card-city-lights-demo'));
|
|
await tester.scrollUntilVisible(
|
|
card,
|
|
220,
|
|
scrollable: find
|
|
.descendant(
|
|
of: find.byKey(const Key('discover-refresh')),
|
|
matching: find.byType(Scrollable),
|
|
)
|
|
.first,
|
|
);
|
|
await tester.ensureVisible(card);
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(card);
|
|
await tester.pumpAndSettle();
|
|
}
|