From aaea62a322c9a69b0f837183563714c5369535d6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Nov 2017 17:16:39 +0100 Subject: [PATCH 1/5] Avoid launching interactive programs in unattended tests This is typically going to fail anyhow, so don't even try to avoid useless error messages. --- tests/exec/exec.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/exec/exec.cpp b/tests/exec/exec.cpp index e3be763544..bc2cdfe052 100644 --- a/tests/exec/exec.cpp +++ b/tests/exec/exec.cpp @@ -171,6 +171,11 @@ void ExecTestCase::TestShell() void ExecTestCase::TestExecute() { + // Launching interactive programs doesn't work without an interactive + // session. + if ( IsAutomaticTest() ) + return; + AsyncInEventLoop asyncInEventLoop; // test asynch exec @@ -238,6 +243,9 @@ void ExecTestCase::TestExecute() void ExecTestCase::TestProcess() { + if ( IsAutomaticTest() ) + return; + AsyncInEventLoop asyncInEventLoop; // test wxExecute with wxProcess From 0425b8b7f02ac170abdeb653bc416e2071448d15 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Nov 2017 17:18:58 +0100 Subject: [PATCH 2/5] Properly terminate UTF-16 strings in cMB2WC() unit test Such strings must have 2 NUL bytes at the end, just the one added implicitly to all C strings isn't enough. --- tests/mbconv/mbconvtest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mbconv/mbconvtest.cpp b/tests/mbconv/mbconvtest.cpp index 0b2b1507c6..5a0afa4e5b 100644 --- a/tests/mbconv/mbconvtest.cpp +++ b/tests/mbconv/mbconvtest.cpp @@ -1484,8 +1484,8 @@ TEST_CASE("wxMBConv::cMB2WC", "[mbconv][mb2wc]") CHECK( convUTF16.cMB2WC("\0").length() == 0 ); CHECK( convUTF16.cMB2WC(wxCharBuffer()).length() == 0 ); - CHECK( convUTF16.cMB2WC("H\0i\0").length() == 2 ); - CHECK( convUTF16.cMB2WC(wxCharBuffer::CreateNonOwned("H\0i\0", 4)).length() == 2 ); + CHECK( convUTF16.cMB2WC("H\0i\0\0").length() == 2 ); + CHECK( convUTF16.cMB2WC(wxCharBuffer::CreateNonOwned("H\0i\0\0", 4)).length() == 2 ); CHECK( wxConvUTF7.cMB2WC("").length() == 0 ); CHECK( wxConvUTF7.cMB2WC(wxCharBuffer()).length() == 0 ); From 10b80a16f0bbdbdc9efac66d7eb26389fcc0628b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Nov 2017 17:28:24 +0100 Subject: [PATCH 3/5] Factor out TempFile class and reuse it in other tests Ensure we don't leave "mytext.dat" and "test.txt" lying around in any directory the tests are run from by ensuring that these files are destroyed by the test code using them. --- tests/filekind/filekind.cpp | 12 ++++-------- tests/streams/datastreamtest.cpp | 20 ++++++++++++++------ tests/streams/textstreamtest.cpp | 14 ++++++++++---- tests/testfile.h | 25 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/tests/filekind/filekind.cpp b/tests/filekind/filekind.cpp index aa812b0884..fa270897b4 100644 --- a/tests/filekind/filekind.cpp +++ b/tests/filekind/filekind.cpp @@ -37,6 +37,8 @@ #define fileno _fileno #endif +#include "testfile.h" + /////////////////////////////////////////////////////////////////////////////// // The test case @@ -100,23 +102,17 @@ void FileKindTestCase::TestFd(wxFile& file, bool expected) CPPUNIT_ASSERT(outStream.IsSeekable() == expected); } -struct TempFile -{ - ~TempFile() { if (!m_name.IsEmpty()) wxRemoveFile(m_name); } - wxString m_name; -}; - // test with an ordinary file // void FileKindTestCase::File() { TempFile tmp; // put first wxFile file; - tmp.m_name = wxFileName::CreateTempFileName(wxT("wxft"), &file); + tmp.Assign(wxFileName::CreateTempFileName(wxT("wxft"), &file)); TestFd(file, true); file.Close(); - wxFFile ffile(tmp.m_name); + wxFFile ffile(tmp.GetName()); TestFILE(ffile, true); } diff --git a/tests/streams/datastreamtest.cpp b/tests/streams/datastreamtest.cpp index ae6c1564ad..ca2456a6c5 100644 --- a/tests/streams/datastreamtest.cpp +++ b/tests/streams/datastreamtest.cpp @@ -26,6 +26,8 @@ #include "wx/wfstream.h" #include "wx/math.h" +#include "testfile.h" + // ---------------------------------------------------------------------------- // test class // ---------------------------------------------------------------------------- @@ -107,8 +109,10 @@ DataStreamTestCase::DataStreamTestCase() wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue) { + TempFile f("mytext.dat"); + { - wxFileOutputStream pFileOutput( wxT("mytext.dat") ); + wxFileOutputStream pFileOutput( f.GetName() ); wxDataOutputStream pDataOutput( pFileOutput ); if ( ms_useBigEndianFormat ) pDataOutput.BigEndianOrdered(true); @@ -121,7 +125,7 @@ wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue) pDataOutput << fValue; } - wxFileInputStream pFileInput( wxT("mytext.dat") ); + wxFileInputStream pFileInput( f.GetName() ); wxDataInputStream pDataInput( pFileInput ); if ( ms_useBigEndianFormat ) pDataInput.BigEndianOrdered(true); @@ -156,15 +160,17 @@ private: { ValueArray InValues(Size); + TempFile f("mytext.dat"); + { - wxFileOutputStream FileOutput( wxT("mytext.dat") ); + wxFileOutputStream FileOutput( f.GetName() ); wxDataOutputStream DataOutput( FileOutput ); (DataOutput.*pfnWriter)(Values, Size); } { - wxFileInputStream FileInput( wxT("mytext.dat") ); + wxFileInputStream FileInput( f.GetName() ); wxDataInputStream DataInput( FileInput ); (DataInput.*pfnReader)(&*InValues.begin(), InValues.size()); @@ -207,15 +213,17 @@ T TestRW(const T &Value) { T InValue; + TempFile f("mytext.dat"); + { - wxFileOutputStream FileOutput( wxT("mytext.dat") ); + wxFileOutputStream FileOutput( f.GetName() ); wxDataOutputStream DataOutput( FileOutput ); DataOutput << Value; } { - wxFileInputStream FileInput( wxT("mytext.dat") ); + wxFileInputStream FileInput( f.GetName() ); wxDataInputStream DataInput( FileInput ); DataInput >> InValue; diff --git a/tests/streams/textstreamtest.cpp b/tests/streams/textstreamtest.cpp index ec88c01ec8..e91cc5a1cf 100644 --- a/tests/streams/textstreamtest.cpp +++ b/tests/streams/textstreamtest.cpp @@ -31,6 +31,8 @@ #include "wx/mstream.h" #endif // wxUSE_UNICODE +#include "testfile.h" + // ---------------------------------------------------------------------------- // test class // ---------------------------------------------------------------------------- @@ -105,7 +107,9 @@ TextStreamTestCase::TextStreamTestCase() void TextStreamTestCase::Endline() { - wxFileOutputStream* pOutFile = new wxFileOutputStream(wxT("test.txt")); + TempFile f("test.txt"); + + wxFileOutputStream* pOutFile = new wxFileOutputStream(f.GetName()); wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile); *pOutText << wxT("Test text") << endl << wxT("More Testing Text (There should be newline before this)"); @@ -113,7 +117,7 @@ void TextStreamTestCase::Endline() delete pOutText; delete pOutFile; - wxFileInputStream* pInFile = new wxFileInputStream(wxT("test.txt")); + wxFileInputStream* pInFile = new wxFileInputStream(f.GetName()); char szIn[9 + NEWLINELEN]; @@ -147,8 +151,10 @@ void TextStreamTestCase::MiscTests() template static void DoTestRoundTrip(const T *values, size_t numValues) { + TempFile f("test.txt"); + { - wxFileOutputStream fileOut(wxT("test.txt")); + wxFileOutputStream fileOut(f.GetName()); wxTextOutputStream textOut(fileOut); for ( size_t n = 0; n < numValues; n++ ) @@ -158,7 +164,7 @@ static void DoTestRoundTrip(const T *values, size_t numValues) } { - wxFileInputStream fileIn(wxT("test.txt")); + wxFileInputStream fileIn(f.GetName()); wxTextInputStream textIn(fileIn); T value; diff --git a/tests/testfile.h b/tests/testfile.h index 519acbe8b2..9c1f5afa3d 100644 --- a/tests/testfile.h +++ b/tests/testfile.h @@ -41,5 +41,30 @@ private: wxString m_name; }; +// ---------------------------------------------------------------------------- +// TempFile: just a self deleting file +// ---------------------------------------------------------------------------- + +class TempFile +{ +public: + explicit TempFile(const wxString& name = wxString()) : m_name(name) { } + + void Assign(const wxString& name) { m_name = name; } + + const wxString& GetName() const { return m_name; } + + ~TempFile() + { + if ( !m_name.empty() ) + wxRemoveFile(m_name); + } + +private: + wxString m_name; + + wxDECLARE_NO_COPY_CLASS(TempFile); +}; + #endif // _WX_TESTS_TEMPFILE_H_ From 0858cd52a7430a8be1cf20acc3e0b7ef2f284c45 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Nov 2017 17:30:46 +0100 Subject: [PATCH 4/5] Don't unnecessarily use heap-allocated objects in a test Just use local stack variables instead. --- tests/streams/textstreamtest.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/streams/textstreamtest.cpp b/tests/streams/textstreamtest.cpp index e91cc5a1cf..0975e7526e 100644 --- a/tests/streams/textstreamtest.cpp +++ b/tests/streams/textstreamtest.cpp @@ -109,23 +109,20 @@ void TextStreamTestCase::Endline() { TempFile f("test.txt"); - wxFileOutputStream* pOutFile = new wxFileOutputStream(f.GetName()); - wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile); - *pOutText << wxT("Test text") << endl - << wxT("More Testing Text (There should be newline before this)"); + { + wxFileOutputStream pOutFile(f.GetName()); + wxTextOutputStream pOutText(pOutFile); + pOutText << wxT("Test text") << endl + << wxT("More Testing Text (There should be newline before this)"); + } - delete pOutText; - delete pOutFile; - - wxFileInputStream* pInFile = new wxFileInputStream(f.GetName()); + wxFileInputStream pInFile(f.GetName()); char szIn[9 + NEWLINELEN]; - pInFile->Read(szIn, 9 + NEWLINELEN); + pInFile.Read(szIn, 9 + NEWLINELEN); CPPUNIT_ASSERT( memcmp(&szIn[9], NEWLINE, NEWLINELEN) == 0 ); - - delete pInFile; } void TextStreamTestCase::MiscTests() From 99378ed0d757b0f37cdc7cc602b41e44cb8dbde5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Nov 2017 17:50:09 +0100 Subject: [PATCH 5/5] Disable tests using wxUIActionSimulator under non-MSW platforms wxUIActionSimulator is just too unreliable to be used there, so while fixing it should really be a priority, for now at least prevent these spurious failures from masking any other ones, which indicate real problems that need to be fixed. Notice that these tests can still be enabled by setting the environment variable WX_UI_TESTS to 1 (or disabled by setting it to 0 under MSW). --- tests/test.cpp | 31 +++++++++++++++++++++++++++++++ tests/testprec.h | 16 ++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/tests/test.cpp b/tests/test.cpp index 5e2f6c5c7d..ba853d53ea 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -360,6 +360,37 @@ extern bool IsAutomaticTest() #if wxUSE_GUI +bool EnableUITests() +{ + static int s_enabled = -1; + if ( s_enabled == -1 ) + { + // Allow explicitly configuring this via an environment variable under + // all platforms. + wxString enabled; + if ( wxGetEnv("WX_UI_TESTS", &enabled) ) + { + if ( enabled == "1" ) + s_enabled = 1; + else if ( enabled == "0" ) + s_enabled = 0; + else + wxFprintf(stderr, "Unknown \"WX_UI_TESTS\" value \"%s\" ignored.\n", enabled); + } + + if ( s_enabled == -1 ) + { +#ifdef __WXMSW__ + s_enabled = 1; +#else // !__WXMSW__ + s_enabled = 0; +#endif // __WXMSW__/!__WXMSW__ + } + } + + return s_enabled == 1; +} + void DeleteTestWindow(wxWindow* win) { if ( !win ) diff --git a/tests/testprec.h b/tests/testprec.h index 8bbaf4be70..f5661446ed 100644 --- a/tests/testprec.h +++ b/tests/testprec.h @@ -10,13 +10,14 @@ // this allows the tests that do not rely on it to run on platforms that don't // support it. // -// FIXME: And while OS X does support it, more or less, too many tests -// currently fail under it so disable all interactive tests there. They -// should, of course, be reenabled a.s.a.p. -#if wxUSE_UIACTIONSIMULATOR && !defined(__WXOSX__) - #define WXUISIM_TEST(test) CPPUNIT_TEST(test) +// Unfortunately, currently too many of the UI tests fail on non-MSW platforms, +// so they're disabled there by default. This really, really needs to be fixed, +// but for now having the UI tests always failing is not helpful as it prevents +// other test failures from being noticed, so disable them there. +#if wxUSE_UIACTIONSIMULATOR + #define WXUISIM_TEST(test) if ( EnableUITests() ) { CPPUNIT_TEST(test) } #else - #define WXUISIM_TEST(test) (void)0 + #define WXUISIM_TEST(test) #endif // define wxHAVE_U_ESCAPE if the compiler supports \uxxxx character constants @@ -169,6 +170,9 @@ private: #if wxUSE_GUI +// Return true if the UI tests are enabled, used by WXUISIM_TEST(). +extern bool EnableUITests(); + // Helper function deleting the window without asserts (and hence exceptions // thrown from its dtor!) even if it has mouse capture. void DeleteTestWindow(wxWindow* win);