* Copyright (c), Huawei Technologies Co., Ltd. 2025-2025.All rights reserved.
*/
#include <gtest/gtest.h>
#include "FileManager/FileInfoManager.h"
#include "FileManager/FileWatcherFactory.h"
using namespace Insight::Scalar::FileInfo;
using namespace Insight::Scalar::FileWatch;
namespace Insight::UnitTest {
class FileWatcherTest : public testing::Test
{
protected:
auto& GetWatcher()
{
static std::unique_ptr<FileWatcher> watcher = FileWatcherFactory::GetFileWatcher();
static bool start = false;
if (!start) {
watcher->Init();
start = true;
}
return watcher;
}
};
TEST_F(FileWatcherTest, Factory)
{
auto& watcher = GetWatcher();
EXPECT_NE(watcher, nullptr);
}
TEST_F(FileWatcherTest, AddFileWatch)
{
auto& watcher = GetWatcher();
watcher->AddWatchPath({"/root/data/file1", "/root/data/file2"});
auto linuxWatcher = (FileWatcherLinuxImpl*)(watcher.get());
EXPECT_EQ(linuxWatcher->IsDirWatched("/root/data"), true);
watcher->AddWatchPath({"/root/data/file1", "/root/data/file2"});
linuxWatcher->DelWatchPath({"/root/data/file1", "/root/data/file2"});
EXPECT_EQ(linuxWatcher->IsDirWatched("/root/data"), false);
}
TEST_F(FileWatcherTest, OnFileCreate)
{
auto& watcher = GetWatcher();
watcher->OnFileCreated("/root/data", "file1");
watcher->OnFileWriteClose("/root/data", "file1");
}
}