Test: remove QSKIP and remove whole test cases instead.
[profile/ivi/qtbase.git] / tests / auto / gui / kernel / qkeysequence / tst_qkeysequence.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
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 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.
24 **
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.
28 **
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.
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 #ifndef Q_OS_MAC
123     void mnemonic_data();
124     void mnemonic();
125 #endif
126     void toString_data();
127     void toString();
128     void toStringFromKeycode_data();
129     void toStringFromKeycode();
130     void streamOperators_data();
131     void streamOperators();
132     void parseString_data();
133     void parseString();
134     void fromString_data();
135     void fromString();
136 #ifdef QT_BUILD_INTERNAL
137     void ensureSorted();
138 #endif
139     void standardKeys_data();
140     void standardKeys();
141     void keyBindings();
142 #if !defined (Q_OS_MAC) && !defined (Q_OS_WINCE)
143     void translated_data();
144     void translated();
145 #endif
146     void i18nKeys_data();
147     void i18nKeys();
148
149
150     void initTestCase();
151 private:
152     int m_keyboardScheme;
153     QTranslator *ourTranslator;
154     QTranslator *qtTranslator;
155 #ifdef Q_OS_MAC
156     static const QString MacCtrl;
157     static const QString MacMeta;
158     static const QString MacAlt;
159     static const QString MacShift;
160 #endif
161
162
163 };
164
165 #ifdef Q_OS_MAC
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));
170 #endif
171
172 tst_QKeySequence::tst_QKeySequence() : m_keyboardScheme(QPlatformTheme::WindowsKeyboardScheme)
173 {
174     if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
175         m_keyboardScheme = theme->themeHint(QPlatformTheme::KeyboardScheme).toInt();
176 }
177
178 tst_QKeySequence::~tst_QKeySequence()
179 {
180
181 }
182
183 void tst_QKeySequence::initTestCase()
184 {
185     ourTranslator = new QTranslator(this);
186     ourTranslator->load(":/keys_de");
187     qtTranslator = new QTranslator(this);
188     qtTranslator->load(":/qt_de");
189 }
190
191 void tst_QKeySequence::swap()
192 {
193     QKeySequence ks1(Qt::CTRL+Qt::Key_O);
194     QKeySequence ks2(Qt::CTRL+Qt::Key_L);
195     ks1.swap(ks2);
196     QCOMPARE(ks1[0], int(Qt::CTRL+Qt::Key_L));
197     QCOMPARE(ks2[0], int(Qt::CTRL+Qt::Key_O));
198 }
199
200 void tst_QKeySequence::operatorQString_data()
201 {
202     QTest::addColumn<int>("modifiers");
203     QTest::addColumn<int>("keycode");
204     QTest::addColumn<QString>("keystring");
205
206     QTest::newRow( "No modifier" ) << 0 << int(Qt::Key_Aring | Qt::UNICODE_ACCEL) << QString::fromLatin1( "\x0c5" );
207
208 #ifndef Q_OS_MAC
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" );
217 #else
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");
226 #endif
227 }
228
229 void tst_QKeySequence::symetricConstructors_data()
230 {
231     QTest::addColumn<int>("modifiers");
232     QTest::addColumn<int>("keycode");
233
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);
239 }
240
241 void tst_QKeySequence::compareConstructors_data()
242 {
243     operatorQString_data();
244 }
245
246 // operator QString()
247 void tst_QKeySequence::operatorQString()
248 {
249     QKeySequence seq;
250     QFETCH( int, modifiers );
251     QFETCH( int, keycode );
252     QFETCH( QString, keystring );
253
254     seq = QKeySequence( modifiers | keycode );
255
256     QCOMPARE( (QString)seq, keystring );
257 }
258
259 // this verifies that the constructors can handle the same strings in and out
260 void tst_QKeySequence::symetricConstructors()
261 {
262     QFETCH( int, modifiers );
263     QFETCH( int, keycode );
264
265     QKeySequence seq1( modifiers | keycode );
266     QKeySequence seq2( (QString)seq1 );
267
268     QVERIFY( seq1 == seq2 );
269 }
270
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()
274 {
275     QFETCH( int, modifiers );
276     QFETCH( int, keycode );
277     QFETCH( QString, keystring );
278
279     QKeySequence qstringSeq( keystring );
280     QKeySequence intSeq( modifiers | keycode );
281
282     QVERIFY( qstringSeq == intSeq );
283 }
284
285 void tst_QKeySequence::checkMultipleNames()
286 {
287     QKeySequence oldK( "Ctrl+Page Up" );
288     QKeySequence newK( "Ctrl+PgUp" );
289     QVERIFY( oldK == newK );
290 }
291
292 //TODO: could test third constructor, or test fromString on all constructor-data
293 void tst_QKeySequence::checkMultipleCodes()
294 {
295     QKeySequence seq1("Alt+d, l");
296     QKeySequence seq2 = QKeySequence::fromString("Alt+d, l");
297     QVERIFY( seq1 == seq2 );
298
299     QKeySequence seq3("Alt+d,l");
300     QKeySequence seq4 = QKeySequence::fromString("Alt+d,l");
301     QVERIFY( seq3 == seq4 );
302 }
303
304 /*
305 * We must ensure that the keyBindings data is always sorted
306 * so that we can safely perform binary searches.
307 */
308 #ifdef QT_BUILD_INTERNAL
309 void tst_QKeySequence::ensureSorted()
310 {
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;
315         if (nextval < val)
316             qDebug() << "Data not sorted at index " << i;
317         QVERIFY(nextval >= val);
318         val = nextval;
319     }
320 }
321 #endif
322
323 void tst_QKeySequence::standardKeys_data()
324 {
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");
340     }
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");
350
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
359 #else
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
367 #endif
368 }
369
370 void tst_QKeySequence::standardKeys()
371 {
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)));
379 }
380
381 void tst_QKeySequence::keyBindings()
382 {
383     const QList<QKeySequence> bindings =
384           QKeySequence::keyBindings(QKeySequence::Copy);
385
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:
391         expected  << ctrlC;
392         break;
393     case QPlatformTheme::WindowsKeyboardScheme:
394         expected  << ctrlC << ctrlInsert;
395         break;
396     default: // X11
397         expected  << ctrlC << QKeySequence(QStringLiteral("F16")) << ctrlInsert;
398         break;
399     }
400     QCOMPARE(bindings, expected);
401 }
402
403 #ifndef Q_OS_MAC
404 void tst_QKeySequence::mnemonic_data()
405 {
406     QTest::addColumn<QString>("string");
407     QTest::addColumn<QString>("key");
408     QTest::addColumn<bool>("warning");
409
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;
421 }
422
423 void tst_QKeySequence::mnemonic()
424 {
425     QFETCH(QString, string);
426     QFETCH(QString, key);
427     QFETCH(bool, warning);
428
429 #ifdef QT_NO_DEBUG
430     Q_UNUSED(warning)
431 #else
432     if (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));
436     }
437 #endif
438     QKeySequence seq = QKeySequence::mnemonic(string);
439     QKeySequence res = QKeySequence(key);
440
441     QCOMPARE(seq, res);
442 }
443 #endif
444
445 void tst_QKeySequence::toString_data()
446 {
447     QTest::addColumn<QString>("strSequence");
448     QTest::addColumn<QString>("neutralString");
449     QTest::addColumn<QString>("platformString");
450
451
452 #ifndef Q_OS_MAC
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");
465
466     QTest::newRow("Invalid") << QString("Ctrly") << QString("") << QString("");
467 #else
468     /*
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);
473                                  */
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("");
485 #endif
486 }
487
488 void tst_QKeySequence::toString()
489 {
490     QFETCH(QString, strSequence);
491     QFETCH(QString, neutralString);
492     QFETCH(QString, platformString);
493
494     QKeySequence ks1(strSequence);
495
496     QCOMPARE(ks1.toString(QKeySequence::NativeText), platformString);
497     QCOMPARE(ks1.toString(QKeySequence::PortableText), neutralString);
498
499 }
500
501 void tst_QKeySequence::toStringFromKeycode_data()
502 {
503     QTest::addColumn<QKeySequence>("keycode");
504     QTest::addColumn<QString>("expectedString");
505
506     QTest::newRow("A") << QKeySequence(Qt::Key_A) << "A";
507     QTest::newRow("-1") << QKeySequence(-1) << "";
508     QTest::newRow("Unknown") << QKeySequence(Qt::Key_unknown) << "";
509 }
510
511 void tst_QKeySequence::toStringFromKeycode()
512 {
513     QFETCH(QKeySequence, keycode);
514     QFETCH(QString, expectedString);
515
516     QCOMPARE(QKeySequence(keycode).toString(), expectedString);
517 }
518
519 void tst_QKeySequence::streamOperators_data()
520 {
521         operatorQString_data();
522 }
523
524 void tst_QKeySequence::streamOperators()
525 {
526     QFETCH( int, modifiers );
527     QFETCH( int, keycode );
528
529         QByteArray data;
530         QKeySequence refK( modifiers | keycode );
531         QKeySequence orgK( "Ctrl+A" );
532         QKeySequence copyOrgK = orgK;
533         QVERIFY( copyOrgK == orgK );
534
535         QDataStream in(&data, QIODevice::WriteOnly);
536         in << refK;
537         QDataStream out(&data, QIODevice::ReadOnly);
538         out >> orgK;
539
540         QVERIFY( orgK == refK );
541
542         // check if detached
543         QVERIFY( orgK != copyOrgK );
544 }
545
546
547 void tst_QKeySequence::parseString_data()
548 {
549     QTest::addColumn<QString>("strSequence");
550     QTest::addColumn<QKeySequence>("keycode");
551
552     // Valid
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);
560
561     // Invalid modifiers
562     QTest::newRow("Win+A") << "Win+a" <<  QKeySequence(Qt::Key_unknown);
563     QTest::newRow("Super+Meta+A") << "Super+Meta+A" << QKeySequence(Qt::Key_unknown);
564
565     // Invalid Keys
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);
569
570     // Garbage
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);
575
576     // Wrong order
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);
580
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);
585
586     // Only Keys
587     QTest::newRow("a") << "a" << QKeySequence(Qt::Key_A);
588     QTest::newRow("A") << "A" << QKeySequence(Qt::Key_A);
589
590     // Incomplete
591     QTest::newRow("Meta+Shift+") << "Meta+Shift+" << QKeySequence(Qt::Key_unknown);
592 }
593
594 void tst_QKeySequence::parseString()
595 {
596     QFETCH( QString, strSequence );
597     QFETCH( QKeySequence, keycode );
598
599 #ifdef Q_OS_MAC
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);
602 #endif
603
604     QCOMPARE( QKeySequence(strSequence).toString(), keycode.toString() );
605     QVERIFY( QKeySequence(strSequence) == keycode );
606 }
607
608 void tst_QKeySequence::fromString_data()
609 {
610     toString_data();
611 }
612
613 void tst_QKeySequence::fromString()
614 {
615     QFETCH(QString, strSequence);
616     QFETCH(QString, neutralString);
617     QFETCH(QString, platformString);
618
619     if (strSequence == "Ctrly") // Key_Unknown gives empty string
620         return;
621
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);
626
627
628     // assume the transitive property exists here.
629     QCOMPARE(ks2, ks1);
630     QCOMPARE(ks3, ks1);
631     QCOMPARE(ks4, ks1);
632 }
633
634 #if !defined (Q_OS_MAC) && !defined (Q_OS_WINCE)
635 void tst_QKeySequence::translated_data()
636 {
637     qApp->installTranslator(ourTranslator);
638     qApp->installTranslator(qtTranslator);
639
640     QTest::addColumn<QString>("transKey");
641     QTest::addColumn<QString>("compKey");
642
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++");
647
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++");
652
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++");
657
658     qApp->removeTranslator(ourTranslator);
659     qApp->removeTranslator(qtTranslator);
660 }
661
662 void tst_QKeySequence::translated()
663 {
664     QFETCH(QString, transKey);
665     QFETCH(QString, compKey);
666
667     qApp->installTranslator(ourTranslator);
668     qApp->installTranslator(qtTranslator);
669
670     QKeySequence ks1(transKey);
671     QCOMPARE(ks1.toString(QKeySequence::NativeText), compKey);
672
673     qApp->removeTranslator(ourTranslator);
674     qApp->removeTranslator(qtTranslator);
675 }
676 #endif
677
678 void tst_QKeySequence::i18nKeys_data()
679 {
680     QTest::addColumn<int>("keycode");
681     QTest::addColumn<QString>("keystring");
682
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");
703
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");
716 }
717
718 void tst_QKeySequence::i18nKeys()
719 {
720     QFETCH(int, keycode);
721     QFETCH(QString, keystring);
722     QKeySequence seq(keycode);
723
724     QCOMPARE(seq, QKeySequence(keystring));
725     QCOMPARE(seq.toString(), keystring);
726 }
727
728 QTEST_MAIN(tst_QKeySequence)
729 #include "tst_qkeysequence.moc"