/* Test that the appropriate signals are emitted when the clipboard contents is changed by calling the qt functions. */ void tst_QClipboard::testSignals() { qRegisterMetaType<QClipboard::Mode>("QClipboard::Mode"); if (!PlatformClipboard::isAvailable()) QSKIP("Native clipboard not working in this setup"); QClipboard * const clipboard = QGuiApplication::clipboard(); QSignalSpy changedSpy(clipboard, SIGNAL(changed(QClipboard::Mode))); QSignalSpy dataChangedSpy(clipboard, SIGNAL(dataChanged())); QSignalSpy searchChangedSpy(clipboard, SIGNAL(findBufferChanged())); QSignalSpy selectionChangedSpy(clipboard, SIGNAL(selectionChanged())); const QString text = "clipboard text;"; // Test the default mode signal. clipboard->setText(text); QCOMPARE(dataChangedSpy.count(), 1); QCOMPARE(searchChangedSpy.count(), 0); QCOMPARE(selectionChangedSpy.count(), 0); QCOMPARE(changedSpy.count(), 1); QCOMPARE(changedSpy.at(0).count(), 1); QCOMPARE(qvariant_cast<QClipboard::Mode>(changedSpy.at(0).at(0)), QClipboard::Clipboard); changedSpy.clear(); // Test the selection mode signal. if (clipboard->supportsSelection()) { clipboard->setText(text, QClipboard::Selection); QCOMPARE(selectionChangedSpy.count(), 1); QCOMPARE(changedSpy.count(), 1); QCOMPARE(changedSpy.at(0).count(), 1); QCOMPARE(qvariant_cast<QClipboard::Mode>(changedSpy.at(0).at(0)), QClipboard::Selection); } else { QCOMPARE(selectionChangedSpy.count(), 0); } QCOMPARE(dataChangedSpy.count(), 1); QCOMPARE(searchChangedSpy.count(), 0); changedSpy.clear(); // Test the search mode signal. if (clipboard->supportsFindBuffer()) { clipboard->setText(text, QClipboard::FindBuffer); QCOMPARE(searchChangedSpy.count(), 1); QCOMPARE(changedSpy.count(), 1); QCOMPARE(changedSpy.at(0).count(), 1); QCOMPARE(qvariant_cast<QClipboard::Mode>(changedSpy.at(0).at(0)), QClipboard::FindBuffer); } else { QCOMPARE(searchChangedSpy.count(), 0); } QCOMPARE(dataChangedSpy.count(), 1); }
/*! \internal Emits the appropriate changed signal for \a mode. */ void QClipboard::emitChanged(Mode mode) { switch (mode) { case Clipboard: emit dataChanged(); break; case Selection: emit selectionChanged(); break; case FindBuffer: emit findBufferChanged(); break; default: break; emit changed(mode); } }
int QClipboard::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QObject::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { switch (_id) { case 0: changed((*reinterpret_cast< QClipboard::Mode(*)>(_a[1]))); break; case 1: selectionChanged(); break; case 2: findBufferChanged(); break; case 3: dataChanged(); break; case 4: ownerDestroyed(); break; } _id -= 5; } return _id; }
void tst_QClipboard::setMimeData() { if (!PlatformClipboard::isAvailable()) QSKIP("Native clipboard not working in this setup"); QMimeData *mimeData = new QMimeData; const QString TestName(QLatin1String("tst_QClipboard::setMimeData() mimeData")); mimeData->setObjectName(TestName); #if defined(Q_OS_WINCE) // need to set text on CE mimeData->setText(QLatin1String("Qt/CE foo")); #endif QGuiApplication::clipboard()->setMimeData(mimeData); QCOMPARE(QGuiApplication::clipboard()->mimeData(), (const QMimeData *)mimeData); QCOMPARE(QGuiApplication::clipboard()->mimeData()->objectName(), TestName); // set it to the same data again, it shouldn't delete mimeData (and crash as a result) QGuiApplication::clipboard()->setMimeData(mimeData); QCOMPARE(QGuiApplication::clipboard()->mimeData(), (const QMimeData *)mimeData); QCOMPARE(QGuiApplication::clipboard()->mimeData()->objectName(), TestName); QGuiApplication::clipboard()->clear(); const QMimeData *appMimeData = QGuiApplication::clipboard()->mimeData(); QVERIFY(appMimeData != mimeData || appMimeData->objectName() != TestName); // check for crash when using the same mimedata object on several clipboards QMimeData *data = new QMimeData; data->setText("foo"); QGuiApplication::clipboard()->setMimeData(data, QClipboard::Clipboard); QGuiApplication::clipboard()->setMimeData(data, QClipboard::Selection); QGuiApplication::clipboard()->setMimeData(data, QClipboard::FindBuffer); QSignalSpy spySelection(QGuiApplication::clipboard(), SIGNAL(selectionChanged())); QSignalSpy spyData(QGuiApplication::clipboard(), SIGNAL(dataChanged())); QSignalSpy spyFindBuffer(QGuiApplication::clipboard(), SIGNAL(findBufferChanged())); QGuiApplication::clipboard()->clear(QClipboard::Clipboard); QGuiApplication::clipboard()->clear(QClipboard::Selection); // used to crash on X11 QGuiApplication::clipboard()->clear(QClipboard::FindBuffer); if (QGuiApplication::clipboard()->supportsSelection()) QCOMPARE(spySelection.count(), 1); else QCOMPARE(spySelection.count(), 0); if (QGuiApplication::clipboard()->supportsFindBuffer()) QCOMPARE(spyFindBuffer.count(), 1); else QCOMPARE(spyFindBuffer.count(), 0); QCOMPARE(spyData.count(), 1); // an other crash test data = new QMimeData; data->setText("foo"); QGuiApplication::clipboard()->setMimeData(data, QClipboard::Clipboard); QGuiApplication::clipboard()->setMimeData(data, QClipboard::Selection); QGuiApplication::clipboard()->setMimeData(data, QClipboard::FindBuffer); QMimeData *newData = new QMimeData; newData->setText("bar"); spySelection.clear(); spyData.clear(); spyFindBuffer.clear(); QGuiApplication::clipboard()->setMimeData(newData, QClipboard::Clipboard); QGuiApplication::clipboard()->setMimeData(newData, QClipboard::Selection); // used to crash on X11 QGuiApplication::clipboard()->setMimeData(newData, QClipboard::FindBuffer); if (QGuiApplication::clipboard()->supportsSelection()) QCOMPARE(spySelection.count(), 1); else QCOMPARE(spySelection.count(), 0); if (QGuiApplication::clipboard()->supportsFindBuffer()) QCOMPARE(spyFindBuffer.count(), 1); else QCOMPARE(spyFindBuffer.count(), 0); QCOMPARE(spyData.count(), 1); }