]> git.saurik.com Git - wxWidgets.git/blame - tests/fswatcher/fswatchertest.cpp
Increase version to 2.9.5.
[wxWidgets.git] / tests / fswatcher / fswatchertest.cpp
CommitLineData
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
37class EventGenerator
38{
39public:
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
174public:
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
180protected:
181 static EventGenerator* ms_instance;
182};
183
184EventGenerator* EventGenerator::ms_instance = 0;
185
186
187// custom event handler
188class EventHandler : public wxEvtHandler
189{
190public:
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
386protected:
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
405class FileSystemWatcherTestCase : public CppUnit::TestCase
406{
407public:
408 FileSystemWatcherTestCase() { }
409
410 virtual void setUp();
411 virtual void tearDown();
412
413protected:
414 wxEventLoopBase* m_loop;
415
416private:
417 CPPUNIT_TEST_SUITE( FileSystemWatcherTestCase );
418 CPPUNIT_TEST( TestEventCreate );
419 CPPUNIT_TEST( TestEventDelete );
420
421 // kqueue-based implementation doesn't collapse create/delete pairs in
422 // renames and doesn't detect neither modifications nor access to the
423 // files reliably currently so disable these tests
424 //
425 // FIXME: fix the code and reenable them
426#ifndef wxHAS_KQUEUE
427 CPPUNIT_TEST( TestEventRename );
428 CPPUNIT_TEST( TestEventModify );
429
430 // MSW implementation doesn't detect file access events currently
bb5a9514 431#ifndef __WINDOWS__
6b8ef0b3 432 CPPUNIT_TEST( TestEventAccess );
bb5a9514 433#endif // __WINDOWS__
6b8ef0b3 434#endif // !wxHAS_KQUEUE
51fb8678
VZ
435
436 CPPUNIT_TEST( TestNoEventsAfterRemove );
6b8ef0b3
VZ
437 CPPUNIT_TEST_SUITE_END();
438
439 void TestEventCreate();
440 void TestEventDelete();
441 void TestEventRename();
442 void TestEventModify();
443 void TestEventAccess();
444
51fb8678
VZ
445 void TestNoEventsAfterRemove();
446
6b8ef0b3
VZ
447 DECLARE_NO_COPY_CLASS(FileSystemWatcherTestCase)
448};
449
b66a6888
VZ
450// the test currently hangs under OS X for some reason and this prevents tests
451// ran by buildbot from completing so disable it until someone has time to
452// debug it
453//
454// FIXME: debug and fix this!
455#ifndef __WXOSX__
6b8ef0b3
VZ
456// register in the unnamed registry so that these tests are run by default
457CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemWatcherTestCase );
b66a6888 458#endif
6b8ef0b3 459
e3778b4d 460// also include in its own registry so that these tests can be run alone
6b8ef0b3
VZ
461CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileSystemWatcherTestCase,
462 "FileSystemWatcherTestCase" );
463
464void FileSystemWatcherTestCase::setUp()
465{
466 wxLog::AddTraceMask(wxTRACE_FSWATCHER);
467 EventGenerator::Get().GetWatchDir();
468}
469
470void FileSystemWatcherTestCase::tearDown()
471{
472 EventGenerator::Get().RemoveWatchDir();
473}
474
475// ----------------------------------------------------------------------------
476// TestEventCreate
477// ----------------------------------------------------------------------------
478void FileSystemWatcherTestCase::TestEventCreate()
479{
480 wxLogDebug("TestEventCreate()");
481
482 class EventTester : public EventHandler
483 {
484 public:
485 virtual void GenerateEvent()
486 {
487 CPPUNIT_ASSERT(eg.CreateFile());
488 }
489
490 virtual wxFileSystemWatcherEvent ExpectedEvent()
491 {
492 wxFileSystemWatcherEvent event(wxFSW_EVENT_CREATE);
493 event.SetPath(eg.m_file);
494 event.SetNewPath(eg.m_file);
495 return event;
496 }
497 };
498
499 EventTester tester;
500
501 wxLogTrace(wxTRACE_FSWATCHER, "TestEventCreate tester created()");
502
503 tester.Run();
504}
505
506// ----------------------------------------------------------------------------
507// TestEventDelete
508// ----------------------------------------------------------------------------
509void FileSystemWatcherTestCase::TestEventDelete()
510{
511 wxLogDebug("TestEventDelete()");
512
513 class EventTester : public EventHandler
514 {
515 public:
516 virtual void GenerateEvent()
517 {
518 CPPUNIT_ASSERT(eg.DeleteFile());
519 }
520
521 virtual wxFileSystemWatcherEvent ExpectedEvent()
522 {
523 wxFileSystemWatcherEvent event(wxFSW_EVENT_DELETE);
524 event.SetPath(eg.m_old);
525
526 // CHECK maybe new path here could be NULL or sth?
527 event.SetNewPath(eg.m_old);
528 return event;
529 }
530 };
531
532 // we need to create a file now, so we can delete it
533 EventGenerator::Get().CreateFile();
534
535 EventTester tester;
536 tester.Run();
537}
538
539// ----------------------------------------------------------------------------
540// TestEventRename
541// ----------------------------------------------------------------------------
542void FileSystemWatcherTestCase::TestEventRename()
543{
544 wxLogDebug("TestEventRename()");
545
546 class EventTester : public EventHandler
547 {
548 public:
549 virtual void GenerateEvent()
550 {
551 CPPUNIT_ASSERT(eg.RenameFile());
552 }
553
554 virtual wxFileSystemWatcherEvent ExpectedEvent()
555 {
556 wxFileSystemWatcherEvent event(wxFSW_EVENT_RENAME);
557 event.SetPath(eg.m_old);
558 event.SetNewPath(eg.m_file);
559 return event;
560 }
561 };
562
563 // need a file to rename later
564 EventGenerator::Get().CreateFile();
565
566 EventTester tester;
567 tester.Run();
568}
569
570// ----------------------------------------------------------------------------
571// TestEventModify
572// ----------------------------------------------------------------------------
573void FileSystemWatcherTestCase::TestEventModify()
574{
575 wxLogDebug("TestEventModify()");
576
577 class EventTester : public EventHandler
578 {
579 public:
580 virtual void GenerateEvent()
581 {
582 CPPUNIT_ASSERT(eg.ModifyFile());
583 }
584
585 virtual wxFileSystemWatcherEvent ExpectedEvent()
586 {
587 wxFileSystemWatcherEvent event(wxFSW_EVENT_MODIFY);
588 event.SetPath(eg.m_file);
589 event.SetNewPath(eg.m_file);
590 return event;
591 }
592 };
593
594 // we need to create a file to modify
595 EventGenerator::Get().CreateFile();
596
597 EventTester tester;
598 tester.Run();
599}
600
601// ----------------------------------------------------------------------------
602// TestEventAccess
603// ----------------------------------------------------------------------------
604void FileSystemWatcherTestCase::TestEventAccess()
605{
606 wxLogDebug("TestEventAccess()");
607
608 class EventTester : public EventHandler
609 {
610 public:
611 virtual void GenerateEvent()
612 {
613 CPPUNIT_ASSERT(eg.ReadFile());
614 }
615
616 virtual wxFileSystemWatcherEvent ExpectedEvent()
617 {
618 wxFileSystemWatcherEvent event(wxFSW_EVENT_ACCESS);
619 event.SetPath(eg.m_file);
620 event.SetNewPath(eg.m_file);
621 return event;
622 }
623 };
624
625 // we need to create a file to read from it and write sth to it
626 EventGenerator::Get().CreateFile();
627 EventGenerator::Get().ModifyFile();
628
629 EventTester tester;
630 tester.Run();
631}
51fb8678 632
d3eec3b6
VZ
633namespace
634{
635
636// We can't define this class locally inside TestNoEventsAfterRemove() for some
637// reason with g++ 4.0 under OS X 10.5, it results in the following mysterious
638// error:
639//
640// /var/tmp//ccTkNCkc.s:unknown:Non-global symbol:
641// __ZThn80_ZN25FileSystemWatcherTestCase23TestNoEventsAfterRemoveEvEN11EventTester6NotifyEv.eh
642// can't be a weak_definition
643//
644// So define this class outside the function instead.
645class NoEventsAfterRemoveEventTester : public EventHandler,
646 public wxTimer
51fb8678 647{
d3eec3b6
VZ
648public:
649 NoEventsAfterRemoveEventTester()
51fb8678 650 {
d3eec3b6
VZ
651 // We need to use an inactivity timer as we never get any file
652 // system events in this test, so we consider that the test is
653 // finished when this 1s timeout expires instead of, as usual,
654 // stopping after getting the file system events.
655 Start(1000, true);
656 }
51fb8678 657
d3eec3b6
VZ
658 virtual void GenerateEvent()
659 {
660 m_watcher->Remove(EventGenerator::GetWatchDir());
661 CPPUNIT_ASSERT(eg.CreateFile());
662 }
51fb8678 663
d3eec3b6
VZ
664 virtual void CheckResult()
665 {
666 CPPUNIT_ASSERT( m_events.empty() );
667 }
51fb8678 668
d3eec3b6
VZ
669 virtual wxFileSystemWatcherEvent ExpectedEvent()
670 {
671 CPPUNIT_FAIL( "Shouldn't be called" );
51fb8678 672
d3eec3b6
VZ
673 return wxFileSystemWatcherEvent(wxFSW_EVENT_ERROR);
674 }
51fb8678 675
d3eec3b6
VZ
676 virtual void Notify()
677 {
678 SendIdle();
679 }
680};
51fb8678 681
d3eec3b6
VZ
682} // anonymous namespace
683
684void FileSystemWatcherTestCase::TestNoEventsAfterRemove()
685{
686 NoEventsAfterRemoveEventTester tester;
51fb8678
VZ
687 tester.Run();
688}