summaryrefslogtreecommitdiffstats
path: root/tests/auto/languagechange/tst_languagechange.cpp
blob: f856f67458f99305f2b22836c1cbd25fdbf1435b (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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#include <qapplication.h>
#include <QtCore/QSet>
#include <QtCore/QTranslator>
#include <private/qthread_p.h>
#include <QtGui/QInputDialog>
#include <QtGui/QColorDialog>
#include <QtGui/QFileDialog>
#include <QtGui/QDesktopWidget>


//TESTED_CLASS=
//TESTED_FILES=

class tst_languageChange : public QObject
{
    Q_OBJECT
public:
    tst_languageChange();

public slots:
    void initTestCase();
    void cleanupTestCase();
private slots:
    void retranslatability_data();
    void retranslatability();

};


tst_languageChange::tst_languageChange()

{
}

void tst_languageChange::initTestCase()
{
}

void tst_languageChange::cleanupTestCase()
{
}
/**
 * Records all calls to translate()
 */
class TransformTranslator : public QTranslator
{
    Q_OBJECT
public:
    TransformTranslator() : QTranslator() {}
    TransformTranslator(QObject *parent) : QTranslator(parent) {}
    virtual QString translate(const char *context, const char *sourceText, const char *comment = 0) const
    {
        QByteArray total(context);
        total.append("::");
        total.append(sourceText);
        if (comment) {
            total.append("::");
            total.append(comment);
        }
        m_translations.insert(total);
        QString res;
        for (int i = 0; i < int(qstrlen(sourceText)); ++i) {
            QChar ch = QLatin1Char(sourceText[i]);
            if (ch.isLower()) {
                res.append(ch.toUpper());
            } else if (ch.isUpper()) {
                res.append(ch.toLower());
            } else {
                res.append(ch);
            }
        }
        return res;
    }

    virtual bool isEmpty() const { return false; }

public slots:
    void install() {
        QCoreApplication::installTranslator(this);
        QTest::qWait(2500);
        //### is there any better way to close a Qt dialog?
        QThreadData *data = QThreadData::current();
        if (!data->eventLoops.isEmpty())
            data->eventLoops.top()->quit();
    }
public:
    mutable QSet<QByteArray> m_translations;
};

enum DialogType {
    InputDialog = 1,
    ColorDialog,
    FileDialog
};

typedef QSet<QByteArray> TranslationSet;
Q_DECLARE_METATYPE(TranslationSet)

void tst_languageChange::retranslatability_data()
{
    QTest::addColumn<int>("dialogType");
    QTest::addColumn<TranslationSet >("expected");

    //next we fill it with data
    QTest::newRow( "QInputDialog" )
        << int(InputDialog) << (QSet<QByteArray>()
                    << "QDialogButtonBox::Cancel");

    QTest::newRow( "QColorDialog" )
        << int(ColorDialog) << (QSet<QByteArray>()
                    << "QDialogButtonBox::Cancel"
                    << "QColorDialog::&Sat:"
                    << "QColorDialog::&Add to Custom Colors"
                    << "QColorDialog::&Green:"
                    << "QColorDialog::&Red:"
                    << "QColorDialog::Bl&ue:"
                    << "QColorDialog::A&lpha channel:"
                    << "QColorDialog::&Basic colors"
                    << "QColorDialog::&Custom colors"
                    << "QColorDialog::&Val:"
                    << "QColorDialog::Hu&e:");

    QTest::newRow( "QFileDialog" )
        << int(FileDialog) << (QSet<QByteArray>()
                    << "QFileDialog::All Files (*)"
                    << "QFileDialog::Back"
                    << "QFileDialog::Create New Folder"
                    << "QFileDialog::Detail View"
#ifndef Q_OS_MAC
                    << "QFileDialog::File"
#endif
                    << "QFileDialog::Files of type:"
                    << "QFileDialog::Forward"
                    << "QFileDialog::List View"
                    << "QFileDialog::Look in:"
                    << "QFileDialog::Open"
                    << "QFileDialog::Parent Directory"
                    << "QFileDialog::Show "
                    << "QFileDialog::Show &hidden files"
                    << "QFileDialog::&Delete"
                    << "QFileDialog::&New Folder"
                    << "QFileDialog::&Rename"
                    << "QFileSystemModel::Date Modified"
#ifdef Q_OS_WIN
                    << "QFileSystemModel::My Computer"
#else
                    << "QFileSystemModel::Computer"
#endif
                    << "QFileSystemModel::Size"
#ifdef Q_OS_MAC
                    << "QFileSystemModel::Kind::Match OS X Finder"
#else
                    << "QFileSystemModel::Type::All other platforms"
#endif
                    << "QFileSystemModel::%1 KB"
                    << "QDialogButtonBox::Cancel"
                    << "QDialogButtonBox::Open"
                    << "QFileDialog::File &name:");
}

void tst_languageChange::retranslatability()
{
    QFETCH( int, dialogType);
    QFETCH( TranslationSet, expected);

    // This will always be queried for when a language changes
    expected.insert("QApplication::QT_LAYOUT_DIRECTION::Translate this string to the string 'LTR' in left-to-right "
                       "languages or to 'RTL' in right-to-left languages (such as Hebrew and Arabic) to "
                       "get proper widget layout.");
    TransformTranslator translator;
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
    // Allow a little extra time or emulator startup delays cause failure 
    QTimer::singleShot(5000, &translator, SLOT(install()));
#else    
    QTimer::singleShot(500, &translator, SLOT(install()));
#endif    
    switch (dialogType) {
    case InputDialog:
        (void)QInputDialog::getInteger(0, QLatin1String("title"), QLatin1String("label"));
        break;

    case ColorDialog:
#ifdef Q_WS_MAC
        QSKIP("The native color dialog is used on Mac OS", SkipSingle);
#else
        (void)QColorDialog::getColor();
#endif
        break;
    case FileDialog: {
#ifdef Q_WS_MAC
        QSKIP("The native file dialog is used on Mac OS", SkipSingle);
#endif
        QFileDialog dlg;
        QString tmpParentDir = QDir::tempPath() + "/languagechangetestdir";
        QString tmpDir = tmpParentDir + "/finaldir";
        QString fooName = tmpParentDir + "/foo";
        QDir dir;
        QCOMPARE(dir.mkpath(tmpDir), true);
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
        // Just create a new file instead of copying exe, because exe is not there in emulator
        {
            QFile fooFile(fooName);
            QVERIFY(fooFile.open(QIODevice::WriteOnly | QIODevice::Text));
            for(int i=0; i<2048; i++) // File needs to be big enough for size to read in KB
               fooFile.write("@");
        }
#else
        QCOMPARE(QFile::copy(QApplication::applicationFilePath(), fooName), true);
#endif

        dlg.setDirectory(tmpParentDir);
#ifdef Q_OS_WINCE
        dlg.setDirectory("\\Windows");
#endif
        dlg.setFileMode(QFileDialog::ExistingFiles);
        dlg.setViewMode(QFileDialog::Detail);
        dlg.exec();
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
        // increase the wait time because of increased delay caused by emulator startup
        QTest::qWait(15000);
#else
        QTest::qWait(3000);
#endif
        QCOMPARE(QFile::remove(fooName), true);
        QCOMPARE(dir.rmdir(tmpDir), true);
        QCOMPARE(dir.rmdir(tmpParentDir), true);
        break; }
    }
#if 0
    QList<QByteArray> list = translator.m_translations.toList();
    qSort(list);
    qDebug() << list;
#endif
    // In case we use a Color dialog, we do not want to test for
    // strings non existing in the dialog and which do not get
    // translated.
    if ((dialogType == ColorDialog) &&
#ifndef Q_OS_WINCE
        (qApp->desktop()->width() < 480 || qApp->desktop()->height() < 350)
#else
        true // On Qt/WinCE we always use compact mode
#endif
        ) {
        expected.remove("QColorDialog::&Basic colors");
        expected.remove("QColorDialog::&Custom colors");
        expected.remove("QColorDialog::&Define Custom Colors >>");
        expected.remove("QColorDialog::&Add to Custom Colors");
    }

    // see if all of our *expected* translations was translated.
    // (There might be more, but thats not that bad)
    QSet<QByteArray> commonTranslations = expected;
    commonTranslations.intersect(translator.m_translations);
    if (!expected.subtract(commonTranslations).isEmpty()) {
        qDebug() << "Missing:" << expected;
        if (!translator.m_translations.subtract(commonTranslations).isEmpty())
            qDebug() << "Unexpected:" << translator.m_translations;
    }

    QVERIFY(expected.isEmpty());
}

QTEST_MAIN(tst_languageChange)
#include "tst_languagechange.moc"