camerauis/cameraxui/cxengine/tsrc/unit/unittest_cxetestutils/cxedummystatemachine.cpp
branchRCL_3
changeset 23 61bc0f252b2b
equal deleted inserted replaced
22:f54ad444594d 23:61bc0f252b2b
       
     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 <QTimer>
       
    19 #include "cxedummystatemachine.h"
       
    20 #include "cxestate.h"
       
    21 
       
    22 
       
    23 /*!
       
    24     \class CxeDummyStateMachine
       
    25     \brief Dummy state machine for testing CxeTestUtils::waitForState()
       
    26 */
       
    27 
       
    28 CxeDummyStateMachine::CxeDummyStateMachine() : CxeStateMachine("dummy state machine")
       
    29 {
       
    30     qRegisterMetaType<CxeDummyStateMachine::State>();
       
    31     addState(new CxeState(A, "A", B|C));
       
    32     addState(new CxeState(B, "B", A|C));
       
    33     addState(new CxeState(C, "C", A|B));
       
    34     setInitialState(A);
       
    35 }
       
    36 
       
    37 CxeDummyStateMachine::~CxeDummyStateMachine()
       
    38 {
       
    39     // no implementation needed here
       
    40 }
       
    41 
       
    42 CxeDummyStateMachine::State CxeDummyStateMachine::state() const
       
    43 {
       
    44     return static_cast<State>(stateId());
       
    45 }
       
    46 
       
    47 void CxeDummyStateMachine::handleStateChanged(int /*newStateId*/, CxeError::Id error)
       
    48 {
       
    49     emit stateChanged(state(), error);
       
    50 }
       
    51 
       
    52 /*!
       
    53     Do an internal state change after a given delay. Multiple state changes
       
    54     can be queued and they are executed in the same order delayedStateChange()
       
    55     was called. The delay between queued state changes is determined by the
       
    56     last delayedStateChange() call.
       
    57 
       
    58     @param targetState Target state for transition (A, B, or C)
       
    59     @param ms          Delay until state change (milliseconds)
       
    60 */
       
    61 void CxeDummyStateMachine::delayedStateChange(State targetState, int ms)
       
    62 {
       
    63     mTargetStateQueue.enqueue(targetState);
       
    64     mDelay = ms;
       
    65     QTimer::singleShot(ms, this, SLOT(timeout()));
       
    66 }
       
    67 
       
    68 void CxeDummyStateMachine::timeout()
       
    69 {
       
    70     if (!mTargetStateQueue.isEmpty()) {
       
    71         if (mDelay == 0) {
       
    72             // No delay, instant changes
       
    73             foreach(State s, mTargetStateQueue) {
       
    74                 setState(s);
       
    75             }
       
    76             mTargetStateQueue.clear();
       
    77         } else {
       
    78             // One state change at a time
       
    79             setState(mTargetStateQueue.dequeue());
       
    80         }
       
    81     }
       
    82     if (!mTargetStateQueue.isEmpty()) {
       
    83         QTimer::singleShot(mDelay, this, SLOT(timeout()));
       
    84     }
       
    85 }