telutils/dialpad/tsrc/unit/ut_dialpadbluetootheventfilter/ut_dialpadbluetootheventfilter.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Tue, 31 Aug 2010 15:45:17 +0300
branchRCL_3
changeset 19 7d48bed6ce0c
permissions -rw-r--r--
Revision: 201033 Kit: 201035

/*!
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

#include <QtGui>
#include <QtTest/QtTest>

#include <hbapplication.h>
#include <hbmainwindow.h>
#include <hbaction.h>
#include <hbtoolbar.h>
#include <hbview.h>
#include <hblineedit.h>
#include <hbinstance.h>

#ifdef Q_OS_SYMBIAN
#include "xqservicerequest.h"
#endif

#include "dialpadtest.h"
#include "dialpadtestutil.h"
#include "dialpadbluetootheventfilter.h"
#include "dialpad.h"

const int WAIT_TIME = 300;
QString mService;
QString mMessage;
bool mXQServiceConstructed;
bool mSendCalled;

#ifdef Q_OS_SYMBIAN
XQServiceRequest::XQServiceRequest(const QString& service, const QString& message, const bool& synchronous) { mService=service; mMessage=message; mXQServiceConstructed=true; }
XQServiceRequest::~XQServiceRequest() {}
bool XQServiceRequest::send(QVariant& retValue) { mSendCalled=true; return true; }
void XQServiceRequest::addArg(const QVariant& v) {}
#endif

// helper class
class KeyEventCatcher : public QObject
{
public:
    bool eventFilter(QObject* watched, QEvent * event)
    {
        Q_UNUSED(watched);

        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            mKeyPresses.append(keyEvent->key());
            return false;
        } else if (event->type() == QEvent::KeyRelease) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            mKeyReleases.append(keyEvent->key());
            return false;
        }
        return false;
    }

public:
    QList<int> mKeyPresses;
    QList<int> mKeyReleases;
};

// test cases
class ut_DialpadBluetoothEventFilter : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void init();
    void cleanup();
    void cleanupTestCase();
    void testLongPressAsteriskKey();
    void testShortAndLongPressAsteriskKey();

private:
    HbMainWindow*  mMainWindow;
    Dialpad*       mDialpad;
    DialpadBluetoothEventFilter *mEventFilter;
    KeyEventCatcher* mKeyCatcher;
    DialpadTestUtil* mUtil;
};

void ut_DialpadBluetoothEventFilter::initTestCase()
{
    mMainWindow = new HbMainWindow;

    mKeyCatcher = new KeyEventCatcher;
    mMainWindow->installEventFilter(mKeyCatcher);

    mUtil = new DialpadTestUtil(*mMainWindow);

    mDialpad = new Dialpad();
    mEventFilter = new DialpadBluetoothEventFilter(mDialpad, this);
    hbInstance->allMainWindows().at(0)->installEventFilter(mEventFilter);
            
    QRectF rect(mMainWindow->contentsRect());
    rect.setHeight(rect.height()*0.7);
    rect.moveTop((mMainWindow->contentsRect().height()-rect.height())/2);

    mDialpad->setPreferredSize(360,400);
    mDialpad->setPos(0,100);

    mMainWindow->show();
    mDialpad->show();
    mDialpad->hide();
}

void ut_DialpadBluetoothEventFilter::init()
{
    mService = QString("");
    mMessage = QString("");
    mXQServiceConstructed = false;
    mSendCalled = false;
}

void ut_DialpadBluetoothEventFilter::cleanupTestCase()
{
    delete mDialpad;
    delete mMainWindow;
    delete mKeyCatcher;
    delete mUtil;
}

void ut_DialpadBluetoothEventFilter::cleanup()
{
    mKeyCatcher->mKeyPresses.clear();
    mKeyCatcher->mKeyReleases.clear();
    mDialpad->editor().setText(QString());
    QTest::qWait(WAIT_TIME); // delay between tests
}

void ut_DialpadBluetoothEventFilter::testLongPressAsteriskKey()
{
    mDialpad->openDialpad();
    QTest::qWait(2*WAIT_TIME);

    // Basic long press
    mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Press);
    QTest::qWait(2000);
    mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Release);
    QTest::qWait(1000);
    QCOMPARE(mDialpad->editor().text(), QString(""));
    mDialpad->closeDialpad();
    
    QVERIFY(mXQServiceConstructed == true);
    QCOMPARE(mService, QString("com.nokia.services.btservices.ToggleBluetooth"));
    QCOMPARE(mMessage, QString("toggleBluetooth()"));
    QVERIFY(mSendCalled == true);
}

void ut_DialpadBluetoothEventFilter::testShortAndLongPressAsteriskKey()
{
    mDialpad->openDialpad();

    // Short press and long press shouldn't do anything
    mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Press);
    mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Release);
    mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Press);
    QTest::qWait(2000);
    mUtil->mouseClickDialpad(Qt::Key_Asterisk, DialpadTestUtil::Release);
    QCOMPARE(mDialpad->editor().text(), QString("**"));
    mDialpad->closeDialpad();	
}

DIALPAD_TEST_MAIN(ut_DialpadBluetoothEventFilter)
#include "ut_dialpadbluetootheventfilter.moc"