From dab15388527d68e8a72494cb3e28c17aab86d4e7 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Tue, 14 Jul 2020 14:18:29 +0200 Subject: Fix tst_QQuickMenu::subMenuPosition() We're still getting failures after QCOMPARE was fixed (in c78a960198d59fb9a9ddd83ad7098b1db396edfd): FAIL! : tst_QQuickMenu::Material::subMenuPosition(cascading,flip) Compared doubles are not the same (fuzzy compare) Actual (subSubMenu1->popupItem()->x()) : 3.88240550819e-11 Expected (subMenu1->popupItem()->x() - subSubMenu1->width() + overlap): 0 tst_qquickmenu.cpp(1532) : failure location Quoting Eddy: "[...] the threshold for double equality is one part in 1e12; the threshold for fuzzy-is-zero is likewise 1e-12; so 3e-11 is non-zero. One work-around, of course, would be to cast float(each operand) so as to get float's wider tolerance (one part in 100,000, null if less than 1e-5)." So that's what we do in this patch. Change-Id: Iecf1b6f4b2cf2c81eb652bb0f565ac682b024dae Reviewed-by: Volker Hilsheimer (cherry picked from commit 4b61d6f52b166ca2b4a2e38a2fc60d631372f6a7) Reviewed-by: Qt Cherry-pick Bot --- tests/auto/qquickmenu/tst_qquickmenu.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tests/auto/qquickmenu/tst_qquickmenu.cpp b/tests/auto/qquickmenu/tst_qquickmenu.cpp index fcec17bb..6f912e8f 100644 --- a/tests/auto/qquickmenu/tst_qquickmenu.cpp +++ b/tests/auto/qquickmenu/tst_qquickmenu.cpp @@ -1397,6 +1397,12 @@ void tst_QQuickMenu::subMenuDisabledKeyboard() QVERIFY(!subMenu->isVisible()); } +/* + QCOMPARE() compares doubles with 1-in-1e12 precision, which is too fine for these tests. + Casting to floats, compared with 1-in-1e5 precision, gives more robust results. +*/ +#define FLOAT_EQ(u, v) QCOMPARE(float(u), float(v)) + void tst_QQuickMenu::subMenuPosition_data() { QTest::addColumn("cascade"); @@ -1497,19 +1503,20 @@ void tst_QQuickMenu::subMenuPosition() if (cascade) { QCOMPARE(subMenu1->parentItem(), subMenu1Item); // vertically aligned to the parent menu item - QCOMPARE(subMenu1->popupItem()->y(), mainMenu->popupItem()->y() + subMenu1Item->y()); + // We cast to float here because we want to use its larger tolerance for equality (because it has less precision than double). + FLOAT_EQ(subMenu1->popupItem()->y(), mainMenu->popupItem()->y() + subMenu1Item->y()); if (mirrored) { // on the left of the parent menu - QCOMPARE(subMenu1->popupItem()->x(), mainMenu->popupItem()->x() - subMenu1->width() + overlap); + FLOAT_EQ(subMenu1->popupItem()->x(), mainMenu->popupItem()->x() - subMenu1->width() + overlap); } else { // on the right of the parent menu - QCOMPARE(subMenu1->popupItem()->x(), mainMenu->popupItem()->x() + mainMenu->width() - overlap); + FLOAT_EQ(subMenu1->popupItem()->x(), mainMenu->popupItem()->x() + mainMenu->width() - overlap); } } else { QCOMPARE(subMenu1->parentItem(), mainMenu->parentItem()); // centered over the parent menu - QCOMPARE(subMenu1->popupItem()->x(), mainMenu->popupItem()->x() + (mainMenu->width() - subMenu1->width()) / 2); - QCOMPARE(subMenu1->popupItem()->y(), mainMenu->popupItem()->y() + (mainMenu->height() - subMenu1->height()) / 2); + FLOAT_EQ(subMenu1->popupItem()->x(), mainMenu->popupItem()->x() + (mainMenu->width() - subMenu1->width()) / 2); + FLOAT_EQ(subMenu1->popupItem()->y(), mainMenu->popupItem()->y() + (mainMenu->height() - subMenu1->height()) / 2); } // open the sub-sub-menu (can flip) @@ -1526,22 +1533,24 @@ void tst_QQuickMenu::subMenuPosition() if (cascade) { QCOMPARE(subSubMenu1->parentItem(), subSubMenu1Item); // vertically aligned to the parent menu item - QCOMPARE(subSubMenu1->popupItem()->y(), subMenu1->popupItem()->y() + subSubMenu1Item->y()); + FLOAT_EQ(subSubMenu1->popupItem()->y(), subMenu1->popupItem()->y() + subSubMenu1Item->y()); if (mirrored != flip) { // on the left of the parent menu - QCOMPARE(subSubMenu1->popupItem()->x(), subMenu1->popupItem()->x() - subSubMenu1->width() + overlap); + FLOAT_EQ(subSubMenu1->popupItem()->x(), subMenu1->popupItem()->x() - subSubMenu1->width() + overlap); } else { // on the right of the parent menu - QCOMPARE(subSubMenu1->popupItem()->x(), subMenu1->popupItem()->x() + subMenu1->width() - overlap); + FLOAT_EQ(subSubMenu1->popupItem()->x(), subMenu1->popupItem()->x() + subMenu1->width() - overlap); } } else { QCOMPARE(subSubMenu1->parentItem(), subMenu1->parentItem()); // centered over the parent menu - QCOMPARE(subSubMenu1->popupItem()->x(), subMenu1->popupItem()->x() + (subMenu1->width() - subSubMenu1->width()) / 2); - QCOMPARE(subSubMenu1->popupItem()->y(), subMenu1->popupItem()->y() + (subMenu1->height() - subSubMenu1->height()) / 2); + FLOAT_EQ(subSubMenu1->popupItem()->x(), subMenu1->popupItem()->x() + (subMenu1->width() - subSubMenu1->width()) / 2); + FLOAT_EQ(subSubMenu1->popupItem()->y(), subMenu1->popupItem()->y() + (subMenu1->height() - subSubMenu1->height()) / 2); } } +#undef FLOAT_EQ + void tst_QQuickMenu::addRemoveSubMenus() { QQuickApplicationHelper helper(this, QLatin1String("subMenus.qml")); -- cgit v1.2.3