tests/auto/qinputcontext/tst_qinputcontext.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 #include <QtTest/QtTest>
       
    43 #include "../../shared/util.h"
       
    44 
       
    45 #include <qinputcontext.h>
       
    46 #include <qlineedit.h>
       
    47 #include <qplaintextedit.h>
       
    48 #include <qlayout.h>
       
    49 #include <qradiobutton.h>
       
    50 #include <qwindowsstyle.h>
       
    51 
       
    52 class tst_QInputContext : public QObject
       
    53 {
       
    54 Q_OBJECT
       
    55 
       
    56 public:
       
    57     tst_QInputContext() {}
       
    58     virtual ~tst_QInputContext() {}
       
    59 
       
    60 public slots:
       
    61     void initTestCase() {}
       
    62     void cleanupTestCase() {}
       
    63     void init() {}
       
    64     void cleanup() {}
       
    65 private slots:
       
    66     void maximumTextLength();
       
    67     void filterMouseEvents();
       
    68     void requestSoftwareInputPanel();
       
    69     void closeSoftwareInputPanel();
       
    70     void selections();
       
    71 };
       
    72 
       
    73 void tst_QInputContext::maximumTextLength()
       
    74 {
       
    75     QLineEdit le;
       
    76 
       
    77     le.setMaxLength(15);
       
    78     QVariant variant = le.inputMethodQuery(Qt::ImMaximumTextLength);
       
    79     QVERIFY(variant.isValid());
       
    80     QCOMPARE(variant.toInt(), 15);
       
    81 
       
    82     QPlainTextEdit pte;
       
    83     // For BC/historical reasons, QPlainTextEdit::inputMethodQuery is protected.
       
    84     variant = static_cast<QWidget *>(&pte)->inputMethodQuery(Qt::ImMaximumTextLength);
       
    85     QVERIFY(!variant.isValid());
       
    86 }
       
    87 
       
    88 class QFilterInputContext : public QInputContext
       
    89 {
       
    90 public:
       
    91     QFilterInputContext() {}
       
    92     ~QFilterInputContext() {}
       
    93 
       
    94     QString identifierName() { return QString(); }
       
    95     QString language() { return QString(); }
       
    96 
       
    97     void reset() {}
       
    98 
       
    99     bool isComposing() const { return false; }
       
   100 
       
   101     bool filterEvent( const QEvent *event )
       
   102     {
       
   103         lastTypes.append(event->type());
       
   104         return false;
       
   105     }
       
   106 
       
   107 public:
       
   108     QList<QEvent::Type> lastTypes;
       
   109 };
       
   110 
       
   111 void tst_QInputContext::filterMouseEvents()
       
   112 {
       
   113     QLineEdit le;
       
   114     le.show();
       
   115     QApplication::setActiveWindow(&le);
       
   116 
       
   117     QFilterInputContext *ic = new QFilterInputContext;
       
   118     le.setInputContext(ic);
       
   119     QTest::mouseClick(&le, Qt::LeftButton);
       
   120 
       
   121     QVERIFY(ic->lastTypes.indexOf(QEvent::MouseButtonRelease) >= 0);
       
   122 
       
   123     le.setInputContext(0);
       
   124 }
       
   125 
       
   126 class RequestSoftwareInputPanelStyle : public QWindowsStyle
       
   127 {
       
   128 public:
       
   129     RequestSoftwareInputPanelStyle()
       
   130         : m_rsipBehavior(RSIP_OnMouseClickAndAlreadyFocused)
       
   131     {
       
   132 #ifdef Q_OS_WINCE
       
   133         qApp->setAutoSipEnabled(true);
       
   134 #endif
       
   135     }
       
   136     ~RequestSoftwareInputPanelStyle()
       
   137     {
       
   138     }
       
   139 
       
   140     int styleHint(StyleHint hint, const QStyleOption *opt = 0,
       
   141                   const QWidget *widget = 0, QStyleHintReturn* returnData = 0) const
       
   142     {
       
   143         if (hint == SH_RequestSoftwareInputPanel) {
       
   144             return m_rsipBehavior;
       
   145         } else {
       
   146             return QWindowsStyle::styleHint(hint, opt, widget, returnData);
       
   147         }
       
   148     }
       
   149 
       
   150     RequestSoftwareInputPanel m_rsipBehavior;
       
   151 };
       
   152 
       
   153 void tst_QInputContext::requestSoftwareInputPanel()
       
   154 {
       
   155     QStyle *oldStyle = qApp->style();
       
   156     oldStyle->setParent(this); // Prevent it being deleted.
       
   157     RequestSoftwareInputPanelStyle *newStyle = new RequestSoftwareInputPanelStyle;
       
   158     qApp->setStyle(newStyle);
       
   159 
       
   160     QWidget w;
       
   161     QLayout *layout = new QVBoxLayout;
       
   162     QLineEdit *le1, *le2;
       
   163     le1 = new QLineEdit;
       
   164     le2 = new QLineEdit;
       
   165     layout->addWidget(le1);
       
   166     layout->addWidget(le2);
       
   167     w.setLayout(layout);
       
   168 
       
   169     QFilterInputContext *ic1, *ic2;
       
   170     ic1 = new QFilterInputContext;
       
   171     ic2 = new QFilterInputContext;
       
   172     le1->setInputContext(ic1);
       
   173     le2->setInputContext(ic2);
       
   174 
       
   175     w.show();
       
   176     QApplication::setActiveWindow(&w);
       
   177 
       
   178     // Testing single click panel activation.
       
   179     newStyle->m_rsipBehavior = QStyle::RSIP_OnMouseClick;
       
   180     QTest::mouseClick(le2, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
       
   181     QVERIFY(ic2->lastTypes.indexOf(QEvent::RequestSoftwareInputPanel) >= 0);
       
   182     ic2->lastTypes.clear();
       
   183 
       
   184     // Testing double click panel activation.
       
   185     newStyle->m_rsipBehavior = QStyle::RSIP_OnMouseClickAndAlreadyFocused;
       
   186     QTest::mouseClick(le1, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
       
   187     QVERIFY(ic1->lastTypes.indexOf(QEvent::RequestSoftwareInputPanel) < 0);
       
   188     QTest::mouseClick(le1, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
       
   189     QVERIFY(ic1->lastTypes.indexOf(QEvent::RequestSoftwareInputPanel) >= 0);
       
   190     ic1->lastTypes.clear();
       
   191 
       
   192     // Testing right mouse button
       
   193     QTest::mouseClick(le1, Qt::RightButton, Qt::NoModifier, QPoint(5, 5));
       
   194     QVERIFY(ic1->lastTypes.indexOf(QEvent::RequestSoftwareInputPanel) < 0);
       
   195 
       
   196     qApp->setStyle(oldStyle);
       
   197     oldStyle->setParent(qApp);
       
   198 }
       
   199 
       
   200 void tst_QInputContext::closeSoftwareInputPanel()
       
   201 {
       
   202     QWidget w;
       
   203     QLayout *layout = new QVBoxLayout;
       
   204     QLineEdit *le1, *le2;
       
   205     QRadioButton *rb;
       
   206     le1 = new QLineEdit;
       
   207     le2 = new QLineEdit;
       
   208     rb = new QRadioButton;
       
   209     layout->addWidget(le1);
       
   210     layout->addWidget(le2);
       
   211     layout->addWidget(rb);
       
   212     w.setLayout(layout);
       
   213 
       
   214     QFilterInputContext *ic1, *ic2;
       
   215     ic1 = new QFilterInputContext;
       
   216     ic2 = new QFilterInputContext;
       
   217     le1->setInputContext(ic1);
       
   218     le2->setInputContext(ic2);
       
   219 
       
   220     w.show();
       
   221     QApplication::setActiveWindow(&w);
       
   222 
       
   223     // Testing that panel doesn't close between two input methods aware widgets.
       
   224     QTest::mouseClick(le1, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
       
   225     QTest::mouseClick(le2, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
       
   226     QVERIFY(ic2->lastTypes.indexOf(QEvent::CloseSoftwareInputPanel) < 0);
       
   227 
       
   228     // Testing that panel closes when focusing non-aware widget.
       
   229     QTest::mouseClick(rb, Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
       
   230     QVERIFY(ic2->lastTypes.indexOf(QEvent::CloseSoftwareInputPanel) >= 0);
       
   231 }
       
   232 
       
   233 void tst_QInputContext::selections()
       
   234 {
       
   235     QLineEdit le;
       
   236     le.setText("Test text");
       
   237     le.setSelection(2, 2);
       
   238     QCOMPARE(le.inputMethodQuery(Qt::ImCursorPosition).toInt(), 4);
       
   239     QCOMPARE(le.inputMethodQuery(Qt::ImAnchorPosition).toInt(), 2);
       
   240 
       
   241     QList<QInputMethodEvent::Attribute> attributes;
       
   242     attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Selection, 5, 3, QVariant()));
       
   243     QInputMethodEvent event("", attributes);
       
   244     QApplication::sendEvent(&le, &event);
       
   245     QCOMPARE(le.cursorPosition(), 8);
       
   246     QCOMPARE(le.selectionStart(), 5);
       
   247     QCOMPARE(le.inputMethodQuery(Qt::ImCursorPosition).toInt(), 8);
       
   248     QCOMPARE(le.inputMethodQuery(Qt::ImAnchorPosition).toInt(), 5);
       
   249 }
       
   250 
       
   251 QTEST_MAIN(tst_QInputContext)
       
   252 #include "tst_qinputcontext.moc"