From 8dd23d167361b2a17f715bf17e228156d9b6e9cb Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Wed, 21 Mar 2018 00:02:43 +0100 Subject: [PATCH] Unit tests update --- examples/termcap.cpp | 12 +++++--- examples/ui.cpp | 2 +- include/final/fstring.h | 1 + src/fbutton.cpp | 6 ++-- src/fdialog.cpp | 2 +- src/ffiledialog.cpp | 2 +- src/fmouse.cpp | 11 +++---- src/fstring.cpp | 33 ++++++++++++++++++++ src/test/fmouse-test.cpp | 66 +++++++++++++++++++++++++++++++++++++++- 9 files changed, 117 insertions(+), 18 deletions(-) diff --git a/examples/termcap.cpp b/examples/termcap.cpp index 4f729329..09b5cb07 100644 --- a/examples/termcap.cpp +++ b/examples/termcap.cpp @@ -205,12 +205,16 @@ void tcapString (const std::string& name, const char cap_str[]) //---------------------------------------------------------------------- void debug (FApplication& TermApp) { + const FString& ab_s = TermApp.getAnswerbackString(); + const FString& sec_da= TermApp.getSecDAString(); + #if DEBUG std::cout << "\n.------------------- debug -------------------\r\n"; #if defined(__linux__) std::cout << "| Framebuffer bpp: " << TermApp.framebuffer_bpp << "\r\n"; #endif + std::cout << "| after init_256colorTerminal(): " << TermApp.termtype_256color << "\r\n"; std::cout << "| after parseAnswerbackMsg(): " @@ -218,11 +222,11 @@ void debug (FApplication& TermApp) std::cout << "| after parseSecDA(): " << TermApp.termtype_SecDA << "\r\n"; - if ( const FString& s = TermApp.getAnswerbackString() ) - tcapString ("| The answerback String", s); + if ( ab_s.isEmpty() ) + tcapString ("| The answerback String", ab_s); - if ( const FString& s = TermApp.getSecDAString() ) - tcapString ("| The SecDA String", s); + if ( ab_s.isEmpty() ) + tcapString ("| The SecDA String", sec_da); std::cout << "`------------------- debug -------------------\r\n"; #endif diff --git a/examples/ui.cpp b/examples/ui.cpp index 52c0ba39..c2c94615 100644 --- a/examples/ui.cpp +++ b/examples/ui.cpp @@ -1018,7 +1018,7 @@ void MyDialog::cb_view (FWidget*, data_ptr data) FString file; FMenuItem* item = static_cast(data); - if ( item && item->getText() ) + if ( item && ! item->getText().isEmpty() ) file = item->getText(); else file = FFileDialog::fileOpenChooser (this); diff --git a/include/final/fstring.h b/include/final/fstring.h index f656dac2..cded68c5 100644 --- a/include/final/fstring.h +++ b/include/final/fstring.h @@ -226,6 +226,7 @@ class FString wchar_t front() const; wchar_t back() const; + FString& sprintf (const FString&, ...); FString& sprintf (const wchar_t[], ...); FString& sprintf (const char[], ...) #if defined(__clang__) diff --git a/src/fbutton.cpp b/src/fbutton.cpp index 1d30a7b5..9232a553 100644 --- a/src/fbutton.cpp +++ b/src/fbutton.cpp @@ -250,10 +250,10 @@ bool FButton::setDown (bool on) //---------------------------------------------------------------------- void FButton::setText (const FString& txt) { - if ( txt ) - text = txt; - else + if ( txt.isNull() ) text = ""; + else + text = txt; detectHotkey(); } diff --git a/src/fdialog.cpp b/src/fdialog.cpp index 5461afbe..7c753528 100644 --- a/src/fdialog.cpp +++ b/src/fdialog.cpp @@ -1212,7 +1212,7 @@ void FDialog::drawTextBar() print (' '); // Print title bar text - if ( tb_text ) + if ( ! tb_text.isEmpty() ) { if ( length <= getWidth() - MENU_BTN - zoom_btn ) print (tb_text); diff --git a/src/ffiledialog.cpp b/src/ffiledialog.cpp index d97a7d6c..ecb3af74 100644 --- a/src/ffiledialog.cpp +++ b/src/ffiledialog.cpp @@ -108,7 +108,7 @@ FFileDialog::FFileDialog ( const FString& dirname , dlg_type(type) , show_hidden(false) { - if ( dirname ) + if ( ! dirname.isNull() ) setPath(dirname); init(); diff --git a/src/fmouse.cpp b/src/fmouse.cpp index adfa720d..f263808f 100644 --- a/src/fmouse.cpp +++ b/src/fmouse.cpp @@ -371,11 +371,10 @@ bool FMouseGPM::gpmMouse (bool on) switch ( gpm_fd ) { - case -1: + case -1: // error return false; - case -2: - Gpm_Close(); + case -2: // xterm is in use return false; default: @@ -503,11 +502,11 @@ void FMouseX11::setRawData (char fifo_buf[], int fifo_buf_size) for (n = len; n < fifo_buf_size; n++) fifo_buf[n - len] = fifo_buf[n]; - n = fifo_buf_size - len - 1; + n = fifo_buf_size - len; // Fill rest with '\0' for (; n < fifo_buf_size; n++) - fifo_buf[n - len] = '\0'; + fifo_buf[n] = '\0'; input_data_pending = bool(fifo_buf[0] != '\0'); } @@ -1238,8 +1237,6 @@ void FMouseControl::clearEvent() #ifdef F_HAVE_LIBGPM void FMouseControl::setStdinNo (int file_descriptor) { - - FMouse* mouse = mouse_protocol[FMouse::gpm]; FMouseGPM* gpm_mouse = static_cast(mouse); diff --git a/src/fstring.cpp b/src/fstring.cpp index 5a4882c5..a4883aa5 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -734,6 +734,27 @@ uInt FString::getUTF8length() const return len; } +//---------------------------------------------------------------------- +FString& FString::sprintf (const FString& format, ...) +{ + static const int BUFSIZE = 4096; + wchar_t buffer[BUFSIZE]; + va_list args; + + if ( ! format ) + { + clear(); + return *this; + } + + va_start (args, format); + std::vswprintf (buffer, BUFSIZE, format.wc_str(), args); + va_end (args); + + _assign (buffer); + return *this; +} + //---------------------------------------------------------------------- FString& FString::sprintf (const wchar_t format[], ...) { @@ -741,6 +762,12 @@ FString& FString::sprintf (const wchar_t format[], ...) wchar_t buffer[BUFSIZE]; va_list args; + if ( ! format ) + { + clear(); + return *this; + } + va_start (args, format); std::vswprintf (buffer, BUFSIZE, format, args); va_end (args); @@ -758,6 +785,12 @@ FString& FString::sprintf (const char format[], ...) int len; va_list args; + if ( ! format ) + { + clear(); + return *this; + } + buffer = buf; va_start (args, format); len = vsnprintf (buffer, sizeof(buf), format, args); diff --git a/src/test/fmouse-test.cpp b/src/test/fmouse-test.cpp index 194da783..0a63f0d2 100644 --- a/src/test/fmouse-test.cpp +++ b/src/test/fmouse-test.cpp @@ -96,7 +96,10 @@ class FMouseTest : public CPPUNIT_NS::TestFixture void noArgumentTest(); void doubleClickTest(); void workspaceSizeTest(); - +#ifdef F_HAVE_LIBGPM + void gpmMouseTest(); +#endif + void x11MouseTest(); private: // Adds code needed to register the test suite @@ -107,6 +110,10 @@ class FMouseTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST (noArgumentTest); CPPUNIT_TEST (doubleClickTest); CPPUNIT_TEST (workspaceSizeTest); +#ifdef F_HAVE_LIBGPM + CPPUNIT_TEST (gpmMouseTest); +#endif + CPPUNIT_TEST (x11MouseTest); // End of test suite definition CPPUNIT_TEST_SUITE_END(); @@ -221,6 +228,63 @@ void FMouseTest::workspaceSizeTest() CPPUNIT_ASSERT ( mouse.getMaxHeight() == 30 ); } +#ifdef F_HAVE_LIBGPM +//---------------------------------------------------------------------- +void FMouseTest::gpmMouseTest() +{ + FMouseGPM gpm_mouse; + CPPUNIT_ASSERT ( ! gpm_mouse.isGpmMouseEnabled() ); + + gpm_mouse.setStdinNo(fileno(stdin)); + + if ( gpm_mouse.enableGpmMouse() ) + { + CPPUNIT_ASSERT ( gpm_mouse.isGpmMouseEnabled() ); + timeval tv; + FObject::getCurrentTime(&tv); + gpm_mouse.processEvent(&tv); + CPPUNIT_ASSERT ( ! gpm_mouse.hasEvent() ); + } + else + CPPUNIT_ASSERT ( ! gpm_mouse.isGpmMouseEnabled() ); + + gpm_mouse.disableGpmMouse(); + CPPUNIT_ASSERT ( ! gpm_mouse.isGpmMouseEnabled() ); +} +#endif + +//---------------------------------------------------------------------- +void FMouseTest::x11MouseTest() +{ + FMouseX11 x11_mouse; + CPPUNIT_ASSERT ( ! x11_mouse.hasData() ); + + char x11_mouse_data[8] = { 0x1b, '[', 'M', 0x23, 0x50, 0x32, 0x40, 0x40 }; + x11_mouse.setRawData (x11_mouse_data, 8); + CPPUNIT_ASSERT ( x11_mouse.hasData() ); + CPPUNIT_ASSERT ( x11_mouse.isInputDataPending() ); + + timeval tv; + FObject::getCurrentTime(&tv); + x11_mouse.processEvent (&tv); + + CPPUNIT_ASSERT ( x11_mouse.getPos() == FPoint(48, 18) ); + CPPUNIT_ASSERT ( x11_mouse.hasEvent() ); + CPPUNIT_ASSERT ( ! x11_mouse.isLeftButtonPressed() ); + CPPUNIT_ASSERT ( ! x11_mouse.isLeftButtonReleased() ); + CPPUNIT_ASSERT ( ! x11_mouse.isLeftButtonDoubleClick() ); + CPPUNIT_ASSERT ( ! x11_mouse.isRightButtonPressed() ); + CPPUNIT_ASSERT ( ! x11_mouse.isRightButtonReleased() ); + CPPUNIT_ASSERT ( ! x11_mouse.isMiddleButtonPressed() ); + CPPUNIT_ASSERT ( ! x11_mouse.isMiddleButtonReleased() ); + CPPUNIT_ASSERT ( ! x11_mouse.isShiftKeyPressed() ); + CPPUNIT_ASSERT ( ! x11_mouse.isControlKeyPressed() ); + CPPUNIT_ASSERT ( ! x11_mouse.isMetaKeyPressed() ); + CPPUNIT_ASSERT ( ! x11_mouse.isWheelUp() ); + CPPUNIT_ASSERT ( ! x11_mouse.isWheelDown() ); + CPPUNIT_ASSERT ( ! x11_mouse.isMoved() ); +} + // Put the test suite in the registry CPPUNIT_TEST_SUITE_REGISTRATION (FMouseTest);