]>
Commit | Line | Data |
---|---|---|
6b8ef0b3 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/fswatcher/fswatchertest.cpp | |
3 | // Purpose: wxFileSystemWatcher unit test | |
4 | // Author: Bartosz Bekier | |
5 | // Created: 2009-06-11 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2009 Bartosz Bekier | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "testprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #include "wx/evtloop.h" | |
21 | #include "wx/filename.h" | |
22 | #include "wx/filefn.h" | |
23 | #include "wx/stdpaths.h" | |
24 | #include "wx/fswatcher.h" | |
25 | ||
26 | #include "testfile.h" | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // local functions | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | // class generating file system events | |
33 | class EventGenerator | |
34 | { | |
35 | public: | |
36 | static EventGenerator& Get() | |
37 | { | |
38 | if (!ms_instance) | |
39 | ms_instance = new EventGenerator(GetWatchDir()); | |
40 | ||
41 | return *ms_instance; | |
42 | } | |
43 | ||
44 | EventGenerator(const wxFileName& path) : m_base(path) | |
45 | { | |
46 | m_old = wxFileName(); | |
47 | m_file = RandomName(); | |
48 | m_new = RandomName(); | |
49 | } | |
50 | ||
51 | // operations | |
52 | bool CreateFile() | |
53 | { | |
54 | wxFile file(m_file.GetFullPath(), wxFile::write); | |
55 | return file.IsOpened() && m_file.FileExists(); | |
56 | } | |
57 | ||
58 | bool RenameFile() | |
59 | { | |
60 | CPPUNIT_ASSERT(m_file.FileExists()); | |
61 | ||
62 | wxLogDebug("Renaming %s=>%s", m_file.GetFullPath(), m_new.GetFullPath()); | |
63 | ||
64 | bool ret = wxRenameFile(m_file.GetFullPath(), m_new.GetFullPath()); | |
65 | if (ret) | |
66 | { | |
67 | m_old = m_file; | |
68 | m_file = m_new; | |
69 | m_new = RandomName(); | |
70 | } | |
71 | ||
72 | return ret; | |
73 | } | |
74 | ||
75 | bool DeleteFile() | |
76 | { | |
77 | CPPUNIT_ASSERT(m_file.FileExists()); | |
78 | ||
79 | bool ret = wxRemoveFile(m_file.GetFullPath()); | |
80 | if (ret) | |
81 | { | |
82 | m_old = m_file; | |
83 | m_file = m_new; | |
84 | m_new = RandomName(); | |
85 | } | |
86 | ||
87 | return ret; | |
88 | } | |
89 | ||
90 | bool TouchFile() | |
91 | { | |
92 | return m_file.Touch(); | |
93 | } | |
94 | ||
95 | bool ReadFile() | |
96 | { | |
97 | wxFile f(m_file.GetFullPath()); | |
98 | CPPUNIT_ASSERT(f.IsOpened()); | |
99 | ||
100 | char buf[1]; | |
101 | ssize_t count = f.Read(buf, sizeof(buf)); | |
102 | CPPUNIT_ASSERT(count > 0); | |
103 | ||
104 | return true; | |
105 | } | |
106 | ||
107 | bool ModifyFile() | |
108 | { | |
109 | CPPUNIT_ASSERT(m_file.FileExists()); | |
110 | ||
111 | wxFile file(m_file.GetFullPath(), wxFile::write_append); | |
112 | CPPUNIT_ASSERT(file.IsOpened()); | |
113 | ||
114 | CPPUNIT_ASSERT(file.Write("Words of Wisdom, Lloyd. Words of wisdom\n")); | |
115 | return file.Close(); | |
116 | } | |
117 | ||
118 | // helpers | |
119 | wxFileName RandomName(int length = 10) | |
120 | { | |
121 | return RandomName(m_base, length); | |
122 | } | |
123 | ||
124 | // static helpers | |
125 | static const wxFileName& GetWatchDir() | |
126 | { | |
127 | static wxFileName dir; | |
128 | ||
129 | if (dir.DirExists()) | |
130 | return dir; | |
131 | ||
132 | wxString tmp = wxStandardPaths::Get().GetTempDir(); | |
133 | dir.AssignDir(tmp); | |
134 | ||
135 | // XXX look for more unique name? there is no function to generate | |
136 | // unique filename, the file always get created... | |
137 | dir.AppendDir("fswatcher_test"); | |
138 | CPPUNIT_ASSERT(!dir.DirExists()); | |
139 | CPPUNIT_ASSERT(dir.Mkdir()); | |
140 | ||
141 | return dir; | |
142 | } | |
143 | ||
144 | static void RemoveWatchDir() | |
145 | { | |
146 | wxFileName dir = GetWatchDir(); | |
147 | CPPUNIT_ASSERT(dir.DirExists()); | |
148 | ||
149 | // just to be really sure we know what we remove | |
771ce939 VZ |
150 | CPPUNIT_ASSERT_EQUAL( "fswatcher_test", dir.GetDirs().Last() ); |
151 | ||
152 | // FIXME-VC6: using non-static Rmdir() results in ICE | |
153 | CPPUNIT_ASSERT( wxFileName::Rmdir(dir.GetFullPath(), wxPATH_RMDIR_RECURSIVE) ); | |
6b8ef0b3 VZ |
154 | } |
155 | ||
156 | static wxFileName RandomName(const wxFileName& base, int length = 10) | |
157 | { | |
158 | static int ALFA_CNT = 'z' - 'a'; | |
159 | ||
160 | wxString s; | |
161 | for (int i = 0 ; i < length; ++i) | |
162 | { | |
163 | char c = 'a' + (rand() % ALFA_CNT); | |
164 | s += c; | |
165 | } | |
166 | ||
167 | return wxFileName(base.GetFullPath(), s); | |
168 | } | |
169 | ||
170 | public: | |
171 | wxFileName m_base; // base dir for doing operations | |
172 | wxFileName m_file; // current file name | |
173 | wxFileName m_old; // previous file name | |
174 | wxFileName m_new; // name after renaming | |
175 | ||
176 | protected: | |
177 | static EventGenerator* ms_instance; | |
178 | }; | |
179 | ||
180 | EventGenerator* EventGenerator::ms_instance = 0; | |
181 | ||
182 | ||
183 | // custom event handler | |
184 | class EventHandler : public wxEvtHandler | |
185 | { | |
186 | public: | |
771ce939 | 187 | enum { WAIT_DURATION = 3 }; |
6b8ef0b3 VZ |
188 | |
189 | EventHandler() : | |
190 | eg(EventGenerator::Get()), m_loop(0), m_count(0), m_watcher(0) | |
191 | { | |
192 | m_loop = new wxEventLoop(); | |
193 | Connect(wxEVT_IDLE, wxIdleEventHandler(EventHandler::OnIdle)); | |
194 | Connect(wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler( | |
195 | EventHandler::OnFileSystemEvent)); | |
196 | } | |
197 | ||
198 | virtual ~EventHandler() | |
199 | { | |
200 | delete m_watcher; | |
201 | if (m_loop) | |
202 | { | |
203 | if (m_loop->IsRunning()) | |
204 | m_loop->Exit(); | |
205 | delete m_loop; | |
206 | } | |
207 | } | |
208 | ||
209 | void Exit() | |
210 | { | |
211 | m_loop->Exit(); | |
212 | } | |
213 | ||
214 | // sends idle event, so we get called in a moment | |
215 | void SendIdle() | |
216 | { | |
217 | wxIdleEvent* e = new wxIdleEvent(); | |
218 | QueueEvent(e); | |
219 | } | |
220 | ||
221 | void Run() | |
222 | { | |
223 | SendIdle(); | |
224 | m_loop->Run(); | |
225 | } | |
226 | ||
227 | void OnIdle(wxIdleEvent& /*evt*/) | |
228 | { | |
229 | bool more = Action(); | |
230 | m_count++; | |
231 | ||
232 | if (more) | |
233 | { | |
234 | SendIdle(); | |
235 | } | |
236 | } | |
237 | ||
238 | // returns whether we should produce more idle events | |
239 | virtual bool Action() | |
240 | { | |
241 | switch (m_count) | |
242 | { | |
243 | case 0: | |
244 | CPPUNIT_ASSERT(Init()); | |
245 | break; | |
246 | case 1: | |
247 | GenerateEvent(); | |
248 | break; | |
249 | case 2: | |
250 | // actual test | |
251 | CPPUNIT_ASSERT(CheckResult()); | |
252 | Exit(); | |
253 | break; | |
254 | ||
255 | // TODO a mechanism that will break the loop in case we | |
256 | // don't receive a file system event | |
257 | // this below doesn't quite work, so all tests must pass :-) | |
258 | #if 0 | |
259 | case 2: | |
260 | m_loop.Yield(); | |
261 | m_loop.WakeUp(); | |
262 | CPPUNIT_ASSERT(KeepWaiting()); | |
263 | m_loop.Yield(); | |
264 | break; | |
265 | case 3: | |
266 | break; | |
267 | case 4: | |
268 | CPPUNIT_ASSERT(AfterWait()); | |
269 | break; | |
270 | #endif | |
271 | } // switch (m_count) | |
272 | ||
273 | return m_count <= 0; | |
274 | } | |
275 | ||
276 | virtual bool Init() | |
277 | { | |
278 | // test we're good to go | |
279 | CPPUNIT_ASSERT(wxEventLoopBase::GetActive()); | |
280 | ||
281 | // XXX only now can we construct Watcher, because we need | |
282 | // active loop here | |
283 | m_watcher = new wxFileSystemWatcher(); | |
284 | m_watcher->SetOwner(this); | |
285 | ||
286 | // add dir to be watched | |
287 | wxFileName dir = EventGenerator::GetWatchDir(); | |
288 | CPPUNIT_ASSERT(m_watcher->Add(dir, wxFSW_EVENT_ALL)); | |
289 | ||
290 | return true; | |
291 | } | |
292 | ||
293 | virtual bool KeepWaiting() | |
294 | { | |
295 | // did we receive event already? | |
296 | if (!tested) | |
297 | { | |
298 | // well, lets wait a bit more | |
299 | wxSleep(WAIT_DURATION); | |
300 | } | |
301 | ||
302 | return true; | |
303 | } | |
304 | ||
305 | virtual bool AfterWait() | |
306 | { | |
307 | // fail if still no events | |
d5236dff VZ |
308 | WX_ASSERT_MESSAGE |
309 | ( | |
310 | ("No events during %d seconds!", static_cast<int>(WAIT_DURATION)), | |
311 | tested | |
312 | ); | |
313 | ||
314 | return true; | |
6b8ef0b3 VZ |
315 | } |
316 | ||
317 | virtual void OnFileSystemEvent(wxFileSystemWatcherEvent& evt) | |
318 | { | |
319 | wxLogDebug("--- %s ---", evt.ToString()); | |
320 | m_lastEvent = wxDynamicCast(evt.Clone(), wxFileSystemWatcherEvent); | |
321 | m_events.Add(m_lastEvent); | |
322 | ||
323 | // test finished | |
324 | SendIdle(); | |
325 | tested = true; | |
326 | } | |
327 | ||
328 | virtual bool CheckResult() | |
329 | { | |
1c9c8e88 VZ |
330 | CPPUNIT_ASSERT_MESSAGE( "No events received", !m_events.empty() ); |
331 | ||
77241feb VZ |
332 | const wxFileSystemWatcherEvent * const e = m_events.front(); |
333 | ||
1c9c8e88 VZ |
334 | WX_ASSERT_EQUAL_MESSAGE |
335 | ( | |
336 | ( | |
77241feb VZ |
337 | "Extra events received, first is of type %x, for path=\"%s\"," |
338 | "last is of type %x, path=\"%s\"", | |
339 | e->GetChangeType(), | |
340 | e->GetPath().GetFullPath(), | |
1c9c8e88 VZ |
341 | m_events.back()->GetChangeType(), |
342 | m_events.back()->GetPath().GetFullPath() | |
343 | ), | |
344 | 1, m_events.size() | |
345 | ); | |
346 | ||
6b8ef0b3 VZ |
347 | // this is our "reference event" |
348 | const wxFileSystemWatcherEvent expected = ExpectedEvent(); | |
349 | ||
350 | CPPUNIT_ASSERT_EQUAL( expected.GetChangeType(), e->GetChangeType() ); | |
351 | ||
352 | CPPUNIT_ASSERT_EQUAL((int)wxEVT_FSWATCHER, e->GetEventType()); | |
353 | ||
354 | // XXX this needs change | |
355 | CPPUNIT_ASSERT_EQUAL(wxEVT_CATEGORY_UNKNOWN, e->GetEventCategory()); | |
356 | ||
357 | CPPUNIT_ASSERT_EQUAL(expected.GetPath(), e->GetPath()); | |
358 | CPPUNIT_ASSERT_EQUAL(expected.GetNewPath(), e->GetNewPath()); | |
6b8ef0b3 VZ |
359 | |
360 | return true; | |
361 | } | |
362 | ||
363 | virtual void GenerateEvent() = 0; | |
364 | ||
365 | virtual wxFileSystemWatcherEvent ExpectedEvent() = 0; | |
366 | ||
367 | ||
368 | protected: | |
369 | EventGenerator& eg; | |
370 | wxEventLoopBase* m_loop; // loop reference | |
371 | int m_count; // idle events count | |
372 | ||
373 | wxFileSystemWatcher* m_watcher; | |
374 | bool tested; // indicates, whether we have already passed the test | |
375 | ||
376 | #include "wx/arrimpl.cpp" | |
377 | WX_DEFINE_ARRAY_PTR(wxFileSystemWatcherEvent*, wxArrayEvent); | |
378 | wxArrayEvent m_events; | |
379 | wxFileSystemWatcherEvent* m_lastEvent; | |
380 | }; | |
381 | ||
382 | ||
383 | // ---------------------------------------------------------------------------- | |
384 | // test class | |
385 | // ---------------------------------------------------------------------------- | |
386 | ||
387 | class FileSystemWatcherTestCase : public CppUnit::TestCase | |
388 | { | |
389 | public: | |
390 | FileSystemWatcherTestCase() { } | |
391 | ||
392 | virtual void setUp(); | |
393 | virtual void tearDown(); | |
394 | ||
395 | protected: | |
396 | wxEventLoopBase* m_loop; | |
397 | ||
398 | private: | |
399 | CPPUNIT_TEST_SUITE( FileSystemWatcherTestCase ); | |
400 | CPPUNIT_TEST( TestEventCreate ); | |
401 | CPPUNIT_TEST( TestEventDelete ); | |
402 | ||
403 | // kqueue-based implementation doesn't collapse create/delete pairs in | |
404 | // renames and doesn't detect neither modifications nor access to the | |
405 | // files reliably currently so disable these tests | |
406 | // | |
407 | // FIXME: fix the code and reenable them | |
408 | #ifndef wxHAS_KQUEUE | |
409 | CPPUNIT_TEST( TestEventRename ); | |
410 | CPPUNIT_TEST( TestEventModify ); | |
411 | ||
412 | // MSW implementation doesn't detect file access events currently | |
413 | #ifndef __WXMSW__ | |
414 | CPPUNIT_TEST( TestEventAccess ); | |
415 | #endif // __WXMSW__ | |
416 | #endif // !wxHAS_KQUEUE | |
417 | CPPUNIT_TEST_SUITE_END(); | |
418 | ||
419 | void TestEventCreate(); | |
420 | void TestEventDelete(); | |
421 | void TestEventRename(); | |
422 | void TestEventModify(); | |
423 | void TestEventAccess(); | |
424 | ||
425 | DECLARE_NO_COPY_CLASS(FileSystemWatcherTestCase) | |
426 | }; | |
427 | ||
428 | // register in the unnamed registry so that these tests are run by default | |
429 | CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemWatcherTestCase ); | |
430 | ||
431 | // also include in it's own registry so that these tests can be run alone | |
432 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileSystemWatcherTestCase, | |
433 | "FileSystemWatcherTestCase" ); | |
434 | ||
435 | void FileSystemWatcherTestCase::setUp() | |
436 | { | |
437 | wxLog::AddTraceMask(wxTRACE_FSWATCHER); | |
438 | EventGenerator::Get().GetWatchDir(); | |
439 | } | |
440 | ||
441 | void FileSystemWatcherTestCase::tearDown() | |
442 | { | |
443 | EventGenerator::Get().RemoveWatchDir(); | |
444 | } | |
445 | ||
446 | // ---------------------------------------------------------------------------- | |
447 | // TestEventCreate | |
448 | // ---------------------------------------------------------------------------- | |
449 | void FileSystemWatcherTestCase::TestEventCreate() | |
450 | { | |
451 | wxLogDebug("TestEventCreate()"); | |
452 | ||
453 | class EventTester : public EventHandler | |
454 | { | |
455 | public: | |
456 | virtual void GenerateEvent() | |
457 | { | |
458 | CPPUNIT_ASSERT(eg.CreateFile()); | |
459 | } | |
460 | ||
461 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
462 | { | |
463 | wxFileSystemWatcherEvent event(wxFSW_EVENT_CREATE); | |
464 | event.SetPath(eg.m_file); | |
465 | event.SetNewPath(eg.m_file); | |
466 | return event; | |
467 | } | |
468 | }; | |
469 | ||
470 | EventTester tester; | |
471 | ||
472 | wxLogTrace(wxTRACE_FSWATCHER, "TestEventCreate tester created()"); | |
473 | ||
474 | tester.Run(); | |
475 | } | |
476 | ||
477 | // ---------------------------------------------------------------------------- | |
478 | // TestEventDelete | |
479 | // ---------------------------------------------------------------------------- | |
480 | void FileSystemWatcherTestCase::TestEventDelete() | |
481 | { | |
482 | wxLogDebug("TestEventDelete()"); | |
483 | ||
484 | class EventTester : public EventHandler | |
485 | { | |
486 | public: | |
487 | virtual void GenerateEvent() | |
488 | { | |
489 | CPPUNIT_ASSERT(eg.DeleteFile()); | |
490 | } | |
491 | ||
492 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
493 | { | |
494 | wxFileSystemWatcherEvent event(wxFSW_EVENT_DELETE); | |
495 | event.SetPath(eg.m_old); | |
496 | ||
497 | // CHECK maybe new path here could be NULL or sth? | |
498 | event.SetNewPath(eg.m_old); | |
499 | return event; | |
500 | } | |
501 | }; | |
502 | ||
503 | // we need to create a file now, so we can delete it | |
504 | EventGenerator::Get().CreateFile(); | |
505 | ||
506 | EventTester tester; | |
507 | tester.Run(); | |
508 | } | |
509 | ||
510 | // ---------------------------------------------------------------------------- | |
511 | // TestEventRename | |
512 | // ---------------------------------------------------------------------------- | |
513 | void FileSystemWatcherTestCase::TestEventRename() | |
514 | { | |
515 | wxLogDebug("TestEventRename()"); | |
516 | ||
517 | class EventTester : public EventHandler | |
518 | { | |
519 | public: | |
520 | virtual void GenerateEvent() | |
521 | { | |
522 | CPPUNIT_ASSERT(eg.RenameFile()); | |
523 | } | |
524 | ||
525 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
526 | { | |
527 | wxFileSystemWatcherEvent event(wxFSW_EVENT_RENAME); | |
528 | event.SetPath(eg.m_old); | |
529 | event.SetNewPath(eg.m_file); | |
530 | return event; | |
531 | } | |
532 | }; | |
533 | ||
534 | // need a file to rename later | |
535 | EventGenerator::Get().CreateFile(); | |
536 | ||
537 | EventTester tester; | |
538 | tester.Run(); | |
539 | } | |
540 | ||
541 | // ---------------------------------------------------------------------------- | |
542 | // TestEventModify | |
543 | // ---------------------------------------------------------------------------- | |
544 | void FileSystemWatcherTestCase::TestEventModify() | |
545 | { | |
546 | wxLogDebug("TestEventModify()"); | |
547 | ||
548 | class EventTester : public EventHandler | |
549 | { | |
550 | public: | |
551 | virtual void GenerateEvent() | |
552 | { | |
553 | CPPUNIT_ASSERT(eg.ModifyFile()); | |
554 | } | |
555 | ||
556 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
557 | { | |
558 | wxFileSystemWatcherEvent event(wxFSW_EVENT_MODIFY); | |
559 | event.SetPath(eg.m_file); | |
560 | event.SetNewPath(eg.m_file); | |
561 | return event; | |
562 | } | |
563 | }; | |
564 | ||
565 | // we need to create a file to modify | |
566 | EventGenerator::Get().CreateFile(); | |
567 | ||
568 | EventTester tester; | |
569 | tester.Run(); | |
570 | } | |
571 | ||
572 | // ---------------------------------------------------------------------------- | |
573 | // TestEventAccess | |
574 | // ---------------------------------------------------------------------------- | |
575 | void FileSystemWatcherTestCase::TestEventAccess() | |
576 | { | |
577 | wxLogDebug("TestEventAccess()"); | |
578 | ||
579 | class EventTester : public EventHandler | |
580 | { | |
581 | public: | |
582 | virtual void GenerateEvent() | |
583 | { | |
584 | CPPUNIT_ASSERT(eg.ReadFile()); | |
585 | } | |
586 | ||
587 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
588 | { | |
589 | wxFileSystemWatcherEvent event(wxFSW_EVENT_ACCESS); | |
590 | event.SetPath(eg.m_file); | |
591 | event.SetNewPath(eg.m_file); | |
592 | return event; | |
593 | } | |
594 | }; | |
595 | ||
596 | // we need to create a file to read from it and write sth to it | |
597 | EventGenerator::Get().CreateFile(); | |
598 | EventGenerator::Get().ModifyFile(); | |
599 | ||
600 | EventTester tester; | |
601 | tester.Run(); | |
602 | } |