satui/satapp/tsrc/ut_satapp/src/ut_satappaction.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: Unit test for satappmainhandler
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QVariant>
       
    19 #include <QTimer>
       
    20 #include <badesca.h>
       
    21 
       
    22 #include <hbmainwindow.h>
       
    23 #include <hblistwidget.h>
       
    24 
       
    25 #include "ut_satappaction.h"
       
    26 #include "satappaction.h"
       
    27 
       
    28 
       
    29 
       
    30 // --------------------------------------------------------
       
    31 // The unit tests begin here
       
    32 // --------------------------------------------------------
       
    33 
       
    34 Ut_SatAppAction::Ut_SatAppAction(QObject *parent)
       
    35     :QObject(parent)
       
    36 {
       
    37 }
       
    38 
       
    39 
       
    40 void Ut_SatAppAction::testAction()
       
    41 {
       
    42     SatAppAction a(ESatDisplayTextAction);
       
    43     SatAppAction b(ESatGetInputAction);
       
    44     SatAppAction c(ESatPlayToneAction);
       
    45     
       
    46     QCOMPARE(a.action(),(int)ESatDisplayTextAction);
       
    47     QCOMPARE(b.action(),(int)ESatGetInputAction);
       
    48     QCOMPARE(c.action(),(int)ESatPlayToneAction);
       
    49 
       
    50 }
       
    51 
       
    52 void Ut_SatAppAction::testParams()
       
    53 {
       
    54     SatAppAction a(ESatDisplayTextAction);
       
    55     // qvariant
       
    56     a.set(KeyText, QVariant("hello"));
       
    57     QCOMPARE(a.value(KeyText).toString(),QString("hello"));
       
    58 
       
    59     // int
       
    60     a.set(KeyMinLength, 25);
       
    61     QCOMPARE(a.value(KeyMinLength).toInt(),25);
       
    62 
       
    63     // string
       
    64     a.set(KeyInputString,_L("kiss"));
       
    65     QCOMPARE(a.value(KeyInputString).toString(),QString("kiss"));
       
    66 
       
    67     // descriptor array
       
    68     CDesCArrayFlat* array1 = new CDesCArrayFlat(8);
       
    69     array1->AppendL(_L("item_0"));
       
    70     array1->AppendL(_L("item_1"));
       
    71     array1->AppendL(_L("item_2"));
       
    72     a.set(KeyMenu,*array1);
       
    73     QList<QVariant> list1 = a.value(KeyMenu).toList();
       
    74     QCOMPARE(list1.size(), 3);
       
    75     QCOMPARE(list1[0].toString(), QString("item_0"));
       
    76     QCOMPARE(list1[1].toString(), QString("item_1"));
       
    77     QCOMPARE(list1[2].toString(), QString("item_2"));
       
    78 
       
    79     // int array
       
    80     CArrayFixFlat<TInt>* array2 = new CArrayFixFlat<TInt>(8);
       
    81     array2->AppendL(1);
       
    82     array2->AppendL(2);
       
    83     array2->AppendL(3);
       
    84     a.set(KeyIconIdArray,*array2);
       
    85     QList<QVariant> list2 = a.value(KeyIconIdArray).toList();
       
    86     QCOMPARE(list2.size(), 3);
       
    87     QCOMPARE(list2[0].toInt(), 1);
       
    88     QCOMPARE(list2[1].toInt(), 2);
       
    89     QCOMPARE(list2[2].toInt(), 3);
       
    90 
       
    91     // TTimeIntervalSeconds
       
    92     TTimeIntervalSeconds seconds(3);
       
    93     a.set(KeyDuration,seconds);
       
    94     QCOMPARE(a.value(KeyDuration).toInt(),3000);
       
    95     
       
    96     // TTimeIntervalMicroSeconds
       
    97     TTimeIntervalMicroSeconds ysec(TInt64(500000));
       
    98     a.set(KeyDuration,ysec);
       
    99     QCOMPARE(a.value(KeyDuration).toInt(),500);
       
   100     
       
   101     // test hasValue
       
   102     QVERIFY(a.hasValue(KeyText));
       
   103     QVERIFY(a.hasValue(KeyMinLength));
       
   104     QVERIFY(a.hasValue(KeyInputString));
       
   105     QVERIFY(a.hasValue(KeyMenu));
       
   106     QVERIFY(a.hasValue(KeyIconIdArray));
       
   107     QVERIFY(a.hasValue(KeyDuration));
       
   108     QVERIFY(!a.hasValue(KeyHelpRequested)); // <-- not set
       
   109     QVERIFY(!a.hasValue(KeyHelpRequested)); // <-- not set
       
   110     QVERIFY(!a.hasValue(KeyCharacterSet)); // <-- not set
       
   111     QVERIFY(!a.hasValue(QString("billy the kid"))); // <-- definitely not set
       
   112 }
       
   113 
       
   114 void Ut_SatAppAction::testComplete()
       
   115 {
       
   116     SatAppAction a(ESatDisplayTextAction);
       
   117     SatAppAction b(ESatGetInputAction);
       
   118     SatAppAction c(ESatPlayToneAction);
       
   119     SatAppAction d(ESatPlayToneAction);
       
   120     SatAppAction e(ESatPlayToneAction);
       
   121     a.complete();
       
   122     b.completeWithSuccess();
       
   123     c.completeWithFailure();
       
   124     d.completeWithBackRequested();
       
   125     e.completeWithSessionTerminated();
       
   126     QCOMPARE(a.response(),ESatNoResponseFromUser);
       
   127     QCOMPARE(b.response(),ESatSuccess);
       
   128     QCOMPARE(c.response(),ESatFailure);
       
   129     QCOMPARE(d.response(),ESatBackwardModeRequestedByUser);
       
   130     QCOMPARE(e.response(),ESatSessionTerminatedByUser);
       
   131 }
       
   132 
       
   133 void Ut_SatAppAction::testCompleteSignal()
       
   134 {
       
   135     SatAppAction a(ESatDisplayTextAction);
       
   136     SatAppAction b(ESatGetInputAction);
       
   137     SatAppAction c(ESatPlayToneAction);
       
   138     SatAppAction d(ESatPlayToneAction);
       
   139     SatAppAction e(ESatPlayToneAction);
       
   140     ActionHelper x;
       
   141     connect(&a, SIGNAL(actionCompleted(SatAppAction*)), &x, SLOT(inc()));
       
   142     connect(&b, SIGNAL(actionCompleted(SatAppAction*)), &x, SLOT(inc()));
       
   143     connect(&c, SIGNAL(actionCompleted(SatAppAction*)), &x, SLOT(inc()));
       
   144     connect(&d, SIGNAL(actionCompleted(SatAppAction*)), &x, SLOT(inc()));
       
   145     connect(&e, SIGNAL(actionCompleted(SatAppAction*)), &x, SLOT(inc()));
       
   146     a.complete();
       
   147     b.completeWithSuccess();
       
   148     c.completeWithFailure();
       
   149     d.completeWithBackRequested();
       
   150     e.completeWithSessionTerminated();
       
   151     QCOMPARE(x.count(),5);
       
   152 }
       
   153 
       
   154 void Ut_SatAppAction::testDoubleComplete()
       
   155 {
       
   156     SatAppAction a(ESatDisplayTextAction);
       
   157     a.completeWithSuccess();
       
   158     QCOMPARE(a.response(),ESatSuccess);
       
   159     a.completeWithFailure(); // <-- no effect because already completed
       
   160     QCOMPARE(a.response(),ESatSuccess); // <-- still in same state!
       
   161 }
       
   162 
       
   163 void Ut_SatAppAction::testAsyncActionRobustness()
       
   164 {
       
   165     // this function tests if an action can be safely deleted in the "completed"
       
   166     // slot, at the same time testing robustness and memory leaks by repeating
       
   167     // the sequence multiple times
       
   168     ActionHelper x;
       
   169     for(int i=0; i<10000; ++i)
       
   170     {
       
   171         SatAppAction* a = new SatAppAction(ESatDisplayTextAction, this);
       
   172         connect(a,SIGNAL(actionCompleted(SatAppAction*)),&x,SLOT(del(SatAppAction*)));
       
   173         a->complete();
       
   174     }
       
   175 }
       
   176 
       
   177 
       
   178 void Ut_SatAppAction::testWait()
       
   179 {
       
   180     SatAppAction a(ESatDisplayTextAction);
       
   181     QTimer::singleShot(1000, &a, SLOT(completeWithSuccess()));
       
   182     // this should last 1 second
       
   183     a.waitUntilCompleted();
       
   184     QCOMPARE(a.response(),ESatSuccess);
       
   185 
       
   186     // this should flow through directly, because already completed
       
   187     a.waitUntilCompleted();
       
   188     QCOMPARE(a.response(),ESatSuccess); // <-- response remains unchanged
       
   189     
       
   190 }
       
   191 
       
   192 
       
   193 
       
   194 // helper
       
   195 void ActionHelper::del(SatAppAction* action)
       
   196 {
       
   197     delete action;
       
   198 }
       
   199 
       
   200 
       
   201 // End of file