203 lines
5.1 KiB
Dart
203 lines
5.1 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:fruitshorts/core/data/models/drama.dart';
|
|
import 'package:fruitshorts/pages/discover/discover_types.dart';
|
|
import 'package:fruitshorts/pages/home/home_logic.dart';
|
|
import 'package:fruitshorts/pages/mine/mine_types.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('should_make_first_catalog_entry_current_when_catalog_loads', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
expect(logic.currentDrama?.id, 'signal');
|
|
expect(logic.tab, 0);
|
|
expect(logic.drawerOpen, isFalse);
|
|
expect(logic.videoImmersive, isFalse);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_allow_empty_catalog_for_startup_skeleton', () {
|
|
final logic = HomeLogic(const []);
|
|
expect(logic.currentDrama, isNull);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_not_raise_request_counter_when_only_tab_switches', () {
|
|
final logic = HomeLogic(dramas);
|
|
var notifications = 0;
|
|
logic.addListener(() => notifications++);
|
|
|
|
logic.select(1);
|
|
|
|
expect(logic.tab, 1);
|
|
expect(logic.resumeRequest, 0);
|
|
expect(logic.backRequest, 0);
|
|
expect(notifications, 1);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_raise_only_resume_counter_when_select_flag_is_used', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
logic.select(0, resume: true);
|
|
expect(logic.resumeRequest, 1);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_open_profile_tab_when_profile_action_is_used', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
logic.openProfile();
|
|
|
|
expect(logic.tab, 4);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_return_home_and_handle_immersive_back', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
logic.onSystemBack();
|
|
expect(logic.tab, 0);
|
|
expect(logic.backRequest, 0);
|
|
|
|
logic.select(1);
|
|
logic.onSystemBack();
|
|
expect(logic.tab, 0);
|
|
|
|
logic.select(0);
|
|
logic.onImmersiveChanged(true);
|
|
logic.onSystemBack();
|
|
expect(logic.backRequest, 1);
|
|
expect(logic.tab, 0);
|
|
|
|
logic.onImmersiveChanged(false);
|
|
logic.onSystemBack();
|
|
expect(logic.tab, 0);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_notify_only_on_actual_change_when_menu_state_is_set', () {
|
|
final logic = HomeLogic(dramas);
|
|
var notifications = 0;
|
|
logic.addListener(() => notifications++);
|
|
|
|
logic.setMenuOpen(false);
|
|
expect(notifications, 0);
|
|
|
|
logic.setMenuOpen(true);
|
|
expect(logic.drawerOpen, isTrue);
|
|
expect(notifications, 1);
|
|
|
|
logic.setMenuOpen(true);
|
|
expect(notifications, 1);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_update_state_when_drama_and_immersive_callbacks_fire', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
logic.onDramaChanged(dramas[1]);
|
|
expect(logic.currentDrama?.id, 'city');
|
|
|
|
logic.onImmersiveChanged(true);
|
|
expect(logic.videoImmersive, isTrue);
|
|
logic.dispose();
|
|
});
|
|
|
|
testWidgets('should_defer_drama_notification_when_changed_during_build', (
|
|
tester,
|
|
) async {
|
|
final logic = HomeLogic(dramas);
|
|
var notifications = 0;
|
|
var immediateNotifications = -1;
|
|
var changed = false;
|
|
logic.addListener(() => notifications++);
|
|
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: ListenableBuilder(
|
|
listenable: logic,
|
|
builder: (context, _) {
|
|
if (!changed) {
|
|
changed = true;
|
|
logic.onDramaChanged(dramas[1]);
|
|
immediateNotifications = notifications;
|
|
}
|
|
return const SizedBox.shrink();
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
expect(immediateNotifications, 0);
|
|
expect(tester.takeException(), isNull);
|
|
expect(logic.currentDrama?.id, 'city');
|
|
await tester.pump();
|
|
expect(notifications, 1);
|
|
expect(tester.takeException(), isNull);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_map_onto_root_tabs_when_my_page_actions_fire', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
logic.myAction(MyPageAction.recommend);
|
|
expect(logic.tab, 1);
|
|
|
|
logic.myAction(MyPageAction.following);
|
|
expect(logic.tab, 3);
|
|
|
|
logic.openFollowingHistory();
|
|
expect(logic.tab, 3);
|
|
expect(logic.followingHistoryRequest, 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('should_map_onto_root_tabs_when_discover_actions_fire', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
logic.discoverAction(DiscoverPageAction.mine);
|
|
expect(logic.tab, 4);
|
|
|
|
logic.discoverAction(DiscoverPageAction.following);
|
|
expect(logic.tab, 3);
|
|
|
|
logic.discoverAction(DiscoverPageAction.home);
|
|
expect(logic.tab, 0);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_do_nothing_when_notifying_after_dispose', () {
|
|
final logic = HomeLogic(dramas);
|
|
var notifications = 0;
|
|
logic.addListener(() => notifications++);
|
|
|
|
logic.dispose();
|
|
logic.notify();
|
|
|
|
expect(notifications, 0);
|
|
});
|
|
}
|