Commit b2a33d31 authored by Vikas Pachdha's avatar Vikas Pachdha
Browse files

iOS: Extend the information cached for simulator



Change-Id: Ia2bc421b2af7d1e82d48bbd9d7914f177fef30d0
Reviewed-by: Eike Ziller's avatarEike Ziller <eike.ziller@qt.io>
parent c888989a
......@@ -42,6 +42,7 @@
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/qtkitinformation.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/qtcprocess.h>
#include <utils/qtcassert.h>
......@@ -67,6 +68,14 @@ namespace Internal {
static const QLatin1String deviceTypeKey("Ios.device_type");
static IosDeviceType toIosDeviceType(const SimulatorInfo &device)
{
IosDeviceType iosDeviceType(IosDeviceType::SimulatedDevice,
device.identifier,
QString("%1, %2").arg(device.name).arg(device.runtimeName));
return iosDeviceType;
}
class IosRunConfigurationWidget : public RunConfigWidget
{
public:
......@@ -350,22 +359,24 @@ QString IosRunConfiguration::disabledReason() const
IosDeviceType IosRunConfiguration::deviceType() const
{
QList<IosDeviceType> availableSimulators;
if (m_deviceType.type == IosDeviceType::SimulatedDevice)
availableSimulators = SimulatorControl::availableSimulators();
if (!availableSimulators.isEmpty()) {
QList<IosDeviceType> elegibleDevices;
QString devname = m_deviceType.identifier.split(QLatin1Char(',')).value(0);
foreach (const IosDeviceType &dType, availableSimulators) {
if (dType == m_deviceType)
return m_deviceType;
if (!devname.isEmpty() && dType.identifier.startsWith(devname)
&& dType.identifier.split(QLatin1Char(',')).value(0) == devname)
elegibleDevices << dType;
if (m_deviceType.type == IosDeviceType::SimulatedDevice) {
QList<SimulatorInfo> availableSimulators = SimulatorControl::availableSimulators();
if (availableSimulators.isEmpty())
return m_deviceType;
if (Utils::contains(availableSimulators,
Utils::equal(&SimulatorInfo::identifier, m_deviceType.identifier))) {
return m_deviceType;
}
if (!elegibleDevices.isEmpty())
return elegibleDevices.last();
return availableSimulators.last();
const QStringList parts = m_deviceType.displayName.split(QLatin1Char(','));
if (parts.count() < 2)
return toIosDeviceType(availableSimulators.last());
QList<SimulatorInfo> eligibleDevices;
eligibleDevices = Utils::filtered(availableSimulators, [parts](const SimulatorInfo &info) {
return info.name == parts.at(0) && info.runtimeName == parts.at(1);
});
return toIosDeviceType(eligibleDevices.isEmpty() ? availableSimulators.last()
: eligibleDevices.last());
}
return m_deviceType;
}
......@@ -413,7 +424,7 @@ void IosRunConfigurationWidget::setDeviceTypeIndex(int devIndex)
{
QVariant selectedDev = m_deviceTypeModel.data(m_deviceTypeModel.index(devIndex, 0), Qt::UserRole + 1);
if (selectedDev.isValid())
m_runConfiguration->setDeviceType(selectedDev.value<IosDeviceType>());
m_runConfiguration->setDeviceType(toIosDeviceType(selectedDev.value<SimulatorInfo>()));
}
......@@ -423,25 +434,27 @@ void IosRunConfigurationWidget::updateValues()
m_deviceTypeLabel->setVisible(showDeviceSelector);
m_deviceTypeComboBox->setVisible(showDeviceSelector);
if (showDeviceSelector && m_deviceTypeModel.rowCount() == 0) {
foreach (const IosDeviceType &dType, SimulatorControl::availableSimulators()) {
QStandardItem *item = new QStandardItem(dType.displayName);
foreach (const SimulatorInfo &device, SimulatorControl::availableSimulators()) {
QStandardItem *item = new QStandardItem(QString("%1, %2").arg(device.name)
.arg(device.runtimeName));
QVariant v;
v.setValue(dType);
v.setValue(device);
item->setData(v);
m_deviceTypeModel.appendRow(item);
}
}
IosDeviceType currentDType = m_runConfiguration->deviceType();
QVariant currentData = m_deviceTypeComboBox->currentData();
if (currentDType.type == IosDeviceType::SimulatedDevice && !currentDType.identifier.isEmpty()
&& (!m_deviceTypeComboBox->currentData().isValid()
|| currentDType != m_deviceTypeComboBox->currentData().value<IosDeviceType>()))
&& (!currentData.isValid()
|| currentDType != toIosDeviceType(currentData.value<SimulatorInfo>())))
{
bool didSet = false;
for (int i = 0; m_deviceTypeModel.hasIndex(i, 0); ++i) {
QVariant vData = m_deviceTypeModel.data(m_deviceTypeModel.index(i, 0), Qt::UserRole + 1);
IosDeviceType dType = vData.value<IosDeviceType>();
if (dType == currentDType) {
SimulatorInfo dType = vData.value<SimulatorInfo>();
if (dType.identifier == currentDType.identifier) {
m_deviceTypeComboBox->setCurrentIndex(i);
didSet = true;
break;
......
......@@ -24,9 +24,9 @@
****************************************************************************/
#include "simulatorcontrol.h"
#include "iossimulator.h"
#include "iosconfigurations.h"
#include "utils/algorithm.h"
#include "utils/runextensions.h"
#include "utils/qtcassert.h"
#include "utils/synchronousprocess.h"
......@@ -83,20 +83,10 @@ static bool runSimCtlCommand(QStringList args, QByteArray *output)
class SimulatorControlPrivate {
private:
struct SimDeviceInfo {
bool isBooted() const { return state.compare(QStringLiteral("Booted")) == 0; }
bool isAvailable() const { return !availability.contains(QStringLiteral("unavailable")); }
QString name;
QString udid;
QString availability;
QString state;
QString sdk;
};
SimulatorControlPrivate();
~SimulatorControlPrivate();
static SimDeviceInfo deviceInfo(const QString &simUdid);
static SimulatorInfo deviceInfo(const QString &simUdid);
static QString bundleIdentifier(const Utils::FileName &bundlePath);
static QString bundleExecutable(const Utils::FileName &bundlePath);
......@@ -108,7 +98,7 @@ private:
const QStringList &extraArgs, const QString &stdoutPath,
const QString &stderrPath);
static QList<IosDeviceType> availableDevices;
static QList<SimulatorInfo> availableDevices;
friend class SimulatorControl;
};
......@@ -123,44 +113,51 @@ SimulatorControl::~SimulatorControl()
delete d;
}
QList<Ios::Internal::IosDeviceType> SimulatorControl::availableSimulators()
QList<SimulatorInfo> SimulatorControl::availableSimulators()
{
return SimulatorControlPrivate::availableDevices;
}
static QList<IosDeviceType> getAvailableSimulators()
static QList<SimulatorInfo> getAllSimulatorDevices()
{
QList<IosDeviceType> availableDevices;
QList<SimulatorInfo> simulatorDevices;
QByteArray output;
runSimCtlCommand({QLatin1String("list"), QLatin1String("-j"), QLatin1String("devices")}, &output);
QJsonDocument doc = QJsonDocument::fromJson(output);
if (!doc.isNull()) {
const QJsonObject buildInfo = doc.object().value("devices").toObject();
foreach (const QString &buildVersion, buildInfo.keys()) {
QJsonArray devices = buildInfo.value(buildVersion).toArray();
foreach (const QJsonValue device, devices) {
QJsonObject deviceInfo = device.toObject();
QString deviceName = QString("%1, %2")
.arg(deviceInfo.value("name").toString("Unknown"))
.arg(buildVersion);
QString deviceUdid = deviceInfo.value("udid").toString("Unknown");
if (!deviceInfo.value("availability").toString().contains("unavailable")) {
IosDeviceType iOSDevice(IosDeviceType::SimulatedDevice, deviceUdid, deviceName);
availableDevices.append(iOSDevice);
}
const QJsonObject runtimeObject = doc.object().value(QStringLiteral("devices")).toObject();
foreach (const QString &runtime, runtimeObject.keys()) {
const QJsonArray devices = runtimeObject.value(runtime).toArray();
foreach (const QJsonValue deviceValue, devices) {
QJsonObject deviceObject = deviceValue.toObject();
SimulatorInfo device;
device.identifier = deviceObject.value(QStringLiteral("udid")).toString();
device.name = deviceObject.value(QStringLiteral("name")).toString();
device.runtimeName = runtime;
const QString availableStr = deviceObject.value(QStringLiteral("availability")).toString();
device.available = !availableStr.contains("unavailable");
device.state = deviceObject.value(QStringLiteral("state")).toString();
simulatorDevices.append(device);
}
}
stable_sort(availableDevices.begin(), availableDevices.end());
stable_sort(simulatorDevices.begin(), simulatorDevices.end());
} else {
qCDebug(simulatorLog) << "Error parsing json output from simctl. Output:" << output;
}
return simulatorDevices;
}
static QList<SimulatorInfo> getAvailableSimulators()
{
auto filterSim = [](const SimulatorInfo &device) { return device.available;};
QList<SimulatorInfo> availableDevices = Utils::filtered(getAllSimulatorDevices(), filterSim);
return availableDevices;
}
void SimulatorControl::updateAvailableSimulators()
{
QFuture< QList<IosDeviceType> > future = Utils::runAsync(getAvailableSimulators);
Utils::onResultReady(future, [](const QList<IosDeviceType> &devices) {
QFuture< QList<SimulatorInfo> > future = Utils::runAsync(getAvailableSimulators);
Utils::onResultReady(future, [](const QList<SimulatorInfo> &devices) {
SimulatorControlPrivate::availableDevices = devices;
});
}
......@@ -202,7 +199,7 @@ SimulatorControl::launchApp(const QString &simUdid, const QString &bundleIdentif
waitForDebugger, extraArgs, stdoutPath, stderrPath);
}
QList<IosDeviceType> SimulatorControlPrivate::availableDevices;
QList<SimulatorInfo> SimulatorControlPrivate::availableDevices;
SimulatorControlPrivate::SimulatorControlPrivate()
{
......@@ -213,41 +210,16 @@ SimulatorControlPrivate::~SimulatorControlPrivate()
}
SimulatorControlPrivate::SimDeviceInfo SimulatorControlPrivate::deviceInfo(const QString &simUdid)
SimulatorInfo SimulatorControlPrivate::deviceInfo(const QString &simUdid)
{
SimDeviceInfo info;
bool found = false;
if (!simUdid.isEmpty()) {
QByteArray output;
runSimCtlCommand({QLatin1String("list"), QLatin1String("-j"), QLatin1String("devices")}, &output);
QJsonDocument doc = QJsonDocument::fromJson(output);
if (!doc.isNull()) {
const QJsonObject buildInfo = doc.object().value(QStringLiteral("devices")).toObject();
foreach (const QString &buildVersion, buildInfo.keys()) {
QJsonArray devices = buildInfo.value(buildVersion).toArray();
foreach (const QJsonValue device, devices) {
QJsonObject deviceInfo = device.toObject();
QString deviceUdid = deviceInfo.value(QStringLiteral("udid")).toString();
if (deviceUdid.compare(simUdid) == 0) {
found = true;
info.name = deviceInfo.value(QStringLiteral("name")).toString();
info.udid = deviceUdid;
info.state = deviceInfo.value(QStringLiteral("state")).toString();
info.sdk = buildVersion;
info.availability = deviceInfo.value(QStringLiteral("availability")).toString();
break;
}
}
if (found)
break;
}
} else {
qCDebug(simulatorLog) << "Cannot find device info. Error parsing json output from simctl. Output:" << output;
}
} else {
auto matchDevice = [simUdid](const SimulatorInfo &device) {
return device.identifier == simUdid;
};
SimulatorInfo device = Utils::findOrDefault(getAllSimulatorDevices(), matchDevice);
if (device.identifier.isEmpty())
qCDebug(simulatorLog) << "Cannot find device info. Invalid UDID.";
}
return info;
return device;
}
QString SimulatorControlPrivate::bundleIdentifier(const Utils::FileName &bundlePath)
......@@ -293,7 +265,7 @@ void SimulatorControlPrivate::startSimulator(QFutureInterface<SimulatorControl::
const QString &simUdid)
{
SimulatorControl::ResponseData response(simUdid);
if (deviceInfo(simUdid).isAvailable()) {
if (deviceInfo(simUdid).available) {
// Simulator is available.
const QString cmd = IosConfigurations::developerPath()
.appendPath(QStringLiteral("/Applications/Simulator.app/Contents/MacOS/Simulator"))
......@@ -307,7 +279,7 @@ void SimulatorControlPrivate::startSimulator(QFutureInterface<SimulatorControl::
// So the simulator is started and we'll wait for it to reach to a state
// where we can interact with it.
auto start = chrono::high_resolution_clock::now();
SimulatorControlPrivate::SimDeviceInfo info;
SimulatorInfo info;
do {
info = deviceInfo(simUdid);
if (fi.isCanceled())
......
......@@ -37,9 +37,22 @@ QT_END_NAMESPACE
namespace Ios {
namespace Internal {
class IosDeviceType;
class SimulatorControlPrivate;
class SimulatorInfo {
public:
bool isBooted() const { return state.compare(QStringLiteral("Booted")) == 0; }
bool available;
QString state;
QString runtimeName;
QString name;
QString identifier;
bool operator <(const SimulatorInfo &o) const
{
return name < o.name;
}
};
class SimulatorControl : public QObject
{
Q_OBJECT
......@@ -59,7 +72,7 @@ public:
~SimulatorControl();
public:
static QList<IosDeviceType> availableSimulators();
static QList<SimulatorInfo> availableSimulators();
static void updateAvailableSimulators();
static bool isSimulatorRunning(const QString &simUdid);
static QString bundleIdentifier(const Utils::FileName &bundlePath);
......@@ -78,3 +91,5 @@ private:
};
} // namespace Internal
} // namespace Ios
Q_DECLARE_METATYPE(Ios::Internal::SimulatorInfo)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment