2022-08-11 13:02:07 +08:00
|
|
|
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2021-08-30 20:46:14 +08:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTest>
|
|
|
|
#include <QUrl>
|
|
|
|
#include "filesystem/dfilewatcher.h"
|
|
|
|
|
|
|
|
DCORE_USE_NAMESPACE
|
|
|
|
|
|
|
|
|
|
|
|
class ut_DFileWatcher : public testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void SetUp() override;
|
|
|
|
void TearDown() override;
|
|
|
|
|
|
|
|
DFileWatcher *fileWatcher = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void ut_DFileWatcher::SetUp()
|
|
|
|
{
|
|
|
|
QDir dir("/tmp/etc/");
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkdir("/tmp/etc/");
|
|
|
|
QFile file("/tmp/etc/test");
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
return;
|
|
|
|
file.close();
|
|
|
|
fileWatcher = new DFileWatcher("/tmp/etc/test");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ut_DFileWatcher::TearDown()
|
|
|
|
{
|
|
|
|
if (fileWatcher) {
|
|
|
|
delete fileWatcher;
|
|
|
|
fileWatcher = nullptr;
|
|
|
|
}
|
|
|
|
QDir dir("/tmp/etc/");
|
|
|
|
if (dir.exists())
|
|
|
|
dir.remove("/tmp/etc/");
|
|
|
|
QFile file("/tmp/etc/test");
|
|
|
|
if (file.exists())
|
|
|
|
file.remove();
|
|
|
|
QFile file1("/tmp/etc/test1");
|
|
|
|
if (file1.exists())
|
|
|
|
file1.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileWatcherFileUrl)
|
|
|
|
{
|
|
|
|
QUrl url = fileWatcher->fileUrl();
|
|
|
|
ASSERT_TRUE(url.toString() == "file:///tmp/etc/test");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileWatcherStartWatcher)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
fileWatcher->setEnabledSubfileWatcher(QUrl());
|
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileWatcherStopWatcher)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
ASSERT_TRUE(fileWatcher->stopWatcher());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileWatcherRestartWatcher)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
ASSERT_TRUE(fileWatcher->restartWatcher());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileDeleted)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileDeleted);
|
|
|
|
QFile file("/tmp/etc/test");
|
|
|
|
if (file.exists())
|
|
|
|
file.remove();
|
2021-09-01 13:54:26 +08:00
|
|
|
ASSERT_TRUE(QTest::qWaitFor([&spy](){
|
2021-08-30 20:46:14 +08:00
|
|
|
return spy.count() >= 1;
|
2021-09-01 13:54:26 +08:00
|
|
|
}, 1000));
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(spy.count() >= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileAttributeChanged)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileAttributeChanged);
|
|
|
|
QFile file("/tmp/etc/test");
|
|
|
|
if (file.exists()) {
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
return;
|
|
|
|
file.close();
|
|
|
|
|
2021-09-01 13:54:26 +08:00
|
|
|
ASSERT_TRUE(QTest::qWaitFor([&spy](){
|
2021-08-30 20:46:14 +08:00
|
|
|
return spy.count() >= 1;
|
2021-09-01 13:54:26 +08:00
|
|
|
}, 1000));
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(spy.count() >= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileMoved)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
2023-06-07 13:54:26 +08:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
2021-08-30 20:46:14 +08:00
|
|
|
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileMoved);
|
2023-06-07 13:54:26 +08:00
|
|
|
#else
|
|
|
|
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileDeleted);
|
|
|
|
#endif
|
2021-08-30 20:46:14 +08:00
|
|
|
QString oldFile("/tmp/etc/test");
|
|
|
|
QString newFile("/tmp/etc/test1");
|
|
|
|
QFile::rename(oldFile, newFile);
|
|
|
|
|
2021-09-01 13:54:26 +08:00
|
|
|
ASSERT_TRUE(QTest::qWaitFor([&spy](){
|
2021-08-30 20:46:14 +08:00
|
|
|
return spy.count() >= 1;
|
2021-09-01 13:54:26 +08:00
|
|
|
}, 1000));
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(spy.count() >= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileSystemWatcherSubfileCreated)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::subfileCreated);
|
|
|
|
QFile file("/tmp/etc/test");
|
|
|
|
if (file.exists()) {
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
return;
|
|
|
|
|
2021-09-01 13:54:26 +08:00
|
|
|
ASSERT_TRUE(QTest::qWaitFor([&spy](){
|
2021-08-30 20:46:14 +08:00
|
|
|
return spy.count() >= 1;
|
2021-09-01 13:54:26 +08:00
|
|
|
}, 1000));
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(spy.count() >= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileModified)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileModified);
|
|
|
|
QFile file("/tmp/etc/test");
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << "hello";
|
|
|
|
file.close();
|
2021-09-01 13:54:26 +08:00
|
|
|
ASSERT_TRUE(QTest::qWaitFor([&spy](){
|
2021-08-30 20:46:14 +08:00
|
|
|
return spy.count() >= 1;
|
2021-09-01 13:54:26 +08:00
|
|
|
}, 1000));
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(spy.count() >= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileClosed)
|
|
|
|
{
|
2022-03-01 14:02:57 +08:00
|
|
|
if (!fileWatcher->startWatcher()) return;
|
|
|
|
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(fileWatcher->startWatcher());
|
|
|
|
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileClosed);
|
|
|
|
QFile file("/tmp/etc/test");
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file.close();
|
2021-09-01 13:54:26 +08:00
|
|
|
ASSERT_TRUE(QTest::qWaitFor([&spy](){
|
2021-08-30 20:46:14 +08:00
|
|
|
return spy.count() >= 1;
|
2021-09-01 13:54:26 +08:00
|
|
|
}, 1000));
|
2021-08-30 20:46:14 +08:00
|
|
|
ASSERT_TRUE(spy.count() >= 1);
|
|
|
|
}
|