From f5ed13b1e2d20ca49adb6fa378faee14bece16a0 Mon Sep 17 00:00:00 2001 From: Szabolcs David Date: Thu, 10 Dec 2015 07:05:29 -0800 Subject: Add function to clear data from the cache It marks the entries of the current cache backend for deletion and starts to remove them. Task-number: QTBUG-48177 Change-Id: I85ec25048ff5429976f1b2dcacd74666bdbe6624 Reviewed-by: Allan Sandfeld Jensen --- .../qwebengineprofile/tst_qwebengineprofile.cpp | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'tests/auto/widgets') diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp index 09929d33f..d24a43b41 100644 --- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp +++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp @@ -37,6 +37,7 @@ #include "../util.h" #include #include +#include class tst_QWebEngineProfile : public QObject { @@ -45,6 +46,7 @@ class tst_QWebEngineProfile : public QObject private Q_SLOTS: void defaultProfile(); void profileConstructors(); + void clearDataFromCache(); }; void tst_QWebEngineProfile::defaultProfile() @@ -69,7 +71,45 @@ void tst_QWebEngineProfile::profileConstructors() QCOMPARE(diskProfile.httpCacheType(), QWebEngineProfile::DiskHttpCache); QCOMPARE(otrProfile.persistentCookiesPolicy(), QWebEngineProfile::NoPersistentCookies); QCOMPARE(diskProfile.persistentCookiesPolicy(), QWebEngineProfile::AllowPersistentCookies); +} + +void tst_QWebEngineProfile::clearDataFromCache() +{ + QWebEnginePage page; + + QDir cacheDir("./tst_QWebEngineProfile_cacheDir"); + cacheDir.makeAbsolute(); + if (cacheDir.exists()) + cacheDir.removeRecursively(); + cacheDir.mkpath(cacheDir.path()); + + QWebEngineProfile *profile = page.profile(); + profile->setCachePath(cacheDir.path()); + profile->setHttpCacheType(QWebEngineProfile::DiskHttpCache); + + QSignalSpy loadFinishedSpy(&page, SIGNAL(loadFinished(bool))); + page.load(QUrl("http://qt-project.org")); + if (!loadFinishedSpy.wait(10000) || !loadFinishedSpy.at(0).at(0).toBool()) + QSKIP("Couldn't load page from network, skipping test."); + + cacheDir.refresh(); + QVERIFY(cacheDir.entryList().contains("Cache")); + cacheDir.cd("./Cache"); + int filesBeforeClear = cacheDir.entryList().count(); + + QFileSystemWatcher fileSystemWatcher; + fileSystemWatcher.addPath(cacheDir.path()); + QSignalSpy directoryChangedSpy(&fileSystemWatcher, SIGNAL(directoryChanged(const QString &))); + + // It deletes most of the files, but not all of them. + profile->clearHttpCache(); + QTest::qWait(1000); + QTRY_VERIFY(directoryChangedSpy.count() > 0); + + cacheDir.refresh(); + QVERIFY(filesBeforeClear > cacheDir.entryList().count()); + cacheDir.removeRecursively(); } QTEST_MAIN(tst_QWebEngineProfile) -- cgit v1.2.3