aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/android/androidsdkmanager_test.cpp
blob: f677c9cd34e5b8172c1ad31dc1f18ca364dc59e7 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "androidsdkmanager_test.h"
#include "androidsdkmanager.h"

#include <QTest>

namespace Android::Internal {

class AndroidSdkManagerTest final : public QObject
{
    Q_OBJECT

private slots:
    void testAndroidSdkManagerProgressParser_data();
    void testAndroidSdkManagerProgressParser();
};

void AndroidSdkManagerTest::testAndroidSdkManagerProgressParser_data()
{
    QTest::addColumn<QString>("output");
    QTest::addColumn<int>("progress");
    QTest::addColumn<bool>("foundAssertion");

    // Output of "sdkmanager --licenses", Android SDK Tools version 4.0
    QTest::newRow("Loading local repository")
        << "Loading local repository...                                                     \r"
        << -1
        << false;

    QTest::newRow("Fetch progress (single line)")
        << "[=============                          ] 34% Fetch remote repository...        \r"
        << 34
        << false;

    QTest::newRow("Fetch progress (multi line)")
        << "[=============================          ] 73% Fetch remote repository...        \r"
           "[=============================          ] 75% Fetch remote repository...        \r"
        << 75
        << false;

    QTest::newRow("Some SDK package licenses not accepted")
        << "7 of 7 SDK package licenses not accepted.\n"
        << -1
        << false;

    QTest::newRow("Unaccepted licenses assertion")
        << "\nReview licenses that have not been accepted (y/N)? "
        << -1
        << true;
}

static int parseProgress(const QString &out, bool &foundAssertion)
{
    int progress = -1;
    if (out.isEmpty())
        return progress;
    static const QRegularExpression reg("(?<progress>\\d*)%");
    static const QRegularExpression regEndOfLine("[\\n\\r]");
    const QStringList lines = out.split(regEndOfLine, Qt::SkipEmptyParts);
    for (const QString &line : lines) {
        QRegularExpressionMatch match = reg.match(line);
        if (match.hasMatch()) {
            progress = match.captured("progress").toInt();
            if (progress < 0 || progress > 100)
                progress = -1;
        }
        if (!foundAssertion)
            foundAssertion = assertionRegExp().match(line).hasMatch();
    }
    return progress;
}

void AndroidSdkManagerTest::testAndroidSdkManagerProgressParser()
{
    QFETCH(QString, output);
    QFETCH(int, progress);
    QFETCH(bool, foundAssertion);

    bool actualFoundAssertion = false;
    const int actualProgress = parseProgress(output, actualFoundAssertion);

    QCOMPARE(progress, actualProgress);
    QCOMPARE(foundAssertion, actualFoundAssertion);
}

QObject *createAndroidSdkManagerTest()
{
    return new AndroidSdkManagerTest;
}

} // Android::Internal

#include "androidsdkmanager_test.moc"