|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:integration_test/integration_test.dart'; |
| 6 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 7 | +import 'package:polyglot_pathways/main.dart' as app; |
| 8 | + |
| 9 | +void main() { |
| 10 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 11 | + |
| 12 | + // Get language from environment variable or default to 'en' |
| 13 | + const String languageCode = String.fromEnvironment( |
| 14 | + 'SCREENSHOT_LANGUAGE', |
| 15 | + defaultValue: 'en', |
| 16 | + ); |
| 17 | + |
| 18 | + group('Screenshot Tests', () { |
| 19 | + testWidgets('Generate screenshots for $languageCode', (WidgetTester tester) async { |
| 20 | + // Clear SharedPreferences to start fresh |
| 21 | + final prefs = await SharedPreferences.getInstance(); |
| 22 | + await prefs.clear(); |
| 23 | + |
| 24 | + // Set the language |
| 25 | + await prefs.setString('selectedLanguage', languageCode); |
| 26 | + |
| 27 | + // Set onboarding as completed so we can navigate to main screens |
| 28 | + await prefs.setBool('has_completed_onboarding', true); |
| 29 | + |
| 30 | + // Build the app |
| 31 | + await tester.pumpWidget(app.MyApp(prefs: prefs)); |
| 32 | + await tester.pumpAndSettle(const Duration(seconds: 2)); |
| 33 | + |
| 34 | + // Wait for any animations |
| 35 | + await Future.delayed(const Duration(milliseconds: 500)); |
| 36 | + |
| 37 | + // Screenshot: Home Screen |
| 38 | + await takeScreenshot(tester, '01_home'); |
| 39 | + |
| 40 | + // Navigate to Achievements tab |
| 41 | + await tester.tap(find.text('Achievements').last); |
| 42 | + await tester.pumpAndSettle(const Duration(seconds: 2)); |
| 43 | + await Future.delayed(const Duration(milliseconds: 500)); |
| 44 | + await takeScreenshot(tester, '02_achievements'); |
| 45 | + |
| 46 | + // Navigate to Profile tab |
| 47 | + await tester.tap(find.text('Profile').last); |
| 48 | + await tester.pumpAndSettle(const Duration(seconds: 2)); |
| 49 | + await Future.delayed(const Duration(milliseconds: 500)); |
| 50 | + await takeScreenshot(tester, '03_profile'); |
| 51 | + |
| 52 | + // Navigate to Settings tab |
| 53 | + await tester.tap(find.text('Settings').last); |
| 54 | + await tester.pumpAndSettle(const Duration(seconds: 2)); |
| 55 | + await Future.delayed(const Duration(milliseconds: 500)); |
| 56 | + await takeScreenshot(tester, '04_settings'); |
| 57 | + |
| 58 | + // Go back to Home tab |
| 59 | + await tester.tap(find.text('Home').last); |
| 60 | + await tester.pumpAndSettle(const Duration(seconds: 2)); |
| 61 | + await Future.delayed(const Duration(milliseconds: 500)); |
| 62 | + |
| 63 | + // Try to open language selection - look for language cards more carefully |
| 64 | + // First, scroll if needed to see language cards |
| 65 | + try { |
| 66 | + final scrollable = find.byType(Scrollable); |
| 67 | + if (scrollable.evaluate().isNotEmpty) { |
| 68 | + await tester.drag(scrollable.first, const Offset(0, -200)); |
| 69 | + await tester.pumpAndSettle(const Duration(seconds: 1)); |
| 70 | + } |
| 71 | + } catch (e) { |
| 72 | + // Ignore if scroll fails |
| 73 | + } |
| 74 | + |
| 75 | + // Try to find and tap a language card by looking for InkWell widgets |
| 76 | + // that are likely language cards (they're tappable) |
| 77 | + try { |
| 78 | + final inkWells = find.byType(InkWell); |
| 79 | + if (inkWells.evaluate().length > 2) { |
| 80 | + // Skip the first few (might be navigation), tap a language card |
| 81 | + await tester.tap(inkWells.at(2)); |
| 82 | + await tester.pumpAndSettle(const Duration(seconds: 2)); |
| 83 | + await Future.delayed(const Duration(milliseconds: 500)); |
| 84 | + await takeScreenshot(tester, '05_language_selection'); |
| 85 | + |
| 86 | + // Try to close the sheet and go back |
| 87 | + final closeButton = find.byIcon(Icons.close); |
| 88 | + if (closeButton.evaluate().isNotEmpty) { |
| 89 | + await tester.tap(closeButton.first); |
| 90 | + await tester.pumpAndSettle(const Duration(seconds: 1)); |
| 91 | + } |
| 92 | + } |
| 93 | + } catch (e) { |
| 94 | + // If language selection fails, continue with onboarding screens |
| 95 | + print('Could not capture language selection: $e'); |
| 96 | + } |
| 97 | + |
| 98 | + // For onboarding screens, we need to restart the app |
| 99 | + // This is complex in integration tests, so we'll skip for now |
| 100 | + // and focus on the main app screens which are more important |
| 101 | + }); |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +Future<void> takeScreenshot(WidgetTester tester, String name) async { |
| 106 | + await tester.binding.defaultBinaryMessenger.handlePlatformMessage( |
| 107 | + 'flutter/screenshot', |
| 108 | + null, |
| 109 | + (ByteData? data) {}, |
| 110 | + ); |
| 111 | + |
| 112 | + await IntegrationTestWidgetsFlutterBinding.instance.takeScreenshot(name); |
| 113 | +} |
| 114 | + |
0 commit comments