2016-09-26 12:40:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "simulatorcontrol.h"
|
|
|
|
|
#include "iosconfigurations.h"
|
|
|
|
|
|
2019-01-08 16:05:57 +01:00
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/runextensions.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/synchronousprocess.h>
|
2016-10-27 16:45:20 +02:00
|
|
|
|
2016-09-26 12:40:09 +02:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
2016-11-04 17:39:39 +01:00
|
|
|
#include <memory>
|
2016-09-26 12:40:09 +02:00
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
using namespace std;
|
|
|
|
|
|
2016-09-26 12:40:09 +02:00
|
|
|
namespace {
|
2018-10-12 09:33:30 +03:00
|
|
|
Q_LOGGING_CATEGORY(simulatorLog, "qtc.ios.simulator", QtWarningMsg)
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Ios {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2017-03-15 17:04:47 +01:00
|
|
|
const int simulatorStartTimeout = 60000;
|
2017-01-23 15:39:01 +01:00
|
|
|
|
|
|
|
|
// simctl Json Tags and tokens.
|
2017-03-15 17:04:47 +01:00
|
|
|
const char deviceTypeTag[] = "devicetypes";
|
|
|
|
|
const char devicesTag[] = "devices";
|
|
|
|
|
const char availabilityTag[] = "availability";
|
|
|
|
|
const char unavailabilityToken[] = "unavailable";
|
|
|
|
|
const char identifierTag[] = "identifier";
|
|
|
|
|
const char runtimesTag[] = "runtimes";
|
|
|
|
|
const char nameTag[] = "name";
|
|
|
|
|
const char stateTag[] = "state";
|
|
|
|
|
const char udidTag[] = "udid";
|
|
|
|
|
const char runtimeVersionTag[] = "version";
|
|
|
|
|
const char buildVersionTag[] = "buildversion";
|
2016-09-26 12:40:09 +02:00
|
|
|
|
2017-01-09 12:43:00 +01:00
|
|
|
static bool checkForTimeout(const chrono::high_resolution_clock::time_point &start, int msecs = 10000)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
|
|
|
|
bool timedOut = false;
|
2016-11-04 17:39:39 +01:00
|
|
|
auto end = chrono::high_resolution_clock::now();
|
|
|
|
|
if (chrono::duration_cast<chrono::milliseconds>(end-start).count() > msecs)
|
2016-09-26 12:40:09 +02:00
|
|
|
timedOut = true;
|
|
|
|
|
return timedOut;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 16:46:23 +02:00
|
|
|
static bool runCommand(QString command, const QStringList &args, QString *output)
|
2016-11-04 17:39:39 +01:00
|
|
|
{
|
|
|
|
|
Utils::SynchronousProcess p;
|
2016-12-19 18:41:00 +01:00
|
|
|
p.setTimeoutS(-1);
|
2016-11-04 17:39:39 +01:00
|
|
|
Utils::SynchronousProcessResponse resp = p.runBlocking(command, args);
|
|
|
|
|
if (output)
|
2018-05-14 16:46:23 +02:00
|
|
|
*output = resp.stdOut();
|
2016-11-04 17:39:39 +01:00
|
|
|
return resp.result == Utils::SynchronousProcessResponse::Finished;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 16:46:23 +02:00
|
|
|
static bool runSimCtlCommand(QStringList args, QString *output)
|
2016-10-27 16:22:03 +02:00
|
|
|
{
|
2017-03-15 17:04:47 +01:00
|
|
|
args.prepend("simctl");
|
|
|
|
|
return runCommand("xcrun", args, output);
|
2016-11-04 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
2017-11-14 12:48:45 +01:00
|
|
|
static bool launchSimulator(const QString &simUdid) {
|
|
|
|
|
QTC_ASSERT(!simUdid.isEmpty(), return false);
|
|
|
|
|
const QString simulatorAppPath = IosConfigurations::developerPath()
|
2019-05-15 15:49:19 +02:00
|
|
|
.pathAppended("Applications/Simulator.app/Contents/MacOS/Simulator").toString();
|
2017-11-14 12:48:45 +01:00
|
|
|
|
|
|
|
|
if (IosConfigurations::xcodeVersion() >= QVersionNumber(9)) {
|
|
|
|
|
// For XCode 9 boot the second device instead of launching simulator app twice.
|
2018-05-14 16:46:23 +02:00
|
|
|
QString psOutput;
|
2017-11-14 12:48:45 +01:00
|
|
|
if (runCommand("ps", {"-A", "-o", "comm"}, &psOutput)) {
|
2018-05-14 16:46:23 +02:00
|
|
|
for (const QString &comm : psOutput.split('\n')) {
|
|
|
|
|
if (comm == simulatorAppPath)
|
2017-11-14 12:48:45 +01:00
|
|
|
return runSimCtlCommand(QStringList({"boot", simUdid}), nullptr);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-10-22 16:34:31 +02:00
|
|
|
qCDebug(simulatorLog) << "Cannot start Simulator device."
|
2017-11-14 12:48:45 +01:00
|
|
|
<< "Error probing Simulator.app instance";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QProcess::startDetached(simulatorAppPath, {"--args", "-CurrentDeviceUDID", simUdid});
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 15:39:01 +01:00
|
|
|
static QList<DeviceTypeInfo> getAvailableDeviceTypes()
|
|
|
|
|
{
|
|
|
|
|
QList<DeviceTypeInfo> deviceTypes;
|
2018-05-14 16:46:23 +02:00
|
|
|
QString output;
|
2017-03-15 17:04:47 +01:00
|
|
|
runSimCtlCommand({"list", "-j", deviceTypeTag}, &output);
|
2018-05-14 16:46:23 +02:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
|
2017-01-23 15:39:01 +01:00
|
|
|
if (!doc.isNull()) {
|
2017-03-15 17:04:47 +01:00
|
|
|
const QJsonArray runtimesArray = doc.object().value(deviceTypeTag).toArray();
|
2017-01-23 15:39:01 +01:00
|
|
|
foreach (const QJsonValue deviceTypeValue, runtimesArray) {
|
|
|
|
|
QJsonObject deviceTypeObject = deviceTypeValue.toObject();
|
2017-03-15 17:04:47 +01:00
|
|
|
if (!deviceTypeObject.value(availabilityTag).toString().contains(unavailabilityToken)) {
|
2017-01-23 15:39:01 +01:00
|
|
|
DeviceTypeInfo deviceType;
|
2017-03-15 17:04:47 +01:00
|
|
|
deviceType.name = deviceTypeObject.value(nameTag).toString("unknown");
|
|
|
|
|
deviceType.identifier = deviceTypeObject.value(identifierTag).toString("unknown");
|
2017-01-23 15:39:01 +01:00
|
|
|
deviceTypes.append(deviceType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
stable_sort(deviceTypes.begin(), deviceTypes.end());
|
|
|
|
|
} else {
|
|
|
|
|
qCDebug(simulatorLog) << "Error parsing json output from simctl. Output:" << output;
|
|
|
|
|
}
|
|
|
|
|
return deviceTypes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QList<RuntimeInfo> getAvailableRuntimes()
|
|
|
|
|
{
|
|
|
|
|
QList<RuntimeInfo> runtimes;
|
2018-05-14 16:46:23 +02:00
|
|
|
QString output;
|
2017-03-15 17:04:47 +01:00
|
|
|
runSimCtlCommand({"list", "-j", runtimesTag}, &output);
|
2018-05-14 16:46:23 +02:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
|
2017-01-23 15:39:01 +01:00
|
|
|
if (!doc.isNull()) {
|
2017-03-15 17:04:47 +01:00
|
|
|
const QJsonArray runtimesArray = doc.object().value(runtimesTag).toArray();
|
2017-01-23 15:39:01 +01:00
|
|
|
foreach (const QJsonValue runtimeValue, runtimesArray) {
|
|
|
|
|
QJsonObject runtimeObject = runtimeValue.toObject();
|
2017-03-15 17:04:47 +01:00
|
|
|
if (!runtimeObject.value(availabilityTag).toString().contains(unavailabilityToken)) {
|
2017-01-23 15:39:01 +01:00
|
|
|
RuntimeInfo runtime;
|
2017-03-15 17:04:47 +01:00
|
|
|
runtime.name = runtimeObject.value(nameTag).toString("unknown");
|
|
|
|
|
runtime.build = runtimeObject.value(buildVersionTag).toString("unknown");
|
|
|
|
|
runtime.identifier = runtimeObject.value(identifierTag).toString("unknown");
|
|
|
|
|
runtime.version = runtimeObject.value(runtimeVersionTag).toString("unknown");
|
2017-01-23 15:39:01 +01:00
|
|
|
runtimes.append(runtime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
stable_sort(runtimes.begin(), runtimes.end());
|
|
|
|
|
} else {
|
|
|
|
|
qCDebug(simulatorLog) << "Error parsing json output from simctl. Output:" << output;
|
|
|
|
|
}
|
|
|
|
|
return runtimes;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
class SimulatorControlPrivate {
|
2016-09-26 12:40:09 +02:00
|
|
|
private:
|
2016-11-04 17:39:39 +01:00
|
|
|
SimulatorControlPrivate();
|
2016-09-26 12:40:09 +02:00
|
|
|
~SimulatorControlPrivate();
|
|
|
|
|
|
2017-01-24 16:30:26 +01:00
|
|
|
static SimulatorInfo deviceInfo(const QString &simUdid);
|
2016-11-04 17:39:39 +01:00
|
|
|
static QString bundleIdentifier(const Utils::FileName &bundlePath);
|
|
|
|
|
static QString bundleExecutable(const Utils::FileName &bundlePath);
|
|
|
|
|
|
|
|
|
|
void startSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi, const QString &simUdid);
|
|
|
|
|
void installApp(QFutureInterface<SimulatorControl::ResponseData> &fi, const QString &simUdid,
|
|
|
|
|
const Utils::FileName &bundlePath);
|
|
|
|
|
void launchApp(QFutureInterface<SimulatorControl::ResponseData> &fi, const QString &simUdid,
|
2016-12-19 18:41:00 +01:00
|
|
|
const QString &bundleIdentifier, bool waitForDebugger,
|
2016-12-21 18:28:42 +01:00
|
|
|
const QStringList &extraArgs, const QString &stdoutPath,
|
|
|
|
|
const QString &stderrPath);
|
2017-01-23 15:39:01 +01:00
|
|
|
void deleteSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid);
|
|
|
|
|
void resetSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid);
|
|
|
|
|
void renameSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid, const QString &newName);
|
|
|
|
|
void createSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &name, const DeviceTypeInfo &deviceType,
|
|
|
|
|
const RuntimeInfo &runtime);
|
|
|
|
|
void takeSceenshot(QFutureInterface<SimulatorControl::ResponseData> &fi, const QString &simUdid,
|
|
|
|
|
const QString &filePath);
|
2016-11-04 17:39:39 +01:00
|
|
|
|
2017-01-24 16:30:26 +01:00
|
|
|
static QList<SimulatorInfo> availableDevices;
|
2017-01-23 15:39:01 +01:00
|
|
|
static QList<DeviceTypeInfo> availableDeviceTypes;
|
|
|
|
|
static QList<RuntimeInfo> availableRuntimes;
|
2016-09-26 12:40:09 +02:00
|
|
|
friend class SimulatorControl;
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
SimulatorControl::SimulatorControl(QObject *parent) :
|
|
|
|
|
QObject(parent),
|
|
|
|
|
d(new SimulatorControlPrivate)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
}
|
2016-09-26 12:40:09 +02:00
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
SimulatorControl::~SimulatorControl()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-24 16:30:26 +01:00
|
|
|
QList<SimulatorInfo> SimulatorControl::availableSimulators()
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
return SimulatorControlPrivate::availableDevices;
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-24 16:30:26 +01:00
|
|
|
static QList<SimulatorInfo> getAllSimulatorDevices()
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2017-01-24 16:30:26 +01:00
|
|
|
QList<SimulatorInfo> simulatorDevices;
|
2018-05-14 16:46:23 +02:00
|
|
|
QString output;
|
2017-03-15 17:04:47 +01:00
|
|
|
runSimCtlCommand({"list", "-j", devicesTag}, &output);
|
2018-05-14 16:46:23 +02:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
|
2016-09-26 12:40:09 +02:00
|
|
|
if (!doc.isNull()) {
|
2017-03-15 17:04:47 +01:00
|
|
|
const QJsonObject runtimeObject = doc.object().value(devicesTag).toObject();
|
2017-01-24 16:30:26 +01:00
|
|
|
foreach (const QString &runtime, runtimeObject.keys()) {
|
|
|
|
|
const QJsonArray devices = runtimeObject.value(runtime).toArray();
|
|
|
|
|
foreach (const QJsonValue deviceValue, devices) {
|
|
|
|
|
QJsonObject deviceObject = deviceValue.toObject();
|
|
|
|
|
SimulatorInfo device;
|
2017-03-15 17:04:47 +01:00
|
|
|
device.identifier = deviceObject.value(udidTag).toString();
|
|
|
|
|
device.name = deviceObject.value(nameTag).toString();
|
2017-01-24 16:30:26 +01:00
|
|
|
device.runtimeName = runtime;
|
2017-03-15 17:04:47 +01:00
|
|
|
const QString availableStr = deviceObject.value(availabilityTag).toString();
|
|
|
|
|
device.available = !availableStr.contains(unavailabilityToken);
|
|
|
|
|
device.state = deviceObject.value(stateTag).toString();
|
2017-01-24 16:30:26 +01:00
|
|
|
simulatorDevices.append(device);
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-24 16:30:26 +01:00
|
|
|
stable_sort(simulatorDevices.begin(), simulatorDevices.end());
|
2016-09-26 12:40:09 +02:00
|
|
|
} else {
|
|
|
|
|
qCDebug(simulatorLog) << "Error parsing json output from simctl. Output:" << output;
|
|
|
|
|
}
|
2017-01-24 16:30:26 +01:00
|
|
|
return simulatorDevices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QList<SimulatorInfo> getAvailableSimulators()
|
|
|
|
|
{
|
|
|
|
|
auto filterSim = [](const SimulatorInfo &device) { return device.available;};
|
|
|
|
|
QList<SimulatorInfo> availableDevices = Utils::filtered(getAllSimulatorDevices(), filterSim);
|
2016-10-27 16:45:20 +02:00
|
|
|
return availableDevices;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 15:39:01 +01:00
|
|
|
QFuture<QList<DeviceTypeInfo> > SimulatorControl::updateDeviceTypes()
|
|
|
|
|
{
|
|
|
|
|
QFuture< QList<DeviceTypeInfo> > future = Utils::runAsync(getAvailableDeviceTypes);
|
|
|
|
|
Utils::onResultReady(future, [](const QList<DeviceTypeInfo> &deviceTypes) {
|
|
|
|
|
SimulatorControlPrivate::availableDeviceTypes = deviceTypes;
|
|
|
|
|
});
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<RuntimeInfo> SimulatorControl::availableRuntimes()
|
|
|
|
|
{
|
|
|
|
|
return SimulatorControlPrivate::availableRuntimes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture<QList<RuntimeInfo> > SimulatorControl::updateRuntimes()
|
|
|
|
|
{
|
|
|
|
|
QFuture< QList<RuntimeInfo> > future = Utils::runAsync(getAvailableRuntimes);
|
|
|
|
|
Utils::onResultReady(future, [](const QList<RuntimeInfo> &runtimes) {
|
|
|
|
|
SimulatorControlPrivate::availableRuntimes = runtimes;
|
|
|
|
|
});
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture< QList<SimulatorInfo> > SimulatorControl::updateAvailableSimulators()
|
2016-10-27 16:45:20 +02:00
|
|
|
{
|
2017-01-24 16:30:26 +01:00
|
|
|
QFuture< QList<SimulatorInfo> > future = Utils::runAsync(getAvailableSimulators);
|
|
|
|
|
Utils::onResultReady(future, [](const QList<SimulatorInfo> &devices) {
|
2016-11-04 17:39:39 +01:00
|
|
|
SimulatorControlPrivate::availableDevices = devices;
|
2016-10-27 16:45:20 +02:00
|
|
|
});
|
2017-01-23 15:39:01 +01:00
|
|
|
return future;
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
bool SimulatorControl::isSimulatorRunning(const QString &simUdid)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
if (simUdid.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
return SimulatorControlPrivate::deviceInfo(simUdid).isBooted();
|
|
|
|
|
}
|
2016-09-26 12:40:09 +02:00
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
QString SimulatorControl::bundleIdentifier(const Utils::FileName &bundlePath)
|
|
|
|
|
{
|
|
|
|
|
return SimulatorControlPrivate::bundleIdentifier(bundlePath);
|
|
|
|
|
}
|
2016-09-26 12:40:09 +02:00
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
QString SimulatorControl::bundleExecutable(const Utils::FileName &bundlePath)
|
|
|
|
|
{
|
|
|
|
|
return SimulatorControlPrivate::bundleExecutable(bundlePath);
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-12-19 18:41:00 +01:00
|
|
|
QFuture<SimulatorControl::ResponseData> SimulatorControl::startSimulator(const QString &simUdid) const
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::startSimulator, d, simUdid);
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
QFuture<SimulatorControl::ResponseData>
|
|
|
|
|
SimulatorControl::installApp(const QString &simUdid, const Utils::FileName &bundlePath) const
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::installApp, d, simUdid, bundlePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture<SimulatorControl::ResponseData>
|
|
|
|
|
SimulatorControl::launchApp(const QString &simUdid, const QString &bundleIdentifier,
|
2016-12-21 18:28:42 +01:00
|
|
|
bool waitForDebugger, const QStringList &extraArgs,
|
|
|
|
|
const QString &stdoutPath, const QString &stderrPath) const
|
2016-11-04 17:39:39 +01:00
|
|
|
{
|
2016-12-19 18:41:00 +01:00
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::launchApp, d, simUdid, bundleIdentifier,
|
2016-12-21 18:28:42 +01:00
|
|
|
waitForDebugger, extraArgs, stdoutPath, stderrPath);
|
2016-11-04 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-23 15:39:01 +01:00
|
|
|
QFuture<SimulatorControl::ResponseData> SimulatorControl::deleteSimulator(const QString &simUdid) const
|
|
|
|
|
{
|
|
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::deleteSimulator, d, simUdid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture<SimulatorControl::ResponseData> SimulatorControl::resetSimulator(const QString &simUdid) const
|
|
|
|
|
{
|
|
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::resetSimulator, d, simUdid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture<SimulatorControl::ResponseData> SimulatorControl::renameSimulator(const QString &simUdid,
|
|
|
|
|
const QString &newName) const
|
|
|
|
|
{
|
|
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::renameSimulator, d, simUdid, newName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture<SimulatorControl::ResponseData>
|
|
|
|
|
SimulatorControl::createSimulator(const QString &name,
|
|
|
|
|
const DeviceTypeInfo &deviceType,
|
|
|
|
|
const RuntimeInfo &runtime)
|
|
|
|
|
{
|
|
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::createSimulator, d, name, deviceType, runtime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture<SimulatorControl::ResponseData> SimulatorControl::takeSceenshot(const QString &simUdid,
|
|
|
|
|
const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
return Utils::runAsync(&SimulatorControlPrivate::takeSceenshot, d, simUdid, filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Static members
|
2017-01-24 16:30:26 +01:00
|
|
|
QList<SimulatorInfo> SimulatorControlPrivate::availableDevices;
|
2017-01-23 15:39:01 +01:00
|
|
|
QList<DeviceTypeInfo> SimulatorControlPrivate::availableDeviceTypes;
|
|
|
|
|
QList<RuntimeInfo> SimulatorControlPrivate::availableRuntimes;
|
2016-11-04 17:39:39 +01:00
|
|
|
|
2018-11-12 19:55:59 +01:00
|
|
|
SimulatorControlPrivate::SimulatorControlPrivate() = default;
|
2016-11-04 17:39:39 +01:00
|
|
|
|
2018-11-12 19:55:59 +01:00
|
|
|
SimulatorControlPrivate::~SimulatorControlPrivate() = default;
|
2016-09-26 12:40:09 +02:00
|
|
|
|
2017-01-24 16:30:26 +01:00
|
|
|
SimulatorInfo SimulatorControlPrivate::deviceInfo(const QString &simUdid)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2017-01-24 16:30:26 +01:00
|
|
|
auto matchDevice = [simUdid](const SimulatorInfo &device) {
|
|
|
|
|
return device.identifier == simUdid;
|
|
|
|
|
};
|
|
|
|
|
SimulatorInfo device = Utils::findOrDefault(getAllSimulatorDevices(), matchDevice);
|
|
|
|
|
if (device.identifier.isEmpty())
|
2016-11-04 17:39:39 +01:00
|
|
|
qCDebug(simulatorLog) << "Cannot find device info. Invalid UDID.";
|
2017-01-24 16:30:26 +01:00
|
|
|
|
|
|
|
|
return device;
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
QString SimulatorControlPrivate::bundleIdentifier(const Utils::FileName &bundlePath)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
|
|
|
|
QString bundleID;
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
|
if (bundlePath.exists()) {
|
|
|
|
|
CFStringRef cFBundlePath = bundlePath.toString().toCFString();
|
|
|
|
|
CFURLRef bundle_url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, cFBundlePath, kCFURLPOSIXPathStyle, true);
|
|
|
|
|
CFRelease(cFBundlePath);
|
|
|
|
|
CFBundleRef bundle = CFBundleCreate (kCFAllocatorDefault, bundle_url);
|
|
|
|
|
CFRelease(bundle_url);
|
|
|
|
|
CFStringRef cFBundleID = CFBundleGetIdentifier(bundle);
|
|
|
|
|
bundleID = QString::fromCFString(cFBundleID).trimmed();
|
|
|
|
|
CFRelease(bundle);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
Q_UNUSED(bundlePath)
|
|
|
|
|
#endif
|
|
|
|
|
return bundleID;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
QString SimulatorControlPrivate::bundleExecutable(const Utils::FileName &bundlePath)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
|
|
|
|
QString executable;
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
|
if (bundlePath.exists()) {
|
|
|
|
|
CFStringRef cFBundlePath = bundlePath.toString().toCFString();
|
|
|
|
|
CFURLRef bundle_url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, cFBundlePath, kCFURLPOSIXPathStyle, true);
|
|
|
|
|
CFRelease(cFBundlePath);
|
|
|
|
|
CFBundleRef bundle = CFBundleCreate (kCFAllocatorDefault, bundle_url);
|
|
|
|
|
CFStringRef cFStrExecutableName = (CFStringRef)CFBundleGetValueForInfoDictionaryKey(bundle, kCFBundleExecutableKey);
|
|
|
|
|
executable = QString::fromCFString(cFStrExecutableName).trimmed();
|
|
|
|
|
CFRelease(bundle);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
Q_UNUSED(bundlePath)
|
|
|
|
|
#endif
|
|
|
|
|
return executable;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
void SimulatorControlPrivate::startSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
SimulatorControl::ResponseData response(simUdid);
|
2017-01-26 13:59:09 +01:00
|
|
|
SimulatorInfo simInfo = deviceInfo(simUdid);
|
2017-04-28 08:27:10 +02:00
|
|
|
|
|
|
|
|
if (!simInfo.available) {
|
|
|
|
|
qCDebug(simulatorLog) << "Simulator device is not available." << simUdid;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shutting down state checks are for the case when simulator start is called within a short
|
|
|
|
|
// interval of closing the previous interval of the simulator. We wait untill the shutdown
|
|
|
|
|
// process is complete.
|
|
|
|
|
auto start = chrono::high_resolution_clock::now();
|
2017-03-15 17:04:47 +01:00
|
|
|
while (simInfo.isShuttingDown() && !checkForTimeout(start, simulatorStartTimeout)) {
|
2017-04-28 08:27:10 +02:00
|
|
|
// Wait till the simulator shuts down, if doing so.
|
|
|
|
|
QThread::currentThread()->msleep(100);
|
|
|
|
|
simInfo = deviceInfo(simUdid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (simInfo.isShuttingDown()) {
|
2018-10-22 16:34:31 +02:00
|
|
|
qCDebug(simulatorLog) << "Cannot start Simulator device. "
|
2017-04-28 08:27:10 +02:00
|
|
|
<< "Previous instance taking too long to shutdown." << simInfo;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (simInfo.isShutdown()) {
|
2017-11-14 12:48:45 +01:00
|
|
|
if (launchSimulator(simUdid)) {
|
2016-11-04 17:39:39 +01:00
|
|
|
if (fi.isCanceled())
|
|
|
|
|
return;
|
|
|
|
|
// At this point the sim device exists, available and was not running.
|
|
|
|
|
// So the simulator is started and we'll wait for it to reach to a state
|
|
|
|
|
// where we can interact with it.
|
2017-04-28 08:27:10 +02:00
|
|
|
start = chrono::high_resolution_clock::now();
|
2017-01-24 16:30:26 +01:00
|
|
|
SimulatorInfo info;
|
2016-11-04 17:39:39 +01:00
|
|
|
do {
|
|
|
|
|
info = deviceInfo(simUdid);
|
|
|
|
|
if (fi.isCanceled())
|
|
|
|
|
return;
|
|
|
|
|
} while (!info.isBooted()
|
2017-03-15 17:04:47 +01:00
|
|
|
&& !checkForTimeout(start, simulatorStartTimeout));
|
2016-11-04 17:39:39 +01:00
|
|
|
if (info.isBooted()) {
|
|
|
|
|
response.success = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
qCDebug(simulatorLog) << "Error starting simulator.";
|
|
|
|
|
}
|
2017-04-28 08:27:10 +02:00
|
|
|
} else {
|
2018-10-22 16:34:31 +02:00
|
|
|
qCDebug(simulatorLog) << "Cannot start Simulator device. Simulator not in shutdown state."
|
2017-04-28 08:27:10 +02:00
|
|
|
<< simInfo;
|
2016-11-04 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fi.isCanceled()) {
|
|
|
|
|
fi.reportResult(response);
|
|
|
|
|
}
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
void SimulatorControlPrivate::installApp(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid, const Utils::FileName &bundlePath)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
QTC_CHECK(bundlePath.exists());
|
|
|
|
|
|
2017-01-23 15:13:19 +01:00
|
|
|
SimulatorControl::ResponseData response(simUdid);
|
2017-03-15 17:04:47 +01:00
|
|
|
response.success = runSimCtlCommand({"install", simUdid, bundlePath.toString()},
|
2017-01-23 15:13:19 +01:00
|
|
|
&response.commandOutput);
|
|
|
|
|
if (!fi.isCanceled())
|
2016-11-04 17:39:39 +01:00
|
|
|
fi.reportResult(response);
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-12-19 18:41:00 +01:00
|
|
|
void SimulatorControlPrivate::launchApp(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid, const QString &bundleIdentifier,
|
2016-12-21 18:28:42 +01:00
|
|
|
bool waitForDebugger, const QStringList &extraArgs,
|
|
|
|
|
const QString &stdoutPath, const QString &stderrPath)
|
2016-09-26 12:40:09 +02:00
|
|
|
{
|
2016-11-04 17:39:39 +01:00
|
|
|
SimulatorControl::ResponseData response(simUdid);
|
2016-12-19 18:41:00 +01:00
|
|
|
if (!bundleIdentifier.isEmpty() && !fi.isCanceled()) {
|
2017-03-15 17:04:47 +01:00
|
|
|
QStringList args({"launch", simUdid, bundleIdentifier});
|
2016-11-04 17:39:39 +01:00
|
|
|
|
2016-12-21 18:28:42 +01:00
|
|
|
// simctl usage documentation : Note: Log output is often directed to stderr, not stdout.
|
|
|
|
|
if (!stdoutPath.isEmpty())
|
2017-03-15 17:04:47 +01:00
|
|
|
args.insert(1, QString("--stderr=%1").arg(stdoutPath));
|
2016-12-21 18:28:42 +01:00
|
|
|
|
|
|
|
|
if (!stderrPath.isEmpty())
|
2017-03-15 17:04:47 +01:00
|
|
|
args.insert(1, QString("--stdout=%1").arg(stderrPath));
|
2016-12-21 18:28:42 +01:00
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
if (waitForDebugger)
|
2017-03-15 17:04:47 +01:00
|
|
|
args.insert(1, "-w");
|
2016-09-26 12:40:09 +02:00
|
|
|
|
2016-12-19 18:41:00 +01:00
|
|
|
foreach (const QString extraArgument, extraArgs) {
|
|
|
|
|
if (!extraArgument.trimmed().isEmpty())
|
|
|
|
|
args << extraArgument;
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-23 15:13:19 +01:00
|
|
|
if (runSimCtlCommand(args, &response.commandOutput)) {
|
2018-05-14 16:46:23 +02:00
|
|
|
const QString pIdStr = response.commandOutput.trimmed().split(' ').last().trimmed();
|
2017-01-23 15:13:19 +01:00
|
|
|
bool validPid = false;
|
|
|
|
|
response.pID = pIdStr.toLongLong(&validPid);
|
|
|
|
|
response.success = validPid;
|
|
|
|
|
}
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 17:39:39 +01:00
|
|
|
if (!fi.isCanceled()) {
|
|
|
|
|
fi.reportResult(response);
|
|
|
|
|
}
|
2016-09-26 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-23 15:39:01 +01:00
|
|
|
void SimulatorControlPrivate::deleteSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid)
|
|
|
|
|
{
|
|
|
|
|
SimulatorControl::ResponseData response(simUdid);
|
2017-03-15 17:04:47 +01:00
|
|
|
response.success = runSimCtlCommand({"delete", simUdid}, &response.commandOutput);
|
2017-01-23 15:39:01 +01:00
|
|
|
|
|
|
|
|
if (!fi.isCanceled())
|
|
|
|
|
fi.reportResult(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimulatorControlPrivate::resetSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid)
|
|
|
|
|
{
|
|
|
|
|
SimulatorControl::ResponseData response(simUdid);
|
2017-03-15 17:04:47 +01:00
|
|
|
response.success = runSimCtlCommand({"erase", simUdid}, &response.commandOutput);
|
2017-01-23 15:39:01 +01:00
|
|
|
|
|
|
|
|
if (!fi.isCanceled())
|
|
|
|
|
fi.reportResult(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimulatorControlPrivate::renameSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid, const QString &newName)
|
|
|
|
|
{
|
|
|
|
|
SimulatorControl::ResponseData response(simUdid);
|
2017-03-15 17:04:47 +01:00
|
|
|
response.success = runSimCtlCommand({"rename", simUdid, newName},
|
2017-01-23 15:39:01 +01:00
|
|
|
&response.commandOutput);
|
|
|
|
|
|
|
|
|
|
if (!fi.isCanceled())
|
|
|
|
|
fi.reportResult(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimulatorControlPrivate::createSimulator(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &name,
|
|
|
|
|
const DeviceTypeInfo &deviceType,
|
|
|
|
|
const RuntimeInfo &runtime)
|
|
|
|
|
{
|
|
|
|
|
SimulatorControl::ResponseData response("Invalid");
|
|
|
|
|
if (!name.isEmpty()) {
|
2017-03-15 17:04:47 +01:00
|
|
|
response.success = runSimCtlCommand({"create", name,
|
2017-02-22 15:09:35 +01:00
|
|
|
deviceType.identifier,
|
|
|
|
|
runtime.identifier},
|
2017-01-23 15:39:01 +01:00
|
|
|
&response.commandOutput);
|
2018-05-14 16:46:23 +02:00
|
|
|
response.simUdid = response.success ? response.commandOutput.trimmed()
|
2017-01-23 15:39:01 +01:00
|
|
|
: QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fi.isCanceled())
|
|
|
|
|
fi.reportResult(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimulatorControlPrivate::takeSceenshot(QFutureInterface<SimulatorControl::ResponseData> &fi,
|
|
|
|
|
const QString &simUdid, const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
SimulatorControl::ResponseData response(simUdid);
|
2017-03-15 17:04:47 +01:00
|
|
|
response.success = runSimCtlCommand({"io", simUdid, "screenshot", filePath},
|
2017-01-23 15:39:01 +01:00
|
|
|
&response.commandOutput);
|
|
|
|
|
if (!fi.isCanceled())
|
|
|
|
|
fi.reportResult(response);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-28 08:27:10 +02:00
|
|
|
QDebug &operator<<(QDebug &stream, const SimulatorInfo &info)
|
|
|
|
|
{
|
|
|
|
|
stream << "Name: " << info.name << "UDID: " << info.identifier
|
|
|
|
|
<< "Availability: " << info.available << "State: " << info.state
|
|
|
|
|
<< "Runtime: " << info.runtimeName;
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 11:00:20 +01:00
|
|
|
bool SimulatorInfo::operator==(const SimulatorInfo &other) const
|
|
|
|
|
{
|
|
|
|
|
return identifier == other.identifier
|
|
|
|
|
&& state == other.state
|
|
|
|
|
&& name == other.name
|
|
|
|
|
&& available == other.available
|
|
|
|
|
&& runtimeName == other.runtimeName;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-26 12:40:09 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Ios
|