| 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/send/send_page.dart'; |
| 6 | import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; |
| 7 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 8 | import 'package:cake_wallet/view_model/send/send_view_model_state.dart'; |
| 9 | import 'package:cw_core/crypto_currency.dart'; |
| 10 | import 'package:cw_core/transaction_priority.dart'; |
| 11 | import 'package:flutter/material.dart'; |
| 12 | import 'package:flutter_test/flutter_test.dart'; |
| 13 | |
| 14 | import '../components/common_test_cases.dart'; |
| 15 | import '../components/common_test_constants.dart'; |
| 16 | import 'auth_page_robot.dart'; |
| 17 | import 'package:cake_wallet/src/widgets/standard_slide_button_widget.dart'; |
| 18 | import 'package:cake_wallet/src/screens/dashboard/dashboard_page.dart'; |
| 19 | |
| 20 | class SendPageRobot { |
| 21 | SendPageRobot({required this.tester}) |
| 22 | : commonTestCases = CommonTestCases(tester), |
| 23 | authPageRobot = AuthPageRobot(tester); |
| 24 | |
| 25 | WidgetTester tester; |
| 26 | CommonTestCases commonTestCases; |
| 27 | AuthPageRobot authPageRobot; |
| 28 | |
| 29 | Future<void> isSendPage() async { |
| 30 | await commonTestCases.isSpecificPage<SendPage>(); |
| 31 | await commonTestCases.takeScreenshots('send_page'); |
| 32 | } |
| 33 | |
| 34 | Future<void> waitForSendPage() async { |
| 35 | tester.printToConsole('Waiting for SendPage to be available'); |
| 36 | |
| 37 | final stopwatch = Stopwatch()..start(); |
| 38 | final maxDuration = Duration(seconds: 30); |
| 39 | |
| 40 | while (stopwatch.elapsed < maxDuration) { |
| 41 | await tester.pump(Duration(milliseconds: 500)); |
| 42 | |
| 43 | // Check if we're still on auth page |
| 44 | if (authPageRobot.onAuthPage()) { |
| 45 | tester.printToConsole('Still on auth page, waiting...'); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | // Check if SendPage is available |
| 50 | final sendPageFinder = find.byType(SendPage); |
| 51 | if (sendPageFinder.tryEvaluate()) { |
| 52 | tester.printToConsole('SendPage found!'); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | tester.printToConsole('SendPage not found yet, waiting...'); |
| 57 | } |
| 58 | |
| 59 | throw Exception('SendPage not found after ${maxDuration.inSeconds} seconds'); |
| 60 | } |
| 61 | |
| 62 | Future<void> checkIfSendPageIsVisible() async { |
| 63 | tester.printToConsole('Confirming SendPage is visible'); |
| 64 | |
| 65 | if (authPageRobot.onAuthPage()) { |
| 66 | tester.printToConsole('Auth page is currently visible'); |
| 67 | await _handleAuthPage(); |
| 68 | await commonTestCases.defaultSleepTime(); |
| 69 | } |
| 70 | |
| 71 | await waitForSendPage(); |
| 72 | } |
| 73 | |
| 74 | void hasTitle() { |
| 75 | commonTestCases.hasText(S.current.send); |
| 76 | } |
| 77 | |
| 78 | void confirmViewComponentsDisplayProperly() { |
| 79 | SendPage sendPage = tester.widget(find.byType(SendPage)); |
| 80 | final sendViewModel = sendPage.sendViewModel; |
| 81 | |
| 82 | commonTestCases.hasValueKey('send_page_address_textfield_key'); |
| 83 | commonTestCases.hasValueKey('send_page_note_textfield_key'); |
| 84 | commonTestCases.hasValueKey('send_page_amount_textfield_key'); |
| 85 | commonTestCases.hasValueKey('send_page_add_template_button_key'); |
| 86 | |
| 87 | if (sendViewModel.hasMultipleTokens) { |
| 88 | commonTestCases.hasValueKey('send_page_currency_picker_button_key'); |
| 89 | } |
| 90 | |
| 91 | if (!sendViewModel.isBatchSending) { |
| 92 | commonTestCases.hasValueKey('send_page_send_all_button_key'); |
| 93 | } |
| 94 | |
| 95 | if (!sendViewModel.isFiatDisabled) { |
| 96 | commonTestCases.hasValueKey('send_page_fiat_amount_textfield_key'); |
| 97 | } |
| 98 | |
| 99 | if (sendViewModel.feesViewModel.hasFees) { |
| 100 | commonTestCases.hasValueKey('send_page_select_fee_priority_button_key'); |
| 101 | } |
| 102 | |
| 103 | if (sendViewModel.hasCoinControl) { |
| 104 | commonTestCases.hasValueKey('send_page_unspent_coin_button_key'); |
| 105 | } |
| 106 | |
| 107 | if (sendViewModel.sendTemplateViewModel.hasMultiRecipient) { |
| 108 | commonTestCases.hasValueKey('send_page_add_receiver_button_key'); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | Future<void> selectReceiveCurrency(CryptoCurrency receiveCurrency) async { |
| 113 | final currencyPickerKey = 'send_page_currency_picker_button_key'; |
| 114 | final currencyPickerDialogKey = 'send_page_currency_picker_dialog_button_key'; |
| 115 | |
| 116 | await commonTestCases.tapItemByKey(currencyPickerKey); |
| 117 | await commonTestCases.defaultSleepTime(); |
| 118 | |
| 119 | // Check if picker dialog is present |
| 120 | if (!commonTestCases.isKeyPresent(currencyPickerDialogKey)) { |
| 121 | tester.printToConsole('Currency picker dialog not found, may already be selected'); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | SendPage sendPage = tester.widget(find.byType(SendPage)); |
| 126 | final sendViewModel = sendPage.sendViewModel; |
| 127 | |
| 128 | if (receiveCurrency == sendViewModel.selectedCryptoCurrency) { |
| 129 | await commonTestCases |
| 130 | .tapItemByKey('picker_items_index_${receiveCurrency.name}_selected_item_button_key'); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | await commonTestCases.enterText(receiveCurrency.title, 'search_bar_widget_key'); |
| 135 | |
| 136 | await commonTestCases.defaultSleepTime(); |
| 137 | |
| 138 | await commonTestCases.tapItemByKey('picker_items_index_${receiveCurrency.fullName}_button_key'); |
| 139 | } |
| 140 | |
| 141 | Future<void> enterReceiveAddress(String receiveAddress) async { |
| 142 | await commonTestCases.enterText(receiveAddress, 'send_page_address_textfield_key'); |
| 143 | await commonTestCases.defaultSleepTime(); |
| 144 | } |
| 145 | |
| 146 | Future<void> enterSendAmount(String amount, {bool isFiat = false}) async { |
| 147 | await commonTestCases.enterText( |
| 148 | amount, |
| 149 | isFiat ? 'send_page_fiat_amount_textfield_key' : 'send_page_amount_textfield_key', |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | String _getTextFromField(ValueKey<String> key) { |
| 154 | final field = find.byKey(key); |
| 155 | if (!field.tryEvaluate()) return ''; |
| 156 | final baseTextFormField = tester.widget<BaseTextFormField>(field); |
| 157 | return baseTextFormField.controller?.text ?? ''; |
| 158 | } |
| 159 | |
| 160 | /// Validates wallet balance for $1 send by checking against the converted crypto amount |
| 161 | /// Returns true if wallet has sufficient balance, false otherwise |
| 162 | Future<bool> validateWalletBalanceForOneDollarSend() async { |
| 163 | SendPage sendPage = tester.widget(find.byType(SendPage)); |
| 164 | final sendViewModel = sendPage.sendViewModel; |
| 165 | |
| 166 | final balance = await sendViewModel.sendingBalance; |
| 167 | |
| 168 | await setupOneDollarSend(); |
| 169 | |
| 170 | // Get the crypto amount that was set for the $1 send |
| 171 | String cryptoAmount = _getTextFromField(ValueKey('send_page_amount_textfield_key')); |
| 172 | if (cryptoAmount.isEmpty || cryptoAmount == '0' || cryptoAmount == '0.0') { |
| 173 | cryptoAmount = '0.001'; // fallback amount |
| 174 | } |
| 175 | |
| 176 | final amount = double.tryParse(cryptoAmount) ?? 0.0; |
| 177 | |
| 178 | tester.printToConsole( |
| 179 | 'Wallet balance: $balance, sending amount for \$${CommonTestConstants.sendTestFiatAmount}: $cryptoAmount', |
| 180 | ); |
| 181 | |
| 182 | if (balance.isEmpty || double.tryParse(balance) == null) { |
| 183 | tester.printToConsole('Invalid wallet balance: $balance'); |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | final balanceValue = double.parse(balance); |
| 188 | if (balanceValue < amount) { |
| 189 | tester.printToConsole( |
| 190 | 'Insufficient balance for \$${CommonTestConstants.sendTestFiatAmount} send: $balanceValue < $amount', |
| 191 | ); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | tester.printToConsole( |
| 196 | 'Wallet has sufficient balance for \$${CommonTestConstants.sendTestFiatAmount} send', |
| 197 | ); |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | Future<void> selectTransactionPriority({TransactionPriority? priority}) async { |
| 202 | SendPage sendPage = tester.widget(find.byType(SendPage)); |
| 203 | final sendViewModel = sendPage.sendViewModel; |
| 204 | |
| 205 | if (!sendViewModel.feesViewModel.hasFees || priority == null) return; |
| 206 | |
| 207 | final transactionPriorityPickerKey = 'send_page_select_fee_priority_button_key'; |
| 208 | await commonTestCases.tapItemByKey(transactionPriorityPickerKey); |
| 209 | |
| 210 | if (priority == sendViewModel.feesViewModel.transactionPriority) { |
| 211 | await commonTestCases |
| 212 | .tapItemByKey('picker_items_index_${priority.title}_selected_item_button_key'); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | await commonTestCases.dragUntilVisible( |
| 217 | 'picker_items_index_${priority.title}_button_key', |
| 218 | 'picker_scrollbar_key', |
| 219 | ); |
| 220 | await commonTestCases.defaultSleepTime(); |
| 221 | |
| 222 | await commonTestCases.tapItemByKey('picker_items_index_${priority.title}_button_key'); |
| 223 | } |
| 224 | |
| 225 | Future<void> onSendButtonPressed() async { |
| 226 | tester.printToConsole('Pressing send'); |
| 227 | |
| 228 | await checkIfSendPageIsVisible(); |
| 229 | |
| 230 | await tester.pumpAndSettle(); |
| 231 | final sendPage = tester.widget<SendPage>(find.byType(SendPage)); |
| 232 | |
| 233 | while (true) { |
| 234 | bool isReadyForSend = sendPage.sendViewModel.isReadyForSend; |
| 235 | await tester.pump(); |
| 236 | if (isReadyForSend) { |
| 237 | tester.printToConsole('Is ready for send'); |
| 238 | break; |
| 239 | } else { |
| 240 | await commonTestCases.defaultSleepTime(); |
| 241 | await tester.pumpAndSettle(); |
| 242 | tester.printToConsole('not yet ready for send'); |
| 243 | } |
| 244 | } |
| 245 | await commonTestCases.tapItemByKey( |
| 246 | 'send_page_send_button_key', |
| 247 | shouldPumpAndSettle: false, |
| 248 | ); |
| 249 | |
| 250 | await _waitForSendTransactionCompletion(); |
| 251 | |
| 252 | await commonTestCases.defaultSleepTime(); |
| 253 | } |
| 254 | |
| 255 | Future<void> _waitForSendTransactionCompletion() async { |
| 256 | await tester.pump(); |
| 257 | final Completer<void> completer = Completer<void>(); |
| 258 | |
| 259 | // Loop to wait for the async operation to complete |
| 260 | while (true) { |
| 261 | await Future.delayed(Duration(seconds: 1)); |
| 262 | |
| 263 | tester.printToConsole('Before _handleAuth'); |
| 264 | |
| 265 | await _handleAuthPage(); |
| 266 | |
| 267 | await commonTestCases.defaultSleepTime(); |
| 268 | |
| 269 | tester.printToConsole('After _handleAuth'); |
| 270 | |
| 271 | await tester.pump(); |
| 272 | |
| 273 | if (authPageRobot.onAuthPage()) { |
| 274 | tester.printToConsole('Still on auth page, continuing to wait'); |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | final sendPageFinder = find.byType(SendPage); |
| 279 | if (!sendPageFinder.tryEvaluate()) { |
| 280 | tester.printToConsole('SendPage not found yet, continuing to wait'); |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | final sendPage = tester.widget<SendPage>(sendPageFinder); |
| 285 | final state = sendPage.sendViewModel.state; |
| 286 | |
| 287 | await tester.pump(); |
| 288 | |
| 289 | bool isDone = state is ExecutedSuccessfullyState || state is TransactionCommitted; |
| 290 | bool isFailed = state is FailureState; |
| 291 | |
| 292 | tester.printToConsole('isDone: $isDone'); |
| 293 | tester.printToConsole('isFailed: $isFailed'); |
| 294 | |
| 295 | if (isDone || isFailed) { |
| 296 | tester.printToConsole( |
| 297 | isDone ? 'Completer is done' : 'Completer is done though operation failed', |
| 298 | ); |
| 299 | completer.complete(); |
| 300 | await tester.pump(); |
| 301 | break; |
| 302 | } else { |
| 303 | tester.printToConsole('Completer is not done'); |
| 304 | await tester.pump(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | await expectLater(completer.future, completes); |
| 309 | |
| 310 | tester.printToConsole('Done confirming sending operation'); |
| 311 | } |
| 312 | |
| 313 | Future<void> _handleAuthPage() async { |
| 314 | tester.printToConsole('Inside _handleAuth'); |
| 315 | |
| 316 | final onAuthPageDesktop = authPageRobot.onAuthPageDesktop(); |
| 317 | if (onAuthPageDesktop) { |
| 318 | await authPageRobot.enterPassword(CommonTestConstants.pin.join("")); |
| 319 | await commonTestCases.defaultSleepTime(); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | await tester.pump(); |
| 324 | tester.printToConsole('starting auth checks'); |
| 325 | |
| 326 | final authPage = authPageRobot.onAuthPage(); |
| 327 | |
| 328 | tester.printToConsole('hasAuth:$authPage'); |
| 329 | |
| 330 | if (authPage) { |
| 331 | await tester.pump(); |
| 332 | tester.printToConsole('Starting inner _handleAuth loop checks'); |
| 333 | |
| 334 | try { |
| 335 | await authPageRobot.enterPinCode(CommonTestConstants.pin, pumpDuration: 500); |
| 336 | tester.printToConsole('Auth done'); |
| 337 | |
| 338 | await tester.pump(Duration(seconds: 3)); |
| 339 | |
| 340 | tester.printToConsole('Auth pump done'); |
| 341 | |
| 342 | await commonTestCases.defaultSleepTime(); |
| 343 | } catch (e) { |
| 344 | tester.printToConsole('Auth failed, retrying: $e'); |
| 345 | await tester.pump(); |
| 346 | |
| 347 | await commonTestCases.defaultSleepTime(seconds: 1); |
| 348 | await _handleAuthPage(); |
| 349 | } |
| 350 | } else { |
| 351 | tester.printToConsole('No auth page detected, proceeding'); |
| 352 | } |
| 353 | await tester.pump(); |
| 354 | } |
| 355 | |
| 356 | //* ------ On Sending Failure ------------ |
| 357 | Future<bool> hasErrorWhileSending() async { |
| 358 | await tester.pump(); |
| 359 | |
| 360 | tester.printToConsole('Checking if there is an error'); |
| 361 | |
| 362 | final errorDialog = find.byKey(ValueKey('send_page_send_failure_dialog_button_key')); |
| 363 | |
| 364 | bool hasError = errorDialog.tryEvaluate(); |
| 365 | |
| 366 | tester.printToConsole('Has error: $hasError'); |
| 367 | |
| 368 | return hasError; |
| 369 | } |
| 370 | |
| 371 | Future<void> onSendFailureDialogButtonPressed() async { |
| 372 | await commonTestCases.defaultSleepTime(); |
| 373 | |
| 374 | tester.printToConsole('Send Button Failure Dialog Triggered'); |
| 375 | |
| 376 | await commonTestCases.tapItemByKey('send_page_send_failure_dialog_button_key'); |
| 377 | } |
| 378 | |
| 379 | //* ------ On Sending Success ------------ |
| 380 | Future<void> onSendSliderOnConfirmSendingBottomSheetDragged() async { |
| 381 | await commonTestCases.defaultSleepTime(); |
| 382 | await tester.pump(); |
| 383 | |
| 384 | if (commonTestCases.isKeyPresent('send_page_confirm_sending_bottom_sheet_key')) { |
| 385 | tester.printToConsole('Found confirm sending bottom sheet, starting slider drag'); |
| 386 | |
| 387 | final accessibleButton = find.byType(PrimaryButton); |
| 388 | if (accessibleButton.tryEvaluate()) { |
| 389 | tester.printToConsole('Found accessible navigation button, tapping it'); |
| 390 | await tester.tap(accessibleButton); |
| 391 | await tester.pumpAndSettle(); |
| 392 | } else { |
| 393 | await _performSendSliderDrag(); |
| 394 | } |
| 395 | |
| 396 | tester.printToConsole('Slider/button action completed, waiting for transaction completion'); |
| 397 | |
| 398 | // Check if bottom sheet is dismissed |
| 399 | if (commonTestCases.isKeyPresent('send_page_confirm_sending_bottom_sheet_key')) { |
| 400 | tester.printToConsole('Bottom sheet still present, waiting a bit more for dismissal'); |
| 401 | await commonTestCases.defaultSleepTime(seconds: 3); |
| 402 | |
| 403 | // If still present, try one more manual dismissal attempt |
| 404 | if (commonTestCases.isKeyPresent('send_page_confirm_sending_bottom_sheet_key')) { |
| 405 | tester.printToConsole('Bottom sheet still present, attempting final manual dismissal'); |
| 406 | await tester.tapAt(Offset(200, 200)); // Tap outside |
| 407 | await tester.pumpAndSettle(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Wait for transaction completion |
| 412 | await _waitForCommitTransactionCompletion(); |
| 413 | await commonTestCases.defaultSleepTime(seconds: 2); |
| 414 | } else { |
| 415 | tester.printToConsole('Confirm sending bottom sheet not found, waiting and retrying'); |
| 416 | await commonTestCases.defaultSleepTime(); |
| 417 | await tester.pump(); |
| 418 | await onSendSliderOnConfirmSendingBottomSheetDragged(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | Future<void> _performSendSliderDrag() async { |
| 423 | final sliderFinder = find.byKey(const ValueKey('standard_slide_button_widget_slider_key')); |
| 424 | expect(sliderFinder, findsOneWidget, reason: 'Slider should be found'); |
| 425 | |
| 426 | // Find the StandardSlideButton widget to get the main container |
| 427 | final slideButtonFinder = find.byType(StandardSlideButton); |
| 428 | expect(slideButtonFinder, findsOneWidget, reason: 'StandardSlideButton should be found'); |
| 429 | |
| 430 | // Get the main container bounds (the outer container, not the slider container) |
| 431 | final mainContainerRect = tester.getRect(slideButtonFinder); |
| 432 | final containerWidth = mainContainerRect.width; |
| 433 | |
| 434 | final sideMargin = 4.0; |
| 435 | final sliderWidth = 42.0; |
| 436 | final effectiveMaxWidth = containerWidth - 2 * sideMargin; |
| 437 | final threshold = effectiveMaxWidth - sliderWidth - 10; |
| 438 | |
| 439 | // Add a small buffer to ensure we exceed the threshold |
| 440 | final dragDistance = threshold + 20; |
| 441 | |
| 442 | tester.printToConsole( |
| 443 | 'Main container width: $containerWidth, Threshold: $threshold, Drag distance: $dragDistance', |
| 444 | ); |
| 445 | |
| 446 | // Start the drag |
| 447 | await tester.drag(sliderFinder, Offset(dragDistance, 0)); |
| 448 | |
| 449 | // Wait for the drag to complete and trigger onHorizontalDragEnd |
| 450 | await tester.pump(Duration(milliseconds: 100)); |
| 451 | |
| 452 | // Release the drag (this should trigger onHorizontalDragEnd) |
| 453 | await tester.pump(Duration(seconds: 2)); |
| 454 | |
| 455 | tester.printToConsole('Drag completed, waiting for callback'); |
| 456 | |
| 457 | // Wait for the slide completion callback to trigger and bottom sheet to dismiss |
| 458 | await _waitForBottomSheetDismissal(); |
| 459 | } |
| 460 | |
| 461 | Future<void> _waitForBottomSheetDismissal() async { |
| 462 | tester.printToConsole('Waiting for bottom sheet dismissal'); |
| 463 | |
| 464 | final stopwatch = Stopwatch()..start(); |
| 465 | final maxDuration = Duration(seconds: 10); |
| 466 | |
| 467 | while (stopwatch.elapsed < maxDuration) { |
| 468 | await tester.pump(Duration(milliseconds: 500)); |
| 469 | |
| 470 | // Check if the confirm bottom sheet is still present |
| 471 | if (!commonTestCases.isKeyPresent('send_page_confirm_sending_bottom_sheet_key')) { |
| 472 | tester.printToConsole('Bottom sheet dismissed successfully!'); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | // Check if transaction has started (this indicates slider was successful) |
| 477 | if (_isTransactionStarted()) { |
| 478 | tester.printToConsole( |
| 479 | 'Transaction started, slider was successful even if bottom sheet is still visible'); |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | tester.printToConsole('Bottom sheet still present, waiting'); |
| 484 | } |
| 485 | |
| 486 | tester.printToConsole('Bottom sheet dismissal timeout reached, trying manual dismissal'); |
| 487 | |
| 488 | // If the bottom sheet is still present after timeout, try to manually dismiss it |
| 489 | // This might happen if the slider drag didn't trigger the onSlideComplete properly |
| 490 | if (commonTestCases.isKeyPresent('send_page_confirm_sending_bottom_sheet_key')) { |
| 491 | tester.printToConsole('Attempting manual bottom sheet dismissal'); |
| 492 | |
| 493 | // Try to find and tap a close button or back button |
| 494 | final closeButton = find.byIcon(Icons.close); |
| 495 | if (closeButton.tryEvaluate()) { |
| 496 | await tester.tap(closeButton); |
| 497 | await tester.pumpAndSettle(); |
| 498 | tester.printToConsole('Manual close button tapped'); |
| 499 | } else { |
| 500 | // Try to pop the bottom sheet by tapping outside or using back gesture |
| 501 | await tester.tapAt(Offset(100, 100)); // Tap outside the bottom sheet |
| 502 | await tester.pumpAndSettle(); |
| 503 | tester.printToConsole('Tapped outside bottom sheet'); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | bool _isTransactionStarted() { |
| 509 | try { |
| 510 | // Check if SendPage is available and transaction state has changed |
| 511 | final sendPageFinder = find.byType(SendPage); |
| 512 | if (!sendPageFinder.tryEvaluate()) { |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | final sendPage = tester.widget<SendPage>(sendPageFinder); |
| 517 | final state = sendPage.sendViewModel.state; |
| 518 | |
| 519 | // Check if we're in a transaction-related state |
| 520 | return state is TransactionCommitting || |
| 521 | state is IsExecutingState || |
| 522 | state is TransactionCommitted || |
| 523 | state is ExecutedSuccessfullyState; |
| 524 | } catch (e) { |
| 525 | return false; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | Future<void> _waitForCommitTransactionCompletion() async { |
| 530 | tester.printToConsole('Starting to wait for transaction completion'); |
| 531 | |
| 532 | final stopwatch = Stopwatch()..start(); |
| 533 | final maxDuration = Duration(seconds: 60); |
| 534 | |
| 535 | while (stopwatch.elapsed < maxDuration) { |
| 536 | await Future.delayed(Duration(seconds: 1)); |
| 537 | |
| 538 | // Check if the confirm bottom sheet is gone (transaction started) |
| 539 | if (!commonTestCases.isKeyPresent('send_page_confirm_sending_bottom_sheet_key')) { |
| 540 | tester.printToConsole('Confirm bottom sheet disappeared, transaction may be processing'); |
| 541 | } |
| 542 | |
| 543 | // Check if we've navigated back to the dashboard (for cases of successful transaction) |
| 544 | final dashboardFinder = find.byType(DashboardPage); |
| 545 | if (dashboardFinder.tryEvaluate()) { |
| 546 | tester.printToConsole('Dashboard detected, transaction completed successfully!'); |
| 547 | await tester.pump(); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | // Check if SendPage is still available (transaction still processing) |
| 552 | final sendPageFinder = find.byType(SendPage); |
| 553 | if (sendPageFinder.tryEvaluate()) { |
| 554 | final sendPage = tester.widget<SendPage>(sendPageFinder); |
| 555 | final state = sendPage.sendViewModel.state; |
| 556 | |
| 557 | bool isDone = state is ExecutedSuccessfullyState || state is TransactionCommitted; |
| 558 | bool isFailed = state is FailureState; |
| 559 | |
| 560 | tester.printToConsole('Transaction state: $state'); |
| 561 | tester.printToConsole('isDone: $isDone'); |
| 562 | tester.printToConsole('isFailed: $isFailed'); |
| 563 | |
| 564 | if (isDone) { |
| 565 | tester.printToConsole('Transaction committed successfully!'); |
| 566 | await tester.pump(); |
| 567 | return; |
| 568 | } else if (isFailed) { |
| 569 | tester.printToConsole('Transaction failed: $state'); |
| 570 | await tester.pump(); |
| 571 | return; |
| 572 | } else { |
| 573 | tester.printToConsole('Transaction still processing'); |
| 574 | await tester.pump(); |
| 575 | } |
| 576 | } else { |
| 577 | // SendPage not found, check if we're on dashboard or still processing |
| 578 | if (dashboardFinder.tryEvaluate()) { |
| 579 | tester.printToConsole('Dashboard detected - transaction completed successfully!'); |
| 580 | await tester.pump(); |
| 581 | return; |
| 582 | } else { |
| 583 | tester.printToConsole( |
| 584 | 'SendPage not found, but dashboard not yet visible - continuing to wait...'); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (stopwatch.elapsed >= maxDuration) { |
| 590 | tester.printToConsole('Transaction completion timeout reached'); |
| 591 | } |
| 592 | |
| 593 | tester.printToConsole('Done waiting for transaction completion'); |
| 594 | } |
| 595 | |
| 596 | //* ---- Handle Transaction Success Flow ----- |
| 597 | Future<void> handleTransactionSuccessFlow() async { |
| 598 | await commonTestCases.defaultSleepTime(); |
| 599 | |
| 600 | // Wait for any success dialogs to appear |
| 601 | await tester.pump(Duration(seconds: 2)); |
| 602 | |
| 603 | // Check for contact addition dialog first (if new contact address exists) |
| 604 | final contactDialog = find.byKey(ValueKey('send_page_add_contact_bottom_sheet_yes_button_key')); |
| 605 | if (contactDialog.tryEvaluate()) { |
| 606 | tester.printToConsole('Found contact addition dialog, selecting Yes'); |
| 607 | |
| 608 | // Check if the button is actually visible and tappable |
| 609 | final buttonRect = tester.getRect(contactDialog); |
| 610 | final screenSize = tester.view.physicalSize / tester.view.devicePixelRatio; |
| 611 | |
| 612 | if (buttonRect.bottom <= screenSize.height && buttonRect.top >= 0) { |
| 613 | await commonTestCases.tapItemByKey('send_page_add_contact_bottom_sheet_yes_button_key'); |
| 614 | await commonTestCases.defaultSleepTime(); |
| 615 | } else { |
| 616 | tester.printToConsole('Contact dialog button is off-screen, skipping'); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // Check for the main success dialog |
| 621 | final successDialog = find.byKey(ValueKey('send_page_sent_dialog_ok_button_key')); |
| 622 | if (successDialog.tryEvaluate()) { |
| 623 | tester.printToConsole('Found transaction success dialog, closing it'); |
| 624 | await commonTestCases.tapItemByKey('send_page_sent_dialog_ok_button_key'); |
| 625 | await commonTestCases.defaultSleepTime(); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | //* ---- Fiat/Crypto Amount Validation ----- |
| 630 | Future<void> testFiatAmountEntry() async { |
| 631 | tester.printToConsole('Testing fiat amount entry...'); |
| 632 | |
| 633 | await enterSendAmount(''); |
| 634 | await enterSendAmount('', isFiat: true); |
| 635 | await commonTestCases.defaultSleepTime(); |
| 636 | |
| 637 | await enterSendAmount(CommonTestConstants.sendTestFiatAmount, isFiat: true); |
| 638 | await commonTestCases.defaultSleepTime(); |
| 639 | |
| 640 | // Wait for conversion to complete |
| 641 | await tester.pump(Duration(seconds: 3)); |
| 642 | await tester.pumpAndSettle(); |
| 643 | |
| 644 | // Next we get the crypto amount value and validate it's not 0 |
| 645 | final cryptoAmount = _getTextFromField(ValueKey('send_page_amount_textfield_key')); |
| 646 | |
| 647 | tester.printToConsole( |
| 648 | 'Crypto amount after entering \$${CommonTestConstants.sendTestFiatAmount}: $cryptoAmount', |
| 649 | ); |
| 650 | |
| 651 | if (cryptoAmount.isNotEmpty && cryptoAmount != '0' && cryptoAmount != '0.0') { |
| 652 | tester.printToConsole('Fiat to crypto conversion working - crypto amount: $cryptoAmount'); |
| 653 | } else { |
| 654 | tester.printToConsole( |
| 655 | 'Fiat to crypto conversion may not be working - crypto amount: $cryptoAmount', |
| 656 | ); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | Future<void> testCryptoAmountEntry() async { |
| 661 | tester.printToConsole('Testing crypto amount entry...'); |
| 662 | |
| 663 | await enterSendAmount(''); |
| 664 | await enterSendAmount('', isFiat: true); |
| 665 | await commonTestCases.defaultSleepTime(); |
| 666 | |
| 667 | String cryptoAmount = '0.001'; |
| 668 | |
| 669 | await enterSendAmount(cryptoAmount); |
| 670 | await commonTestCases.defaultSleepTime(); |
| 671 | |
| 672 | // Wait for conversion to complete |
| 673 | await tester.pump(Duration(seconds: 3)); |
| 674 | await tester.pumpAndSettle(); |
| 675 | |
| 676 | // Get the fiat amount value and validate it's not 0 |
| 677 | final fiatAmount = _getTextFromField(ValueKey('send_page_fiat_amount_textfield_key')); |
| 678 | |
| 679 | tester.printToConsole('Fiat amount after entering $cryptoAmount: $fiatAmount'); |
| 680 | |
| 681 | if (fiatAmount.isNotEmpty && fiatAmount != '0' && fiatAmount != '0.0') { |
| 682 | tester.printToConsole('Crypto to fiat conversion working, fiat amount: $fiatAmount'); |
| 683 | } else { |
| 684 | tester.printToConsole( |
| 685 | 'Crypto to fiat conversion may not be working - fiat amount: $fiatAmount'); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | Future<void> setupOneDollarSend() async { |
| 690 | // Clear existing amounts |
| 691 | await enterSendAmount(''); |
| 692 | await enterSendAmount('', isFiat: true); |
| 693 | await commonTestCases.defaultSleepTime(); |
| 694 | |
| 695 | await enterSendAmount(CommonTestConstants.sendTestFiatAmount, isFiat: true); |
| 696 | await commonTestCases.defaultSleepTime(); |
| 697 | |
| 698 | // Wait for conversion to complete |
| 699 | await tester.pump(Duration(seconds: 3)); |
| 700 | await tester.pumpAndSettle(); |
| 701 | |
| 702 | // Get the converted crypto amount |
| 703 | String cryptoAmount = _getTextFromField(ValueKey('send_page_amount_textfield_key')); |
| 704 | if (cryptoAmount.isEmpty || cryptoAmount == '0' || cryptoAmount == '0.0') { |
| 705 | cryptoAmount = '0.001'; // fallback amount |
| 706 | } |
| 707 | |
| 708 | tester.printToConsole( |
| 709 | 'Sending $cryptoAmount (equivalent to \$${CommonTestConstants.sendTestFiatAmount}) to test wallet', |
| 710 | ); |
| 711 | |
| 712 | // Update the amount field with the converted value |
| 713 | await enterSendAmount(cryptoAmount); |
| 714 | await commonTestCases.defaultSleepTime(); |
| 715 | } |
| 716 | } |