Skip to content

Commit

Permalink
PAINTROID-696: If no projects available, create new one when tapping …
Browse files Browse the repository at this point in the history
…edit icon (#48)

* Add if no projects available, create new one when tapping edit icon

* Add tests

* Add plus icon

* Baki debug commit

* Add baki's good enough testcase
  • Loading branch information
msesko authored Jun 5, 2024
1 parent 3210d66 commit f162800
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 17 deletions.
54 changes: 37 additions & 17 deletions lib/ui/pages/landing_page/landing_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@ class _LandingPageState extends ConsumerState<LandingPage> {
Flexible(
flex: 2,
child: _ProjectPreview(
ioHandler: ioHandler,
imageService: imageService,
latestModifiedProject: latestModifiedProject,
openProject: () =>
_openProject(latestModifiedProject, ioHandler, ref),
),
ioHandler: ioHandler,
imageService: imageService,
latestModifiedProject: latestModifiedProject,
onProjectPreviewTap: () {
if (latestModifiedProject != null) {
_openProject(latestModifiedProject, ioHandler, ref);
} else {
_clearCanvas();
_navigateToPocketPaint();
}
}),
),
Container(
color: PaintroidTheme.of(context).primaryContainerColor,
Expand Down Expand Up @@ -208,13 +213,13 @@ class _ProjectPreview extends StatelessWidget {
final Project? latestModifiedProject;
final IOHandler ioHandler;
final IImageService imageService;
final VoidCallback openProject;
final VoidCallback onProjectPreviewTap;

const _ProjectPreview({
this.latestModifiedProject,
required this.ioHandler,
required this.imageService,
required this.openProject,
required this.onProjectPreviewTap,
});

@override
Expand All @@ -223,7 +228,7 @@ class _ProjectPreview extends StatelessWidget {
children: [
Material(
child: InkWell(
onTap: openProject,
onTap: onProjectPreviewTap,
child: ImagePreview(
project: latestModifiedProject,
imageService: imageService,
Expand All @@ -236,15 +241,30 @@ class _ProjectPreview extends StatelessWidget {
key: const Key('myEditIcon'),
iconSize: 264,
onPressed: () async {
if (latestModifiedProject != null) {
openProject();
}
onProjectPreviewTap.call();
},
icon: const IconSvg(
path: 'assets/svg/ic_edit_circle.svg',
height: 264.0,
width: 264.0,
),
icon: latestModifiedProject == null
? Container(
height: 170.0,
width: 170.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: PaintroidTheme.of(context)
.outlineColor
.withAlpha(180)),
child: Center(
child: Icon(
Icons.add,
color: PaintroidTheme.of(context).backgroundColor,
size: 150.0,
),
),
)
: const IconSvg(
path: 'assets/svg/ic_edit_circle.svg',
height: 264.0,
width: 264.0,
),
),
),
Align(
Expand Down
123 changes: 123 additions & 0 deletions test/widget/landing_page/landing_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:paintroid/core/models/database/project.dart';
import 'package:paintroid/core/providers/object/device_service.dart';
import 'package:paintroid/core/providers/object/file_service.dart';
import 'package:paintroid/core/providers/object/image_service.dart';
import 'package:paintroid/core/providers/state/canvas_state_provider.dart';
import 'package:paintroid/ui/pages/landing_page/components/main_overflow_menu.dart';
import 'package:paintroid/ui/pages/landing_page/components/project_list_tile.dart';
import 'package:paintroid/ui/pages/landing_page/components/project_overflow_menu.dart';
Expand Down Expand Up @@ -156,6 +157,128 @@ void main() {
},
);

testWidgets(
'Should open PocketPaint widget trough edit (new project) icon and return back to Landing page',
(tester) async {
when(database.projectDAO).thenReturn(dao);
when(dao.getProjects()).thenAnswer((_) => Future.value([]));
await tester.pumpWidget(sut);
await tester.pumpAndSettle();
verify(database.projectDAO);
verify(dao.getProjects());

final editButton = find.byKey(const Key('myEditIcon'));
await tester.tap(editButton);
await tester.pumpAndSettle();

expect(find.byType(TopAppBar), findsOneWidget);
expect(find.byType(NavigationBar), findsOneWidget);

final titleFinder = find.widgetWithText(TopAppBar, 'Pocket Paint');
expect(titleFinder, findsOneWidget);

final overflowMenuButtonFinder = find.widgetWithIcon(
PopupMenuButton<OverflowMenuOption>,
Icons.more_vert,
);
expect(overflowMenuButtonFinder, findsOneWidget);

// Check the canvas is empty
final container = ProviderContainer();
final canvasState = container.read(canvasStateProvider);
expect(canvasState.backgroundImage, isNull);
expect(canvasState.cachedImage, isNull);
expect(canvasState.size, equals(Size.zero));

await tester.pageBack();
await tester.pumpAndSettle();
expect(find.text('My Projects'), findsOneWidget);
},
);

testWidgets(
'Should open PocketPaint widget trough edit (new project) icon save it and return back to Landing page. Then open the project trough edit icon.',
(tester) async {
String projectName = 'project.catrobat-image';

when(database.projectDAO).thenReturn(dao);
when(dao.getProjects()).thenAnswer((_) => Future.value([]));
when(fileService.checkIfFileExistsInApplicationDirectory(projectName))
.thenAnswer((_) => Future.value(false));
when(imageService.getProjectPreview(filePath))
.thenReturn(Result.ok(testFile.readAsBytesSync()));
await tester.pumpWidget(sut);
await tester.pumpAndSettle();
verify(database.projectDAO);
verify(dao.getProjects());

final plusButton = find.byKey(const Key('myEditIcon'));
expect(plusButton, findsOneWidget);
await tester.tap(plusButton);
await tester.pumpAndSettle();

expect(find.byType(TopAppBar), findsOneWidget);
expect(find.byType(NavigationBar), findsOneWidget);

final titleFinder = find.widgetWithText(TopAppBar, 'Pocket Paint');
expect(titleFinder, findsOneWidget);

final overflowMenuButtonFinder = find.widgetWithIcon(
PopupMenuButton<OverflowMenuOption>,
Icons.more_vert,
);
expect(overflowMenuButtonFinder, findsOneWidget);

// Check the canvas is empty
final container = ProviderContainer();
final canvasState = container.read(canvasStateProvider);
expect(canvasState.backgroundImage, isNull);
expect(canvasState.cachedImage, isNull);
expect(canvasState.size, equals(Size.zero));

await tester.tap(overflowMenuButtonFinder);
await tester.pumpAndSettle();

final saveProjectButton = find.text('Save project');
expect(saveProjectButton, findsOneWidget);

await tester.tap(saveProjectButton);
await tester.pumpAndSettle();

final textFormField = find.widgetWithText(TextFormField, 'Project name');
expect(textFormField, findsOneWidget);

await tester.enterText(textFormField, 'project');

final saveButton = find.widgetWithText(TextButton, 'Save');
expect(saveButton, findsOneWidget);

await tester.tap(saveButton);
await tester.pumpAndSettle();

await tester.pageBack();
await tester.pumpAndSettle();

expect(find.text('My Projects'), findsOneWidget);

final editButton = find.byKey(const Key('myEditIcon'));
await tester.tap(editButton);
await tester.pumpAndSettle();

expect(find.byType(TopAppBar), findsOneWidget);
expect(find.byType(NavigationBar), findsOneWidget);

final titleFinder2 = find.widgetWithText(TopAppBar, 'Pocket Paint');
expect(titleFinder2, findsOneWidget);

final overflowMenuButtonFinder2 = find.widgetWithIcon(
PopupMenuButton<OverflowMenuOption>,
Icons.more_vert,
);
expect(overflowMenuButtonFinder2, findsOneWidget);
},
);

testWidgets(
'Should have "My Projects" section',
(tester) async {
Expand Down

0 comments on commit f162800

Please sign in to comment.