Skip to content

Commit

Permalink
PAINTROID-765 add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bakicelebi committed Nov 4, 2024
1 parent 3652d58 commit a2113c9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/core/utils/widget_identifier.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class WidgetIdentifier {
static const canvasPainter = 'CanvasPainter';
static const newImageActionButton = 'NewImageActionButton';
static const squareShapeTypeChip = 'SquareShapeTypeChip';
static const circleShapeTypeChip = 'CircleShapeTypeChip';
static const starShapeTypeChip = 'StarShapeTypeChip';
static const heartShapeTypeChip = 'HeartShapeTypeChip';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class ShapesToolShapeTypeOptions extends ConsumerWidget {
Wrap(
children: [
CustomActionChip(
key: const ValueKey(
WidgetIdentifier.squareShapeTypeChip,
),
hint: 'Square',
chipBackgroundColor:
shapesToolOptionsState.shapeType == ShapeType.square
Expand Down
62 changes: 61 additions & 1 deletion test/integration/shapes_tool_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ void main() {
await tester.pumpWidget(sut);
await UIInteraction.createNewImage();
await UIInteraction.selectTool(ToolData.SHAPES.name);
await UIInteraction.selectCircleShapeTypeChip();
await UIInteraction.selectShapesToolShapeType(
WidgetFinder.circleShapeTypeChip,
);

final (left, top, right, bottom) =
await UIInteraction.getCircleShapeColors();
Expand All @@ -79,4 +81,62 @@ void main() {
expect(bottomAfter, currentColor);
});
}

if (testID == -1 || testID == 1) {
testWidgets('[SHAPES_TOOL]: test star shape', (WidgetTester tester) async {
UIInteraction.initialize(tester);
await tester.pumpWidget(sut);
await UIInteraction.createNewImage();
await UIInteraction.selectTool(ToolData.SHAPES.name);
await UIInteraction.selectShapesToolShapeType(
WidgetFinder.starShapeTypeChip,
);

final colorsBefore = await UIInteraction.getStarShapeColors();

for (final color in colorsBefore) {
expect(color, Colors.transparent);
}

await UIInteraction.tapAt(CanvasPosition.center);
await UIInteraction.clickCheckmark();

final colorsAfter = await UIInteraction.getStarShapeColors();

final currentColor = UIInteraction.getCurrentColor();

for (final color in colorsAfter) {
expect(color, currentColor);
}
});
}

if (testID == -1 || testID == 1) {
testWidgets('[SHAPES_TOOL]: test heart shape', (WidgetTester tester) async {
UIInteraction.initialize(tester);
await tester.pumpWidget(sut);
await UIInteraction.createNewImage();
await UIInteraction.selectTool(ToolData.SHAPES.name);
await UIInteraction.selectShapesToolShapeType(
WidgetFinder.heartShapeTypeChip,
);

final colorsBefore = await UIInteraction.getHeartShapeColors();

for (final color in colorsBefore) {
expect(color, Colors.transparent);
}

await UIInteraction.tapAt(CanvasPosition.center);
await UIInteraction.clickCheckmark();

final colorsAfter = await UIInteraction.getHeartShapeColors();

final currentColor = UIInteraction.getCurrentColor();

for (final color in colorsAfter) {
expect(color, currentColor);
}
});
}
}
50 changes: 47 additions & 3 deletions test/utils/ui_interaction.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -75,6 +78,32 @@ class UIInteraction {
return (leftPixel, rightPixel, topPixel, bottomPixel);
}

static Future<List<Color>> getStarShapeColors() async {
final shapesTool = getShapesTool();
final padding = getCurrentPaint().strokeWidth * sqrt2 / 2;
final radius = shapesTool.boundingBox.innerRadius - padding;
final path = shapesTool.boundingBox
.getStarPath(ShapesTool.starShapeNumberOfPoints, radius);
// trace number of points in star shape and calculate the colors of the corners
final points = extractPointsFromPath(path);
final colors = <Color>[];
for (final point in points) {
colors.add(await getPixelColor(point.dx.toInt(), point.dy.toInt()));
}
return colors;
}

static Future<List<Color>> getHeartShapeColors() async {
final shapesTool = getShapesTool();
final path = shapesTool.boundingBox.getHeartPath();
final points = extractPointsFromPath(path);
final colors = <Color>[];
for (final point in points) {
colors.add(await getPixelColor(point.dx.toInt(), point.dy.toInt()));
}
return colors;
}

static Future<Color> getPixelColor(int x, int y) async {
final container =
ProviderScope.containerOf(tester.element(find.byType(App)));
Expand All @@ -100,6 +129,21 @@ class UIInteraction {
return Color(argbColor);
}

static List<Offset> extractPointsFromPath(Path path) {
// Get the metrics for the path
final List<Offset> points = [];
for (PathMetric pathMetric in path.computeMetrics()) {
// Iterate through the path segments
for (double distance = 0; distance < pathMetric.length; distance += 1.0) {
Tangent? tangent = pathMetric.getTangentForOffset(distance);
if (tangent != null) {
points.add(tangent.position);
}
}
}
return points;
}

static Future<void> createNewImage() async {
expect(WidgetFinder.newImageButton, findsOneWidget);
await tester.tap(WidgetFinder.newImageButton);
Expand Down Expand Up @@ -175,9 +219,9 @@ class UIInteraction {
}
}

static Future<void> selectCircleShapeTypeChip() async {
expect(WidgetFinder.circleShapeTypeChip, findsOneWidget);
await tester.tap(WidgetFinder.circleShapeTypeChip);
static Future<void> selectShapesToolShapeType(Finder shapeTypeFinder) async {
expect(shapeTypeFinder, findsOneWidget);
await tester.tap(shapeTypeFinder);
await tester.pumpAndSettle();
}

Expand Down
6 changes: 6 additions & 0 deletions test/utils/widget_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class WidgetFinder {

static final Finder circleShapeTypeChip =
find.byKey(const ValueKey(WidgetIdentifier.circleShapeTypeChip));
static final Finder squareShapeTypeChip =
find.byKey(const ValueKey(WidgetIdentifier.squareShapeTypeChip));
static final Finder heartShapeTypeChip =
find.byKey(const ValueKey(WidgetIdentifier.heartShapeTypeChip));
static final Finder starShapeTypeChip =
find.byKey(const ValueKey(WidgetIdentifier.starShapeTypeChip));
}

0 comments on commit a2113c9

Please sign in to comment.