tests/auto/qclipboard/tst_qclipboard.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <QtCore/QDebug>
       
    45 #include <QtGui/QApplication>
       
    46 #include <QtGui/QClipboard>
       
    47 #ifdef Q_WS_MAC
       
    48 #include <Carbon/Carbon.h>
       
    49 #endif
       
    50 
       
    51 //TESTED_CLASS=
       
    52 //TESTED_FILES=
       
    53 
       
    54 class tst_QClipboard : public QObject
       
    55 {
       
    56     Q_OBJECT
       
    57 private slots:
       
    58 
       
    59     void copy_exit_paste();
       
    60     void capabiliyFunctions();
       
    61     void modes();
       
    62     void testSignals();
       
    63     void setMimeData();
       
    64     void clearBeforeSetText();
       
    65 
       
    66 private:
       
    67     bool nativeClipboardWorking();
       
    68 };
       
    69 
       
    70 
       
    71 bool tst_QClipboard::nativeClipboardWorking()
       
    72 {
       
    73 #ifdef Q_WS_MAC
       
    74     PasteboardRef pasteboard;
       
    75     OSStatus status = PasteboardCreate(0, &pasteboard);
       
    76     if (status == noErr)
       
    77         CFRelease(pasteboard);
       
    78     return status == noErr;
       
    79 #endif
       
    80     return true;
       
    81 }
       
    82 
       
    83 Q_DECLARE_METATYPE(QClipboard::Mode)
       
    84 
       
    85 /*
       
    86     Tests that the capability functions are implemented on all
       
    87     platforms.
       
    88 */
       
    89 void tst_QClipboard::capabiliyFunctions()
       
    90 {
       
    91     QClipboard * const clipboard =  QApplication::clipboard();
       
    92 
       
    93     clipboard->supportsSelection();
       
    94     clipboard->supportsFindBuffer();
       
    95     clipboard->ownsSelection();
       
    96     clipboard->ownsClipboard();
       
    97     clipboard->ownsFindBuffer();
       
    98 }
       
    99 
       
   100 /*
       
   101     Test that text inserted into the clipboard in different modes is
       
   102     kept separate.
       
   103 */
       
   104 void tst_QClipboard::modes()
       
   105 {
       
   106     QClipboard * const clipboard =  QApplication::clipboard();
       
   107 
       
   108     if (!nativeClipboardWorking())
       
   109         QSKIP("Native clipboard not working in this setup", SkipAll);
       
   110 
       
   111     const QString defaultMode = "default mode text;";
       
   112     clipboard->setText(defaultMode);
       
   113     QCOMPARE(clipboard->text(), defaultMode);
       
   114 
       
   115     if (clipboard->supportsSelection()) {
       
   116         const QString selectionMode = "selection mode text";
       
   117         clipboard->setText(selectionMode, QClipboard::Selection);
       
   118         QCOMPARE(clipboard->text(QClipboard::Selection), selectionMode);
       
   119         QCOMPARE(clipboard->text(), defaultMode);
       
   120     }
       
   121 
       
   122     if (clipboard->supportsFindBuffer()) {
       
   123         const QString searchMode = "find mode text";
       
   124         clipboard->setText(searchMode, QClipboard::FindBuffer);
       
   125         QCOMPARE(clipboard->text(QClipboard::FindBuffer), searchMode);
       
   126         QCOMPARE(clipboard->text(), defaultMode);
       
   127     }
       
   128 }
       
   129 
       
   130 /*
       
   131     Test that the apropriate signals are emitted when the cliboard
       
   132     contents is changed by calling the qt funcitons.
       
   133 */
       
   134 void tst_QClipboard::testSignals()
       
   135 {
       
   136     qRegisterMetaType<QClipboard::Mode>("QClipboard::Mode");
       
   137 
       
   138     if (!nativeClipboardWorking())
       
   139         QSKIP("Native clipboard not working in this setup", SkipAll);
       
   140 
       
   141     QClipboard * const clipboard =  QApplication::clipboard();
       
   142 
       
   143     QSignalSpy changedSpy(clipboard, SIGNAL(changed(QClipboard::Mode)));
       
   144     QSignalSpy dataChangedSpy(clipboard, SIGNAL(dataChanged()));
       
   145     QSignalSpy searchChangedSpy(clipboard, SIGNAL(findBufferChanged()));
       
   146     QSignalSpy selectionChangedSpy(clipboard, SIGNAL(selectionChanged()));
       
   147 
       
   148     const QString text = "clipboard text;";
       
   149 
       
   150     // Test the default mode signal.
       
   151     clipboard->setText(text);
       
   152     QCOMPARE(dataChangedSpy.count(), 1);
       
   153     QCOMPARE(searchChangedSpy.count(), 0);
       
   154     QCOMPARE(selectionChangedSpy.count(), 0);
       
   155     QCOMPARE(changedSpy.count(), 1);
       
   156     QCOMPARE(changedSpy.at(0).count(), 1);
       
   157     QCOMPARE(qVariantValue<QClipboard::Mode>(changedSpy.at(0).at(0)), QClipboard::Clipboard);
       
   158 
       
   159     changedSpy.clear();
       
   160 
       
   161     // Test the selction mode signal.
       
   162     if (clipboard->supportsSelection()) {
       
   163         clipboard->setText(text, QClipboard::Selection);
       
   164         QCOMPARE(selectionChangedSpy.count(), 1);
       
   165         QCOMPARE(changedSpy.count(), 1);
       
   166         QCOMPARE(changedSpy.at(0).count(), 1);
       
   167         QCOMPARE(qVariantValue<QClipboard::Mode>(changedSpy.at(0).at(0)), QClipboard::Selection);
       
   168     } else {
       
   169         QCOMPARE(selectionChangedSpy.count(), 0);
       
   170     }
       
   171     QCOMPARE(dataChangedSpy.count(), 1);
       
   172     QCOMPARE(searchChangedSpy.count(), 0);
       
   173 
       
   174     changedSpy.clear();
       
   175 
       
   176     // Test the search mode signal.
       
   177     if (clipboard->supportsFindBuffer()) {
       
   178         clipboard->setText(text, QClipboard::FindBuffer);
       
   179         QCOMPARE(searchChangedSpy.count(), 1);
       
   180         QCOMPARE(changedSpy.count(), 1);
       
   181         QCOMPARE(changedSpy.at(0).count(), 1);
       
   182         QCOMPARE(qVariantValue<QClipboard::Mode>(changedSpy.at(0).at(0)), QClipboard::FindBuffer);
       
   183     } else {
       
   184         QCOMPARE(searchChangedSpy.count(), 0);
       
   185     }
       
   186     QCOMPARE(dataChangedSpy.count(), 1);
       
   187 }
       
   188 
       
   189 /*
       
   190     Test that pasted text remain on the clipboard
       
   191     after a Qt application exits.
       
   192 */
       
   193 void tst_QClipboard::copy_exit_paste()
       
   194 {
       
   195 #ifndef QT_NO_PROCESS
       
   196 #if defined Q_WS_X11 || defined Q_WS_QWS
       
   197     QSKIP("This test does not make sense on X11 and embedded, copied data disappears from the clipboard when the application exits ", SkipAll);
       
   198     // ### It's still possible to test copy/paste - just keep the apps running
       
   199 #elif defined (Q_OS_SYMBIAN) && defined (Q_CC_NOKIAX86)
       
   200     QSKIP("emulator cannot launch multiple processes",SkipAll);
       
   201 #endif
       
   202     if (!nativeClipboardWorking())
       
   203         QSKIP("Native clipboard not working in this setup", SkipAll);
       
   204     const QStringList stringArgument = QStringList() << "Test string.";
       
   205     QCOMPARE(QProcess::execute("copier/copier", stringArgument), 0);
       
   206 #ifdef Q_WS_MAC
       
   207     // The Pasteboard needs a moment to breathe (at least on older Macs).
       
   208     QTest::qWait(100);
       
   209 #endif
       
   210     QCOMPARE(QProcess::execute("paster/paster", stringArgument), 0);
       
   211 #endif
       
   212 }
       
   213 
       
   214 void tst_QClipboard::setMimeData()
       
   215 {
       
   216     if (!nativeClipboardWorking())
       
   217         QSKIP("Native clipboard not working in this setup", SkipAll);
       
   218     QMimeData *mimeData = new QMimeData;
       
   219     const QString TestName(QLatin1String("tst_QClipboard::setMimeData() mimeData"));
       
   220     mimeData->setObjectName(TestName);
       
   221 #if defined(Q_OS_WINCE)
       
   222     // need to set text on CE
       
   223     mimeData->setText(QLatin1String("Qt/CE foo"));
       
   224 #endif
       
   225 
       
   226     QApplication::clipboard()->setMimeData(mimeData);
       
   227     QCOMPARE(QApplication::clipboard()->mimeData(), (const QMimeData *)mimeData);
       
   228     QCOMPARE(QApplication::clipboard()->mimeData()->objectName(), TestName);
       
   229 
       
   230     // set it to the same data again, it shouldn't delete mimeData (and crash as a result)
       
   231     QApplication::clipboard()->setMimeData(mimeData);
       
   232     QCOMPARE(QApplication::clipboard()->mimeData(), (const QMimeData *)mimeData);
       
   233     QCOMPARE(QApplication::clipboard()->mimeData()->objectName(), TestName);
       
   234     QApplication::clipboard()->clear();
       
   235     const QMimeData *appMimeData = QApplication::clipboard()->mimeData();
       
   236     QVERIFY(appMimeData != mimeData || appMimeData->objectName() != TestName);
       
   237 
       
   238     // check for crash when using the same mimedata object on several clipboards
       
   239     QMimeData *data = new QMimeData;
       
   240     data->setText("foo");
       
   241 
       
   242     QApplication::clipboard()->setMimeData(data, QClipboard::Clipboard);
       
   243     QApplication::clipboard()->setMimeData(data, QClipboard::Selection);
       
   244     QApplication::clipboard()->setMimeData(data, QClipboard::FindBuffer);
       
   245 
       
   246     QSignalSpy spySelection(QApplication::clipboard(), SIGNAL(selectionChanged()));
       
   247     QSignalSpy spyData(QApplication::clipboard(), SIGNAL(dataChanged()));
       
   248     QSignalSpy spyFindBuffer(QApplication::clipboard(), SIGNAL(findBufferChanged()));
       
   249 
       
   250     QApplication::clipboard()->clear(QClipboard::Clipboard);
       
   251     QApplication::clipboard()->clear(QClipboard::Selection); // used to crash on X11
       
   252     QApplication::clipboard()->clear(QClipboard::FindBuffer);
       
   253 
       
   254 #if defined(Q_WS_X11)
       
   255     QCOMPARE(spySelection.count(), 1);
       
   256     QCOMPARE(spyData.count(), 1);
       
   257     QCOMPARE(spyFindBuffer.count(), 0);
       
   258 #elif defined(Q_WS_MAC)
       
   259     QCOMPARE(spySelection.count(), 0);
       
   260     QCOMPARE(spyData.count(), 1);
       
   261     QCOMPARE(spyFindBuffer.count(), 1);
       
   262 #elif defined(Q_WS_WIN)
       
   263     QCOMPARE(spySelection.count(), 0);
       
   264     QCOMPARE(spyData.count(), 1);
       
   265     QCOMPARE(spyFindBuffer.count(), 0);
       
   266 #endif
       
   267 
       
   268     // an other crash test
       
   269     data = new QMimeData;
       
   270     data->setText("foo");
       
   271 
       
   272     QApplication::clipboard()->setMimeData(data, QClipboard::Clipboard);
       
   273     QApplication::clipboard()->setMimeData(data, QClipboard::Selection);
       
   274     QApplication::clipboard()->setMimeData(data, QClipboard::FindBuffer);
       
   275 
       
   276     QMimeData *newData = new QMimeData;
       
   277     newData->setText("bar");
       
   278 
       
   279     spySelection.clear();
       
   280     spyData.clear();
       
   281     spyFindBuffer.clear();
       
   282 
       
   283     QApplication::clipboard()->setMimeData(newData, QClipboard::Clipboard);
       
   284     QApplication::clipboard()->setMimeData(newData, QClipboard::Selection); // used to crash on X11
       
   285     QApplication::clipboard()->setMimeData(newData, QClipboard::FindBuffer);
       
   286 
       
   287 #if defined(Q_WS_X11)
       
   288     QCOMPARE(spySelection.count(), 1);
       
   289     QCOMPARE(spyData.count(), 1);
       
   290     QCOMPARE(spyFindBuffer.count(), 0);
       
   291 #elif defined(Q_WS_MAC)
       
   292     QCOMPARE(spySelection.count(), 0);
       
   293     QCOMPARE(spyData.count(), 1);
       
   294     QCOMPARE(spyFindBuffer.count(), 1);
       
   295 #elif defined(Q_WS_WIN)
       
   296     QCOMPARE(spySelection.count(), 0);
       
   297     QCOMPARE(spyData.count(), 1);
       
   298     QCOMPARE(spyFindBuffer.count(), 0);
       
   299 #endif
       
   300 }
       
   301 
       
   302 void tst_QClipboard::clearBeforeSetText()
       
   303 {
       
   304     QApplication::processEvents();
       
   305 
       
   306     if (!nativeClipboardWorking())
       
   307         QSKIP("Native clipboard not working in this setup", SkipAll);
       
   308 
       
   309     const QString text = "tst_QClipboard::clearBeforeSetText()";
       
   310 
       
   311     // setText() should work after processEvents()
       
   312     QApplication::clipboard()->setText(text);
       
   313     QCOMPARE(QApplication::clipboard()->text(), text);
       
   314     QApplication::processEvents();
       
   315     QCOMPARE(QApplication::clipboard()->text(), text);
       
   316 
       
   317     // same with clear()
       
   318     QApplication::clipboard()->clear();
       
   319     QVERIFY(QApplication::clipboard()->text().isEmpty());
       
   320     QApplication::processEvents();
       
   321     QVERIFY(QApplication::clipboard()->text().isEmpty());
       
   322 
       
   323     // setText() again
       
   324     QApplication::clipboard()->setText(text);
       
   325     QCOMPARE(QApplication::clipboard()->text(), text);
       
   326     QApplication::processEvents();
       
   327     QCOMPARE(QApplication::clipboard()->text(), text);
       
   328 
       
   329     // clear() immediately followed by setText() should still return the text
       
   330     QApplication::clipboard()->clear();
       
   331     QVERIFY(QApplication::clipboard()->text().isEmpty());
       
   332     QApplication::clipboard()->setText(text);
       
   333     QCOMPARE(QApplication::clipboard()->text(), text);
       
   334     QApplication::processEvents();
       
   335     QCOMPARE(QApplication::clipboard()->text(), text);
       
   336 }
       
   337 
       
   338 QTEST_MAIN(tst_QClipboard)
       
   339 
       
   340 #include "tst_qclipboard.moc"