11// SPDX-License-Identifier: LGPL-2.1-or-later
22
33#include < QDebug>
4+ #include < QLayout>
5+ #include < QMainWindow>
46#include < QTest>
57
68#include < App/Application.h>
@@ -15,14 +17,33 @@ class testQuantitySpinBox: public QObject
1517
1618public:
1719 testQuantitySpinBox ()
20+ : qsb(nullptr )
21+ , mainWindow(nullptr )
1822 {
1923 if (App::Application::GetARGC () == 0 ) {
2024 constexpr int argc = 1 ;
2125 std::array<char *, argc> argv {" FreeCAD" };
2226 App::Application::Config ()[" ExeName" ] = " FreeCAD" ;
2327 App::Application::init (argc, argv.data ());
2428 }
25- qsb = std::make_unique<Gui::QuantitySpinBox>();
29+ auto topLevel = qApp->topLevelWindows ();
30+
31+ for (const auto window : topLevel) {
32+ if (auto mw = qobject_cast<QMainWindow*>(window)) {
33+ mainWindow = mw;
34+ break ;
35+ }
36+ }
37+ if (!mainWindow) {
38+ mainWindow = new QMainWindow ();
39+ }
40+ qsb = new Gui::QuantitySpinBox;
41+ if (!mainWindow->layout ()) {
42+ mainWindow->setLayout (new QVBoxLayout);
43+ }
44+ mainWindow->layout ()->addWidget (qsb);
45+ qsb->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding);
46+ mainWindow->show ();
2647 }
2748
2849private Q_SLOTS:
@@ -51,8 +72,32 @@ private Q_SLOTS:
5172 QCOMPARE (result, Base::Quantity (0.1 , QLatin1String (" mm" )));
5273 }
5374
75+ void test_DefaultUnitLost ()// NOLINT
76+ {
77+ // Arrange
78+ auto foundWindow = QTest::qWaitForWindowActive (mainWindow);
79+ QTEST_ASSERT (foundWindow);
80+ qsb->activateWindow ();
81+ qsb->clear ();
82+ QTest::keyClicks (qsb, " 1um" );
83+ QTest::keyClick (qsb, Qt::Key_Enter);
84+ QTEST_ASSERT (qsb->value () == Base::Quantity (1 , QLatin1String (" um" )));
85+ QTest::qWait (5000 );
86+
87+ // Act
88+ QTest::mouseClick (qsb, Qt::MouseButton::LeftButton);
89+ QTest::keyClicks (qsb, " 3" );
90+ QTest::keyClick (qsb, Qt::Key_Enter);
91+ qsb->evaluateExpression ();
92+ QTest::qWait (5000 );
93+
94+ // Assert
95+ QCOMPARE (qsb->value (), Base::Quantity (3 , QLatin1String (" um" )));
96+ }
97+
5498private:
55- std::unique_ptr<Gui::QuantitySpinBox> qsb;
99+ Gui::QuantitySpinBox* qsb;
100+ QMainWindow* mainWindow;
56101};
57102
58103// NOLINTEND(readability-magic-numbers)
0 commit comments