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