Add tests for copy and paste using keyboard shortcuts.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Tue, 31 Jul 2012 06:25:16 +0000 (16:25 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 6 Aug 2012 04:04:47 +0000 (06:04 +0200)
Fixes TextInput.cut() removing text while read only.

Change-Id: I03cd44d381be9d53f71ba168b8be7971ab0bbad7
Reviewed-by: Damian Jansen <damian.jansen@nokia.com>
src/quick/items/qquicktextinput.cpp
tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp
tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp

index 5b7bbaf..ced2e62 100644 (file)
@@ -1899,8 +1899,10 @@ bool QQuickTextInput::isRightToLeft(int start, int end)
 void QQuickTextInput::cut()
 {
     Q_D(QQuickTextInput);
-    d->copy();
-    d->del();
+    if (!d->m_readOnly) {
+        d->copy();
+        d->del();
+    }
 }
 
 /*!
index 6684e68..9f38105 100644 (file)
@@ -2618,6 +2618,9 @@ void tst_qquicktextedit::copyAndPaste() {
     QVERIFY(textEdit->canPaste());
     textEdit->setReadOnly(true);
     QVERIFY(!textEdit->canPaste());
+    textEdit->paste();
+    QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
+    QCOMPARE(textEdit->text().length(), 24);
     textEdit->setReadOnly(false);
     QVERIFY(textEdit->canPaste());
 
@@ -2627,11 +2630,21 @@ void tst_qquicktextedit::copyAndPaste() {
     QQuickTextEditPrivate *editPrivate = static_cast<QQuickTextEditPrivate*>(pri);
     QCOMPARE(textEdit->text(), editPrivate->text);
 
+    // cut: no selection
+    textEdit->cut();
+    QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
+
     // select word
     textEdit->setCursorPosition(0);
     textEdit->selectWord();
     QCOMPARE(textEdit->selectedText(), QString("Hello"));
 
+    // cut: read only.
+    textEdit->setReadOnly(true);
+    textEdit->cut();
+    QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
+    textEdit->setReadOnly(false);
+
     // select all and cut
     textEdit->selectAll();
     textEdit->cut();
@@ -2639,6 +2652,18 @@ void tst_qquicktextedit::copyAndPaste() {
     textEdit->paste();
     QCOMPARE(textEdit->text(), QString("Hello world!Hello world!"));
     QCOMPARE(textEdit->text().length(), 24);
+
+    // Copy first word.
+    textEdit->setCursorPosition(0);
+    textEdit->selectWord();
+    textEdit->copy();
+    // copy: no selection, previous copy retained;
+    textEdit->setCursorPosition(0);
+    QCOMPARE(textEdit->selectedText(), QString());
+    textEdit->copy();
+    textEdit->setText(QString());
+    textEdit->paste();
+    QCOMPARE(textEdit->text(), QString("Hello"));
 #endif
 }
 
@@ -2680,6 +2705,7 @@ void tst_qquicktextedit::canPasteEmpty() {
 #endif
 }
 
+
 void tst_qquicktextedit::readOnly()
 {
     QQuickView window(testFileUrl("readOnly.qml"));
@@ -4667,6 +4693,50 @@ void tst_qquicktextedit::undo_keypressevents_data()
 
         QTest::newRow("Inserts,moving,selection and overwriting") << keys << expectedString;
     }
+
+
+#ifndef QT_NO_CLIPBOARD
+
+    bool canCopyPaste = true;
+#ifdef Q_OS_MAC
+
+    {
+        PasteboardRef pasteboard;
+        OSStatus status = PasteboardCreate(0, &pasteboard);
+        canCopyPaste = status == noErr;
+    }
+#endif
+
+    if (canCopyPaste) {
+        KeyList keys;
+        keys    << "123"
+                << QKeySequence(QKeySequence::SelectStartOfLine)
+                << QKeySequence(QKeySequence::Cut)
+                << "ABC"
+                << QKeySequence(QKeySequence::Paste);
+        QStringList expectedString = QStringList()
+                << "ABC123"
+                << "ABC"
+                << ""
+                << "123";
+        QTest::newRow("Cut,paste") << keys << expectedString;
+    }
+    if (canCopyPaste) {
+        KeyList keys;
+        keys    << "123"
+                << QKeySequence(QKeySequence::SelectStartOfLine)
+                << QKeySequence(QKeySequence::Copy)
+                << "ABC"
+                << QKeySequence(QKeySequence::Paste);
+        QStringList expectedString = QStringList()
+                << "ABC123"
+                << "ABC"
+                << "A"
+                << "123";
+        QTest::newRow("Copy,paste") << keys << expectedString;
+    }
+
+#endif
 }
 
 void tst_qquicktextedit::undo_keypressevents()
index 09d2119..8beb871 100644 (file)
@@ -2459,14 +2459,27 @@ void tst_qquicktextinput::copyAndPaste() {
     QVERIFY(textInput->canPaste());
     textInput->setReadOnly(true);
     QVERIFY(!textInput->canPaste());
+    textInput->paste();
+    QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
+    QCOMPARE(textInput->text().length(), 24);
     textInput->setReadOnly(false);
     QVERIFY(textInput->canPaste());
 
+    // cut: no selection
+    textInput->cut();
+    QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
+
     // select word
     textInput->setCursorPosition(0);
     textInput->selectWord();
     QCOMPARE(textInput->selectedText(), QString("Hello"));
 
+    // cut: read only.
+    textInput->setReadOnly(true);
+    textInput->cut();
+    QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
+    textInput->setReadOnly(false);
+
     // select all and cut
     textInput->selectAll();
     textInput->cut();
@@ -2475,6 +2488,18 @@ void tst_qquicktextinput::copyAndPaste() {
     QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
     QCOMPARE(textInput->text().length(), 24);
 
+    // Copy first word.
+    textInput->setCursorPosition(0);
+    textInput->selectWord();
+    textInput->copy();
+    // copy: no selection, previous copy retained;
+    textInput->setCursorPosition(0);
+    QCOMPARE(textInput->selectedText(), QString());
+    textInput->copy();
+    textInput->setText(QString());
+    textInput->paste();
+    QCOMPARE(textInput->text(), QString("Hello"));
+
     // clear copy buffer
     QClipboard *clipboard = QGuiApplication::clipboard();
     QVERIFY(clipboard);
@@ -5483,6 +5508,49 @@ void tst_qquicktextinput::undo_keypressevents_data()
 
         QTest::newRow("Insert,move,select,delete next word,undo,insert") << keys << expectedString;
     }
+
+#ifndef QT_NO_CLIPBOARD
+
+    bool canCopyPaste = true;
+#ifdef Q_OS_MAC
+
+    {
+        PasteboardRef pasteboard;
+        OSStatus status = PasteboardCreate(0, &pasteboard);
+        canCopyPaste = status == noErr;
+    }
+#endif
+
+    if (canCopyPaste) {
+        KeyList keys;
+        keys    << "123"
+                << QKeySequence(QKeySequence::SelectStartOfLine)
+                << QKeySequence(QKeySequence::Cut)
+                << "ABC"
+                << QKeySequence(QKeySequence::Paste);
+        QStringList expectedString = QStringList()
+                << "ABC123"
+                << "ABC"
+                // TextEdit: ""
+                << "123";
+        QTest::newRow("Cut,paste") << keys << expectedString;
+    }
+    if (canCopyPaste) {
+        KeyList keys;
+        keys    << "123"
+                << QKeySequence(QKeySequence::SelectStartOfLine)
+                << QKeySequence(QKeySequence::Copy)
+                << "ABC"
+                << QKeySequence(QKeySequence::Paste);
+        QStringList expectedString = QStringList()
+                << "ABC123"
+                << "ABC"
+                // TextEdit: "A"
+                << "123";
+        QTest::newRow("Copy,paste") << keys << expectedString;
+    }
+
+#endif
 }
 
 void tst_qquicktextinput::undo_keypressevents()