telutils/dialpad/tsrc/unit/ut_dialpadbluetootheventfilter/ut_dialpadbluetootheventfilter.cpp
branchRCL_3
changeset 20 987c9837762f
parent 19 7d48bed6ce0c
child 21 0a6dd2dc9970
equal deleted inserted replaced
19:7d48bed6ce0c 20:987c9837762f
     1 /*!
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QtGui>
       
    19 #include <QtTest/QtTest>
       
    20 
       
    21 #include <hbapplication.h>
       
    22 #include <hbmainwindow.h>
       
    23 #include <hbaction.h>
       
    24 #include <hbtoolbar.h>
       
    25 #include <hbview.h>
       
    26 #include <hblineedit.h>
       
    27 #include <hbinstance.h>
       
    28 
       
    29 #ifdef Q_OS_SYMBIAN
       
    30 #include "xqservicerequest.h"
       
    31 #endif
       
    32 
       
    33 #include "dialpadtest.h"
       
    34 #include "dialpadtestutil.h"
       
    35 #include "dialpadbluetootheventfilter.h"
       
    36 #include "dialpad.h"
       
    37 
       
    38 const int WAIT_TIME = 300;
       
    39 QString mService;
       
    40 QString mMessage;
       
    41 bool mXQServiceConstructed;
       
    42 bool mSendCalled;
       
    43 
       
    44 #ifdef Q_OS_SYMBIAN
       
    45 XQServiceRequest::XQServiceRequest(const QString& service, const QString& message, const bool& synchronous) { mService=service; mMessage=message; mXQServiceConstructed=true; }
       
    46 XQServiceRequest::~XQServiceRequest() {}
       
    47 bool XQServiceRequest::send(QVariant& retValue) { mSendCalled=true; return true; }
       
    48 void XQServiceRequest::addArg(const QVariant& v) {}
       
    49 #endif
       
    50 
       
    51 // helper class
       
    52 class KeyEventCatcher : public QObject
       
    53 {
       
    54 public:
       
    55     bool eventFilter(QObject* watched, QEvent * event)
       
    56     {
       
    57         Q_UNUSED(watched);
       
    58 
       
    59         if (event->type() == QEvent::KeyPress) {
       
    60             QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
    61             mKeyPresses.append(keyEvent->key());
       
    62             return false;
       
    63         } else if (event->type() == QEvent::KeyRelease) {
       
    64             QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
    65             mKeyReleases.append(keyEvent->key());
       
    66             return false;
       
    67         }
       
    68         return false;
       
    69     }
       
    70 
       
    71 public:
       
    72     QList<int> mKeyPresses;
       
    73     QList<int> mKeyReleases;
       
    74 };
       
    75 
       
    76 // test cases
       
    77 class ut_DialpadBluetoothEventFilter : public QObject
       
    78 {
       
    79     Q_OBJECT
       
    80 
       
    81 private slots:
       
    82     void initTestCase();
       
    83     void init();
       
    84     void cleanup();
       
    85     void cleanupTestCase();
       
    86     void testLongPressAsteriskKey();
       
    87     void testShortAndLongPressAsteriskKey();
       
    88 
       
    89 private:
       
    90     HbMainWindow*  mMainWindow;
       
    91     Dialpad*       mDialpad;
       
    92     DialpadBluetoothEventFilter *mEventFilter;
       
    93     KeyEventCatcher* mKeyCatcher;
       
    94     DialpadTestUtil* mUtil;
       
    95 };
       
    96 
       
    97 void ut_DialpadBluetoothEventFilter::initTestCase()
       
    98 {
       
    99     mMainWindow = new HbMainWindow;
       
   100 
       
   101     mKeyCatcher = new KeyEventCatcher;
       
   102     mMainWindow->installEventFilter(mKeyCatcher);
       
   103 
       
   104     mUtil = new DialpadTestUtil(*mMainWindow);
       
   105 
       
   106     mDialpad = new Dialpad();
       
   107     mEventFilter = new DialpadBluetoothEventFilter(mDialpad, this);
       
   108     hbInstance->allMainWindows().at(0)->installEventFilter(mEventFilter);
       
   109             
       
   110     QRectF rect(mMainWindow->contentsRect());
       
   111     rect.setHeight(rect.height()*0.7);
       
   112     rect.moveTop((mMainWindow->contentsRect().height()-rect.height())/2);
       
   113 
       
   114     mDialpad->setPreferredSize(360,400);
       
   115     mDialpad->setPos(0,100);
       
   116 
       
   117     mMainWindow->show();
       
   118     mDialpad->show();
       
   119     mDialpad->hide();
       
   120 }
       
   121 
       
   122 void ut_DialpadBluetoothEventFilter::init()
       
   123 {
       
   124     mService = QString("");
       
   125     mMessage = QString("");
       
   126     mXQServiceConstructed = false;
       
   127     mSendCalled = false;
       
   128 }
       
   129 
       
   130 void ut_DialpadBluetoothEventFilter::cleanupTestCase()
       
   131 {
       
   132     delete mDialpad;
       
   133     delete mMainWindow;
       
   134     delete mKeyCatcher;
       
   135     delete mUtil;
       
   136 }
       
   137 
       
   138 void ut_DialpadBluetoothEventFilter::cleanup()
       
   139 {
       
   140     mKeyCatcher->mKeyPresses.clear();
       
   141     mKeyCatcher->mKeyReleases.clear();
       
   142     mDialpad->editor().setText(QString());
       
   143     QTest::qWait(WAIT_TIME); // delay between tests
       
   144 }
       
   145 
       
   146 void ut_DialpadBluetoothEventFilter::testLongPressAsteriskKey()
       
   147 {
       
   148     mDialpad->openDialpad();
       
   149     QTest::qWait(2*WAIT_TIME);
       
   150 
       
   151     // Basic long press
       
   152     mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Press);
       
   153     QTest::qWait(2000);
       
   154     mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Release);
       
   155     QTest::qWait(1000);
       
   156     QCOMPARE(mDialpad->editor().text(), QString(""));
       
   157     mDialpad->closeDialpad();
       
   158     
       
   159     QVERIFY(mXQServiceConstructed == true);
       
   160     QCOMPARE(mService, QString("com.nokia.services.btservices.ToggleBluetooth"));
       
   161     QCOMPARE(mMessage, QString("toggleBluetooth()"));
       
   162     QVERIFY(mSendCalled == true);
       
   163 }
       
   164 
       
   165 void ut_DialpadBluetoothEventFilter::testShortAndLongPressAsteriskKey()
       
   166 {
       
   167     mDialpad->openDialpad();
       
   168 
       
   169     // Short press and long press shouldn't do anything
       
   170     mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Press);
       
   171     mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Release);
       
   172     mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Press);
       
   173     QTest::qWait(2000);
       
   174     mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Release);
       
   175     QCOMPARE(mDialpad->editor().text(), QString("**"));
       
   176     mDialpad->closeDialpad();	
       
   177 }
       
   178 
       
   179 DIALPAD_TEST_MAIN(ut_DialpadBluetoothEventFilter)
       
   180 #include "ut_dialpadbluetootheventfilter.moc"
       
   181