|
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: Test class for CalenDayContentScrollArea |
|
15 * |
|
16 */ |
|
17 #include <QGraphicsItem> |
|
18 #include <QGraphicsScene> |
|
19 #include <QtTest/QtTest> |
|
20 #include <QGesture> |
|
21 #include <QPanGesture> |
|
22 |
|
23 #include <HbStyleLoader> |
|
24 #include <HbWidget> |
|
25 #include <HbSwipeGesture> |
|
26 |
|
27 #define private public |
|
28 #define protected public |
|
29 |
|
30 #include "calendaycontentscrollarea.h" |
|
31 |
|
32 // Test variables |
|
33 qreal gTestScrollAreaWidth = 300; |
|
34 qreal gTestScrollAreaHeight = 200; |
|
35 QRectF gTestWindowRect = QRectF(0, 0, gTestScrollAreaWidth, gTestScrollAreaHeight); |
|
36 Qt::Orientation gTestOrientation = Qt::Vertical; |
|
37 Qt::GestureState gTestGestureState = Qt::NoGesture; |
|
38 qreal gTestSceneSwipeAngle = 0.0; |
|
39 QSwipeGesture::SwipeDirection gTestSceneHorizontalDirection = QSwipeGesture::NoDirection; |
|
40 |
|
41 class TestCalenDayContentScrollArea : public QObject |
|
42 { |
|
43 Q_OBJECT |
|
44 |
|
45 public: |
|
46 TestCalenDayContentScrollArea(); |
|
47 virtual ~TestCalenDayContentScrollArea(); |
|
48 |
|
49 private slots: |
|
50 void initTestCase(); |
|
51 void cleanupTestCase(); |
|
52 void init(); |
|
53 void cleanup(); |
|
54 |
|
55 void testConstructors(); |
|
56 void testScrollToMiddleWidget(); |
|
57 void testScrollByAmount(); |
|
58 void testGestureEvent(); |
|
59 void testEventFilter(); |
|
60 void testEvent(); |
|
61 void testMoveTo(); |
|
62 void testHorizontalSwipe(); |
|
63 void testMoveFinished(); |
|
64 void testOrientationChanged(); |
|
65 |
|
66 private: |
|
67 CalenDayContentScrollArea *mContentScrollArea; |
|
68 }; |
|
69 |
|
70 /*! |
|
71 Constructor |
|
72 */ |
|
73 TestCalenDayContentScrollArea::TestCalenDayContentScrollArea() : |
|
74 mContentScrollArea(NULL) |
|
75 { |
|
76 |
|
77 } |
|
78 |
|
79 /*! |
|
80 Destructor |
|
81 */ |
|
82 TestCalenDayContentScrollArea::~TestCalenDayContentScrollArea() |
|
83 { |
|
84 |
|
85 } |
|
86 |
|
87 /*! |
|
88 Called before testcase |
|
89 */ |
|
90 void TestCalenDayContentScrollArea::initTestCase() |
|
91 { |
|
92 HbStyleLoader::registerFilePath(":/calendayhourelement.css"); |
|
93 HbStyleLoader::registerFilePath(":/calendayhourelement.widgetml"); |
|
94 HbStyleLoader::registerFilePath(":/calendayitem.css"); |
|
95 HbStyleLoader::registerFilePath(":/calendayitem.widgetml"); |
|
96 HbStyleLoader::registerFilePath(":/calendayeventspane.css"); |
|
97 } |
|
98 |
|
99 /*! |
|
100 Called after testcase |
|
101 */ |
|
102 void TestCalenDayContentScrollArea::cleanupTestCase() |
|
103 { |
|
104 |
|
105 } |
|
106 |
|
107 /*! |
|
108 Called before every function |
|
109 */ |
|
110 void TestCalenDayContentScrollArea::init() |
|
111 { |
|
112 // Create and configure scroll area |
|
113 mContentScrollArea = new CalenDayContentScrollArea(); |
|
114 mContentScrollArea->setScrollDirections(Qt::Horizontal | Qt::Vertical); |
|
115 mContentScrollArea->resize(gTestScrollAreaWidth, gTestScrollAreaHeight); |
|
116 |
|
117 // Create and set content widget |
|
118 qreal contentWidth = mContentScrollArea->mContentWidth; |
|
119 HbWidget *contentWidget = new HbWidget(); |
|
120 contentWidget->resize(3*contentWidth, 2*gTestScrollAreaHeight); |
|
121 mContentScrollArea->setContentWidget(contentWidget); |
|
122 } |
|
123 |
|
124 /*! |
|
125 Called after everyfunction |
|
126 */ |
|
127 void TestCalenDayContentScrollArea::cleanup() |
|
128 { |
|
129 if (mContentScrollArea) { |
|
130 delete mContentScrollArea; |
|
131 mContentScrollArea = NULL; |
|
132 } |
|
133 } |
|
134 |
|
135 /*! |
|
136 Test function for constructors |
|
137 1. Test if content scroll area is not initialized |
|
138 2. Test if content scroll area is correcty created |
|
139 */ |
|
140 void TestCalenDayContentScrollArea::testConstructors() |
|
141 { |
|
142 //1) |
|
143 CalenDayContentScrollArea *testContentScrollArea = 0; |
|
144 QVERIFY(!testContentScrollArea); |
|
145 |
|
146 testContentScrollArea = new CalenDayContentScrollArea(); |
|
147 QVERIFY(testContentScrollArea); |
|
148 |
|
149 delete testContentScrollArea; |
|
150 } |
|
151 |
|
152 /*! |
|
153 Test function for scrollToMiddleWidget |
|
154 1. Test if content widget is available |
|
155 2. Test initial position of content widget |
|
156 3. Test the position of content widget after scrolling |
|
157 */ |
|
158 void TestCalenDayContentScrollArea::testScrollToMiddleWidget() |
|
159 { |
|
160 //1) |
|
161 QVERIFY(mContentScrollArea->contentWidget()); |
|
162 |
|
163 //2) |
|
164 QPointF pos = mContentScrollArea->contentWidget()->pos(); |
|
165 QCOMPARE(pos.x(), abs(0.0)); |
|
166 QCOMPARE(pos.y(), abs(0.0)); |
|
167 |
|
168 //3) |
|
169 qreal contentWidth = mContentScrollArea->mContentWidth; |
|
170 mContentScrollArea->scrollToMiddleWidget(); |
|
171 pos = mContentScrollArea->contentWidget()->pos(); |
|
172 QCOMPARE(abs(pos.x()), abs(contentWidth)); |
|
173 QCOMPARE(abs(pos.y()), abs(0.0)); |
|
174 } |
|
175 |
|
176 /*! |
|
177 Test function for scrollToMiddleWidget |
|
178 1. Test if content widget is available |
|
179 2. Test initial position of content widget |
|
180 3. Test the position of content widget after vertical gesture |
|
181 4. Test the position of content widget after horizontal gesture |
|
182 5. Test the position of content widget after non-specified gesture |
|
183 */ |
|
184 void TestCalenDayContentScrollArea::testScrollByAmount() |
|
185 { |
|
186 //1) |
|
187 QVERIFY(mContentScrollArea->contentWidget()); |
|
188 |
|
189 //2) |
|
190 QPointF pos = mContentScrollArea->contentWidget()->pos(); |
|
191 QCOMPARE(abs(pos.x()), abs(0.0)); |
|
192 QCOMPARE(abs(pos.y()), abs(0.0)); |
|
193 |
|
194 //3) |
|
195 QPointF delta(50.0, 25.0); |
|
196 mContentScrollArea->mPanDayDirection |
|
197 = CalenDayContentScrollArea::ECalenPanVertical; |
|
198 mContentScrollArea->scrollByAmount(delta); |
|
199 pos = mContentScrollArea->contentWidget()->pos(); |
|
200 QCOMPARE(abs(pos.x()), abs(0.0)); |
|
201 QCOMPARE(abs(pos.y()), abs(delta.y())); |
|
202 |
|
203 //4) |
|
204 mContentScrollArea->mPanDayDirection |
|
205 = CalenDayContentScrollArea::ECalenPanHorizontal; |
|
206 mContentScrollArea->scrollByAmount(delta); |
|
207 pos = mContentScrollArea->contentWidget()->pos(); |
|
208 QCOMPARE(abs(pos.x()), abs(delta.x())); |
|
209 QCOMPARE(abs(pos.y()), abs(delta.y())); |
|
210 |
|
211 //5) |
|
212 QPointF newDelta(5.0, 5.0); |
|
213 delta += newDelta; |
|
214 mContentScrollArea->mPanDayDirection |
|
215 = CalenDayContentScrollArea::ECalenPanNotSet; |
|
216 mContentScrollArea->scrollByAmount(newDelta); |
|
217 pos = mContentScrollArea->contentWidget()->pos(); |
|
218 QCOMPARE(abs(pos.x()), abs(delta.x())); |
|
219 QCOMPARE(abs(pos.y()), abs(delta.y())); |
|
220 } |
|
221 |
|
222 /*! |
|
223 Test function for gestureEvent |
|
224 1. Call gestureEvent with QPanGesture (nothing should happen) |
|
225 2. Call gestureEvent with HbSwipeGesture, vertical movement |
|
226 KCalenSwipeAngle < angle < 180 - KCalenSwipeAngle |
|
227 3. Call gestureEvent with HbSwipeGesture, horizontal movement right |
|
228 angle < KCalenSwipeAngle |
|
229 4. Call gestureEvent with HbSwipeGesture, horizontal movement left |
|
230 180.0 - KCalenSwipeAngle < angle < 180.0 |
|
231 */ |
|
232 void TestCalenDayContentScrollArea::testGestureEvent() |
|
233 { |
|
234 QGestureEvent *gestureEvent = NULL; |
|
235 mContentScrollArea->mMoveDirection = ECalenScrollNoDayChange; |
|
236 |
|
237 //1) |
|
238 QList<QGesture*> list; |
|
239 QPanGesture *panGesture = new QPanGesture(); |
|
240 list.append(panGesture); |
|
241 gestureEvent = new QGestureEvent(list); |
|
242 mContentScrollArea->gestureEvent(gestureEvent); |
|
243 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollNoDayChange); |
|
244 |
|
245 delete gestureEvent; |
|
246 gestureEvent = NULL; |
|
247 |
|
248 //2) |
|
249 HbSwipeGesture *swipeGesture = new HbSwipeGesture(); |
|
250 list.append(swipeGesture); |
|
251 gTestGestureState = Qt::GestureStarted; |
|
252 gTestSceneSwipeAngle = KCalenSwipeAngle + 1.0; |
|
253 gestureEvent = new QGestureEvent(list); |
|
254 mContentScrollArea->gestureEvent(gestureEvent); |
|
255 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollNoDayChange); |
|
256 |
|
257 //3) |
|
258 gTestGestureState = Qt::GestureStarted; |
|
259 gTestSceneSwipeAngle = KCalenSwipeAngle - 1.0; |
|
260 gTestSceneHorizontalDirection = QSwipeGesture::Right; |
|
261 mContentScrollArea->gestureEvent(gestureEvent); |
|
262 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollToPrev); |
|
263 |
|
264 //4) |
|
265 gTestGestureState = Qt::GestureStarted; |
|
266 gTestSceneSwipeAngle = 180.0 - KCalenSwipeAngle + 1.0; |
|
267 gTestSceneHorizontalDirection = QSwipeGesture::Left; |
|
268 mContentScrollArea->gestureEvent(gestureEvent); |
|
269 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollToNext); |
|
270 |
|
271 delete gestureEvent; |
|
272 } |
|
273 |
|
274 /*! |
|
275 Test function for eventFilter |
|
276 1. Call eventFilter with event type: None |
|
277 2. Call eventFilter with event type: Gesture, scroll area is moving |
|
278 3. Call eventFilter with event type: Gesture, scroll area area not moving, |
|
279 swipe gesture |
|
280 */ |
|
281 void TestCalenDayContentScrollArea::testEventFilter() |
|
282 { |
|
283 bool result = false; |
|
284 |
|
285 //1) |
|
286 QEvent noneEvent(QEvent::None); |
|
287 result = mContentScrollArea->eventFilter(NULL, &noneEvent); |
|
288 QVERIFY(!result); //false |
|
289 |
|
290 //2) |
|
291 QEvent gestureEvent(QEvent::Gesture); |
|
292 mContentScrollArea->mIsMoving = true; |
|
293 result = mContentScrollArea->eventFilter(NULL, &gestureEvent); |
|
294 QVERIFY(result); //true |
|
295 |
|
296 //3) |
|
297 QSwipeGesture *swipeGesture = new QSwipeGesture(); |
|
298 QList<QGesture*> list; |
|
299 list.append(swipeGesture); |
|
300 QGestureEvent *gesture = new QGestureEvent(list); |
|
301 QEvent *swipeGestureEvent = static_cast <QEvent*> (gesture); |
|
302 |
|
303 mContentScrollArea->mIsMoving = false; |
|
304 result = mContentScrollArea->eventFilter(NULL, swipeGestureEvent); |
|
305 QVERIFY(result); //true |
|
306 |
|
307 delete gesture; |
|
308 } |
|
309 |
|
310 /*! |
|
311 Test function for event |
|
312 1. Test event type: Gesture, scroll area not moving |
|
313 2. Test event type: Gesture, scroll area is moving |
|
314 3. Test event type: FocusOut, scroll area not moving |
|
315 4. Test event type: FocusOut, scroll area is moving |
|
316 5. Test event type: FocusIn, scroll area not moving |
|
317 6. Test event type: FocusIn, scroll area is moving |
|
318 7. Test event type: LayoutRequest |
|
319 */ |
|
320 void TestCalenDayContentScrollArea::testEvent() |
|
321 { |
|
322 bool result = false; |
|
323 |
|
324 //1) |
|
325 QEvent gestureEvent(QEvent::Gesture); |
|
326 mContentScrollArea->mIsMoving = false; |
|
327 result = mContentScrollArea->event(&gestureEvent); |
|
328 QVERIFY(!result); //false |
|
329 |
|
330 //2) |
|
331 mContentScrollArea->mIsMoving = true; |
|
332 result = mContentScrollArea->event(&gestureEvent); |
|
333 QVERIFY(result); //true |
|
334 |
|
335 //3) |
|
336 QEvent focusOutEvent(QEvent::FocusOut); |
|
337 mContentScrollArea->mIsMoving = false; |
|
338 result = mContentScrollArea->event(&focusOutEvent); |
|
339 QVERIFY(!result); //false |
|
340 |
|
341 //4) |
|
342 mContentScrollArea->mIsMoving = true; |
|
343 result = mContentScrollArea->event(&focusOutEvent); |
|
344 QVERIFY(result); //true |
|
345 |
|
346 //5) |
|
347 QEvent focusInEvent(QEvent::FocusIn); |
|
348 mContentScrollArea->mIsMoving = false; |
|
349 result = mContentScrollArea->event(&focusInEvent); |
|
350 QVERIFY(!result); //false |
|
351 |
|
352 //6) |
|
353 mContentScrollArea->mIsMoving = true; |
|
354 result = mContentScrollArea->event(&focusInEvent); |
|
355 QVERIFY(result); //true |
|
356 |
|
357 //7) |
|
358 QEvent layoutReqEvent(QEvent::LayoutRequest); |
|
359 QPointF oldPos = mContentScrollArea->contentWidget()->pos(); |
|
360 mContentScrollArea->event(&layoutReqEvent); |
|
361 QPointF pos = mContentScrollArea->contentWidget()->pos(); |
|
362 QCOMPARE(oldPos == pos, false); |
|
363 } |
|
364 |
|
365 /*! |
|
366 Test function for moveTo |
|
367 1. Test moveTo, day not changed |
|
368 2. Test moveTo, day changed to next |
|
369 3. Test moveTo, day changed to prev |
|
370 */ |
|
371 void TestCalenDayContentScrollArea::testMoveTo() |
|
372 { |
|
373 #ifndef __WINSCW__ |
|
374 //1) |
|
375 mContentScrollArea->mMoveDirection = ECalenScrollNoDayChange; |
|
376 QPointF newPos(10,0); |
|
377 mContentScrollArea->moveTo(newPos); |
|
378 QPointF pos = mContentScrollArea->contentWidget()->pos(); |
|
379 QCOMPARE(abs(pos.x()), abs(mContentScrollArea->contentWidget()->pos().x())); |
|
380 QCOMPARE(abs(pos.y()), abs(mContentScrollArea->contentWidget()->pos().y())); |
|
381 |
|
382 //2) |
|
383 newPos = QPointF(20,0); |
|
384 mContentScrollArea->mMoveDirection = ECalenScrollToNext; |
|
385 mContentScrollArea->moveTo(newPos); |
|
386 pos = mContentScrollArea->contentWidget()->pos(); |
|
387 QCOMPARE(abs(pos.x()), abs(mContentScrollArea->contentWidget()->pos().x())); |
|
388 QCOMPARE(abs(pos.y()), abs(mContentScrollArea->contentWidget()->pos().y())); |
|
389 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollNoDayChange); |
|
390 |
|
391 //2) |
|
392 newPos = QPointF(-20,0); |
|
393 mContentScrollArea->mMoveDirection = ECalenScrollToNext; |
|
394 mContentScrollArea->moveTo(newPos); |
|
395 pos = mContentScrollArea->contentWidget()->pos(); |
|
396 QCOMPARE(abs(mContentScrollArea->contentWidget()->pos().x()), abs(pos.x())); |
|
397 QCOMPARE(abs(mContentScrollArea->contentWidget()->pos().y()), abs(pos.y())); |
|
398 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollNoDayChange); |
|
399 #endif /* __WINSCW__ */ |
|
400 } |
|
401 |
|
402 /*! |
|
403 Test function for checking if swipe is horizontal or not |
|
404 1) test Horizontal swipe |
|
405 2) test no horizontal swipe |
|
406 */ |
|
407 void TestCalenDayContentScrollArea::testHorizontalSwipe() |
|
408 { |
|
409 #ifndef __WINSCW__ |
|
410 //1) |
|
411 QCOMPARE(mContentScrollArea->isHorizontalSwipe(KCalenSwipeAngle - 5), true); |
|
412 |
|
413 //2) |
|
414 QCOMPARE(mContentScrollArea->isHorizontalSwipe(KCalenSwipeAngle + 5), false); |
|
415 #endif /* __WINSCW__ */ |
|
416 } |
|
417 |
|
418 /*! |
|
419 Test moveFinished slot |
|
420 1) test moveFinished, no day change |
|
421 2) test moveFinished, day changed to next |
|
422 3) test moveFinished, day changed to prev |
|
423 */ |
|
424 void TestCalenDayContentScrollArea::testMoveFinished() |
|
425 { |
|
426 #ifndef __WINSCW__ |
|
427 //1) |
|
428 mContentScrollArea->mMoveDirection = ECalenScrollNoDayChange; |
|
429 mContentScrollArea->moveFinished(); |
|
430 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollNoDayChange); |
|
431 |
|
432 //2) |
|
433 mContentScrollArea->mMoveDirection = ECalenScrollToNext; |
|
434 mContentScrollArea->moveFinished(); |
|
435 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollNoDayChange); |
|
436 |
|
437 //3) |
|
438 mContentScrollArea->mMoveDirection = ECalenScrollToPrev; |
|
439 mContentScrollArea->moveFinished(); |
|
440 QCOMPARE(mContentScrollArea->mMoveDirection, ECalenScrollNoDayChange); |
|
441 #endif /* __WINSCW__ */ |
|
442 } |
|
443 |
|
444 /*! |
|
445 Test orientationChanged slot |
|
446 1) test orientation changed to horizontal |
|
447 2) test orientation changed to vertical |
|
448 */ |
|
449 void TestCalenDayContentScrollArea::testOrientationChanged() |
|
450 { |
|
451 #ifndef __WINSCW__ |
|
452 //1) |
|
453 mContentScrollArea->orientationChanged(Qt::Horizontal); |
|
454 QCOMPARE(mContentScrollArea->mOrientation, Qt::Horizontal); |
|
455 |
|
456 //2) |
|
457 mContentScrollArea->orientationChanged(Qt::Vertical); |
|
458 QCOMPARE(mContentScrollArea->mOrientation, Qt::Vertical); |
|
459 #endif /* __WINSCW__ */ |
|
460 } |
|
461 |
|
462 QTEST_MAIN(TestCalenDayContentScrollArea); |
|
463 #include "unittest_calendaycontentscrollarea.moc" |