tests/auto/languagechange/tst_languagechange.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <qapplication.h>
       
    46 #include <QtCore/QSet>
       
    47 #include <QtCore/QTranslator>
       
    48 #include <private/qthread_p.h>
       
    49 #include <QtGui/QInputDialog>
       
    50 #include <QtGui/QColorDialog>
       
    51 #include <QtGui/QFileDialog>
       
    52 #include <QtGui/QDesktopWidget>
       
    53 
       
    54 
       
    55 //TESTED_CLASS=
       
    56 //TESTED_FILES=
       
    57 
       
    58 class tst_languageChange : public QObject
       
    59 {
       
    60     Q_OBJECT
       
    61 public:
       
    62     tst_languageChange();
       
    63 
       
    64 public slots:
       
    65     void initTestCase();
       
    66     void cleanupTestCase();
       
    67 private slots:
       
    68     void retranslatability_data();
       
    69     void retranslatability();
       
    70 
       
    71 };
       
    72 
       
    73 
       
    74 tst_languageChange::tst_languageChange()
       
    75 
       
    76 {
       
    77 }
       
    78 
       
    79 void tst_languageChange::initTestCase()
       
    80 {
       
    81 }
       
    82 
       
    83 void tst_languageChange::cleanupTestCase()
       
    84 {
       
    85 }
       
    86 /**
       
    87  * Records all calls to translate()
       
    88  */
       
    89 class TransformTranslator : public QTranslator
       
    90 {
       
    91     Q_OBJECT
       
    92 public:
       
    93     TransformTranslator() : QTranslator() {}
       
    94     TransformTranslator(QObject *parent) : QTranslator(parent) {}
       
    95     virtual QString translate(const char *context, const char *sourceText, const char *comment = 0) const
       
    96     {
       
    97         QByteArray total(context);
       
    98         total.append("::");
       
    99         total.append(sourceText);
       
   100         if (comment) {
       
   101             total.append("::");
       
   102             total.append(comment);
       
   103         }
       
   104         m_translations.insert(total);
       
   105         QString res;
       
   106         for (int i = 0; i < int(qstrlen(sourceText)); ++i) {
       
   107             QChar ch = QLatin1Char(sourceText[i]);
       
   108             if (ch.isLower()) {
       
   109                 res.append(ch.toUpper());
       
   110             } else if (ch.isUpper()) {
       
   111                 res.append(ch.toLower());
       
   112             } else {
       
   113                 res.append(ch);
       
   114             }
       
   115         }
       
   116         return res;
       
   117     }
       
   118 
       
   119     virtual bool isEmpty() const { return false; }
       
   120 
       
   121 public slots:
       
   122     void install() {
       
   123         QCoreApplication::installTranslator(this);
       
   124         QTest::qWait(2500);
       
   125         QApplication::closeAllWindows();
       
   126     }
       
   127 public:
       
   128     mutable QSet<QByteArray> m_translations;
       
   129 };
       
   130 
       
   131 enum DialogType {
       
   132     InputDialog = 1,
       
   133     ColorDialog,
       
   134     FileDialog
       
   135 };
       
   136 
       
   137 typedef QSet<QByteArray> TranslationSet;
       
   138 Q_DECLARE_METATYPE(TranslationSet)
       
   139 
       
   140 void tst_languageChange::retranslatability_data()
       
   141 {
       
   142     QTest::addColumn<int>("dialogType");
       
   143     QTest::addColumn<TranslationSet >("expected");
       
   144 
       
   145     //next we fill it with data
       
   146     QTest::newRow( "QInputDialog" )
       
   147         << int(InputDialog) << (QSet<QByteArray>()
       
   148                     << "QDialogButtonBox::Cancel");
       
   149 
       
   150     QTest::newRow( "QColorDialog" )
       
   151         << int(ColorDialog) << (QSet<QByteArray>()
       
   152                     << "QDialogButtonBox::Cancel"
       
   153                     << "QColorDialog::&Sat:"
       
   154                     << "QColorDialog::&Add to Custom Colors"
       
   155                     << "QColorDialog::&Green:"
       
   156                     << "QColorDialog::&Red:"
       
   157                     << "QColorDialog::Bl&ue:"
       
   158                     << "QColorDialog::A&lpha channel:"
       
   159                     << "QColorDialog::&Basic colors"
       
   160                     << "QColorDialog::&Custom colors"
       
   161                     << "QColorDialog::&Val:"
       
   162                     << "QColorDialog::Hu&e:");
       
   163 
       
   164     QTest::newRow( "QFileDialog" )
       
   165         << int(FileDialog) << (QSet<QByteArray>()
       
   166                     << "QFileDialog::All Files (*)"
       
   167                     << "QFileDialog::Back"
       
   168                     << "QFileDialog::Create New Folder"
       
   169                     << "QFileDialog::Detail View"
       
   170 #ifndef Q_OS_MAC
       
   171                     << "QFileDialog::File"
       
   172 #endif
       
   173                     << "QFileDialog::Files of type:"
       
   174                     << "QFileDialog::Forward"
       
   175                     << "QFileDialog::List View"
       
   176                     << "QFileDialog::Look in:"
       
   177                     << "QFileDialog::Open"
       
   178                     << "QFileDialog::Parent Directory"
       
   179                     << "QFileDialog::Show "
       
   180                     << "QFileDialog::Show &hidden files"
       
   181                     << "QFileDialog::&Delete"
       
   182                     << "QFileDialog::&New Folder"
       
   183                     << "QFileDialog::&Rename"
       
   184                     << "QFileSystemModel::Date Modified"
       
   185 #ifdef Q_OS_WIN
       
   186                     << "QFileSystemModel::My Computer"
       
   187 #else
       
   188                     << "QFileSystemModel::Computer"
       
   189 #endif
       
   190                     << "QFileSystemModel::Size"
       
   191 #ifdef Q_OS_MAC
       
   192                     << "QFileSystemModel::Kind::Match OS X Finder"
       
   193 #else
       
   194                     << "QFileSystemModel::Type::All other platforms"
       
   195 #endif
       
   196                     << "QFileSystemModel::%1 KB"
       
   197                     << "QDialogButtonBox::Cancel"
       
   198                     << "QDialogButtonBox::Open"
       
   199                     << "QFileDialog::File &name:");
       
   200 }
       
   201 
       
   202 void tst_languageChange::retranslatability()
       
   203 {
       
   204     QFETCH( int, dialogType);
       
   205     QFETCH( TranslationSet, expected);
       
   206 
       
   207     // This will always be queried for when a language changes
       
   208     expected.insert("QApplication::QT_LAYOUT_DIRECTION::Translate this string to the string 'LTR' in left-to-right "
       
   209                        "languages or to 'RTL' in right-to-left languages (such as Hebrew and Arabic) to "
       
   210                        "get proper widget layout.");
       
   211     TransformTranslator translator;
       
   212 #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
       
   213     // Allow a little extra time or emulator startup delays cause failure
       
   214     QTimer::singleShot(5000, &translator, SLOT(install()));
       
   215 #else
       
   216     QTimer::singleShot(500, &translator, SLOT(install()));
       
   217 #endif
       
   218     switch (dialogType) {
       
   219     case InputDialog:
       
   220         (void)QInputDialog::getInteger(0, QLatin1String("title"), QLatin1String("label"));
       
   221         break;
       
   222 
       
   223     case ColorDialog:
       
   224 #ifdef Q_WS_MAC
       
   225         QSKIP("The native color dialog is used on Mac OS", SkipSingle);
       
   226 #else
       
   227         (void)QColorDialog::getColor();
       
   228 #endif
       
   229         break;
       
   230     case FileDialog: {
       
   231 #ifdef Q_WS_MAC
       
   232         QSKIP("The native file dialog is used on Mac OS", SkipSingle);
       
   233 #endif
       
   234         QFileDialog dlg;
       
   235         QString tmpParentDir = QDir::tempPath() + "/languagechangetestdir";
       
   236         QString tmpDir = tmpParentDir + "/finaldir";
       
   237         QString fooName = tmpParentDir + "/foo";
       
   238         QDir dir;
       
   239         QCOMPARE(dir.mkpath(tmpDir), true);
       
   240 #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
       
   241         // Just create a new file instead of copying exe, because exe is not there in emulator
       
   242         {
       
   243             QFile fooFile(fooName);
       
   244             QVERIFY(fooFile.open(QIODevice::WriteOnly | QIODevice::Text));
       
   245             for(int i=0; i<2048; i++) // File needs to be big enough for size to read in KB
       
   246                fooFile.write("@");
       
   247         }
       
   248 #else
       
   249         QCOMPARE(QFile::copy(QApplication::applicationFilePath(), fooName), true);
       
   250 #endif
       
   251 
       
   252         dlg.setDirectory(tmpParentDir);
       
   253 #ifdef Q_OS_WINCE
       
   254         dlg.setDirectory("\\Windows");
       
   255 #endif
       
   256         dlg.setFileMode(QFileDialog::ExistingFiles);
       
   257         dlg.setViewMode(QFileDialog::Detail);
       
   258         dlg.exec();
       
   259 #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
       
   260         // increase the wait time because of increased delay caused by emulator startup
       
   261         QTest::qWait(15000);
       
   262 #else
       
   263         QTest::qWait(3000);
       
   264 #endif
       
   265         QCOMPARE(QFile::remove(fooName), true);
       
   266         QCOMPARE(dir.rmdir(tmpDir), true);
       
   267         QCOMPARE(dir.rmdir(tmpParentDir), true);
       
   268         break; }
       
   269     }
       
   270 #if 0
       
   271     QList<QByteArray> list = translator.m_translations.toList();
       
   272     qSort(list);
       
   273     qDebug() << list;
       
   274 #endif
       
   275     // In case we use a Color dialog, we do not want to test for
       
   276     // strings non existing in the dialog and which do not get
       
   277     // translated.
       
   278     if ((dialogType == ColorDialog) &&
       
   279 #ifndef Q_OS_WINCE
       
   280         (qApp->desktop()->width() < 480 || qApp->desktop()->height() < 350)
       
   281 #else
       
   282         true // On Qt/WinCE we always use compact mode
       
   283 #endif
       
   284         ) {
       
   285         expected.remove("QColorDialog::&Basic colors");
       
   286         expected.remove("QColorDialog::&Custom colors");
       
   287         expected.remove("QColorDialog::&Define Custom Colors >>");
       
   288         expected.remove("QColorDialog::&Add to Custom Colors");
       
   289     }
       
   290 
       
   291     // see if all of our *expected* translations was translated.
       
   292     // (There might be more, but thats not that bad)
       
   293     QSet<QByteArray> commonTranslations = expected;
       
   294     commonTranslations.intersect(translator.m_translations);
       
   295     if (!expected.subtract(commonTranslations).isEmpty()) {
       
   296         qDebug() << "Missing:" << expected;
       
   297         if (!translator.m_translations.subtract(commonTranslations).isEmpty())
       
   298             qDebug() << "Unexpected:" << translator.m_translations;
       
   299     }
       
   300 
       
   301     QVERIFY(expected.isEmpty());
       
   302 }
       
   303 
       
   304 QTEST_MAIN(tst_languageChange)
       
   305 #include "tst_languagechange.moc"