File indexing completed on 2024-11-10 03:39:47

0001 /*
0002     SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QSignalSpy>
0008 #include <QTest>
0009 
0010 #include "test_model_helpers.h"
0011 #include <knumbermodel.h>
0012 using namespace TestModelHelpers;
0013 
0014 Q_DECLARE_METATYPE(QModelIndex)
0015 
0016 class tst_KNumberModel : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private Q_SLOTS:
0021     void init()
0022     {
0023         QLocale::setDefault(QLocale::c());
0024     }
0025 
0026     void basicTest()
0027     {
0028         KNumberModel m;
0029         m.setMinimumValue(3);
0030         m.setMaximumValue(5);
0031         QCOMPARE(m.rowCount(), 3);
0032         QCOMPARE(m.data(m.index(0, 0), Qt::DisplayRole), QVariant("3"));
0033         QCOMPARE(m.data(m.index(1, 0), Qt::DisplayRole), QVariant("4"));
0034         QCOMPARE(m.data(m.index(2, 0), Qt::DisplayRole), QVariant("5"));
0035     }
0036 
0037     void testUpdates()
0038     {
0039         KNumberModel m;
0040         m.setMinimumValue(3);
0041         m.setMaximumValue(5);
0042         QSignalSpy resetSpy(&m, &QAbstractItemModel::modelReset);
0043         m.setMaximumValue(7);
0044         QVERIFY(resetSpy.count() == 1);
0045     }
0046 
0047     void testStep()
0048     {
0049         KNumberModel m;
0050         m.setMinimumValue(3);
0051         m.setMaximumValue(4);
0052         m.setStepSize(0.4);
0053         QCOMPARE(m.rowCount(), 3);
0054         QCOMPARE(m.data(m.index(2, 0), Qt::DisplayRole), QVariant("3.8"));
0055     }
0056 
0057     void testLocale()
0058     {
0059         KNumberModel m;
0060         m.setMinimumValue(1000);
0061         m.setMaximumValue(1000);
0062         QCOMPARE(m.rowCount(), 1);
0063         QCOMPARE(m.data(m.index(0, 0), Qt::DisplayRole), QVariant("1,000"));
0064 
0065         m.setFormattingOptions(QLocale::OmitGroupSeparator);
0066         QCOMPARE(m.data(m.index(0, 0), Qt::DisplayRole), QVariant("1000"));
0067     }
0068 
0069 private:
0070 };
0071 
0072 QTEST_MAIN(tst_KNumberModel)
0073 
0074 #include "knumbermodeltest.moc"