Skip to content

Commit

Permalink
paintroid-765 add star and heart shape unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bakicelebi committed Nov 4, 2024
1 parent b3d2be9 commit 3652d58
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ SPEC CHECKSUMS:
file_picker: ce3938a0df3cc1ef404671531facef740d03f920
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_localization: f43b18844a2b3d2c71fd64f04ffd6b1e64dd54d4
image_picker_ios: c560581cceedb403a6ff17f2f816d7fea1421fc1
image_picker_ios: 99dfe1854b4fa34d0364e74a78448a0151025425
integration_test: ce0a3ffa1de96d1a89ca0ac26fca7ea18a749ef4
launch_review: 75d5a956ba8eaa493e9c9d4bf4c05e505e8d5ed0
package_info_plus: 115f4ad11e0698c8c1c5d8a689390df880f47e85
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
path_provider_foundation: 3784922295ac71e43754bd15e0653ccfd36a147c
permission_handler_apple: e76247795d700c14ea09e3a2d8855d41ee80a2e6
SDWebImage: dfe95b2466a9823cf9f0c6d01217c06550d7b29a
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
sqflite: 673a0e54cc04b7d6dba8d24fb8095b31c3a99eec
SwiftyGif: 706c60cf65fa2bc5ee0313beece843c8eb8194d4
url_launcher_ios: 5334b05cef931de560670eeae103fd3e431ac3fe
url_launcher_ios: bbd758c6e7f9fd7b5b1d4cde34d2b95fcce5e812

PODFILE CHECKSUM: 303789365c3a8d7bc562e5e65d7e8e15218ec5c6

Expand Down
37 changes: 37 additions & 0 deletions test/unit/command/shape_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/circle_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/heart_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/square_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/star_shape_command.dart';
import 'shape_command_test.mocks.dart';

@GenerateMocks([Canvas])
Expand All @@ -23,6 +25,27 @@ void main() {
const center = Offset(200, 200);
final circleShapeCommand = CircleShapeCommand(testPaint, radius, center);

const numberOfPoints = 5;
const angle = 0.0;
final starShapeCommand = StarShapeCommand(
testPaint,
numberOfPoints,
radius,
angle,
center,
);

const width = 200.0;
const height = 200.0;

final heartShapeCommand = HeartShapeCommand(
testPaint,
width,
height,
angle,
center,
);

setUp(() => mockCanvas = MockCanvas());

test('SquareShapeCommand: should call drawPath path', () {
Expand All @@ -38,4 +61,18 @@ void main() {
verify(mockCanvas.drawCircle(any, any, testPaint));
verifyNoMoreInteractions(mockCanvas);
});

test('StarShapeCommand: should call drawPath path', () {
when(mockCanvas.drawPath(any, testPaint)).thenReturn(null);
starShapeCommand.call(mockCanvas);
verify(mockCanvas.drawPath(any, testPaint));
verifyNoMoreInteractions(mockCanvas);
});

test('HeartShapeCommand: should call drawPath path', () {
when(mockCanvas.drawPath(any, testPaint)).thenReturn(null);
heartShapeCommand.call(mockCanvas);
verify(mockCanvas.drawPath(any, testPaint));
verifyNoMoreInteractions(mockCanvas);
});
}
43 changes: 43 additions & 0 deletions test/unit/serialization/command/heart_shape_serializer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/heart_shape_command.dart';
import 'package:paintroid/core/json_serialization/versioning/serializer_version.dart';

import '../utils/dummy_command_factory.dart';
import '../utils/dummy_paint_factory.dart';

void main() {
group('Version 1', () {
test('Test Star serialization', () {
const type = SerializerType.HEART_SHAPE_COMMAND;
final originalPaint = DummyPaintFactory.createPaint(version: Version.v1);
const center = Offset(100, 100);
const width = 50.0;
const height = 50.0;
const angle = 0.0;

final command = DummyCommandFactory.createHeartShapeCommand(
originalPaint,
width,
height,
angle,
center,
);

final deserializedCommand = HeartShapeCommand.fromJson(command.toJson());

expect(
DummyPaintFactory.comparePaint(
originalPaint,
deserializedCommand.paint,
version: Version.v1,
),
isTrue);
expect(command.version, equals(deserializedCommand.version));
expect(deserializedCommand.center, equals(center));
expect(deserializedCommand.type, equals(type));
expect(deserializedCommand.angle, equals(angle));
expect(deserializedCommand.width, equals(width));
expect(deserializedCommand.height, equals(height));
});
});
}
42 changes: 42 additions & 0 deletions test/unit/serialization/command/star_shape_serializer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/star_shape_command.dart';
import 'package:paintroid/core/json_serialization/versioning/serializer_version.dart';

import '../utils/dummy_command_factory.dart';
import '../utils/dummy_paint_factory.dart';

void main() {
group('Version 1', () {
test('Test Star serialization', () {
const type = SerializerType.STAR_SHAPE_COMMAND;
final originalPaint = DummyPaintFactory.createPaint(version: Version.v1);
const center = Offset(100, 100);
const radius = 50.0;
const numberOfPoints = 5;
const angle = 0.0;
final command = DummyCommandFactory.createStarShapeCommand(
originalPaint,
numberOfPoints,
radius,
angle,
center,
);

final deserializedCommand = StarShapeCommand.fromJson(command.toJson());

expect(
DummyPaintFactory.comparePaint(
originalPaint,
deserializedCommand.paint,
version: Version.v1,
),
isTrue);
expect(command.version, equals(deserializedCommand.version));
expect(deserializedCommand.center, equals(center));
expect(deserializedCommand.radius, equals(radius));
expect(deserializedCommand.type, equals(type));
expect(deserializedCommand.numberOfPoints, equals(numberOfPoints));
expect(deserializedCommand.angle, equals(angle));
});
});
}
42 changes: 42 additions & 0 deletions test/unit/serialization/utils/dummy_command_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:paintroid/core/commands/command_implementation/command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/line_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/path_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/circle_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/heart_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/square_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/star_shape_command.dart';
import 'package:paintroid/core/commands/path_with_action_history.dart';
import 'package:paintroid/core/json_serialization/versioning/serializer_version.dart';
import 'package:paintroid/core/json_serialization/versioning/version_strategy.dart';
Expand Down Expand Up @@ -87,6 +89,46 @@ class DummyCommandFactory {
return commandFactory.createCircleShapeCommand(paint, radius, center);
}

static StarShapeCommand createStarShapeCommand(
Paint paint,
int numPoints,
double radius,
double angle,
Offset center, {
int version = Version.v1,
}) {
VersionStrategyManager.setStrategy(
DummyVersionStrategy(starShapeCommandVersion: version),
);
return commandFactory.createStarShapeCommand(
paint,
numPoints,
radius,
angle,
center,
);
}

static HeartShapeCommand createHeartShapeCommand(
Paint paint,
double width,
double height,
double angle,
Offset center, {
int version = Version.v1,
}) {
VersionStrategyManager.setStrategy(
DummyVersionStrategy(heartShapeCommandVersion: version),
);
return commandFactory.createHeartShapeCommand(
paint,
width,
height,
angle,
center,
);
}

static bool compareCommandLists(
Iterable<Command> commands1, Iterable<Command> commands2) {
if (commands1.length != commands2.length) {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/tools/shapes_tool_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';
import 'package:paintroid/core/commands/command_factory/command_factory.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/circle_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/heart_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/square_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/star_shape_command.dart';
import 'package:paintroid/core/commands/command_manager/command_manager.dart';
import 'package:paintroid/core/enums/shape_type.dart';
import 'package:paintroid/core/enums/tool_types.dart';
Expand Down Expand Up @@ -31,6 +33,20 @@ void main() {
);
});

test('onCheckmark: should generate HearthShapeCommand', () {
sut.shapeType = ShapeType.heart;
sut.onCheckmark(paint);
final command = sut.commandManager.undoStack.last;
expect(command.runtimeType, HeartShapeCommand);
});

test('onCheckmark: should generate StarShapeCommand', () {
sut.shapeType = ShapeType.star;
sut.onCheckmark(paint);
final command = sut.commandManager.undoStack.last;
expect(command.runtimeType, StarShapeCommand);
});

test('onCheckmark: should generate CircleShapeCommand', () {
sut.shapeType = ShapeType.circle;
sut.onCheckmark(paint);
Expand Down

0 comments on commit 3652d58

Please sign in to comment.