[Qt] Avoid QApplication duplication via test main()

This commit is contained in:
Bruno de Oliveira Abinader
2017-01-13 15:58:29 +02:00
parent 49da57393a
commit 14fe15e3fc
5 changed files with 33 additions and 34 deletions

View File

@@ -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}"
)

View File

@@ -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) {

View File

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

24
platform/qt/test/main.cpp Normal file
View File

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

View File

@@ -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: