From ad9ee382d3eb15f4f738a35c68f27c01c1602b63 Mon Sep 17 00:00:00 2001
From: poka <poka@p2p.legal>
Date: Thu, 1 Sep 2022 04:51:51 +0200
Subject: [PATCH] wip: test onboarding

---
 integration_test/gecko_complete.dart          |  2 +-
 integration_test/general_actions.dart         | 64 +++++++++++++++++++
 integration_test/launch_test.sh               |  2 +-
 integration_test/multi_chests.dart            | 17 +++++
 integration_test/tests_utility.dart           | 27 ++++++--
 lib/models/widgets_keys.dart                  |  1 +
 lib/screens/home.dart                         |  2 +-
 lib/screens/onBoarding/10.dart                |  2 +
 .../onBoarding/11_congratulations.dart        | 15 +++--
 lib/screens/onBoarding/5.dart                 |  4 +-
 lib/screens/onBoarding/6.dart                 |  1 +
 lib/screens/transaction_in_progress.dart      |  2 -
 12 files changed, 119 insertions(+), 20 deletions(-)
 create mode 100644 integration_test/multi_chests.dart

diff --git a/integration_test/gecko_complete.dart b/integration_test/gecko_complete.dart
index 7157f113..bcf1c236 100644
--- a/integration_test/gecko_complete.dart
+++ b/integration_test/gecko_complete.dart
@@ -134,6 +134,6 @@ Future certifyTest5() async {
 
   // Check UD reval
   await spawnBlock(until: 50);
-  await waitFor('509.35');
+  await waitFor('509.36');
   humanRead(5);
 }
diff --git a/integration_test/general_actions.dart b/integration_test/general_actions.dart
index cf8dfc1a..8a734cd4 100644
--- a/integration_test/general_actions.dart
+++ b/integration_test/general_actions.dart
@@ -3,6 +3,8 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
 import 'package:flutter_test/flutter_test.dart';
 import 'package:gecko/globals.dart';
 import 'package:gecko/models/widgets_keys.dart';
+import 'package:gecko/providers/generate_wallets.dart';
+import 'package:provider/provider.dart';
 import 'tests_utility.dart';
 
 // GENERAL ACTIONS
@@ -83,6 +85,68 @@ Future restoreChest() async {
   await goBack();
 }
 
+Future onboardingNewChest() async {
+  final generateWalletProvider =
+      Provider.of<GenerateWalletsProvider>(homeContext, listen: false);
+  // Open screen create new wallet
+  await tapKey(keyOnboardingNewChest);
+
+  // Tap on next button 4 times to skip 3 screen
+  await tapKey(keyGoNext);
+  await tapKey(keyGoNext);
+  await tapKey(keyGoNext);
+  await tapKey(keyGoNext);
+  await waitFor('7', exactMatch: true);
+
+  final word41 = getWidgetText(keyMnemonicWord('4'));
+
+  // Change 2 times mnemonic
+  await tapKey(keyGenerateMnemonic);
+  await tester.pumpAndSettle();
+  final word42 = getWidgetText(keyMnemonicWord('4'));
+  expect(word41, isNot(word42));
+  await tapKey(keyGenerateMnemonic, duration: 500);
+  await tester.pumpAndSettle();
+  final word43 = getWidgetText(keyMnemonicWord('4'));
+  expect(word42, isNot(word43));
+
+  // Go next screen
+  await tapKey(keyGoNext);
+  await tester.pumpAndSettle();
+
+  // Enter asked word
+  final askedWordNumber = int.parse(getWidgetText(keyAskedWord));
+  List mnemonic = generateWalletProvider.generatedMnemonic!.split(' ');
+
+  final askedWord = mnemonic[askedWordNumber - 1];
+  await enterText(keyInputWord, askedWord);
+  await waitFor('Continuer', exactMatch: true);
+  await tapKey(keyGoNext);
+  await tapKey(keyGoNext);
+  await tapKey(keyGoNext);
+  await waitFor('AAAAA', exactMatch: true);
+  await tapKey(keyGoNext);
+
+  // Check if cached password checkbox is checked
+  final isCached = await isIconPresent(Icons.check_box);
+
+  // If not, tap on to cache password
+  if (!isCached) await tapKey(keyCachePassword, duration: 0);
+
+  // Enter password
+  await enterText(keyPinForm, 'AAAAA', 0);
+
+  // Check if string "Accéder à mon coffre" is present in screen
+  await waitFor('Accéder à mon coffre');
+
+  // Go to wallets home
+  await tapKey(keyGoWalletsHome, duration: 0);
+
+  // Check if string "ĞD" is present in screen
+  await waitFor('Mon portefeuille co');
+  await waitFor('0.0 $currencyName');
+}
+
 Future addDerivation() async {
   await tapKey(keyAddDerivation);
   await waitFor('Portefeuille 5');
diff --git a/integration_test/launch_test.sh b/integration_test/launch_test.sh
index 9259b2fb..44e9c024 100755
--- a/integration_test/launch_test.sh
+++ b/integration_test/launch_test.sh
@@ -17,7 +17,7 @@ docker-compose up -d
 cd ../..
 
 # Start integration test
-flutter test integration_test/$testName.dart
+flutter test integration_test/$testName.dart && echo '0' > /tmp/geckoTestResult || echo '1' > /tmp/geckoTestResult
 
 # Reset .env
 echo "ip_address=127.0.0.1" > .env
diff --git a/integration_test/multi_chests.dart b/integration_test/multi_chests.dart
new file mode 100644
index 00000000..2b299db7
--- /dev/null
+++ b/integration_test/multi_chests.dart
@@ -0,0 +1,17 @@
+import 'package:flutter_dotenv/flutter_dotenv.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:gecko/models/widgets_keys.dart';
+import 'package:integration_test/integration_test.dart';
+import 'general_actions.dart';
+import 'tests_utility.dart';
+
+void main() async {
+  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
+  await dotenv.load();
+
+  testWidgets('Onboarding and multi chest', (testerLoc) async {
+    tester = testerLoc;
+    await bkFastStart(false);
+    await onboardingNewChest();
+  }, timeout: testTimeout());
+}
diff --git a/integration_test/tests_utility.dart b/integration_test/tests_utility.dart
index bb39cabe..e8c60b73 100644
--- a/integration_test/tests_utility.dart
+++ b/integration_test/tests_utility.dart
@@ -265,13 +265,19 @@ Future<WalletData> _addImportAccount(
 Future bkDeleteAllWallets() async {
   final myWalletProvider =
       Provider.of<MyWalletsProvider>(homeContext, listen: false);
-  if (myWalletProvider.listWallets.isNotEmpty) {
-    await myWalletProvider.deleteAllWallet(homeContext);
+  final isWalletsPresents =
+      await isPresent('Scanner un', timeout: const Duration(milliseconds: 300));
+  if (isWalletsPresents) {
+    await walletBox.clear();
+    await chestBox.clear();
+    await configBox.delete('defaultWallet');
+    await sub.deleteAllAccounts();
+    myWalletProvider.pinCode = '';
     myWalletProvider.rebuildWidget();
   }
 }
 
-Future bkFastStart() async {
+Future bkFastStart([bool restoreChest = true]) async {
   // Start app and wait finish starting
   await startWait();
 
@@ -282,16 +288,23 @@ Future bkFastStart() async {
   // Delete all existing chests is exists
   await bkDeleteAllWallets();
 
-  // Restore the test chest
-  await bkRestoreChest();
-  await waitFor("y'a pas de lézard");
+  if (restoreChest) {
+    // Restore the test chest
+    await bkRestoreChest();
+    await waitFor("y'a pas de lézard");
+  }
 }
 
 Future startWait() async {
   app.main();
   await waitFor('Test starting...', reverse: true);
   await tester.pumpAndSettle(const Duration(milliseconds: 300));
-  await sleep(2000);
+  await sleep(3000);
+}
+
+String getWidgetText(Key key) {
+  final word4Finder = find.byKey(key);
+  return (word4Finder.evaluate().single.widget as Text).data!;
 }
 
 class TestWallet {
diff --git a/lib/models/widgets_keys.dart b/lib/models/widgets_keys.dart
index e10c39d0..85bb6913 100644
--- a/lib/models/widgets_keys.dart
+++ b/lib/models/widgets_keys.dart
@@ -16,6 +16,7 @@ const keyDrawerMenu = Key('keyDrawerMenu');
 const keyOpenWalletsHomme = Key('keyOpenWalletsHomme');
 const keyOpenSearch = Key('keyOpenSearch');
 const keyRestoreChest = Key('keyRestoreChest');
+const keyOnboardingNewChest = Key('keyOnboardingNewChest');
 
 // Wallets home
 const keyImportG1v1 = Key('keyImportG1v1');
diff --git a/lib/screens/home.dart b/lib/screens/home.dart
index 88507815..2516f867 100644
--- a/lib/screens/home.dart
+++ b/lib/screens/home.dart
@@ -538,7 +538,7 @@ Widget welcomeHome(context) {
                 SizedBox(
                   width: 410,
                   height: 70,
-                  child: ElevatedButton(
+                  child: ElevatedButton(key: keyOnboardingNewChest,
                     style: ElevatedButton.styleFrom(
                       elevation: 4,
                       primary: orangeC, // background
diff --git a/lib/screens/onBoarding/10.dart b/lib/screens/onBoarding/10.dart
index 82df70c2..e3150a15 100644
--- a/lib/screens/onBoarding/10.dart
+++ b/lib/screens/onBoarding/10.dart
@@ -1,6 +1,7 @@
 // ignore_for_file: file_names
 
 import 'dart:async';
+import 'dart:io';
 import 'package:easy_localization/easy_localization.dart';
 import 'package:flutter/foundation.dart';
 import 'package:flutter/services.dart';
@@ -228,6 +229,7 @@ class OnboardingStepTen extends StatelessWidget {
 
                 generateWalletProvider.generatedMnemonic = '';
                 myWalletProvider.resetPinCode();
+                // sleep(const Duration(milliseconds: 500));
                 Navigator.push(
                   context,
                   FaderTransition(
diff --git a/lib/screens/onBoarding/11_congratulations.dart b/lib/screens/onBoarding/11_congratulations.dart
index 11f6b29c..de1747d6 100644
--- a/lib/screens/onBoarding/11_congratulations.dart
+++ b/lib/screens/onBoarding/11_congratulations.dart
@@ -1,5 +1,7 @@
 // ignore_for_file: file_names
 
+import 'dart:io';
+
 import 'package:easy_localization/easy_localization.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter/material.dart';
@@ -62,13 +64,12 @@ Widget finishButton(BuildContext context) {
           onPrimary: Colors.white, // foreground
         ),
         onPressed: () {
-          Navigator.pushAndRemoveUntil(
-            context,
-            MaterialPageRoute(builder: (context) {
-              return const WalletsHome();
-            }),
-            ModalRoute.withName('/'),
-          );
+          // sleep(const Duration(milliseconds: 50));
+          Navigator.popUntil(homeContext, ModalRoute.withName('/'));
+          // sleep(const Duration(milliseconds: 500));
+          Navigator.pushNamed(homeContext, '/mywallets');
+          // Navigator.pushNamedAndRemoveUntil(
+          //     homeContext, '/mywallets', ModalRoute.withName('/'));
         },
         child: Text("accessMyChest".tr(),
             style:
diff --git a/lib/screens/onBoarding/5.dart b/lib/screens/onBoarding/5.dart
index 55e24664..dd4907c3 100644
--- a/lib/screens/onBoarding/5.dart
+++ b/lib/screens/onBoarding/5.dart
@@ -102,7 +102,8 @@ class _ChooseChestState extends State<OnboardingStepFive> {
             SizedBox(height: 22 * ratio),
             nextButton(
                 context, "iNotedMyMnemonic".tr(), false, widget.skipIntro),
-            SizedBox(height: 35 * ratio),
+            const Spacer(),
+            // SizedBox(height: 35 * ratio),
           ]),
           CommonElements().offlineInfo(context),
         ]),
@@ -231,6 +232,7 @@ Widget nextButton(
     width: 380 * ratio,
     height: 60 * ratio,
     child: ElevatedButton(
+      key: keyGoNext,
       style: ElevatedButton.styleFrom(
         elevation: 4,
         primary: orangeC, // background
diff --git a/lib/screens/onBoarding/6.dart b/lib/screens/onBoarding/6.dart
index 309ae299..2c8562bc 100644
--- a/lib/screens/onBoarding/6.dart
+++ b/lib/screens/onBoarding/6.dart
@@ -225,6 +225,7 @@ Widget nextButton(BuildContext context, String text, nextScreen, bool isFast) {
     width: 380 * ratio,
     height: 60 * ratio,
     child: ElevatedButton(
+      key: keyGoNext,
       style: ElevatedButton.styleFrom(
         elevation: 4,
         primary: orangeC, // background
diff --git a/lib/screens/transaction_in_progress.dart b/lib/screens/transaction_in_progress.dart
index febc0ede..9c8b2614 100644
--- a/lib/screens/transaction_in_progress.dart
+++ b/lib/screens/transaction_in_progress.dart
@@ -1,5 +1,3 @@
-import 'dart:io';
-
 import 'package:easy_localization/easy_localization.dart';
 import 'package:flutter/services.dart';
 import 'package:gecko/globals.dart';
-- 
GitLab