| 1 | import 'dart:async'; |
| 2 | |
| 3 | import 'package:cake_wallet/core/execution_state.dart'; |
| 4 | import 'package:cake_wallet/generated/i18n.dart'; |
| 5 | import 'package:cake_wallet/src/screens/exchange_trade/exchange_trade_page.dart'; |
| 6 | import 'package:flutter/foundation.dart'; |
| 7 | import 'package:flutter_test/flutter_test.dart'; |
| 8 | |
| 9 | import '../components/common_test_cases.dart'; |
| 10 | |
| 11 | class ExchangeTradePageRobot { |
| 12 | ExchangeTradePageRobot(this.tester) : commonTestCases = CommonTestCases(tester); |
| 13 | |
| 14 | final WidgetTester tester; |
| 15 | late CommonTestCases commonTestCases; |
| 16 | |
| 17 | Future<void> isExchangeTradePage() async { |
| 18 | await commonTestCases.isSpecificPage<ExchangeTradePage>(); |
| 19 | await commonTestCases.takeScreenshots('exchange_trade_page'); |
| 20 | } |
| 21 | |
| 22 | void hasInformationDialog() { |
| 23 | commonTestCases.hasValueKey('information_page_dialog_key'); |
| 24 | } |
| 25 | |
| 26 | Future<void> onGotItButtonPressed() async { |
| 27 | await commonTestCases.tapItemByKey('information_page_got_it_button_key'); |
| 28 | await commonTestCases.defaultSleepTime(); |
| 29 | } |
| 30 | |
| 31 | Future<void> onSendFromExternalButtonPressed() async { |
| 32 | tester.printToConsole('Routing to send from external details page'); |
| 33 | |
| 34 | await commonTestCases.tapItemByKey( |
| 35 | 'exchange_trade_page_send_from_external_button_key', |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | Future<void> onSendFromCakeButtonPressed() async { |
| 40 | tester.printToConsole('Now sending from cake'); |
| 41 | |
| 42 | await commonTestCases.tapItemByKey( |
| 43 | 'exchange_trade_page_send_from_cake_button_key', |
| 44 | shouldPumpAndSettle: false, |
| 45 | ); |
| 46 | |
| 47 | final Completer<void> completer = Completer<void>(); |
| 48 | |
| 49 | // Loop to wait for the async operation to complete |
| 50 | while (true) { |
| 51 | await Future.delayed(Duration(seconds: 1)); |
| 52 | |
| 53 | final ExchangeTradeState state = tester.state(find.byType(ExchangeTradeForm)); |
| 54 | final execState = state.widget.exchangeTradeViewModel.sendViewModel.state; |
| 55 | |
| 56 | bool isDone = execState is ExecutedSuccessfullyState; |
| 57 | bool isFailed = execState is FailureState; |
| 58 | |
| 59 | tester.printToConsole('isDone: $isDone'); |
| 60 | tester.printToConsole('isFailed: $isFailed'); |
| 61 | |
| 62 | if (isDone || isFailed) { |
| 63 | tester.printToConsole( |
| 64 | isDone ? 'Completer is done' : 'Completer is done though operation failed'); |
| 65 | completer.complete(); |
| 66 | await tester.pump(); |
| 67 | break; |
| 68 | } else { |
| 69 | tester.printToConsole('Completer is not done'); |
| 70 | await tester.pump(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | await expectLater(completer.future, completes); |
| 75 | |
| 76 | tester.printToConsole('Done confirming sending'); |
| 77 | |
| 78 | await commonTestCases.defaultSleepTime(seconds: 4); |
| 79 | } |
| 80 | |
| 81 | Future<void> onSendButtonOnConfirmSendingDialogPressed() async { |
| 82 | tester.printToConsole('Send Button on Confirm Dialog Triggered'); |
| 83 | await commonTestCases.defaultSleepTime(seconds: 4); |
| 84 | |
| 85 | final sendText = find.text(S.current.send); |
| 86 | bool hasText = sendText.tryEvaluate(); |
| 87 | |
| 88 | if (hasText) { |
| 89 | await commonTestCases.tapItemByFinder(sendText); |
| 90 | |
| 91 | await commonTestCases.defaultSleepTime(seconds: 4); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Future<void> onCancelButtonOnConfirmSendingDialogPressed() async { |
| 96 | tester.printToConsole('Cancel Button on Confirm Dialog Triggered'); |
| 97 | |
| 98 | await commonTestCases.tapItemByKey( |
| 99 | 'exchange_trade_page_confirm_sending_dialog_cancel_button_key', |
| 100 | ); |
| 101 | |
| 102 | await commonTestCases.defaultSleepTime(); |
| 103 | } |
| 104 | |
| 105 | Future<void> onSendFailureDialogButtonPressed() async { |
| 106 | await commonTestCases.defaultSleepTime(seconds: 6); |
| 107 | |
| 108 | tester.printToConsole('Send Button Failure Dialog Triggered'); |
| 109 | |
| 110 | await commonTestCases.tapItemByKey('exchange_trade_page_send_failure_dialog_button_key'); |
| 111 | } |
| 112 | |
| 113 | Future<bool> hasErrorWhileSending() async { |
| 114 | await tester.pump(); |
| 115 | |
| 116 | tester.printToConsole('Checking if there is an error'); |
| 117 | |
| 118 | final errorDialog = find.byKey( |
| 119 | ValueKey('exchange_trade_page_send_failure_dialog_button_key'), |
| 120 | ); |
| 121 | |
| 122 | bool hasError = errorDialog.tryEvaluate(); |
| 123 | |
| 124 | tester.printToConsole('Has error: $hasError'); |
| 125 | |
| 126 | return hasError; |
| 127 | } |
| 128 | |
| 129 | Future<void> handleConfirmSendResult() async { |
| 130 | bool hasError = false; |
| 131 | |
| 132 | hasError = await hasErrorWhileSending(); |
| 133 | |
| 134 | int maxRetries = 20; |
| 135 | int retries = 0; |
| 136 | |
| 137 | while (hasError && retries < maxRetries) { |
| 138 | tester.printToConsole('hasErrorInLoop: $hasError'); |
| 139 | await tester.pump(); |
| 140 | |
| 141 | await onSendFailureDialogButtonPressed(); |
| 142 | tester.printToConsole('Failure button tapped'); |
| 143 | |
| 144 | await commonTestCases.defaultSleepTime(); |
| 145 | |
| 146 | await onSendFromCakeButtonPressed(); |
| 147 | tester.printToConsole('Confirm sending button tapped'); |
| 148 | |
| 149 | hasError = await hasErrorWhileSending(); |
| 150 | |
| 151 | retries++; |
| 152 | } |
| 153 | |
| 154 | if (!hasError) { |
| 155 | tester.printToConsole('No error, proceeding with flow'); |
| 156 | await tester.pump(); |
| 157 | } |
| 158 | |
| 159 | await commonTestCases.defaultSleepTime(); |
| 160 | } |
| 161 | } |