| 1 |
/* |
| 2 |
* Copyright 2012 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU 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 |
* Author: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <QtTest/QtTest> |
| 20 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 21 |
#include <LomiriToolkit-Qt6/private/ucunits_p.h> |
| 22 |
#else |
| 23 |
#include <LomiriToolkit/private/ucunits_p.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
UT_USE_NAMESPACE |
| 27 |
|
| 28 |
class tst_UCUnits: public QObject |
| 29 |
{ |
| 30 |
Q_OBJECT |
| 31 |
|
| 32 |
private Q_SLOTS: |
| 33 |
|
| 34 |
void defaultGridUnit() { |
| 35 |
qunsetenv("GRID_UNIT_PX"); |
| 36 |
UCUnits units; |
| 37 |
QCOMPARE(units.gridUnit(), 8.0f); |
| 38 |
} |
| 39 |
|
| 40 |
void setGridUnit() { |
| 41 |
UCUnits units; |
| 42 |
units.setGridUnit(0.0); |
| 43 |
QCOMPARE(units.gridUnit(), 0.0f); |
| 44 |
units.setGridUnit(42.0); |
| 45 |
QCOMPARE(units.gridUnit(), 42.0f); |
| 46 |
} |
| 47 |
|
| 48 |
void gridUnitEnvironmentVariable() { |
| 49 |
QByteArray gridUnit = QString::number(11).toLocal8Bit(); |
| 50 |
qputenv("GRID_UNIT_PX", gridUnit); |
| 51 |
UCUnits units; |
| 52 |
QCOMPARE(units.gridUnit(), 11.0); |
| 53 |
qunsetenv("GRID_UNIT_PX"); |
| 54 |
} |
| 55 |
|
| 56 |
void dpGridUnitDefault() { |
| 57 |
UCUnits units; |
| 58 |
|
| 59 |
QCOMPARE(units.dp(1.0), 1.0f); |
| 60 |
QCOMPARE(units.dp(1.32), 1.0f); |
| 61 |
QCOMPARE(units.dp(1.72), 2.0f); |
| 62 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 63 |
QCOMPARE(units.dp(0.51), 1.0f); |
| 64 |
QCOMPARE(units.dp(0.9999), 1.0f); |
| 65 |
QCOMPARE(units.dp(1000.01), 1000.0f); |
| 66 |
} |
| 67 |
|
| 68 |
void guGridUnitDefault() { |
| 69 |
UCUnits units; |
| 70 |
|
| 71 |
QCOMPARE(units.gu(1.0), 8.0f); |
| 72 |
QCOMPARE(units.gu(1.32), 11.0f); |
| 73 |
QCOMPARE(units.gu(1.72), 14.0f); |
| 74 |
QCOMPARE(units.gu(0.23), 2.0f); |
| 75 |
QCOMPARE(units.gu(0.51), 4.0f); |
| 76 |
QCOMPARE(units.gu(0.9999), 8.0f); |
| 77 |
QCOMPARE(units.gu(1000.01), 8000.0f); |
| 78 |
} |
| 79 |
void dpGridUnitTen() { |
| 80 |
UCUnits units; |
| 81 |
units.setGridUnit(10); |
| 82 |
|
| 83 |
QCOMPARE(units.dp(1.0), 1.0f); |
| 84 |
QCOMPARE(units.dp(1.32), 1.0f); |
| 85 |
QCOMPARE(units.dp(1.72), 2.0f); |
| 86 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 87 |
QCOMPARE(units.dp(0.51), 1.0f); |
| 88 |
QCOMPARE(units.dp(0.9999), 1.0f); |
| 89 |
QCOMPARE(units.dp(1000.01), 1250.0f); |
| 90 |
} |
| 91 |
|
| 92 |
void guGridUnitTen() { |
| 93 |
UCUnits units; |
| 94 |
units.setGridUnit(10); |
| 95 |
|
| 96 |
QCOMPARE(units.gu(0.5), 5.0f); |
| 97 |
QCOMPARE(units.gu(1), 10.0f); |
| 98 |
QCOMPARE(units.gu(1.5), 15.0f); |
| 99 |
QCOMPARE(units.gu(2), 20.0f); |
| 100 |
QCOMPARE(units.gu(4), 40.0f); |
| 101 |
QCOMPARE(units.gu(100000), 1000000.0f); |
| 102 |
QCOMPARE(units.gu(150.51983), 1505.0f); |
| 103 |
} |
| 104 |
|
| 105 |
void dpGridUnitSixteen() { |
| 106 |
UCUnits units; |
| 107 |
units.setGridUnit(16); |
| 108 |
|
| 109 |
QCOMPARE(units.dp(1.0), 2.0f); |
| 110 |
QCOMPARE(units.dp(1.32), 3.0f); |
| 111 |
QCOMPARE(units.dp(1.72), 3.0f); |
| 112 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 113 |
QCOMPARE(units.dp(0.51), 1.0f); |
| 114 |
QCOMPARE(units.dp(0.9999), 2.0f); |
| 115 |
QCOMPARE(units.dp(1000.01), 2000.0f); |
| 116 |
} |
| 117 |
|
| 118 |
void guGridUnitSixteen() { |
| 119 |
UCUnits units; |
| 120 |
units.setGridUnit(16); |
| 121 |
|
| 122 |
QCOMPARE(units.gu(0.5), 8.0f); |
| 123 |
QCOMPARE(units.gu(1), 16.0f); |
| 124 |
QCOMPARE(units.gu(1.5), 24.0f); |
| 125 |
QCOMPARE(units.gu(2), 32.0f); |
| 126 |
QCOMPARE(units.gu(4), 64.0f); |
| 127 |
QCOMPARE(units.gu(100000), 1600000.0f); |
| 128 |
QCOMPARE(units.gu(150.51983), 2408.0f); |
| 129 |
} |
| 130 |
|
| 131 |
void dpGridUnitEighteen() { |
| 132 |
UCUnits units; |
| 133 |
units.setGridUnit(18); |
| 134 |
|
| 135 |
QCOMPARE(units.dp(1.0), 2.0f); |
| 136 |
QCOMPARE(units.dp(1.32), 3.0f); |
| 137 |
QCOMPARE(units.dp(1.72), 3.0f); |
| 138 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 139 |
QCOMPARE(units.dp(0.51), 1.0f); |
| 140 |
QCOMPARE(units.dp(0.9999), 2.0f); |
| 141 |
QCOMPARE(units.dp(1000.01), 2250.0f); |
| 142 |
} |
| 143 |
|
| 144 |
void dpGridUnitTwenty() { |
| 145 |
UCUnits units; |
| 146 |
units.setGridUnit(20); |
| 147 |
|
| 148 |
QCOMPARE(units.dp(1.0), 2.0f); |
| 149 |
QCOMPARE(units.dp(1.32), 3.0f); |
| 150 |
QCOMPARE(units.dp(1.72), 3.0f); |
| 151 |
QCOMPARE(units.dp(0.23), 0.0f); |
| 152 |
QCOMPARE(units.dp(0.51), 1.0f); |
| 153 |
QCOMPARE(units.dp(0.9999), 2.0f); |
| 154 |
QCOMPARE(units.dp(1000.01), 2500.0f); |
| 155 |
} |
| 156 |
|
| 157 |
void resolveEmpty() { |
| 158 |
UCUnits units; |
| 159 |
QString resolved; |
| 160 |
QString expected; |
| 161 |
|
| 162 |
resolved = units.resolveResource(QUrl("")); |
| 163 |
expected = QString(""); |
| 164 |
QCOMPARE(resolved, expected); |
| 165 |
|
| 166 |
units.setGridUnit(18); |
| 167 |
|
| 168 |
resolved = units.resolveResource(QUrl("")); |
| 169 |
expected = QString(""); |
| 170 |
QCOMPARE(resolved, expected); |
| 171 |
} |
| 172 |
|
| 173 |
void resolveNonExisting() { |
| 174 |
UCUnits units; |
| 175 |
QString resolved; |
| 176 |
QString expected; |
| 177 |
|
| 178 |
resolved = units.resolveResource(QUrl::fromLocalFile("non_existing.png")); |
| 179 |
expected = QString(""); |
| 180 |
QCOMPARE(resolved, expected); |
| 181 |
} |
| 182 |
|
| 183 |
void resolveDirectory() { |
| 184 |
UCUnits units; |
| 185 |
QString resolved; |
| 186 |
QString expected; |
| 187 |
|
| 188 |
resolved = units.resolveResource(QUrl::fromLocalFile("")); |
| 189 |
expected = QString(""); |
| 190 |
QCOMPARE(resolved, expected); |
| 191 |
} |
| 192 |
|
| 193 |
void resolveExactMatch() { |
| 194 |
UCUnits units; |
| 195 |
QString resolved; |
| 196 |
QString expected; |
| 197 |
|
| 198 |
units.setGridUnit(8); |
| 199 |
resolved = units.resolveResource(QUrl::fromLocalFile("exact_match.png")); |
| 200 |
expected = QString("1/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 201 |
QCOMPARE(resolved, expected); |
| 202 |
} |
| 203 |
|
| 204 |
void resolveExistsOnlyHigherGridUnit() { |
| 205 |
UCUnits units; |
| 206 |
QString resolved; |
| 207 |
QString expected; |
| 208 |
|
| 209 |
units.setGridUnit(10); |
| 210 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource_only_higher.png")); |
| 211 |
expected = QString("0.769231/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 212 |
QCOMPARE(resolved, expected); |
| 213 |
} |
| 214 |
|
| 215 |
void resolveExistsOnlyHigherGridUnitQrc() { |
| 216 |
UCUnits units; |
| 217 |
QString resolved; |
| 218 |
QString expected; |
| 219 |
|
| 220 |
units.setGridUnit(10); |
| 221 |
resolved = units.resolveResource(QUrl("qrc:/test/prefix/resource_only_higher.png")); |
| 222 |
|
| 223 |
expected = QString("0.769231/:/test/prefix/[email protected]"); |
| 224 |
QCOMPARE(resolved, expected); |
| 225 |
} |
| 226 |
|
| 227 |
void resolveExistsOnlySmallerGridUnit() { |
| 228 |
UCUnits units; |
| 229 |
QString resolved; |
| 230 |
QString expected; |
| 231 |
|
| 232 |
units.setGridUnit(10); |
| 233 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource_only_smaller.png")); |
| 234 |
expected = QString("1.42857/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 235 |
QCOMPARE(resolved, expected); |
| 236 |
} |
| 237 |
|
| 238 |
void resolveExistsOnlySmallerGridUnitQrc() { |
| 239 |
UCUnits units; |
| 240 |
QString resolved; |
| 241 |
QString expected; |
| 242 |
|
| 243 |
units.setGridUnit(10); |
| 244 |
resolved = units.resolveResource(QUrl("qrc:/test/prefix/resource_only_smaller.png")); |
| 245 |
expected = QString("1.42857/:/test/prefix/[email protected]"); |
| 246 |
QCOMPARE(resolved, expected); |
| 247 |
} |
| 248 |
|
| 249 |
void resolveExistsMultipleGridUnit() { |
| 250 |
UCUnits units; |
| 251 |
QString resolved; |
| 252 |
QString expected; |
| 253 |
|
| 254 |
units.setGridUnit(20); |
| 255 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource.png")); |
| 256 |
expected = QString("1.05263/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 257 |
QCOMPARE(resolved, expected); |
| 258 |
|
| 259 |
units.setGridUnit(19); |
| 260 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource.png")); |
| 261 |
expected = QString("1/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 262 |
QCOMPARE(resolved, expected); |
| 263 |
|
| 264 |
units.setGridUnit(18); |
| 265 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource.png")); |
| 266 |
expected = QString("1/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 267 |
QCOMPARE(resolved, expected); |
| 268 |
|
| 269 |
units.setGridUnit(17); |
| 270 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource.png")); |
| 271 |
expected = QString("0.944444/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 272 |
QCOMPARE(resolved, expected); |
| 273 |
|
| 274 |
units.setGridUnit(9); |
| 275 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource.png")); |
| 276 |
expected = QString("0.9/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 277 |
QCOMPARE(resolved, expected); |
| 278 |
|
| 279 |
units.setGridUnit(8); |
| 280 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource.png")); |
| 281 |
expected = QString("1/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 282 |
QCOMPARE(resolved, expected); |
| 283 |
|
| 284 |
units.setGridUnit(7); |
| 285 |
resolved = units.resolveResource(QUrl::fromLocalFile("resource.png")); |
| 286 |
expected = QString("0.875/" + QDir::currentPath() + QDir::separator() + "[email protected]"); |
| 287 |
QCOMPARE(resolved, expected); |
| 288 |
} |
| 289 |
}; |
| 290 |
|
| 291 |
QTEST_MAIN(tst_UCUnits) |
| 292 |
|
| 293 |
#include "tst_units.moc" |