1 /* |
|
2 * Copyright (c) 2009-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: |
|
15 * |
|
16 */ |
|
17 #include <QTest> |
|
18 |
|
19 #include "unittest_cxestatemachine.h" |
|
20 #include "cxestate.h" |
|
21 #include "cxestatemachineunit.h" |
|
22 |
|
23 // ----------------------------------- |
|
24 // CxsStateMachine |
|
25 // ----------------------------------- |
|
26 UnitTestCxeStateMachine::UnitTestCxeStateMachine() |
|
27 : mStateMachine(NULL), |
|
28 mStartState(NULL), |
|
29 mMiddleState(NULL), |
|
30 mEndState(NULL), |
|
31 mIsolatedState(NULL) |
|
32 { |
|
33 qRegisterMetaType<CxeError::Id>("CxeError::Id"); |
|
34 } |
|
35 |
|
36 UnitTestCxeStateMachine::~UnitTestCxeStateMachine() |
|
37 { |
|
38 } |
|
39 |
|
40 // Run before each individual test case |
|
41 void UnitTestCxeStateMachine::init() |
|
42 { |
|
43 // create instance of the unit to be tested |
|
44 mStateMachine = new CxeStateMachineUnit("UnitTestCxeStateMachine"); |
|
45 |
|
46 // create test data (CxeState class is tested separated, so we assume it works correctly) |
|
47 // these state classes are according to specification, |
|
48 // error input is created in that particular test case |
|
49 mStartState = new CxeState(StartId, "Start", MiddleId); // starting state |
|
50 mMiddleState = new CxeState(MiddleId, "Middle", StartId|EndId|MiddleId); // middle state, "normal" |
|
51 mEndState = new CxeState(EndId, "End", 0); // cannot transfer to other states |
|
52 mIsolatedState = new CxeState(IsolatedId, "Isolated", MiddleId); // cannot be set after any other state |
|
53 |
|
54 QVERIFY(mStateMachine->addState(mStartState)); |
|
55 QVERIFY(mStateMachine->addState(mMiddleState)); |
|
56 QVERIFY(mStateMachine->addState(mEndState)); |
|
57 QVERIFY(mStateMachine->addState(mIsolatedState)); |
|
58 } |
|
59 |
|
60 // Run after each individual test case |
|
61 void UnitTestCxeStateMachine::cleanup() |
|
62 { |
|
63 // state machine takes ownership of the states when states are added to statemachine |
|
64 // those are deleted when statemachine is deleted |
|
65 delete mStateMachine; |
|
66 mStateMachine = NULL; |
|
67 |
|
68 mStartState = NULL; |
|
69 mMiddleState = NULL; |
|
70 mEndState = NULL; |
|
71 mIsolatedState = NULL; |
|
72 } |
|
73 |
|
74 |
|
75 void UnitTestCxeStateMachine::testAddState(){ |
|
76 |
|
77 // test error cases ( OK cases are tested in init ) |
|
78 CxeState* mErrorState = NULL; |
|
79 |
|
80 // add same state twice -> NOK |
|
81 QVERIFY(!mStateMachine->addState(mStartState)); |
|
82 |
|
83 // stateId should not have overlapping bits, because we need to be able to use bitwise |
|
84 // AND operator -> NOK |
|
85 mErrorState = new CxeState(5, "Overlapping id", StartId|MiddleId); |
|
86 QVERIFY(!mStateMachine->addState(mErrorState)); |
|
87 delete mErrorState; |
|
88 |
|
89 // add state with negative id -> NOK |
|
90 mErrorState = new CxeState(-1, "Negative id", StartId|MiddleId); |
|
91 QVERIFY(!mStateMachine->addState(mErrorState)); |
|
92 delete mErrorState; |
|
93 |
|
94 // add null state -> NOK |
|
95 QVERIFY(!mStateMachine->addState(NULL)); |
|
96 } |
|
97 |
|
98 void UnitTestCxeStateMachine::testSetState() |
|
99 { |
|
100 |
|
101 // CxeStateMachine::setState |
|
102 // sets a state, verifies the that change is allowed, informs about the state change |
|
103 // NOTE: check that handleStateChange is called, when state changes (correct parameters) |
|
104 // NOTE: check that correct stateid is returned after change |
|
105 // NOTE: verifyStateChange has separate testing |
|
106 |
|
107 // reset the counter for handleStateChanged slot |
|
108 mStateMachine->mHandleStateChangedCounter = 0; |
|
109 |
|
110 // set state, when initial state is not set -> NOK |
|
111 // check that handleStateChange is not called |
|
112 // current state is undefined |
|
113 QVERIFY(!mStateMachine->setState(StartId)); |
|
114 QCOMPARE(mStateMachine->mHandleStateChangedCounter, 0); |
|
115 QCOMPARE(mStateMachine->stateId(), 0); // 0 - undefined state |
|
116 |
|
117 // set start state as initial state, change to another state (allowed change) -> OK |
|
118 // (there should be a call to state change, check the error code) |
|
119 mStateMachine->setInitialState(StartId); |
|
120 QVERIFY(mStateMachine->setState(MiddleId, CxeError::None)); |
|
121 QCOMPARE(mStateMachine->mHandleStateChangedCounter, 1); |
|
122 QVERIFY(mStateMachine->mStateChangeStateId == MiddleId); |
|
123 QCOMPARE(mStateMachine->mStateChangeErrorId, CxeError::None); |
|
124 QVERIFY(mStateMachine->stateId() == MiddleId); |
|
125 |
|
126 // set another state (allowed change) -> OK |
|
127 // check the error code is correct one |
|
128 QVERIFY(mStateMachine->setState(EndId, CxeError::NotFound)); |
|
129 QCOMPARE(mStateMachine->mHandleStateChangedCounter, 2); |
|
130 QVERIFY(mStateMachine->mStateChangeStateId == EndId); |
|
131 QCOMPARE(mStateMachine->mStateChangeErrorId, CxeError::NotFound); |
|
132 QVERIFY(mStateMachine->stateId() == EndId); |
|
133 |
|
134 // reset the counter for handleStateChanged slot |
|
135 mStateMachine->mHandleStateChangedCounter = 0; |
|
136 |
|
137 // set same state again -> OK |
|
138 // but state change should not be called |
|
139 QVERIFY(mStateMachine->setState(EndId, CxeError::None)); |
|
140 QCOMPARE(mStateMachine->mHandleStateChangedCounter, 0); |
|
141 |
|
142 // setstate to a state that does not exists -> NOK |
|
143 // state has not changed |
|
144 QVERIFY(mStateMachine->stateId() == EndId); |
|
145 QVERIFY(!mStateMachine->setState(UnknownId)); |
|
146 QCOMPARE(mStateMachine->mHandleStateChangedCounter, 0); |
|
147 QVERIFY(mStateMachine->stateId() == EndId); |
|
148 |
|
149 // state change verification method has own test cases |
|
150 } |
|
151 |
|
152 void UnitTestCxeStateMachine::testSetInitialState() |
|
153 { |
|
154 // CxeStateMachine::setInitialState |
|
155 // sets a initial state |
|
156 // check that handleStateChange is not called |
|
157 // check that correct state has been set |
|
158 |
|
159 // set state, when initial state is not set -> OK |
|
160 QVERIFY(mStateMachine->setInitialState(StartId)); |
|
161 QVERIFY(mStateMachine->stateId() == StartId); |
|
162 QCOMPARE(mStateMachine->mHandleStateChangedCounter, 0); |
|
163 |
|
164 // set state, when initial state is already set -> NOK |
|
165 QVERIFY(!mStateMachine->setInitialState(MiddleId)); |
|
166 QVERIFY(mStateMachine->stateId() == StartId); |
|
167 QCOMPARE(mStateMachine->mHandleStateChangedCounter, 0); |
|
168 |
|
169 } |
|
170 |
|
171 void UnitTestCxeStateMachine::testStateId() |
|
172 { |
|
173 |
|
174 // check state id, when initial state not set -> stateid == 0 |
|
175 QCOMPARE(mStateMachine->stateId(), 0); |
|
176 |
|
177 // setinitialstate -> check state id |
|
178 mStateMachine->setInitialState(StartId); |
|
179 QVERIFY(mStateMachine->stateId() == StartId); |
|
180 |
|
181 } |
|
182 |
|
183 void UnitTestCxeStateMachine::testVerifyStateChange() |
|
184 { |
|
185 // test change possibilities |
|
186 |
|
187 // initial START -> MIDDLE -> OK |
|
188 // START -> END -> NOK |
|
189 // START -> ISOLATED -> NOK |
|
190 // START -> 0 -> NOK |
|
191 // START -> UnknownId -> NOK |
|
192 // START -> START -> NOK (since START is not defined in allowed next state) |
|
193 mStateMachine->setInitialState(StartId); |
|
194 QVERIFY(mStateMachine->verifyStateChange(MiddleId)); |
|
195 QVERIFY(!mStateMachine->verifyStateChange(EndId)); |
|
196 QVERIFY(!mStateMachine->verifyStateChange(IsolatedId)); |
|
197 QVERIFY(!mStateMachine->verifyStateChange(0)); |
|
198 QVERIFY(!mStateMachine->verifyStateChange(UnknownId)); |
|
199 QVERIFY(!mStateMachine->verifyStateChange(StartId)); |
|
200 |
|
201 // MIDDLE -> ISOLATED -> NOK |
|
202 // MIDDLE -> START -> OK |
|
203 // MIDDLE -> END -> OK |
|
204 // MIDDLE -> 0 -> NOK |
|
205 // MIDDLE -> MIDDLE -> OK (defined in allowed next states) |
|
206 mStateMachine->setState(MiddleId); |
|
207 QVERIFY(!mStateMachine->verifyStateChange(IsolatedId)); |
|
208 QVERIFY(mStateMachine->verifyStateChange(StartId)); |
|
209 QVERIFY(mStateMachine->verifyStateChange(EndId)); |
|
210 QVERIFY(!mStateMachine->verifyStateChange(0)); |
|
211 |
|
212 // END -> MIDDLE -> NOK |
|
213 // END -> START -> NOK |
|
214 // END -> 0 -> NOK |
|
215 // END -> UnknownId -> NOK |
|
216 mStateMachine->setState(EndId); |
|
217 QVERIFY(!mStateMachine->verifyStateChange(MiddleId)); |
|
218 QVERIFY(!mStateMachine->verifyStateChange(StartId)); |
|
219 QVERIFY(!mStateMachine->verifyStateChange(0)); |
|
220 QVERIFY(!mStateMachine->verifyStateChange(UnknownId)); |
|
221 |
|
222 } |
|
223 |
|
224 |
|
225 // main() function non-GUI testing |
|
226 QTEST_APPLESS_MAIN(UnitTestCxeStateMachine); |
|
227 |
|