camerauis/cameraxui/cxengine/tsrc/unit/unittest_cxetestutils/unittest_cxetestutils.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 23 61bc0f252b2b
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
     1 /*
       
     2 * Copyright (c) 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 <QTest>
       
    19 #include <QTimer>
       
    20 #include <QSignalSpy>
       
    21 #include "unittest_cxetestutils.h"
       
    22 #include "cxetestutils.h"
       
    23 #include "cxedummystatemachine.h"
       
    24 
       
    25 UnitTestCxeTestUtils::UnitTestCxeTestUtils()
       
    26 {
       
    27 }
       
    28 
       
    29 UnitTestCxeTestUtils::~UnitTestCxeTestUtils()
       
    30 {
       
    31 }
       
    32 
       
    33 /*!
       
    34     Signal should be received within timeout
       
    35 */
       
    36 void UnitTestCxeTestUtils::testWaitForSignal1()
       
    37 {
       
    38     QTimer t1;
       
    39     QSignalSpy spy1(&t1, SIGNAL(timeout()));
       
    40     t1.setSingleShot(true);
       
    41     t1.start(100);
       
    42     QVERIFY(CxeTestUtils::waitForSignal(spy1, 200));
       
    43 }
       
    44 
       
    45 /*!
       
    46     Test for signal timeout
       
    47 */
       
    48 void UnitTestCxeTestUtils::testWaitForSignal2()
       
    49 {
       
    50     QTimer t2;
       
    51     QSignalSpy spy2(&t2, SIGNAL(timeout()));
       
    52     t2.setSingleShot(true);
       
    53     t2.start(1000);
       
    54     QVERIFY(!CxeTestUtils::waitForSignal(spy2, 100));
       
    55 }
       
    56 
       
    57 /*!
       
    58     Wait for multiple (3) signals
       
    59 */
       
    60 void UnitTestCxeTestUtils::testWaitForSignal3()
       
    61 {
       
    62     QTimer t3;
       
    63     QSignalSpy spy3(&t3, SIGNAL(timeout()));
       
    64     t3.setSingleShot(false);
       
    65     t3.start(100);
       
    66     QVERIFY(CxeTestUtils::waitForSignal(spy3, 500, 3));
       
    67     QCOMPARE(spy3.count(), 3);
       
    68 }
       
    69 
       
    70 /*!
       
    71     Verify that extra signals should not be tolerated if allowExtraSignals
       
    72     is false
       
    73 */
       
    74 void UnitTestCxeTestUtils::testWaitForSignal4()
       
    75 {
       
    76     QTimer t4;
       
    77     QSignalSpy spy4(&t4, SIGNAL(timeout()));
       
    78     t4.setSingleShot(false);
       
    79     t4.start(10);
       
    80 
       
    81     // Wait until we get enough signals from the timer
       
    82     while (spy4.count() < 4) {
       
    83         QTest::qWait(10);
       
    84     }
       
    85 
       
    86     // waitForSignal() should return false due to too many signals
       
    87     QVERIFY(!CxeTestUtils::waitForSignal(spy4, 500, 3, false));
       
    88 }
       
    89 
       
    90 /*!
       
    91     Test a successful state change
       
    92 */
       
    93 void UnitTestCxeTestUtils::testWaitForState1()
       
    94 {
       
    95     CxeDummyStateMachine m1;
       
    96     m1.delayedStateChange(CxeDummyStateMachine::B, 100);
       
    97     QVERIFY(CxeTestUtils::waitForState<CxeDummyStateMachine>(
       
    98         m1, CxeDummyStateMachine::B, 200));
       
    99 }
       
   100 
       
   101 /*!
       
   102     // Test state change time out
       
   103 */
       
   104 void UnitTestCxeTestUtils::testWaitForState2()
       
   105 {
       
   106     CxeDummyStateMachine m1;
       
   107     m1.delayedStateChange(CxeDummyStateMachine::C, 300);
       
   108     QVERIFY(!CxeTestUtils::waitForState<CxeDummyStateMachine>(
       
   109         m1, CxeDummyStateMachine::C, 100));
       
   110 }
       
   111 
       
   112 /*!
       
   113     // Test a very quick state change
       
   114 */
       
   115 void UnitTestCxeTestUtils::testWaitForState3()
       
   116 {
       
   117     CxeDummyStateMachine m1;
       
   118     m1.delayedStateChange(CxeDummyStateMachine::B, 0);
       
   119     m1.delayedStateChange(CxeDummyStateMachine::C, 0);
       
   120 
       
   121     // Even though the state changes from A to B to C immediately without
       
   122     // allowing Qt to process events, it should be possible to catch the
       
   123     // transition to state B using waitForState().
       
   124 
       
   125     // waitForState() should never miss state transitions.
       
   126 
       
   127     QVERIFY(CxeTestUtils::waitForState<CxeDummyStateMachine>(
       
   128         m1, CxeDummyStateMachine::B, 100));
       
   129 }
       
   130 
       
   131 QTEST_MAIN(UnitTestCxeTestUtils);