tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    96     void maxLength();
    96     void maxLength();
    97     void masks();
    97     void masks();
    98     void validators();
    98     void validators();
    99     void inputMethods();
    99     void inputMethods();
   100 
   100 
       
   101     void passwordCharacter();
   101     void cursorDelegate();
   102     void cursorDelegate();
   102     void navigation();
   103     void navigation();
   103     void copyAndPaste();
   104     void copyAndPaste();
   104     void readOnly();
   105     void readOnly();
   105 
   106 
   191         QDeclarativeComponent textinputComponent(&engine);
   192         QDeclarativeComponent textinputComponent(&engine);
   192         textinputComponent.setData(componentStr.toLatin1(), QUrl());
   193         textinputComponent.setData(componentStr.toLatin1(), QUrl());
   193         QDeclarativeTextInput *textinputObject = qobject_cast<QDeclarativeTextInput*>(textinputComponent.create());
   194         QDeclarativeTextInput *textinputObject = qobject_cast<QDeclarativeTextInput*>(textinputComponent.create());
   194 
   195 
   195         QVERIFY(textinputObject != 0);
   196         QVERIFY(textinputObject != 0);
   196         int delta = abs(int(textinputObject->width()) - metricWidth);
   197         int delta = abs(int(int(textinputObject->width()) - metricWidth));
   197         QVERIFY(delta <= 3.0); // As best as we can hope for cross-platform.
   198         QVERIFY(delta <= 3.0); // As best as we can hope for cross-platform.
   198 
   199 
   199         delete textinputObject;
   200         delete textinputObject;
   200     }
   201     }
   201 }
   202 }
   448 
   449 
   449     // Check autoscrolled...
   450     // Check autoscrolled...
   450     QFontMetrics fm(textinputObject->font());
   451     QFontMetrics fm(textinputObject->font());
   451 
   452 
   452     int pos = textinputObject->positionAt(textinputObject->width()/2);
   453     int pos = textinputObject->positionAt(textinputObject->width()/2);
   453     int diff = abs(fm.width(textinputObject->text()) - (fm.width(textinputObject->text().left(pos))+textinputObject->width()/2));
   454     int diff = abs(int(fm.width(textinputObject->text()) - (fm.width(textinputObject->text().left(pos))+textinputObject->width()/2)));
   454 
   455 
   455     // some tollerance for different fonts.
   456     // some tollerance for different fonts.
   456 #ifdef Q_OS_LINUX
   457 #ifdef Q_OS_LINUX
   457     QVERIFY(diff < 2);
   458     QVERIFY(diff < 2);
   458 #else
   459 #else
   460 #endif
   461 #endif
   461 
   462 
   462     // Check without autoscroll...
   463     // Check without autoscroll...
   463     textinputObject->setAutoScroll(false);
   464     textinputObject->setAutoScroll(false);
   464     pos = textinputObject->positionAt(textinputObject->width()/2);
   465     pos = textinputObject->positionAt(textinputObject->width()/2);
   465     diff = abs(fm.width(textinputObject->text().left(pos))-textinputObject->width()/2);
   466     diff = abs(int(fm.width(textinputObject->text().left(pos))-textinputObject->width()/2));
   466 
   467 
   467     // some tollerance for different fonts.
   468     // some tollerance for different fonts.
   468 #ifdef Q_OS_LINUX
   469 #ifdef Q_OS_LINUX
   469     QVERIFY(diff < 2);
   470     QVERIFY(diff < 2);
   470 #else
   471 #else
   739     textInput->cut();
   740     textInput->cut();
   740     QCOMPARE(textInput->text().length(), 0);
   741     QCOMPARE(textInput->text().length(), 0);
   741     textInput->paste();
   742     textInput->paste();
   742     QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
   743     QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
   743     QCOMPARE(textInput->text().length(), 24);
   744     QCOMPARE(textInput->text().length(), 24);
       
   745 
       
   746     // clear copy buffer
       
   747     QClipboard *clipboard = QApplication::clipboard();
       
   748     QVERIFY(clipboard);
       
   749     clipboard->clear();
       
   750 
       
   751     // test that copy functionality is disabled
       
   752     // when echo mode is set to hide text/password mode
       
   753     int index = 0;
       
   754     while (index < 4) {
       
   755         QDeclarativeTextInput::EchoMode echoMode = QDeclarativeTextInput::EchoMode(index);
       
   756         textInput->setEchoMode(echoMode);
       
   757         textInput->setText("My password");
       
   758         textInput->select(0, textInput->text().length());;
       
   759         textInput->copy();
       
   760         if (echoMode == QDeclarativeTextInput::Normal) {
       
   761             QVERIFY(!clipboard->text().isEmpty());
       
   762             QCOMPARE(clipboard->text(), QString("My password"));
       
   763             clipboard->clear();
       
   764         } else {
       
   765             QVERIFY(clipboard->text().isEmpty());
       
   766         }
       
   767         index++;
       
   768     }
   744 #endif
   769 #endif
       
   770 }
       
   771 
       
   772 void tst_qdeclarativetextinput::passwordCharacter()
       
   773 {
       
   774     QString componentStr = "import Qt 4.7\nTextInput { text: \"Hello world!\"; font.family: \"Helvetica\"; echoMode: TextInput.Password }";
       
   775     QDeclarativeComponent textInputComponent(&engine);
       
   776     textInputComponent.setData(componentStr.toLatin1(), QUrl());
       
   777     QDeclarativeTextInput *textInput = qobject_cast<QDeclarativeTextInput*>(textInputComponent.create());
       
   778     QVERIFY(textInput != 0);
       
   779 
       
   780     textInput->setPasswordCharacter("X");
       
   781     QSize contentsSize = textInput->contentsSize();
       
   782     textInput->setPasswordCharacter(".");
       
   783     // QTBUG-12383 content is updated and redrawn
       
   784     QVERIFY(contentsSize != textInput->contentsSize());
   745 }
   785 }
   746 
   786 
   747 void tst_qdeclarativetextinput::cursorDelegate()
   787 void tst_qdeclarativetextinput::cursorDelegate()
   748 {
   788 {
   749     QDeclarativeView* view = createView(SRCDIR "/data/cursorTest.qml");
   789     QDeclarativeView* view = createView(SRCDIR "/data/cursorTest.qml");
   996     scene.addItem(&anotherInput);
  1036     scene.addItem(&anotherInput);
   997     anotherInput.setFocus(true);
  1037     anotherInput.setFocus(true);
   998     QApplication::processEvents();
  1038     QApplication::processEvents();
   999     QCOMPARE(ic.openInputPanelReceived, true);
  1039     QCOMPARE(ic.openInputPanelReceived, true);
  1000     ic.openInputPanelReceived = false;
  1040     ic.openInputPanelReceived = false;
  1001     QCOMPARE(view.inputContext(), &ic);
  1041     QCOMPARE(view.inputContext(), (QInputContext*)&ic);
  1002     QVERIFY(view.testAttribute(Qt::WA_InputMethodEnabled));
  1042     QVERIFY(view.testAttribute(Qt::WA_InputMethodEnabled));
  1003 
  1043 
  1004     // input method should be disabled if focus
  1044     // input method should be disabled if focus
  1005     // is lost to an item that doesn't accept inputs
  1045     // is lost to an item that doesn't accept inputs
  1006     QDeclarativeItem item;
  1046     QDeclarativeItem item;