diff options
author | Tim Jenssen <tim.jenssen@qt.io> | 2020-08-13 08:34:24 +0200 |
---|---|---|
committer | Tim Jenssen <tim.jenssen@qt.io> | 2020-08-13 06:39:40 +0000 |
commit | 4fe5b923cddc9110094a883b5b7dd3051d8c8d90 (patch) | |
tree | 34e3984ab983882423bc563542a041b99b35f065 /tests/unit | |
parent | 24ca17f714ec133acc2bf0fd93c051ef730d69dc (diff) | |
parent | dc870f538dd3e11464a3ece179edf024161e14ba (diff) |
Merge remote-tracking branch 'origin/qds-1.59' into 4.13
Change-Id: Ic720f3adab305c5ef8cd10e713ccabf510eff0c0
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/unittest/listmodeleditor-test.cpp | 461 |
1 files changed, 444 insertions, 17 deletions
diff --git a/tests/unit/unittest/listmodeleditor-test.cpp b/tests/unit/unittest/listmodeleditor-test.cpp index ca0913f865..bdd5e9c070 100644 --- a/tests/unit/unittest/listmodeleditor-test.cpp +++ b/tests/unit/unittest/listmodeleditor-test.cpp @@ -31,14 +31,17 @@ #include <qmldesigner/components/listmodeleditor/listmodeleditormodel.h> #include <qmldesigner/designercore/include/abstractview.h> +#include <qmldesigner/designercore/include/bindingproperty.h> #include <qmldesigner/designercore/include/model.h> #include <qmldesigner/designercore/include/nodelistproperty.h> +#include <qmldesigner/designercore/include/nodeproperty.h> #include <qmldesigner/designercore/include/variantproperty.h> namespace { using QmlDesigner::AbstractProperty; using QmlDesigner::AbstractView; +using QmlDesigner::ListModelEditorModel; using QmlDesigner::ModelNode; MATCHER_P2(HasItem, @@ -93,6 +96,7 @@ public: emptyListModelNode = mockView.createModelNode("QtQml.Models.ListModel", 2, 15); + listViewNode = mockView.createModelNode("QtQuick.ListView", 2, 15); listModelNode = mockView.createModelNode("QtQml.Models.ListModel", 2, 15); mockView.rootModelNode().defaultNodeListProperty().reparentHere(listModelNode); element1 = createElement({{"name", "foo"}, {"value", 1}, {"value2", 42}}); @@ -172,10 +176,20 @@ public: return properties; } + QModelIndex index(int row, int column) const { return model.index(row, column); } + + QList<ModelNode> elements(const ModelNode &node) const + { + return node.defaultNodeListProperty().toModelNodeList(); + } + protected: std::unique_ptr<QmlDesigner::Model> designerModel{QmlDesigner::Model::create("QtQuick.Item", 1, 1)}; NiceMock<MockListModelEditorView> mockView; - QmlDesigner::ListModelEditorModel model; + QmlDesigner::ListModelEditorModel model{ + [&] { return mockView.createModelNode("QtQml.Models.ListModel", 2, 15); }, + [&] { return mockView.createModelNode("QtQml.Models.ListElement", 2, 15); }}; + ModelNode listViewNode; ModelNode listModelNode; ModelNode emptyListModelNode; ModelNode element1; @@ -427,7 +441,7 @@ TEST_F(ListModelEditor, RemoveColumnRemovesDisplayValues) { model.setListModel(listModelNode); - model.removeColumn(2); + model.removeColumns({index(0, 2)}); ASSERT_THAT(displayValues(), ElementsAre(ElementsAre(IsInvalid(), "foo", 42), @@ -442,14 +456,14 @@ TEST_F(ListModelEditor, RemoveColumnRemovesProperties) EXPECT_CALL(mockView, propertiesRemoved(ElementsAre(IsAbstractProperty(element2, "image")))); EXPECT_CALL(mockView, propertiesRemoved(ElementsAre(IsAbstractProperty(element3, "image")))); - model.removeColumn(0); + model.removeColumns({index(0, 0)}); } TEST_F(ListModelEditor, RemoveColumnRemovesPropertyName) { model.setListModel(listModelNode); - model.removeColumn(1); + model.removeColumns({index(0, 1)}); ASSERT_THAT(model.propertyNames(), ElementsAre("image", "value", "value2")); } @@ -458,7 +472,7 @@ TEST_F(ListModelEditor, RemoveRowRemovesDisplayValues) { model.setListModel(listModelNode); - model.removeRow(1); + model.removeRows({index(1, 0)}); ASSERT_THAT(displayValues(), ElementsAre(ElementsAre(IsInvalid(), "foo", 1, 42), @@ -471,7 +485,7 @@ TEST_F(ListModelEditor, RemoveRowRemovesElementInListModel) EXPECT_CALL(mockView, nodeRemoved(Eq(element2), _, _)); - model.removeRow(1); + model.removeRows({index(1, 0)}); } TEST_F(ListModelEditor, ConvertStringFloatToFloat) @@ -721,7 +735,7 @@ TEST_F(ListModelEditor, RemoveColumnAfterRenameColumn) model.setListModel(listModelNode); model.renameColumn(1, "mood"); - model.removeColumn(1); + model.removeColumns({index(0, 1)}); ASSERT_THAT(properties(), ElementsAre(UnorderedElementsAre(IsVariantProperty("value", 1), @@ -909,44 +923,457 @@ TEST_F(ListModelEditor, RemoveLastRow) model.addColumn("mood"); model.addRow(); - model.removeRow(0); + model.removeRows({index(0, 0)}); ASSERT_THAT(displayValues(), IsEmpty()); } -TEST_F(ListModelEditor, RemoveLastColumn) +TEST_F(ListModelEditor, RemoveLastEmptyRow) { model.setListModel(emptyListModelNode); model.addColumn("mood"); model.addRow(); + model.removeColumns({index(0, 0)}); - model.removeColumn(0); + model.removeRows({index(0, 0)}); ASSERT_THAT(displayValues(), ElementsAre(IsEmpty())); } -TEST_F(ListModelEditor, RemoveLastEmptyColumn) +TEST_F(ListModelEditor, RemoveLastColumn) { model.setListModel(emptyListModelNode); model.addColumn("mood"); model.addRow(); - model.removeRow(0); - model.removeColumn(0); + model.removeColumns({index(0, 0)}); - ASSERT_THAT(displayValues(), IsEmpty()); + ASSERT_THAT(displayValues(), ElementsAre(IsEmpty())); } -TEST_F(ListModelEditor, RemoveLastEmptyRow) +TEST_F(ListModelEditor, RemoveLastEmptyColumn) { model.setListModel(emptyListModelNode); model.addColumn("mood"); model.addRow(); - model.removeColumn(0); + model.removeRows({index(0, 0)}); - model.removeRow(0); + model.removeColumns({index(0, 0)}); ASSERT_THAT(displayValues(), IsEmpty()); } +TEST_F(ListModelEditor, RemoveColumns) +{ + model.setListModel(listModelNode); + model.removeColumns({index(0, 1), index(0, 3), index(1, 1), index(0, 4)}); + + ASSERT_THAT(properties(), + ElementsAre(UnorderedElementsAre(IsVariantProperty("value", 1)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("value", 4)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("value", 111)))); +} + +TEST_F(ListModelEditor, RemoveRows) +{ + model.setListModel(listModelNode); + + model.removeRows({index(1, 0), index(2, 0), index(3, 0), index(2, 0)}); + + ASSERT_THAT(properties(), + ElementsAre(UnorderedElementsAre(IsVariantProperty("name", "foo"), + IsVariantProperty("value", 1), + IsVariantProperty("value2", 42)))); +} + +TEST_F(ListModelEditor, FilterColumns) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(0, 0), index(1, 1), index(0, 2), index(0, 1)}; + + auto columns = ListModelEditorModel::filterColumns(indices); + + ASSERT_THAT(columns, ElementsAre(0, 1, 2)); +} + +TEST_F(ListModelEditor, FilterColumnsInvalidColumns) +{ + QList<QModelIndex> indices = {index(0, 0), index(1, 1), index(0, 2), index(0, 1)}; + + auto columns = ListModelEditorModel::filterColumns(indices); + + ASSERT_THAT(columns, IsEmpty()); +} + +TEST_F(ListModelEditor, FilterColumnsEmptyInput) +{ + QList<QModelIndex> indices; + + auto columns = ListModelEditorModel::filterColumns(indices); + + ASSERT_THAT(columns, IsEmpty()); +} + +TEST_F(ListModelEditor, FilterRows) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(0, 0), index(1, 1), index(2, 2), index(0, 1)}; + + auto rows = ListModelEditorModel::filterRows(indices); + + ASSERT_THAT(rows, ElementsAre(0, 1, 2)); +} + +TEST_F(ListModelEditor, FilterRowsInvalidColumns) +{ + QList<QModelIndex> indices = {index(0, 0), index(1, 1), index(2, 2), index(0, 1)}; + + auto rows = ListModelEditorModel::filterRows(indices); + + ASSERT_THAT(rows, IsEmpty()); +} + +TEST_F(ListModelEditor, FilterRowsEmptyInput) +{ + QList<QModelIndex> indices; + + auto rows = ListModelEditorModel::filterRows(indices); + + ASSERT_THAT(rows, IsEmpty()); +} + +TEST_F(ListModelEditor, CannotMoveEmptyRowsUp) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(-1, 1)}; + + model.moveRowsUp(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element1, element2, element3)); +} + +TEST_F(ListModelEditor, MoveRowUp) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(1, 2), index(1, 0)}; + + model.moveRowsUp(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element2, element1, element3)); +} + +TEST_F(ListModelEditor, MoveRowsUp) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(2, 2), index(1, 0)}; + + model.moveRowsUp(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element2, element3, element1)); +} + +TEST_F(ListModelEditor, CannotMoveFirstRowsUp) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(0, 1), index(1, 2), index(0, 0)}; + + model.moveRowsUp(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element1, element2, element3)); +} + +TEST_F(ListModelEditor, CannotMoveEmptyRowsUpDisplayValues) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(-1, 1)}; + + model.moveRowsUp(indices); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre(IsInvalid(), "foo", 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()))); +} + +TEST_F(ListModelEditor, CannotMoveFirstRowUpDisplayValues) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(0, 1), index(1, 2), index(0, 0)}; + + model.moveRowsUp(indices); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre(IsInvalid(), "foo", 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()))); +} + +TEST_F(ListModelEditor, MoveRowsUpDisplayValues) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(2, 2), index(1, 0)}; + + model.moveRowsUp(indices); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()), + ElementsAre(IsInvalid(), "foo", 1, 42))); +} + +TEST_F(ListModelEditor, NoSelectionAfterCannotMoveLastRowsDown) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(0, 1), index(1, 2), index(0, 0)}; + + auto selection = model.moveRowsUp(indices); + + ASSERT_THAT(selection.indexes(), IsEmpty()); +} + +TEST_F(ListModelEditor, NoSelectionAfterMoveEmptyRowsDown) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(-1, 1)}; + + auto selection = model.moveRowsUp(indices); + + ASSERT_THAT(selection.indexes(), IsEmpty()); +} + +TEST_F(ListModelEditor, SelectionAfterMoveRowsDown) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(2, 2), index(1, 0)}; + + auto selection = model.moveRowsUp(indices); + + ASSERT_THAT(selection.indexes(), + ElementsAre(index(0, 0), + index(0, 1), + index(0, 2), + index(0, 3), + index(1, 0), + index(1, 1), + index(1, 2), + index(1, 3))); +} + +TEST_F(ListModelEditor, CannotMoveEmptyRowsDown) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(-1, 1)}; + + model.moveRowsDown(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element1, element2, element3)); +} + +TEST_F(ListModelEditor, MoveRowDown) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(1, 2), index(1, 0)}; + + model.moveRowsDown(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element1, element3, element2)); +} + +TEST_F(ListModelEditor, MoveRowsDown) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(0, 2), index(1, 0)}; + + model.moveRowsDown(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element3, element1, element2)); +} + +TEST_F(ListModelEditor, CannotMoveLastRowsDown) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(2, 1), index(1, 2), index(2, 0)}; + + model.moveRowsDown(indices); + + ASSERT_THAT(elements(listModelNode), ElementsAre(element1, element2, element3)); +} + +TEST_F(ListModelEditor, CannotMoveEmptyRowsDownDisplayValues) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(-1, 1)}; + + model.moveRowsDown(indices); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre(IsInvalid(), "foo", 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()))); +} + +TEST_F(ListModelEditor, CannotMoveLastRowDownDisplayValues) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(2, 1), index(1, 2), index(2, 0)}; + + model.moveRowsDown(indices); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre(IsInvalid(), "foo", 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()))); +} + +TEST_F(ListModelEditor, MoveRowsDownDisplayValues) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(0, 2), index(1, 0)}; + + model.moveRowsDown(indices); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre("pic.png", "poo", 111, IsInvalid()), + ElementsAre(IsInvalid(), "foo", 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()))); +} + +TEST_F(ListModelEditor, NoSelectionAfterCannotMoveLastRowsUp) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(2, 1), index(1, 2), index(2, 0)}; + + auto selection = model.moveRowsDown(indices); + + ASSERT_THAT(selection.indexes(), IsEmpty()); +} + +TEST_F(ListModelEditor, NoSelectionAfterMoveEmptyRowsUp) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(-1, 1)}; + + auto selection = model.moveRowsDown(indices); + + ASSERT_THAT(selection.indexes(), IsEmpty()); +} + +TEST_F(ListModelEditor, SelectionAfterMoveRowsUp) +{ + model.setListModel(listModelNode); + QList<QModelIndex> indices = {index(1, 1), index(0, 2), index(1, 0)}; + + auto selection = model.moveRowsDown(indices); + + ASSERT_THAT(selection.indexes(), + ElementsAre(index(1, 0), + index(1, 1), + index(1, 2), + index(1, 3), + index(2, 0), + index(2, 1), + index(2, 2), + index(2, 3))); +} + +TEST_F(ListModelEditor, ListViewHasNoModel) +{ + model.setListView(listViewNode); + + ASSERT_THAT(listViewNode.nodeProperty("model").modelNode().type(), Eq("QtQml.Models.ListModel")); +} + +TEST_F(ListModelEditor, ListViewHasModelInside) +{ + listViewNode.nodeProperty("model").reparentHere(listModelNode); + + model.setListView(listViewNode); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre(IsInvalid(), "foo", 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()))); +} + +TEST_F(ListModelEditor, ListViewHasModelBinding) +{ + listModelNode.setIdWithoutRefactoring("listModel"); + listViewNode.bindingProperty("model").setExpression("listModel"); + + model.setListView(listViewNode); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre(IsInvalid(), "foo", 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()))); +} + +TEST_F(ListModelEditor, AddBooleanDisplayValues) +{ + model.setListModel(listModelNode); + + model.setValue(0, 1, true); + + ASSERT_THAT(displayValues(), + ElementsAre(ElementsAre(IsInvalid(), true, 1, 42), + ElementsAre("pic.png", "bar", 4, IsInvalid()), + ElementsAre("pic.png", "poo", 111, IsInvalid()))); +} + +TEST_F(ListModelEditor, AddBooleanProperties) +{ + model.setListModel(listModelNode); + + model.setValue(0, 1, true); + + ASSERT_THAT(properties(), + ElementsAre(UnorderedElementsAre(IsVariantProperty("name", "foo"), + IsVariantProperty("value", true), + IsVariantProperty("value2", 42)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("name", "bar"), + IsVariantProperty("value", 4)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("name", "poo"), + IsVariantProperty("value", 111)))); +} + +TEST_F(ListModelEditor, AddTrueAsStringProperties) +{ + model.setListModel(listModelNode); + + model.setValue(0, 1, "true"); + + ASSERT_THAT(properties(), + ElementsAre(UnorderedElementsAre(IsVariantProperty("name", true), + IsVariantProperty("value", 1), + IsVariantProperty("value2", 42)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("name", "bar"), + IsVariantProperty("value", 4)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("name", "poo"), + IsVariantProperty("value", 111)))); +} + +TEST_F(ListModelEditor, AddFalseAsStringProperties) +{ + model.setListModel(listModelNode); + + model.setValue(0, 1, "false"); + + ASSERT_THAT(properties(), + ElementsAre(UnorderedElementsAre(IsVariantProperty("name", false), + IsVariantProperty("value", 1), + IsVariantProperty("value2", 42)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("name", "bar"), + IsVariantProperty("value", 4)), + UnorderedElementsAre(IsVariantProperty("image", "pic.png"), + IsVariantProperty("name", "poo"), + IsVariantProperty("value", 111)))); +} + } // namespace |