766352f9180dd30bf17bf02472d7a4ba7e2901d7
[profile/ivi/qtbase.git] / tests / auto / gui / kernel / qkeysequence / tst_qkeysequence.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
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>
50
51 #ifdef Q_OS_MAC
52 #include <Carbon/Carbon.h>
53 struct MacSpecialKey {
54     int key;
55     ushort macSymbol;
56 };
57
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 },
80 };
81
82 static bool operator<(const MacSpecialKey &entry, int key)
83 {
84     return entry.key < key;
85 }
86
87 static bool operator<(int key, const MacSpecialKey &entry)
88 {
89     return key < entry.key;
90 }
91
92 static const MacSpecialKey * const MacSpecialKeyEntriesEnd = entries + NumEntries;
93
94 static QChar macSymbolForQtKey(int key)
95 {
96     const MacSpecialKey *i = qBinaryFind(entries, MacSpecialKeyEntriesEnd, key);
97     if (i == MacSpecialKeyEntriesEnd)
98         return QChar();
99     return QChar(i->macSymbol);
100 }
101
102 #endif
103
104 class tst_QKeySequence : public QObject
105 {
106     Q_OBJECT
107
108 public:
109     tst_QKeySequence();
110     virtual ~tst_QKeySequence();
111
112 private slots:
113     void swap();
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();
122     void mnemonic_data();
123     void mnemonic();
124     void toString_data();
125     void toString();
126     void toStringFromKeycode_data();
127     void toStringFromKeycode();
128     void streamOperators_data();
129     void streamOperators();
130     void parseString_data();
131     void parseString();
132     void fromString_data();
133     void fromString();
134     void ensureSorted();
135     void standardKeys_data();
136     void standardKeys();
137     void keyBindings();
138     void translated_data();
139     void translated();
140     void i18nKeys_data();
141     void i18nKeys();
142
143
144     void initTestCase();
145 private:
146     int m_keyboardScheme;
147     QTranslator *ourTranslator;
148     QTranslator *qtTranslator;
149 #ifdef Q_OS_MAC
150     static const QString MacCtrl;
151     static const QString MacMeta;
152     static const QString MacAlt;
153     static const QString MacShift;
154 #endif
155
156
157 };
158
159 #ifdef Q_OS_MAC
160 const QString tst_QKeySequence::MacCtrl = QString(QChar(0x2318));
161 const QString tst_QKeySequence::MacMeta = QString(QChar(0x2303));
162 const QString tst_QKeySequence::MacAlt = QString(QChar(0x2325));
163 const QString tst_QKeySequence::MacShift = QString(QChar(0x21E7));
164 #endif
165
166 tst_QKeySequence::tst_QKeySequence() : m_keyboardScheme(QPlatformTheme::WindowsKeyboardScheme)
167 {
168     if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
169         m_keyboardScheme = theme->themeHint(QPlatformTheme::KeyboardScheme).toInt();
170 }
171
172 tst_QKeySequence::~tst_QKeySequence()
173 {
174
175 }
176
177 void tst_QKeySequence::initTestCase()
178 {
179     ourTranslator = new QTranslator(this);
180     ourTranslator->load(":/keys_de");
181     qtTranslator = new QTranslator(this);
182     qtTranslator->load(":/qt_de");
183 }
184
185 void tst_QKeySequence::swap()
186 {
187     QKeySequence ks1(Qt::CTRL+Qt::Key_O);
188     QKeySequence ks2(Qt::CTRL+Qt::Key_L);
189     ks1.swap(ks2);
190     QCOMPARE(ks1[0], int(Qt::CTRL+Qt::Key_L));
191     QCOMPARE(ks2[0], int(Qt::CTRL+Qt::Key_O));
192 }
193
194 void tst_QKeySequence::operatorQString_data()
195 {
196     QTest::addColumn<int>("modifiers");
197     QTest::addColumn<int>("keycode");
198     QTest::addColumn<QString>("keystring");
199
200     QTest::newRow( "No modifier" ) << 0 << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "\x0c5" );
201
202 #ifndef Q_OS_MAC
203     QTest::newRow( "Ctrl+Left" ) << int(Qt::CTRL) << int(Qt::Key_Left) << QString( "Ctrl+Left" );
204     QTest::newRow( "Ctrl+," ) << int(Qt::CTRL) << int(Qt::Key_Comma) << QString( "Ctrl+," );
205     QTest::newRow( "Alt+Left" ) << int(Qt::ALT) << int(Qt::Key_Left) << QString( "Alt+Left" );
206     QTest::newRow( "Alt+Shift+Left" ) << int(Qt::ALT | Qt::SHIFT) << int(Qt::Key_Left) << QString( "Alt+Shift+Left" );
207     QTest::newRow( "Ctrl" ) << int(Qt::CTRL) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Ctrl+\x0c5" );
208     QTest::newRow( "Alt" ) << int(Qt::ALT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Alt+\x0c5" );
209     QTest::newRow( "Shift" ) << int(Qt::SHIFT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Shift+\x0c5" );
210     QTest::newRow( "Meta" ) << int(Qt::META) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "Meta+\x0c5" );
211 #else
212     QTest::newRow( "Ctrl+Left" ) << int(Qt::CTRL) << int(Qt::Key_Left) << MacCtrl + macSymbolForQtKey(Qt::Key_Left);
213     QTest::newRow( "Ctrl+," ) << int(Qt::CTRL) << int(Qt::Key_Comma) << MacCtrl + ",";
214     QTest::newRow( "Alt+Left" ) << int(Qt::ALT) << int(Qt::Key_Left) << MacAlt + macSymbolForQtKey(Qt::Key_Left);
215     QTest::newRow( "Alt+Shift+Left" ) << int(Qt::ALT | Qt::SHIFT) << int(Qt::Key_Left) << MacAlt + MacShift + macSymbolForQtKey(Qt::Key_Left);
216     QTest::newRow( "Ctrl" ) << int(Qt::CTRL) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacCtrl + QLatin1String("\x0c5");
217     QTest::newRow( "Alt" ) << int(Qt::ALT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacAlt + QLatin1String("\x0c5");
218     QTest::newRow( "Shift" ) << int(Qt::SHIFT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacShift + QLatin1String("\x0c5");
219     QTest::newRow( "Meta" ) << int(Qt::META) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << MacMeta + QLatin1String("\x0c5");
220 #endif
221 }
222
223 void tst_QKeySequence::symetricConstructors_data()
224 {
225     QTest::addColumn<int>("modifiers");
226     QTest::addColumn<int>("keycode");
227
228     QTest::newRow( "No modifier" ) << 0 << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
229     QTest::newRow( "Ctrl" ) << int(Qt::CTRL) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
230     QTest::newRow( "Alt" ) << int(Qt::ALT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
231     QTest::newRow( "Shift" ) << int(Qt::SHIFT) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
232     QTest::newRow( "Meta" ) << int(Qt::META) << int(Qt::Key_Aring | Qt::UNICODE_ACCEL);
233 }
234
235 void tst_QKeySequence::compareConstructors_data()
236 {
237     operatorQString_data();
238 }
239
240 // operator QString()
241 void tst_QKeySequence::operatorQString()
242 {
243     QKeySequence seq;
244     QFETCH( int, modifiers );
245     QFETCH( int, keycode );
246     QFETCH( QString, keystring );
247
248     seq = QKeySequence( modifiers | keycode );
249
250     QCOMPARE( (QString)seq, keystring );
251 }
252
253 // this verifies that the constructors can handle the same strings in and out
254 void tst_QKeySequence::symetricConstructors()
255 {
256     QFETCH( int, modifiers );
257     QFETCH( int, keycode );
258
259     QKeySequence seq1( modifiers | keycode );
260     QKeySequence seq2( (QString)seq1 );
261
262     QVERIFY( seq1 == seq2 );
263 }
264
265 /* Compares QKeySequence constructurs with int or QString arguments
266    We don't do this for 3.0 since it doesn't support unicode accelerators */
267 void tst_QKeySequence::compareConstructors()
268 {
269     QFETCH( int, modifiers );
270     QFETCH( int, keycode );
271     QFETCH( QString, keystring );
272
273     QKeySequence qstringSeq( keystring );
274     QKeySequence intSeq( modifiers | keycode );
275
276     QVERIFY( qstringSeq == intSeq );
277 }
278
279 void tst_QKeySequence::checkMultipleNames()
280 {
281     QKeySequence oldK( "Ctrl+Page Up" );
282     QKeySequence newK( "Ctrl+PgUp" );
283     QVERIFY( oldK == newK );
284 }
285
286 //TODO: could test third constructor, or test fromString on all constructor-data
287 void tst_QKeySequence::checkMultipleCodes()
288 {
289     QKeySequence seq1("Alt+d, l");
290     QKeySequence seq2 = QKeySequence::fromString("Alt+d, l");
291     QVERIFY( seq1 == seq2 );
292
293     QKeySequence seq3("Alt+d,l");
294     QKeySequence seq4 = QKeySequence::fromString("Alt+d,l");
295     QVERIFY( seq3 == seq4 );
296 }
297
298 /*
299 * We must ensure that the keyBindings data is always sorted
300 * so that we can safely perform binary searches.
301 */
302 void tst_QKeySequence::ensureSorted()
303 {
304 #if defined(QT_BUILD_INTERNAL)
305     uint N = QKeySequencePrivate::numberOfKeyBindings;
306     uint val = QKeySequencePrivate::keyBindings[0].shortcut;
307     for ( uint i = 1 ; i < N ; ++i) {
308         uint nextval = QKeySequencePrivate::keyBindings[i].shortcut;
309         if (nextval < val)
310             qDebug() << "Data not sorted at index " << i;
311         QVERIFY(nextval >= val);
312         val = nextval;
313     }
314 #endif
315 }
316
317 void tst_QKeySequence::standardKeys_data()
318 {
319     QTest::addColumn<int>("standardKey");
320     QTest::addColumn<QString>("expected");
321     QTest::newRow("unknownkey") << (int)QKeySequence::UnknownKey<< QString("");
322     QTest::newRow("copy") << (int)QKeySequence::Copy << QString("CTRL+C");
323     QTest::newRow("cut") << (int)QKeySequence::Cut << QString("CTRL+X");
324     QTest::newRow("paste") << (int)QKeySequence::Paste << QString("CTRL+V");
325     QTest::newRow("delete") << (int)QKeySequence::Delete<< QString("DEL");
326     QTest::newRow("open") << (int)QKeySequence::Open << QString("CTRL+O");
327     QTest::newRow("find") << (int)QKeySequence::Find<< QString("CTRL+F");
328     if (m_keyboardScheme == QPlatformTheme::WindowsKeyboardScheme) {
329         QTest::newRow("addTab") << (int)QKeySequence::AddTab<< QString("CTRL+T");
330         QTest::newRow("findNext") << (int)QKeySequence::FindNext<< QString("F3");
331         QTest::newRow("findPrevious") << (int)QKeySequence::FindPrevious << QString("SHIFT+F3");
332         QTest::newRow("close") << (int)QKeySequence::Close<< QString("CTRL+F4");
333         QTest::newRow("replace") << (int)QKeySequence::Replace<< QString("CTRL+H");
334     }
335     QTest::newRow("bold") << (int)QKeySequence::Bold << QString("CTRL+B");
336     QTest::newRow("italic") << (int)QKeySequence::Italic << QString("CTRL+I");
337     QTest::newRow("underline") << (int)QKeySequence::Underline << QString("CTRL+U");
338     QTest::newRow("selectall") << (int)QKeySequence::SelectAll << QString("CTRL+A");
339     QTest::newRow("print") << (int)QKeySequence::Print << QString("CTRL+P");
340     QTest::newRow("movenextchar") << (int)QKeySequence::MoveToNextChar<< QString("RIGHT");
341     QTest::newRow("zoomIn") << (int)QKeySequence::ZoomIn<< QString("CTRL++");
342     QTest::newRow("zoomOut") << (int)QKeySequence::ZoomOut<< QString("CTRL+-");
343     QTest::newRow("whatsthis") << (int)QKeySequence::WhatsThis<< QString("SHIFT+F1");
344
345 #if defined(Q_OS_MAC)
346     QTest::newRow("help") << (int)QKeySequence::HelpContents<< QString("Ctrl+?");
347     QTest::newRow("nextChild") << (int)QKeySequence::NextChild << QString("CTRL+}");
348     QTest::newRow("previousChild") << (int)QKeySequence::PreviousChild << QString("CTRL+{");
349     QTest::newRow("MoveToEndOfBlock") << (int)QKeySequence::MoveToEndOfBlock << QString("ALT+DOWN");
350     QTest::newRow("forward") << (int)QKeySequence::Forward << QString("CTRL+]");
351     QTest::newRow("backward") << (int)QKeySequence::Back << QString("CTRL+[");
352     QTest::newRow("SelectEndOfDocument") << (int)QKeySequence::SelectEndOfDocument<< QString("CTRL+SHIFT+DOWN"); //mac only
353 #else
354     QTest::newRow("help") << (int)QKeySequence::HelpContents<< QString("F1");
355     QTest::newRow("nextChild") << (int)QKeySequence::NextChild<< QString("CTRL+Tab");
356     QTest::newRow("previousChild") << (int)QKeySequence::PreviousChild<< QString("CTRL+SHIFT+BACKTAB");
357     QTest::newRow("forward") << (int)QKeySequence::Forward << QString("ALT+RIGHT");
358     QTest::newRow("backward") << (int)QKeySequence::Back << QString("ALT+LEFT");
359     QTest::newRow("MoveToEndOfBlock") << (int)QKeySequence::MoveToEndOfBlock<< QString(""); //mac only
360     QTest::newRow("SelectEndOfDocument") << (int)QKeySequence::SelectEndOfDocument<< QString("CTRL+SHIFT+END"); //mac only
361 #endif
362 }
363
364 void tst_QKeySequence::standardKeys()
365 {
366     QFETCH(int, standardKey);
367     QFETCH(QString, expected);
368     QKeySequence actualKeySequence((QKeySequence::StandardKey)standardKey);
369     QKeySequence expectedKeySequence(expected);
370     QVERIFY2(actualKeySequence == expectedKeySequence,
371              qPrintable(QString::fromLatin1("Key mismatch, expected '%1', got '%2' for standard key %3").
372                         arg(expected, actualKeySequence.toString()).arg(standardKey)));
373 }
374
375 void tst_QKeySequence::keyBindings()
376 {
377     const QList<QKeySequence> bindings =
378           QKeySequence::keyBindings(QKeySequence::Copy);
379
380     QList<QKeySequence> expected;
381     const QKeySequence ctrlC = QKeySequence(QStringLiteral("CTRL+C"));
382     const QKeySequence ctrlInsert = QKeySequence(QStringLiteral("CTRL+INSERT"));
383     switch (m_keyboardScheme) {
384     case QPlatformTheme::MacKeyboardScheme:
385         expected  << ctrlC;
386         break;
387     case QPlatformTheme::WindowsKeyboardScheme:
388         expected  << ctrlC << ctrlInsert;
389         break;
390     default: // X11
391         expected  << ctrlC << QKeySequence(QStringLiteral("F16")) << ctrlInsert;
392         break;
393     }
394     QCOMPARE(bindings, expected);
395 }
396
397 void tst_QKeySequence::mnemonic_data()
398 {
399     QTest::addColumn<QString>("string");
400     QTest::addColumn<QString>("key");
401     QTest::addColumn<bool>("warning");
402
403     QTest::newRow("1") << QString::fromLatin1("&bonjour") << QString::fromLatin1("ALT+B") << false;
404     QTest::newRow("2") << QString::fromLatin1("&&bonjour") << QString() << false;
405     QTest::newRow("3") << QString::fromLatin1("&&bon&jour") << QString::fromLatin1("ALT+J") << false;
406     QTest::newRow("4") << QString::fromLatin1("&&bon&jo&ur") << QString::fromLatin1("ALT+J") << true;
407     QTest::newRow("5") << QString::fromLatin1("b&on&&jour") << QString::fromLatin1("ALT+O") << false;
408     QTest::newRow("6") << QString::fromLatin1("bonjour") << QString() << false;
409     QTest::newRow("7") << QString::fromLatin1("&&&bonjour") << QString::fromLatin1("ALT+B") << false;
410     QTest::newRow("8") << QString::fromLatin1("bonjour&&&") << QString() << false;
411     QTest::newRow("9") << QString::fromLatin1("bo&&nj&o&&u&r") << QString::fromLatin1("ALT+O") << true;
412     QTest::newRow("10") << QString::fromLatin1("BON&JOUR") << QString::fromLatin1("ALT+J") << false;
413     QTest::newRow("11") << QString::fromUtf8("bonjour") << QString() << false;
414 }
415
416 void tst_QKeySequence::mnemonic()
417 {
418 #ifdef Q_OS_MAC
419     QSKIP("mnemonics are not used on Mac OS X");
420 #endif
421     QFETCH(QString, string);
422     QFETCH(QString, key);
423     QFETCH(bool, warning);
424
425 #ifdef QT_NO_DEBUG
426     Q_UNUSED(warning)
427 #else
428     if (warning) {
429         QString str = QString::fromLatin1("QKeySequence::mnemonic: \"%1\" contains multiple occurrences of '&'").arg(string);
430         QTest::ignoreMessage(QtWarningMsg, qPrintable(str));
431     //    qWarning(qPrintable(str));
432     }
433 #endif
434     QKeySequence seq = QKeySequence::mnemonic(string);
435     QKeySequence res = QKeySequence(key);
436
437     QCOMPARE(seq, res);
438 }
439
440 void tst_QKeySequence::toString_data()
441 {
442     QTest::addColumn<QString>("strSequence");
443     QTest::addColumn<QString>("neutralString");
444     QTest::addColumn<QString>("platformString");
445
446
447 #ifndef Q_OS_MAC
448     QTest::newRow("Ctrl+Left") << QString("Ctrl+Left") << QString("Ctrl+Left") << QString("Ctrl+Left");
449     QTest::newRow("Alt+Left") << QString("Alt+Left") << QString("Alt+Left") << QString("Alt+Left");
450     QTest::newRow("Alt+Shift+Left") << QString("Alt+Shift+Left") << QString("Alt+Shift+Left") << QString("Alt+Shift+Left");
451     QTest::newRow("Ctrl") << QString::fromLatin1("Ctrl+\x0c5") << QString::fromLatin1("Ctrl+\x0c5") << QString::fromLatin1("Ctrl+\x0c5");
452     QTest::newRow("Alt") << QString::fromLatin1("Alt+\x0c5") << QString::fromLatin1("Alt+\x0c5") << QString::fromLatin1("Alt+\x0c5");
453     QTest::newRow("Shift") << QString::fromLatin1("Shift+\x0c5") << QString::fromLatin1("Shift+\x0c5") << QString::fromLatin1("Shift+\x0c5");
454     QTest::newRow("Meta") << QString::fromLatin1("Meta+\x0c5") << QString::fromLatin1("Meta+\x0c5") << QString::fromLatin1("Meta+\x0c5");
455     QTest::newRow("Ctrl+Plus") << QString("Ctrl++") << QString("Ctrl++") << QString("Ctrl++");
456     QTest::newRow("Ctrl+,") << QString("Ctrl+,") << QString("Ctrl+,") << QString("Ctrl+,");
457     QTest::newRow("Ctrl+,,Ctrl+,") << QString("Ctrl+,,Ctrl+,") << QString("Ctrl+,, Ctrl+,") << QString("Ctrl+,, Ctrl+,");
458     QTest::newRow("MultiKey") << QString("Alt+X, Ctrl+Y, Z") << QString("Alt+X, Ctrl+Y, Z")
459                            << QString("Alt+X, Ctrl+Y, Z");
460
461     QTest::newRow("Invalid") << QString("Ctrly") << QString("") << QString("");
462 #else
463     /*
464     QTest::newRow("Ctrl+Left") << MacCtrl + "Left" << QString("Ctrl+Left") << MacCtrl + macSymbolForQtKey(Qt::Key_Left);
465     QTest::newRow("Alt+Left") << MacAlt + "Left" << QString("Alt+Left") << MacAlt + macSymbolForQtKey(Qt::Key_Left);
466     QTest::newRow("Alt+Shift+Left") << MacAlt + MacShift + "Left" << QString("Alt+Shift+Left")
467                                  << MacAlt + MacShift + macSymbolForQtKey(Qt::Key_Left);
468                                  */
469     QTest::newRow("Ctrl+Right,Left") << MacCtrl + "Right, Left" << QString("Ctrl+Right, Left") << MacCtrl + macSymbolForQtKey(Qt::Key_Right) + QString(", ") + macSymbolForQtKey(Qt::Key_Left);
470     QTest::newRow("Ctrl") << MacCtrl + QLatin1String("\x0c5") << QString::fromLatin1("Ctrl+\x0c5") << MacCtrl + QLatin1String("\x0c5");
471     QTest::newRow("Alt") << MacAlt + QLatin1String("\x0c5") << QString::fromLatin1("Alt+\x0c5") << MacAlt + QLatin1String("\x0c5");
472     QTest::newRow("Shift") << MacShift + QLatin1String("\x0c5") << QString::fromLatin1("Shift+\x0c5") << MacShift + QLatin1String("\x0c5");
473     QTest::newRow("Meta") << MacMeta + QLatin1String("\x0c5") << QString::fromLatin1("Meta+\x0c5") << MacMeta + QLatin1String("\x0c5");
474     QTest::newRow("Ctrl+Plus") << MacCtrl + "+" << QString("Ctrl++") << MacCtrl + "+";
475     QTest::newRow("Ctrl+,") << MacCtrl + "," << QString("Ctrl+,") << MacCtrl + ",";
476     QTest::newRow("Ctrl+,,Ctrl+,") << MacCtrl + ",, " + MacCtrl + "," << QString("Ctrl+,, Ctrl+,") << MacCtrl + ",, " + MacCtrl + ",";
477     QTest::newRow("MultiKey") << MacAlt + "X, " + MacCtrl + "Y, Z" << QString("Alt+X, Ctrl+Y, Z")
478                            << MacAlt + "X, " + MacCtrl + "Y, Z";
479     QTest::newRow("Invalid") << QString("Ctrly") << QString("") << QString("");
480 #endif
481 }
482
483 void tst_QKeySequence::toString()
484 {
485     QFETCH(QString, strSequence);
486     QFETCH(QString, neutralString);
487     QFETCH(QString, platformString);
488
489     QKeySequence ks1(strSequence);
490
491     QCOMPARE(ks1.toString(QKeySequence::NativeText), platformString);
492     QCOMPARE(ks1.toString(QKeySequence::PortableText), neutralString);
493
494 }
495
496 void tst_QKeySequence::toStringFromKeycode_data()
497 {
498     QTest::addColumn<QKeySequence>("keycode");
499     QTest::addColumn<QString>("expectedString");
500
501     QTest::newRow("A") << QKeySequence(Qt::Key_A) << "A";
502     QTest::newRow("-1") << QKeySequence(-1) << "";
503     QTest::newRow("Unknown") << QKeySequence(Qt::Key_unknown) << "";
504 }
505
506 void tst_QKeySequence::toStringFromKeycode()
507 {
508     QFETCH(QKeySequence, keycode);
509     QFETCH(QString, expectedString);
510
511     QCOMPARE(QKeySequence(keycode).toString(), expectedString);
512 }
513
514 void tst_QKeySequence::streamOperators_data()
515 {
516         operatorQString_data();
517 }
518
519 void tst_QKeySequence::streamOperators()
520 {
521     QFETCH( int, modifiers );
522     QFETCH( int, keycode );
523
524         QByteArray data;
525         QKeySequence refK( modifiers | keycode );
526         QKeySequence orgK( "Ctrl+A" );
527         QKeySequence copyOrgK = orgK;
528         QVERIFY( copyOrgK == orgK );
529
530         QDataStream in(&data, QIODevice::WriteOnly);
531         in << refK;
532         QDataStream out(&data, QIODevice::ReadOnly);
533         out >> orgK;
534
535         QVERIFY( orgK == refK );
536
537         // check if detached
538         QVERIFY( orgK != copyOrgK );
539 }
540
541
542 void tst_QKeySequence::parseString_data()
543 {
544     QTest::addColumn<QString>("strSequence");
545     QTest::addColumn<QKeySequence>("keycode");
546
547     // Valid
548     QTest::newRow("A") << "A" << QKeySequence(Qt::Key_A);
549     QTest::newRow("a") << "a" << QKeySequence(Qt::Key_A);
550     QTest::newRow("Ctrl+Left") << "Ctrl+Left" << QKeySequence(Qt::CTRL + Qt::Key_Left);
551     QTest::newRow("CTRL+LEFT") << "CTRL+LEFT" << QKeySequence(Qt::CTRL + Qt::Key_Left);
552     QTest::newRow("Meta+A") << "Meta+a" <<  QKeySequence(Qt::META + Qt::Key_A);
553     QTest::newRow("mEtA+A") << "mEtA+a" <<  QKeySequence(Qt::META + Qt::Key_A);
554     QTest::newRow("Ctrl++") << "Ctrl++" << QKeySequence(Qt::CTRL + Qt::Key_Plus);
555
556     // Invalid modifiers
557     QTest::newRow("Win+A") << "Win+a" <<  QKeySequence(Qt::Key_unknown);
558     QTest::newRow("Super+Meta+A") << "Super+Meta+A" << QKeySequence(Qt::Key_unknown);
559
560     // Invalid Keys
561     QTest::newRow("Meta+Trolls") << "Meta+Trolls" << QKeySequence(Qt::Key_unknown);
562     QTest::newRow("Meta+Period") << "Meta+Period" << QKeySequence(Qt::Key_unknown);
563     QTest::newRow("Meta+Ypsilon") << "Meta+Ypsilon" << QKeySequence(Qt::Key_unknown);
564
565     // Garbage
566     QTest::newRow("4+3=2") << "4+3=2" <<  QKeySequence(Qt::Key_unknown);
567     QTest::newRow("Alabama") << "Alabama" << QKeySequence(Qt::Key_unknown);
568     QTest::newRow("Simon+G") << "Simon+G" << QKeySequence(Qt::Key_unknown);
569     QTest::newRow("Shift+++2") << "Shift+++2" <<  QKeySequence(Qt::Key_unknown);
570
571     // Wrong order
572     QTest::newRow("A+Meta") << "a+Meta" <<  QKeySequence(Qt::Key_unknown);
573     QTest::newRow("Meta+++Shift") << "Meta+++Shift" <<  QKeySequence(Qt::Key_unknown);
574     QTest::newRow("Meta+a+Shift") << "Meta+a+Shift" <<  QKeySequence(Qt::Key_unknown);
575
576     // Only Modifiers - currently not supported
577     //QTest::newRow("Meta+Shift") << "Meta+Shift" << QKeySequence(Qt::META + Qt::SHIFT);
578     //QTest::newRow("Ctrl") << "Ctrl" << QKeySequence(Qt::CTRL);
579     //QTest::newRow("Shift") << "Shift" << QKeySequence(Qt::SHIFT);
580
581     // Only Keys
582     QTest::newRow("a") << "a" << QKeySequence(Qt::Key_A);
583     QTest::newRow("A") << "A" << QKeySequence(Qt::Key_A);
584
585     // Incomplete
586     QTest::newRow("Meta+Shift+") << "Meta+Shift+" << QKeySequence(Qt::Key_unknown);
587 }
588
589 void tst_QKeySequence::parseString()
590 {
591     QFETCH( QString, strSequence );
592     QFETCH( QKeySequence, keycode );
593
594 #ifdef Q_OS_MAC
595     QEXPECT_FAIL("Win+A", "QTBUG-24406 - This test fails on OSX", Abort);
596     QEXPECT_FAIL("Simon+G", "QTBUG-24406 - This test fails on OSX", Abort);
597 #endif
598
599     QCOMPARE( QKeySequence(strSequence).toString(), keycode.toString() );
600     QVERIFY( QKeySequence(strSequence) == keycode );
601 }
602
603 void tst_QKeySequence::fromString_data()
604 {
605     toString_data();
606 }
607
608 void tst_QKeySequence::fromString()
609 {
610     QFETCH(QString, strSequence);
611     QFETCH(QString, neutralString);
612     QFETCH(QString, platformString);
613
614     if (strSequence == "Ctrly") // Key_Unknown gives empty string
615         return;
616
617     QKeySequence ks1(strSequence);
618     QKeySequence ks2 = QKeySequence::fromString(ks1.toString());
619     QKeySequence ks3 = QKeySequence::fromString(neutralString, QKeySequence::PortableText);
620     QKeySequence ks4 = QKeySequence::fromString(platformString, QKeySequence::NativeText);
621
622
623     // assume the transitive property exists here.
624     QCOMPARE(ks2, ks1);
625     QCOMPARE(ks3, ks1);
626     QCOMPARE(ks4, ks1);
627 }
628
629 void tst_QKeySequence::translated_data()
630 {
631     qApp->installTranslator(ourTranslator);
632     qApp->installTranslator(qtTranslator);
633
634     QTest::addColumn<QString>("transKey");
635     QTest::addColumn<QString>("compKey");
636
637     QTest::newRow("Shift++") << tr("Shift++") << QString("Umschalt++");
638     QTest::newRow("Ctrl++")  << tr("Ctrl++") << QString("Strg++");
639     QTest::newRow("Alt++")   << tr("Alt++") << QString("Alt++");
640     QTest::newRow("Meta++")  << tr("Meta++") << QString("Meta++");
641
642     QTest::newRow("Shift+,, Shift++") << tr("Shift+,, Shift++") << QString("Umschalt+,, Umschalt++");
643     QTest::newRow("Shift+,, Ctrl++")  << tr("Shift+,, Ctrl++") << QString("Umschalt+,, Strg++");
644     QTest::newRow("Shift+,, Alt++")   << tr("Shift+,, Alt++") << QString("Umschalt+,, Alt++");
645     QTest::newRow("Shift+,, Meta++")  << tr("Shift+,, Meta++") << QString("Umschalt+,, Meta++");
646
647     QTest::newRow("Ctrl+,, Shift++") << tr("Ctrl+,, Shift++") << QString("Strg+,, Umschalt++");
648     QTest::newRow("Ctrl+,, Ctrl++")  << tr("Ctrl+,, Ctrl++") << QString("Strg+,, Strg++");
649     QTest::newRow("Ctrl+,, Alt++")   << tr("Ctrl+,, Alt++") << QString("Strg+,, Alt++");
650     QTest::newRow("Ctrl+,, Meta++")  << tr("Ctrl+,, Meta++") << QString("Strg+,, Meta++");
651
652     qApp->removeTranslator(ourTranslator);
653     qApp->removeTranslator(qtTranslator);
654 }
655
656 void tst_QKeySequence::translated()
657 {
658     QFETCH(QString, transKey);
659     QFETCH(QString, compKey);
660 #ifdef Q_OS_MAC
661     QSKIP("No need to translate modifiers on Mac OS X");
662 #elif defined(Q_OS_WINCE)
663     QSKIP("No need to translate modifiers on WinCE");
664 #endif
665
666     qApp->installTranslator(ourTranslator);
667     qApp->installTranslator(qtTranslator);
668
669     QKeySequence ks1(transKey);
670     QCOMPARE(ks1.toString(QKeySequence::NativeText), compKey);
671
672     qApp->removeTranslator(ourTranslator);
673     qApp->removeTranslator(qtTranslator);
674 }
675
676
677 void tst_QKeySequence::i18nKeys_data()
678 {
679     QTest::addColumn<int>("keycode");
680     QTest::addColumn<QString>("keystring");
681
682     // Japanese keyboard support
683     QTest::newRow("Kanji") << (int)Qt::Key_Kanji << QString("Kanji");
684     QTest::newRow("Muhenkan") << (int)Qt::Key_Muhenkan << QString("Muhenkan");
685     QTest::newRow("Henkan") << (int)Qt::Key_Henkan << QString("Henkan");
686     QTest::newRow("Romaji") << (int)Qt::Key_Romaji << QString("Romaji");
687     QTest::newRow("Hiragana") << (int)Qt::Key_Hiragana << QString("Hiragana");
688     QTest::newRow("Katakana") << (int)Qt::Key_Katakana << QString("Katakana");
689     QTest::newRow("Hiragana Katakana") << (int)Qt::Key_Hiragana_Katakana << QString("Hiragana Katakana");
690     QTest::newRow("Zenkaku") << (int)Qt::Key_Zenkaku << QString("Zenkaku");
691     QTest::newRow("Hankaku") << (int)Qt::Key_Hankaku << QString("Hankaku");
692     QTest::newRow("Zenkaku Hankaku") << (int)Qt::Key_Zenkaku_Hankaku << QString("Zenkaku Hankaku");
693     QTest::newRow("Touroku") << (int)Qt::Key_Touroku << QString("Touroku");
694     QTest::newRow("Massyo") << (int)Qt::Key_Massyo << QString("Massyo");
695     QTest::newRow("Kana Lock") << (int)Qt::Key_Kana_Lock << QString("Kana Lock");
696     QTest::newRow("Kana Shift") << (int)Qt::Key_Kana_Shift << QString("Kana Shift");
697     QTest::newRow("Eisu Shift") << (int)Qt::Key_Eisu_Shift << QString("Eisu Shift");
698     QTest::newRow("Eisu_toggle") << (int)Qt::Key_Eisu_toggle << QString("Eisu toggle");
699     QTest::newRow("Code input") << (int)Qt::Key_Codeinput << QString("Code input");
700     QTest::newRow("Multiple Candidate") << (int)Qt::Key_MultipleCandidate << QString("Multiple Candidate");
701     QTest::newRow("Previous Candidate") << (int)Qt::Key_PreviousCandidate << QString("Previous Candidate");
702
703     // Korean keyboard support
704     QTest::newRow("Hangul") << (int)Qt::Key_Hangul << QString("Hangul");
705     QTest::newRow("Hangul Start") << (int)Qt::Key_Hangul_Start << QString("Hangul Start");
706     QTest::newRow("Hangul End") << (int)Qt::Key_Hangul_End << QString("Hangul End");
707     QTest::newRow("Hangul Hanja") << (int)Qt::Key_Hangul_Hanja << QString("Hangul Hanja");
708     QTest::newRow("Hangul Jamo") << (int)Qt::Key_Hangul_Jamo << QString("Hangul Jamo");
709     QTest::newRow("Hangul Romaja") << (int)Qt::Key_Hangul_Romaja << QString("Hangul Romaja");
710     QTest::newRow("Hangul Jeonja") << (int)Qt::Key_Hangul_Jeonja << QString("Hangul Jeonja");
711     QTest::newRow("Hangul Banja") << (int)Qt::Key_Hangul_Banja << QString("Hangul Banja");
712     QTest::newRow("Hangul PreHanja") << (int)Qt::Key_Hangul_PreHanja << QString("Hangul PreHanja");
713     QTest::newRow("Hangul PostHanja") << (int)Qt::Key_Hangul_PostHanja << QString("Hangul PostHanja");
714     QTest::newRow("Hangul Special") << (int)Qt::Key_Hangul_Special << QString("Hangul Special");
715 }
716
717 void tst_QKeySequence::i18nKeys()
718 {
719     QFETCH(int, keycode);
720     QFETCH(QString, keystring);
721     QKeySequence seq(keycode);
722
723     QCOMPARE(seq, QKeySequence(keystring));
724     QCOMPARE(seq.toString(), keystring);
725 }
726
727 QTEST_MAIN(tst_QKeySequence)
728 #include "tst_qkeysequence.moc"