2024-10-08 20:22:22 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
import QtTest 1.15
|
2022-03-10 18:49:42 +00:00
|
|
|
|
2024-10-08 20:22:22 +00:00
|
|
|
import StatusQ 0.1
|
2022-03-10 18:49:42 +00:00
|
|
|
import StatusQ.Controls 0.1
|
|
|
|
import StatusQ.Controls.Validators 0.1
|
|
|
|
|
2022-03-21 11:04:11 +00:00
|
|
|
import StatusQ.TestHelpers 0.1
|
|
|
|
|
2022-03-10 18:49:42 +00:00
|
|
|
Item {
|
2023-01-03 15:25:45 +00:00
|
|
|
id: root
|
|
|
|
|
2022-03-10 18:49:42 +00:00
|
|
|
width: 300
|
|
|
|
height: 100
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
function loadControl(test, sourceComponent) {
|
|
|
|
let testItem = test.createTemporaryObject(sourceComponent, root)
|
|
|
|
test.verify(test.waitForRendering(testItem))
|
|
|
|
test.mouseClick(testItem)
|
|
|
|
return testItem
|
2022-03-10 18:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TestCase {
|
2022-03-21 11:04:11 +00:00
|
|
|
id: regexTC
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
property StatusInput testControl: null
|
|
|
|
|
2024-10-08 20:22:22 +00:00
|
|
|
name: "StatusInput-RegexValidationTest"
|
2022-03-10 18:49:42 +00:00
|
|
|
|
|
|
|
when: windowShown
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
Component {
|
|
|
|
id: defaultComponent
|
|
|
|
|
|
|
|
StatusInput {
|
|
|
|
label: "Control under test"
|
|
|
|
charLimit: 30
|
|
|
|
placeholderText: `Must match regex(${validators[0].regularExpression.toString()}) and <= 30 chars`
|
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
focus: true
|
|
|
|
|
|
|
|
validators: [
|
|
|
|
StatusRegularExpressionValidator {
|
|
|
|
regularExpression: /^[0-9A-Za-z_\$-\s]*$/
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:49:42 +00:00
|
|
|
//
|
|
|
|
// Test guards
|
2023-01-03 15:25:45 +00:00
|
|
|
|
2022-04-19 17:45:26 +00:00
|
|
|
function init() {
|
|
|
|
qtOuput.restartCapturing()
|
2023-01-03 15:25:45 +00:00
|
|
|
regexTC.testControl = root.loadControl(regexTC, defaultComponent)
|
2022-03-10 18:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Tests
|
|
|
|
function test_initial_empty_is_valid() {
|
2023-01-03 15:25:45 +00:00
|
|
|
verify(!regexTC.testControl.valid, "Expected valid input")
|
|
|
|
|
|
|
|
verify(qtOuput.qtOuput().length === 0, `No output expected. Found:\n"${qtOuput.qtOuput()}"\n`)
|
2022-03-10 18:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_regex_validation() {
|
2023-01-03 15:25:45 +00:00
|
|
|
TestUtils.pressKeyAndWait(regexTC, regexTC.testControl, Qt.Key_1)
|
|
|
|
verify(regexTC.testControl.valid, "Expected valid input")
|
|
|
|
TestUtils.pressKeyAndWait(regexTC, regexTC.testControl, Qt.Key_Ampersand)
|
|
|
|
verify(!regexTC.testControl.valid, "Expected invalid input")
|
|
|
|
|
|
|
|
verify(qtOuput.qtOuput().length === 0, `No output expected. Found:\n"${qtOuput.qtOuput()}"\n`)
|
2022-03-10 18:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_no_invalid_input() {
|
2023-09-05 14:54:34 +00:00
|
|
|
skip("Test outdated. Needs to be reviewed and fixed.")
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
regexTC.testControl.validationMode = StatusInput.ValidationMode.IgnoreInvalidInput
|
|
|
|
|
|
|
|
verify(regexTC.testControl.valid, "Expected valid input")
|
|
|
|
verify(regexTC.testControl.text.length === 0, "Expected no input")
|
|
|
|
TestUtils.pressKeyAndWait(regexTC, regexTC.testControl, Qt.Key_2)
|
|
|
|
verify(regexTC.testControl.valid, "Expected valid input")
|
|
|
|
verify(regexTC.testControl.text === "2", "Expect one character")
|
|
|
|
TestUtils.pressKeyAndWait(regexTC, regexTC.testControl, Qt.Key_Ampersand)
|
|
|
|
verify(regexTC.testControl.valid, "Expected invalid input")
|
|
|
|
verify(regexTC.testControl.text === "2", "Expect the same input")
|
|
|
|
|
|
|
|
verify(qtOuput.qtOuput().length === 0, `No output expected. Found:\n"${qtOuput.qtOuput()}"\n`)
|
2022-03-10 18:49:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
// Use case expected in case new validation changes are enabled with old invalid data
|
2022-03-10 18:49:42 +00:00
|
|
|
function test_user_can_delete_initial_invalid_input() {
|
|
|
|
const appendInvalidChars = "#@!*"
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
regexTC.testControl.text = "invalid $" + appendInvalidChars
|
|
|
|
TestUtils.pressKeyAndWait(regexTC, regexTC.testControl, Qt.Key_End)
|
|
|
|
verify(!regexTC.testControl.valid, "Expected invalid input due to characters not matching")
|
2022-03-10 18:49:42 +00:00
|
|
|
// Delete invalid characters to get a valid text
|
|
|
|
for(let i = 0; i < appendInvalidChars.length; ++i)
|
2023-01-03 15:25:45 +00:00
|
|
|
TestUtils.pressKeyAndWait(regexTC, regexTC.testControl, Qt.Key_Backspace)
|
|
|
|
verify(regexTC.testControl.valid, "Expected valid input")
|
|
|
|
|
|
|
|
verify(qtOuput.qtOuput().length === 0, `No output expected. Found:\n"${qtOuput.qtOuput()}"\n`)
|
2022-03-10 18:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-21 11:04:11 +00:00
|
|
|
|
|
|
|
TestCase {
|
2023-01-03 15:25:45 +00:00
|
|
|
id: bindedTC
|
2022-03-21 11:04:11 +00:00
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
property StatusInput testControl: null
|
2022-03-21 11:04:11 +00:00
|
|
|
|
2024-10-08 20:22:22 +00:00
|
|
|
name: "StatusInput-BindedValuesTest"
|
2022-03-21 11:04:11 +00:00
|
|
|
when: windowShown
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
property QtObject dataObject: QtObject {
|
|
|
|
property string text: "Test text"
|
|
|
|
property string emoji: "👍"
|
|
|
|
property string color: "#FF0000"
|
|
|
|
}
|
2022-03-21 11:04:11 +00:00
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
Component {
|
|
|
|
id: bindedTextComponent
|
|
|
|
|
|
|
|
StatusInput {
|
|
|
|
charLimit: 10
|
|
|
|
input.isIconSelectable: true
|
|
|
|
placeholderText: qsTr("Enter an account name...")
|
|
|
|
text: bindedTC.dataObject.text
|
|
|
|
input.asset.emoji: bindedTC.dataObject.emoji
|
|
|
|
input.asset.color: bindedTC.dataObject.color
|
|
|
|
|
|
|
|
anchors.fill: parent
|
|
|
|
focus: true
|
|
|
|
|
|
|
|
validators: [
|
|
|
|
StatusMinLengthValidator {
|
|
|
|
errorMessage: qsTr("You need to enter an account name")
|
|
|
|
minLength: 1
|
|
|
|
},
|
|
|
|
StatusRegularExpressionValidator {
|
|
|
|
regularExpression: /^[^<>]+$/
|
|
|
|
errorMessage: qsTr("This is not a valid account name")
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2022-03-21 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 15:25:45 +00:00
|
|
|
//
|
|
|
|
// Test guards
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
bindedTC.testControl = root.loadControl(regexTC, bindedTextComponent)
|
2022-03-21 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Tests
|
2023-01-03 15:25:45 +00:00
|
|
|
function test_assigning_valid_value_works() {
|
|
|
|
qtOuput.restartCapturing()
|
|
|
|
|
|
|
|
verify(!testControl.valid, "Expected input not validated yet")
|
|
|
|
testControl.validationMode = StatusInput.ValidationMode.Always
|
|
|
|
verify(waitForRendering(testControl))
|
|
|
|
verify(testControl.valid, "Expected valid input")
|
|
|
|
bindedTC.dataObject.text = "Test<New"
|
|
|
|
verify(!testControl.valid, "Expected valid input")
|
2022-03-21 11:04:11 +00:00
|
|
|
|
|
|
|
verify(qtOuput.qtOuput().length === 0, `No output expected. Found:\n"${qtOuput.qtOuput()}"\n`)
|
|
|
|
}
|
2024-10-08 20:22:22 +00:00
|
|
|
|
|
|
|
// regression test for https://github.com/status-im/status-desktop/issues/16479
|
|
|
|
function test_paste_text() {
|
|
|
|
verify(!!testControl)
|
|
|
|
|
|
|
|
const textToPaste = "1234567890abcdef" // 16 chars
|
|
|
|
ClipboardUtils.setText(textToPaste)
|
|
|
|
|
|
|
|
// clear
|
|
|
|
testControl.input.edit.clear()
|
|
|
|
testControl.charLimit = 0
|
|
|
|
|
|
|
|
keySequence(StandardKey.Paste)
|
|
|
|
|
|
|
|
// verify we can paste the full text
|
|
|
|
compare(testControl.input.edit.length, textToPaste.length)
|
|
|
|
compare(testControl.input.edit.text, textToPaste)
|
|
|
|
compare(testControl.input.dirty, true)
|
|
|
|
|
|
|
|
// clear again, and set a lower limit
|
|
|
|
testControl.input.edit.clear()
|
|
|
|
testControl.charLimit = 10
|
|
|
|
|
|
|
|
keySequence(StandardKey.Paste)
|
|
|
|
|
|
|
|
// verify we can paste (some) text and it gets truncated to the charLimit
|
|
|
|
compare(testControl.input.edit.length, testControl.charLimit)
|
|
|
|
compare(testControl.input.edit.text, textToPaste.slice(0, testControl.charLimit))
|
|
|
|
}
|
2022-03-21 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MonitorQtOutput {
|
|
|
|
id: qtOuput
|
|
|
|
}
|
2022-03-10 18:49:42 +00:00
|
|
|
}
|