| 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:flutter_test/flutter_test.dart'; |
| 3 | import 'package:integration_test/integration_test.dart'; |
| 4 | import 'package:cw_core/wallet_type.dart'; |
| 5 | |
| 6 | import '../components/common_test_constants.dart'; |
| 7 | import '../components/common_test_flows.dart'; |
| 8 | import '../components/common_test_cases.dart'; |
| 9 | import '../robots/dashboard_page_robot.dart'; |
| 10 | import '../robots/send_page_robot.dart'; |
| 11 | import 'package:cake_wallet/.secrets.g.dart' as secrets; |
| 12 | |
| 13 | void main() { |
| 14 | IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 15 | |
| 16 | SendPageRobot sendPageRobot; |
| 17 | CommonTestFlows commonTestFlows; |
| 18 | DashboardPageRobot dashboardPageRobot; |
| 19 | CommonTestCases commonTestCases; |
| 20 | |
| 21 | testWidgets('Send flow with wallet switching for insufficient balance - testing \$1 send', |
| 22 | (tester) async { |
| 23 | FlutterError.onError = (FlutterErrorDetails details) { |
| 24 | debugPrint('FlutterError caught: ${details.exception}'); |
| 25 | }; |
| 26 | |
| 27 | commonTestFlows = CommonTestFlows(tester); |
| 28 | sendPageRobot = SendPageRobot(tester: tester); |
| 29 | dashboardPageRobot = DashboardPageRobot(tester); |
| 30 | commonTestCases = CommonTestCases(tester); |
| 31 | |
| 32 | await commonTestFlows.startAppFlow(ValueKey('send_test_app_key')); |
| 33 | |
| 34 | // Various wallet options we want to use to test the send flow |
| 35 | final walletConfigs = [ |
| 36 | { |
| 37 | 'type': WalletType.solana, |
| 38 | 'seed': secrets.solanaTestWalletSeeds2, |
| 39 | 'name': 'Solana Wallet 1', |
| 40 | }, |
| 41 | { |
| 42 | 'type': WalletType.solana, |
| 43 | 'seed': secrets.solanaTestWalletSeeds, |
| 44 | 'name': 'Solana Wallet 2', |
| 45 | }, |
| 46 | { |
| 47 | 'type': WalletType.ethereum, |
| 48 | 'seed': secrets.ethereumTestWalletSeeds, |
| 49 | 'name': 'Ethereum Wallet', |
| 50 | }, |
| 51 | ]; |
| 52 | |
| 53 | bool hasSufficientBalance = false; |
| 54 | WalletType? successfulWalletType; |
| 55 | |
| 56 | // We try each wallet configuration until we find one with sufficient balance |
| 57 | for (int i = 0; i < walletConfigs.length; i++) { |
| 58 | final config = walletConfigs[i]; |
| 59 | |
| 60 | if ((config['seed'] as String).isEmpty) { |
| 61 | tester.printToConsole('Skipping ${config['name']}, seed is empty'); |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | tester.printToConsole('Trying ${config['name']}'); |
| 66 | |
| 67 | // For first wallet, we restore wallet afresh, for subsequent ones, we do a wallet switch |
| 68 | if (i == 0) { |
| 69 | await commonTestFlows.welcomePageToRestoreWalletThroughSeedsFlow( |
| 70 | config['type'] as WalletType, |
| 71 | config['seed'] as String, |
| 72 | CommonTestConstants.pin, |
| 73 | ); |
| 74 | } else { |
| 75 | await commonTestCases.goBack(); |
| 76 | await commonTestCases.defaultSleepTime(); |
| 77 | |
| 78 | await dashboardPageRobot.navigateToWalletsListPage(); |
| 79 | await commonTestCases.defaultSleepTime(); |
| 80 | |
| 81 | await commonTestFlows.restoreWalletFromWalletMenu( |
| 82 | config['type'] as WalletType, |
| 83 | config['seed'] as String, |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | await dashboardPageRobot.confirmWalletTypeIsDisplayedCorrectly(config['type'] as WalletType); |
| 88 | |
| 89 | // Navigate to send page |
| 90 | await dashboardPageRobot.navigateToSendPage(); |
| 91 | await sendPageRobot.checkIfSendPageIsVisible(); |
| 92 | |
| 93 | await sendPageRobot.enterReceiveAddress(CommonTestConstants.testWalletAddress); |
| 94 | await sendPageRobot.selectReceiveCurrency(CommonTestConstants.sendTestReceiveCurrency); |
| 95 | |
| 96 | await sendPageRobot.selectTransactionPriority(); |
| 97 | |
| 98 | // Main check to see if this wallet has sufficient balance for $1 send |
| 99 | final hasBalance = await sendPageRobot.validateWalletBalanceForOneDollarSend(); |
| 100 | if (hasBalance) { |
| 101 | hasSufficientBalance = true; |
| 102 | successfulWalletType = config['type'] as WalletType; |
| 103 | tester.printToConsole( |
| 104 | '${config['name']} has sufficient balance for \$${CommonTestConstants.sendTestFiatAmount} send, proceeding with send', |
| 105 | ); |
| 106 | break; |
| 107 | } else { |
| 108 | tester.printToConsole( |
| 109 | '${config['name']} has insufficient balance for \$${CommonTestConstants.sendTestFiatAmount} send', |
| 110 | ); |
| 111 | if (i < walletConfigs.length - 1) { |
| 112 | tester.printToConsole('Switching to next wallet...'); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // We only proceed with the send if we have sufficient balance |
| 118 | if (hasSufficientBalance && successfulWalletType != null) { |
| 119 | tester.printToConsole('Performing send transaction with $successfulWalletType wallet'); |
| 120 | |
| 121 | await sendPageRobot.testFiatAmountEntry(); |
| 122 | await sendPageRobot.testCryptoAmountEntry(); |
| 123 | |
| 124 | await sendPageRobot.onSendButtonPressed(); |
| 125 | await sendPageRobot.onSendSliderOnConfirmSendingBottomSheetDragged(); |
| 126 | await sendPageRobot.handleTransactionSuccessFlow(); |
| 127 | } else { |
| 128 | tester.printToConsole( |
| 129 | 'Test completed without sending - all available wallets have insufficient balance for \$${CommonTestConstants.sendTestFiatAmount} send', |
| 130 | ); |
| 131 | } |
| 132 | }); |
| 133 | } |