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 WX_ASSERT_EQUAL_MESSAGE
341 "Extra events received, first is of type %x, for path=\"%s\","
342 "last is of type %x, path=\"%s\"",
344 e
->GetPath().GetFullPath(),
345 m_events
.back()->GetChangeType(),
346 m_events
.back()->GetPath().GetFullPath()
351 // this is our "reference event"
352 const wxFileSystemWatcherEvent expected
= ExpectedEvent();
354 CPPUNIT_ASSERT_EQUAL( expected
.GetChangeType(), e
->GetChangeType() );
356 CPPUNIT_ASSERT_EQUAL((int)wxEVT_FSWATCHER
, e
->GetEventType());
358 // XXX this needs change
359 CPPUNIT_ASSERT_EQUAL(wxEVT_CATEGORY_UNKNOWN
, e
->GetEventCategory());
361 CPPUNIT_ASSERT_EQUAL(expected
.GetPath(), e
->GetPath());
362 CPPUNIT_ASSERT_EQUAL(expected
.GetNewPath(), e
->GetNewPath());
365 virtual void GenerateEvent() = 0;
367 virtual wxFileSystemWatcherEvent
ExpectedEvent() = 0;
372 wxEventLoopBase
* m_loop
; // loop reference
373 int m_count
; // idle events count
375 wxFileSystemWatcher
* m_watcher
;
376 bool tested
; // indicates, whether we have already passed the test
378 #include "wx/arrimpl.cpp"
379 WX_DEFINE_ARRAY_PTR(wxFileSystemWatcherEvent
*, wxArrayEvent
);
380 wxArrayEvent m_events
;
381 wxFileSystemWatcherEvent
* m_lastEvent
;
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 class FileSystemWatcherTestCase
: public CppUnit::TestCase
392 FileSystemWatcherTestCase() { }
394 virtual void setUp();
395 virtual void tearDown();
398 wxEventLoopBase
* m_loop
;
401 CPPUNIT_TEST_SUITE( FileSystemWatcherTestCase
);
402 CPPUNIT_TEST( TestEventCreate
);
403 CPPUNIT_TEST( TestEventDelete
);
405 // kqueue-based implementation doesn't collapse create/delete pairs in
406 // renames and doesn't detect neither modifications nor access to the
407 // files reliably currently so disable these tests
409 // FIXME: fix the code and reenable them
411 CPPUNIT_TEST( TestEventRename
);
412 CPPUNIT_TEST( TestEventModify
);
414 // MSW implementation doesn't detect file access events currently
416 CPPUNIT_TEST( TestEventAccess
);
418 #endif // !wxHAS_KQUEUE
420 CPPUNIT_TEST( TestNoEventsAfterRemove
);
421 CPPUNIT_TEST_SUITE_END();
423 void TestEventCreate();
424 void TestEventDelete();
425 void TestEventRename();
426 void TestEventModify();
427 void TestEventAccess();
429 void TestNoEventsAfterRemove();
431 DECLARE_NO_COPY_CLASS(FileSystemWatcherTestCase
)
434 // the test currently hangs under OS X for some reason and this prevents tests
435 // ran by buildbot from completing so disable it until someone has time to
438 // FIXME: debug and fix this!
440 // register in the unnamed registry so that these tests are run by default
441 CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemWatcherTestCase
);
444 // also include in its own registry so that these tests can be run alone
445 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileSystemWatcherTestCase
,
446 "FileSystemWatcherTestCase" );
448 void FileSystemWatcherTestCase::setUp()
450 wxLog::AddTraceMask(wxTRACE_FSWATCHER
);
451 EventGenerator::Get().GetWatchDir();
454 void FileSystemWatcherTestCase::tearDown()
456 EventGenerator::Get().RemoveWatchDir();
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
462 void FileSystemWatcherTestCase::TestEventCreate()
464 wxLogDebug("TestEventCreate()");
466 class EventTester
: public EventHandler
469 virtual void GenerateEvent()
471 CPPUNIT_ASSERT(eg
.CreateFile());
474 virtual wxFileSystemWatcherEvent
ExpectedEvent()
476 wxFileSystemWatcherEvent
event(wxFSW_EVENT_CREATE
);
477 event
.SetPath(eg
.m_file
);
478 event
.SetNewPath(eg
.m_file
);
485 wxLogTrace(wxTRACE_FSWATCHER
, "TestEventCreate tester created()");
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
493 void FileSystemWatcherTestCase::TestEventDelete()
495 wxLogDebug("TestEventDelete()");
497 class EventTester
: public EventHandler
500 virtual void GenerateEvent()
502 CPPUNIT_ASSERT(eg
.DeleteFile());
505 virtual wxFileSystemWatcherEvent
ExpectedEvent()
507 wxFileSystemWatcherEvent
event(wxFSW_EVENT_DELETE
);
508 event
.SetPath(eg
.m_old
);
510 // CHECK maybe new path here could be NULL or sth?
511 event
.SetNewPath(eg
.m_old
);
516 // we need to create a file now, so we can delete it
517 EventGenerator::Get().CreateFile();
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
526 void FileSystemWatcherTestCase::TestEventRename()
528 wxLogDebug("TestEventRename()");
530 class EventTester
: public EventHandler
533 virtual void GenerateEvent()
535 CPPUNIT_ASSERT(eg
.RenameFile());
538 virtual wxFileSystemWatcherEvent
ExpectedEvent()
540 wxFileSystemWatcherEvent
event(wxFSW_EVENT_RENAME
);
541 event
.SetPath(eg
.m_old
);
542 event
.SetNewPath(eg
.m_file
);
547 // need a file to rename later
548 EventGenerator::Get().CreateFile();
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
557 void FileSystemWatcherTestCase::TestEventModify()
559 wxLogDebug("TestEventModify()");
561 class EventTester
: public EventHandler
564 virtual void GenerateEvent()
566 CPPUNIT_ASSERT(eg
.ModifyFile());
569 virtual wxFileSystemWatcherEvent
ExpectedEvent()
571 wxFileSystemWatcherEvent
event(wxFSW_EVENT_MODIFY
);
572 event
.SetPath(eg
.m_file
);
573 event
.SetNewPath(eg
.m_file
);
578 // we need to create a file to modify
579 EventGenerator::Get().CreateFile();
585 // ----------------------------------------------------------------------------
587 // ----------------------------------------------------------------------------
588 void FileSystemWatcherTestCase::TestEventAccess()
590 wxLogDebug("TestEventAccess()");
592 class EventTester
: public EventHandler
595 virtual void GenerateEvent()
597 CPPUNIT_ASSERT(eg
.ReadFile());
600 virtual wxFileSystemWatcherEvent
ExpectedEvent()
602 wxFileSystemWatcherEvent
event(wxFSW_EVENT_ACCESS
);
603 event
.SetPath(eg
.m_file
);
604 event
.SetNewPath(eg
.m_file
);
609 // we need to create a file to read from it and write sth to it
610 EventGenerator::Get().CreateFile();
611 EventGenerator::Get().ModifyFile();
620 // We can't define this class locally inside TestNoEventsAfterRemove() for some
621 // reason with g++ 4.0 under OS X 10.5, it results in the following mysterious
624 // /var/tmp//ccTkNCkc.s:unknown:Non-global symbol:
625 // __ZThn80_ZN25FileSystemWatcherTestCase23TestNoEventsAfterRemoveEvEN11EventTester6NotifyEv.eh
626 // can't be a weak_definition
628 // So define this class outside the function instead.
629 class NoEventsAfterRemoveEventTester
: public EventHandler
,
633 NoEventsAfterRemoveEventTester()
635 // We need to use an inactivity timer as we never get any file
636 // system events in this test, so we consider that the test is
637 // finished when this 1s timeout expires instead of, as usual,
638 // stopping after getting the file system events.
642 virtual void GenerateEvent()
644 m_watcher
->Remove(EventGenerator::GetWatchDir());
645 CPPUNIT_ASSERT(eg
.CreateFile());
648 virtual void CheckResult()
650 CPPUNIT_ASSERT( m_events
.empty() );
653 virtual wxFileSystemWatcherEvent
ExpectedEvent()
655 CPPUNIT_FAIL( "Shouldn't be called" );
657 return wxFileSystemWatcherEvent(wxFSW_EVENT_ERROR
);
660 virtual void Notify()
666 } // anonymous namespace
668 void FileSystemWatcherTestCase::TestNoEventsAfterRemove()
670 NoEventsAfterRemoveEventTester tester
;