Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[Qt] Avoid QApplication duplication via test main()
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Jan 18, 2017
1 parent 49da573 commit 14fe15e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
6 changes: 3 additions & 3 deletions platform/qt/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ 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
PRIVATE platform/default/mbgl/gl/headless_display.hpp
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}"
)

Expand Down
19 changes: 3 additions & 16 deletions platform/qt/src/run_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down Expand Up @@ -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) {
Expand All @@ -90,6 +75,7 @@ void RunLoop::run() {
}

void RunLoop::stop() {
assert(QCoreApplication::instance());
invoke([&] {
if (impl->type == Type::Default) {
QCoreApplication::instance()->exit();
Expand All @@ -100,6 +86,7 @@ void RunLoop::stop() {
}

void RunLoop::runOnce() {
assert(QCoreApplication::instance());
MBGL_VERIFY_THREAD(tid);

if (impl->type == Type::Default) {
Expand Down
8 changes: 0 additions & 8 deletions platform/qt/test/headless_backend_qt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <mbgl/gl/headless_backend.hpp>

#include <QApplication>
#include <QGLContext>
#include <QGLWidget>

Expand Down Expand Up @@ -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);
}

Expand Down
24 changes: 24 additions & 0 deletions platform/qt/test/main.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 3 additions & 7 deletions platform/qt/test/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -26,19 +26,15 @@ class QMapboxGLTest : public QObject, public ::testing::Test {
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:
Expand Down

0 comments on commit 14fe15e

Please sign in to comment.