aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/ios/simulatorcontrol.cpp
blob: 4def4c0e471170f64239222dd7fb72e5f8156eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "simulatorcontrol.h"
#include "iosconfigurations.h"
#include "iostr.h"

#include <utils/algorithm.h>
#include <utils/async.h>
#include <utils/qtcprocess.h>
#include <utils/qtcassert.h>

#ifdef Q_OS_MAC
#include <CoreFoundation/CoreFoundation.h>
#endif

#include <memory>

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLoggingCategory>

using namespace Utils;

using namespace std;
using namespace std::chrono;

namespace {
static Q_LOGGING_CATEGORY(simulatorLog, "qtc.ios.simulator", QtWarningMsg)
}

namespace Ios::Internal {

const seconds simulatorStartTimeout = seconds(60);

// simctl Json Tags and tokens.
const char deviceTypeTag[] = "devicetypes";
const char devicesTag[] = "devices";
const char availabilityTag[] = "availability";
const char unavailabilityToken[] = "unavailable";
const char availabilityTagNew[] = "isAvailable"; // at least since Xcode 10
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";

static expected_str<void> runCommand(
    const CommandLine &command,
    QString *stdOutput,
    QString *allOutput = nullptr,
    std::function<bool()> shouldStop = [] { return false; })
{
    Process p;
    p.setCommand(command);
    p.start();

    if (!p.waitForStarted())
        return make_unexpected(Tr::tr("Failed to start process."));

    forever {
        if (shouldStop() || p.waitForFinished(seconds(1)))
            break;
    }

    if (p.state() != QProcess::ProcessState::NotRunning) {
        p.kill();
        if (shouldStop())
            return make_unexpected(Tr::tr("Process was canceled."));

        return make_unexpected(Tr::tr("Process was forced to exit."));
    }

    if (stdOutput)
        *stdOutput = p.cleanedStdOut();
    if (allOutput)
        *allOutput = p.allOutput();

    if (p.result() != ProcessResult::FinishedWithSuccess)
        return make_unexpected(p.errorString());

    return {};
}

static expected_str<void> runSimCtlCommand(
    QStringList args,
    QString *output,
    QString *allOutput = nullptr,
    std::function<bool()> shouldStop = [] { return false; })
{
    args.prepend("simctl");

    // Cache xcrun's path, as this function will be called often.
    static FilePath xcrun = FilePath::fromString("xcrun").searchInPath();
    if (xcrun.isEmpty())
        return make_unexpected(Tr::tr("Cannot find xcrun."));
    else if (!xcrun.isExecutableFile())
        return make_unexpected(Tr::tr("xcrun is not executable."));
    return runCommand({xcrun, args}, output, allOutput, shouldStop);
}

static expected_str<void> launchSimulator(const QString &simUdid, std::function<bool()> shouldStop)
{
    QTC_ASSERT(!simUdid.isEmpty(), return make_unexpected(Tr::tr("Invalid Empty UDID.")));
    const FilePath simulatorAppPath = IosConfigurations::developerPath()
            .pathAppended("Applications/Simulator.app/Contents/MacOS/Simulator");

    if (IosConfigurations::xcodeVersion() >= QVersionNumber(9)) {
        // For XCode 9 boot the second device instead of launching simulator app twice.
        QString psOutput;
        expected_str<void> result
            = runCommand({"ps", {"-A", "-o", "comm"}}, &psOutput, nullptr, shouldStop);
        if (!result)
            return result;

        for (const QString &comm : psOutput.split('\n')) {
            if (comm == simulatorAppPath.toString())
                return runSimCtlCommand({"boot", simUdid}, nullptr, nullptr, shouldStop);
        }
    }
    const bool started = Process::startDetached(
        {simulatorAppPath, {"--args", "-CurrentDeviceUDID", simUdid}});
    if (!started)
        return make_unexpected(Tr::tr("Failed to start simulator app."));
    return {};
}

static bool isAvailable(const QJsonObject &object)
{
    return object.contains(availabilityTagNew)
               ? object.value(availabilityTagNew).toBool()
               : !object.value(availabilityTag).toString().contains(unavailabilityToken);
}

static QList<DeviceTypeInfo> getAvailableDeviceTypes()
{
    QList<DeviceTypeInfo> deviceTypes;
    QString output;
    runSimCtlCommand({"list", "-j", deviceTypeTag}, &output);
    QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
    if (!doc.isNull()) {
        const QJsonArray runtimesArray = doc.object().value(deviceTypeTag).toArray();
        for (const QJsonValue deviceTypeValue : runtimesArray) {
            QJsonObject deviceTypeObject = deviceTypeValue.toObject();
            if (isAvailable(deviceTypeObject)) {
                DeviceTypeInfo deviceType;
                deviceType.name = deviceTypeObject.value(nameTag).toString("unknown");
                deviceType.identifier = deviceTypeObject.value(identifierTag).toString("unknown");
                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;
    QString output;
    runSimCtlCommand({"list", "-j", runtimesTag}, &output);
    QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
    if (!doc.isNull()) {
        const QJsonArray runtimesArray = doc.object().value(runtimesTag).toArray();
        for (const QJsonValue runtimeValue : runtimesArray) {
            QJsonObject runtimeObject = runtimeValue.toObject();
            if (isAvailable(runtimeObject)) {
                RuntimeInfo runtime;
                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");
                runtimes.append(runtime);
            }
        }
        stable_sort(runtimes.begin(), runtimes.end());
    } else {
        qCDebug(simulatorLog) << "Error parsing json output from simctl. Output:" << output;
    }
    return runtimes;
}

static SimulatorInfo deviceInfo(const QString &simUdid);
static QString bundleIdentifier(const Utils::FilePath &bundlePath);
static QString bundleExecutable(const Utils::FilePath &bundlePath);

static void startSimulator(QPromise<SimulatorControl::Response> &promise, const QString &simUdid);
static void installApp(QPromise<SimulatorControl::Response> &promise,
                       const QString &simUdid,
                       const Utils::FilePath &bundlePath);
static void launchApp(QPromise<SimulatorControl::Response> &promise,
                      const QString &simUdid,
                      const QString &bundleIdentifier,
                      bool waitForDebugger,
                      const QStringList &extraArgs,
                      const QString &stdoutPath,
                      const QString &stderrPath);
static void deleteSimulator(QPromise<SimulatorControl::Response> &promise, const QString &simUdid);
static void resetSimulator(QPromise<SimulatorControl::Response> &promise, const QString &simUdid);
static void renameSimulator(QPromise<SimulatorControl::Response> &promise,
                            const QString &simUdid,
                            const QString &newName);
static void createSimulator(QPromise<SimulatorControl::Response> &promise,
                            const QString &name,
                            const DeviceTypeInfo &deviceType,
                            const RuntimeInfo &runtime);
static void takeSceenshot(QPromise<SimulatorControl::Response> &promise,
                          const QString &simUdid,
                          const QString &filePath);

static QList<SimulatorInfo> s_availableDevices;

QList<SimulatorInfo> SimulatorControl::availableSimulators()
{
    return s_availableDevices;
}

static QList<SimulatorInfo> getAllSimulatorDevices()
{
    QList<SimulatorInfo> simulatorDevices;
    QString output;
    runSimCtlCommand({"list", "-j", devicesTag}, &output);
    QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
    if (!doc.isNull()) {
        const QJsonObject runtimeObject = doc.object().value(devicesTag).toObject();
        const QStringList keys = runtimeObject.keys();
        for (const QString &runtime : keys) {
            const QJsonArray devices = runtimeObject.value(runtime).toArray();
            for (const QJsonValue deviceValue : devices) {
                QJsonObject deviceObject = deviceValue.toObject();
                SimulatorInfo device;
                device.identifier = deviceObject.value(udidTag).toString();
                device.name = deviceObject.value(nameTag).toString();
                device.runtimeName = runtime;
                device.available = isAvailable(deviceObject);
                device.state = deviceObject.value(stateTag).toString();
                simulatorDevices.append(device);
            }
        }
        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;
}

QFuture<QList<DeviceTypeInfo>> SimulatorControl::updateDeviceTypes()
{
    return Utils::asyncRun(getAvailableDeviceTypes);
}

QFuture<QList<RuntimeInfo>> SimulatorControl::updateRuntimes()
{
    return Utils::asyncRun(getAvailableRuntimes);
}

QFuture<QList<SimulatorInfo>> SimulatorControl::updateAvailableSimulators(QObject *context)
{
    QFuture<QList<SimulatorInfo>> future = Utils::asyncRun(getAvailableSimulators);
    Utils::onResultReady(future, context, [](const QList<SimulatorInfo> &devices) {
        s_availableDevices = devices;
    });
    return future;
}

bool SimulatorControl::isSimulatorRunning(const QString &simUdid)
{
    if (simUdid.isEmpty())
        return false;
    return deviceInfo(simUdid).isBooted();
}

QString SimulatorControl::bundleIdentifier(const Utils::FilePath &bundlePath)
{
    return Internal::bundleIdentifier(bundlePath);
}

QString SimulatorControl::bundleExecutable(const Utils::FilePath &bundlePath)
{
    return Internal::bundleExecutable(bundlePath);
}

QFuture<SimulatorControl::Response> SimulatorControl::startSimulator(const QString &simUdid)
{
    return Utils::asyncRun(Internal::startSimulator, simUdid);
}

QFuture<SimulatorControl::Response> SimulatorControl::installApp(const QString &simUdid,
                                                                 const Utils::FilePath &bundlePath)
{
    return Utils::asyncRun(Internal::installApp, simUdid, bundlePath);
}

QFuture<SimulatorControl::Response> SimulatorControl::launchApp(const QString &simUdid,
                                                                const QString &bundleIdentifier,
                                                                bool waitForDebugger,
                                                                const QStringList &extraArgs,
                                                                const QString &stdoutPath,
                                                                const QString &stderrPath)
{
    return Utils::asyncRun(Internal::launchApp,
                           simUdid,
                           bundleIdentifier,
                           waitForDebugger,
                           extraArgs,
                           stdoutPath,
                           stderrPath);
}

QFuture<SimulatorControl::Response> SimulatorControl::deleteSimulator(const QString &simUdid)
{
    return Utils::asyncRun(Internal::deleteSimulator, simUdid);
}

QFuture<SimulatorControl::Response> SimulatorControl::resetSimulator(const QString &simUdid)
{
    return Utils::asyncRun(Internal::resetSimulator, simUdid);
}

QFuture<SimulatorControl::Response> SimulatorControl::renameSimulator(const QString &simUdid,
                                                                      const QString &newName)
{
    return Utils::asyncRun(Internal::renameSimulator, simUdid, newName);
}

QFuture<SimulatorControl::Response> SimulatorControl::createSimulator(
    const QString &name, const DeviceTypeInfo &deviceType, const RuntimeInfo &runtime)
{
    return Utils::asyncRun(Internal::createSimulator, name, deviceType, runtime);
}

QFuture<SimulatorControl::Response> SimulatorControl::takeSceenshot(const QString &simUdid,
                                                                    const QString &filePath)
{
    return Utils::asyncRun(Internal::takeSceenshot, simUdid, filePath);
}

// Static members

SimulatorInfo deviceInfo(const QString &simUdid)
{
    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 device;
}

QString bundleIdentifier(const Utils::FilePath &bundlePath)
{
    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;
}

QString bundleExecutable(const Utils::FilePath &bundlePath)
{
    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;
}

void startSimulator(QPromise<SimulatorControl::Response> &promise, const QString &simUdid)
{
    SimulatorControl::ResponseData response(simUdid);
    SimulatorInfo simInfo = deviceInfo(simUdid);

    if (!simInfo.available) {
        promise.addResult(
            make_unexpected(Tr::tr("Simulator device is not available. (%1)").arg(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.
    QDeadlineTimer simulatorStartDeadline(simulatorStartTimeout);
    while (simInfo.isShuttingDown() && !simulatorStartDeadline.hasExpired()) {
        // Wait till the simulator shuts down, if doing so.
        if (promise.isCanceled()) {
            promise.addResult(make_unexpected(Tr::tr("Simulator start was canceled.")));
            return;
        }

        QThread::msleep(100);
        simInfo = deviceInfo(simUdid);
    }

    if (simInfo.isShuttingDown()) {
        promise.addResult(
            make_unexpected(Tr::tr("Cannot start Simulator device. Previous instance taking "
                                   "too long to shut down. (%1)")
                                .arg(simInfo.toString())));
        return;
    }

    if (!simInfo.isShutdown()) {
        promise.addResult(make_unexpected(
            Tr::tr("Cannot start Simulator device. Simulator not in shutdown state. (%1)")
                .arg(simInfo.toString())));
        return;
    }

    expected_str<void> result = launchSimulator(simUdid,
                                                [&promise] { return promise.isCanceled(); });
    if (!result) {
        promise.addResult(make_unexpected(result.error()));
        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.
    // We restart the deadline to give it some more time.
    simulatorStartDeadline = QDeadlineTimer(simulatorStartTimeout);
    SimulatorInfo info;
    do {
        info = deviceInfo(simUdid);
        if (promise.isCanceled()) {
            promise.addResult(make_unexpected(Tr::tr("Simulator start was canceled.")));
            return;
        }
    } while (!info.isBooted() && !simulatorStartDeadline.hasExpired());

    if (!info.isBooted()) {
        promise.addResult(make_unexpected(
            Tr::tr("Cannot start Simulator device. Simulator not in booted state. (%1)")
                .arg(info.toString())));
        return;
    }

    promise.addResult(response);
}

void installApp(QPromise<SimulatorControl::Response> &promise,
                const QString &simUdid,
                const Utils::FilePath &bundlePath)
{
    SimulatorControl::ResponseData response(simUdid);

    if (!bundlePath.exists()) {
        promise.addResult(make_unexpected(Tr::tr("Bundle path does not exist.")));
        return;
    }

    expected_str<void> result = runSimCtlCommand({"install", simUdid, bundlePath.toString()},
                                                 nullptr,
                                                 &response.commandOutput,
                                                 [&promise] { return promise.isCanceled(); });
    if (!result)
        promise.addResult(make_unexpected(result.error()));
    else
        promise.addResult(response);
}

void launchApp(QPromise<SimulatorControl::Response> &promise,
               const QString &simUdid,
               const QString &bundleIdentifier,
               bool waitForDebugger,
               const QStringList &extraArgs,
               const QString &stdoutPath,
               const QString &stderrPath)
{
    SimulatorControl::ResponseData response(simUdid);

    if (bundleIdentifier.isEmpty()) {
        promise.addResult(make_unexpected(Tr::tr("Invalid (empty) bundle identifier.")));
        return;
    }

    QStringList args({"launch", simUdid, bundleIdentifier});

    // simctl usage documentation : Note: Log output is often directed to stderr, not stdout.
    if (!stdoutPath.isEmpty())
        args.insert(1, QString("--stderr=%1").arg(stdoutPath));

    if (!stderrPath.isEmpty())
        args.insert(1, QString("--stdout=%1").arg(stderrPath));

    if (waitForDebugger)
        args.insert(1, "-w");

    for (const QString &extraArgument : extraArgs) {
        if (!extraArgument.trimmed().isEmpty())
            args << extraArgument;
    }

    QString stdOutput;
    expected_str<void> result = runSimCtlCommand(args,
                                                 &stdOutput,
                                                 &response.commandOutput,
                                                 [&promise] { return promise.isCanceled(); });

    if (!result) {
        promise.addResult(make_unexpected(result.error()));
        return;
    }

    const QString pIdStr = stdOutput.trimmed().split(' ').last().trimmed();
    bool validPid = false;
    response.inferiorPid = pIdStr.toLongLong(&validPid);

    if (!validPid) {
        promise.addResult(
            make_unexpected(Tr::tr("Failed to convert inferior pid. (%1)").arg(pIdStr)));
        return;
    }

    promise.addResult(response);
}

void deleteSimulator(QPromise<SimulatorControl::Response> &promise, const QString &simUdid)
{
    SimulatorControl::ResponseData response(simUdid);
    expected_str<void> result = runSimCtlCommand({"delete", simUdid},
                                                 nullptr,
                                                 &response.commandOutput,
                                                 [&promise] { return promise.isCanceled(); });

    if (!result)
        promise.addResult(make_unexpected(result.error()));
    else
        promise.addResult(response);
}

void resetSimulator(QPromise<SimulatorControl::Response> &promise, const QString &simUdid)
{
    SimulatorControl::ResponseData response(simUdid);
    expected_str<void> result = runSimCtlCommand({"erase", simUdid},
                                                 nullptr,
                                                 &response.commandOutput,
                                                 [&promise] { return promise.isCanceled(); });

    if (!result)
        promise.addResult(make_unexpected(result.error()));
    else
        promise.addResult(response);
}

void renameSimulator(QPromise<SimulatorControl::Response> &promise,
                     const QString &simUdid,
                     const QString &newName)
{
    SimulatorControl::ResponseData response(simUdid);
    expected_str<void> result = runSimCtlCommand({"rename", simUdid, newName},
                                                 nullptr,
                                                 &response.commandOutput,
                                                 [&promise] { return promise.isCanceled(); });
    if (!result)
        promise.addResult(make_unexpected(result.error()));
    else
        promise.addResult(response);
}

void createSimulator(QPromise<SimulatorControl::Response> &promise,
                     const QString &name,
                     const DeviceTypeInfo &deviceType,
                     const RuntimeInfo &runtime)
{
    SimulatorControl::ResponseData response("Invalid");

    if (name.isEmpty()) {
        promise.addResult(response);
        return;
    }

    QString stdOutput;
    expected_str<void> result
        = runSimCtlCommand({"create", name, deviceType.identifier, runtime.identifier},
                           &stdOutput,
                           &response.commandOutput,
                           [&promise] { return promise.isCanceled(); });

    if (result)
        response.simUdid = stdOutput.trimmed();

    if (!result)
        promise.addResult(make_unexpected(result.error()));
    else
        promise.addResult(response);
}

void takeSceenshot(QPromise<SimulatorControl::Response> &promise,
                   const QString &simUdid,
                   const QString &filePath)
{
    SimulatorControl::ResponseData response(simUdid);
    expected_str<void> result = runSimCtlCommand({"io", simUdid, "screenshot", filePath},
                                                 nullptr,
                                                 &response.commandOutput,
                                                 [&promise] { return promise.isCanceled(); });

    if (!result)
        promise.addResult(make_unexpected(result.error()));
    else
        promise.addResult(response);
}

QString SimulatorInfo::toString() const
{
    return QString("Name: %1 UDID: %2 Availability: %3 State: %4 Runtime: %5")
        .arg(name)
        .arg(identifier)
        .arg(available)
        .arg(state)
        .arg(runtimeName);
}

bool SimulatorInfo::operator==(const SimulatorInfo &other) const
{
    return identifier == other.identifier
            && state == other.state
            && name == other.name
            && available == other.available
            && runtimeName == other.runtimeName;
}

} // Ios::Internal