File indexing completed on 2024-11-24 05:00:37
0001 /* 0002 SPDX-FileCopyrightText: 2011 Andriy Rysin <rysin@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <QApplication> 0008 #include <QTest> 0009 #include <QTemporaryFile> 0010 0011 #include "../keyboard_config.h" 0012 #include "../layout_memory.h" 0013 #include "../layout_memory_persister.h" 0014 0015 class TestLayoutMemory : public LayoutMemory 0016 { 0017 public: 0018 TestLayoutMemory(const KeyboardConfig &keyboardConfig) 0019 : LayoutMemory(keyboardConfig) 0020 { 0021 } 0022 QMap<QString, LayoutSet> &getLayoutMap() 0023 { 0024 return layoutMap; 0025 } 0026 }; 0027 0028 class LayoutMemoryPersisterTest : public QObject 0029 { 0030 Q_OBJECT 0031 0032 QTemporaryFile file; 0033 KeyboardConfig *keyboardConfig; 0034 TestLayoutMemory *layoutMemory; 0035 LayoutMemoryPersister *layoutMemoryPersister; 0036 0037 const LayoutUnit layoutUnit1; 0038 const LayoutUnit layoutUnit2; 0039 const LayoutUnit layoutUnit3; 0040 0041 public: 0042 LayoutMemoryPersisterTest() 0043 : layoutUnit1("xx") 0044 , layoutUnit2("yy", "var1") 0045 , layoutUnit3("zz", "var2") 0046 { 0047 Q_ASSERT(file.open()); 0048 } 0049 0050 private Q_SLOTS: 0051 void initTestCase() 0052 { 0053 keyboardConfig = new KeyboardConfig(this); 0054 layoutMemory = new TestLayoutMemory(*keyboardConfig); 0055 layoutMemoryPersister = new LayoutMemoryPersister(*layoutMemory); 0056 } 0057 0058 void cleanupTestCase() 0059 { 0060 delete layoutMemoryPersister; 0061 delete layoutMemory; 0062 } 0063 0064 void testSaveNA() 0065 { 0066 keyboardConfig->setSwitchingPolicy(KeyboardConfig::SWITCH_POLICY_WINDOW); 0067 QVERIFY(!layoutMemoryPersister->saveToFile(file)); 0068 QVERIFY(file.size() == 0); 0069 0070 QVERIFY(!layoutMemoryPersister->restoreFromFile(file)); 0071 } 0072 0073 void testSaveGlobal() 0074 { 0075 file.resize(0); 0076 0077 keyboardConfig->setSwitchingPolicy(KeyboardConfig::SWITCH_POLICY_GLOBAL); 0078 layoutMemoryPersister->setGlobalLayout(layoutUnit1); 0079 QVERIFY(layoutMemoryPersister->saveToFile(file)); 0080 QVERIFY(file.size() > 0); 0081 0082 keyboardConfig->layouts.clear(); 0083 0084 QVERIFY(layoutMemoryPersister->restoreFromFile(file)); 0085 QVERIFY(!layoutMemoryPersister->getGlobalLayout().isValid()); 0086 0087 keyboardConfig->layouts << layoutUnit1; 0088 QVERIFY(layoutMemoryPersister->restoreFromFile(file)); 0089 QVERIFY(layoutUnit1.isValid()); 0090 QVERIFY(layoutMemoryPersister->getGlobalLayout().isValid()); 0091 QCOMPARE(layoutMemoryPersister->getGlobalLayout(), layoutUnit1); 0092 } 0093 0094 void testSaveByApp() 0095 { 0096 file.resize(0); 0097 0098 keyboardConfig->setSwitchingPolicy(KeyboardConfig::SWITCH_POLICY_APPLICATION); 0099 0100 layoutMemory->getLayoutMap().clear(); 0101 keyboardConfig->layouts.clear(); 0102 keyboardConfig->layouts << layoutUnit1 << layoutUnit2; 0103 0104 LayoutSet layoutSet1; 0105 layoutSet1.layouts << layoutUnit1 << layoutUnit2; 0106 layoutSet1.currentLayout = layoutUnit2; 0107 layoutMemory->getLayoutMap().insert(QString("app1"), layoutSet1); 0108 0109 LayoutSet layoutSet2; 0110 layoutSet2.layouts << layoutUnit2 << layoutUnit1 << layoutUnit3; 0111 layoutSet2.currentLayout = layoutUnit2; 0112 layoutMemory->getLayoutMap().insert(QString("app2"), layoutSet2); 0113 0114 QVERIFY(layoutMemoryPersister->saveToFile(file)); 0115 QVERIFY(file.size() > 0); 0116 0117 layoutMemory->getLayoutMap().clear(); 0118 QVERIFY(!layoutMemory->getLayoutMap().value("app1").isValid()); 0119 QVERIFY(!layoutMemory->getLayoutMap().value("app2").isValid()); 0120 0121 QVERIFY(layoutMemoryPersister->restoreFromFile(file)); 0122 QVERIFY(!layoutMemoryPersister->getGlobalLayout().isValid()); 0123 QCOMPARE(layoutMemory->getLayoutMap().value("app1"), layoutSet1); 0124 QVERIFY(!layoutMemory->getLayoutMap().value("app2").isValid()); 0125 0126 keyboardConfig->layouts << layoutUnit3; 0127 0128 QVERIFY(layoutMemoryPersister->restoreFromFile(file)); 0129 QVERIFY(!layoutMemoryPersister->getGlobalLayout().isValid()); 0130 QCOMPARE(layoutMemory->getLayoutMap().value("app1"), layoutSet1); 0131 QCOMPARE(layoutMemory->getLayoutMap().value("app2"), layoutSet2); 0132 } 0133 0134 // void layoutMemroyPersisterBenchmark() { 0135 // QBENCHMARK { 0136 // //TODO: generate big map 0137 // layoutMemoryPersister->save(); 0138 // layoutMemoryPersister->restore(); 0139 // } 0140 // } 0141 }; 0142 0143 QTEST_MAIN(LayoutMemoryPersisterTest) 0144 0145 #include "layout_memory_persister_test.moc"