Skip to content

Commit b655eea

Browse files
author
chetanr25
committed
test: Add tests for search and sort functionality in landing page
1 parent 5194560 commit b655eea

File tree

3 files changed

+171
-24
lines changed

3 files changed

+171
-24
lines changed

lib/ui/pages/landing_page/components/search_text_field.dart

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,26 @@ class SearchTextField extends StatelessWidget {
2020

2121
@override
2222
Widget build(BuildContext context) {
23-
2423
return Row(
2524
children: [
2625
Expanded(
27-
child: Column(
28-
crossAxisAlignment: CrossAxisAlignment.start,
29-
mainAxisSize: MainAxisSize.min,
30-
children: [
31-
TextField(
32-
controller: controller,
33-
focusNode: focusNode,
34-
autofocus: true,
35-
style:
36-
TextStyle(color: PaintroidTheme.of(context).onSurfaceColor),
37-
decoration: InputDecoration(
38-
hintText: 'Search projects...',
39-
hintStyle: TextStyle(
40-
color: PaintroidTheme.of(context)
41-
.onSurfaceColor
42-
.withOpacity(0.6),
43-
),
44-
border: InputBorder.none,
45-
),
46-
onChanged: onChanged,
26+
child: TextField(
27+
controller: controller,
28+
focusNode: focusNode,
29+
autofocus: true,
30+
style: TextStyle(
31+
color: PaintroidTheme.of(context).onSurfaceColor,
32+
),
33+
decoration: InputDecoration(
34+
hintText: 'Search projects...',
35+
hintStyle: TextStyle(
36+
color:
37+
PaintroidTheme.of(context).onSurfaceColor.withOpacity(0.6),
4738
),
48-
],
39+
border: InputBorder.none,
40+
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
41+
),
42+
onChanged: onChanged,
4943
),
5044
),
5145
PopupMenuButton<SortOption>(
@@ -68,14 +62,18 @@ class SearchTextField extends StatelessWidget {
6862
size: 18,
6963
),
7064
const SizedBox(width: 8),
71-
Text(option.label),
65+
Flexible(
66+
child: Text(
67+
option.label,
68+
overflow: TextOverflow.ellipsis,
69+
),
70+
),
7271
],
7372
),
7473
),
7574
)
7675
.toList(),
7776
),
78-
const SizedBox(width: 2),
7977
],
8078
);
8179
}

lib/ui/pages/landing_page/landing_page.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ class _LandingPageState extends ConsumerState<LandingPage> {
231231
flex: 3,
232232
child: ListView.builder(
233233
itemBuilder: (context, index) {
234+
if (index == 0) {
235+
return Container();
236+
}
234237
Project project = filteredProjects[index];
235238
return ProjectListTile(
236239
project: project,

test/widget/landing_page/landing_page_test.dart

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:paintroid/core/providers/state/canvas_state_provider.dart';
2323
import 'package:paintroid/ui/pages/landing_page/components/main_overflow_menu.dart';
2424
import 'package:paintroid/ui/pages/landing_page/components/project_list_tile.dart';
2525
import 'package:paintroid/ui/pages/landing_page/components/project_overflow_menu.dart';
26+
import 'package:paintroid/ui/pages/landing_page/components/search_text_field.dart';
2627
import 'package:paintroid/ui/pages/workspace_page/components/top_bar/overflow_menu.dart';
2728
import 'package:paintroid/ui/pages/workspace_page/components/top_bar/top_app_bar.dart';
2829
import 'package:paintroid/ui/shared/dialogs/about_dialog.dart';
@@ -670,4 +671,149 @@ void main() {
670671
expect(find.byType(CircularProgressIndicator), findsOneWidget);
671672
},
672673
);
674+
675+
testWidgets(
676+
'Should show search bar when search icon is tapped',
677+
(tester) async {
678+
when(database.projectDAO).thenReturn(dao);
679+
when(dao.getProjects()).thenAnswer((_) => Future.value([]));
680+
await tester.pumpWidget(sut);
681+
await tester.pumpAndSettle();
682+
verify(database.projectDAO);
683+
verify(dao.getProjects());
684+
685+
final searchIcon = find.byIcon(Icons.search);
686+
expect(searchIcon, findsOneWidget);
687+
688+
await tester.tap(searchIcon);
689+
await tester.pumpAndSettle();
690+
691+
expect(find.byType(SearchTextField), findsOneWidget);
692+
expect(find.byIcon(Icons.close), findsOneWidget);
693+
expect(find.text('Search projects...'), findsOneWidget);
694+
},
695+
);
696+
697+
testWidgets(
698+
'Should hide search bar when close icon is tapped',
699+
(tester) async {
700+
when(database.projectDAO).thenReturn(dao);
701+
when(dao.getProjects()).thenAnswer((_) => Future.value([]));
702+
await tester.pumpWidget(sut);
703+
await tester.pumpAndSettle();
704+
705+
await tester.tap(find.byIcon(Icons.search));
706+
await tester.pumpAndSettle();
707+
expect(find.byType(SearchTextField), findsOneWidget);
708+
709+
await tester.tap(find.byIcon(Icons.close));
710+
await tester.pumpAndSettle();
711+
expect(find.byType(SearchTextField), findsNothing);
712+
expect(find.text('Pocket Paint'), findsOneWidget);
713+
},
714+
);
715+
716+
testWidgets(
717+
'Should filter projects based on search query',
718+
(tester) async {
719+
when(database.projectDAO).thenReturn(dao);
720+
when(dao.getProjects()).thenAnswer((_) => Future.value(projects));
721+
when(imageService.getProjectPreview(filePath))
722+
.thenReturn(Result.ok(testFile.readAsBytesSync()));
723+
await tester.pumpWidget(sut);
724+
await tester.pumpAndSettle();
725+
726+
await tester.tap(find.byIcon(Icons.search));
727+
await tester.pumpAndSettle();
728+
729+
await tester.enterText(find.byType(TextField), 'project1');
730+
await tester.pumpAndSettle();
731+
732+
expect(find.text('project1'), findsOneWidget);
733+
expect(find.text('project2'), findsNothing);
734+
},
735+
);
736+
737+
testWidgets(
738+
'Should show sort options menu when sort icon is tapped',
739+
(tester) async {
740+
when(database.projectDAO).thenReturn(dao);
741+
when(dao.getProjects()).thenAnswer((_) => Future.value([]));
742+
await tester.pumpWidget(sut);
743+
await tester.pumpAndSettle();
744+
745+
await tester.tap(find.byIcon(Icons.search));
746+
await tester.pumpAndSettle();
747+
748+
await tester.tap(find.byIcon(Icons.sort));
749+
await tester.pumpAndSettle();
750+
751+
expect(find.text('Name (A to Z)'), findsOneWidget);
752+
expect(find.text('Name (Z to A)'), findsOneWidget);
753+
expect(find.text('Last Modified (Newest)'), findsOneWidget);
754+
expect(find.text('Last Modified (Oldest)'), findsOneWidget);
755+
expect(find.text('Date Created (Newest)'), findsOneWidget);
756+
expect(find.text('Date Created (Oldest)'), findsOneWidget);
757+
},
758+
);
759+
760+
testWidgets(
761+
'Should sort projects by name when name sort option is selected',
762+
(tester) async {
763+
final projectsToSort = [
764+
createProject('Project first'),
765+
createProject('Project second'),
766+
createProject('Project third'),
767+
];
768+
769+
when(database.projectDAO).thenReturn(dao);
770+
when(deviceService.getSizeInPixels())
771+
.thenAnswer((_) => Future.value(const Size(1080, 1920)));
772+
when(dao.getProjects()).thenAnswer((_) => Future.value(projectsToSort));
773+
when(imageService.getProjectPreview(filePath))
774+
.thenReturn(Result.ok(testFile.readAsBytesSync()));
775+
await tester.pumpWidget(sut);
776+
await tester.pumpAndSettle();
777+
778+
await tester.tap(find.byIcon(Icons.search));
779+
await tester.pumpAndSettle();
780+
781+
await tester.tap(find.byIcon(Icons.sort));
782+
await tester.pumpAndSettle();
783+
await tester.tap(find.text('Name (A to Z)'));
784+
await tester.pumpAndSettle();
785+
786+
final projectNames = tester
787+
.widgetList<Text>(find.byType(Text))
788+
.map((widget) => widget.data)
789+
.where((text) =>
790+
text == 'Project first' ||
791+
text == 'Project second' ||
792+
text == 'Project third')
793+
.toList();
794+
795+
expect(projectNames[0], equals('Project second'));
796+
expect(projectNames[1], equals('Project third'));
797+
},
798+
);
799+
800+
testWidgets(
801+
'Should hide floating action buttons when search is active',
802+
(tester) async {
803+
when(database.projectDAO).thenReturn(dao);
804+
when(dao.getProjects()).thenAnswer((_) => Future.value([]));
805+
await tester.pumpWidget(sut);
806+
await tester.pumpAndSettle();
807+
808+
expect(find.byType(FloatingActionButton), findsNWidgets(2));
809+
810+
await tester.tap(find.byIcon(Icons.search));
811+
await tester.pumpAndSettle();
812+
813+
expect(find.byType(FloatingActionButton), findsNothing);
814+
await tester.tap(find.byIcon(Icons.close));
815+
await tester.pumpAndSettle();
816+
expect(find.byType(FloatingActionButton), findsNWidgets(2));
817+
},
818+
);
673819
}

0 commit comments

Comments
 (0)