/
kconfigtest.cpp
1954 lines (1676 loc) · 74.2 KB
/
kconfigtest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of the KDE libraries
Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// Qt5 TODO: re-enable. No point in doing it before, it breaks on QString::fromUtf8(QByteArray), which exists in qt5.
#undef QT_NO_CAST_FROM_BYTEARRAY
#include "kconfigtest.h"
#include "helper.h"
#include "config-kconfig.h"
#include <QtTest>
#include <qtemporarydir.h>
#include <QStandardPaths>
#include <kdesktopfile.h>
#include <ksharedconfig.h>
#include <kconfiggroup.h>
#include <kconfigwatcher.h>
#ifdef Q_OS_UNIX
#include <utime.h>
#endif
#ifndef Q_OS_WIN
#include <unistd.h> // getuid
#endif
KCONFIGGROUP_DECLARE_ENUM_QOBJECT(KConfigTest, Testing)
KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(KConfigTest, Flags)
QTEST_MAIN(KConfigTest)
Q_DECLARE_METATYPE(KConfigGroup)
static QString homePath()
{
#ifdef Q_OS_WIN
return QDir::homePath();
#else
// Don't use QDir::homePath() on Unix, it removes any trailing slash, while KConfig uses $HOME.
return QString::fromLocal8Bit(qgetenv("HOME"));
#endif
}
#define BOOLENTRY1 true
#define BOOLENTRY2 false
#define STRINGENTRY1 "hello"
#define STRINGENTRY2 " hello"
#define STRINGENTRY3 "hello "
#define STRINGENTRY4 " hello "
#define STRINGENTRY5 " "
#define STRINGENTRY6 ""
#define UTF8BITENTRY "Hello äöü"
#define TRANSLATEDSTRINGENTRY1 "bonjour"
#define BYTEARRAYENTRY QByteArray( "\x00\xff\x7f\x3c abc\x00\x00", 10 )
#define ESCAPEKEY " []\0017[]==]"
#define ESCAPEENTRY "[]\170[]]=3=]\\] "
#define DOUBLEENTRY 123456.78912345
#define FLOATENTRY 123.567f
#define POINTENTRY QPoint( 4351, 1235 )
#define SIZEENTRY QSize( 10, 20 )
#define RECTENTRY QRect( 10, 23, 5321, 13 )
#define DATETIMEENTRY QDateTime( QDate( 2002, 06, 23 ), QTime( 12, 55, 40 ) )
#define STRINGLISTENTRY (QStringList( "Hello," ) << " World")
#define STRINGLISTEMPTYENTRY QStringList()
#define STRINGLISTJUSTEMPTYELEMENT QStringList(QString())
#define STRINGLISTEMPTYTRAINLINGELEMENT (QStringList( "Hi" ) << QString())
#define STRINGLISTESCAPEODDENTRY (QStringList( "Hello\\\\\\" ) << "World")
#define STRINGLISTESCAPEEVENENTRY (QStringList( "Hello\\\\\\\\" ) << "World")
#define STRINGLISTESCAPECOMMAENTRY (QStringList( "Hel\\\\\\,\\\\,\\,\\\\\\\\,lo" ) << "World")
#define INTLISTENTRY1 QList<int>() << 1 << 2 << 3 << 4
#define BYTEARRAYLISTENTRY1 QList<QByteArray>() << "" << "1,2" << "end"
#define VARIANTLISTENTRY (QVariantList() << true << false << QString("joe") << 10023)
#define VARIANTLISTENTRY2 (QVariantList() << POINTENTRY << SIZEENTRY)
#define HOMEPATH QString(homePath()+"/foo")
#define HOMEPATHESCAPE QString(homePath()+"/foo/$HOME")
#define DOLLARGROUP "$i"
#define TEST_SUBDIR "kconfigtest_subdir/"
static inline QString testConfigDir() {
return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/" TEST_SUBDIR;
}
static inline QString kdeGlobalsPath() {
return QDir::cleanPath(testConfigDir() + "..") + "/kdeglobals";
}
void KConfigTest::initTestCase()
{
// ensure we don't use files in the real config directory
QStandardPaths::setTestModeEnabled(true);
qRegisterMetaType<KConfigGroup>();
// to make sure all files from a previous failed run are deleted
cleanupTestCase();
KSharedConfigPtr mainConfig = KSharedConfig::openConfig();
mainConfig->group("Main").writeEntry("Key", "Value");
mainConfig->sync();
KConfig sc(TEST_SUBDIR "kconfigtest");
KConfigGroup cg(&sc, "AAA");
cg.writeEntry("stringEntry1", STRINGENTRY1,
KConfig::Persistent | KConfig::Global);
cg.deleteEntry("stringEntry2", KConfig::Global);
cg = KConfigGroup(&sc, "Hello");
cg.writeEntry("boolEntry1", BOOLENTRY1);
cg.writeEntry("boolEntry2", BOOLENTRY2);
QByteArray data(UTF8BITENTRY);
QCOMPARE(data.size(), 12); // the source file is in utf8
QCOMPARE(QString::fromUtf8(data).length(), 9);
cg.writeEntry("Test", data);
cg.writeEntry("bytearrayEntry", BYTEARRAYENTRY);
cg.writeEntry(ESCAPEKEY, ESCAPEENTRY);
cg.writeEntry("emptyEntry", "");
cg.writeEntry("stringEntry1", STRINGENTRY1);
cg.writeEntry("stringEntry2", STRINGENTRY2);
cg.writeEntry("stringEntry3", STRINGENTRY3);
cg.writeEntry("stringEntry4", STRINGENTRY4);
cg.writeEntry("stringEntry5", STRINGENTRY5);
cg.writeEntry("urlEntry1", QUrl(QStringLiteral("http://qt-project.org")));
cg.writeEntry("keywith=equalsign", STRINGENTRY1);
cg.deleteEntry("stringEntry5");
cg.deleteEntry("stringEntry6"); // deleting a nonexistent entry
cg.writeEntry("byteArrayEntry1", QByteArray(STRINGENTRY1),
KConfig::Global | KConfig::Persistent);
cg.writeEntry("doubleEntry1", DOUBLEENTRY);
cg.writeEntry("floatEntry1", FLOATENTRY);
sc.deleteGroup("deleteMe"); // deleting a nonexistent group
cg = KConfigGroup(&sc, "Complex Types");
cg.writeEntry("rectEntry", RECTENTRY);
cg.writeEntry("pointEntry", POINTENTRY);
cg.writeEntry("sizeEntry", SIZEENTRY);
cg.writeEntry("dateTimeEntry", DATETIMEENTRY);
cg.writeEntry("dateEntry", DATETIMEENTRY.date());
KConfigGroup ct = cg;
cg = KConfigGroup(&ct, "Nested Group 1");
cg.writeEntry("stringentry1", STRINGENTRY1);
cg = KConfigGroup(&ct, "Nested Group 2");
cg.writeEntry("stringEntry2", STRINGENTRY2);
cg = KConfigGroup(&cg, "Nested Group 2.1");
cg.writeEntry("stringEntry3", STRINGENTRY3);
cg = KConfigGroup(&ct, "Nested Group 3");
cg.writeEntry("stringEntry3", STRINGENTRY3);
cg = KConfigGroup(&sc, "List Types");
cg.writeEntry("listOfIntsEntry1", INTLISTENTRY1);
cg.writeEntry("listOfByteArraysEntry1", BYTEARRAYLISTENTRY1);
cg.writeEntry("stringListEntry", STRINGLISTENTRY);
cg.writeEntry("stringListEmptyEntry", STRINGLISTEMPTYENTRY);
cg.writeEntry("stringListJustEmptyElement", STRINGLISTJUSTEMPTYELEMENT);
cg.writeEntry("stringListEmptyTrailingElement", STRINGLISTEMPTYTRAINLINGELEMENT);
cg.writeEntry("stringListEscapeOddEntry", STRINGLISTESCAPEODDENTRY);
cg.writeEntry("stringListEscapeEvenEntry", STRINGLISTESCAPEEVENENTRY);
cg.writeEntry("stringListEscapeCommaEntry", STRINGLISTESCAPECOMMAENTRY);
cg.writeEntry("variantListEntry", VARIANTLISTENTRY);
cg = KConfigGroup(&sc, "Path Type");
cg.writePathEntry("homepath", HOMEPATH);
cg.writePathEntry("homepathescape", HOMEPATHESCAPE);
cg = KConfigGroup(&sc, "Enum Types");
#if defined(_MSC_VER) && _MSC_VER == 1600
cg.writeEntry("dummy", 42);
#else
//Visual C++ 2010 throws an Internal Compiler Error here
cg.writeEntry("enum-10", Tens);
cg.writeEntry("enum-100", Hundreds);
cg.writeEntry("flags-bit0", Flags(bit0));
cg.writeEntry("flags-bit0-bit1", Flags(bit0 | bit1));
#endif
cg = KConfigGroup(&sc, "ParentGroup");
KConfigGroup cg1(&cg, "SubGroup1");
cg1.writeEntry("somestring", "somevalue");
cg.writeEntry("parentgrpstring", "somevalue");
KConfigGroup cg2(&cg, "SubGroup2");
cg2.writeEntry("substring", "somevalue");
KConfigGroup cg3(&cg, "SubGroup/3");
cg3.writeEntry("sub3string", "somevalue");
QVERIFY(sc.isDirty());
QVERIFY(sc.sync());
QVERIFY(!sc.isDirty());
QVERIFY2(QFile::exists(testConfigDir() + QStringLiteral("/kconfigtest")),
qPrintable(testConfigDir() + QStringLiteral("/kconfigtest must exist")));
QVERIFY2(QFile::exists(kdeGlobalsPath()),
qPrintable(kdeGlobalsPath() + QStringLiteral(" must exist")));
KConfig sc1(TEST_SUBDIR "kdebugrc", KConfig::SimpleConfig);
KConfigGroup sg0(&sc1, "0");
sg0.writeEntry("AbortFatal", false);
sg0.writeEntry("WarnOutput", 0);
sg0.writeEntry("FatalOutput", 0);
QVERIFY(sc1.sync());
//Setup stuff to test KConfig::addConfigSources()
KConfig devcfg(TEST_SUBDIR "specificrc");
KConfigGroup devonlygrp(&devcfg, "Specific Only Group");
devonlygrp.writeEntry("ExistingEntry", "DevValue");
KConfigGroup devandbasegrp(&devcfg, "Shared Group");
devandbasegrp.writeEntry("SomeSharedEntry", "DevValue");
devandbasegrp.writeEntry("SomeSpecificOnlyEntry", "DevValue");
QVERIFY(devcfg.sync());
KConfig basecfg(TEST_SUBDIR "baserc");
KConfigGroup basegrp(&basecfg, "Base Only Group");
basegrp.writeEntry("ExistingEntry", "BaseValue");
KConfigGroup baseanddevgrp(&basecfg, "Shared Group");
baseanddevgrp.writeEntry("SomeSharedEntry", "BaseValue");
baseanddevgrp.writeEntry("SomeBaseOnlyEntry", "BaseValue");
QVERIFY(basecfg.sync());
KConfig gecfg(TEST_SUBDIR "groupescapetest", KConfig::SimpleConfig);
cg = KConfigGroup(&gecfg, DOLLARGROUP);
cg.writeEntry("entry", "doesntmatter");
}
void KConfigTest::cleanupTestCase()
{
//ensure we don't delete the real directory
QDir localConfig(testConfigDir());
//qDebug() << "Erasing" << localConfig;
if (localConfig.exists()) {
QVERIFY(localConfig.removeRecursively());
}
QVERIFY(!localConfig.exists());
if (QFile::exists(kdeGlobalsPath())) {
QVERIFY(QFile::remove(kdeGlobalsPath()));
}
}
static QList<QByteArray> readLinesFrom(const QString &path)
{
QFile file(path);
const bool opened = file.open(QIODevice::ReadOnly | QIODevice::Text);
QList<QByteArray> lines;
if (!opened) {
QWARN(qPrintable(QLatin1String("Failed to open ") + path));
return lines;
}
QByteArray line;
do {
line = file.readLine();
if (!line.isEmpty()) {
lines.append(line);
}
} while (!line.isEmpty());
return lines;
}
static QList<QByteArray> readLines(const char *fileName = TEST_SUBDIR "kconfigtest")
{
const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
Q_ASSERT(!path.isEmpty());
return readLinesFrom(path + '/' + fileName);
}
// see also testDefaults, which tests reverting with a defaults (global) file available
void KConfigTest::testDirtyAfterRevert()
{
KConfig sc(TEST_SUBDIR "kconfigtest_revert");
KConfigGroup cg(&sc, "Hello");
cg.revertToDefault("does_not_exist");
QVERIFY(!sc.isDirty());
cg.writeEntry("Test", "Correct");
QVERIFY(sc.isDirty());
sc.sync();
QVERIFY(!sc.isDirty());
cg.revertToDefault("Test");
QVERIFY(sc.isDirty());
QVERIFY(sc.sync());
QVERIFY(!sc.isDirty());
cg.revertToDefault("Test");
QVERIFY(!sc.isDirty());
}
void KConfigTest::testRevertAllEntries()
{
// this tests the case were we revert (delete) all entries in a file,
// leaving a blank file
{
KConfig sc(TEST_SUBDIR "konfigtest2", KConfig::SimpleConfig);
KConfigGroup cg(&sc, "Hello");
cg.writeEntry("Test", "Correct");
}
{
KConfig sc(TEST_SUBDIR "konfigtest2", KConfig::SimpleConfig);
KConfigGroup cg(&sc, "Hello");
QCOMPARE(cg.readEntry("Test", "Default"), QString("Correct"));
cg.revertToDefault("Test");
}
KConfig sc(TEST_SUBDIR "konfigtest2", KConfig::SimpleConfig);
KConfigGroup cg(&sc, "Hello");
QCOMPARE(cg.readEntry("Test", "Default"), QString("Default"));
}
void KConfigTest::testSimple()
{
// kdeglobals (which was created in initTestCase) must be found this way:
const QStringList kdeglobals = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("kdeglobals"));
QVERIFY(!kdeglobals.isEmpty());
KConfig sc2(TEST_SUBDIR "kconfigtest");
QCOMPARE(sc2.name(), QString(TEST_SUBDIR "kconfigtest"));
// make sure groupList() isn't returning something it shouldn't
const QStringList lstGroup = sc2.groupList();
for (const QString &group : lstGroup) {
QVERIFY(!group.isEmpty() && group != "<default>");
QVERIFY(!group.contains(QChar(0x1d)));
}
KConfigGroup sc3(&sc2, "AAA");
QVERIFY(sc3.hasKey("stringEntry1")); // from kdeglobals
QVERIFY(!sc3.isEntryImmutable("stringEntry1"));
QCOMPARE(sc3.readEntry("stringEntry1"), QString(STRINGENTRY1));
QVERIFY(!sc3.hasKey("stringEntry2"));
QCOMPARE(sc3.readEntry("stringEntry2", QString("bla")), QString("bla"));
QVERIFY(!sc3.hasDefault("stringEntry1"));
sc3 = KConfigGroup(&sc2, "Hello");
QCOMPARE(sc3.readEntry("Test", QByteArray()), QByteArray(UTF8BITENTRY));
QCOMPARE(sc3.readEntry("bytearrayEntry", QByteArray()), BYTEARRAYENTRY);
QCOMPARE(sc3.readEntry(ESCAPEKEY), QString(ESCAPEENTRY));
QCOMPARE(sc3.readEntry("Test", QString()), QString::fromUtf8(UTF8BITENTRY));
QCOMPARE(sc3.readEntry("emptyEntry"/*, QString("Fietsbel")*/), QLatin1String(""));
QCOMPARE(sc3.readEntry("emptyEntry", QString("Fietsbel")).isEmpty(), true);
QCOMPARE(sc3.readEntry("stringEntry1"), QString(STRINGENTRY1));
QCOMPARE(sc3.readEntry("stringEntry2"), QString(STRINGENTRY2));
QCOMPARE(sc3.readEntry("stringEntry3"), QString(STRINGENTRY3));
QCOMPARE(sc3.readEntry("stringEntry4"), QString(STRINGENTRY4));
QVERIFY(!sc3.hasKey("stringEntry5"));
QCOMPARE(sc3.readEntry("stringEntry5", QString("test")), QString("test"));
QVERIFY(!sc3.hasKey("stringEntry6"));
QCOMPARE(sc3.readEntry("stringEntry6", QString("foo")), QString("foo"));
QCOMPARE(sc3.readEntry("urlEntry1", QUrl()), QUrl("http://qt-project.org"));
QCOMPARE(sc3.readEntry("boolEntry1", BOOLENTRY1), BOOLENTRY1);
QCOMPARE(sc3.readEntry("boolEntry2", false), BOOLENTRY2);
QCOMPARE(sc3.readEntry("keywith=equalsign", QString("wrong")), QString(STRINGENTRY1));
QCOMPARE(sc3.readEntry("byteArrayEntry1", QByteArray()),
QByteArray(STRINGENTRY1));
QCOMPARE(sc3.readEntry("doubleEntry1", 0.0), DOUBLEENTRY);
QCOMPARE(sc3.readEntry("floatEntry1", 0.0f), FLOATENTRY);
}
void KConfigTest::testDefaults()
{
KConfig config(TEST_SUBDIR "defaulttest", KConfig::NoGlobals);
const QString defaultsFile = TEST_SUBDIR "defaulttest.defaults";
KConfig defaults(defaultsFile, KConfig::SimpleConfig);
const QString Default(QStringLiteral("Default"));
const QString NotDefault(QStringLiteral("Not Default"));
const QString Value1(STRINGENTRY1);
const QString Value2(STRINGENTRY2);
KConfigGroup group = defaults.group("any group");
group.writeEntry("entry1", Default);
QVERIFY(group.sync());
group = config.group("any group");
group.writeEntry("entry1", Value1);
group.writeEntry("entry2", Value2);
QVERIFY(group.sync());
config.addConfigSources(QStringList() << QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + '/' + defaultsFile);
config.setReadDefaults(true);
QCOMPARE(group.readEntry("entry1", QString()), Default);
QCOMPARE(group.readEntry("entry2", NotDefault), NotDefault); // no default for entry2
config.setReadDefaults(false);
QCOMPARE(group.readEntry("entry1", Default), Value1);
QCOMPARE(group.readEntry("entry2", NotDefault), Value2);
group.revertToDefault("entry1");
QCOMPARE(group.readEntry("entry1", QString()), Default);
group.revertToDefault("entry2");
QCOMPARE(group.readEntry("entry2", QString()), QString());
// TODO test reverting localized entries
Q_ASSERT(config.isDirty());
group.sync();
// Check that everything is OK on disk, too
KConfig reader(TEST_SUBDIR "defaulttest", KConfig::NoGlobals);
reader.addConfigSources(QStringList() << QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + '/' + defaultsFile);
KConfigGroup readerGroup = reader.group("any group");
QCOMPARE(readerGroup.readEntry("entry1", QString()), Default);
QCOMPARE(readerGroup.readEntry("entry2", QString()), QString());
}
void KConfigTest::testLocale()
{
KConfig config(TEST_SUBDIR "kconfigtest.locales", KConfig::SimpleConfig);
const QString Translated(TRANSLATEDSTRINGENTRY1);
const QString Untranslated(STRINGENTRY1);
KConfigGroup group = config.group("Hello");
group.writeEntry("stringEntry1", Untranslated);
config.setLocale(QStringLiteral("fr"));
group.writeEntry("stringEntry1", Translated, KConfig::Localized | KConfig::Persistent);
QVERIFY(config.sync());
QCOMPARE(group.readEntry("stringEntry1", QString()), Translated);
QCOMPARE(group.readEntryUntranslated("stringEntry1"), Untranslated);
config.setLocale(QStringLiteral("C")); // strings written in the "C" locale are written as nonlocalized
group.writeEntry("stringEntry1", Untranslated, KConfig::Localized | KConfig::Persistent);
QVERIFY(config.sync());
QCOMPARE(group.readEntry("stringEntry1", QString()), Untranslated);
}
void KConfigTest::testEncoding()
{
QString groupstr = QString::fromUtf8("UTF-8:\xc3\xb6l");
KConfig c(TEST_SUBDIR "kconfigtestencodings");
KConfigGroup cg(&c, groupstr);
cg.writeEntry("key", "value");
QVERIFY(c.sync());
QList<QByteArray> lines = readLines(TEST_SUBDIR "kconfigtestencodings");
QCOMPARE(lines.count(), 2);
QCOMPARE(lines.first(), QByteArray("[UTF-8:\xc3\xb6l]\n"));
KConfig c2(TEST_SUBDIR "kconfigtestencodings");
KConfigGroup cg2(&c2, groupstr);
QVERIFY(cg2.readEntry("key") == QByteArray("value"));
QVERIFY(c2.groupList().contains(groupstr));
}
void KConfigTest::testLists()
{
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup sc3(&sc2, "List Types");
QCOMPARE(sc3.readEntry(QString("stringListEntry"), QStringList()),
STRINGLISTENTRY);
QCOMPARE(sc3.readEntry(QString("stringListEmptyEntry"), QStringList("wrong")),
STRINGLISTEMPTYENTRY);
QCOMPARE(sc3.readEntry(QString("stringListJustEmptyElement"), QStringList()),
STRINGLISTJUSTEMPTYELEMENT);
QCOMPARE(sc3.readEntry(QString("stringListEmptyTrailingElement"), QStringList()),
STRINGLISTEMPTYTRAINLINGELEMENT);
QCOMPARE(sc3.readEntry(QString("stringListEscapeOddEntry"), QStringList()),
STRINGLISTESCAPEODDENTRY);
QCOMPARE(sc3.readEntry(QString("stringListEscapeEvenEntry"), QStringList()),
STRINGLISTESCAPEEVENENTRY);
QCOMPARE(sc3.readEntry(QString("stringListEscapeCommaEntry"), QStringList()),
STRINGLISTESCAPECOMMAENTRY);
QCOMPARE(sc3.readEntry("listOfIntsEntry1"), QString::fromLatin1("1,2,3,4"));
QList<int> expectedIntList = INTLISTENTRY1;
QVERIFY(sc3.readEntry("listOfIntsEntry1", QList<int>()) == expectedIntList);
QCOMPARE(QVariant(sc3.readEntry("variantListEntry", VARIANTLISTENTRY)).toStringList(),
QVariant(VARIANTLISTENTRY).toStringList());
QCOMPARE(sc3.readEntry("listOfByteArraysEntry1", QList<QByteArray>()), BYTEARRAYLISTENTRY1);
}
void KConfigTest::testPath()
{
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup sc3(&sc2, "Path Type");
QCOMPARE(sc3.readPathEntry("homepath", QString()), HOMEPATH);
QCOMPARE(sc3.readPathEntry("homepathescape", QString()), HOMEPATHESCAPE);
QCOMPARE(sc3.entryMap()["homepath"], HOMEPATH);
qputenv("WITHSLASH", "/a/");
{
QFile file(testConfigDir() + "/pathtest");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out.setCodec("UTF-8");
out << "[Test Group]" << endl
<< "homePath=$HOME/foo" << endl
<< "homePath2=file://$HOME/foo" << endl
<< "withSlash=$WITHSLASH/foo" << endl
<< "withSlash2=$WITHSLASH" << endl
<< "withBraces[$e]=file://${HOME}/foo" << endl
<< "URL[$e]=file://${HOME}/foo" << endl
<< "hostname[$e]=$(hostname)" << endl
<< "escapes=aaa,bb/b,ccc\\,ccc" << endl
<< "noeol=foo" // no EOL
;
}
KConfig cf2(TEST_SUBDIR "pathtest");
KConfigGroup group = cf2.group("Test Group");
QVERIFY(group.hasKey("homePath"));
QCOMPARE(group.readPathEntry("homePath", QString()), HOMEPATH);
QVERIFY(group.hasKey("homePath2"));
QCOMPARE(group.readPathEntry("homePath2", QString()), QString("file://" + HOMEPATH));
QVERIFY(group.hasKey("withSlash"));
QCOMPARE(group.readPathEntry("withSlash", QString()), QStringLiteral("/a//foo"));
QVERIFY(group.hasKey("withSlash2"));
QCOMPARE(group.readPathEntry("withSlash2", QString()), QStringLiteral("/a/"));
QVERIFY(group.hasKey("withBraces"));
QCOMPARE(group.readPathEntry("withBraces", QString()), QString("file://" + HOMEPATH));
QVERIFY(group.hasKey("URL"));
QCOMPARE(group.readEntry("URL", QString()), QString("file://" + HOMEPATH));
QVERIFY(group.hasKey("hostname"));
QCOMPARE(group.readEntry("hostname", QString()), QStringLiteral("(hostname)")); // the $ got removed because empty var name
QVERIFY(group.hasKey("noeol"));
QCOMPARE(group.readEntry("noeol", QString()), QString("foo"));
const auto val = QStringList { QStringLiteral("aaa"), QStringLiteral("bb/b"), QStringLiteral("ccc,ccc")};
QCOMPARE(group.readPathEntry("escapes", QStringList()), val);
}
void KConfigTest::testPersistenceOfExpandFlagForPath()
{
// This test checks that a path entry starting with $HOME is still flagged
// with the expand flag after the config was altered without rewriting the
// path entry.
// 1st step: Open the config, add a new dummy entry and then sync the config
// back to the storage.
{
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup sc3(&sc2, "Path Type");
sc3.writeEntry("dummy", "dummy");
QVERIFY(sc2.sync());
}
// 2nd step: Call testPath() again. Rewriting the config must not break
// the testPath() test.
testPath();
}
void KConfigTest::testPathQtHome()
{
{
QFile file(testConfigDir() + "/pathtest");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out.setCodec("UTF-8");
out << "[Test Group]" << endl
<< "dataDir[$e]=$QT_DATA_HOME/kconfigtest" << endl
<< "cacheDir[$e]=$QT_CACHE_HOME/kconfigtest" << endl
<< "configDir[$e]=$QT_CONFIG_HOME/kconfigtest" << endl;
}
KConfig cf2(TEST_SUBDIR "pathtest");
KConfigGroup group = cf2.group("Test Group");
qunsetenv("QT_DATA_HOME");
qunsetenv("QT_CACHE_HOME");
qunsetenv("QT_CONFIG_HOME");
QVERIFY(group.hasKey("dataDir"));
QCOMPARE(group.readEntry("dataDir", QString()), QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation).append(QStringLiteral("/kconfigtest")));
QVERIFY(group.hasKey("cacheDir"));
QCOMPARE(group.readEntry("cacheDir", QString()), QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation).append(QStringLiteral("/kconfigtest")));
QVERIFY(group.hasKey("configDir"));
QCOMPARE(group.readEntry("configDir", QString()), QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation).append(QStringLiteral("/kconfigtest")));
qputenv("QT_DATA_HOME","/1");
qputenv("QT_CACHE_HOME","/2");
qputenv("QT_CONFIG_HOME","/3");
QVERIFY(group.hasKey("dataDir"));
QCOMPARE(group.readEntry("dataDir", QString()), QStringLiteral("/1/kconfigtest"));
QVERIFY(group.hasKey("cacheDir"));
QCOMPARE(group.readEntry("cacheDir", QString()), QStringLiteral("/2/kconfigtest"));
QVERIFY(group.hasKey("configDir"));
QCOMPARE(group.readEntry("configDir", QString()), QStringLiteral("/3/kconfigtest"));
}
void KConfigTest::testComplex()
{
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup sc3(&sc2, "Complex Types");
QCOMPARE(sc3.readEntry("pointEntry", QPoint()), POINTENTRY);
QCOMPARE(sc3.readEntry("sizeEntry", SIZEENTRY), SIZEENTRY);
QCOMPARE(sc3.readEntry("rectEntry", QRect(1, 2, 3, 4)), RECTENTRY);
QCOMPARE(sc3.readEntry("dateTimeEntry", QDateTime()).toString(Qt::ISODate),
DATETIMEENTRY.toString(Qt::ISODate));
QCOMPARE(sc3.readEntry("dateEntry", QDate()).toString(Qt::ISODate),
DATETIMEENTRY.date().toString(Qt::ISODate));
QCOMPARE(sc3.readEntry("dateTimeEntry", QDate()), DATETIMEENTRY.date());
}
void KConfigTest::testEnums()
{
//Visual C++ 2010 (compiler version 16.0) throws an Internal Compiler Error
//when compiling the code in initTestCase that creates these KConfig entries,
//so we can't run this test
#if defined(_MSC_VER) && _MSC_VER == 1600
QSKIP("Visual C++ 2010 can't compile this test");
#endif
KConfig sc(TEST_SUBDIR "kconfigtest");
KConfigGroup sc3(&sc, "Enum Types");
QCOMPARE(sc3.readEntry("enum-10"), QString("Tens"));
QVERIFY(sc3.readEntry("enum-100", Ones) != Ones);
QVERIFY(sc3.readEntry("enum-100", Ones) != Tens);
QCOMPARE(sc3.readEntry("flags-bit0"), QString("bit0"));
QVERIFY(sc3.readEntry("flags-bit0", Flags()) == bit0);
int eid = staticMetaObject.indexOfEnumerator("Flags");
QVERIFY(eid != -1);
QMetaEnum me = staticMetaObject.enumerator(eid);
Flags bitfield = bit0 | bit1;
QCOMPARE(sc3.readEntry("flags-bit0-bit1"), QString(me.valueToKeys(bitfield)));
QVERIFY(sc3.readEntry("flags-bit0-bit1", Flags()) == bitfield);
}
void KConfigTest::testEntryMap()
{
KConfig sc(TEST_SUBDIR "kconfigtest");
KConfigGroup cg(&sc, "Hello");
QMap<QString, QString> entryMap = cg.entryMap();
qDebug() << entryMap.keys();
QCOMPARE(entryMap.value("stringEntry1"), QString(STRINGENTRY1));
QCOMPARE(entryMap.value("stringEntry2"), QString(STRINGENTRY2));
QCOMPARE(entryMap.value("stringEntry3"), QString(STRINGENTRY3));
QCOMPARE(entryMap.value("stringEntry4"), QString(STRINGENTRY4));
QVERIFY(!entryMap.contains("stringEntry5"));
QVERIFY(!entryMap.contains("stringEntry6"));
QCOMPARE(entryMap.value("Test"), QString::fromUtf8(UTF8BITENTRY));
QCOMPARE(entryMap.value("bytearrayEntry"), QString::fromUtf8(BYTEARRAYENTRY.constData()));
QCOMPARE(entryMap.value("emptyEntry"), QString());
QVERIFY(entryMap.contains("emptyEntry"));
QCOMPARE(entryMap.value("boolEntry1"), QString(BOOLENTRY1 ? "true" : "false"));
QCOMPARE(entryMap.value("boolEntry2"), QString(BOOLENTRY2 ? "true" : "false"));
QCOMPARE(entryMap.value("keywith=equalsign"), QString(STRINGENTRY1));
QCOMPARE(entryMap.value("byteArrayEntry1"), QString(STRINGENTRY1));
QCOMPARE(entryMap.value("doubleEntry1").toDouble(), DOUBLEENTRY);
QCOMPARE(entryMap.value("floatEntry1").toFloat(), FLOATENTRY);
}
void KConfigTest::testInvalid()
{
KConfig sc(TEST_SUBDIR "kconfigtest");
// all of these should print a message to the kdebug.dbg file
KConfigGroup sc3(&sc, "Invalid Types");
sc3.writeEntry("badList", VARIANTLISTENTRY2);
QList<int> list;
// 1 element list
list << 1;
sc3.writeEntry(QStringLiteral("badList"), list);
QVERIFY(sc3.readEntry("badList", QPoint()) == QPoint());
QVERIFY(sc3.readEntry("badList", QRect()) == QRect());
QVERIFY(sc3.readEntry("badList", QSize()) == QSize());
QVERIFY(sc3.readEntry("badList", QDate()) == QDate());
QVERIFY(sc3.readEntry("badList", QDateTime()) == QDateTime());
// 2 element list
list << 2;
sc3.writeEntry("badList", list);
QVERIFY(sc3.readEntry("badList", QRect()) == QRect());
QVERIFY(sc3.readEntry("badList", QDate()) == QDate());
QVERIFY(sc3.readEntry("badList", QDateTime()) == QDateTime());
// 3 element list
list << 303;
sc3.writeEntry("badList", list);
QVERIFY(sc3.readEntry("badList", QPoint()) == QPoint());
QVERIFY(sc3.readEntry("badList", QRect()) == QRect());
QVERIFY(sc3.readEntry("badList", QSize()) == QSize());
QVERIFY(sc3.readEntry("badList", QDate()) == QDate()); // out of bounds
QVERIFY(sc3.readEntry("badList", QDateTime()) == QDateTime());
// 4 element list
list << 4;
sc3.writeEntry("badList", list);
QVERIFY(sc3.readEntry("badList", QPoint()) == QPoint());
QVERIFY(sc3.readEntry("badList", QSize()) == QSize());
QVERIFY(sc3.readEntry("badList", QDate()) == QDate());
QVERIFY(sc3.readEntry("badList", QDateTime()) == QDateTime());
// 5 element list
list[2] = 3;
list << 5;
sc3.writeEntry("badList", list);
QVERIFY(sc3.readEntry("badList", QPoint()) == QPoint());
QVERIFY(sc3.readEntry("badList", QRect()) == QRect());
QVERIFY(sc3.readEntry("badList", QSize()) == QSize());
QVERIFY(sc3.readEntry("badList", QDate()) == QDate());
QVERIFY(sc3.readEntry("badList", QDateTime()) == QDateTime());
// 6 element list
list << 6;
sc3.writeEntry("badList", list);
QVERIFY(sc3.readEntry("badList", QPoint()) == QPoint());
QVERIFY(sc3.readEntry("badList", QRect()) == QRect());
QVERIFY(sc3.readEntry("badList", QSize()) == QSize());
}
void KConfigTest::testChangeGroup()
{
KConfig sc(TEST_SUBDIR "kconfigtest");
KConfigGroup sc3(&sc, "Hello");
QCOMPARE(sc3.name(), QString("Hello"));
KConfigGroup newGroup(sc3);
#ifndef KDE_NO_DEPRECATED
newGroup.changeGroup("FooBar"); // deprecated!
QCOMPARE(newGroup.name(), QString("FooBar"));
QCOMPARE(sc3.name(), QString("Hello")); // unchanged
// Write into the "changed group" and check that it works
newGroup.writeEntry("InFooBar", "FB");
QCOMPARE(KConfigGroup(&sc, "FooBar").entryMap().value("InFooBar"), QString("FB"));
QCOMPARE(KConfigGroup(&sc, "Hello").entryMap().value("InFooBar"), QString());
#endif
KConfigGroup rootGroup(sc.group(""));
QCOMPARE(rootGroup.name(), QString("<default>"));
KConfigGroup sc32(rootGroup.group("Hello"));
QCOMPARE(sc32.name(), QString("Hello"));
KConfigGroup newGroup2(sc32);
#ifndef KDE_NO_DEPRECATED
newGroup2.changeGroup("FooBar"); // deprecated!
QCOMPARE(newGroup2.name(), QString("FooBar"));
QCOMPARE(sc32.name(), QString("Hello")); // unchanged
#endif
}
// Simple test for deleteEntry
void KConfigTest::testDeleteEntry()
{
const char *configFile = TEST_SUBDIR "kconfigdeletetest";
{
KConfig conf(configFile);
conf.group("Hello").writeEntry("DelKey", "ToBeDeleted");
}
const QList<QByteArray> lines = readLines(configFile);
Q_ASSERT(lines.contains("[Hello]\n"));
Q_ASSERT(lines.contains("DelKey=ToBeDeleted\n"));
KConfig sc(configFile);
KConfigGroup group(&sc, "Hello");
group.deleteEntry("DelKey");
QCOMPARE(group.readEntry("DelKey", QString("Fietsbel")), QString("Fietsbel"));
QVERIFY(group.sync());
Q_ASSERT(!readLines(configFile).contains("DelKey=ToBeDeleted\n"));
QCOMPARE(group.readEntry("DelKey", QString("still deleted")), QString("still deleted"));
}
void KConfigTest::testDelete()
{
KConfig sc(TEST_SUBDIR "kconfigtest");
KConfigGroup ct(&sc, "Complex Types");
// First delete a nested group
KConfigGroup delgr(&ct, "Nested Group 3");
QVERIFY(delgr.exists());
QVERIFY(ct.hasGroup("Nested Group 3"));
delgr.deleteGroup();
QVERIFY(!delgr.exists());
QVERIFY(!ct.hasGroup("Nested Group 3"));
QVERIFY(ct.groupList().contains("Nested Group 3"));
KConfigGroup ng(&ct, "Nested Group 2");
QVERIFY(sc.hasGroup("Complex Types"));
QVERIFY(!sc.hasGroup("Does not exist"));
sc.deleteGroup("Complex Types");
QCOMPARE(sc.group("Complex Types").keyList().count(), 0);
QVERIFY(!sc.hasGroup("Complex Types")); // #192266
QVERIFY(!sc.group("Complex Types").exists());
QVERIFY(!ct.hasGroup("Nested Group 1"));
QCOMPARE(ct.group("Nested Group 1").keyList().count(), 0);
QCOMPARE(ct.group("Nested Group 2").keyList().count(), 0);
QCOMPARE(ng.group("Nested Group 2.1").keyList().count(), 0);
KConfigGroup cg(&sc, "AAA");
cg.deleteGroup();
QVERIFY(sc.entryMap("Complex Types").isEmpty());
QVERIFY(sc.entryMap("AAA").isEmpty());
QVERIFY(!sc.entryMap("Hello").isEmpty()); //not deleted group
QVERIFY(sc.entryMap("FooBar").isEmpty()); //inexistant group
QVERIFY(cg.sync());
// Check what happens on disk
const QList<QByteArray> lines = readLines();
//qDebug() << lines;
QVERIFY(!lines.contains("[Complex Types]\n"));
QVERIFY(!lines.contains("[Complex Types][Nested Group 1]\n"));
QVERIFY(!lines.contains("[Complex Types][Nested Group 2]\n"));
QVERIFY(!lines.contains("[Complex Types][Nested Group 2.1]\n"));
QVERIFY(!lines.contains("[AAA]\n"));
QVERIFY(lines.contains("[Hello]\n")); // a group that was not deleted
// test for entries that are marked as deleted when there is no default
KConfig cf(TEST_SUBDIR "kconfigtest", KConfig::SimpleConfig); // make sure there are no defaults
cg = cf.group("Portable Devices");
cg.writeEntry("devices|manual|(null)", "whatever");
cg.writeEntry("devices|manual|/mnt/ipod", "/mnt/ipod");
QVERIFY(cf.sync());
int count = 0;
const QList<QByteArray> listLines = readLines();
for (const QByteArray &item : listLines)
if (item.startsWith("devices|")) { // krazy:exclude=strings
count++;
}
QCOMPARE(count, 2);
cg.deleteEntry("devices|manual|/mnt/ipod");
QVERIFY(cf.sync());
const QList<QByteArray> listLines2 = readLines();
for (const QByteArray &item : listLines2) {
QVERIFY(!item.contains("ipod"));
}
}
void KConfigTest::testDefaultGroup()
{
KConfig sc(TEST_SUBDIR "kconfigtest");
KConfigGroup defaultGroup(&sc, "<default>");
QCOMPARE(defaultGroup.name(), QString("<default>"));
QVERIFY(!defaultGroup.exists());
defaultGroup.writeEntry("TestKey", "defaultGroup");
QVERIFY(defaultGroup.exists());
QCOMPARE(defaultGroup.readEntry("TestKey", QString()), QString("defaultGroup"));
QVERIFY(sc.sync());
{
// Test reading it
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup defaultGroup2(&sc2, "<default>");
QCOMPARE(defaultGroup2.name(), QString("<default>"));
QVERIFY(defaultGroup2.exists());
QCOMPARE(defaultGroup2.readEntry("TestKey", QString()), QString("defaultGroup"));
}
{
// Test reading it
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup emptyGroup(&sc2, "");
QCOMPARE(emptyGroup.name(), QString("<default>"));
QVERIFY(emptyGroup.exists());
QCOMPARE(emptyGroup.readEntry("TestKey", QString()), QString("defaultGroup"));
}
QList<QByteArray> lines = readLines();
QVERIFY(!lines.contains("[]\n"));
QCOMPARE(lines.first(), QByteArray("TestKey=defaultGroup\n"));
// Now that the group exists make sure it isn't returned from groupList()
const QStringList groupList = sc.groupList();
for (const QString &group : groupList) {
QVERIFY(!group.isEmpty() && group != "<default>");
}
defaultGroup.deleteGroup();
QVERIFY(sc.sync());
// Test if deleteGroup worked
lines = readLines();
QVERIFY(lines.first() != QByteArray("TestKey=defaultGroup\n"));
}
void KConfigTest::testEmptyGroup()
{
KConfig sc(TEST_SUBDIR "kconfigtest");
KConfigGroup emptyGroup(&sc, "");
QCOMPARE(emptyGroup.name(), QString("<default>")); // confusing, heh?
QVERIFY(!emptyGroup.exists());
emptyGroup.writeEntry("TestKey", "emptyGroup");
QVERIFY(emptyGroup.exists());
QCOMPARE(emptyGroup.readEntry("TestKey", QString()), QString("emptyGroup"));
QVERIFY(sc.sync());
{
// Test reading it
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup defaultGroup(&sc2, "<default>");
QCOMPARE(defaultGroup.name(), QString("<default>"));
QVERIFY(defaultGroup.exists());
QCOMPARE(defaultGroup.readEntry("TestKey", QString()), QString("emptyGroup"));
}
{
// Test reading it
KConfig sc2(TEST_SUBDIR "kconfigtest");
KConfigGroup emptyGroup2(&sc2, "");
QCOMPARE(emptyGroup2.name(), QString("<default>"));
QVERIFY(emptyGroup2.exists());
QCOMPARE(emptyGroup2.readEntry("TestKey", QString()), QString("emptyGroup"));
}
QList<QByteArray> lines = readLines();
QVERIFY(!lines.contains("[]\n")); // there's no support for the [] group, in fact.
QCOMPARE(lines.first(), QByteArray("TestKey=emptyGroup\n"));
// Now that the group exists make sure it isn't returned from groupList()
const QStringList groupList = sc.groupList();
for (const QString &group : groupList) {
QVERIFY(!group.isEmpty() && group != "<default>");
}
emptyGroup.deleteGroup();
QVERIFY(sc.sync());
// Test if deleteGroup worked
lines = readLines();
QVERIFY(lines.first() != QByteArray("TestKey=defaultGroup\n"));
}
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_BLACKBERRY) && !defined(Q_OS_ANDROID)
#define Q_XDG_PLATFORM
#endif
void KConfigTest::testCascadingWithLocale()
{
// This test relies on XDG_CONFIG_DIRS, which only has effect on Unix.
// Cascading (more than two levels) isn't available at all on Windows.
#ifdef Q_XDG_PLATFORM
QTemporaryDir middleDir;
QTemporaryDir globalDir;
qputenv("XDG_CONFIG_DIRS", qPrintable(middleDir.path() + QString(":") + globalDir.path()));
const QString globalConfigDir = globalDir.path() + "/" TEST_SUBDIR;
QVERIFY(QDir().mkpath(globalConfigDir));
QFile global(globalConfigDir + "foo.desktop");
QVERIFY(global.open(QIODevice::WriteOnly | QIODevice::Text));
QTextStream globalOut(&global);
globalOut << "[Group]" << endl
<< "FromGlobal=true" << endl
<< "FromGlobal[fr]=vrai" << endl
<< "Name=Testing" << endl
<< "Name[fr]=FR" << endl
<< "Other=Global" << endl
<< "Other[fr]=Global_FR" << endl;
global.close();
const QString middleConfigDir = middleDir.path() + "/" TEST_SUBDIR;
QVERIFY(QDir().mkpath(middleConfigDir));
QFile local(middleConfigDir + "foo.desktop");
QVERIFY(local.open(QIODevice::WriteOnly | QIODevice::Text));
QTextStream out(&local);
out << "[Group]" << endl
<< "FromLocal=true" << endl
<< "FromLocal[fr]=vrai" << endl
<< "Name=Local Testing" << endl
<< "Name[fr]=FR" << endl
<< "Other=English Only" << endl;
local.close();
KConfig config(TEST_SUBDIR "foo.desktop");
KConfigGroup group = config.group("Group");
QCOMPARE(group.readEntry("FromGlobal"), QString("true"));
QCOMPARE(group.readEntry("FromLocal"), QString("true"));
QCOMPARE(group.readEntry("Name"), QString("Local Testing"));