feat(client): C 端 1-2 账号链路(邮箱登录/注册 + 令牌与 401 + 匿名/登录分叉)

为什么:客户端此前完全没有账号概念(mine 页的"登录"只是本地 demo 开关),
而受限动作(收藏/评论)要对到具体账号。本批把账号链路接上,为后续互动、进度同步、下载打底。

- AuthApi + AuthController + AuthSessionStore:注册/登录/登出/刷新,令牌与设备号存 shared_preferences
  (用 SharedPreferencesAsync,与 engagement 仓库同一套 API)
- ApiClient:统一带 Authorization 与 X-Device-Id;401 自动刷新一次并重试,刷新失败清登录态走 onUnauthorized
- 登录/注册页(pages/auth,沿用 binding/logic/view 与现有视觉):邮箱格式、密码 8-64 位含字母数字本地先校验,
  错误提示走 l10n;登录/注册一键切换
- 匿名/登录分叉:首页、发现、详情保持匿名可看;收藏/点赞等受限动作走 ensureSignedIn() 弹底部提示引导登录
- mine 页展示真实账号(昵称 + uid),拿不到才回落到演示文案
- 测试:新增 auth 控制器(注册落盘/401 刷新重试/刷新失败清会话/登出)与登录页(校验/切换)用例;
  widget_test 改为预置本地会话(账号不再是本地开关);修掉 playback 用例对 host 大小写的环境依赖断言

门禁:flutter analyze 0 问题;flutter test 148 全过。
This commit is contained in:
Vicky committed 2026-10-15 17:53:35 +08:00
1 parent 77d5748dbb
commit e1b98df509
24 files changed
+2871 -1016

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.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';
// 包名此刻仍是 nontonki,1-3 改名时会一并替换成 fruitshorts
import 'package:nontonki/core/data/remote/api_client.dart';
import 'package:nontonki/core/data/remote/auth_api.dart';
import 'package:nontonki/core/data/services/auth_controller.dart';
import 'package:nontonki/core/data/services/auth_session_store.dart';
/// 用假的 http.Client 覆盖三条关键路径:注册成功、401 → 刷新 → 重试、刷新失败 → 清会话。
void main() {
setUp(() {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
});
/// http.Response(String) 默认按 Latin-1 编码,中文消息会直接抛
/// "Contains invalid characters" —— 统一用 UTF-8 字节构造。
http.Response jsonResponse(String body, [int statusCode = 200]) {
return http.Response.bytes(
utf8.encode(body),
statusCode,
headers: const {'content-type': 'application/json; charset=utf-8'},
);
}
String tokenBody({
required String accessToken,
required String refreshToken,
String uid = 'U1',
}) {
return jsonEncode({
'code': 0,
'message': 'ok',
'data': {
'accessToken': accessToken,
'refreshToken': refreshToken,
'expiresIn': 7200,
'user': {'uid': uid, 'nickname': 'Tester', 'avatarUrl': ''},
},
});
}
ApiClient clientWith(
AuthController Function() auth, {
required MockClient httpClient,
}) {
final store = AuthSessionStore();
return ApiClient(
baseUrl: 'http://127.0.0.1:8081',
client: httpClient,
accessTokenReader: () => auth().accessToken,
tokenRefresher: () => auth().refresh(),
onUnauthorized: () => auth().clear(),
deviceIdReader: store.deviceId,
);
}
test('register stores session and exposes signed-in state', () async {
late AuthController auth;
final mock = MockClient((request) async {
expect(request.url.path, '/app/auth/register');
expect(request.headers['X-Device-Id'], isNotEmpty);
expect(request.headers.containsKey('Authorization'), isFalse);
return jsonResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
});
final store = AuthSessionStore();
auth = AuthController(AuthApi(clientWith(() => auth, httpClient: mock)), store);
expect(auth.isSignedIn, isFalse);
await auth.register(email: 'a@b.com', password: 'abcd1234');
expect(auth.isSignedIn, isTrue);
expect(auth.accessToken, 'a1');
expect(auth.user?.nickname, 'Tester');
// 落盘:新建一个 store 也能读回来
expect((await AuthSessionStore().read())?.refreshToken, 'r1');
});
test('401 triggers refresh once and retries with the new token', () async {
var calls = 0;
late AuthController auth;
final mock = MockClient((request) async {
calls++;
if (request.url.path == '/app/auth/refresh') {
return jsonResponse(tokenBody(accessToken: 'a2', refreshToken: 'r2'));
}
if (request.url.path != '/app/me') {
// 注册等其它请求:直接发第一版令牌
return jsonResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
}
final token = request.headers['Authorization'];
if (token == 'Bearer a1') {
return jsonResponse(
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
);
}
expect(token, 'Bearer a2');
return jsonResponse(jsonEncode({'code': 0, 'message': 'ok', 'data': {'hit': true}}));
});
final store = AuthSessionStore();
auth = AuthController(AuthApi(clientWith(() => auth, httpClient: mock)), store);
await auth.register(email: 'a@b.com', password: 'abcd1234');
calls = 0;
final api = clientWith(() => auth, httpClient: mock);
final data = await api.get('/app/me');
expect((data as Map)['hit'], isTrue);
expect(calls, 3); // 原请求(401) + 刷新 + 重试
expect(auth.accessToken, 'a2');
});
test('refresh failure clears the session (401 handling)', () async {
late AuthController auth;
final mock = MockClient((request) async {
if (request.url.path == '/app/auth/refresh') {
return jsonResponse(jsonEncode({'code': 401, 'message': '登录已过期', 'data': null}));
}
if (request.url.path == '/app/auth/register') {
return jsonResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
}
return jsonResponse(
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
);
});
final store = AuthSessionStore();
auth = AuthController(AuthApi(clientWith(() => auth, httpClient: mock)), store);
await auth.register(email: 'a@b.com', password: 'abcd1234');
final api = clientWith(() => auth, httpClient: mock);
await expectLater(api.get('/app/me'), throwsA(isA<ApiException>()));
expect(auth.isSignedIn, isFalse);
expect(await AuthSessionStore().read(), isNull);
});
test('signOut clears local session even if the server call fails', () async {
late AuthController auth;
final mock = MockClient((request) async {
if (request.url.path == '/app/auth/logout') {
return jsonResponse('{}', 500);
}
return jsonResponse(tokenBody(accessToken: 'a1', refreshToken: 'r1'));
});
final store = AuthSessionStore();
auth = AuthController(AuthApi(clientWith(() => auth, httpClient: mock)), store);
await auth.register(email: 'a@b.com', password: 'abcd1234');
await auth.signOut();
expect(auth.isSignedIn, isFalse);
expect(await AuthSessionStore().read(), isNull);
});
}
+81
View File
@@ -0,0 +1,81 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:provider/provider.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 'package:nontonki/core/data/remote/auth_api.dart';
import 'package:nontonki/core/data/remote/api_client.dart';
import 'package:nontonki/core/data/services/auth_controller.dart';
import 'package:nontonki/core/data/services/auth_session_store.dart';
import 'package:nontonki/l10n/app_localizations.dart';
import 'package:nontonki/pages/auth/auth_view.dart';
/// 登录/注册页的可读证据:校验提示 + 登录/注册切换 + 未登录时不会去发请求。
void main() {
setUp(() {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
});
Future<void> pumpAuthPage(WidgetTester tester, {bool signUp = false}) async {
final auth = AuthController(
AuthApi(
// 这个测试不联网:请求一律 500,正好用于验证"本地校验没过就不发请求"
ApiClient(
baseUrl: 'http://127.0.0.1:1',
client: MockClient((_) async => http.Response('{}', 500)),
),
),
AuthSessionStore(),
);
await tester.pumpWidget(
ChangeNotifierProvider<AuthController>.value(
value: auth,
child: MaterialApp(
locale: const Locale('en'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: AuthPage(signUpMode: signUp),
),
),
);
await tester.pumpAndSettle();
}
testWidgets('empty form shows the email error and stays on the page', (tester) async {
await pumpAuthPage(tester);
expect(find.text('Welcome back'), findsOneWidget);
await tester.tap(find.byType(FilledButton));
await tester.pumpAndSettle();
expect(find.text('Enter your email'), findsOneWidget);
expect(find.text('Welcome back'), findsOneWidget);
});
testWidgets('weak password is rejected locally', (tester) async {
await pumpAuthPage(tester);
await tester.enterText(find.byType(TextField).first, 'a@b.com');
await tester.enterText(find.byType(TextField).last, 'abcdefgh');
await tester.tap(find.byType(FilledButton));
await tester.pumpAndSettle();
expect(find.text('Password needs both letters and numbers'), findsOneWidget);
});
testWidgets('switch link flips between sign in and sign up', (tester) async {
await pumpAuthPage(tester);
expect(find.text('Welcome back'), findsOneWidget);
await tester.tap(find.text('New here? Create an account'));
await tester.pumpAndSettle();
expect(find.text('Create your account'), findsOneWidget);
expect(find.text('Already have an account? Sign in'), findsOneWidget);
});
}
+227
View File
@@ -0,0 +1,227 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:nontonki/core/data/models/drama.dart';
import 'package:nontonki/pages/discover/discover_logic.dart';
import 'package:nontonki/pages/home/home_logic.dart';
import 'package:nontonki/pages/mine/mine_logic.dart';
Drama _drama(String id) => Drama(
id: id,
title: id,
synopsis: 'synopsis',
posterUrl: '/files/poster.png',
episodes: const [],
);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final dramas = [_drama('signal'), _drama('city')];
test('the first catalog entry becomes the current drama', () {
final logic = HomeLogic(dramas);
expect(logic.currentDrama.id, 'signal');
expect(logic.tab, 1);
expect(logic.mySection, 0);
expect(logic.drawerOpen, isFalse);
expect(logic.profile, isFalse);
expect(logic.videoImmersive, isFalse);
logic.dispose();
});
test('an empty catalog cannot build the shell', () {
expect(() => HomeLogic(const []), throwsStateError);
});
test('a plain tab switch raises no request counter', () {
final logic = HomeLogic(dramas);
var notifications = 0;
logic.addListener(() => notifications++);
logic.select(1);
expect(logic.tab, 1);
expect(logic.searchRequest, 0);
expect(logic.sectionRequest, 0);
expect(logic.resumeRequest, 0);
expect(logic.backRequest, 0);
expect(notifications, 1);
logic.dispose();
});
test('each select flag raises exactly its own counter', () {
final logic = HomeLogic(dramas);
logic.select(1, search: true);
expect(logic.tab, 1);
expect(logic.searchRequest, 1);
expect(logic.sectionRequest, 0);
logic.select(2, mySection: 0);
expect(logic.mySection, 0);
expect(logic.sectionRequest, 1);
expect(logic.searchRequest, 1);
logic.select(0, resume: true);
expect(logic.resumeRequest, 1);
expect(logic.sectionRequest, 1);
expect(logic.backRequest, 0);
logic.dispose();
});
test('the watchlist shortcut defaults to the second section', () {
final logic = HomeLogic(dramas);
logic.select(2, watchlist: true);
expect(logic.tab, 2);
expect(logic.mySection, 1);
expect(logic.sectionRequest, 1);
logic.dispose();
});
test('openProfile and openLibrary disagree about the profile flag', () {
final logic = HomeLogic(dramas);
logic.openProfile();
expect(logic.profile, isTrue);
expect(logic.tab, 2);
expect(logic.mySection, 0);
expect(logic.sectionRequest, 1);
logic.openLibrary(section: 1);
expect(logic.profile, isFalse);
expect(logic.mySection, 1);
expect(logic.sectionRequest, 2);
logic.openLibrary();
expect(logic.profile, isFalse);
expect(logic.mySection, 1);
expect(logic.sectionRequest, 3);
logic.dispose();
});
test('system back returns home, and home itself opens the drawer', () {
final logic = HomeLogic(dramas);
logic.onSystemBack();
expect(logic.drawerOpen, isTrue);
expect(logic.tab, 1);
expect(logic.backRequest, 0);
logic.onDrawerChanged(false);
logic.select(0);
logic.onSystemBack();
expect(logic.tab, 1);
expect(logic.backRequest, 0);
logic.select(0);
logic.onImmersiveChanged(true);
logic.onSystemBack();
expect(logic.backRequest, 1);
expect(logic.tab, 0);
logic.onDrawerChanged(true);
logic.onSystemBack();
expect(logic.backRequest, 1);
logic.onDrawerChanged(false);
logic.onImmersiveChanged(false);
logic.onSystemBack();
expect(logic.tab, 1);
logic.dispose();
});
test('drawer state only notifies on an actual change', () {
final logic = HomeLogic(dramas);
var notifications = 0;
logic.addListener(() => notifications++);
logic.onDrawerChanged(false);
expect(notifications, 0);
logic.onDrawerChanged(true);
expect(logic.drawerOpen, isTrue);
expect(notifications, 1);
logic.onDrawerChanged(true);
expect(notifications, 1);
logic.dispose();
});
test('drama and immersive callbacks update state', () {
final logic = HomeLogic(dramas);
logic.onDramaChanged(dramas[1]);
expect(logic.currentDrama.id, 'city');
logic.onImmersiveChanged(true);
expect(logic.videoImmersive, isTrue);
logic.dispose();
});
test('my page actions map onto root tabs', () {
final logic = HomeLogic(dramas);
logic.myAction(MyPageAction.discover);
expect(logic.tab, 1);
expect(logic.searchRequest, 1);
logic.myAction(MyPageAction.resume);
expect(logic.tab, 0);
expect(logic.resumeRequest, 1);
logic.myAction(MyPageAction.home);
expect(logic.tab, 0);
expect(logic.resumeRequest, 1);
logic.dispose();
});
test('notifying after dispose is a no-op', () {
final logic = HomeLogic(dramas);
var notifications = 0;
logic.addListener(() => notifications++);
logic.dispose();
logic.notify();
expect(notifications, 0);
});
testWidgets('discover actions map onto root tabs', (tester) async {
final logic = HomeLogic(dramas);
late BuildContext context;
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (inner) {
context = inner;
return const SizedBox.shrink();
},
),
),
);
logic.discoverAction(context, DiscoverPageAction.watchlist);
expect(logic.tab, 2);
expect(logic.profile, isFalse);
expect(logic.mySection, 1);
expect(logic.sectionRequest, 1);
logic.discoverAction(context, DiscoverPageAction.my);
expect(logic.tab, 2);
expect(logic.profile, isFalse);
logic.discoverAction(context, DiscoverPageAction.resume);
expect(logic.tab, 0);
expect(logic.resumeRequest, 1);
logic.discoverAction(context, DiscoverPageAction.home);
expect(logic.tab, 0);
expect(tester.takeException(), isNull);
logic.dispose();
});
}
+37
View File
@@ -0,0 +1,37 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:nontonki/core/config/app_config.dart';
import 'package:nontonki/core/data/models/drama.dart';
import 'package:nontonki/core/data/services/video_player_port.dart';
Episode _episode(String videoUrl) => Episode(
id: '1',
number: 1,
title: '第 1 集',
isFree: true,
videoUrl: videoUrl,
);
void main() {
test('相对播放地址补上服务地址', () {
final uri = VideoPlayerPort.resolveUri(
_episode('/files/episode/202610/clip.mp4'),
);
expect(uri.isAbsolute, isTrue);
// Uri.parse().toString() 会把 host 规范成小写,而 devHostBaseUrl 里的
// Bonjour 名字带大写(jiangdeMacBook-Air.local)—— 比较时统一小写,
// 否则会依赖运行平台(flutter test 里 defaultTargetPlatform 默认是 android)
expect(
uri.toString().toLowerCase(),
'${AppConfig.apiBaseUrl}/files/episode/202610/clip.mp4'.toLowerCase(),
);
});
test('已经是绝对地址的不再拼接', () {
final uri = VideoPlayerPort.resolveUri(
_episode('https://cdn.example.test/clip.mp4'),
);
expect(uri.toString(), 'https://cdn.example.test/clip.mp4');
});
}
+143 -184
View File
@@ -6,12 +6,25 @@ import 'package:nontonki/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/widget_player.dart';
void main() {
setUp(() {
/// 默认按「已登录」起测:账号体系接上后,"登录"不再是本地演示开关,
/// 而是本地会话(这里直接给 shared_preferences 的内存实现塞一份会话)。
/// 昵称/uid 与演示文案保持一致,避免为显示细节改写既有断言。
void seedSignedInSession() {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
InMemorySharedPreferencesAsync.withData({
'auth.accessToken': 'test-access',
'auth.refreshToken': 'test-refresh',
'auth.user':
'{"uid":"M1CZWVYE","nickname":"Vicky sheng","avatarUrl":""}',
});
}
setUp(() {
seedSignedInSession();
});
testWidgets(
@@ -24,9 +37,12 @@ void main() {
created.add(episode.id);
return WidgetPlayer();
},
catalogRepository: FakeCatalogRepository(),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(created, hasLength(2));
expect(find.byType(CircularProgressIndicator), findsNothing);
await tester.drag(find.byType(PageView), const Offset(0, -550));
@@ -48,25 +64,25 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(find.text('The Signal'), findsOneWidget);
expect(find.byType(Slider), findsOneWidget);
expect(find.text('Home'), findsWidgets);
expect(find.text('Recommended'), findsOneWidget);
expect(find.textContaining('Playing now'), findsOneWidget);
expect(find.text('Recommended'), findsNothing);
expect(find.text('Following'), findsNothing);
expect(find.byTooltip('Episodes'), findsWidgets);
expect(find.byTooltip('Watch in landscape'), findsOneWidget);
expect(find.byTooltip('Clear screen'), findsOneWidget);
expect(find.byTooltip('Picture in picture'), findsOneWidget);
await tester.tap(find.byTooltip('Explore series'));
await tester.pumpAndSettle();
expect(find.text('Watch episode 1'), findsOneWidget);
expect(find.text('Episodes'), findsOneWidget);
expect(find.text('Watch Full'), findsNothing);
expect(find.byTooltip('Explore series'), findsNothing);
});
testWidgets('landscape control hides tabs and restores them on exit', (
testWidgets('clear screen hides tabs and restores them on tap', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
@@ -75,16 +91,19 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Watch in landscape'));
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Clear screen'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('home-tabs-bar')), findsNothing);
expect(find.byTooltip('Exit fullscreen'), findsOneWidget);
await tester.tap(find.byTooltip('Exit fullscreen'));
expect(find.byTooltip('Exit clear screen'), findsOneWidget);
await tester.tap(find.byTooltip('Exit clear screen'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('home-tabs-bar')), findsOneWidget);
expect(find.byTooltip('Clear screen'), findsOneWidget);
expect(tester.takeException(), isNull);
});
@@ -97,17 +116,23 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Open quick menu'));
expect(find.text('Continue Watching'), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(tester.widget<PlayerPage>(find.byType(PlayerPage)).active, isFalse);
expect(
tester
.widget<PlayerPage>(find.byType(PlayerPage, skipOffstage: false))
.active,
isFalse,
);
expect(find.text('Quick access'), findsOneWidget);
expect(
find.descendant(
of: find.byType(Drawer),
matching: find.text('Same demo clip · free'),
matching: find.text('7 EPISODES'),
),
findsOneWidget,
);
@@ -118,15 +143,21 @@ void main() {
expect(find.text('Watch episode 1'), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(tester.widget<PlayerPage>(find.byType(PlayerPage)).active, isTrue);
expect(
tester
.widget<PlayerPage>(find.byType(PlayerPage, skipOffstage: false))
.active,
isFalse,
);
expect(find.text('Continue Watching'), findsOneWidget);
await tester.tap(find.byTooltip('Open quick menu'));
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('drawer-discover')));
await tester.pumpAndSettle();
expect(find.text('Trending Micro-Dramas'), findsOneWidget);
expect(find.text('Continue Watching'), findsOneWidget);
expect(
Navigator.of(tester.element(find.text('Trending Micro-Dramas'))).canPop(),
Navigator.of(tester.element(find.text('Continue Watching'))).canPop(),
isFalse,
);
});
@@ -140,37 +171,26 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
expect(find.byTooltip('Captions'), findsNothing);
await tester.tap(find.byTooltip('My'));
await tester.tap(find.byKey(const Key('tab-my')));
await tester.pumpAndSettle();
expect(find.text('Continue Watching'), findsOneWidget);
expect(find.text('History'), findsOneWidget);
expect(find.text('Watchlist'), findsOneWidget);
await tester.scrollUntilVisible(
find.text('App Language'),
120,
scrollable: find
.descendant(
of: find.byType(ListView),
matching: find.byType(Scrollable),
)
.first,
);
expect(find.text('Vicky sheng'), findsOneWidget);
expect(find.text('Coins'), findsOneWidget);
expect(find.text('PREFERENCES'), findsOneWidget);
await tester.tap(find.text('App Language'));
await tester.pumpAndSettle();
await tester.tap(find.text('简体中文').last);
await tester.pumpAndSettle();
expect(find.text('我的'), findsWidgets);
expect(find.text('继续观看'), findsOneWidget);
expect(find.text('找剧'), findsOneWidget);
await tester.tap(find.text('找剧'));
expect(find.text('我的账户'), findsOneWidget);
expect(find.text('首页'), findsOneWidget);
await tester.tap(find.text('首页'));
await tester.pumpAndSettle();
expect(find.text('热门短剧'), findsOneWidget);
expect(find.text('排行榜'), findsOneWidget);
expect(find.text('为你推荐'), findsOneWidget);
expect(find.text('热门'), findsOneWidget);
});
testWidgets('home controls fit a compact Android viewport', (tester) async {
@@ -180,16 +200,18 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
expect(find.text('Recommended'), findsOneWidget);
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(find.text('The Signal'), findsOneWidget);
await tester.drag(find.byType(PageView), const Offset(0, -500));
await tester.pumpAndSettle();
expect(find.text('City Lights & Velvet Nights'), findsOneWidget);
await tester.tap(find.byTooltip('Explore series'));
await tester.tap(find.byKey(const Key('tab-discover')));
await tester.pumpAndSettle();
await tester.tap(find.text('Watch episode 1'));
await tester.tap(find.text('City Lights & Velvet Nights'));
await tester.pumpAndSettle();
expect(find.byType(Slider), findsOneWidget);
expect(tester.takeException(), isNull);
@@ -204,17 +226,18 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('My'));
await tester.tap(find.byKey(const Key('tab-my')));
await tester.pumpAndSettle();
expect(find.text('Continue Watching'), findsOneWidget);
expect(find.text('History'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.tap(find.text('Home').last);
expect(find.text('Vicky sheng'), findsOneWidget);
expect(find.text('Coins'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.tap(find.text('Shorts'));
await tester.pumpAndSettle();
expect(find.text('Recommended'), findsOneWidget);
expect(find.text('The Signal'), findsOneWidget);
});
testWidgets(
@@ -226,58 +249,53 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Discover'));
await tester.tap(find.text('Home'));
await tester.pumpAndSettle();
expect(find.text('Trending Micro-Dramas'), findsOneWidget);
expect(find.text('Filter'), findsOneWidget);
expect(find.text('Top Charts'), findsOneWidget);
expect(find.text('New Releases'), findsOneWidget);
expect(find.text('Watchlist'), findsOneWidget);
expect(find.text('Same demo clip · free'), findsNWidgets(3));
expect(find.text('Video pending'), findsOneWidget);
expect(find.text('For You'), findsOneWidget);
expect(find.text('Continue Watching'), findsOneWidget);
expect(find.text('Rewards'), findsOneWidget);
expect(find.text(r'$ 0'), findsOneWidget);
expect(find.text('Urban Romance', skipOffstage: false), findsOneWidget);
expect(
find.text('Action · Thriller', skipOffstage: false),
findsOneWidget,
);
expect(find.byTooltip('Search'), findsNothing);
expect(tester.takeException(), isNull);
await tester.enterText(find.byType(TextField), 'Vengeance');
await tester.pumpAndSettle();
expect(find.text('Vengeance Rising'), findsOneWidget);
expect(find.text('Same demo clip · free'), findsOneWidget);
await tester.tap(find.text('Watchlist'));
await tester.tap(find.byKey(const Key('tab-my-list')));
await tester.pumpAndSettle();
expect(find.text('No shows here yet.'), findsOneWidget);
await tester.tap(find.text('Home').last);
await tester.tap(find.text('Shorts'));
await tester.pumpAndSettle();
expect(find.text('Recommended'), findsOneWidget);
expect(find.text('The Signal'), findsOneWidget);
},
);
testWidgets('bottom tabs stay on one route and preserve Discover search', (
tester,
) async {
testWidgets('bottom tabs stay on one route', (tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Discover'));
await tester.tap(find.text('Home'));
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), 'Vengeance');
await tester.pumpAndSettle();
expect(find.text('Vengeance Rising'), findsOneWidget);
expect(find.text('Continue Watching'), findsOneWidget);
expect(find.byTooltip('Search'), findsNothing);
expect(
Navigator.of(tester.element(find.byType(TextField))).canPop(),
Navigator.of(tester.element(find.text('Continue Watching'))).canPop(),
isFalse,
);
await tester.tap(find.byKey(const Key('tab-my')));
await tester.tap(find.byKey(const Key('tab-my-list')));
await tester.pumpAndSettle();
expect(find.text('History'), findsOneWidget);
expect(
@@ -290,20 +308,20 @@ void main() {
await tester.tap(find.byKey(const Key('tab-discover')));
await tester.pumpAndSettle();
expect(find.widgetWithText(TextField, 'Vengeance'), findsOneWidget);
expect(find.text('Vengeance Rising'), findsOneWidget);
expect(find.text('Continue Watching'), findsOneWidget);
expect(find.byTooltip('Search'), findsNothing);
expect(
Navigator.of(tester.element(find.byType(TextField))).canPop(),
Navigator.of(tester.element(find.text('Continue Watching'))).canPop(),
isFalse,
);
await tester.tap(find.byKey(const Key('tab-my')));
await tester.tap(find.byKey(const Key('tab-my-list')));
await tester.pumpAndSettle();
expect(find.text('No shows here yet.'), findsOneWidget);
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(find.text('Recommended'), findsOneWidget);
expect(find.text('The Signal'), findsOneWidget);
expect(
Navigator.of(tester.element(find.text('Recommended'))).canPop(),
Navigator.of(tester.element(find.text('The Signal'))).canPop(),
isFalse,
);
});
@@ -317,9 +335,11 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(find.text('The Signal'), findsOneWidget);
await tester.drag(find.byType(PageView), const Offset(0, -550));
@@ -327,66 +347,38 @@ void main() {
expect(find.text('City Lights & Velvet Nights'), findsOneWidget);
expect(find.textContaining('Episode 1'), findsOneWidget);
await tester.tap(find.byTooltip('Explore series'));
await tester.tap(find.byKey(const Key('tab-discover')));
await tester.pumpAndSettle();
expect(find.text('City Lights & Velvet Nights'), findsOneWidget);
await tester.tap(find.text('Watch episode 1'));
await tester.tap(find.text('City Lights & Velvet Nights'));
await tester.pumpAndSettle();
expect(find.byType(Slider), findsOneWidget);
await tester.drag(find.byType(PageView), const Offset(0, -550));
await tester.pumpAndSettle();
expect(find.textContaining('EPISODE 2 / 3'), findsOneWidget);
expect(find.text('Episode 2 · Playing now'), findsOneWidget);
expect(find.text('City Lights & Velvet Nights'), findsWidgets);
},
);
testWidgets('home categories switch by video, top row and tap', (
tester,
) async {
testWidgets('shorts page has no category navigation', (tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
expect(find.byIcon(Icons.tune_rounded), findsNothing);
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(find.text('Following'), findsNothing);
expect(find.text('Animated'), findsNothing);
expect(find.text('Live Action'), findsNothing);
expect(find.text('Recommended'), findsNothing);
expect(find.text('The Signal'), findsOneWidget);
await tester.drag(
find.byKey(const Key('home-video-swipe')),
const Offset(220, 0),
);
await tester.pumpAndSettle();
expect(find.text('City Lights & Velvet Nights'), findsOneWidget);
expect(find.byKey(const Key('home-category-liveAction')), findsOneWidget);
await tester.drag(find.byType(PageView), const Offset(0, -550));
await tester.pumpAndSettle();
expect(find.text('Vengeance Rising'), findsOneWidget);
await tester.drag(
find.byKey(const Key('home-category-swipe')),
const Offset(190, 0),
);
await tester.pumpAndSettle();
expect(find.text('The Signal'), findsOneWidget);
await tester.drag(
find.byKey(const Key('home-category-swipe')),
const Offset(-190, 0),
);
await tester.pumpAndSettle();
expect(find.text('City Lights & Velvet Nights'), findsOneWidget);
await tester.tap(find.byKey(const Key('home-category-following')));
await tester.pumpAndSettle();
expect(find.text('No dramas in this category yet.'), findsOneWidget);
await tester.tap(find.byKey(const Key('home-category-recommended')));
await tester.pumpAndSettle();
expect(find.text('The Signal'), findsOneWidget);
expect(tester.takeException(), isNull);
});
testWidgets('right rail saves, comments, likes and invokes share in order', (
@@ -412,13 +404,15 @@ void main() {
);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
final keys = [
const Key('rail-like'),
const Key('rail-save'),
const Key('rail-comments'),
const Key('rail-like'),
const Key('rail-share'),
];
final tops = keys
@@ -426,9 +420,9 @@ void main() {
.toList();
expect(tops, orderedEquals(tops.toList()..sort()));
await tester.tap(find.byKey(keys[0]));
await tester.tap(find.byKey(const Key('rail-save')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(keys[1]));
await tester.tap(find.byKey(const Key('rail-comments')));
await tester.pumpAndSettle();
expect(find.text('Only visible on this device'), findsOneWidget);
await tester.enterText(find.byKey(const Key('comment-input')), 'Nice demo');
@@ -438,75 +432,38 @@ void main() {
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
await tester.tap(find.byKey(keys[2]));
await tester.tap(find.byKey(keys[3]));
await tester.tap(find.byKey(const Key('rail-like')));
await tester.tap(find.byKey(const Key('rail-share')));
await tester.pumpAndSettle();
expect(sharedText, contains('The Signal'));
expect(tester.takeException(), isNull);
});
testWidgets(
'right swipe from leftmost category opens drawer and root stays',
(tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
);
await tester.pumpAndSettle();
for (var i = 0; i < 3; i++) {
await tester.drag(
find.byKey(const Key('home-category-swipe')),
const Offset(200, 0),
);
await tester.pumpAndSettle();
}
expect(find.text('No dramas in this category yet.'), findsOneWidget);
await tester.drag(
find.byKey(const Key('home-empty-swipe')),
const Offset(200, 0),
);
await tester.pumpAndSettle();
expect(find.text('Quick access'), findsOneWidget);
await tester.drag(find.byType(Drawer), const Offset(150, 0));
await tester.pumpAndSettle();
expect(find.text('Quick access'), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.byKey(const Key('home-tabs-bar')), findsOneWidget);
},
);
testWidgets('system back steps categories then opens drawer without exit', (
tester,
) async {
testWidgets('system back opens the drawer and stays on 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(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('City Lights & Velvet Nights'), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('The Signal'), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('No dramas in this category yet.'), findsOneWidget);
expect(find.text('Continue Watching'), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('Quick access'), findsOneWidget);
expect(find.byKey(const Key('home-tabs-bar')), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('Quick access'), findsNothing);
expect(find.text('Continue Watching'), findsOneWidget);
expect(
Navigator.of(
tester.element(find.byKey(const Key('home-tabs-bar'))),
).canPop(),
isFalse,
);
});
testWidgets('My watchlist lists saved dramas across the home feed', (
@@ -517,9 +474,11 @@ void main() {
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
const LumaDramaApp(playerFactory: createWidgetPlayer),
LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('rail-save')));
await tester.pumpAndSettle();
@@ -527,7 +486,7 @@ void main() {
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('rail-save')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-my')));
await tester.tap(find.byKey(const Key('tab-my-list')));
await tester.pumpAndSettle();
await tester.tap(find.text('Watchlist'));
await tester.pumpAndSettle();