forked from Catrobat/Paintroid-Flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Catrobat#34 from bakicelebi/PAINTROID-658
PAINTROID-658 : Add hand tool
- Loading branch information
Showing
9 changed files
with
255 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:equatable/equatable.dart'; | ||
import 'package:paintroid/command/src/command_factory.dart'; | ||
import 'package:paintroid/command/src/command_manager.dart'; | ||
import 'package:paintroid/core/graphic_factory.dart'; | ||
import 'package:paintroid/tool/src/tool.dart'; | ||
import 'package:paintroid/tool/src/tool_types.dart'; | ||
|
||
class HandTool extends Tool with EquatableMixin { | ||
HandTool({ | ||
required super.paint, | ||
required super.commandFactory, | ||
required super.commandManager, | ||
required super.type, | ||
}); | ||
|
||
@override | ||
void onDown(Offset point) {} | ||
|
||
@override | ||
void onDrag(Offset point) {} | ||
|
||
@override | ||
void onUp(Offset? point) {} | ||
|
||
@override | ||
void onCancel() {} | ||
|
||
@override | ||
List<Object?> get props => [commandManager, commandFactory]; | ||
|
||
HandTool copyWith({ | ||
Paint? paint, | ||
CommandFactory? commandFactory, | ||
CommandManager? commandManager, | ||
GraphicFactory? graphicFactory, | ||
ToolType? type, | ||
}) { | ||
return HandTool( | ||
paint: paint ?? this.paint, | ||
commandFactory: commandFactory ?? this.commandFactory, | ||
commandManager: commandManager ?? this.commandManager, | ||
type: type ?? this.type, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:paintroid/command/src/command_factory_provider.dart'; | ||
import 'package:paintroid/command/src/command_manager_provider.dart'; | ||
import 'package:paintroid/tool/src/brush_tool/brush_tool_state_provider.dart'; | ||
import 'package:paintroid/tool/src/hand_tool/hand_tool.dart'; | ||
import 'package:paintroid/tool/src/tool_types.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'hand_tool_provider.g.dart'; | ||
|
||
@riverpod | ||
HandTool handTool(HandToolRef ref) { | ||
return HandTool( | ||
paint: ref.watch(brushToolStateProvider.select((state) => state.paint)), | ||
type: ToolType.HAND, | ||
commandManager: ref.watch(commandManagerProvider), | ||
commandFactory: ref.watch(commandFactoryProvider), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/annotations.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:paintroid/command/src/command_factory.dart'; | ||
import 'package:paintroid/command/src/command_manager.dart'; | ||
import 'package:paintroid/tool/src/hand_tool/hand_tool.dart'; | ||
import 'package:paintroid/tool/src/tool_types.dart'; | ||
|
||
import 'hand_tool_test.mocks.dart'; | ||
|
||
@GenerateMocks([ | ||
Paint, | ||
CommandManager, | ||
CommandFactory, | ||
]) | ||
void main() { | ||
late MockPaint mockPaint; | ||
late MockCommandFactory mockCommandFactory; | ||
late MockCommandManager mockCommandManager; | ||
|
||
late HandTool sut; | ||
const Offset offset = Offset(10, 10); | ||
|
||
setUp(() { | ||
mockPaint = MockPaint(); | ||
mockCommandFactory = MockCommandFactory(); | ||
mockCommandManager = MockCommandManager(); | ||
|
||
sut = HandTool( | ||
paint: mockPaint, | ||
commandFactory: mockCommandFactory, | ||
commandManager: mockCommandManager, | ||
type: ToolType.HAND, | ||
); | ||
}); | ||
|
||
group('HandTool Tests', () { | ||
test('onDown should not interact with any dependencies', () { | ||
sut.onDown(offset); | ||
|
||
verifyNoMoreInteractions(mockPaint); | ||
verifyNoMoreInteractions(mockCommandFactory); | ||
verifyNoMoreInteractions(mockCommandManager); | ||
}); | ||
|
||
test('onDrag should not interact with any dependencies', () { | ||
sut.onDrag(offset); | ||
|
||
verifyNoMoreInteractions(mockPaint); | ||
verifyNoMoreInteractions(mockCommandFactory); | ||
verifyNoMoreInteractions(mockCommandManager); | ||
}); | ||
|
||
test('onUp should not interact with any dependencies', () { | ||
sut.onUp(offset); | ||
|
||
verifyNoMoreInteractions(mockPaint); | ||
verifyNoMoreInteractions(mockCommandFactory); | ||
verifyNoMoreInteractions(mockCommandManager); | ||
}); | ||
|
||
test('onCancel should not interact with any dependencies', () { | ||
sut.onCancel(); | ||
|
||
verifyNoMoreInteractions(mockPaint); | ||
verifyNoMoreInteractions(mockCommandFactory); | ||
verifyNoMoreInteractions(mockCommandManager); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
import 'package:paintroid/service/device_service.dart'; | ||
import 'package:paintroid/tool/tool.dart'; | ||
import 'package:paintroid/ui/pocket_paint.dart'; | ||
|
||
import '../utils/utils.dart'; | ||
import '../utils/interactions/interactive_viewer_interactions.dart'; | ||
|
||
void main() { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
late Widget sut; | ||
|
||
setUp(() { | ||
sut = ProviderScope( | ||
overrides: [ | ||
IDeviceService.sizeProvider | ||
.overrideWith((ref) => Future.value(const Size(600, 600))) | ||
], | ||
child: const MaterialApp( | ||
home: PocketPaint(), | ||
), | ||
); | ||
}); | ||
|
||
testWidgets('Pan bottom left', (WidgetTester tester) async { | ||
await tester.pumpWidget(sut); | ||
await tester.pumpAndSettle(); | ||
|
||
final bottomNavBarInteractions = BottomNavBarInteractions(tester); | ||
final interactiveViewerInteractions = InterActiveViewerInteractions(tester); | ||
await bottomNavBarInteractions.selectTool(ToolData.HAND); | ||
await interactiveViewerInteractions.panAndVerify(const Offset(-50, 50)); | ||
}); | ||
|
||
testWidgets('Pan bottom right', (WidgetTester tester) async { | ||
await tester.pumpWidget(sut); | ||
await tester.pumpAndSettle(); | ||
|
||
final bottomNavBarInteractions = BottomNavBarInteractions(tester); | ||
final interactiveViewerInteractions = InterActiveViewerInteractions(tester); | ||
await bottomNavBarInteractions.selectTool(ToolData.HAND); | ||
await interactiveViewerInteractions.panAndVerify(const Offset(50, 50)); | ||
}); | ||
|
||
testWidgets('Pan top left', (WidgetTester tester) async { | ||
await tester.pumpWidget(sut); | ||
await tester.pumpAndSettle(); | ||
|
||
final bottomNavBarInteractions = BottomNavBarInteractions(tester); | ||
final interactiveViewerInteractions = InterActiveViewerInteractions(tester); | ||
await bottomNavBarInteractions.selectTool(ToolData.HAND); | ||
await interactiveViewerInteractions.panAndVerify(const Offset(-50, -50)); | ||
}); | ||
|
||
testWidgets('Pan top right', (WidgetTester tester) async { | ||
await tester.pumpWidget(sut); | ||
await tester.pumpAndSettle(); | ||
|
||
final bottomNavBarInteractions = BottomNavBarInteractions(tester); | ||
final interactiveViewerInteractions = InterActiveViewerInteractions(tester); | ||
await bottomNavBarInteractions.selectTool(ToolData.HAND); | ||
await interactiveViewerInteractions.panAndVerify(const Offset(50, -50)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/widget/utils/interactions/interactive_viewer_interactions.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
class InterActiveViewerInteractions { | ||
InterActiveViewerInteractions(this._tester); | ||
|
||
double epsilon = 0.1; | ||
|
||
final WidgetTester _tester; | ||
|
||
Future<InterActiveViewerInteractions> panAndVerify(Offset offset) async { | ||
final finder = find.byType(InteractiveViewer); | ||
|
||
expect(finder, findsOneWidget); | ||
|
||
InteractiveViewer interactiveViewer = _tester.widget(finder); | ||
|
||
TransformationController controller = | ||
interactiveViewer.transformationController!; | ||
|
||
expect(controller, isNotNull); | ||
|
||
final initialMatrix = controller.value; | ||
|
||
await _tester.drag(finder, const Offset(-50, 50)); | ||
await _tester.pumpAndSettle(); | ||
|
||
double expectedX = initialMatrix.getTranslation().x - 50; | ||
double expectedY = initialMatrix.getTranslation().y + 50; | ||
|
||
expect(controller.value.getTranslation().x, closeTo(expectedX, epsilon)); | ||
expect(controller.value.getTranslation().y, closeTo(expectedY, epsilon)); | ||
return this; | ||
} | ||
} |