hsappkeyhandler/tsrc/t_hsappkeyhandler/src/t_hsappkeyhandler.cpp
changeset 117 c63ee96dbe5f
equal deleted inserted replaced
115:3ab5c078b490 117:c63ee96dbe5f
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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 "t_hsappkeyhandler.h"
       
    19 
       
    20 #include <QtTest/QtTest>
       
    21 #include <QScopedPointer>
       
    22 
       
    23 #include <QValueSpaceSubscriber>
       
    24 
       
    25 #include <hb/hbcore/hbsymbianvariant.h>
       
    26 
       
    27 #include <tspropertydefs.h>
       
    28 #include <homescreendomainpskeys.h>
       
    29 
       
    30 #include "hsappkeyplugin.h"
       
    31 
       
    32 QTM_USE_NAMESPACE
       
    33 
       
    34 namespace {
       
    35     _LIT(KTsPluginName, "com.nokia.taskswitcher.tsdevicedialogplugin/1.0");
       
    36 }
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 //  CONSTRUCTOR
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 T_HsAppKeyHandler::T_HsAppKeyHandler(QObject *parent) :
       
    43     QObject(parent),
       
    44     mCHsAppKeyPluginInstance(0),
       
    45     mDialog(0)
       
    46 {
       
    47 }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 //  INIT AND CLEANUP
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void T_HsAppKeyHandler::init()
       
    54 {
       
    55     TRAPD(errNo, mCHsAppKeyPluginInstance = CHsAppKeyPlugin::NewL());
       
    56     QCOMPARE(errNo, KErrNone);
       
    57     QVERIFY(mCHsAppKeyPluginInstance);
       
    58 }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void T_HsAppKeyHandler::cleanup()
       
    64 {
       
    65     delete mCHsAppKeyPluginInstance;
       
    66     mCHsAppKeyPluginInstance = 0;
       
    67     
       
    68     delete mDialog;
       
    69     mDialog = 0;
       
    70 }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 //  TEST CASES
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void T_HsAppKeyHandler::callDataReceived()
       
    77 {
       
    78     // just call unused part of HbDeviceDialogMonitor interface
       
    79     CHbSymbianVariantMap *dummy;
       
    80     TRAPD(errNo, dummy = CHbSymbianVariantMap::NewL());
       
    81     
       
    82     QCOMPARE(errNo, KErrNone);
       
    83     QVERIFY(dummy);
       
    84 
       
    85     mCHsAppKeyPluginInstance->DataReceived(*dummy);
       
    86     delete dummy;
       
    87 }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void T_HsAppKeyHandler::testProvideKeyEvents()
       
    93 {    
       
    94     RArray<TKeyEvent> keyEventArray;
       
    95     CleanupClosePushL(keyEventArray);
       
    96 
       
    97     TRAPD(errNo, mCHsAppKeyPluginInstance->ProvideKeyEventsL(keyEventArray));
       
    98     QCOMPARE(errNo, KErrNone);
       
    99 
       
   100     QCOMPARE(keyEventArray.Count(), 2);
       
   101 
       
   102     TKeyEvent shortPress = keyEventArray[0];
       
   103     QCOMPARE(shortPress.iCode, static_cast<TUint>(EKeyApplication0));
       
   104     QCOMPARE(shortPress.iScanCode, static_cast<TInt>(EStdKeyApplication0));
       
   105     QCOMPARE(shortPress.iModifiers, static_cast<TUint>(0));
       
   106     QCOMPARE(shortPress.iRepeats, 0);
       
   107     
       
   108     TKeyEvent longPress = keyEventArray[0];
       
   109     QCOMPARE(longPress.iCode, static_cast<TUint>(EKeyApplication0));
       
   110     QCOMPARE(longPress.iScanCode, static_cast<TInt>(EStdKeyApplication0));
       
   111     QCOMPARE(longPress.iModifiers, static_cast<TUint>(0));
       
   112     QCOMPARE(longPress.iRepeats, 0);
       
   113 
       
   114     CleanupStack::PopAndDestroy(&keyEventArray);
       
   115 }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void T_HsAppKeyHandler::testDeviceDialogClosed()
       
   121 {
       
   122     // @todo: verify that calling this method closes task switcher
       
   123     mCHsAppKeyPluginInstance->DeviceDialogClosed(0);
       
   124 }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void T_HsAppKeyHandler::testHandleKeyEventIgnoresKeysOtherThanAppKey()
       
   130 {
       
   131     TInt errNo(KErrNone);
       
   132     TKeyResponse keyResponse(EKeyWasNotConsumed);
       
   133 
       
   134     TKeyEvent invalidKeyEvent;
       
   135     invalidKeyEvent.iCode = !EKeyApplication0;
       
   136 
       
   137     TRAP(errNo, keyResponse = mCHsAppKeyPluginInstance->HandleKeyEventL(invalidKeyEvent, EEventNull));
       
   138     QCOMPARE(errNo, KErrNone);
       
   139     QCOMPARE(keyResponse, EKeyWasNotConsumed);
       
   140 }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void T_HsAppKeyHandler::testHandleKeyEventConsumesEverySecondEventForAppKey()
       
   146 {
       
   147     TInt errNo(KErrNone);
       
   148     TKeyResponse keyResponse(EKeyWasNotConsumed);
       
   149 
       
   150     // verify that for every supported key we consume every second event and ignore the others
       
   151     RArray<TKeyEvent> keyEventArray;
       
   152     CleanupClosePushL(keyEventArray);    
       
   153     TRAP(errNo, mCHsAppKeyPluginInstance->ProvideKeyEventsL(keyEventArray));
       
   154     QCOMPARE(errNo, KErrNone);
       
   155     
       
   156     for (TInt i(0); i != keyEventArray.Count(); ++i) {
       
   157         TKeyEvent keyEvent = keyEventArray[i];
       
   158         
       
   159         // first event ignored
       
   160         TRAP(errNo, keyResponse = mCHsAppKeyPluginInstance->HandleKeyEventL(keyEvent, EEventNull));
       
   161         QCOMPARE(errNo, KErrNone);
       
   162         QCOMPARE(keyResponse, EKeyWasNotConsumed);
       
   163 
       
   164         // second event consumed
       
   165         TRAP(errNo, keyResponse = mCHsAppKeyPluginInstance->HandleKeyEventL(keyEvent, EEventNull));
       
   166         QCOMPARE(errNo, KErrNone);
       
   167         QCOMPARE(keyResponse, EKeyWasConsumed);
       
   168     }    
       
   169     
       
   170     CleanupStack::PopAndDestroy(&keyEventArray);
       
   171 }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void T_HsAppKeyHandler::testShortPressSwitchesBetweenHsAndApplibWhenTsIsNotVisible()
       
   177 {
       
   178     // verify preconditions
       
   179     QCOMPARE(isTaskSwitcherVisible(), false);
       
   180 
       
   181     QValueSpaceSubscriber hsStateGetter(QString("%1/%2").arg(HsStatePSKeyPath).arg(HsStatePSKeySubPath));
       
   182     QVERIFY(hsStateGetter.isConnected());
       
   183     
       
   184     // make sure we start from homescreen
       
   185     if (hsStateGetter.value().toInt() != EHomeScreenIdleState) {
       
   186         QVERIFY(makeShortPress()); 
       
   187     }
       
   188     
       
   189     QVariant initialState = hsStateGetter.value();
       
   190     
       
   191     QVERIFY(makeShortPress()); 
       
   192     QVERIFY(hsStateGetter.value() != initialState);
       
   193 
       
   194     QVERIFY(makeShortPress());   
       
   195     QVERIFY(hsStateGetter.value() == initialState);    
       
   196 }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void T_HsAppKeyHandler::testLongPressShowsTaskSwitcher()
       
   202 {
       
   203     // verify preconditions
       
   204     QCOMPARE(isTaskSwitcherVisible(), false);
       
   205     
       
   206     QVERIFY(makeLongPress());
       
   207     QCOMPARE(isTaskSwitcherVisible(), true);    
       
   208     
       
   209     // second longpress does nothing
       
   210     QVERIFY(makeLongPress());
       
   211     QCOMPARE(isTaskSwitcherVisible(), true);    
       
   212 }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 void T_HsAppKeyHandler::testShortPressDismissesTaskSwitcherShownByLongpress()
       
   218 {
       
   219     // verify preconditions
       
   220     QCOMPARE(isTaskSwitcherVisible(), false);
       
   221     
       
   222     QVERIFY(makeLongPress());
       
   223     QCOMPARE(isTaskSwitcherVisible(), true);    
       
   224     
       
   225     QVERIFY(makeShortPress());
       
   226     QCOMPARE(isTaskSwitcherVisible(), false);    
       
   227 }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void T_HsAppKeyHandler::testLongpressDoesNothingIfTaskSwitcherIsShownByOtherComponent()
       
   233 {
       
   234     // verify preconditions
       
   235     QCOMPARE(isTaskSwitcherVisible(), false);
       
   236     QVERIFY(!mDialog);
       
   237     
       
   238     QVERIFY(showTaskSwitcher());
       
   239     QCOMPARE(isTaskSwitcherVisible(), true);
       
   240     
       
   241     QVERIFY(makeLongPress());
       
   242     QCOMPARE(isTaskSwitcherVisible(), true);    
       
   243 }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 void T_HsAppKeyHandler::testShortPressDismissesTaskSwitcherShownByOtherComponent()
       
   249 {
       
   250     // verify preconditions
       
   251     QCOMPARE(isTaskSwitcherVisible(), false);
       
   252     QVERIFY(!mDialog);
       
   253     
       
   254     QVERIFY(showTaskSwitcher());
       
   255     QCOMPARE(isTaskSwitcherVisible(), true);
       
   256     
       
   257     // after short press the TaskSwitcher will call DeviceDialogClosed callback
       
   258     // of MHbDeviceDialogObserver, which should delete and clear mDialog member.
       
   259     QVERIFY(makeShortPress());
       
   260     QTest::qWait(1000);
       
   261     QCOMPARE(isTaskSwitcherVisible(), false);    
       
   262     QVERIFY(!mDialog);
       
   263 }
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 //  HELPER MEHTODS
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 
       
   270 bool T_HsAppKeyHandler::makeShortPress()
       
   271 {
       
   272     TKeyEvent shortpress = {EKeyApplication0, EStdKeyApplication0, 0, 0};        
       
   273     return sendKeyEventTwice(shortpress);
       
   274 }
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 bool T_HsAppKeyHandler::makeLongPress()
       
   280 {
       
   281     TKeyEvent longpress = {EKeyApplication0, EStdKeyApplication0, 0, 1};        
       
   282     return sendKeyEventTwice(longpress);
       
   283 }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 bool T_HsAppKeyHandler::sendKeyEventTwice(TKeyEvent keyEvent)
       
   289 {
       
   290     Q_ASSERT(mCHsAppKeyPluginInstance);
       
   291     
       
   292     TKeyResponse keyResponse(EKeyWasNotConsumed);
       
   293     // send key event twice (HandleKeyEventL accepts every second event) 
       
   294     TRAPD(errNo,
       
   295         mCHsAppKeyPluginInstance->HandleKeyEventL(keyEvent, EEventNull);
       
   296         keyResponse = mCHsAppKeyPluginInstance->HandleKeyEventL(keyEvent, EEventNull);
       
   297     );
       
   298     
       
   299     return (KErrNone == errNo) && (EKeyWasConsumed == keyResponse);
       
   300 }
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 bool T_HsAppKeyHandler::isTaskSwitcherVisible()
       
   306 {
       
   307     QValueSpaceSubscriber tsVisibilityGetter(QString("%1/%2").arg(TsProperty::KTsPath).arg(TsProperty::KVisibilityPath));
       
   308     Q_ASSERT(tsVisibilityGetter.isConnected());
       
   309     
       
   310     QVariant tsVisibility = tsVisibilityGetter.value();
       
   311     Q_ASSERT(tsVisibility.isValid());
       
   312     return tsVisibility.toBool();
       
   313 }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 bool T_HsAppKeyHandler::showTaskSwitcher()
       
   319 {
       
   320     mDialog = CHbDeviceDialogSymbian::NewL();    
       
   321     QScopedPointer<CHbSymbianVariantMap> params(CHbSymbianVariantMap::NewL());
       
   322     return KErrNone == mDialog->Show(KTsPluginName, *params, this);
       
   323 }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // MHbDeviceDialogObserver implementation
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 void T_HsAppKeyHandler::DataReceived(CHbSymbianVariantMap &/*aData*/)
       
   330 {
       
   331     //no implementation required
       
   332 }
       
   333 
       
   334 void T_HsAppKeyHandler::DeviceDialogClosed(TInt /*aCompletionCode*/)
       
   335 {
       
   336     delete mDialog;
       
   337     mDialog = 0;
       
   338 }
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 QTEST_MAIN(T_HsAppKeyHandler)