diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-01-13 15:58:29 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-01-18 19:06:32 +0100 |
commit | 14fe15e3fc2b4c3013806047d202b3ca6beed259 (patch) | |
tree | c2f81105090b0a274ebecfd12fa1e5057477577f /platform/qt | |
parent | 49da57393a5c5ddff1a7f60620c38858643b1220 (diff) | |
download | qtlocation-mapboxgl-14fe15e3fc2b4c3013806047d202b3ca6beed259.tar.gz |
[Qt] Avoid QApplication duplication via test main()
Diffstat (limited to 'platform/qt')
-rw-r--r-- | platform/qt/config.cmake | 6 | ||||
-rw-r--r-- | platform/qt/src/run_loop.cpp | 19 | ||||
-rw-r--r-- | platform/qt/test/headless_backend_qt.cpp | 8 | ||||
-rw-r--r-- | platform/qt/test/main.cpp | 24 | ||||
-rw-r--r-- | platform/qt/test/qmapboxgl.cpp | 10 |
5 files changed, 33 insertions, 34 deletions
diff --git a/platform/qt/config.cmake b/platform/qt/config.cmake index f2beedcfa2..e1c454c757 100644 --- a/platform/qt/config.cmake +++ b/platform/qt/config.cmake @@ -54,8 +54,6 @@ endmacro() macro(mbgl_platform_test) target_sources(mbgl-test - PRIVATE test/src/main.cpp - PRIVATE platform/qt/test/qmapboxgl.cpp PRIVATE platform/default/mbgl/gl/headless_backend.cpp PRIVATE platform/default/mbgl/gl/headless_backend.hpp PRIVATE platform/default/mbgl/gl/headless_display.cpp @@ -63,10 +61,12 @@ macro(mbgl_platform_test) PRIVATE platform/default/mbgl/gl/offscreen_view.cpp PRIVATE platform/default/mbgl/gl/offscreen_view.hpp PRIVATE platform/qt/test/headless_backend_qt.cpp + PRIVATE platform/qt/test/main.cpp + PRIVATE platform/qt/test/qmapboxgl.cpp ) set_source_files_properties( - test/src/main.cpp + platform/qt/test/main.cpp PROPERTIES COMPILE_FLAGS -DWORK_DIRECTORY="${CMAKE_SOURCE_DIR}" ) diff --git a/platform/qt/src/run_loop.cpp b/platform/qt/src/run_loop.cpp index 1c199be9de..c44f284852 100644 --- a/platform/qt/src/run_loop.cpp +++ b/platform/qt/src/run_loop.cpp @@ -31,22 +31,6 @@ RunLoop* RunLoop::Get() { } RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) { - // XXX: We should probably throw an runtime exception - // here instead of creating a QCoreApplication which is - // way too intrusive. This is a hack mostly for the unit - // tests to work, as you always need a QCoreApplication - // prior to run a Qt app. - if (!QCoreApplication::instance()) { - static const char* argv[] = { "mbgl" }; - static int argc = 1; - - // We need to keep this around because it would otherwise crash - // on Qt4 due to a bug on QNetworkConfigurationManager when recreating - // a QCoreApplication: https://bugreports.qt.io/browse/QTBUG-36897 - static auto* app = new QCoreApplication(argc, const_cast<char**>(argv)); - Q_UNUSED(app); - } - switch (type) { case Type::New: impl->loop = std::make_unique<QEventLoop>(); @@ -80,6 +64,7 @@ void RunLoop::push(std::shared_ptr<WorkTask> task) { } void RunLoop::run() { + assert(QCoreApplication::instance()); MBGL_VERIFY_THREAD(tid); if (impl->type == Type::Default) { @@ -90,6 +75,7 @@ void RunLoop::run() { } void RunLoop::stop() { + assert(QCoreApplication::instance()); invoke([&] { if (impl->type == Type::Default) { QCoreApplication::instance()->exit(); @@ -100,6 +86,7 @@ void RunLoop::stop() { } void RunLoop::runOnce() { + assert(QCoreApplication::instance()); MBGL_VERIFY_THREAD(tid); if (impl->type == Type::Default) { diff --git a/platform/qt/test/headless_backend_qt.cpp b/platform/qt/test/headless_backend_qt.cpp index 1992cab2fa..00b519d5bf 100644 --- a/platform/qt/test/headless_backend_qt.cpp +++ b/platform/qt/test/headless_backend_qt.cpp @@ -1,6 +1,5 @@ #include <mbgl/gl/headless_backend.hpp> -#include <QApplication> #include <QGLContext> #include <QGLWidget> @@ -40,13 +39,6 @@ bool HeadlessBackend::hasDisplay() { void HeadlessBackend::createContext() { assert(!hasContext()); - - static const char* argv[] = { "mbgl" }; - static int argc = 1; - static auto* app = new QApplication(argc, const_cast<char**>(argv)); - - Q_UNUSED(app); - impl.reset(new QtImpl); } diff --git a/platform/qt/test/main.cpp b/platform/qt/test/main.cpp new file mode 100644 index 0000000000..d9f7d480e5 --- /dev/null +++ b/platform/qt/test/main.cpp @@ -0,0 +1,24 @@ +#include <QApplication> + +#include <mbgl/test.hpp> +#include <unistd.h> +#include <cstring> +#include <cerrno> +#include <cstdio> + +#define xstr(s) str(s) +#define str(s) #s + +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + +#ifdef WORK_DIRECTORY + const int result = chdir(xstr(WORK_DIRECTORY)); + if (result != 0) { + fprintf(stderr, "failed to change directory: %s\n", strerror(errno)); + return errno; + } +#endif + + return mbgl::runTests(argc, argv); +} diff --git a/platform/qt/test/qmapboxgl.cpp b/platform/qt/test/qmapboxgl.cpp index 9406fa2420..8b88a4f6f6 100644 --- a/platform/qt/test/qmapboxgl.cpp +++ b/platform/qt/test/qmapboxgl.cpp @@ -10,7 +10,7 @@ class QMapboxGLTest : public QObject, public ::testing::Test { Q_OBJECT public: - QMapboxGLTest() : app(argc, const_cast<char**>(&argv)), map(nullptr, settings) { + QMapboxGLTest() : map(nullptr, settings) { connect(&map, SIGNAL(mapChanged(QMapboxGL::MapChange)), this, SLOT(onMapChanged(QMapboxGL::MapChange))); connect(&map, SIGNAL(needsRendering()), @@ -26,19 +26,15 @@ public: void runUntil(QMapboxGL::MapChange status) { changeCallback = [&](QMapboxGL::MapChange change) { if (change == status) { - app.exit(); + qApp->exit(); changeCallback = nullptr; } }; - app.exec(); + qApp->exec(); } private: - int argc = 1; - const char* argv = "mbgl-test"; - - QApplication app; QGLWidget widget; protected: |