1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
44 #include <qkeysequence.h>
45 #include <qpa/qplatformtheme.h>
46 #include <private/qkeysequence_p.h>
47 #include <private/qguiapplication_p.h>
48 #include <QTranslator>
49 #include <QLibraryInfo>
52 #include <Carbon/Carbon.h>
53 struct MacSpecialKey {
58 static const int NumEntries = 21;
59 static const MacSpecialKey entries[NumEntries] = {
60 { Qt::Key_Escape, 0x238B },
61 { Qt::Key_Tab, 0x21E5 },
62 { Qt::Key_Backtab, 0x21E4 },
63 { Qt::Key_Backspace, 0x232B },
64 { Qt::Key_Return, 0x21B5 },
65 { Qt::Key_Enter, 0x21B5 },
66 { Qt::Key_Delete, 0x2326 },
67 { Qt::Key_Home, 0x2196 },
68 { Qt::Key_End, 0x2198 },
69 { Qt::Key_Left, 0x2190 },
70 { Qt::Key_Up, 0x2191 },
71 { Qt::Key_Right, 0x2192 },
72 { Qt::Key_Down, 0x2193 },
73 { Qt::Key_PageUp, 0x21DE },
74 { Qt::Key_PageDown, 0x21DF },
75 { Qt::Key_Shift, kShiftUnicode },
76 { Qt::Key_Control, kCommandUnicode },
77 { Qt::Key_Meta, kControlUnicode },
78 { Qt::Key_Alt, kOptionUnicode },
79 { Qt::Key_CapsLock, 0x21EA },
82 static bool operator<(const MacSpecialKey &entry, int key)
84 return entry.key < key;
87 static bool operator<(int key, const MacSpecialKey &entry)
89 return key < entry.key;
92 static const MacSpecialKey * const MacSpecialKeyEntriesEnd = entries + NumEntries;
94 static QChar macSymbolForQtKey(int key)
96 const MacSpecialKey *i = qBinaryFind(entries, MacSpecialKeyEntriesEnd, key);
97 if (i == MacSpecialKeyEntriesEnd)
99 return QChar(i->macSymbol);
104 class tst_QKeySequence : public QObject
110 virtual ~tst_QKeySequence();
114 void operatorQString_data();
115 void operatorQString();
116 void compareConstructors_data();
117 void compareConstructors();
118 void symetricConstructors_data();
119 void symetricConstructors();
120 void checkMultipleNames();
121 void checkMultipleCodes();
123 void mnemonic_data();
126 void toString_data();
128 void toStringFromKeycode_data();
129 void toStringFromKeycode();
130 void streamOperators_data();
131 void streamOperators();
132 void parseString_data();
134 void fromString_data();
136 #ifdef QT_BUILD_INTERNAL
139 void standardKeys_data();
142 #if !defined (Q_OS_MAC) && !defined (Q_OS_WINCE)
143 void translated_data();
146 void i18nKeys_data();
152 int m_keyboardScheme;
153 QTranslator *ourTranslator;
154 QTranslator *qtTranslator;
156 static const QString MacCtrl;
157 static const QString MacMeta;
158 static const QString MacAlt;
159 static const QString MacShift;
166 const QString tst_QKeySequence::MacCtrl = QString(QChar(0x2318));
167 const QString tst_QKeySequence::MacMeta = QString(QChar(0x2303));
168 const QString tst_QKeySequence::MacAlt = QString(QChar(0x2325));
169 const QString tst_QKeySequence::MacShift = QString(QChar(0x21E7));
172 tst_QKeySequence::tst_QKeySequence() : m_keyboardScheme(QPlatformTheme::WindowsKeyboardScheme)
174 if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
175 m_keyboardScheme = theme->themeHint(QPlatformTheme::KeyboardScheme).toInt();
178 tst_QKeySequence::~tst_QKeySequence()
183 void tst_QKeySequence::initTestCase()
185 ourTranslator = new QTranslator(this);
186 ourTranslator->load(":/keys_de");
187 qtTranslator = new QTranslator(this);
188 qtTranslator->load(":/qt_de");
191 void tst_QKeySequence::swap()
193 QKeySequence ks1(Qt::CTRL+Qt::Key_O);
194 QKeySequence ks2(Qt::CTRL+Qt::Key_L);
196 QCOMPARE(ks1[0], int(Qt::CTRL+Qt::Key_L));
197 QCOMPARE(ks2[0], int(Qt::CTRL+Qt::Key_O));
200 void tst_QKeySequence::operatorQString_data()
202 QTest::addColumn<int>("modifiers");
203 QTest::addColumn<int>("keycode");
204 QTest::addColumn<QString>("keystring");
206 QTest::newRow( "No modifier" ) << 0 << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "\x0c5" );
209 QTest::newRow( "Ctrl+Left" ) << int(Qt::CTRL) << int(Qt::Key_Left) << QString( "Ctrl+Left" );
210 QTest::newRow( "Ctrl+," ) << int(Qt::CTRL) << int(Qt::Key_Comma) << QString( "Ctrl+," );
211 QTest::newRow( "Alt+Left" ) << int(Qt::ALT) << int(Qt::Key_Left) << QString( "Alt+Left" );
212 QTest::newRow( "Alt+Shift+Left" ) << int(Qt::ALT | Qt::SHIFT) << int(Qt::Key_Left) << QString( "Alt+Shift+Left" );
213 QTest::newRow( "Ctrl" ) << int(Qt::CTRL) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Ctrl+\x0c5" );
214 QTest::newRow( "Alt" ) << int(Qt::ALT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Alt+\x0c5" );
215 QTest::newRow( "Shift" ) << int(Qt::SHIFT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Shift+\x0c5" );
216 QTest::newRow( "Meta" ) << int(Qt::META) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Meta+\x0c5" );
218 QTest::newRow( "Ctrl+Left" ) << int(Qt::CTRL) << int(Qt::Key_Left) << MacCtrl + macSymbolForQtKey(Qt::Key_Left);
219 QTest::newRow( "Ctrl+," ) << int(Qt::CTRL) << int(Qt::Key_Comma) << MacCtrl + ",";
220 QTest::newRow( "Alt+Left" ) << int(Qt::ALT) << int(Qt::Key_Left) << MacAlt + macSymbolForQtKey(Qt::Key_Left);
221 QTest::newRow( "Alt+Shift+Left" ) << int(Qt::ALT | Qt::SHIFT) << int(Qt::Key_Left) << MacAlt + MacShift + macSymbolForQtKey(Qt::Key_Left);
222 QTest::newRow( "Ctrl" ) << int(Qt::CTRL) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacCtrl + QLatin1String("\x0c5");
223 QTest::newRow( "Alt" ) << int(Qt::ALT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacAlt + QLatin1String("\x0c5");
224 QTest::newRow( "Shift" ) << int(Qt::SHIFT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacShift + QLatin1String("\x0c5");
225 QTest::newRow( "Meta" ) << int(Qt::META) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacMeta + QLatin1String("\x0c5");
229 void tst_QKeySequence::symetricConstructors_data()
231 QTest::addColumn<int>("modifiers");
232 QTest::addColumn<int>("keycode");
234 QTest::newRow( "No modifier" ) << 0 << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
235 QTest::newRow( "Ctrl" ) << int(Qt::CTRL) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
236 QTest::newRow( "Alt" ) << int(Qt::ALT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
237 QTest::newRow( "Shift" ) << int(Qt::SHIFT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
238 QTest::newRow( "Meta" ) << int(Qt::META) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
241 void tst_QKeySequence::compareConstructors_data()
243 operatorQString_data();
246 // operator QString()
247 void tst_QKeySequence::operatorQString()
250 QFETCH( int, modifiers );
251 QFETCH( int, keycode );
252 QFETCH( QString, keystring );
254 seq = QKeySequence( modifiers | keycode );
256 QCOMPARE( (QString)seq, keystring );
259 // this verifies that the constructors can handle the same strings in and out
260 void tst_QKeySequence::symetricConstructors()
262 QFETCH( int, modifiers );
263 QFETCH( int, keycode );
265 QKeySequence seq1( modifiers | keycode );
266 QKeySequence seq2( (QString)seq1 );
268 QVERIFY( seq1 == seq2 );
271 /* Compares QKeySequence constructurs with int or QString arguments
272 We don't do this for 3.0 since it doesn't support unicode accelerators */
273 void tst_QKeySequence::compareConstructors()
275 QFETCH( int, modifiers );
276 QFETCH( int, keycode );
277 QFETCH( QString, keystring );
279 QKeySequence qstringSeq( keystring );
280 QKeySequence intSeq( modifiers | keycode );
282 QVERIFY( qstringSeq == intSeq );
285 void tst_QKeySequence::checkMultipleNames()
287 QKeySequence oldK( "Ctrl+Page Up" );
288 QKeySequence newK( "Ctrl+PgUp" );
289 QVERIFY( oldK == newK );
292 //TODO: could test third constructor, or test fromString on all constructor-data
293 void tst_QKeySequence::checkMultipleCodes()
295 QKeySequence seq1("Alt+d, l");
296 QKeySequence seq2 = QKeySequence::fromString("Alt+d, l");
297 QVERIFY( seq1 == seq2 );
299 QKeySequence seq3("Alt+d,l");
300 QKeySequence seq4 = QKeySequence::fromString("Alt+d,l");
301 QVERIFY( seq3 == seq4 );
305 * We must ensure that the keyBindings data is always sorted
306 * so that we can safely perform binary searches.
308 #ifdef QT_BUILD_INTERNAL
309 void tst_QKeySequence::ensureSorted()
311 uint N = QKeySequencePrivate::numberOfKeyBindings;
312 uint val = QKeySequencePrivate::keyBindings[0].shortcut;
313 for ( uint i = 1 ; i < N ; ++i) {
314 uint nextval = QKeySequencePrivate::keyBindings[i].shortcut;
316 qDebug() << "Data not sorted at index " << i;
317 QVERIFY(nextval >= val);
323 void tst_QKeySequence::standardKeys_data()
325 QTest::addColumn<int>("standardKey");
326 QTest::addColumn<QString>("expected");
327 QTest::newRow("unknownkey") << (int)QKeySequence::UnknownKey<< QString("");
328 QTest::newRow("copy") << (int)QKeySequence::Copy << QString("CTRL+C");
329 QTest::newRow("cut") << (int)QKeySequence::Cut << QString("CTRL+X");
330 QTest::newRow("paste") << (int)QKeySequence::Paste << QString("CTRL+V");
331 QTest::newRow("delete") << (int)QKeySequence::Delete<< QString("DEL");
332 QTest::newRow("open") << (int)QKeySequence::Open << QString("CTRL+O");
333 QTest::newRow("find") << (int)QKeySequence::Find<< QString("CTRL+F");
334 if (m_keyboardScheme == QPlatformTheme::WindowsKeyboardScheme) {
335 QTest::newRow("addTab") << (int)QKeySequence::AddTab<< QString("CTRL+T");
336 QTest::newRow("findNext") << (int)QKeySequence::FindNext<< QString("F3");
337 QTest::newRow("findPrevious") << (int)QKeySequence::FindPrevious << QString("SHIFT+F3");
338 QTest::newRow("close") << (int)QKeySequence::Close<< QString("CTRL+F4");
339 QTest::newRow("replace") << (int)QKeySequence::Replace<< QString("CTRL+H");
341 QTest::newRow("bold") << (int)QKeySequence::Bold << QString("CTRL+B");
342 QTest::newRow("italic") << (int)QKeySequence::Italic << QString("CTRL+I");
343 QTest::newRow("underline") << (int)QKeySequence::Underline << QString("CTRL+U");
344 QTest::newRow("selectall") << (int)QKeySequence::SelectAll << QString("CTRL+A");
345 QTest::newRow("print") << (int)QKeySequence::Print << QString("CTRL+P");
346 QTest::newRow("movenextchar") << (int)QKeySequence::MoveToNextChar<< QString("RIGHT");
347 QTest::newRow("zoomIn") << (int)QKeySequence::ZoomIn<< QString("CTRL++");
348 QTest::newRow("zoomOut") << (int)QKeySequence::ZoomOut<< QString("CTRL+-");
349 QTest::newRow("whatsthis") << (int)QKeySequence::WhatsThis<< QString("SHIFT+F1");
351 #if defined(Q_OS_MAC)
352 QTest::newRow("help") << (int)QKeySequence::HelpContents<< QString("Ctrl+?");
353 QTest::newRow("nextChild") << (int)QKeySequence::NextChild << QString("CTRL+}");
354 QTest::newRow("previousChild") << (int)QKeySequence::PreviousChild << QString("CTRL+{");
355 QTest::newRow("MoveToEndOfBlock") << (int)QKeySequence::MoveToEndOfBlock << QString("ALT+DOWN");
356 QTest::newRow("forward") << (int)QKeySequence::Forward << QString("CTRL+]");
357 QTest::newRow("backward") << (int)QKeySequence::Back << QString("CTRL+[");
358 QTest::newRow("SelectEndOfDocument") << (int)QKeySequence::SelectEndOfDocument<< QString("CTRL+SHIFT+DOWN"); //mac only
360 QTest::newRow("help") << (int)QKeySequence::HelpContents<< QString("F1");
361 QTest::newRow("nextChild") << (int)QKeySequence::NextChild<< QString("CTRL+Tab");
362 QTest::newRow("previousChild") << (int)QKeySequence::PreviousChild<< QString("CTRL+SHIFT+BACKTAB");
363 QTest::newRow("forward") << (int)QKeySequence::Forward << QString("ALT+RIGHT");
364 QTest::newRow("backward") << (int)QKeySequence::Back << QString("ALT+LEFT");
365 QTest::newRow("MoveToEndOfBlock") << (int)QKeySequence::MoveToEndOfBlock<< QString(""); //mac only
366 QTest::newRow("SelectEndOfDocument") << (int)QKeySequence::SelectEndOfDocument<< QString("CTRL+SHIFT+END"); //mac only
370 void tst_QKeySequence::standardKeys()
372 QFETCH(int, standardKey);
373 QFETCH(QString, expected);
374 QKeySequence actualKeySequence((QKeySequence::StandardKey)standardKey);
375 QKeySequence expectedKeySequence(expected);
376 QVERIFY2(actualKeySequence == expectedKeySequence,
377 qPrintable(QString::fromLatin1("Key mismatch, expected '%1', got '%2' for standard key %3").
378 arg(expected, actualKeySequence.toString()).arg(standardKey)));
381 void tst_QKeySequence::keyBindings()
383 const QList<QKeySequence> bindings =
384 QKeySequence::keyBindings(QKeySequence::Copy);
386 QList<QKeySequence> expected;
387 const QKeySequence ctrlC = QKeySequence(QStringLiteral("CTRL+C"));
388 const QKeySequence ctrlInsert = QKeySequence(QStringLiteral("CTRL+INSERT"));
389 switch (m_keyboardScheme) {
390 case QPlatformTheme::MacKeyboardScheme:
393 case QPlatformTheme::WindowsKeyboardScheme:
394 expected << ctrlC << ctrlInsert;
397 expected << ctrlC << QKeySequence(QStringLiteral("F16")) << ctrlInsert;
400 QCOMPARE(bindings, expected);
404 void tst_QKeySequence::mnemonic_data()
406 QTest::addColumn<QString>("string");
407 QTest::addColumn<QString>("key");
408 QTest::addColumn<bool>("warning");
410 QTest::newRow("1") << QString::fromLatin1("&bonjour") << QString::fromLatin1("ALT+B") << false;
411 QTest::newRow("2") << QString::fromLatin1("&&bonjour") << QString() << false;
412 QTest::newRow("3") << QString::fromLatin1("&&bon&jour") << QString::fromLatin1("ALT+J") << false;
413 QTest::newRow("4") << QString::fromLatin1("&&bon&jo&ur") << QString::fromLatin1("ALT+J") << true;
414 QTest::newRow("5") << QString::fromLatin1("b&on&&jour") << QString::fromLatin1("ALT+O") << false;
415 QTest::newRow("6") << QString::fromLatin1("bonjour") << QString() << false;
416 QTest::newRow("7") << QString::fromLatin1("&&&bonjour") << QString::fromLatin1("ALT+B") << false;
417 QTest::newRow("8") << QString::fromLatin1("bonjour&&&") << QString() << false;
418 QTest::newRow("9") << QString::fromLatin1("bo&&nj&o&&u&r") << QString::fromLatin1("ALT+O") << true;
419 QTest::newRow("10") << QString::fromLatin1("BON&JOUR") << QString::fromLatin1("ALT+J") << false;
420 QTest::newRow("11") << QString::fromUtf8("bonjour") << QString() << false;
423 void tst_QKeySequence::mnemonic()
425 QFETCH(QString, string);
426 QFETCH(QString, key);
427 QFETCH(bool, warning);
433 QString str = QString::fromLatin1("QKeySequence::mnemonic: \"%1\" contains multiple occurrences of '&'").arg(string);
434 QTest::ignoreMessage(QtWarningMsg, qPrintable(str));
435 // qWarning(qPrintable(str));
438 QKeySequence seq = QKeySequence::mnemonic(string);
439 QKeySequence res = QKeySequence(key);
445 void tst_QKeySequence::toString_data()
447 QTest::addColumn<QString>("strSequence");
448 QTest::addColumn<QString>("neutralString");
449 QTest::addColumn<QString>("platformString");
453 QTest::newRow("Ctrl+Left") << QString("Ctrl+Left") << QString("Ctrl+Left") << QString("Ctrl+Left");
454 QTest::newRow("Alt+Left") << QString("Alt+Left") << QString("Alt+Left") << QString("Alt+Left");
455 QTest::newRow("Alt+Shift+Left") << QString("Alt+Shift+Left") << QString("Alt+Shift+Left") << QString("Alt+Shift+Left");
456 QTest::newRow("Ctrl") << QString::fromLatin1("Ctrl+\x0c5") << QString::fromLatin1("Ctrl+\x0c5") << QString::fromLatin1("Ctrl+\x0c5");
457 QTest::newRow("Alt") << QString::fromLatin1("Alt+\x0c5") << QString::fromLatin1("Alt+\x0c5") << QString::fromLatin1("Alt+\x0c5");
458 QTest::newRow("Shift") << QString::fromLatin1("Shift+\x0c5") << QString::fromLatin1("Shift+\x0c5") << QString::fromLatin1("Shift+\x0c5");
459 QTest::newRow("Meta") << QString::fromLatin1("Meta+\x0c5") << QString::fromLatin1("Meta+\x0c5") << QString::fromLatin1("Meta+\x0c5");
460 QTest::newRow("Ctrl+Plus") << QString("Ctrl++") << QString("Ctrl++") << QString("Ctrl++");
461 QTest::newRow("Ctrl+,") << QString("Ctrl+,") << QString("Ctrl+,") << QString("Ctrl+,");
462 QTest::newRow("Ctrl+,,Ctrl+,") << QString("Ctrl+,,Ctrl+,") << QString("Ctrl+,, Ctrl+,") << QString("Ctrl+,, Ctrl+,");
463 QTest::newRow("MultiKey") << QString("Alt+X, Ctrl+Y, Z") << QString("Alt+X, Ctrl+Y, Z")
464 << QString("Alt+X, Ctrl+Y, Z");
466 QTest::newRow("Invalid") << QString("Ctrly") << QString("") << QString("");
469 QTest::newRow("Ctrl+Left") << MacCtrl + "Left" << QString("Ctrl+Left") << MacCtrl + macSymbolForQtKey(Qt::Key_Left);
470 QTest::newRow("Alt+Left") << MacAlt + "Left" << QString("Alt+Left") << MacAlt + macSymbolForQtKey(Qt::Key_Left);
471 QTest::newRow("Alt+Shift+Left") << MacAlt + MacShift + "Left" << QString("Alt+Shift+Left")
472 << MacAlt + MacShift + macSymbolForQtKey(Qt::Key_Left);
474 QTest::newRow("Ctrl+Right,Left") << MacCtrl + "Right, Left" << QString("Ctrl+Right, Left") << MacCtrl + macSymbolForQtKey(Qt::Key_Right) + QString(", ") + macSymbolForQtKey(Qt::Key_Left);
475 QTest::newRow("Ctrl") << MacCtrl + QLatin1String("\x0c5") << QString::fromLatin1("Ctrl+\x0c5") << MacCtrl + QLatin1String("\x0c5");
476 QTest::newRow("Alt") << MacAlt + QLatin1String("\x0c5") << QString::fromLatin1("Alt+\x0c5") << MacAlt + QLatin1String("\x0c5");
477 QTest::newRow("Shift") << MacShift + QLatin1String("\x0c5") << QString::fromLatin1("Shift+\x0c5") << MacShift + QLatin1String("\x0c5");
478 QTest::newRow("Meta") << MacMeta + QLatin1String("\x0c5") << QString::fromLatin1("Meta+\x0c5") << MacMeta + QLatin1String("\x0c5");
479 QTest::newRow("Ctrl+Plus") << MacCtrl + "+" << QString("Ctrl++") << MacCtrl + "+";
480 QTest::newRow("Ctrl+,") << MacCtrl + "," << QString("Ctrl+,") << MacCtrl + ",";
481 QTest::newRow("Ctrl+,,Ctrl+,") << MacCtrl + ",, " + MacCtrl + "," << QString("Ctrl+,, Ctrl+,") << MacCtrl + ",, " + MacCtrl + ",";
482 QTest::newRow("MultiKey") << MacAlt + "X, " + MacCtrl + "Y, Z" << QString("Alt+X, Ctrl+Y, Z")
483 << MacAlt + "X, " + MacCtrl + "Y, Z";
484 QTest::newRow("Invalid") << QString("Ctrly") << QString("") << QString("");
488 void tst_QKeySequence::toString()
490 QFETCH(QString, strSequence);
491 QFETCH(QString, neutralString);
492 QFETCH(QString, platformString);
494 QKeySequence ks1(strSequence);
496 QCOMPARE(ks1.toString(QKeySequence::NativeText), platformString);
497 QCOMPARE(ks1.toString(QKeySequence::PortableText), neutralString);
501 void tst_QKeySequence::toStringFromKeycode_data()
503 QTest::addColumn<QKeySequence>("keycode");
504 QTest::addColumn<QString>("expectedString");
506 QTest::newRow("A") << QKeySequence(Qt::Key_A) << "A";
507 QTest::newRow("-1") << QKeySequence(-1) << "";
508 QTest::newRow("Unknown") << QKeySequence(Qt::Key_unknown) << "";
511 void tst_QKeySequence::toStringFromKeycode()
513 QFETCH(QKeySequence, keycode);
514 QFETCH(QString, expectedString);
516 QCOMPARE(QKeySequence(keycode).toString(), expectedString);
519 void tst_QKeySequence::streamOperators_data()
521 operatorQString_data();
524 void tst_QKeySequence::streamOperators()
526 QFETCH( int, modifiers );
527 QFETCH( int, keycode );
530 QKeySequence refK( modifiers | keycode );
531 QKeySequence orgK( "Ctrl+A" );
532 QKeySequence copyOrgK = orgK;
533 QVERIFY( copyOrgK == orgK );
535 QDataStream in(&data, QIODevice::WriteOnly);
537 QDataStream out(&data, QIODevice::ReadOnly);
540 QVERIFY( orgK == refK );
543 QVERIFY( orgK != copyOrgK );
547 void tst_QKeySequence::parseString_data()
549 QTest::addColumn<QString>("strSequence");
550 QTest::addColumn<QKeySequence>("keycode");
553 QTest::newRow("A") << "A" << QKeySequence(Qt::Key_A);
554 QTest::newRow("a") << "a" << QKeySequence(Qt::Key_A);
555 QTest::newRow("Ctrl+Left") << "Ctrl+Left" << QKeySequence(Qt::CTRL + Qt::Key_Left);
556 QTest::newRow("CTRL+LEFT") << "CTRL+LEFT" << QKeySequence(Qt::CTRL + Qt::Key_Left);
557 QTest::newRow("Meta+A") << "Meta+a" << QKeySequence(Qt::META + Qt::Key_A);
558 QTest::newRow("mEtA+A") << "mEtA+a" << QKeySequence(Qt::META + Qt::Key_A);
559 QTest::newRow("Ctrl++") << "Ctrl++" << QKeySequence(Qt::CTRL + Qt::Key_Plus);
562 QTest::newRow("Win+A") << "Win+a" << QKeySequence(Qt::Key_unknown);
563 QTest::newRow("Super+Meta+A") << "Super+Meta+A" << QKeySequence(Qt::Key_unknown);
566 QTest::newRow("Meta+Trolls") << "Meta+Trolls" << QKeySequence(Qt::Key_unknown);
567 QTest::newRow("Meta+Period") << "Meta+Period" << QKeySequence(Qt::Key_unknown);
568 QTest::newRow("Meta+Ypsilon") << "Meta+Ypsilon" << QKeySequence(Qt::Key_unknown);
571 QTest::newRow("4+3=2") << "4+3=2" << QKeySequence(Qt::Key_unknown);
572 QTest::newRow("Alabama") << "Alabama" << QKeySequence(Qt::Key_unknown);
573 QTest::newRow("Simon+G") << "Simon+G" << QKeySequence(Qt::Key_unknown);
574 QTest::newRow("Shift+++2") << "Shift+++2" << QKeySequence(Qt::Key_unknown);
577 QTest::newRow("A+Meta") << "a+Meta" << QKeySequence(Qt::Key_unknown);
578 QTest::newRow("Meta+++Shift") << "Meta+++Shift" << QKeySequence(Qt::Key_unknown);
579 QTest::newRow("Meta+a+Shift") << "Meta+a+Shift" << QKeySequence(Qt::Key_unknown);
581 // Only Modifiers - currently not supported
582 //QTest::newRow("Meta+Shift") << "Meta+Shift" << QKeySequence(Qt::META + Qt::SHIFT);
583 //QTest::newRow("Ctrl") << "Ctrl" << QKeySequence(Qt::CTRL);
584 //QTest::newRow("Shift") << "Shift" << QKeySequence(Qt::SHIFT);
587 QTest::newRow("a") << "a" << QKeySequence(Qt::Key_A);
588 QTest::newRow("A") << "A" << QKeySequence(Qt::Key_A);
591 QTest::newRow("Meta+Shift+") << "Meta+Shift+" << QKeySequence(Qt::Key_unknown);
594 void tst_QKeySequence::parseString()
596 QFETCH( QString, strSequence );
597 QFETCH( QKeySequence, keycode );
600 QEXPECT_FAIL("Win+A", "QTBUG-24406 - This test fails on OSX", Abort);
601 QEXPECT_FAIL("Simon+G", "QTBUG-24406 - This test fails on OSX", Abort);
604 QCOMPARE( QKeySequence(strSequence).toString(), keycode.toString() );
605 QVERIFY( QKeySequence(strSequence) == keycode );
608 void tst_QKeySequence::fromString_data()
613 void tst_QKeySequence::fromString()
615 QFETCH(QString, strSequence);
616 QFETCH(QString, neutralString);
617 QFETCH(QString, platformString);
619 if (strSequence == "Ctrly") // Key_Unknown gives empty string
622 QKeySequence ks1(strSequence);
623 QKeySequence ks2 = QKeySequence::fromString(ks1.toString());
624 QKeySequence ks3 = QKeySequence::fromString(neutralString, QKeySequence::PortableText);
625 QKeySequence ks4 = QKeySequence::fromString(platformString, QKeySequence::NativeText);
628 // assume the transitive property exists here.
634 #if !defined (Q_OS_MAC) && !defined (Q_OS_WINCE)
635 void tst_QKeySequence::translated_data()
637 qApp->installTranslator(ourTranslator);
638 qApp->installTranslator(qtTranslator);
640 QTest::addColumn<QString>("transKey");
641 QTest::addColumn<QString>("compKey");
643 QTest::newRow("Shift++") << tr("Shift++") << QString("Umschalt++");
644 QTest::newRow("Ctrl++") << tr("Ctrl++") << QString("Strg++");
645 QTest::newRow("Alt++") << tr("Alt++") << QString("Alt++");
646 QTest::newRow("Meta++") << tr("Meta++") << QString("Meta++");
648 QTest::newRow("Shift+,, Shift++") << tr("Shift+,, Shift++") << QString("Umschalt+,, Umschalt++");
649 QTest::newRow("Shift+,, Ctrl++") << tr("Shift+,, Ctrl++") << QString("Umschalt+,, Strg++");
650 QTest::newRow("Shift+,, Alt++") << tr("Shift+,, Alt++") << QString("Umschalt+,, Alt++");
651 QTest::newRow("Shift+,, Meta++") << tr("Shift+,, Meta++") << QString("Umschalt+,, Meta++");
653 QTest::newRow("Ctrl+,, Shift++") << tr("Ctrl+,, Shift++") << QString("Strg+,, Umschalt++");
654 QTest::newRow("Ctrl+,, Ctrl++") << tr("Ctrl+,, Ctrl++") << QString("Strg+,, Strg++");
655 QTest::newRow("Ctrl+,, Alt++") << tr("Ctrl+,, Alt++") << QString("Strg+,, Alt++");
656 QTest::newRow("Ctrl+,, Meta++") << tr("Ctrl+,, Meta++") << QString("Strg+,, Meta++");
658 qApp->removeTranslator(ourTranslator);
659 qApp->removeTranslator(qtTranslator);
662 void tst_QKeySequence::translated()
664 QFETCH(QString, transKey);
665 QFETCH(QString, compKey);
667 qApp->installTranslator(ourTranslator);
668 qApp->installTranslator(qtTranslator);
670 QKeySequence ks1(transKey);
671 QCOMPARE(ks1.toString(QKeySequence::NativeText), compKey);
673 qApp->removeTranslator(ourTranslator);
674 qApp->removeTranslator(qtTranslator);
678 void tst_QKeySequence::i18nKeys_data()
680 QTest::addColumn<int>("keycode");
681 QTest::addColumn<QString>("keystring");
683 // Japanese keyboard support
684 QTest::newRow("Kanji") << (int)Qt::Key_Kanji << QString("Kanji");
685 QTest::newRow("Muhenkan") << (int)Qt::Key_Muhenkan << QString("Muhenkan");
686 QTest::newRow("Henkan") << (int)Qt::Key_Henkan << QString("Henkan");
687 QTest::newRow("Romaji") << (int)Qt::Key_Romaji << QString("Romaji");
688 QTest::newRow("Hiragana") << (int)Qt::Key_Hiragana << QString("Hiragana");
689 QTest::newRow("Katakana") << (int)Qt::Key_Katakana << QString("Katakana");
690 QTest::newRow("Hiragana Katakana") << (int)Qt::Key_Hiragana_Katakana << QString("Hiragana Katakana");
691 QTest::newRow("Zenkaku") << (int)Qt::Key_Zenkaku << QString("Zenkaku");
692 QTest::newRow("Hankaku") << (int)Qt::Key_Hankaku << QString("Hankaku");
693 QTest::newRow("Zenkaku Hankaku") << (int)Qt::Key_Zenkaku_Hankaku << QString("Zenkaku Hankaku");
694 QTest::newRow("Touroku") << (int)Qt::Key_Touroku << QString("Touroku");
695 QTest::newRow("Massyo") << (int)Qt::Key_Massyo << QString("Massyo");
696 QTest::newRow("Kana Lock") << (int)Qt::Key_Kana_Lock << QString("Kana Lock");
697 QTest::newRow("Kana Shift") << (int)Qt::Key_Kana_Shift << QString("Kana Shift");
698 QTest::newRow("Eisu Shift") << (int)Qt::Key_Eisu_Shift << QString("Eisu Shift");
699 QTest::newRow("Eisu_toggle") << (int)Qt::Key_Eisu_toggle << QString("Eisu toggle");
700 QTest::newRow("Code input") << (int)Qt::Key_Codeinput << QString("Code input");
701 QTest::newRow("Multiple Candidate") << (int)Qt::Key_MultipleCandidate << QString("Multiple Candidate");
702 QTest::newRow("Previous Candidate") << (int)Qt::Key_PreviousCandidate << QString("Previous Candidate");
704 // Korean keyboard support
705 QTest::newRow("Hangul") << (int)Qt::Key_Hangul << QString("Hangul");
706 QTest::newRow("Hangul Start") << (int)Qt::Key_Hangul_Start << QString("Hangul Start");
707 QTest::newRow("Hangul End") << (int)Qt::Key_Hangul_End << QString("Hangul End");
708 QTest::newRow("Hangul Hanja") << (int)Qt::Key_Hangul_Hanja << QString("Hangul Hanja");
709 QTest::newRow("Hangul Jamo") << (int)Qt::Key_Hangul_Jamo << QString("Hangul Jamo");
710 QTest::newRow("Hangul Romaja") << (int)Qt::Key_Hangul_Romaja << QString("Hangul Romaja");
711 QTest::newRow("Hangul Jeonja") << (int)Qt::Key_Hangul_Jeonja << QString("Hangul Jeonja");
712 QTest::newRow("Hangul Banja") << (int)Qt::Key_Hangul_Banja << QString("Hangul Banja");
713 QTest::newRow("Hangul PreHanja") << (int)Qt::Key_Hangul_PreHanja << QString("Hangul PreHanja");
714 QTest::newRow("Hangul PostHanja") << (int)Qt::Key_Hangul_PostHanja << QString("Hangul PostHanja");
715 QTest::newRow("Hangul Special") << (int)Qt::Key_Hangul_Special << QString("Hangul Special");
718 void tst_QKeySequence::i18nKeys()
720 QFETCH(int, keycode);
721 QFETCH(QString, keystring);
722 QKeySequence seq(keycode);
724 QCOMPARE(seq, QKeySequence(keystring));
725 QCOMPARE(seq.toString(), keystring);
728 QTEST_MAIN(tst_QKeySequence)
729 #include "tst_qkeysequence.moc"