| 1 |
/* |
| 2 |
* Copyright (C) 2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify it under |
| 5 |
* the terms of the GNU Lesser General Public License version 3, as published by |
| 6 |
* the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 |
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
| 10 |
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <QtTest/QtTest> |
| 18 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 19 |
#include <LomiriToolkit-Qt6/private/ucunits_p.h> |
| 20 |
#else |
| 21 |
#include <LomiriToolkit/private/ucunits_p.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
UT_USE_NAMESPACE |
| 25 |
|
| 26 |
class tst_UCUnitsDPR2: public QObject |
| 27 |
{ |
| 28 |
Q_OBJECT |
| 29 |
|
| 30 |
private Q_SLOTS: |
| 31 |
|
| 32 |
void dpGridUnitDefaultWithDPR2() { |
| 33 |
UCUnits units; |
| 34 |
|
| 35 |
QCOMPARE(units.dp(1.0), 1.0f); |
| 36 |
QCOMPARE(units.dp(1.32), 1.5f); |
| 37 |
QCOMPARE(units.dp(1.72), 1.5f); |
| 38 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 39 |
QCOMPARE(units.dp(0.51), 0.5f); |
| 40 |
QCOMPARE(units.dp(0.9999), 1.0f); |
| 41 |
QCOMPARE(units.dp(1000.01), 1000.0f); |
| 42 |
} |
| 43 |
|
| 44 |
void guGridUnitDefaultWithDPR2() { |
| 45 |
UCUnits units; |
| 46 |
|
| 47 |
QCOMPARE(units.gu(0.5), 4.0f); |
| 48 |
QCOMPARE(units.gu(1), 8.0f); |
| 49 |
QCOMPARE(units.gu(1.5), 12.0f); |
| 50 |
QCOMPARE(units.gu(2), 16.0f); |
| 51 |
QCOMPARE(units.gu(4), 32.0f); |
| 52 |
QCOMPARE(units.gu(100000), 800000.0f); |
| 53 |
QCOMPARE(units.gu(150.51983), 1204.0f); |
| 54 |
} |
| 55 |
|
| 56 |
void dpGridUnitEightWithDPR2() { |
| 57 |
UCUnits units; |
| 58 |
units.setGridUnit(8); |
| 59 |
|
| 60 |
QCOMPARE(units.dp(1.0), 0.5f); |
| 61 |
QCOMPARE(units.dp(1.32), 0.5f); |
| 62 |
QCOMPARE(units.dp(1.72), 1.0f); |
| 63 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 64 |
QCOMPARE(units.dp(0.51), 0.5f); |
| 65 |
QCOMPARE(units.dp(0.9999), 0.5f); |
| 66 |
QCOMPARE(units.dp(1000.01), 500.0f); |
| 67 |
} |
| 68 |
|
| 69 |
void guGridUnitEightWithDPR2() { |
| 70 |
UCUnits units; |
| 71 |
units.setGridUnit(8); |
| 72 |
|
| 73 |
QCOMPARE(units.gu(0.5), 2.0f); |
| 74 |
QCOMPARE(units.gu(1), 4.0f); |
| 75 |
QCOMPARE(units.gu(1.5), 6.0f); |
| 76 |
QCOMPARE(units.gu(2), 8.0f); |
| 77 |
QCOMPARE(units.gu(4), 16.0f); |
| 78 |
QCOMPARE(units.gu(100000), 400000.0f); |
| 79 |
QCOMPARE(units.gu(150.51983), 602.0f); |
| 80 |
} |
| 81 |
|
| 82 |
void dpGridUnitSixteenWithDPR2() { |
| 83 |
UCUnits units; |
| 84 |
units.setGridUnit(16); |
| 85 |
/* This testcase covers unit calculations when the Qt device pixel ratio is 2. |
| 86 |
* |
| 87 |
*/ |
| 88 |
|
| 89 |
QCOMPARE(units.dp(1.0), 1.0f); |
| 90 |
QCOMPARE(units.dp(1.32), 1.5f); |
| 91 |
QCOMPARE(units.dp(1.72), 1.5f); |
| 92 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 93 |
QCOMPARE(units.dp(0.51), 0.5f); |
| 94 |
QCOMPARE(units.dp(0.9999), 1.0f); |
| 95 |
QCOMPARE(units.dp(1000.01), 1000.0f); |
| 96 |
} |
| 97 |
|
| 98 |
void guGridUnitSixteenWithDPR2() { |
| 99 |
UCUnits units; |
| 100 |
units.setGridUnit(16); |
| 101 |
|
| 102 |
QCOMPARE(units.gu(0.5), 4.0f); |
| 103 |
QCOMPARE(units.gu(1), 8.0f); |
| 104 |
QCOMPARE(units.gu(1.5), 12.0f); |
| 105 |
QCOMPARE(units.gu(2), 16.0f); |
| 106 |
QCOMPARE(units.gu(4), 32.0f); |
| 107 |
QCOMPARE(units.gu(100000), 800000.0f); |
| 108 |
QCOMPARE(units.gu(150.51983), 1204.0f); |
| 109 |
} |
| 110 |
|
| 111 |
void dpGridUnitTwentyWithDPR2() { |
| 112 |
UCUnits units; |
| 113 |
units.setGridUnit(20); |
| 114 |
|
| 115 |
QCOMPARE(units.dp(1.0), 1.0f); |
| 116 |
QCOMPARE(units.dp(1.32), 1.5f); |
| 117 |
QCOMPARE(units.dp(1.72), 1.5f); |
| 118 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 119 |
QCOMPARE(units.dp(0.51), 0.5f); |
| 120 |
QCOMPARE(units.dp(0.9999), 1.0f); |
| 121 |
QCOMPARE(units.dp(1000.01), 1250.0f); |
| 122 |
} |
| 123 |
|
| 124 |
void guGridUnitTenWithDPR2() { |
| 125 |
UCUnits units; |
| 126 |
units.setGridUnit(10); |
| 127 |
|
| 128 |
QCOMPARE(units.gu(0.5), 2.5f); |
| 129 |
QCOMPARE(units.gu(1), 5.0f); |
| 130 |
QCOMPARE(units.gu(1.5), 7.5f); |
| 131 |
QCOMPARE(units.gu(2), 10.0f); |
| 132 |
QCOMPARE(units.gu(4), 20.0f); |
| 133 |
QCOMPARE(units.gu(100000), 500000.0f); |
| 134 |
QCOMPARE(units.gu(150.51983), 752.5f); |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
//QTEST_MAIN(tst_UCUnitsDPR2) - want to set custom env var, so need to use actual code: |
| 139 |
int main(int argc, char *argv[]) |
| 140 |
{ |
| 141 |
qputenv("QT_SCALE_FACTOR", "2"); |
| 142 |
qunsetenv("GRID_UNIT_PX"); |
| 143 |
|
| 144 |
QGuiApplication app(argc, argv); |
| 145 |
app.setAttribute(Qt::AA_Use96Dpi, true); |
| 146 |
tst_UCUnitsDPR2 tc; |
| 147 |
return QTest::qExec(&tc, argc, argv); |
| 148 |
} |
| 149 |
|
| 150 |
#include "tst_units_dpr2.moc" |