Repository navigation
Expand file tree
/
Copy pathFileSystemWatcherTest.cpp
More file actions
162 lines (128 loc) · 5.16 KB
/
Copy pathFileSystemWatcherTest.cpp
File metadata and controls
162 lines (128 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (C) Explorer++ Project
// SPDX-License-Identifier: GPL-3.0-only
// See LICENSE in the top level directory
#include "pch.h"
#include "FileSystemWatcher.h"
#include "ExecutorWrapper.h"
#include "MessageLoop.h"
#include "PidlTestHelper.h"
#include "ScopedTestDir.h"
#include "UIThreadExecutor.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <fstream>
using namespace std::chrono_literals;
using namespace testing;
class FileSystemWatcherTest : public Test
{
protected:
FileSystemWatcherTest() : m_uiThreadExecutor(std::make_shared<UIThreadExecutor>())
{
}
void SetUp() override
{
HRESULT hr = SHParseDisplayName(m_scopedTestDir.GetPath().c_str(), nullptr,
PidlOutParam(m_scopedTestDirPidl), 0, nullptr);
ASSERT_HRESULT_SUCCEEDED(hr);
}
std::unique_ptr<FileSystemWatcher> CreateWatcher(DirectoryWatcher::Event eventToObserve)
{
std::unique_ptr<FileSystemWatcher> watcher;
CreateWatcher(watcher, eventToObserve);
return watcher;
}
void WaitForNotifications()
{
m_messageLoop.RunWithTimeout(TIMEOUT_DURATION, m_uiThreadExecutor.Get());
}
static constexpr std::chrono::duration TIMEOUT_DURATION = 5s;
ScopedTestDir m_scopedTestDir;
PidlAbsolute m_scopedTestDirPidl;
ExecutorWrapper<UIThreadExecutor> m_uiThreadExecutor;
MockFunction<void(DirectoryWatcher::Event event, const PidlAbsolute &simplePidl1,
const PidlAbsolute &simplePidl2)>
m_callback;
MessageLoop m_messageLoop;
private:
void CreateWatcher(std::unique_ptr<FileSystemWatcher> &watcher,
DirectoryWatcher::Event eventToObserve)
{
watcher = FileSystemWatcher::MaybeCreate(
m_scopedTestDirPidl, DirectoryWatcher::Filters::All, m_uiThreadExecutor.Get(),
[this, eventToObserve](DirectoryWatcher::Event event, const PidlAbsolute &simplePidl1,
const PidlAbsolute &simplePidl2)
{
if (event == eventToObserve)
{
m_callback.Call(event, simplePidl1, simplePidl2);
m_messageLoop.Stop();
}
},
DirectoryWatcher::Behavior::NonRecursive);
ASSERT_NE(watcher, nullptr);
}
};
TEST_F(FileSystemWatcherTest, AddItem)
{
auto watcher = CreateWatcher(DirectoryWatcher::Event::Added);
auto newItemPath = m_scopedTestDir.GetPath() / L"new-item";
auto newItemPidl = CreateSimplePidlForTest(newItemPath, nullptr, ShellItemType::Folder);
EXPECT_CALL(m_callback, Call(DirectoryWatcher::Event::Added, newItemPidl, PidlAbsolute{}));
std::filesystem::create_directory(newItemPath);
WaitForNotifications();
}
TEST_F(FileSystemWatcherTest, ModifyItem)
{
auto watcher = CreateWatcher(DirectoryWatcher::Event::Modified);
auto itemPath = m_scopedTestDir.GetPath() / L"item";
auto itemPidl = CreateSimplePidlForTest(itemPath, nullptr, ShellItemType::File);
EXPECT_CALL(m_callback, Call(DirectoryWatcher::Event::Modified, itemPidl, PidlAbsolute{}));
{
std::ofstream stream(itemPath);
stream << "data";
}
WaitForNotifications();
}
TEST_F(FileSystemWatcherTest, RenameItem)
{
auto watcher = CreateWatcher(DirectoryWatcher::Event::Renamed);
auto originalItemPath = m_scopedTestDir.GetPath() / L"original-item";
auto originalItemPidl =
CreateSimplePidlForTest(originalItemPath, nullptr, ShellItemType::Folder);
auto updatedItemPath = m_scopedTestDir.GetPath() / L"updated-item";
auto updatedItemPidl = CreateSimplePidlForTest(updatedItemPath, nullptr, ShellItemType::Folder);
EXPECT_CALL(m_callback,
Call(DirectoryWatcher::Event::Renamed, originalItemPidl, updatedItemPidl));
std::filesystem::create_directory(originalItemPath);
std::filesystem::rename(originalItemPath, updatedItemPath);
WaitForNotifications();
}
TEST_F(FileSystemWatcherTest, RemoveItem)
{
auto watcher = CreateWatcher(DirectoryWatcher::Event::Removed);
auto itemPath = m_scopedTestDir.GetPath() / L"item";
auto itemPidl = CreateSimplePidlForTest(itemPath, nullptr, ShellItemType::Folder);
EXPECT_CALL(m_callback, Call(DirectoryWatcher::Event::Removed, itemPidl, PidlAbsolute{}));
std::filesystem::create_directory(itemPath);
std::filesystem::remove(itemPath);
WaitForNotifications();
}
TEST_F(FileSystemWatcherTest, ResumeAfterDestruction)
{
auto watcher = CreateWatcher(DirectoryWatcher::Event::Added);
auto itemPath = m_scopedTestDir.GetPath() / L"item";
std::filesystem::create_directory(itemPath);
// To transfer back to the UI thread, the FileSystemWatcher instance will queue a task to
// UIThreadExecutor. That ultimately involves posting a message. This will wait (with a timeout)
// until that message has been posted. It's important to do this before the
// FileSystemWatcher instance is destroyed, otherwise the instance could be destroyed too
// early (before any change notifications have been processed).
std::chrono::milliseconds timeoutInMs = TIMEOUT_DURATION;
auto res = MsgWaitForMultipleObjectsEx(0, nullptr, static_cast<DWORD>(timeoutInMs.count()),
QS_ALLINPUT, 0);
ASSERT_NE(res, static_cast<DWORD>(WAIT_TIMEOUT));
watcher.reset();
// This should result in the queued task being processed. This should be a safe operation, even
// though the FileSystemWatcher instance has been destroyed.
m_messageLoop.RunUntilIdle();
}