|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +import 'package:flutter_localizations/flutter_localizations.dart'; |
| 4 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:integration_test/integration_test.dart'; |
| 7 | + |
| 8 | +import 'package:paintroid/core/localization/app_localizations.dart'; |
| 9 | +import 'package:paintroid/core/providers/object/device_service.dart'; |
| 10 | +import 'package:paintroid/core/tools/tool_data.dart'; |
| 11 | +import 'package:paintroid/ui/pages/workspace_page/workspace_page.dart'; |
| 12 | +import 'package:paintroid/ui/theme/theme.dart'; |
| 13 | +import '../utils/bottom_nav_bar_interactions.dart'; |
| 14 | +import '../utils/interactive_viewer_interactions.dart'; |
| 15 | + |
| 16 | +void main() { |
| 17 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 18 | + |
| 19 | + const String testIDStr = String.fromEnvironment('id', defaultValue: '-1'); |
| 20 | + final testID = int.tryParse(testIDStr) ?? testIDStr; |
| 21 | + |
| 22 | + late Widget sut; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + final lightTheme = LightPaintroidThemeData(); |
| 26 | + final darkTheme = DarkPaintroidThemeData(); |
| 27 | + |
| 28 | + sut = ProviderScope( |
| 29 | + overrides: [ |
| 30 | + IDeviceService.sizeProvider |
| 31 | + .overrideWith((ref) => Future.value(const Size(600, 600))) |
| 32 | + ], |
| 33 | + child: PaintroidTheme( |
| 34 | + lightTheme: lightTheme, |
| 35 | + darkTheme: darkTheme, |
| 36 | + child: MaterialApp( |
| 37 | + theme: lightTheme.materialThemeData, |
| 38 | + darkTheme: darkTheme.materialThemeData, |
| 39 | + home: const WorkspacePage(), |
| 40 | + localizationsDelegates: const [ |
| 41 | + AppLocalizations.delegate, |
| 42 | + GlobalMaterialLocalizations.delegate, |
| 43 | + ], |
| 44 | + ), |
| 45 | + ), |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + if (testID == -1 || testID == 0) { |
| 50 | + testWidgets( |
| 51 | + '[HAND_TOOL]: Pan sequentially in different directions and verify each step', |
| 52 | + (WidgetTester tester) async { |
| 53 | + await tester.pumpWidget(sut); |
| 54 | + await tester.pumpAndSettle(); |
| 55 | + |
| 56 | + final bottomNavBarInteractions = BottomNavBarInteractions(tester); |
| 57 | + final interactiveViewerInteractions = |
| 58 | + InterActiveViewerInteractions(tester); |
| 59 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 60 | + |
| 61 | + await interactiveViewerInteractions.panAndVerify(const Offset(100, 0)); |
| 62 | + await interactiveViewerInteractions.panAndVerify(const Offset(0, 100)); |
| 63 | + await interactiveViewerInteractions.panAndVerify(const Offset(-100, 0)); |
| 64 | + await interactiveViewerInteractions.panAndVerify(const Offset(0, -100)); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + if (testID == -1 || testID == 1) { |
| 69 | + testWidgets('[HAND_TOOL]: Tiny pans in different directions', |
| 70 | + (WidgetTester tester) async { |
| 71 | + await tester.pumpWidget(sut); |
| 72 | + await tester.pumpAndSettle(); |
| 73 | + |
| 74 | + final bottomNavBarInteractions = BottomNavBarInteractions(tester); |
| 75 | + final interactiveViewerInteractions = |
| 76 | + InterActiveViewerInteractions(tester); |
| 77 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 78 | + |
| 79 | + await interactiveViewerInteractions.panAndVerify(const Offset(1, 0)); |
| 80 | + await interactiveViewerInteractions.panAndVerify(const Offset(0, 1)); |
| 81 | + await interactiveViewerInteractions.panAndVerify(const Offset(-1, 0)); |
| 82 | + await interactiveViewerInteractions.panAndVerify(const Offset(0, -1)); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + if (testID == -1 || testID == 2) { |
| 87 | + testWidgets( |
| 88 | + '[HAND_TOOL]: Pan, switch tool, switch back to Hand, and continue panning', |
| 89 | + (WidgetTester tester) async { |
| 90 | + await tester.pumpWidget(sut); |
| 91 | + await tester.pumpAndSettle(); |
| 92 | + |
| 93 | + final bottomNavBarInteractions = BottomNavBarInteractions(tester); |
| 94 | + final interactiveViewerInteractions = |
| 95 | + InterActiveViewerInteractions(tester); |
| 96 | + |
| 97 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 98 | + await interactiveViewerInteractions.panAndVerify(const Offset(50, 50)); |
| 99 | + |
| 100 | + await bottomNavBarInteractions.selectTool(ToolData.BRUSH); |
| 101 | + await tester.pumpAndSettle(); |
| 102 | + |
| 103 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 104 | + await tester.pumpAndSettle(); |
| 105 | + |
| 106 | + await interactiveViewerInteractions.panAndVerify(const Offset(50, 50)); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + if (testID == -1 || testID == 3) { |
| 111 | + testWidgets('[HAND_TOOL]: Pan by zero offset', (WidgetTester tester) async { |
| 112 | + await tester.pumpWidget(sut); |
| 113 | + await tester.pumpAndSettle(); |
| 114 | + |
| 115 | + final bottomNavBarInteractions = BottomNavBarInteractions(tester); |
| 116 | + final interactiveViewerInteractions = |
| 117 | + InterActiveViewerInteractions(tester); |
| 118 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 119 | + |
| 120 | + await interactiveViewerInteractions.panAndVerify(Offset.zero); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + if (testID == -1 || testID == 4) { |
| 125 | + testWidgets('[HAND_TOOL]: Select hand tool multiple times then pan', |
| 126 | + (WidgetTester tester) async { |
| 127 | + await tester.pumpWidget(sut); |
| 128 | + await tester.pumpAndSettle(); |
| 129 | + |
| 130 | + final bottomNavBarInteractions = BottomNavBarInteractions(tester); |
| 131 | + final interactiveViewerInteractions = |
| 132 | + InterActiveViewerInteractions(tester); |
| 133 | + |
| 134 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 135 | + await tester.pumpAndSettle(); |
| 136 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 137 | + await tester.pumpAndSettle(); |
| 138 | + |
| 139 | + await interactiveViewerInteractions.panAndVerify(const Offset(30, -30)); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + if (testID == -1 || testID == 5) { |
| 144 | + testWidgets('[HAND_TOOL]: Pan after zoom', (WidgetTester tester) async { |
| 145 | + await tester.pumpWidget(sut); |
| 146 | + await tester.pumpAndSettle(); |
| 147 | + |
| 148 | + final bottomNavBarInteractions = BottomNavBarInteractions(tester); |
| 149 | + final interactiveViewerInteractions = |
| 150 | + InterActiveViewerInteractions(tester); |
| 151 | + await bottomNavBarInteractions.selectTool(ToolData.HAND); |
| 152 | + |
| 153 | + final ivFinder = find.byType(InteractiveViewer); |
| 154 | + expect(ivFinder, findsOneWidget, |
| 155 | + reason: 'InteractiveViewer should be present'); |
| 156 | + InteractiveViewer interactiveViewer = tester.widget(ivFinder); |
| 157 | + TransformationController controller = |
| 158 | + interactiveViewer.transformationController!; |
| 159 | + |
| 160 | + final initialMatrixBeforeZoom = Matrix4.copy(controller.value); |
| 161 | + final initialTranslation = initialMatrixBeforeZoom.getTranslation(); |
| 162 | + |
| 163 | + const double zoomFactor = 2.0; |
| 164 | + controller.value = Matrix4.identity() |
| 165 | + ..scale(zoomFactor) |
| 166 | + ..translate(initialTranslation.x, initialTranslation.y); |
| 167 | + |
| 168 | + await tester.pumpAndSettle(); |
| 169 | + |
| 170 | + expect(controller.value[0], closeTo(zoomFactor, 0.001), |
| 171 | + reason: 'X scale should be zoomFactor'); |
| 172 | + expect(controller.value[5], closeTo(zoomFactor, 0.001), |
| 173 | + reason: 'Y scale should be zoomFactor'); |
| 174 | + |
| 175 | + await interactiveViewerInteractions.panAndVerify(const Offset(50, 50)); |
| 176 | + }); |
| 177 | + } |
| 178 | +} |
0 commit comments