diff --git a/.travis.yml b/.travis.yml index e7cf20a43..7ed9a0680 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ before_install: script: - bash package/linux/travis-setup.sh - mkdir build && cd build - - cmake -DBOOST_ROOT=$BOOST_DIR -DSLIC3R_STATIC=1 ../src + - cmake -DBOOST_ROOT=$BOOST_DIR -DSLIC3R_STATIC=ON ../src - cmake --build . branches: only: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c3b3b4b35..e9e1fae1d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,9 @@ cmake_minimum_required (VERSION 3.9) project (slic3r) +option(GUI_BUILD_TESTS "Build tests for Slic3r GUI." ON) +option(SLIC3R_BUILD_TESTS "Build tests for libslic3r." ON) + # only on newer GCCs: -ftemplate-backtrace-limit=0 set(CMAKE_CXX_FLAGS "-g ${CMAKE_CXX_FLAGS} -Wall -DM_PI=3.14159265358979323846 -D_GLIBCXX_USE_C99 -DHAS_BOOL -DNOGDI -DBOOST_ASIO_DISABLE_KQUEUE") @@ -37,13 +40,15 @@ ELSE(CMAKE_HOST_APPLE) # set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -L.") ENDIF(CMAKE_HOST_APPLE) -if(DEFINED ENV{SLIC3R_STATIC}) +option(SLIC3R_STATIC "Build and link Slic3r statically." ON) + +if(SLIC3R_STATIC) set(Boost_USE_STATIC_LIBS ON) set(Boost_USE_STATIC_RUNTIME ON) -else(DEFINED ENV{SLIC3R_STATIC}) +else(SLIC3R_STATIC) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_STATIC_RUNTIME OFF) -endif(DEFINED ENV{SLIC3R_STATIC}) +endif(SLIC3R_STATIC) find_package(Threads REQUIRED) @@ -51,6 +56,8 @@ find_package(Boost REQUIRED COMPONENTS system thread filesystem) set(LIBDIR ${CMAKE_CURRENT_SOURCE_DIR}/../xs/src/) set(GUI_LIBDIR ${CMAKE_CURRENT_SOURCE_DIR}/GUI/) +set(TESTDIR ${CMAKE_CURRENT_SOURCE_DIR}/test) +set(GUI_TESTDIR ${CMAKE_CURRENT_SOURCE_DIR}/test/GUI/) include_directories(${LIBDIR}) include_directories(${LIBDIR}/libslic3r) @@ -154,6 +161,12 @@ add_library(poly2tri STATIC ${LIBDIR}/poly2tri/sweep/sweep.cc ) +set(UI_TEST_SOURCES + ${GUI_TESTDIR}/testableframe.cpp + ${GUI_TESTDIR}/test_harness_gui.cpp + ${GUI_TESTDIR}/test_field_checkbox.cpp +) + add_executable(slic3r slic3r.cpp) #set_target_properties(slic3r PROPERTIES LINK_SEARCH_START_STATIC 1) #set_target_properties(slic3r PROPERTIES LINK_SEARCH_END_STATIC 1) @@ -224,6 +237,43 @@ IF(wxWidgets_FOUND) #only build GUI lib if building with wx target_link_libraries (slic3r slic3r_gui ${wxWidgets_LIBRARIES}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_WX") + + if (GUI_BUILD_TESTS) + enable_testing() + if (NOT TARGET Catch) + include (ExternalProject) + if(IS_TRAVIS_BUILD) # on travis, use git for fetching instead of wget + set(FETCH_EXTERNAL_CATCH + GIT_REPOSITORY https://github.com/philsquared/Catch.git + GIT_TAG 19ab2117c5bac2f376f8da4a4b25e183137bcec0) + elseif(WIN32) + set(FETCH_EXTERNAL_CATCH + URL https://github.com/catchorg/Catch2/archive/v2.0.1.zip + URL_HASH MD5=1abca1b324b99b1631e999119b172620) + else() + set(FETCH_EXTERNAL_CATCH + URL https://github.com/catchorg/Catch2/archive/v2.0.1.tar.gz + URL_HASH MD5=2080f4696579351d9323b3b5a8c3c71b) + endif() + ExternalProject_Add(Catch-External + PREFIX ${CMAKE_BINARY_DIR}/external/Catch + ${FETCH_EXTERNAL_CATCH} + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/external/Catch/src/Catch-External/single_include/catch.hpp + ${CMAKE_BINARY_DIR}/external/Catch/include/catch.hpp + ) + add_library(Catch INTERFACE) + add_dependencies(Catch Catch-External) + + target_include_directories(Catch INTERFACE ${CMAKE_BINARY_DIR}/external/Catch/include) + target_compile_definitions(Catch INTERFACE $<$:_SILENCE_CXX17_UNCAUGHT_EXCEPTION_DEPRECATION_WARNING>) + endif() + add_executable(gui_test ${UI_TEST_SOURCES}) + add_test(NAME TestBase COMMAND gui_test) + target_link_libraries(gui_test PUBLIC libslic3r slic3r_gui Catch ${wxWidgets_LIBRARIES}) + + endif() ELSE(wxWidgets_FOUND) # For convenience. When we cannot continue, inform the user MESSAGE("wx not found!") @@ -249,4 +299,9 @@ IF (WIN32) # target_link_libraries(extrude-tin boost-nowide) ENDIF(WIN32) + + + + + #target_link_libraries (extrude-tin libslic3r admesh BSpline clipper expat polypartition poly2tri ${Boost_LIBRARIES}) diff --git a/src/test/GUI/test_field_checkbox.cpp b/src/test/GUI/test_field_checkbox.cpp new file mode 100644 index 000000000..23f275068 --- /dev/null +++ b/src/test/GUI/test_field_checkbox.cpp @@ -0,0 +1,11 @@ +#include "test/catch.hpp" + +#ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/checkbox.h" +#endif // WX_PRECOMP + +#include "testableframe.h" + +TEST_CASE( "Dummy" ) { +} diff --git a/src/test/GUI/test_harness_gui.cpp b/src/test/GUI/test_harness_gui.cpp new file mode 100644 index 000000000..fe8666f2d --- /dev/null +++ b/src/test/GUI/test_harness_gui.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file +#include "test/catch.hpp" diff --git a/src/test/GUI/testableframe.cpp b/src/test/GUI/testableframe.cpp new file mode 100644 index 000000000..fae750124 --- /dev/null +++ b/src/test/GUI/testableframe.cpp @@ -0,0 +1,61 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: testableframe.cpp +// Purpose: An improved wxFrame for unit-testing +// Author: Steven Lamerton +// Copyright: (c) 2010 Steven Lamerton +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#include "wx/app.h" +#include "testableframe.h" + +wxTestableFrame::wxTestableFrame() : wxFrame(NULL, wxID_ANY, "Test Frame") +{ + // Use fixed position to facilitate debugging. + Move(200, 200); + + Show(); +} + +void wxTestableFrame::OnEvent(wxEvent& evt) +{ + m_count[evt.GetEventType()]++; + + if(! evt.IsCommandEvent() ) + evt.Skip(); +} + +int wxTestableFrame::GetEventCount(wxEventType type) +{ + return m_count[type]; +} + +void wxTestableFrame::ClearEventCount(wxEventType type) +{ + m_count[type] = 0; +} + +EventCounter::EventCounter(wxWindow* win, wxEventType type) : m_type(type), + m_win(win) + +{ + m_frame = wxStaticCast(wxTheApp->GetTopWindow(), wxTestableFrame); + + m_win->Connect(m_type, wxEventHandler(wxTestableFrame::OnEvent), + NULL, m_frame); +} + +EventCounter::~EventCounter() +{ + m_win->Disconnect(m_type, wxEventHandler(wxTestableFrame::OnEvent), + NULL, m_frame); + + //This stops spurious counts from previous tests + Clear(); + + m_frame = NULL; + m_win = NULL; +} diff --git a/src/test/GUI/testableframe.h b/src/test/GUI/testableframe.h new file mode 100644 index 000000000..a0465d829 --- /dev/null +++ b/src/test/GUI/testableframe.h @@ -0,0 +1,42 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: testableframe.h +// Purpose: An improved wxFrame for unit-testing +// Author: Steven Lamerton +// Copyright: (c) 2010 Steven Lamerton +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#include "wx/frame.h" +#include "wx/hashmap.h" +#include "wx/event.h" + +class wxTestableFrame : public wxFrame +{ +public: + wxTestableFrame(); + + void OnEvent(wxEvent& evt); + +private: + friend class EventCounter; + + int GetEventCount(wxEventType type); + void ClearEventCount(wxEventType type); + + wxLongToLongHashMap m_count; +}; + +class EventCounter +{ +public: + EventCounter(wxWindow* win, wxEventType type); + ~EventCounter(); + + int GetCount() { return m_frame->GetEventCount(m_type); } + void Clear() { m_frame->ClearEventCount(m_type); } + +private: + wxEventType m_type; + wxTestableFrame* m_frame; + wxWindow* m_win; +};