| 1 |
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include <gtest/gtest.h> |
| 6 |
#include <QDir> |
| 7 |
#include <QSignalSpy> |
| 8 |
#include <QTest> |
| 9 |
#include <QUrl> |
| 10 |
#include "filesystem/dfilewatcher.h" |
| 11 |
|
| 12 |
DCORE_USE_NAMESPACE |
| 13 |
|
| 14 |
|
| 15 |
class ut_DFileWatcher : public testing::Test |
| 16 |
{ |
| 17 |
protected: |
| 18 |
void SetUp() override; |
| 19 |
void TearDown() override; |
| 20 |
|
| 21 |
DFileWatcher *fileWatcher = nullptr; |
| 22 |
|
| 23 |
}; |
| 24 |
|
| 25 |
void ut_DFileWatcher::SetUp() |
| 26 |
{ |
| 27 |
QDir dir("/tmp/etc/"); |
| 28 |
if (!dir.exists()) |
| 29 |
dir.mkdir("/tmp/etc/"); |
| 30 |
QFile file("/tmp/etc/test"); |
| 31 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 32 |
return; |
| 33 |
file.close(); |
| 34 |
fileWatcher = new DFileWatcher("/tmp/etc/test"); |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
void ut_DFileWatcher::TearDown() |
| 39 |
{ |
| 40 |
if (fileWatcher) { |
| 41 |
delete fileWatcher; |
| 42 |
fileWatcher = nullptr; |
| 43 |
} |
| 44 |
QDir dir("/tmp/etc/"); |
| 45 |
if (dir.exists()) |
| 46 |
dir.remove("/tmp/etc/"); |
| 47 |
QFile file("/tmp/etc/test"); |
| 48 |
if (file.exists()) |
| 49 |
file.remove(); |
| 50 |
QFile file1("/tmp/etc/test1"); |
| 51 |
if (file1.exists()) |
| 52 |
file1.remove(); |
| 53 |
} |
| 54 |
|
| 55 |
TEST_F(ut_DFileWatcher, testDFileWatcherFileUrl) |
| 56 |
{ |
| 57 |
QUrl url = fileWatcher->fileUrl(); |
| 58 |
ASSERT_TRUE(url.toString() == "file:///tmp/etc/test"); |
| 59 |
} |
| 60 |
|
| 61 |
TEST_F(ut_DFileWatcher, testDFileWatcherStartWatcher) |
| 62 |
{ |
| 63 |
if (!fileWatcher->startWatcher()) return; |
| 64 |
|
| 65 |
fileWatcher->setEnabledSubfileWatcher(QUrl()); |
| 66 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_F(ut_DFileWatcher, testDFileWatcherStopWatcher) |
| 70 |
{ |
| 71 |
if (!fileWatcher->startWatcher()) return; |
| 72 |
|
| 73 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 74 |
ASSERT_TRUE(fileWatcher->stopWatcher()); |
| 75 |
} |
| 76 |
|
| 77 |
TEST_F(ut_DFileWatcher, testDFileWatcherRestartWatcher) |
| 78 |
{ |
| 79 |
if (!fileWatcher->startWatcher()) return; |
| 80 |
|
| 81 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 82 |
ASSERT_TRUE(fileWatcher->restartWatcher()); |
| 83 |
} |
| 84 |
|
| 85 |
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileDeleted) |
| 86 |
{ |
| 87 |
if (!fileWatcher->startWatcher()) return; |
| 88 |
|
| 89 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 90 |
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileDeleted); |
| 91 |
QFile file("/tmp/etc/test"); |
| 92 |
if (file.exists()) |
| 93 |
file.remove(); |
| 94 |
ASSERT_TRUE(QTest::qWaitFor([&spy](){ |
| 95 |
return spy.count() >= 1; |
| 96 |
}, 1000)); |
| 97 |
ASSERT_TRUE(spy.count() >= 1); |
| 98 |
} |
| 99 |
|
| 100 |
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileAttributeChanged) |
| 101 |
{ |
| 102 |
if (!fileWatcher->startWatcher()) return; |
| 103 |
|
| 104 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 105 |
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileAttributeChanged); |
| 106 |
QFile file("/tmp/etc/test"); |
| 107 |
if (file.exists()) { |
| 108 |
file.remove(); |
| 109 |
} |
| 110 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 111 |
return; |
| 112 |
file.close(); |
| 113 |
|
| 114 |
ASSERT_TRUE(QTest::qWaitFor([&spy](){ |
| 115 |
return spy.count() >= 1; |
| 116 |
}, 1000)); |
| 117 |
ASSERT_TRUE(spy.count() >= 1); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileMoved) |
| 122 |
{ |
| 123 |
if (!fileWatcher->startWatcher()) return; |
| 124 |
|
| 125 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 126 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 127 |
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileMoved); |
| 128 |
#else |
| 129 |
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileDeleted); |
| 130 |
#endif |
| 131 |
QString oldFile("/tmp/etc/test"); |
| 132 |
QString newFile("/tmp/etc/test1"); |
| 133 |
QFile::rename(oldFile, newFile); |
| 134 |
|
| 135 |
ASSERT_TRUE(QTest::qWaitFor([&spy](){ |
| 136 |
return spy.count() >= 1; |
| 137 |
}, 1000)); |
| 138 |
ASSERT_TRUE(spy.count() >= 1); |
| 139 |
} |
| 140 |
|
| 141 |
TEST_F(ut_DFileWatcher, testDFileSystemWatcherSubfileCreated) |
| 142 |
{ |
| 143 |
if (!fileWatcher->startWatcher()) return; |
| 144 |
|
| 145 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 146 |
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::subfileCreated); |
| 147 |
QFile file("/tmp/etc/test"); |
| 148 |
if (file.exists()) { |
| 149 |
file.remove(); |
| 150 |
} |
| 151 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 152 |
return; |
| 153 |
|
| 154 |
ASSERT_TRUE(QTest::qWaitFor([&spy](){ |
| 155 |
return spy.count() >= 1; |
| 156 |
}, 1000)); |
| 157 |
ASSERT_TRUE(spy.count() >= 1); |
| 158 |
} |
| 159 |
|
| 160 |
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileModified) |
| 161 |
{ |
| 162 |
if (!fileWatcher->startWatcher()) return; |
| 163 |
|
| 164 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 165 |
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileModified); |
| 166 |
QFile file("/tmp/etc/test"); |
| 167 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 168 |
return; |
| 169 |
} |
| 170 |
QTextStream out(&file); |
| 171 |
out << "hello"; |
| 172 |
file.close(); |
| 173 |
ASSERT_TRUE(QTest::qWaitFor([&spy](){ |
| 174 |
return spy.count() >= 1; |
| 175 |
}, 1000)); |
| 176 |
ASSERT_TRUE(spy.count() >= 1); |
| 177 |
} |
| 178 |
|
| 179 |
TEST_F(ut_DFileWatcher, testDFileSystemWatcherFileClosed) |
| 180 |
{ |
| 181 |
if (!fileWatcher->startWatcher()) return; |
| 182 |
|
| 183 |
ASSERT_TRUE(fileWatcher->startWatcher()); |
| 184 |
QSignalSpy spy(fileWatcher, &DBaseFileWatcher::fileClosed); |
| 185 |
QFile file("/tmp/etc/test"); |
| 186 |
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 187 |
return; |
| 188 |
} |
| 189 |
file.close(); |
| 190 |
ASSERT_TRUE(QTest::qWaitFor([&spy](){ |
| 191 |
return spy.count() >= 1; |
| 192 |
}, 1000)); |
| 193 |
ASSERT_TRUE(spy.count() >= 1); |
| 194 |
} |