aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2020-02-27 15:36:20 +0100
committerMitch Curtis <mitch.curtis@qt.io>2020-07-16 16:40:36 +0200
commit49ffc6e6af83b295c67fd119b79c925879cc292e (patch)
tree782961b0fecd7ad2ea935e95073309028c8e4f22 /tests/auto
parentfeabd686a31d257638d9cdc80c6dd36e8709e279 (diff)
ComboBox: add implicitContentWidthPolicyHEADdev
[ChangeLog][Controls][ComboBox] Added implicitContentWidthPolicy, which controls how the implicitContentWidth of the ComboBox is calculated. This can be used to automatically ensure that text is not elided. Fixes: QTBUG-78281 Change-Id: If669fa4ef05e5da498992843469000ef39c335ec Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/controls/data/tst_combobox.qml186
1 files changed, 182 insertions, 4 deletions
diff --git a/tests/auto/controls/data/tst_combobox.qml b/tests/auto/controls/data/tst_combobox.qml
index 64a525bf..7369f263 100644
--- a/tests/auto/controls/data/tst_combobox.qml
+++ b/tests/auto/controls/data/tst_combobox.qml
@@ -48,10 +48,10 @@
**
****************************************************************************/
-import QtQuick 2.14
-import QtQuick.Window 2.2
-import QtTest 1.0
-import QtQuick.Controls 2.12
+import QtQuick 2.15
+import QtQuick.Window 2.15
+import QtTest 1.15
+import QtQuick.Controls 2.15
TestCase {
id: testCase
@@ -1970,4 +1970,182 @@ TestCase {
compare(comboBox1.currentIndex, 9)
compare(currentIndexSpy.count, 1)
}
+
+ Component {
+ id: appFontTextFieldComponent
+ TextField {
+ objectName: "appFontTextField"
+ font: Qt.application.font
+ // We don't want the background's implicit width to interfere with our tests,
+ // which are about implicit width of the contentItem of ComboBox, which is by default TextField.
+ background: null
+ }
+ }
+
+ Component {
+ id: appFontContentItemComboBoxComponent
+ ComboBox {
+ // Override the contentItem so that the font doesn't vary between styles.
+ contentItem: TextField {
+ objectName: "appFontContentItemTextField"
+ // We do this just to be extra sure that the font never comes from the control,
+ // as we want it to match that of the TextField in the appFontTextFieldComponent.
+ font: Qt.application.font
+ background: null
+ }
+ }
+ }
+
+ Component {
+ id: twoItemListModelComponent
+
+ ListModel {
+ ListElement { display: "Short" }
+ ListElement { display: "Kinda long" }
+ }
+ }
+
+ function appendedToModel(model, item) {
+ if (Array.isArray(model)) {
+ let newModel = model
+ newModel.push(item)
+ return newModel
+ }
+
+ if (model.hasOwnProperty("append")) {
+ model.append({ display: item })
+ // To account for the fact that changes to a JS array are not seen by the QML engine,
+ // we need to reassign the entire model and hence return it. For simplicity in the
+ // calling code, we do it for the ListModel code path too. It should be a no-op.
+ return model
+ }
+
+ console.warn("appendedToModel: unrecognised model")
+ return undefined
+ }
+
+ function removedFromModel(model, index, count) {
+ if (Array.isArray(model)) {
+ let newModel = model
+ newModel.splice(index, count)
+ return newModel
+ }
+
+ if (model.hasOwnProperty("remove")) {
+ model.remove(index, count)
+ return model
+ }
+
+ console.warn("removedFromModel: unrecognised model")
+ return undefined
+ }
+
+ // We don't use a data-driven test for the policy because the checks vary a lot based on which enum we're testing.
+ function test_implicitContentWidthPolicy_ContentItemImplicitWidth() {
+ // Set ContentItemImplicitWidth and ensure that implicitContentWidth is as wide as the current item
+ // by comparing it against the implicitWidth of an identical TextField
+ let control = createTemporaryObject(appFontContentItemComboBoxComponent, testCase, {
+ model: ["Short", "Kinda long"],
+ implicitContentWidthPolicy: ComboBox.ContentItemImplicitWidth
+ })
+ verify(control)
+ compare(control.implicitContentWidthPolicy, ComboBox.ContentItemImplicitWidth)
+
+ let textField = createTemporaryObject(appFontTextFieldComponent, testCase)
+ verify(textField)
+ // Don't set any text on textField because we're not accounting for the widest
+ // text here, so we want to compare it against an empty TextField.
+ compare(control.implicitContentWidth, textField.implicitWidth)
+
+ textField.font.pixelSize *= 2
+ control.font.pixelSize *= 2
+ compare(control.implicitContentWidth, textField.implicitWidth)
+ }
+
+ function test_implicitContentWidthPolicy_WidestText_data() {
+ return [
+ { tag: "Array", model: ["Short", "Kinda long"] },
+ { tag: "ListModel", model: twoItemListModelComponent.createObject(testCase) },
+ ]
+ }
+
+ function test_implicitContentWidthPolicy_WidestText(data) {
+ let control = createTemporaryObject(appFontContentItemComboBoxComponent, testCase, {
+ model: data.model,
+ implicitContentWidthPolicy: ComboBox.WidestText
+ })
+ verify(control)
+ compare(control.implicitContentWidthPolicy, ComboBox.WidestText)
+
+ let textField = createTemporaryObject(appFontTextFieldComponent, testCase)
+ verify(textField)
+ textField.text = "Kinda long"
+ // Note that we don't need to change the current index here, as the implicitContentWidth
+ // is set to the implicitWidth of the TextField within the ComboBox as if it had the largest
+ // text from the model set on it.
+ // We use Math.ceil because TextInput uses qCeil internally, whereas the implicitWidth
+ // binding for TextField does not.
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(textField.implicitWidth))
+
+ // Add a longer item; it should affect the implicit content width.
+ let modifiedModel = appendedToModel(data.model, "Moderately long")
+ control.model = modifiedModel
+ textField.text = "Moderately long"
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(textField.implicitWidth))
+
+ // Remove the last two items; it should use the only remaining item's width.
+ modifiedModel = removedFromModel(data.model, 1, 2)
+ control.model = modifiedModel
+ compare(control.count, 1)
+ compare(control.currentText, "Short")
+ textField.text = "Short"
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(textField.implicitWidth))
+
+ // Changes in font should result in the implicitContentWidth being updated.
+ textField.font.pixelSize *= 2
+ // We have to change the contentItem's font size manually since we break the
+ // style's binding to the control's font when we set Qt.application.font to it.
+ control.contentItem.font.pixelSize *= 2
+ control.font.pixelSize *= 2
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(textField.implicitWidth))
+ }
+
+ function test_implicitContentWidthPolicy_WidestTextWhenCompleted_data() {
+ return test_implicitContentWidthPolicy_WidestText_data()
+ }
+
+ function test_implicitContentWidthPolicy_WidestTextWhenCompleted(data) {
+ let control = createTemporaryObject(appFontContentItemComboBoxComponent, testCase, {
+ model: data.model,
+ implicitContentWidthPolicy: ComboBox.WidestTextWhenCompleted
+ })
+ verify(control)
+ compare(control.implicitContentWidthPolicy, ComboBox.WidestTextWhenCompleted)
+
+ let textField = createTemporaryObject(appFontTextFieldComponent, testCase)
+ verify(textField)
+ textField.text = "Kinda long"
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(textField.implicitWidth))
+
+ // Add a longer item; it should not affect the implicit content width
+ // since we've already accounted for it once.
+ let modifiedModel = appendedToModel(data.model, "Moderately long")
+ control.model = modifiedModel
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(textField.implicitWidth))
+
+ // Remove the last two items; it should still not affect the implicit content width.
+ modifiedModel = removedFromModel(data.model, 1, 2)
+ control.model = modifiedModel
+ compare(control.count, 1)
+ compare(control.currentText, "Short")
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(textField.implicitWidth))
+
+ // Changes in font should not result in the implicitContentWidth being updated.
+ let oldTextFieldImplicitWidth = textField.implicitWidth
+ // Changes in font should result in the implicitContentWidth being updated.
+ textField.font.pixelSize *= 2
+ control.contentItem.font.pixelSize *= 2
+ control.font.pixelSize *= 2
+ compare(Math.ceil(control.implicitContentWidth), Math.ceil(oldTextFieldImplicitWidth))
+ }
}