summaryrefslogtreecommitdiffstats
path: root/tests/auto/installer/messageboxhandler/tst_messageboxhandler.cpp
blob: 9515848aac98a91526969240cfb45d25c2d092c6 (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
#include <messageboxhandler.h>
#include <qinstallerglobal.h>
#include <scriptengine.h>
#include <packagemanagercore.h>

#include <QTest>
#include <QMetaEnum>
#include <QDebug>

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace QInstaller;

namespace QTest {
    template<>
    char *toString(const QMessageBox::StandardButton &button)
    {
        QString buttonAsString(QString::number(button));
        return qstrdup(buttonAsString.toLatin1().data());
    }
}

class tst_MessageBoxHandler : public QObject
{
    Q_OBJECT
public:
private slots:
    void initTestCase()
    {
        m_maxStandardButtons = 0;

        const QMetaObject &messageBoxMetaObject = QMessageBox::staticMetaObject;
        int index = messageBoxMetaObject.indexOfEnumerator("StandardButtons");

        QMetaEnum metaEnum = messageBoxMetaObject.enumerator(index);
        for (int i = 0; i < metaEnum.keyCount(); i++) {
            int enumValue = metaEnum.value(i);
            if (enumValue < QMessageBox::FirstButton)
                continue;
            m_standardButtonValueMap.insert(static_cast<QMessageBox::StandardButton>(enumValue),
                metaEnum.valueToKey(metaEnum.value(i)));
            m_maxStandardButtons += enumValue;
            if (enumValue == QMessageBox::LastButton)
                break;
        }
    }

    void testScriptButtonValues()
    {
        PackageManagerCore core;
        ScriptEngine *scriptEngine = new ScriptEngine(&core);
        QMapIterator<QMessageBox::StandardButton, QString> i(m_standardButtonValueMap);
        while (i.hasNext()) {
            i.next();
            const QString scriptString = QString::fromLatin1("QMessageBox.%1").arg(i.value());
            const QJSValue scriptValue = scriptEngine->evaluate(scriptString);

            QVERIFY2(!scriptValue.isError(), qPrintable(scriptValue.toString()));
            QVERIFY2(!scriptValue.isUndefined(), qPrintable(
                QString::fromLatin1("It seems that %1 is undefined.").arg(scriptString)));
            QCOMPARE(static_cast<QMessageBox::StandardButton>(scriptValue.toInt()), i.key());
        }
    }

    void testDefaultAction()
    {
        const char ignoreMessage[] = "Created critical message box \"TestError\": \"A test error\", "
            "\"This is a test error message.\"";
        srand(time(0)); /* initialize random seed: */

        int standardButtons = QMessageBox::NoButton;
        MessageBoxHandler::instance()->setDefaultAction(MessageBoxHandler::Reject);
        const QList<QMessageBox::Button> orderedButtons = MessageBoxHandler::orderedButtons();
        do {
            standardButtons += QMessageBox::FirstButton;

            /* generate secret number between 1 and 10: */
            const int iSecret = rand() % 10 + 1;
            // use only every 5th run to reduce the time which it takes to run this test
            if (iSecret > 2)
                continue;

            QTest::ignoreMessage(QtDebugMsg, ignoreMessage);
            int returnButton = MessageBoxHandler::instance()->critical(QLatin1String("TestError"),
                QLatin1String("A test error"), QLatin1String("This is a test error message."),
                static_cast<QMessageBox::StandardButton>(standardButtons));

            QMessageBox::StandardButton wantedButton = QMessageBox::NoButton;
            // find the last button which is the wanted reject button in the current
            // standardButtons combination
            foreach (QMessageBox::StandardButton button, orderedButtons) {
                if (standardButtons & button)
                    wantedButton = button;
            }

            QVERIFY2(wantedButton != QMessageBox::NoButton, "Cannot find a wantedButton.");
            QCOMPARE(static_cast<QMessageBox::StandardButton>(returnButton), wantedButton);
        } while (standardButtons < m_maxStandardButtons);
    }

private:
    QMap<QMessageBox::StandardButton, QString> m_standardButtonValueMap;
    int m_maxStandardButtons;
};

QTEST_MAIN(tst_MessageBoxHandler)

#include "tst_messageboxhandler.moc"