232 lines
6.0 KiB
Dart
232 lines
6.0 KiB
Dart
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_logic.dart';
|
|
import 'package:fruitshorts/pages/home/home_logic.dart';
|
|
import 'package:fruitshorts/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(
|
|
'should_make_first_catalog_entry_the_current_drama_when_catalog_loads',
|
|
() {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
expect(logic.currentDrama.id, 'signal');
|
|
expect(logic.tab, 0);
|
|
expect(logic.mySection, 0);
|
|
expect(logic.drawerOpen, isFalse);
|
|
expect(logic.profile, isFalse);
|
|
expect(logic.videoImmersive, isFalse);
|
|
logic.dispose();
|
|
},
|
|
);
|
|
|
|
test('should_not_build_shell_when_catalog_is_empty', () {
|
|
expect(() => HomeLogic(const []), throwsStateError);
|
|
});
|
|
|
|
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.searchRequest, 0);
|
|
expect(logic.sectionRequest, 0);
|
|
expect(logic.resumeRequest, 0);
|
|
expect(logic.backRequest, 0);
|
|
expect(notifications, 1);
|
|
logic.dispose();
|
|
});
|
|
|
|
test('should_raise_only_its_own_counter_when_each_select_flag_is_used', () {
|
|
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('should_default_to_second_section_when_watchlist_shortcut_is_used', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
logic.select(2, watchlist: true);
|
|
|
|
expect(logic.tab, 2);
|
|
expect(logic.mySection, 1);
|
|
expect(logic.sectionRequest, 1);
|
|
logic.dispose();
|
|
});
|
|
|
|
test(
|
|
'should_differ_on_profile_flag_when_open_profile_and_open_library_are_used',
|
|
() {
|
|
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('should_return_to_discover_and_handle_immersive_back', () {
|
|
final logic = HomeLogic(dramas);
|
|
|
|
// Home is the launch tab; the first back press lands on Discover.
|
|
logic.onSystemBack();
|
|
expect(logic.tab, 1);
|
|
logic.onSystemBack();
|
|
expect(logic.tab, 1);
|
|
expect(logic.backRequest, 0);
|
|
|
|
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.onImmersiveChanged(false);
|
|
logic.onSystemBack();
|
|
expect(logic.tab, 1);
|
|
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();
|
|
});
|
|
|
|
test('should_map_onto_root_tabs_when_my_page_actions_fire', () {
|
|
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('should_do_nothing_when_notifying_after_dispose', () {
|
|
final logic = HomeLogic(dramas);
|
|
var notifications = 0;
|
|
logic.addListener(() => notifications++);
|
|
|
|
logic.dispose();
|
|
logic.notify();
|
|
|
|
expect(notifications, 0);
|
|
});
|
|
|
|
testWidgets('should_map_onto_root_tabs_when_discover_actions_fire', (
|
|
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();
|
|
});
|
|
}
|