1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/fswatcher/fswatchertest.cpp
3 // Purpose: wxFileSystemWatcher unit test
4 // Author: Bartosz Bekier
7 // Copyright: (c) 2009 Bartosz Bekier
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
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"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // class generating file system events
40 static EventGenerator
& Get()
43 ms_instance
= new EventGenerator(GetWatchDir());
48 EventGenerator(const wxFileName
& path
) : m_base(path
)
51 m_file
= RandomName();
58 wxFile
file(m_file
.GetFullPath(), wxFile::write
);
59 return file
.IsOpened() && m_file
.FileExists();
64 CPPUNIT_ASSERT(m_file
.FileExists());
66 wxLogDebug("Renaming %s=>%s", m_file
.GetFullPath(), m_new
.GetFullPath());
68 bool ret
= wxRenameFile(m_file
.GetFullPath(), m_new
.GetFullPath());
81 CPPUNIT_ASSERT(m_file
.FileExists());
83 bool ret
= wxRemoveFile(m_file
.GetFullPath());
96 return m_file
.Touch();
101 wxFile
f(m_file
.GetFullPath());
102 CPPUNIT_ASSERT(f
.IsOpened());
105 ssize_t count
= f
.Read(buf
, sizeof(buf
));
106 CPPUNIT_ASSERT(count
> 0);
113 CPPUNIT_ASSERT(m_file
.FileExists());
115 wxFile
file(m_file
.GetFullPath(), wxFile::write_append
);
116 CPPUNIT_ASSERT(file
.IsOpened());
118 CPPUNIT_ASSERT(file
.Write("Words of Wisdom, Lloyd. Words of wisdom\n"));
123 wxFileName
RandomName(int length
= 10)
125 return RandomName(m_base
, length
);
129 static const wxFileName
& GetWatchDir()
131 static wxFileName dir
;
136 wxString tmp
= wxStandardPaths::Get().GetTempDir();
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());
148 static void RemoveWatchDir()
150 wxFileName dir
= GetWatchDir();
151 CPPUNIT_ASSERT(dir
.DirExists());
153 // just to be really sure we know what we remove
154 CPPUNIT_ASSERT_EQUAL( "fswatcher_test", dir
.GetDirs().Last() );
156 // FIXME-VC6: using non-static Rmdir() results in ICE
157 CPPUNIT_ASSERT( wxFileName::Rmdir(dir
.GetFullPath(), wxPATH_RMDIR_RECURSIVE
) );
160 static wxFileName
RandomName(const wxFileName
& base
, int length
= 10)
162 static int ALFA_CNT
= 'z' - 'a';
165 for (int i
= 0 ; i
< length
; ++i
)
167 char c
= 'a' + (rand() % ALFA_CNT
);
171 return wxFileName(base
.GetFullPath(), s
);
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
181 static EventGenerator
* ms_instance
;
184 EventGenerator
* EventGenerator::ms_instance
= 0;
187 // custom event handler
188 class EventHandler
: public wxEvtHandler
191 enum { WAIT_DURATION
= 3 };
194 eg(EventGenerator::Get()), m_loop(0), m_count(0), m_watcher(0)
196 m_loop
= new wxEventLoop();
197 Connect(wxEVT_IDLE
, wxIdleEventHandler(EventHandler::OnIdle
));
198 Connect(wxEVT_FSWATCHER
, wxFileSystemWatcherEventHandler(
199 EventHandler::OnFileSystemEvent
));
202 virtual ~EventHandler()
207 if (m_loop
->IsRunning())
218 // sends idle event, so we get called in a moment
221 wxIdleEvent
* e
= new wxIdleEvent();
231 void OnIdle(wxIdleEvent
& /*evt*/)
233 bool more
= Action();
242 // returns whether we should produce more idle events
243 virtual bool Action()
248 CPPUNIT_ASSERT(Init());
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 :-)
266 CPPUNIT_ASSERT(KeepWaiting());
272 CPPUNIT_ASSERT(AfterWait());
275 } // switch (m_count)
282 // test we're good to go
283 CPPUNIT_ASSERT(wxEventLoopBase::GetActive());
285 // XXX only now can we construct Watcher, because we need
287 m_watcher
= new wxFileSystemWatcher();
288 m_watcher
->SetOwner(this);
290 // add dir to be watched
291 wxFileName dir
= EventGenerator::GetWatchDir();
292 CPPUNIT_ASSERT(m_watcher
->Add(dir
, wxFSW_EVENT_ALL
));
297 virtual bool KeepWaiting()
299 // did we receive event already?
302 // well, let's wait a bit more
303 wxSleep(WAIT_DURATION
);
309 virtual bool AfterWait()
311 // fail if still no events
314 ("No events during %d seconds!", static_cast<int>(WAIT_DURATION
)),
321 virtual void OnFileSystemEvent(wxFileSystemWatcherEvent
& evt
)
323 wxLogDebug("--- %s ---", evt
.ToString());
324 m_lastEvent
= wxDynamicCast(evt
.Clone(), wxFileSystemWatcherEvent
);
325 m_events
.Add(m_lastEvent
);
332 virtual void CheckResult()
334 CPPUNIT_ASSERT_MESSAGE( "No events received", !m_events
.empty() );
336 const wxFileSystemWatcherEvent
* const e
= m_events
.front();
338 // this is our "reference event"
339 const wxFileSystemWatcherEvent expected
= ExpectedEvent();
341 CPPUNIT_ASSERT_EQUAL( expected
.GetChangeType(), e
->GetChangeType() );
343 CPPUNIT_ASSERT_EQUAL((int)wxEVT_FSWATCHER
, e
->GetEventType());
345 // XXX this needs change
346 CPPUNIT_ASSERT_EQUAL(wxEVT_CATEGORY_UNKNOWN
, e
->GetEventCategory());
348 CPPUNIT_ASSERT_EQUAL(expected
.GetPath(), e
->GetPath());
349 CPPUNIT_ASSERT_EQUAL(expected
.GetNewPath(), e
->GetNewPath());
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 )
357 const wxFileSystemWatcherEvent
* const e2
= m_events
.back();
358 if ( e2
->GetChangeType() == wxFSW_EVENT_MODIFY
&&
359 e2
->GetPath() == e
->GetNewPath() )
361 // This is a modify event for the new file, ignore it.
366 WX_ASSERT_EQUAL_MESSAGE
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()
381 virtual void GenerateEvent() = 0;
383 virtual wxFileSystemWatcherEvent
ExpectedEvent() = 0;
388 wxEventLoopBase
* m_loop
; // loop reference
389 int m_count
; // idle events count
391 wxFileSystemWatcher
* m_watcher
;
392 bool tested
; // indicates, whether we have already passed the test
394 #include "wx/arrimpl.cpp"
395 WX_DEFINE_ARRAY_PTR(wxFileSystemWatcherEvent
*, wxArrayEvent
);
396 wxArrayEvent m_events
;
397 wxFileSystemWatcherEvent
* m_lastEvent
;
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 class FileSystemWatcherTestCase
: public CppUnit::TestCase
408 FileSystemWatcherTestCase() { }
410 virtual void setUp();
411 virtual void tearDown();
414 wxEventLoopBase
* m_loop
;
417 CPPUNIT_TEST_SUITE( FileSystemWatcherTestCase
);
418 CPPUNIT_TEST( TestEventCreate
);
419 CPPUNIT_TEST( TestEventDelete
);
420 #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
421 CPPUNIT_TEST( TestTrees
);
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
428 // FIXME: fix the code and reenable them
430 CPPUNIT_TEST( TestEventRename
);
431 CPPUNIT_TEST( TestEventModify
);
433 // MSW implementation doesn't detect file access events currently
435 CPPUNIT_TEST( TestEventAccess
);
436 #endif // __WINDOWS__
437 #endif // !wxHAS_KQUEUE
439 CPPUNIT_TEST( TestNoEventsAfterRemove
);
440 CPPUNIT_TEST_SUITE_END();
442 void TestEventCreate();
443 void TestEventDelete();
444 void TestEventRename();
445 void TestEventModify();
446 void TestEventAccess();
447 #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
448 void TestTrees(); // Visual C++ 6 can't build this
450 void TestNoEventsAfterRemove();
452 DECLARE_NO_COPY_CLASS(FileSystemWatcherTestCase
)
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
459 // FIXME: debug and fix this!
461 // register in the unnamed registry so that these tests are run by default
462 CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemWatcherTestCase
);
465 // also include in its own registry so that these tests can be run alone
466 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileSystemWatcherTestCase
,
467 "FileSystemWatcherTestCase" );
469 void FileSystemWatcherTestCase::setUp()
471 wxLog::AddTraceMask(wxTRACE_FSWATCHER
);
472 EventGenerator::Get().GetWatchDir();
475 void FileSystemWatcherTestCase::tearDown()
477 EventGenerator::Get().RemoveWatchDir();
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
483 void FileSystemWatcherTestCase::TestEventCreate()
485 wxLogDebug("TestEventCreate()");
487 class EventTester
: public EventHandler
490 virtual void GenerateEvent()
492 CPPUNIT_ASSERT(eg
.CreateFile());
495 virtual wxFileSystemWatcherEvent
ExpectedEvent()
497 wxFileSystemWatcherEvent
event(wxFSW_EVENT_CREATE
);
498 event
.SetPath(eg
.m_file
);
499 event
.SetNewPath(eg
.m_file
);
506 wxLogTrace(wxTRACE_FSWATCHER
, "TestEventCreate tester created()");
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
514 void FileSystemWatcherTestCase::TestEventDelete()
516 wxLogDebug("TestEventDelete()");
518 class EventTester
: public EventHandler
521 virtual void GenerateEvent()
523 CPPUNIT_ASSERT(eg
.DeleteFile());
526 virtual wxFileSystemWatcherEvent
ExpectedEvent()
528 wxFileSystemWatcherEvent
event(wxFSW_EVENT_DELETE
);
529 event
.SetPath(eg
.m_old
);
531 // CHECK maybe new path here could be NULL or sth?
532 event
.SetNewPath(eg
.m_old
);
537 // we need to create a file now, so we can delete it
538 EventGenerator::Get().CreateFile();
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
547 void FileSystemWatcherTestCase::TestEventRename()
549 wxLogDebug("TestEventRename()");
551 class EventTester
: public EventHandler
554 virtual void GenerateEvent()
556 CPPUNIT_ASSERT(eg
.RenameFile());
559 virtual wxFileSystemWatcherEvent
ExpectedEvent()
561 wxFileSystemWatcherEvent
event(wxFSW_EVENT_RENAME
);
562 event
.SetPath(eg
.m_old
);
563 event
.SetNewPath(eg
.m_file
);
568 // need a file to rename later
569 EventGenerator::Get().CreateFile();
575 // ----------------------------------------------------------------------------
577 // ----------------------------------------------------------------------------
578 void FileSystemWatcherTestCase::TestEventModify()
580 wxLogDebug("TestEventModify()");
582 class EventTester
: public EventHandler
585 virtual void GenerateEvent()
587 CPPUNIT_ASSERT(eg
.ModifyFile());
590 virtual wxFileSystemWatcherEvent
ExpectedEvent()
592 wxFileSystemWatcherEvent
event(wxFSW_EVENT_MODIFY
);
593 event
.SetPath(eg
.m_file
);
594 event
.SetNewPath(eg
.m_file
);
599 // we need to create a file to modify
600 EventGenerator::Get().CreateFile();
606 // ----------------------------------------------------------------------------
608 // ----------------------------------------------------------------------------
609 void FileSystemWatcherTestCase::TestEventAccess()
611 wxLogDebug("TestEventAccess()");
613 class EventTester
: public EventHandler
616 virtual void GenerateEvent()
618 CPPUNIT_ASSERT(eg
.ReadFile());
621 virtual wxFileSystemWatcherEvent
ExpectedEvent()
623 wxFileSystemWatcherEvent
event(wxFSW_EVENT_ACCESS
);
624 event
.SetPath(eg
.m_file
);
625 event
.SetNewPath(eg
.m_file
);
630 // we need to create a file to read from it and write sth to it
631 EventGenerator::Get().CreateFile();
632 EventGenerator::Get().ModifyFile();
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
642 #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
643 void FileSystemWatcherTestCase::TestTrees()
645 class TreeTester
: public EventHandler
647 const size_t subdirs
;
651 TreeTester() : subdirs(5), files(3) {}
653 void GrowTree(wxFileName dir
, bool withSymlinks
)
655 CPPUNIT_ASSERT(dir
.Mkdir());
656 // Now add a subdir with an easy name to remember in WatchTree()
657 dir
.AppendDir("child");
658 CPPUNIT_ASSERT(dir
.Mkdir());
659 wxFileName
child(dir
); // Create a copy to which to symlink
661 // Create a branch of 5 numbered subdirs, each containing 3
663 for ( unsigned d
= 0; d
< subdirs
; ++d
)
665 dir
.AppendDir(wxString::Format("subdir%u", d
+1));
666 CPPUNIT_ASSERT(dir
.Mkdir());
668 const wxString prefix
= dir
.GetPathWithSep();
669 const wxString ext
[] = { ".txt", ".log", "" };
670 for ( unsigned f
= 0; f
< files
; ++f
)
672 // Just create the files.
673 wxFile(prefix
+ wxString::Format("file%u", f
+1) + ext
[f
],
676 #if defined(__UNIX__)
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()));
691 void RmDir(wxFileName dir
)
693 CPPUNIT_ASSERT(dir
.DirExists());
695 CPPUNIT_ASSERT(dir
.Rmdir(wxPATH_RMDIR_RECURSIVE
));
698 void WatchDir(wxFileName dir
)
700 CPPUNIT_ASSERT(m_watcher
);
702 // Store the initial count; there may already be some watches
703 const int initial
= m_watcher
->GetWatchedPathsCount();
706 CPPUNIT_ASSERT_EQUAL(initial
+ 1,
707 m_watcher
->GetWatchedPathsCount());
710 void RemoveSingleWatch(wxFileName dir
)
712 CPPUNIT_ASSERT(m_watcher
);
714 const int initial
= m_watcher
->GetWatchedPathsCount();
716 m_watcher
->Remove(dir
);
717 CPPUNIT_ASSERT_EQUAL(initial
- 1,
718 m_watcher
->GetWatchedPathsCount());
721 void WatchTree(const wxFileName
& dir
)
723 CPPUNIT_ASSERT(m_watcher
);
725 size_t treeitems
= 1; // the trunk
727 // When there's no file mask, wxMSW sets a single watch
728 // on the trunk which is implemented recursively.
729 // wxGTK always sets an additional watch for each subdir
730 treeitems
+= subdirs
+ 1; // +1 for 'child'
731 #endif // __WINDOWS__
733 // Store the initial count; there may already be some watches
734 const int initial
= m_watcher
->GetWatchedPathsCount();
736 GrowTree(dir
, false /* no symlinks */);
738 m_watcher
->AddTree(dir
);
739 const int plustree
= m_watcher
->GetWatchedPathsCount();
741 CPPUNIT_ASSERT_EQUAL(initial
+ treeitems
, plustree
);
743 m_watcher
->RemoveTree(dir
);
744 CPPUNIT_ASSERT_EQUAL(initial
, m_watcher
->GetWatchedPathsCount());
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
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());
766 #if defined(__UNIX__)
767 // Finally, test a tree containing internal symlinks
769 GrowTree(dir
, true /* test symlinks */);
771 // Without the DontFollowLink() call AddTree() would now assert
772 // (and without the assert, it would infinitely loop)
775 CPPUNIT_ASSERT(m_watcher
->AddTree(fn
));
776 CPPUNIT_ASSERT(m_watcher
->RemoveTree(fn
));
778 // Regrow the tree without symlinks, ready for the next test
780 GrowTree(dir
, false);
784 void WatchTreeWithFilespec(const wxFileName
& dir
)
786 CPPUNIT_ASSERT(m_watcher
);
787 CPPUNIT_ASSERT(dir
.DirExists()); // Was built in WatchTree()
789 // Store the initial count; there may already be some watches
790 const int initial
= m_watcher
->GetWatchedPathsCount();
792 // When we use a filter, both wxMSW and wxGTK implementations set
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;
796 m_watcher
->AddTree(dir
, wxFSW_EVENT_ALL
, "*.txt");
798 const int plustree
= m_watcher
->GetWatchedPathsCount();
799 CPPUNIT_ASSERT_EQUAL(initial
+ treeitems
, plustree
);
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());
806 void RemoveAllWatches()
808 CPPUNIT_ASSERT(m_watcher
);
810 m_watcher
->RemoveAll();
811 CPPUNIT_ASSERT_EQUAL(0, m_watcher
->GetWatchedPathsCount());
814 virtual void GenerateEvent()
816 // We don't use this function for events. Just run the tests
818 wxFileName watchdir
= EventGenerator::GetWatchDir();
819 CPPUNIT_ASSERT(watchdir
.DirExists());
821 wxFileName
treedir(watchdir
);
822 treedir
.AppendDir("treetrunk");
823 CPPUNIT_ASSERT(!treedir
.DirExists());
825 wxFileName
singledir(watchdir
);
826 singledir
.AppendDir("single");
827 CPPUNIT_ASSERT(!singledir
.DirExists());
828 CPPUNIT_ASSERT(singledir
.Mkdir());
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
836 WatchTreeWithFilespec(treedir
);
837 #endif // __WINDOWS__
839 RemoveSingleWatch(singledir
);
840 // Add it back again, ready to test RemoveAll()
852 virtual wxFileSystemWatcherEvent
ExpectedEvent()
854 CPPUNIT_FAIL("Shouldn't be called");
856 return wxFileSystemWatcherEvent(wxFSW_EVENT_ERROR
);
859 virtual void CheckResult()
861 // Do nothing. We override this to prevent receiving events in
869 #endif // !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
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
879 // /var/tmp//ccTkNCkc.s:unknown:Non-global symbol:
880 // __ZThn80_ZN25FileSystemWatcherTestCase23TestNoEventsAfterRemoveEvEN11EventTester6NotifyEv.eh
881 // can't be a weak_definition
883 // So define this class outside the function instead.
884 class NoEventsAfterRemoveEventTester
: public EventHandler
,
888 NoEventsAfterRemoveEventTester()
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.
897 virtual void GenerateEvent()
899 m_watcher
->Remove(EventGenerator::GetWatchDir());
900 CPPUNIT_ASSERT(eg
.CreateFile());
903 virtual void CheckResult()
905 CPPUNIT_ASSERT( m_events
.empty() );
908 virtual wxFileSystemWatcherEvent
ExpectedEvent()
910 CPPUNIT_FAIL( "Shouldn't be called" );
912 return wxFileSystemWatcherEvent(wxFSW_EVENT_ERROR
);
915 virtual void Notify()
921 } // anonymous namespace
923 void FileSystemWatcherTestCase::TestNoEventsAfterRemove()
925 NoEventsAfterRemoveEventTester tester
;