File indexing completed on 2024-05-12 05:52:03
0001 /* 0002 SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "location_history_test.h" 0007 #include "kateapp.h" 0008 #include "katemainwindow.h" 0009 #include "kateviewmanager.h" 0010 #include "kateviewspace.h" 0011 0012 #include <QCommandLineParser> 0013 #include <QSignalSpy> 0014 #include <QTemporaryDir> 0015 #include <QTest> 0016 0017 #include <KLocalizedString> 0018 0019 QTEST_MAIN(LocationHistoryTest) 0020 0021 LocationHistoryTest::LocationHistoryTest(QObject *parent) 0022 : QObject(parent) 0023 { 0024 // ensure ui file can be found and the translation domain is set to avoid warnings 0025 qApp->setApplicationName(QStringLiteral("kate")); 0026 KLocalizedString::setApplicationDomain(QByteArrayLiteral("kate")); 0027 0028 m_tempdir = new QTemporaryDir; 0029 QVERIFY(m_tempdir->isValid()); 0030 0031 // ensure we use some dummy config 0032 KConfig::setMainConfigName(m_tempdir->path() + QStringLiteral("/testconfigfilerc")); 0033 0034 // create KWrite variant to avoid plugin loading! 0035 static QCommandLineParser parser; 0036 app = new KateApp(parser, KateApp::ApplicationKWrite, m_tempdir->path()); 0037 app->sessionManager()->activateAnonymousSession(); 0038 0039 // create some usable example 0040 QVERIFY(tempFile.open()); 0041 for (int i = 0; i < 10000; ++i) { 0042 tempFile.write("test line lala lala lala lala lala lala lala lala\n"); 0043 } 0044 tempFile.flush(); 0045 } 0046 0047 LocationHistoryTest::~LocationHistoryTest() 0048 { 0049 delete app; 0050 } 0051 0052 void LocationHistoryTest::init() 0053 { 0054 } 0055 0056 void LocationHistoryTest::cleanup() 0057 { 0058 auto vs = viewSpace(); 0059 Q_ASSERT(vs); 0060 vs->locationHistoryBuffer().clear(); 0061 vs->currentLoc() = 0; 0062 } 0063 0064 KateViewSpace *LocationHistoryTest::viewSpace() 0065 { 0066 Q_ASSERT(viewManager()); 0067 return viewManager()->activeViewSpace(); 0068 } 0069 0070 KateViewManager *LocationHistoryTest::viewManager() 0071 { 0072 auto mw = app->activeKateMainWindow(); 0073 Q_ASSERT(mw); 0074 return mw->viewManager(); 0075 } 0076 0077 static int viewLineCount(KTextEditor::View *v) 0078 { 0079 Q_ASSERT(v); 0080 return v->lastDisplayedLine() - v->firstDisplayedLine(); 0081 } 0082 0083 void LocationHistoryTest::test_addLocationInvalidUrl() 0084 { 0085 auto vs = viewSpace(); 0086 Q_ASSERT(vs); 0087 0088 // location should be zero 0089 QCOMPARE(vs->currentLoc(), 0); 0090 0091 // invalid url will have no effect 0092 0093 vs->addPositionToHistory(QUrl(), {1, 1}); 0094 QCOMPARE(vs->currentLoc(), 0); 0095 QCOMPARE(vs->locationHistoryBuffer().size(), 0); 0096 } 0097 0098 void LocationHistoryTest::test_addLocation() 0099 { 0100 auto vs = viewSpace(); 0101 auto vm = viewManager(); 0102 Q_ASSERT(vs && vm); 0103 0104 const auto currentFileUrl = QUrl::fromLocalFile(tempFile.fileName()); 0105 0106 vm->openUrl(currentFileUrl, QString()); 0107 0108 QCOMPARE(vs->currentLoc(), 0); 0109 QCOMPARE(vs->locationHistoryBuffer().size(), 0); 0110 0111 // Add first position 0112 0113 vm->addPositionToHistory(currentFileUrl, {0, 0}); 0114 0115 QCOMPARE(vs->currentLoc(), 0); 0116 QCOMPARE(vs->locationHistoryBuffer().size(), 1); 0117 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(0, 0)); 0118 0119 // Adding same position again will have no affect 0120 0121 vm->addPositionToHistory(currentFileUrl, {0, 0}); 0122 0123 QCOMPARE(vs->currentLoc(), 0); 0124 QCOMPARE(vs->locationHistoryBuffer().size(), 1); 0125 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(0, 0)); 0126 0127 // Adding position on same line with different column will update the existing cursor 0128 // But no new positions will be added to the buffer 0129 vm->addPositionToHistory(currentFileUrl, {0, 2}); 0130 0131 QCOMPARE(vs->currentLoc(), 0); 0132 QCOMPARE(vs->locationHistoryBuffer().size(), 1); 0133 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(0, 2)); 0134 0135 // Adding position, but via internal call i.e., cursorPositionChanged will only have an affect if 0136 // new position is at least "viewLineCount" away 0137 vs->addPositionToHistory(currentFileUrl, {0, 3}); 0138 QCOMPARE(vs->currentLoc(), 0); 0139 QCOMPARE(vs->locationHistoryBuffer().size(), 1); 0140 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(0, 3)); 0141 0142 const int viewLineCnt = viewLineCount(vm->activeView()); 0143 vs->addPositionToHistory(currentFileUrl, {0 + viewLineCnt + 1, 0}); 0144 QCOMPARE(vs->currentLoc(), 1); 0145 QCOMPARE(vs->locationHistoryBuffer().size(), 2); 0146 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(0 + viewLineCnt + 1, 0)); 0147 } 0148 0149 void LocationHistoryTest::test_addMaxLocations() 0150 { 0151 auto vs = viewSpace(); 0152 auto vm = viewManager(); 0153 Q_ASSERT(vs && vm); 0154 0155 const auto currentFileUrl = QUrl::fromLocalFile(tempFile.fileName()); 0156 0157 for (int i = 0; i < 100; ++i) { 0158 vm->addPositionToHistory(currentFileUrl, {i, 0}); 0159 } 0160 0161 QCOMPARE(vs->locationHistoryBuffer().size(), 50); 0162 QCOMPARE(vs->currentLoc(), 49); 0163 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(99, 0)); 0164 } 0165 0166 void LocationHistoryTest::test_goBackForward() 0167 { 0168 auto vs = viewSpace(); 0169 auto vm = viewManager(); 0170 Q_ASSERT(vs && vm); 0171 0172 const auto currentFileUrl = QUrl::fromLocalFile(tempFile.fileName()); 0173 0174 for (int i = 0; i <= 49; ++i) { 0175 vm->addPositionToHistory(currentFileUrl, {i, 0}); 0176 } 0177 QCOMPARE(vs->isHistoryBackEnabled(), true); 0178 QCOMPARE(vs->isHistoryForwardEnabled(), false); 0179 0180 QCOMPARE(vs->locationHistoryBuffer().size(), 50); 0181 QCOMPARE(vs->currentLoc(), 49); 0182 0183 for (int i = 49; i >= 0; --i) { 0184 QCOMPARE(vs->currentLoc(), i); 0185 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(i, 0)); 0186 vs->goBack(); 0187 } 0188 QCOMPARE(vs->isHistoryBackEnabled(), false); 0189 QCOMPARE(vs->isHistoryForwardEnabled(), true); 0190 0191 for (int i = 0; i < 49; ++i) { 0192 QCOMPARE(vs->currentLoc(), i); 0193 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(i, 0)); 0194 vs->goForward(); 0195 } 0196 QCOMPARE(vs->isHistoryBackEnabled(), true); 0197 QCOMPARE(vs->isHistoryForwardEnabled(), false); 0198 } 0199 0200 void LocationHistoryTest::test_goBackForward2() 0201 { 0202 auto vs = viewSpace(); 0203 auto vm = viewManager(); 0204 Q_ASSERT(vs && vm); 0205 0206 const auto currentFileUrl = QUrl::fromLocalFile(tempFile.fileName()); 0207 0208 // add 5 positions 0209 for (int i = 0; i < 5; ++i) { 0210 vm->addPositionToHistory(currentFileUrl, {i, 0}); 0211 } 0212 QCOMPARE(vs->isHistoryBackEnabled(), true); 0213 QCOMPARE(vs->isHistoryForwardEnabled(), false); 0214 0215 // go back 3 positions 0216 for (int i = 4; i >= 2; --i) { 0217 QCOMPARE(vs->currentLoc(), i); 0218 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(i, 0)); 0219 vs->goBack(); 0220 } 0221 QCOMPARE(vs->isHistoryBackEnabled(), true); 0222 QCOMPARE(vs->isHistoryForwardEnabled(), true); 0223 0224 QCOMPARE(vs->locationHistoryBuffer().size(), 5); 0225 QCOMPARE(vs->currentLoc(), 1); 0226 0227 // jump to new position 0228 // should clear forward history 0229 vm->addPositionToHistory(currentFileUrl, {50, 0}); 0230 0231 QCOMPARE(vs->locationHistoryBuffer().size(), 3); 0232 QCOMPARE(vs->currentLoc(), 2); 0233 0234 QCOMPARE(vs->isHistoryBackEnabled(), true); 0235 QCOMPARE(vs->isHistoryForwardEnabled(), false); 0236 } 0237 0238 void LocationHistoryTest::test_addOnlyIfViewLineCountAwayFromCurrentPos() 0239 { 0240 auto vs = viewSpace(); 0241 auto vm = viewManager(); 0242 Q_ASSERT(vs && vm); 0243 0244 const auto currentFileUrl = QUrl::fromLocalFile(tempFile.fileName()); 0245 0246 vm->addPositionToHistory(currentFileUrl, {0, 0}); 0247 QCOMPARE(vs->locationHistoryBuffer().size(), 1); 0248 0249 QCOMPARE(vs->isHistoryBackEnabled(), true); 0250 QCOMPARE(vs->isHistoryForwardEnabled(), false); 0251 0252 // Changing cursor position will only be added to location 0253 // history if view line count away 0254 // ensure the view has some non-null size 0255 auto view = vm->activeView(); 0256 view->resize(600, 600); 0257 int vlc = viewLineCount(view); 0258 view->setCursorPosition({2 * vlc, 0}); 0259 QCOMPARE(vs->locationHistoryBuffer().size(), 2); 0260 0261 QCOMPARE(vs->isHistoryBackEnabled(), true); 0262 QCOMPARE(vs->isHistoryForwardEnabled(), false); 0263 0264 // changing cursor position again but not far enough, should lead 0265 // to no change 0266 view->setCursorPosition({view->cursorPosition().line() + 1, 0}); 0267 QCOMPARE(vs->locationHistoryBuffer().size(), 2); 0268 0269 // go back one position i.e., to {0, 0} 0270 vs->goBack(); 0271 0272 QCOMPARE(vs->isHistoryBackEnabled(), false); 0273 QCOMPARE(vs->isHistoryForwardEnabled(), true); 0274 0275 // changing cursor position now should only add a location if cursor is viewLineCount away 0276 // from "current" position, which is {0, 0}, and not from the last location in buffer. 0277 view->setCursorPosition({vlc + 5, 0}); 0278 0279 QCOMPARE(vs->locationHistoryBuffer().size(), 2); 0280 QCOMPARE(vs->locationHistoryBuffer().at(vs->currentLoc()).cursor, KTextEditor::Cursor(vlc + 5, 0)); 0281 0282 QCOMPARE(vs->isHistoryBackEnabled(), true); 0283 QCOMPARE(vs->isHistoryForwardEnabled(), false); 0284 } 0285 0286 void LocationHistoryTest::test_signalEmission() 0287 { 0288 auto vs = viewSpace(); 0289 auto vm = viewManager(); 0290 Q_ASSERT(vs && vm); 0291 0292 const auto currentFileUrl = QUrl::fromLocalFile(tempFile.fileName()); 0293 0294 QSignalSpy sigSpy(vm, &KateViewManager::historyBackEnabled); 0295 0296 vm->addPositionToHistory(currentFileUrl, {0, 0}); 0297 QCOMPARE(vs->locationHistoryBuffer().size(), 1); 0298 0299 // only one position, no signals for now 0300 QCOMPARE(sigSpy.count(), 0); 0301 0302 vm->addPositionToHistory(currentFileUrl, {1, 0}); 0303 QCOMPARE(sigSpy.count(), 1); 0304 auto args = sigSpy.takeFirst(); 0305 QCOMPARE(args.at(0).toBool(), true); 0306 0307 QSignalSpy spy2(vm, &KateViewManager::historyForwardEnabled); 0308 0309 vs->goBack(); 0310 0311 // 2 signals (back disabled, forward enabled) 0312 0313 QCOMPARE(spy2.count(), 1); 0314 { 0315 auto args = spy2.takeFirst(); 0316 QCOMPARE(args.at(0).toBool(), true); 0317 } 0318 QCOMPARE(sigSpy.count(), 1); 0319 { 0320 auto args = sigSpy.takeFirst(); 0321 QCOMPARE(args.at(0).toBool(), false); 0322 } 0323 0324 vs->goForward(); 0325 // 2 signals (back enabled, forward disabled) 0326 QCOMPARE(spy2.count(), 1); 0327 { 0328 auto args = spy2.takeFirst(); 0329 QCOMPARE(args.at(0).toBool(), false); 0330 } 0331 QCOMPARE(sigSpy.count(), 1); 0332 { 0333 auto args = sigSpy.takeFirst(); 0334 QCOMPARE(args.at(0).toBool(), true); 0335 } 0336 } 0337 0338 #include "moc_location_history_test.cpp"