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 | ||
6a9455d3 VZ |
20 | #ifndef WX_PRECOMP |
21 | #include "wx/timer.h" | |
22 | #endif | |
23 | ||
6b8ef0b3 VZ |
24 | #include "wx/evtloop.h" |
25 | #include "wx/filename.h" | |
26 | #include "wx/filefn.h" | |
27 | #include "wx/stdpaths.h" | |
28 | #include "wx/fswatcher.h" | |
29 | ||
30 | #include "testfile.h" | |
31 | ||
32 | // ---------------------------------------------------------------------------- | |
33 | // local functions | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | // class generating file system events | |
37 | class EventGenerator | |
38 | { | |
39 | public: | |
40 | static EventGenerator& Get() | |
41 | { | |
42 | if (!ms_instance) | |
43 | ms_instance = new EventGenerator(GetWatchDir()); | |
44 | ||
45 | return *ms_instance; | |
46 | } | |
47 | ||
48 | EventGenerator(const wxFileName& path) : m_base(path) | |
49 | { | |
50 | m_old = wxFileName(); | |
51 | m_file = RandomName(); | |
52 | m_new = RandomName(); | |
53 | } | |
54 | ||
55 | // operations | |
56 | bool CreateFile() | |
57 | { | |
58 | wxFile file(m_file.GetFullPath(), wxFile::write); | |
59 | return file.IsOpened() && m_file.FileExists(); | |
60 | } | |
61 | ||
62 | bool RenameFile() | |
63 | { | |
64 | CPPUNIT_ASSERT(m_file.FileExists()); | |
65 | ||
66 | wxLogDebug("Renaming %s=>%s", m_file.GetFullPath(), m_new.GetFullPath()); | |
67 | ||
68 | bool ret = wxRenameFile(m_file.GetFullPath(), m_new.GetFullPath()); | |
69 | if (ret) | |
70 | { | |
71 | m_old = m_file; | |
72 | m_file = m_new; | |
73 | m_new = RandomName(); | |
74 | } | |
75 | ||
76 | return ret; | |
77 | } | |
78 | ||
79 | bool DeleteFile() | |
80 | { | |
81 | CPPUNIT_ASSERT(m_file.FileExists()); | |
82 | ||
83 | bool ret = wxRemoveFile(m_file.GetFullPath()); | |
84 | if (ret) | |
85 | { | |
86 | m_old = m_file; | |
87 | m_file = m_new; | |
88 | m_new = RandomName(); | |
89 | } | |
90 | ||
91 | return ret; | |
92 | } | |
93 | ||
94 | bool TouchFile() | |
95 | { | |
96 | return m_file.Touch(); | |
97 | } | |
98 | ||
99 | bool ReadFile() | |
100 | { | |
101 | wxFile f(m_file.GetFullPath()); | |
102 | CPPUNIT_ASSERT(f.IsOpened()); | |
103 | ||
104 | char buf[1]; | |
105 | ssize_t count = f.Read(buf, sizeof(buf)); | |
106 | CPPUNIT_ASSERT(count > 0); | |
107 | ||
108 | return true; | |
109 | } | |
110 | ||
111 | bool ModifyFile() | |
112 | { | |
113 | CPPUNIT_ASSERT(m_file.FileExists()); | |
114 | ||
115 | wxFile file(m_file.GetFullPath(), wxFile::write_append); | |
116 | CPPUNIT_ASSERT(file.IsOpened()); | |
117 | ||
118 | CPPUNIT_ASSERT(file.Write("Words of Wisdom, Lloyd. Words of wisdom\n")); | |
119 | return file.Close(); | |
120 | } | |
121 | ||
122 | // helpers | |
123 | wxFileName RandomName(int length = 10) | |
124 | { | |
125 | return RandomName(m_base, length); | |
126 | } | |
127 | ||
128 | // static helpers | |
129 | static const wxFileName& GetWatchDir() | |
130 | { | |
131 | static wxFileName dir; | |
132 | ||
133 | if (dir.DirExists()) | |
134 | return dir; | |
135 | ||
136 | wxString tmp = wxStandardPaths::Get().GetTempDir(); | |
137 | dir.AssignDir(tmp); | |
138 | ||
139 | // XXX look for more unique name? there is no function to generate | |
140 | // unique filename, the file always get created... | |
141 | dir.AppendDir("fswatcher_test"); | |
142 | CPPUNIT_ASSERT(!dir.DirExists()); | |
143 | CPPUNIT_ASSERT(dir.Mkdir()); | |
144 | ||
145 | return dir; | |
146 | } | |
147 | ||
148 | static void RemoveWatchDir() | |
149 | { | |
150 | wxFileName dir = GetWatchDir(); | |
151 | CPPUNIT_ASSERT(dir.DirExists()); | |
152 | ||
153 | // just to be really sure we know what we remove | |
771ce939 VZ |
154 | CPPUNIT_ASSERT_EQUAL( "fswatcher_test", dir.GetDirs().Last() ); |
155 | ||
156 | // FIXME-VC6: using non-static Rmdir() results in ICE | |
157 | CPPUNIT_ASSERT( wxFileName::Rmdir(dir.GetFullPath(), wxPATH_RMDIR_RECURSIVE) ); | |
6b8ef0b3 VZ |
158 | } |
159 | ||
160 | static wxFileName RandomName(const wxFileName& base, int length = 10) | |
161 | { | |
162 | static int ALFA_CNT = 'z' - 'a'; | |
163 | ||
164 | wxString s; | |
165 | for (int i = 0 ; i < length; ++i) | |
166 | { | |
167 | char c = 'a' + (rand() % ALFA_CNT); | |
168 | s += c; | |
169 | } | |
170 | ||
171 | return wxFileName(base.GetFullPath(), s); | |
172 | } | |
173 | ||
174 | public: | |
175 | wxFileName m_base; // base dir for doing operations | |
176 | wxFileName m_file; // current file name | |
177 | wxFileName m_old; // previous file name | |
178 | wxFileName m_new; // name after renaming | |
179 | ||
180 | protected: | |
181 | static EventGenerator* ms_instance; | |
182 | }; | |
183 | ||
184 | EventGenerator* EventGenerator::ms_instance = 0; | |
185 | ||
186 | ||
187 | // custom event handler | |
188 | class EventHandler : public wxEvtHandler | |
189 | { | |
190 | public: | |
771ce939 | 191 | enum { WAIT_DURATION = 3 }; |
6b8ef0b3 VZ |
192 | |
193 | EventHandler() : | |
194 | eg(EventGenerator::Get()), m_loop(0), m_count(0), m_watcher(0) | |
195 | { | |
196 | m_loop = new wxEventLoop(); | |
197 | Connect(wxEVT_IDLE, wxIdleEventHandler(EventHandler::OnIdle)); | |
198 | Connect(wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler( | |
199 | EventHandler::OnFileSystemEvent)); | |
200 | } | |
201 | ||
202 | virtual ~EventHandler() | |
203 | { | |
204 | delete m_watcher; | |
205 | if (m_loop) | |
206 | { | |
207 | if (m_loop->IsRunning()) | |
208 | m_loop->Exit(); | |
209 | delete m_loop; | |
210 | } | |
211 | } | |
212 | ||
213 | void Exit() | |
214 | { | |
215 | m_loop->Exit(); | |
216 | } | |
217 | ||
218 | // sends idle event, so we get called in a moment | |
219 | void SendIdle() | |
220 | { | |
221 | wxIdleEvent* e = new wxIdleEvent(); | |
222 | QueueEvent(e); | |
223 | } | |
224 | ||
225 | void Run() | |
226 | { | |
227 | SendIdle(); | |
228 | m_loop->Run(); | |
229 | } | |
230 | ||
231 | void OnIdle(wxIdleEvent& /*evt*/) | |
232 | { | |
233 | bool more = Action(); | |
234 | m_count++; | |
235 | ||
236 | if (more) | |
237 | { | |
238 | SendIdle(); | |
239 | } | |
240 | } | |
241 | ||
242 | // returns whether we should produce more idle events | |
243 | virtual bool Action() | |
244 | { | |
245 | switch (m_count) | |
246 | { | |
247 | case 0: | |
248 | CPPUNIT_ASSERT(Init()); | |
249 | break; | |
250 | case 1: | |
251 | GenerateEvent(); | |
252 | break; | |
253 | case 2: | |
254 | // actual test | |
17e23c0c | 255 | CheckResult(); |
6b8ef0b3 VZ |
256 | Exit(); |
257 | break; | |
258 | ||
259 | // TODO a mechanism that will break the loop in case we | |
260 | // don't receive a file system event | |
261 | // this below doesn't quite work, so all tests must pass :-) | |
262 | #if 0 | |
263 | case 2: | |
264 | m_loop.Yield(); | |
265 | m_loop.WakeUp(); | |
266 | CPPUNIT_ASSERT(KeepWaiting()); | |
267 | m_loop.Yield(); | |
268 | break; | |
269 | case 3: | |
270 | break; | |
271 | case 4: | |
272 | CPPUNIT_ASSERT(AfterWait()); | |
273 | break; | |
274 | #endif | |
275 | } // switch (m_count) | |
276 | ||
277 | return m_count <= 0; | |
278 | } | |
279 | ||
280 | virtual bool Init() | |
281 | { | |
282 | // test we're good to go | |
283 | CPPUNIT_ASSERT(wxEventLoopBase::GetActive()); | |
284 | ||
285 | // XXX only now can we construct Watcher, because we need | |
286 | // active loop here | |
287 | m_watcher = new wxFileSystemWatcher(); | |
288 | m_watcher->SetOwner(this); | |
289 | ||
290 | // add dir to be watched | |
291 | wxFileName dir = EventGenerator::GetWatchDir(); | |
292 | CPPUNIT_ASSERT(m_watcher->Add(dir, wxFSW_EVENT_ALL)); | |
293 | ||
294 | return true; | |
295 | } | |
296 | ||
297 | virtual bool KeepWaiting() | |
298 | { | |
299 | // did we receive event already? | |
300 | if (!tested) | |
301 | { | |
4c51a665 | 302 | // well, let's wait a bit more |
6b8ef0b3 VZ |
303 | wxSleep(WAIT_DURATION); |
304 | } | |
305 | ||
306 | return true; | |
307 | } | |
308 | ||
309 | virtual bool AfterWait() | |
310 | { | |
311 | // fail if still no events | |
d5236dff VZ |
312 | WX_ASSERT_MESSAGE |
313 | ( | |
314 | ("No events during %d seconds!", static_cast<int>(WAIT_DURATION)), | |
315 | tested | |
316 | ); | |
317 | ||
318 | return true; | |
6b8ef0b3 VZ |
319 | } |
320 | ||
321 | virtual void OnFileSystemEvent(wxFileSystemWatcherEvent& evt) | |
322 | { | |
323 | wxLogDebug("--- %s ---", evt.ToString()); | |
324 | m_lastEvent = wxDynamicCast(evt.Clone(), wxFileSystemWatcherEvent); | |
325 | m_events.Add(m_lastEvent); | |
326 | ||
327 | // test finished | |
328 | SendIdle(); | |
329 | tested = true; | |
330 | } | |
331 | ||
17e23c0c | 332 | virtual void CheckResult() |
6b8ef0b3 | 333 | { |
1c9c8e88 VZ |
334 | CPPUNIT_ASSERT_MESSAGE( "No events received", !m_events.empty() ); |
335 | ||
77241feb VZ |
336 | const wxFileSystemWatcherEvent * const e = m_events.front(); |
337 | ||
6b8ef0b3 VZ |
338 | // this is our "reference event" |
339 | const wxFileSystemWatcherEvent expected = ExpectedEvent(); | |
340 | ||
341 | CPPUNIT_ASSERT_EQUAL( expected.GetChangeType(), e->GetChangeType() ); | |
342 | ||
343 | CPPUNIT_ASSERT_EQUAL((int)wxEVT_FSWATCHER, e->GetEventType()); | |
344 | ||
345 | // XXX this needs change | |
346 | CPPUNIT_ASSERT_EQUAL(wxEVT_CATEGORY_UNKNOWN, e->GetEventCategory()); | |
347 | ||
348 | CPPUNIT_ASSERT_EQUAL(expected.GetPath(), e->GetPath()); | |
349 | CPPUNIT_ASSERT_EQUAL(expected.GetNewPath(), e->GetNewPath()); | |
69b554dc | 350 | |
921e411c VZ |
351 | // Under MSW extra modification events are sometimes reported after a |
352 | // rename and we just can't get rid of them, so ignore them in this | |
353 | // test if they do happen. | |
354 | if ( e->GetChangeType() == wxFSW_EVENT_RENAME && | |
355 | m_events.size() == 2 ) | |
356 | { | |
357 | const wxFileSystemWatcherEvent* const e2 = m_events.back(); | |
358 | if ( e2->GetChangeType() == wxFSW_EVENT_MODIFY && | |
359 | e2->GetPath() == e->GetNewPath() ) | |
360 | { | |
361 | // This is a modify event for the new file, ignore it. | |
362 | return; | |
363 | } | |
364 | } | |
365 | ||
69b554dc VZ |
366 | WX_ASSERT_EQUAL_MESSAGE |
367 | ( | |
368 | ( | |
369 | "Extra events received, last one is of type %x, path=\"%s\" " | |
370 | "(the original event was for \"%s\" (\"%s\")", | |
371 | m_events.back()->GetChangeType(), | |
372 | m_events.back()->GetPath().GetFullPath(), | |
373 | e->GetPath().GetFullPath(), | |
374 | e->GetNewPath().GetFullPath() | |
375 | ), | |
376 | 1, m_events.size() | |
377 | ); | |
378 | ||
6b8ef0b3 VZ |
379 | } |
380 | ||
381 | virtual void GenerateEvent() = 0; | |
382 | ||
383 | virtual wxFileSystemWatcherEvent ExpectedEvent() = 0; | |
384 | ||
385 | ||
386 | protected: | |
387 | EventGenerator& eg; | |
388 | wxEventLoopBase* m_loop; // loop reference | |
389 | int m_count; // idle events count | |
390 | ||
391 | wxFileSystemWatcher* m_watcher; | |
392 | bool tested; // indicates, whether we have already passed the test | |
393 | ||
394 | #include "wx/arrimpl.cpp" | |
395 | WX_DEFINE_ARRAY_PTR(wxFileSystemWatcherEvent*, wxArrayEvent); | |
396 | wxArrayEvent m_events; | |
397 | wxFileSystemWatcherEvent* m_lastEvent; | |
398 | }; | |
399 | ||
400 | ||
401 | // ---------------------------------------------------------------------------- | |
402 | // test class | |
403 | // ---------------------------------------------------------------------------- | |
404 | ||
405 | class FileSystemWatcherTestCase : public CppUnit::TestCase | |
406 | { | |
407 | public: | |
408 | FileSystemWatcherTestCase() { } | |
409 | ||
410 | virtual void setUp(); | |
411 | virtual void tearDown(); | |
412 | ||
413 | protected: | |
414 | wxEventLoopBase* m_loop; | |
415 | ||
416 | private: | |
417 | CPPUNIT_TEST_SUITE( FileSystemWatcherTestCase ); | |
418 | CPPUNIT_TEST( TestEventCreate ); | |
419 | CPPUNIT_TEST( TestEventDelete ); | |
227dee95 | 420 | #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7) |
2b6d227c | 421 | CPPUNIT_TEST( TestTrees ); |
227dee95 | 422 | #endif |
6b8ef0b3 VZ |
423 | |
424 | // kqueue-based implementation doesn't collapse create/delete pairs in | |
425 | // renames and doesn't detect neither modifications nor access to the | |
426 | // files reliably currently so disable these tests | |
427 | // | |
428 | // FIXME: fix the code and reenable them | |
429 | #ifndef wxHAS_KQUEUE | |
430 | CPPUNIT_TEST( TestEventRename ); | |
431 | CPPUNIT_TEST( TestEventModify ); | |
432 | ||
433 | // MSW implementation doesn't detect file access events currently | |
bb5a9514 | 434 | #ifndef __WINDOWS__ |
6b8ef0b3 | 435 | CPPUNIT_TEST( TestEventAccess ); |
bb5a9514 | 436 | #endif // __WINDOWS__ |
6b8ef0b3 | 437 | #endif // !wxHAS_KQUEUE |
51fb8678 VZ |
438 | |
439 | CPPUNIT_TEST( TestNoEventsAfterRemove ); | |
6b8ef0b3 VZ |
440 | CPPUNIT_TEST_SUITE_END(); |
441 | ||
442 | void TestEventCreate(); | |
443 | void TestEventDelete(); | |
444 | void TestEventRename(); | |
445 | void TestEventModify(); | |
446 | void TestEventAccess(); | |
227dee95 VZ |
447 | #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7) |
448 | void TestTrees(); // Visual C++ 6 can't build this | |
449 | #endif | |
51fb8678 VZ |
450 | void TestNoEventsAfterRemove(); |
451 | ||
6b8ef0b3 VZ |
452 | DECLARE_NO_COPY_CLASS(FileSystemWatcherTestCase) |
453 | }; | |
454 | ||
b66a6888 VZ |
455 | // the test currently hangs under OS X for some reason and this prevents tests |
456 | // ran by buildbot from completing so disable it until someone has time to | |
457 | // debug it | |
458 | // | |
459 | // FIXME: debug and fix this! | |
460 | #ifndef __WXOSX__ | |
6b8ef0b3 VZ |
461 | // register in the unnamed registry so that these tests are run by default |
462 | CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemWatcherTestCase ); | |
b66a6888 | 463 | #endif |
6b8ef0b3 | 464 | |
e3778b4d | 465 | // also include in its own registry so that these tests can be run alone |
6b8ef0b3 VZ |
466 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileSystemWatcherTestCase, |
467 | "FileSystemWatcherTestCase" ); | |
468 | ||
469 | void FileSystemWatcherTestCase::setUp() | |
470 | { | |
471 | wxLog::AddTraceMask(wxTRACE_FSWATCHER); | |
472 | EventGenerator::Get().GetWatchDir(); | |
473 | } | |
474 | ||
475 | void FileSystemWatcherTestCase::tearDown() | |
476 | { | |
477 | EventGenerator::Get().RemoveWatchDir(); | |
478 | } | |
479 | ||
480 | // ---------------------------------------------------------------------------- | |
481 | // TestEventCreate | |
482 | // ---------------------------------------------------------------------------- | |
483 | void FileSystemWatcherTestCase::TestEventCreate() | |
484 | { | |
485 | wxLogDebug("TestEventCreate()"); | |
486 | ||
487 | class EventTester : public EventHandler | |
488 | { | |
489 | public: | |
490 | virtual void GenerateEvent() | |
491 | { | |
492 | CPPUNIT_ASSERT(eg.CreateFile()); | |
493 | } | |
494 | ||
495 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
496 | { | |
497 | wxFileSystemWatcherEvent event(wxFSW_EVENT_CREATE); | |
498 | event.SetPath(eg.m_file); | |
499 | event.SetNewPath(eg.m_file); | |
500 | return event; | |
501 | } | |
502 | }; | |
503 | ||
504 | EventTester tester; | |
505 | ||
506 | wxLogTrace(wxTRACE_FSWATCHER, "TestEventCreate tester created()"); | |
507 | ||
508 | tester.Run(); | |
509 | } | |
510 | ||
511 | // ---------------------------------------------------------------------------- | |
512 | // TestEventDelete | |
513 | // ---------------------------------------------------------------------------- | |
514 | void FileSystemWatcherTestCase::TestEventDelete() | |
515 | { | |
516 | wxLogDebug("TestEventDelete()"); | |
517 | ||
518 | class EventTester : public EventHandler | |
519 | { | |
520 | public: | |
521 | virtual void GenerateEvent() | |
522 | { | |
523 | CPPUNIT_ASSERT(eg.DeleteFile()); | |
524 | } | |
525 | ||
526 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
527 | { | |
528 | wxFileSystemWatcherEvent event(wxFSW_EVENT_DELETE); | |
529 | event.SetPath(eg.m_old); | |
530 | ||
531 | // CHECK maybe new path here could be NULL or sth? | |
532 | event.SetNewPath(eg.m_old); | |
533 | return event; | |
534 | } | |
535 | }; | |
536 | ||
537 | // we need to create a file now, so we can delete it | |
538 | EventGenerator::Get().CreateFile(); | |
539 | ||
540 | EventTester tester; | |
541 | tester.Run(); | |
542 | } | |
543 | ||
544 | // ---------------------------------------------------------------------------- | |
545 | // TestEventRename | |
546 | // ---------------------------------------------------------------------------- | |
547 | void FileSystemWatcherTestCase::TestEventRename() | |
548 | { | |
549 | wxLogDebug("TestEventRename()"); | |
550 | ||
551 | class EventTester : public EventHandler | |
552 | { | |
553 | public: | |
554 | virtual void GenerateEvent() | |
555 | { | |
556 | CPPUNIT_ASSERT(eg.RenameFile()); | |
557 | } | |
558 | ||
559 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
560 | { | |
561 | wxFileSystemWatcherEvent event(wxFSW_EVENT_RENAME); | |
562 | event.SetPath(eg.m_old); | |
563 | event.SetNewPath(eg.m_file); | |
564 | return event; | |
565 | } | |
566 | }; | |
567 | ||
568 | // need a file to rename later | |
569 | EventGenerator::Get().CreateFile(); | |
570 | ||
571 | EventTester tester; | |
572 | tester.Run(); | |
573 | } | |
574 | ||
575 | // ---------------------------------------------------------------------------- | |
576 | // TestEventModify | |
577 | // ---------------------------------------------------------------------------- | |
578 | void FileSystemWatcherTestCase::TestEventModify() | |
579 | { | |
580 | wxLogDebug("TestEventModify()"); | |
581 | ||
582 | class EventTester : public EventHandler | |
583 | { | |
584 | public: | |
585 | virtual void GenerateEvent() | |
586 | { | |
587 | CPPUNIT_ASSERT(eg.ModifyFile()); | |
588 | } | |
589 | ||
590 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
591 | { | |
592 | wxFileSystemWatcherEvent event(wxFSW_EVENT_MODIFY); | |
593 | event.SetPath(eg.m_file); | |
594 | event.SetNewPath(eg.m_file); | |
595 | return event; | |
596 | } | |
597 | }; | |
598 | ||
599 | // we need to create a file to modify | |
600 | EventGenerator::Get().CreateFile(); | |
601 | ||
602 | EventTester tester; | |
603 | tester.Run(); | |
604 | } | |
605 | ||
606 | // ---------------------------------------------------------------------------- | |
607 | // TestEventAccess | |
608 | // ---------------------------------------------------------------------------- | |
609 | void FileSystemWatcherTestCase::TestEventAccess() | |
610 | { | |
611 | wxLogDebug("TestEventAccess()"); | |
612 | ||
613 | class EventTester : public EventHandler | |
614 | { | |
615 | public: | |
616 | virtual void GenerateEvent() | |
617 | { | |
618 | CPPUNIT_ASSERT(eg.ReadFile()); | |
619 | } | |
620 | ||
621 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
622 | { | |
623 | wxFileSystemWatcherEvent event(wxFSW_EVENT_ACCESS); | |
624 | event.SetPath(eg.m_file); | |
625 | event.SetNewPath(eg.m_file); | |
626 | return event; | |
627 | } | |
628 | }; | |
629 | ||
630 | // we need to create a file to read from it and write sth to it | |
631 | EventGenerator::Get().CreateFile(); | |
632 | EventGenerator::Get().ModifyFile(); | |
633 | ||
634 | EventTester tester; | |
635 | tester.Run(); | |
636 | } | |
51fb8678 | 637 | |
2b6d227c VZ |
638 | // ---------------------------------------------------------------------------- |
639 | // TestTrees | |
640 | // ---------------------------------------------------------------------------- | |
227dee95 VZ |
641 | |
642 | #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7) | |
2b6d227c VZ |
643 | void FileSystemWatcherTestCase::TestTrees() |
644 | { | |
645 | class TreeTester : public EventHandler | |
646 | { | |
647 | const size_t subdirs; | |
648 | const size_t files; | |
649 | ||
650 | public: | |
651 | TreeTester() : subdirs(5), files(3) {} | |
652 | ||
0fccda2c | 653 | void GrowTree(wxFileName dir, bool withSymlinks) |
2b6d227c VZ |
654 | { |
655 | CPPUNIT_ASSERT(dir.Mkdir()); | |
76cfd1bf VZ |
656 | // Now add a subdir with an easy name to remember in WatchTree() |
657 | dir.AppendDir("child"); | |
658 | CPPUNIT_ASSERT(dir.Mkdir()); | |
0fccda2c | 659 | wxFileName child(dir); // Create a copy to which to symlink |
2b6d227c VZ |
660 | |
661 | // Create a branch of 5 numbered subdirs, each containing 3 | |
662 | // numbered files | |
663 | for ( unsigned d = 0; d < subdirs; ++d ) | |
664 | { | |
665 | dir.AppendDir(wxString::Format("subdir%u", d+1)); | |
666 | CPPUNIT_ASSERT(dir.Mkdir()); | |
667 | ||
668 | const wxString prefix = dir.GetPathWithSep(); | |
227dee95 | 669 | const wxString ext[] = { ".txt", ".log", "" }; |
2b6d227c VZ |
670 | for ( unsigned f = 0; f < files; ++f ) |
671 | { | |
672 | // Just create the files. | |
227dee95 | 673 | wxFile(prefix + wxString::Format("file%u", f+1) + ext[f], |
2b6d227c VZ |
674 | wxFile::write); |
675 | } | |
0fccda2c VZ |
676 | #if defined(__UNIX__) |
677 | if ( withSymlinks ) | |
678 | { | |
679 | // Create a symlink to a files, and another to 'child' | |
680 | CPPUNIT_ASSERT_EQUAL(0, | |
681 | symlink(wxString(prefix + "file1").c_str(), | |
682 | wxString(prefix + "file.lnk").c_str())); | |
683 | CPPUNIT_ASSERT_EQUAL(0, | |
684 | symlink(child.GetFullPath().c_str(), | |
685 | wxString(prefix + "dir.lnk").c_str())); | |
686 | } | |
687 | #endif // __UNIX__ | |
2b6d227c VZ |
688 | } |
689 | } | |
690 | ||
691 | void RmDir(wxFileName dir) | |
692 | { | |
693 | CPPUNIT_ASSERT(dir.DirExists()); | |
694 | ||
695 | CPPUNIT_ASSERT(dir.Rmdir(wxPATH_RMDIR_RECURSIVE)); | |
696 | } | |
697 | ||
698 | void WatchDir(wxFileName dir) | |
699 | { | |
700 | CPPUNIT_ASSERT(m_watcher); | |
701 | ||
702 | // Store the initial count; there may already be some watches | |
703 | const int initial = m_watcher->GetWatchedPathsCount(); | |
704 | ||
705 | m_watcher->Add(dir); | |
706 | CPPUNIT_ASSERT_EQUAL(initial + 1, | |
707 | m_watcher->GetWatchedPathsCount()); | |
708 | } | |
709 | ||
710 | void RemoveSingleWatch(wxFileName dir) | |
711 | { | |
712 | CPPUNIT_ASSERT(m_watcher); | |
713 | ||
714 | const int initial = m_watcher->GetWatchedPathsCount(); | |
715 | ||
716 | m_watcher->Remove(dir); | |
717 | CPPUNIT_ASSERT_EQUAL(initial - 1, | |
718 | m_watcher->GetWatchedPathsCount()); | |
719 | } | |
720 | ||
721 | void WatchTree(const wxFileName& dir) | |
722 | { | |
723 | CPPUNIT_ASSERT(m_watcher); | |
724 | ||
227dee95 VZ |
725 | size_t treeitems = 1; // the trunk |
726 | #ifndef __WINDOWS__ | |
727 | // When there's no file mask, wxMSW sets a single watch | |
728 | // on the trunk which is implemented recursively. | |
6eef5763 VZ |
729 | // wxGTK always sets an additional watch for each subdir |
730 | treeitems += subdirs + 1; // +1 for 'child' | |
227dee95 | 731 | #endif // __WINDOWS__ |
2b6d227c VZ |
732 | |
733 | // Store the initial count; there may already be some watches | |
734 | const int initial = m_watcher->GetWatchedPathsCount(); | |
735 | ||
0fccda2c | 736 | GrowTree(dir, false /* no symlinks */); |
2b6d227c VZ |
737 | |
738 | m_watcher->AddTree(dir); | |
739 | const int plustree = m_watcher->GetWatchedPathsCount(); | |
740 | ||
741 | CPPUNIT_ASSERT_EQUAL(initial + treeitems, plustree); | |
742 | ||
743 | m_watcher->RemoveTree(dir); | |
744 | CPPUNIT_ASSERT_EQUAL(initial, m_watcher->GetWatchedPathsCount()); | |
76cfd1bf VZ |
745 | |
746 | // Now test the refcount mechanism by watching items more than once | |
747 | wxFileName child(dir); | |
748 | child.AppendDir("child"); | |
749 | m_watcher->AddTree(child); | |
750 | // Check some watches were added; we don't care about the number | |
751 | CPPUNIT_ASSERT(initial < m_watcher->GetWatchedPathsCount()); | |
752 | // Now watch the whole tree and check that the count is the same | |
753 | // as it was the first time, despite also adding 'child' separately | |
754 | // Except that in wxMSW this isn't true: each watch will be a | |
755 | // single, recursive dir; so fudge the count | |
756 | size_t fudge = 0; | |
757 | #ifdef __WINDOWS__ | |
758 | fudge = 1; | |
759 | #endif // __WINDOWS__ | |
760 | m_watcher->AddTree(dir); | |
761 | CPPUNIT_ASSERT_EQUAL(plustree + fudge, m_watcher->GetWatchedPathsCount()); | |
762 | m_watcher->RemoveTree(child); | |
763 | CPPUNIT_ASSERT(initial < m_watcher->GetWatchedPathsCount()); | |
764 | m_watcher->RemoveTree(dir); | |
765 | CPPUNIT_ASSERT_EQUAL(initial, m_watcher->GetWatchedPathsCount()); | |
0fccda2c VZ |
766 | #if defined(__UNIX__) |
767 | // Finally, test a tree containing internal symlinks | |
768 | RmDir(dir); | |
769 | GrowTree(dir, true /* test symlinks */); | |
770 | ||
771 | // Without the DontFollowLink() call AddTree() would now assert | |
772 | // (and without the assert, it would infinitely loop) | |
773 | wxFileName fn = dir; | |
774 | fn.DontFollowLink(); | |
775 | CPPUNIT_ASSERT(m_watcher->AddTree(fn)); | |
776 | CPPUNIT_ASSERT(m_watcher->RemoveTree(fn)); | |
777 | ||
778 | // Regrow the tree without symlinks, ready for the next test | |
779 | RmDir(dir); | |
780 | GrowTree(dir, false); | |
781 | #endif // __UNIX__ | |
2b6d227c VZ |
782 | } |
783 | ||
227dee95 VZ |
784 | void WatchTreeWithFilespec(const wxFileName& dir) |
785 | { | |
786 | CPPUNIT_ASSERT(m_watcher); | |
787 | CPPUNIT_ASSERT(dir.DirExists()); // Was built in WatchTree() | |
788 | ||
789 | // Store the initial count; there may already be some watches | |
790 | const int initial = m_watcher->GetWatchedPathsCount(); | |
791 | ||
792 | // When we use a filter, both wxMSW and wxGTK implementations set | |
6eef5763 VZ |
793 | // an additional watch for each subdir (+1 for the root dir itself |
794 | // and another +1 for "child"). | |
795 | const size_t treeitems = subdirs + 2; | |
227dee95 VZ |
796 | m_watcher->AddTree(dir, wxFSW_EVENT_ALL, "*.txt"); |
797 | ||
798 | const int plustree = m_watcher->GetWatchedPathsCount(); | |
799 | CPPUNIT_ASSERT_EQUAL(initial + treeitems, plustree); | |
800 | ||
801 | // RemoveTree should try to remove only those files that were added | |
802 | m_watcher->RemoveTree(dir); | |
803 | CPPUNIT_ASSERT_EQUAL(initial, m_watcher->GetWatchedPathsCount()); | |
804 | } | |
805 | ||
2b6d227c VZ |
806 | void RemoveAllWatches() |
807 | { | |
808 | CPPUNIT_ASSERT(m_watcher); | |
809 | ||
810 | m_watcher->RemoveAll(); | |
811 | CPPUNIT_ASSERT_EQUAL(0, m_watcher->GetWatchedPathsCount()); | |
812 | } | |
813 | ||
814 | virtual void GenerateEvent() | |
815 | { | |
816 | // We don't use this function for events. Just run the tests | |
817 | ||
818 | wxFileName watchdir = EventGenerator::GetWatchDir(); | |
819 | CPPUNIT_ASSERT(watchdir.DirExists()); | |
820 | ||
821 | wxFileName treedir(watchdir); | |
822 | treedir.AppendDir("treetrunk"); | |
823 | CPPUNIT_ASSERT(!treedir.DirExists()); | |
824 | ||
825 | wxFileName singledir(watchdir); | |
826 | singledir.AppendDir("single"); | |
827 | CPPUNIT_ASSERT(!singledir.DirExists()); | |
828 | CPPUNIT_ASSERT(singledir.Mkdir()); | |
829 | ||
830 | WatchDir(singledir); | |
831 | WatchTree(treedir); | |
227dee95 VZ |
832 | // Now test adding and removing a tree using a filespec |
833 | // wxMSW uses the generic method to add matching files; which fails | |
834 | // as it doesn't support adding files :/ So disable the test | |
835 | #ifndef __WINDOWS__ | |
836 | WatchTreeWithFilespec(treedir); | |
837 | #endif // __WINDOWS__ | |
2b6d227c VZ |
838 | |
839 | RemoveSingleWatch(singledir); | |
840 | // Add it back again, ready to test RemoveAll() | |
841 | WatchDir(singledir); | |
842 | ||
843 | RemoveAllWatches(); | |
844 | ||
845 | // Clean up | |
846 | RmDir(singledir); | |
847 | RmDir(treedir); | |
848 | ||
849 | Exit(); | |
850 | } | |
851 | ||
852 | virtual wxFileSystemWatcherEvent ExpectedEvent() | |
853 | { | |
854 | CPPUNIT_FAIL("Shouldn't be called"); | |
855 | ||
856 | return wxFileSystemWatcherEvent(wxFSW_EVENT_ERROR); | |
857 | } | |
858 | ||
859 | virtual void CheckResult() | |
860 | { | |
861 | // Do nothing. We override this to prevent receiving events in | |
862 | // ExpectedEvent() | |
863 | } | |
864 | }; | |
865 | ||
866 | TreeTester tester; | |
867 | tester.Run(); | |
868 | } | |
227dee95 VZ |
869 | #endif // !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7) |
870 | ||
2b6d227c | 871 | |
d3eec3b6 VZ |
872 | namespace |
873 | { | |
874 | ||
875 | // We can't define this class locally inside TestNoEventsAfterRemove() for some | |
876 | // reason with g++ 4.0 under OS X 10.5, it results in the following mysterious | |
877 | // error: | |
878 | // | |
879 | // /var/tmp//ccTkNCkc.s:unknown:Non-global symbol: | |
880 | // __ZThn80_ZN25FileSystemWatcherTestCase23TestNoEventsAfterRemoveEvEN11EventTester6NotifyEv.eh | |
881 | // can't be a weak_definition | |
882 | // | |
883 | // So define this class outside the function instead. | |
884 | class NoEventsAfterRemoveEventTester : public EventHandler, | |
885 | public wxTimer | |
51fb8678 | 886 | { |
d3eec3b6 VZ |
887 | public: |
888 | NoEventsAfterRemoveEventTester() | |
51fb8678 | 889 | { |
d3eec3b6 VZ |
890 | // We need to use an inactivity timer as we never get any file |
891 | // system events in this test, so we consider that the test is | |
892 | // finished when this 1s timeout expires instead of, as usual, | |
893 | // stopping after getting the file system events. | |
894 | Start(1000, true); | |
895 | } | |
51fb8678 | 896 | |
d3eec3b6 VZ |
897 | virtual void GenerateEvent() |
898 | { | |
899 | m_watcher->Remove(EventGenerator::GetWatchDir()); | |
900 | CPPUNIT_ASSERT(eg.CreateFile()); | |
901 | } | |
51fb8678 | 902 | |
d3eec3b6 VZ |
903 | virtual void CheckResult() |
904 | { | |
905 | CPPUNIT_ASSERT( m_events.empty() ); | |
906 | } | |
51fb8678 | 907 | |
d3eec3b6 VZ |
908 | virtual wxFileSystemWatcherEvent ExpectedEvent() |
909 | { | |
910 | CPPUNIT_FAIL( "Shouldn't be called" ); | |
51fb8678 | 911 | |
d3eec3b6 VZ |
912 | return wxFileSystemWatcherEvent(wxFSW_EVENT_ERROR); |
913 | } | |
51fb8678 | 914 | |
d3eec3b6 VZ |
915 | virtual void Notify() |
916 | { | |
917 | SendIdle(); | |
918 | } | |
919 | }; | |
51fb8678 | 920 | |
d3eec3b6 VZ |
921 | } // anonymous namespace |
922 | ||
923 | void FileSystemWatcherTestCase::TestNoEventsAfterRemove() | |
924 | { | |
925 | NoEventsAfterRemoveEventTester tester; | |
51fb8678 VZ |
926 | tester.Run(); |
927 | } |