| 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 |
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
| 8 |
#include <filesystem> //Avoid changing the access control of the standard library |
| 9 |
#endif |
| 10 |
#define private public |
| 11 |
#include "filesystem/dfilesystemwatcher.h" |
| 12 |
#undef private |
| 13 |
|
| 14 |
DCORE_USE_NAMESPACE |
| 15 |
|
| 16 |
|
| 17 |
class ut_DFileSystemWatcher : public testing::Test |
| 18 |
{ |
| 19 |
protected: |
| 20 |
void SetUp() override; |
| 21 |
void TearDown() override; |
| 22 |
|
| 23 |
DFileSystemWatcher *fileSystemWatcher = nullptr; |
| 24 |
|
| 25 |
}; |
| 26 |
|
| 27 |
void ut_DFileSystemWatcher::SetUp() |
| 28 |
{ |
| 29 |
fileSystemWatcher = new DFileSystemWatcher(nullptr); |
| 30 |
QDir dir0("/tmp/etc0/"); |
| 31 |
if (!dir0.exists()) |
| 32 |
dir0.mkdir("/tmp/etc0/"); |
| 33 |
QDir dir1("/tmp/etc1/"); |
| 34 |
if (!dir1.exists()) |
| 35 |
dir1.mkdir("/tmp/etc1/"); |
| 36 |
} |
| 37 |
|
| 38 |
void ut_DFileSystemWatcher::TearDown() |
| 39 |
{ |
| 40 |
if (fileSystemWatcher) { |
| 41 |
delete fileSystemWatcher; |
| 42 |
fileSystemWatcher = nullptr; |
| 43 |
} |
| 44 |
QDir dir0("/tmp/etc0/"); |
| 45 |
if (dir0.exists()) |
| 46 |
dir0.remove("/tmp/etc0/"); |
| 47 |
QDir dir1("/tmp/etc1/"); |
| 48 |
if (dir1.exists()) |
| 49 |
dir1.remove("/tmp/etc1/"); |
| 50 |
} |
| 51 |
|
| 52 |
TEST_F(ut_DFileSystemWatcher, testDFileSystemWatcherAddPath) |
| 53 |
{ |
| 54 |
if (!fileSystemWatcher->d_func()) return; |
| 55 |
|
| 56 |
fileSystemWatcher->addPath("/tmp/etc0"); |
| 57 |
|
| 58 |
QStringList dirs = fileSystemWatcher->directories(); |
| 59 |
ASSERT_TRUE(dirs.contains("/tmp/etc0")); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_F(ut_DFileSystemWatcher, testDFileSystemWatcherAddPaths) |
| 63 |
{ |
| 64 |
if (!fileSystemWatcher->d_func()) return; |
| 65 |
|
| 66 |
fileSystemWatcher->addPaths( QStringList() << "/tmp/etc0" << "/tmp/etc1"); |
| 67 |
QStringList dirs = fileSystemWatcher->directories(); |
| 68 |
ASSERT_TRUE(dirs.contains("/tmp/etc0")); |
| 69 |
ASSERT_TRUE(dirs.contains("/tmp/etc1")); |
| 70 |
} |
| 71 |
|
| 72 |
TEST_F(ut_DFileSystemWatcher, testDFileSystemWatcherRemovePath) |
| 73 |
{ |
| 74 |
if (!fileSystemWatcher->d_func()) return; |
| 75 |
|
| 76 |
fileSystemWatcher->addPath("/tmp/etc0"); |
| 77 |
QStringList dirs0 = fileSystemWatcher->directories(); |
| 78 |
ASSERT_TRUE(dirs0.contains("/tmp/etc0")); |
| 79 |
fileSystemWatcher->removePath("/tmp/etc0"); |
| 80 |
QStringList dirs1 = fileSystemWatcher->directories(); |
| 81 |
ASSERT_FALSE(dirs1.contains("/tmp/etc0")); |
| 82 |
} |
| 83 |
|
| 84 |
TEST_F(ut_DFileSystemWatcher, testDFileSystemWatcherRemovePaths) |
| 85 |
{ |
| 86 |
if (!fileSystemWatcher->d_func()) return; |
| 87 |
|
| 88 |
fileSystemWatcher->addPaths( QStringList() << "/tmp/etc0" << "/tmp/etc1"); |
| 89 |
QStringList dirs0 = fileSystemWatcher->directories(); |
| 90 |
ASSERT_TRUE(dirs0.contains("/tmp/etc0")); |
| 91 |
ASSERT_TRUE(dirs0.contains("/tmp/etc1")); |
| 92 |
fileSystemWatcher->removePaths(QStringList() << "/tmp/etc0" << "/tmp/etc1"); |
| 93 |
QStringList dirs1 = fileSystemWatcher->directories(); |
| 94 |
ASSERT_FALSE(dirs1.contains("/tmp/etc0")); |
| 95 |
ASSERT_FALSE(dirs1.contains("/tmp/etc1")); |
| 96 |
} |