| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2019 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include <QtTest/qtest.h> |
| 38 | #include "../shared/visualtestutil.h" |
| 39 | |
| 40 | #include <QtCore/qtranslator.h> |
| 41 | #include <QtGui/private/qguiapplication_p.h> |
| 42 | #include <QtGui/qpa/qplatformtheme.h> |
| 43 | #include <QtQuick/qquickview.h> |
| 44 | #include <QtQuickTemplates2/private/qquickabstractbutton_p.h> |
| 45 | #include <QtQuickTemplates2/private/qquickcombobox_p.h> |
| 46 | #include <QtQuickTemplates2/private/qquickdialog_p.h> |
| 47 | #include <QtQuickTemplates2/private/qquickdialogbuttonbox_p.h> |
| 48 | #include <QtQuickTemplates2/private/qquicktextfield_p.h> |
| 49 | |
| 50 | using namespace QQuickVisualTestUtil; |
| 51 | |
| 52 | class tst_translation : public QQmlDataTest |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | |
| 56 | private slots: |
| 57 | void dialogButtonBox(); |
| 58 | void dialogButtonBoxWithCustomButtons(); |
| 59 | void comboBox(); |
| 60 | }; |
| 61 | |
| 62 | void tst_translation::dialogButtonBox() |
| 63 | { |
| 64 | QQuickView view(testFileUrl(fileName: "dialogButtonBox.qml" )); |
| 65 | if (view.status() != QQuickView::Ready) |
| 66 | QFAIL("Failed to load QML file" ); |
| 67 | view.show(); |
| 68 | QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 69 | |
| 70 | QQuickDialog *dialog = view.rootObject()->property(name: "dialog" ).value<QQuickDialog*>(); |
| 71 | QVERIFY(dialog); |
| 72 | |
| 73 | QQuickDialogButtonBox *dialogButtonBox = qobject_cast<QQuickDialogButtonBox*>(object: dialog->footer()); |
| 74 | QVERIFY(dialogButtonBox); |
| 75 | |
| 76 | QQuickAbstractButton *saveButton = dialogButtonBox->standardButton(button: QPlatformDialogHelper::Save); |
| 77 | QVERIFY(saveButton); |
| 78 | QString defaultSaveText = QGuiApplicationPrivate::platformTheme()->standardButtonText(button: QPlatformDialogHelper::Save); |
| 79 | defaultSaveText = QPlatformTheme::removeMnemonics(original: defaultSaveText); |
| 80 | QCOMPARE(saveButton->text(), defaultSaveText); |
| 81 | |
| 82 | QQuickAbstractButton *discardButton = dialogButtonBox->standardButton(button: QPlatformDialogHelper::Discard); |
| 83 | QVERIFY(discardButton); |
| 84 | QString defaultDiscardText = QGuiApplicationPrivate::platformTheme()->standardButtonText(button: QPlatformDialogHelper::Discard); |
| 85 | defaultDiscardText = QPlatformTheme::removeMnemonics(original: defaultDiscardText); |
| 86 | QCOMPARE(discardButton->text(), defaultDiscardText); |
| 87 | |
| 88 | QTranslator translator; |
| 89 | QVERIFY(translator.load("qtbase_fr.qm" , ":/" )); |
| 90 | QVERIFY(qApp->installTranslator(&translator)); |
| 91 | qApp->sendPostedEvents(); |
| 92 | view.engine()->retranslate(); |
| 93 | |
| 94 | QString translatedSaveText = QGuiApplicationPrivate::platformTheme()->standardButtonText(button: QPlatformDialogHelper::Save); |
| 95 | translatedSaveText = QPlatformTheme::removeMnemonics(original: translatedSaveText); |
| 96 | QCOMPARE(saveButton->text(), translatedSaveText); |
| 97 | |
| 98 | QString translatedDiscardText = QGuiApplicationPrivate::platformTheme()->standardButtonText(button: QPlatformDialogHelper::Discard); |
| 99 | translatedDiscardText = QPlatformTheme::removeMnemonics(original: translatedDiscardText); |
| 100 | QCOMPARE(discardButton->text(), translatedDiscardText); |
| 101 | } |
| 102 | |
| 103 | // Test that custom buttons with explicitly specified text |
| 104 | // do not have that text overwritten on language changes. |
| 105 | void tst_translation::dialogButtonBoxWithCustomButtons() |
| 106 | { |
| 107 | // This is just a way of simulating the translator going out of scope |
| 108 | // after the QML has been loaded. |
| 109 | QScopedPointer<QTranslator> translator(new QTranslator); |
| 110 | // Doesn't matter which language it is, as we won't be using it anyway. |
| 111 | QVERIFY(translator->load("qtbase_fr.qm" , ":/" )); |
| 112 | QVERIFY(qApp->installTranslator(translator.data())); |
| 113 | |
| 114 | QQuickView view(testFileUrl(fileName: "dialogButtonBoxWithCustomButtons.qml" )); |
| 115 | if (view.status() != QQuickView::Ready) |
| 116 | QFAIL("Failed to load QML file" ); |
| 117 | view.show(); |
| 118 | QVERIFY(QTest::qWaitForWindowActive(&view)); |
| 119 | |
| 120 | QQuickDialog *dialog = view.rootObject()->property(name: "dialog" ).value<QQuickDialog*>(); |
| 121 | QVERIFY(dialog); |
| 122 | |
| 123 | QQuickDialogButtonBox *dialogButtonBox = qobject_cast<QQuickDialogButtonBox*>(object: dialog->footer()); |
| 124 | QVERIFY(dialogButtonBox); |
| 125 | |
| 126 | auto okButton = dialogButtonBox->findChild<QQuickAbstractButton*>(aName: "okButton" ); |
| 127 | QVERIFY(okButton); |
| 128 | QCOMPARE(okButton->text(), QLatin1String("OK" )); |
| 129 | |
| 130 | QQuickAbstractButton *cancelButton = dialogButtonBox->findChild<QQuickAbstractButton*>(aName: "cancelButton" ); |
| 131 | QVERIFY(cancelButton); |
| 132 | QCOMPARE(cancelButton->text(), QLatin1String("Cancel" )); |
| 133 | |
| 134 | // Delete the translator and hence cause a LanguageChange event, |
| 135 | // but _without_ calling QQmlEngine::retranslate(), which would |
| 136 | // restore the original bindings and hence not reproduce the issue. |
| 137 | translator.reset(); |
| 138 | QCOMPARE(okButton->text(), QLatin1String("OK" )); |
| 139 | QCOMPARE(cancelButton->text(), QLatin1String("Cancel" )); |
| 140 | } |
| 141 | |
| 142 | void tst_translation::comboBox() |
| 143 | { |
| 144 | QQuickView view(testFileUrl(fileName: "comboBox.qml" )); |
| 145 | |
| 146 | QQuickComboBox *comboBox = qobject_cast<QQuickComboBox*>(object: view.rootObject()); |
| 147 | QVERIFY(comboBox); |
| 148 | QCOMPARE(comboBox->displayText(), QLatin1String("Hello" )); |
| 149 | |
| 150 | QQuickTextField *contentItem = qobject_cast<QQuickTextField*>(object: comboBox->contentItem()); |
| 151 | QVERIFY(contentItem); |
| 152 | QCOMPARE(contentItem->text(), QLatin1String("Hello" )); |
| 153 | |
| 154 | QTranslator translator; |
| 155 | QVERIFY(translator.load("qml_jp.qm" , ":/" )); |
| 156 | QVERIFY(qApp->installTranslator(&translator)); |
| 157 | view.engine()->retranslate(); |
| 158 | QTRY_COMPARE(comboBox->displayText(), QString::fromUtf8("こんにちは" )); |
| 159 | QCOMPARE(contentItem->text(), QString::fromUtf8("こんにちは" )); |
| 160 | } |
| 161 | |
| 162 | QTEST_MAIN(tst_translation) |
| 163 | |
| 164 | #include "tst_translation.moc" |
| 165 | |