aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2020-07-14 14:18:29 +0200
committerMitch Curtis <mitch.curtis@qt.io>2020-07-14 15:34:25 +0200
commit4b61d6f52b166ca2b4a2e38a2fc60d631372f6a7 (patch)
tree0cbd6ca5d93deb732a17282f9c4bbeceb9676b36 /tests/auto
parent04901b170e009d844232067446fdd56f270d155d (diff)
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 Pick-to: 5.15 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/qquickmenu/tst_qquickmenu.cpp29
1 files changed, 19 insertions, 10 deletions
diff --git a/tests/auto/qquickmenu/tst_qquickmenu.cpp b/tests/auto/qquickmenu/tst_qquickmenu.cpp
index 9d29c675..a06b715f 100644
--- a/tests/auto/qquickmenu/tst_qquickmenu.cpp
+++ b/tests/auto/qquickmenu/tst_qquickmenu.cpp
@@ -1396,6 +1396,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<bool>("cascade");
@@ -1496,19 +1502,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)
@@ -1525,22 +1532,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"));