diff --git a/tests/unit/tests/unittests/model/modelutils-test.cpp b/tests/unit/tests/unittests/model/modelutils-test.cpp index 4418d36ac83..8d69e69aba8 100644 --- a/tests/unit/tests/unittests/model/modelutils-test.cpp +++ b/tests/unit/tests/unittests/model/modelutils-test.cpp @@ -3,13 +3,16 @@ #include +#include #include #include - #include #include +#include namespace { +using QmlDesigner::ModelNode; +using QmlDesigner::ModelNodes; class ModelUtils : public ::testing::Test { @@ -106,4 +109,59 @@ TEST_F(ModelUtils, component_file_path_for_non_file_component_node_is_empty) ASSERT_THAT(path, IsEmpty()); } +TEST_F(ModelUtils, find_lowest_common_ancestor) +{ + auto child1 = model.createModelNode("Item"); + auto child2 = model.createModelNode("Item"); + model.rootModelNode().defaultNodeAbstractProperty().reparentHere(child1); + model.rootModelNode().defaultNodeAbstractProperty().reparentHere(child2); + ModelNodes nodes{child1, child2}; + + auto commonAncestor = QmlDesigner::ModelUtils::lowestCommonAncestor(nodes); + + ASSERT_THAT(commonAncestor, model.rootModelNode()); +} + +TEST_F(ModelUtils, lowest_common_ancestor_return_invalid_node_if_argument_is_invalid) +{ + auto child1 = model.createModelNode("Item"); + auto child2 = ModelNode{}; + model.rootModelNode().defaultNodeAbstractProperty().reparentHere(child1); + ModelNodes nodes{child1, child2}; + + auto commonAncestor = QmlDesigner::ModelUtils::lowestCommonAncestor(nodes); + + ASSERT_THAT(commonAncestor, Not(IsValid())); +} + +TEST_F(ModelUtils, find_lowest_common_ancestor_when_one_of_the_nodes_is_parent) +{ + auto parentNode = model.createModelNode("Item"); + auto childNode = model.createModelNode("Item"); + parentNode.defaultNodeAbstractProperty().reparentHere(childNode); + model.rootModelNode().defaultNodeAbstractProperty().reparentHere(parentNode); + ModelNodes nodes{childNode, parentNode}; + + auto commonAncestor = QmlDesigner::ModelUtils::lowestCommonAncestor(nodes); + + ASSERT_THAT(commonAncestor, parentNode); +} + +TEST_F(ModelUtils, lowest_common_ancestor_for_uncle_and_nephew_should_return_the_grandFather) +{ + auto grandFatherNode = model.createModelNode("Item"); + auto fatherNode = model.createModelNode("Item"); + auto uncleNode = model.createModelNode("Item"); + auto nephewNode = model.createModelNode("Item"); + fatherNode.defaultNodeAbstractProperty().reparentHere(nephewNode); + grandFatherNode.defaultNodeAbstractProperty().reparentHere(fatherNode); + grandFatherNode.defaultNodeAbstractProperty().reparentHere(uncleNode); + model.rootModelNode().defaultNodeAbstractProperty().reparentHere(grandFatherNode); + ModelNodes nodes{uncleNode, nephewNode}; + + auto commonAncestor = QmlDesigner::ModelUtils::lowestCommonAncestor(nodes); + + ASSERT_THAT(commonAncestor, grandFatherNode); +} + } // namespace