From 37fd251413e60fff82bd8a34c1cae3da5d0fc522 Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Mon, 9 Aug 2021 15:14:10 -0400
Subject: [PATCH] wizardview: line edit enter key submits

Add enter key event handle for lineEdits in wizardview

Gitlab: #470

Change-Id: I41948196742068cd0a03ff37efe93982410e91ce
---
 src/commoncomponents/MaterialLineEdit.qml     |  18 +++
 .../ConnectToAccountManagerPage.qml           |   6 +
 .../components/CreateAccountPage.qml          |  14 ++
 .../components/CreateSIPAccountPage.qml       |   8 +
 .../components/ImportFromBackupPage.qml       |   4 +
 .../components/ImportFromDevicePage.qml       |   5 +
 src/wizardview/components/ProfilePage.qml     |   5 +
 tests/qml/src/tst_WizardView.qml              | 138 ++++++++++++++++--
 8 files changed, 189 insertions(+), 9 deletions(-)

diff --git a/src/commoncomponents/MaterialLineEdit.qml b/src/commoncomponents/MaterialLineEdit.qml
index 968529730..ea6348478 100644
--- a/src/commoncomponents/MaterialLineEdit.qml
+++ b/src/commoncomponents/MaterialLineEdit.qml
@@ -42,6 +42,8 @@ TextField {
     property var backgroundColor: JamiTheme.editBackgroundColor
     property var borderColor: JamiTheme.greyBorderColor
 
+    property bool loseFocusWhenEnterPressed: false
+
     signal imageClicked
 
     onBorderColorModeChanged: {
@@ -147,4 +149,20 @@ TextField {
         if (event.button == Qt.RightButton)
             lineEditContextMenu.openMenuAt(event)
     }
+
+    // Enter/Return keys intervention
+    // Now, both editingFinished and accepted
+    // signals will be emitted with focus set to false
+    // Use editingFinished when the info is saved by focus lost
+    // (since losing focus will also emit editingFinished)
+    // Use accepted when the info is not saved by focus lost
+    Keys.onPressed: {
+        if (event.key === Qt.Key_Enter ||
+                event.key === Qt.Key_Return) {
+            if (loseFocusWhenEnterPressed)
+                root.focus = false
+            root.accepted()
+            event.accepted = true;
+        }
+    }
 }
diff --git a/src/wizardview/components/ConnectToAccountManagerPage.qml b/src/wizardview/components/ConnectToAccountManagerPage.qml
index 08bcfaee1..e05cdb000 100644
--- a/src/wizardview/components/ConnectToAccountManagerPage.qml
+++ b/src/wizardview/components/ConnectToAccountManagerPage.qml
@@ -121,6 +121,7 @@ Rectangle {
             KeyNavigation.down: KeyNavigation.tab
 
             onTextChanged: errorText = ""
+            onAccepted: usernameManagerEdit.forceActiveFocus()
         }
 
         Label {
@@ -156,6 +157,7 @@ Rectangle {
             KeyNavigation.down: KeyNavigation.tab
 
             onTextChanged: errorText = ""
+            onAccepted: passwordManagerEdit.forceActiveFocus()
         }
 
         MaterialLineEdit {
@@ -186,6 +188,10 @@ Rectangle {
             KeyNavigation.down: KeyNavigation.tab
 
             onTextChanged: errorText = ""
+            onAccepted: {
+                if (connectBtn.enabled)
+                    connectBtn.clicked()
+            }
         }
 
         SpinnerButton {
diff --git a/src/wizardview/components/CreateAccountPage.qml b/src/wizardview/components/CreateAccountPage.qml
index bcb290879..4860ecb6c 100644
--- a/src/wizardview/components/CreateAccountPage.qml
+++ b/src/wizardview/components/CreateAccountPage.qml
@@ -138,6 +138,13 @@ Rectangle {
 
                     placeholderText: isRendezVous ? JamiStrings.chooseAName :
                                                     JamiStrings.chooseYourUserName
+
+                    onAccepted: {
+                        if (chooseUsernameButton.enabled)
+                            chooseUsernameButton.clicked()
+                        else
+                            skipButton.clicked()
+                    }
                 }
 
                 Label {
@@ -306,6 +313,8 @@ Rectangle {
                     KeyNavigation.tab: passwordConfirmEdit
                     KeyNavigation.up: passwordSwitch
                     KeyNavigation.down: KeyNavigation.tab
+
+                    onAccepted: passwordConfirmEdit.forceActiveFocus()
                 }
 
                 MaterialLineEdit {
@@ -329,6 +338,11 @@ Rectangle {
                                                                      backButton
                     KeyNavigation.up: passwordEdit
                     KeyNavigation.down: KeyNavigation.tab
+
+                    onAccepted: {
+                        if (createAccountButton.enabled)
+                            createAccountButton.clicked()
+                    }
                 }
 
                 Label {
diff --git a/src/wizardview/components/CreateSIPAccountPage.qml b/src/wizardview/components/CreateSIPAccountPage.qml
index 8aca180f8..ad1232495 100644
--- a/src/wizardview/components/CreateSIPAccountPage.qml
+++ b/src/wizardview/components/CreateSIPAccountPage.qml
@@ -102,6 +102,8 @@ Rectangle {
             KeyNavigation.tab: sipProxyEdit
             KeyNavigation.up: backButton
             KeyNavigation.down: KeyNavigation.tab
+
+            onAccepted: sipProxyEdit.forceActiveFocus()
         }
 
         MaterialLineEdit {
@@ -121,6 +123,8 @@ Rectangle {
             KeyNavigation.tab: sipUsernameEdit
             KeyNavigation.up: sipServernameEdit
             KeyNavigation.down: KeyNavigation.tab
+
+            onAccepted: sipUsernameEdit.forceActiveFocus()
         }
 
         MaterialLineEdit {
@@ -140,6 +144,8 @@ Rectangle {
             KeyNavigation.tab: sipPasswordEdit
             KeyNavigation.up: sipProxyEdit
             KeyNavigation.down: KeyNavigation.tab
+
+            onAccepted: sipPasswordEdit.forceActiveFocus()
         }
 
         MaterialLineEdit {
@@ -160,6 +166,8 @@ Rectangle {
             KeyNavigation.tab: createAccountButton
             KeyNavigation.up: sipUsernameEdit
             KeyNavigation.down: KeyNavigation.tab
+
+            onAccepted: createAccountButton.clicked()
         }
 
         MaterialButton {
diff --git a/src/wizardview/components/ImportFromBackupPage.qml b/src/wizardview/components/ImportFromBackupPage.qml
index 31ae31437..b55779cf5 100644
--- a/src/wizardview/components/ImportFromBackupPage.qml
+++ b/src/wizardview/components/ImportFromBackupPage.qml
@@ -195,6 +195,10 @@ Rectangle {
             KeyNavigation.down: KeyNavigation.tab
 
             onTextChanged: errorText = ""
+            onAccepted: {
+                if (connectBtn.enabled)
+                    connectBtn.clicked()
+            }
         }
 
         SpinnerButton {
diff --git a/src/wizardview/components/ImportFromDevicePage.qml b/src/wizardview/components/ImportFromDevicePage.qml
index d26ad109c..d8377e4f5 100644
--- a/src/wizardview/components/ImportFromDevicePage.qml
+++ b/src/wizardview/components/ImportFromDevicePage.qml
@@ -108,6 +108,7 @@ Rectangle {
             KeyNavigation.down: KeyNavigation.tab
 
             onTextChanged: errorText = ""
+            onAccepted: pinFromDevice.forceActiveFocus()
         }
 
         Text {
@@ -158,6 +159,10 @@ Rectangle {
             KeyNavigation.down: KeyNavigation.tab
 
             onTextChanged: errorText = ""
+            onAccepted: {
+                if (connectBtn.enabled)
+                    connectBtn.clicked()
+            }
         }
 
         SpinnerButton {
diff --git a/src/wizardview/components/ProfilePage.qml b/src/wizardview/components/ProfilePage.qml
index 91d48292b..e4009525d 100644
--- a/src/wizardview/components/ProfilePage.qml
+++ b/src/wizardview/components/ProfilePage.qml
@@ -171,6 +171,11 @@ Rectangle {
                     AccountAdapter.setCurrAccDisplayName(lastFirstChar)
                 }
             }
+
+            onAccepted: {
+                if (saveProfileBtn.enabled)
+                    saveProfileBtn.clicked()
+            }
         }
 
         SpinnerButton {
diff --git a/tests/qml/src/tst_WizardView.qml b/tests/qml/src/tst_WizardView.qml
index ac0889f4e..b85a6459d 100644
--- a/tests/qml/src/tst_WizardView.qml
+++ b/tests/qml/src/tst_WizardView.qml
@@ -647,6 +647,8 @@ WizardView {
         }
 
         function test_createAccountPageKeyNavigation() {
+            uut.clearSignalSpy()
+
             var welcomePage = findChild(uut, "welcomePage")
             var createAccountPage = findChild(uut, "createAccountPage")
 
@@ -741,8 +743,6 @@ WizardView {
             compare(usernameEdit.focus, true)
 
             // To createAccountPage - passwordSetupPage
-            keyClick(Qt.Key_Down)
-            keyClick(Qt.Key_Down)
             keyClick(Qt.Key_Enter)
             keyClick(Qt.Key_Tab)
             compare(passwordSwitch.focus, true)
@@ -861,14 +861,52 @@ WizardView {
             keyClick(Qt.Key_Up)
             compare(passwordSwitch.focus, true)
 
-            // Go back to welcomePage
-            keyClick(Qt.Key_Up)
+            passwordEdit.text = ""
+            passwordConfirmEdit.text = ""
+
+            // Check lineEdit enter key press corrspond correctly
+            keyClick(Qt.Key_Tab)
             keyClick(Qt.Key_Enter)
-            keyClick(Qt.Key_Up)
             keyClick(Qt.Key_Enter)
+            compare(passwordConfirmEdit.focus, true)
+
+            passwordEdit.text = "test"
+            passwordConfirmEdit.text = "test"
+
+            keyClick(Qt.Key_Enter)
+
+            // Wait until the account creation is finished
+            spyAccountIsReady.wait()
+            compare(spyAccountIsReady.count, 1)
+
+            // Go back to welcomePage
+            WizardViewStepModel.nextStep()
+
+            var showBackup = (WizardViewStepModel.accountCreationOption ===
+                              WizardViewStepModel.AccountCreationOption.CreateJamiAccount
+                              || WizardViewStepModel.accountCreationOption ===
+                              WizardViewStepModel.AccountCreationOption.CreateRendezVous)
+                              && !AppSettingsManager.getValue(Settings.NeverShowMeAgain)
+            if (showBackup) {
+                WizardViewStepModel.nextStep()
+            }
+
+            spyAccountConfigFinalized.wait()
+            compare(spyAccountConfigFinalized.count, 1)
+
+            spyCloseWizardView.wait()
+            compare(spyCloseWizardView.count, 1)
+
+            AccountAdapter.deleteCurrentAccount()
+
+            // Wait until the account removal is finished
+            spyAccountIsRemoved.wait()
+            compare(spyAccountIsRemoved.count, 1)
         }
 
         function test_importFromDevicePageKeyNavigation() {
+            uut.clearSignalSpy()
+
             var welcomePage = findChild(uut, "welcomePage")
             var importFromDevicePage = findChild(uut, "importFromDevicePage")
 
@@ -980,6 +1018,20 @@ WizardView {
 
             importFromDevicePageConnectBtn.spinnerTriggered = false
 
+            // Check lineEdit enter key press corrspond correctly
+            keyClick(Qt.Key_Enter)
+            compare(pinFromDevice.focus, true)
+
+            keyClick(Qt.Key_Up)
+            keyClick(Qt.Key_Enter)
+            compare(pinFromDevice.focus, true)
+
+            pinFromDevice.text = "test"
+            keyClick(Qt.Key_Enter)
+
+            spyReportFailure.wait(15000)
+            verify(spyReportFailure.count >= 1)
+
             // Go back to welcomePage
             keyClick(Qt.Key_Up)
             keyClick(Qt.Key_Up)
@@ -987,6 +1039,8 @@ WizardView {
         }
 
         function test_importFromBackupPageKeyNavigation() {
+            uut.clearSignalSpy()
+
             var welcomePage = findChild(uut, "welcomePage")
             var importFromBackupPage = findChild(uut, "importFromBackupPage")
 
@@ -1099,6 +1153,18 @@ WizardView {
 
             importFromBackupPageConnectBtn.spinnerTriggered = false
 
+            // Check lineEdit enter key press corrspond correctly
+            var fileName = "gz_test.gz"
+            var wrongPassword = "ccc"
+            importFromBackupPage.filePath = UtilsAdapter.toFileAbsolutepath(
+                        "tests/qml/src/resources/gz_test.gz") + "/" + fileName
+            passwordFromBackupEdit.text = wrongPassword
+
+            keyClick(Qt.Key_Enter)
+
+            spyReportFailure.wait(15000)
+            verify(spyReportFailure.count >= 1)
+
             // Go back to welcomePage
             keyClick(Qt.Key_Up)
             keyClick(Qt.Key_Up)
@@ -1106,6 +1172,8 @@ WizardView {
         }
 
         function test_connectToAccountManagerPageKeyNavigation() {
+            uut.clearSignalSpy()
+
             var welcomePage = findChild(uut, "welcomePage")
             var connectToAccountManagerPage = findChild(uut, "connectToAccountManagerPage")
 
@@ -1250,8 +1318,21 @@ WizardView {
 
             connectToAccountManagerPageConnectBtn.spinnerTriggered = false
 
+            // Check lineEdit enter key press corrspond correctly
+            accountManagerEdit.text = "test"
+            usernameManagerEdit.text = "test"
+            passwordManagerEdit.text = "test"
+
+            keyClick(Qt.Key_Enter)
+            keyClick(Qt.Key_Enter)
+            keyClick(Qt.Key_Enter)
+
+            spyReportFailure.wait(15000)
+            verify(spyReportFailure.count >= 1)
+
             // Go back to welcomePage
-            keyClick(Qt.Key_Up)
+            keyClick(Qt.Key_Tab)
+            keyClick(Qt.Key_Tab)
             keyClick(Qt.Key_Enter)
 
             // Hide advanced options
@@ -1259,6 +1340,8 @@ WizardView {
         }
 
         function test_createSIPAccountPageKeyNavigation() {
+            uut.clearSignalSpy()
+
             var welcomePage = findChild(uut, "welcomePage")
             var createSIPAccountPage = findChild(uut, "createSIPAccountPage")
 
@@ -1339,10 +1422,33 @@ WizardView {
             keyClick(Qt.Key_Up)
             compare(sipServernameEdit.focus, true)
 
-            // Go back to welcomePage
-            keyClick(Qt.Key_Up)
+            // Check lineEdit enter key press corrspond correctly
+            keyClick(Qt.Key_Enter)
+            keyClick(Qt.Key_Enter)
+            keyClick(Qt.Key_Enter)
             keyClick(Qt.Key_Enter)
 
+            // Wait until the account creation is finished
+            spyAccountIsReady.wait()
+            compare(spyAccountIsReady.count, 1)
+
+            spyAccountStatusChanged.wait()
+            verify(spyAccountStatusChanged.count >= 1)
+
+            WizardViewStepModel.nextStep()
+
+            spyAccountConfigFinalized.wait()
+            compare(spyAccountConfigFinalized.count, 1)
+
+            spyCloseWizardView.wait()
+            compare(spyCloseWizardView.count, 1)
+
+            AccountAdapter.deleteCurrentAccount()
+
+            // Wait until the account removal is finished
+            spyAccountIsRemoved.wait()
+            compare(spyAccountIsRemoved.count, 1)
+
             // Hide advanced options
             showAdvancedButton.clicked()
         }
@@ -1564,7 +1670,15 @@ WizardView {
 
             setAvatarWidget.isPreviewing = false
 
-            WizardViewStepModel.nextStep()
+            // Check lineEdit enter key press corrspond correctly
+            var aliasName = "test"
+            aliasEdit.text = aliasName
+            spyAccountStatusChanged.clear()
+
+            keyClick(Qt.Key_Tab)
+            keyClick(Qt.Key_Tab)
+            keyClick(Qt.Key_Tab)
+            keyClick(Qt.Key_Enter)
 
             var showBackup = (WizardViewStepModel.accountCreationOption ===
                               WizardViewStepModel.AccountCreationOption.CreateJamiAccount
@@ -1577,9 +1691,15 @@ WizardView {
                 WizardViewStepModel.nextStep()
             }
 
+            spyAccountStatusChanged.wait()
+            verify(spyAccountStatusChanged.count >= 1)
+
             spyCloseWizardView.wait()
             compare(spyCloseWizardView.count, 1)
 
+            // Check alias text
+            compare(SettingsAdapter.getCurrentAccount_Profile_Info_Alias(), aliasName)
+
             AccountAdapter.deleteCurrentAccount()
 
             // Wait until the account removal is finished
-- 
GitLab