222 lines
7.2 KiB
Dart
222 lines
7.2 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:fruitshorts/app.dart';
|
|
import 'package:fruitshorts/core/data/repositories/favorite_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/theme/app_colors.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_haptics.dart';
|
|
import '../support/fake_interaction_repositories.dart';
|
|
import '../support/widget_player.dart';
|
|
|
|
const _keys = [
|
|
Key('home-tabs-bar'),
|
|
Key('tab-home'),
|
|
Key('tab-theater'),
|
|
Key('tab-create'),
|
|
Key('tab-mine'),
|
|
];
|
|
|
|
void main() {
|
|
setUp(() {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.withData({
|
|
'auth.accessToken': 'test-access',
|
|
'auth.refreshToken': 'test-refresh',
|
|
'auth.user': '{"uid":"9","nickname":"Viewer","avatarUrl":""}',
|
|
});
|
|
});
|
|
|
|
testWidgets('should_theme_four_tabs_and_switch_selected_state_when_tapped', (
|
|
tester,
|
|
) async {
|
|
final haptics = FakeHaptics();
|
|
await _pumpApp(tester, haptics);
|
|
haptics.reset();
|
|
|
|
for (final key in _keys) {
|
|
expect(find.byKey(key), findsOneWidget);
|
|
}
|
|
for (final key in _keys.skip(1)) {
|
|
expect(tester.getSize(find.byKey(key)).height, greaterThanOrEqualTo(48));
|
|
}
|
|
|
|
final barFinder = find.byKey(const Key('home-tabs-bar'));
|
|
final bar = tester.widget<Container>(barFinder);
|
|
final decoration = bar.decoration as BoxDecoration;
|
|
final border = decoration.border as Border;
|
|
// 首页 feed 用深色导航。
|
|
expect(decoration.color, const Color(0xE6000000));
|
|
expect(border.top.color, Colors.white12);
|
|
expect(
|
|
tester
|
|
.widget<Text>(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-theater')),
|
|
matching: find.text('Theater'),
|
|
),
|
|
)
|
|
.style
|
|
?.color,
|
|
Colors.white70,
|
|
);
|
|
expect(
|
|
tester
|
|
.widget<AnimatedScale>(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-home')),
|
|
matching: find.byType(AnimatedScale),
|
|
),
|
|
)
|
|
.duration,
|
|
lessThanOrEqualTo(const Duration(milliseconds: 200)),
|
|
);
|
|
expect(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-home')),
|
|
matching: find.byIcon(Icons.home_rounded),
|
|
),
|
|
findsOneWidget,
|
|
);
|
|
expect(
|
|
tester
|
|
.widget<Icon>(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-home')),
|
|
matching: find.byIcon(Icons.home_rounded),
|
|
),
|
|
)
|
|
.color,
|
|
AppColors.blue,
|
|
);
|
|
|
|
await tester.tap(find.byKey(const Key('tab-theater')));
|
|
await tester.pumpAndSettle();
|
|
expect(haptics.selectionClicks, 1);
|
|
final lightBar = tester.widget<Container>(barFinder);
|
|
final lightDecoration = lightBar.decoration as BoxDecoration;
|
|
expect(lightDecoration.color, AppColors.bar.withValues(alpha: 0.95));
|
|
expect((lightDecoration.border as Border).top.color, AppColors.border);
|
|
await tester.tap(find.byKey(const Key('tab-theater')));
|
|
await tester.pumpAndSettle();
|
|
expect(haptics.selectionClicks, 1);
|
|
expect(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-theater')),
|
|
matching: find.byIcon(Icons.explore_rounded),
|
|
),
|
|
findsOneWidget,
|
|
);
|
|
expect(
|
|
tester
|
|
.widget<Text>(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-theater')),
|
|
matching: find.text('Theater'),
|
|
),
|
|
)
|
|
.style
|
|
?.color,
|
|
AppColors.blue,
|
|
);
|
|
expect(
|
|
tester
|
|
.widget<Text>(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-home')),
|
|
matching: find.text('Home'),
|
|
),
|
|
)
|
|
.style
|
|
?.color,
|
|
AppColors.navUnselected,
|
|
);
|
|
expect(
|
|
find.descendant(
|
|
of: find.byKey(const Key('tab-home')),
|
|
matching: find.byIcon(Icons.home_outlined),
|
|
),
|
|
findsOneWidget,
|
|
);
|
|
});
|
|
|
|
testWidgets('should_map_four_tabs_to_home_theater_create_and_me', (
|
|
tester,
|
|
) async {
|
|
await _pumpApp(tester);
|
|
|
|
expect(find.byKey(const Key('tab-home')), findsOneWidget);
|
|
expect(find.byKey(const Key('tab-theater')), findsOneWidget);
|
|
expect(find.byKey(const Key('tab-create')), findsOneWidget);
|
|
expect(find.byKey(const Key('tab-mine')), findsOneWidget);
|
|
expect(find.byKey(const Key('home-feed-title')), findsOneWidget);
|
|
|
|
await tester.tap(find.byKey(const Key('tab-theater')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('home-all-dramas-grid')), 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.text('Viewer'), findsOneWidget);
|
|
expect(find.byKey(const Key('mine-menu')), findsOneWidget);
|
|
expect(find.byKey(const Key('mine-watchlist')), findsOneWidget);
|
|
expect(find.byKey(const Key('mine-library-tab-0')), findsNothing);
|
|
|
|
await tester.tap(find.byKey(const Key('tab-home')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('home-feed-title')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('should_not_haptic_when_ordinary_list_item_is_opened', (
|
|
tester,
|
|
) async {
|
|
final haptics = FakeHaptics();
|
|
await _pumpApp(tester, haptics);
|
|
await tester.tap(find.byKey(const Key('tab-mine')));
|
|
await tester.pumpAndSettle();
|
|
haptics.reset();
|
|
|
|
await tester.ensureVisible(find.byKey(const Key('mine-settings')));
|
|
await tester.tap(find.byKey(const Key('mine-settings')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.byKey(const Key('settings-page')), findsOneWidget);
|
|
expect(haptics.calls, isEmpty);
|
|
});
|
|
}
|
|
|
|
Future<void> _pumpApp(WidgetTester tester, [FakeHaptics? haptics]) async {
|
|
tester.view.physicalSize = const Size(390, 844);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
final catalog = FakeCatalogRepository();
|
|
await tester.pumpWidget(
|
|
LumaDramaApp(
|
|
playerFactory: createWidgetPlayer,
|
|
catalogRepository: catalog,
|
|
categoryRepository: FakeCategoryRepository(catalog: catalog),
|
|
watchHistoryRepository: InMemoryWatchHistoryRepository(),
|
|
favoriteRepository: InMemoryFavoriteRepository(cards: catalog.dramas),
|
|
playbackRepository: FakePlaybackRepository(),
|
|
commentRepository: FakeCommentRepository(),
|
|
interactionRepository: FakeInteractionRepository(),
|
|
haptics: haptics ?? FakeHaptics(),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(const Key('tab-home')));
|
|
await tester.pumpAndSettle();
|
|
}
|