@@ -16,179 +16,205 @@
17import QtQuick 2.0
17import QtQuick 2.0
18import QtTest 1.0
18import QtTest 1.0
19+import "../../../qml/Components/"
19import "../../../qml/Components/carousel.js" as Carousel
20import "../../../qml/Components/carousel.js" as Carousel
31+ property int createdDelegates: 0
32+ property int destroyedDelegates: 0
34+ itemComponent: Rectangle {
36+ Component.onCompleted: carousel.createdDelegates++
37+ Component.onDestruction: carousel.destroyedDelegates++
21−TestCase {
39+ TestCase {
23− name: "Carousel"
41+ name: "Carousel"
25− property real carouselWidth
44+ property real carouselWidth
26− property int itemCount
45+ property int itemCount
27− property real tileWidth
46+ property real tileWidth
29− property real contentWidth: itemCount * tileWidth
48+ property real contentWidth: itemCount * tileWidth
30− // following variables are copied from Carousel.qml
49+ // following variables are copied from Carousel.qml
31− // I'm not using the variables directly from there, as then tileWidth and others would be affected as well
50+ // I'm not using the variables directly from there, as then tileWidth and others would be affected as well
32− property real gapToMiddlePhase: Math.min(carouselWidth / 2 - tileWidth / 2, (contentWidth - carouselWidth) / 2)
51+ property real gapToMiddlePhase: Math.min(carouselWidth / 2 - tileWidth / 2, (contentWidth - carouselWidth) / 2)
33− property real gapToEndPhase: contentWidth - carouselWidth - gapToMiddlePhase
52+ property real gapToEndPhase: contentWidth - carouselWidth - gapToMiddlePhase
34− property real kMiddleIndex: (carouselWidth / 2) / tileWidth - 0.5
53+ property real kMiddleIndex: (carouselWidth / 2) / tileWidth - 0.5
35− property real kGapEnd: kMiddleIndex * (1 - gapToEndPhase / gapToMiddlePhase)
54+ property real kGapEnd: kMiddleIndex * (1 - gapToEndPhase / gapToMiddlePhase)
36− property real kXBeginningEnd: 1 / tileWidth + kMiddleIndex / gapToMiddlePhase
55+ property real kXBeginningEnd: 1 / tileWidth + kMiddleIndex / gapToMiddlePhase
57+ function test_onlyNeededItemsCreated() {
58+ compare(carousel.createdDelegates, 10);
59+ compare(carousel.destroyedDelegates, 0);
38− // test for the getContinuousIndex() function
62+ // test for the getContinuousIndex() function
39− function test_getContinuousIndex_data() {
63+ function test_getContinuousIndex_data() {
40− // testing for 10 items of size 100 pixel
64+ // testing for 10 items of size 100 pixel
41− return [ {tag:"at start",
65+ return [ {tag:"at start",
42− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, result: 0},
66+ x: 0, carouselWidth:400, tileWidth:100, itemCount:10, result: 0},
43− {tag:"in startup",
67+ {tag:"in startup",
44− x: 100, carouselWidth:400, tileWidth:100, itemCount:10, result: 2},
68+ x: 100, carouselWidth:400, tileWidth:100, itemCount:10, result: 2},
45− {tag:"in the middle",
69+ {tag:"in the middle",
46− x: 350, carouselWidth:400, tileWidth:100, itemCount:10, result: 5},
70+ x: 350, carouselWidth:400, tileWidth:100, itemCount:10, result: 5},
47− {tag:"at end",
71+ {tag:"at end",
48− x: 600, carouselWidth:400, tileWidth:100, itemCount:10, result: 9},
72+ x: 600, carouselWidth:400, tileWidth:100, itemCount:10, result: 9},
52− function test_getContinuousIndex(data) {
76+ function test_getContinuousIndex(data) {
53− root.carouselWidth = data.carouselWidth
77+ root.carouselWidth = data.carouselWidth
54− root.tileWidth = data.tileWidth
78+ root.tileWidth = data.tileWidth
55− root.itemCount = data.itemCount
79+ root.itemCount = data.itemCount
57− var index = Carousel.getContinuousIndex(data.x,
81+ var index = Carousel.getContinuousIndex(data.x,
58− data.tileWidth,
82+ data.tileWidth,
59− gapToMiddlePhase,
83+ gapToMiddlePhase,
60− gapToEndPhase,
84+ gapToEndPhase,
62− kMiddleIndex,
86+ kMiddleIndex,
63− kXBeginningEnd)
87+ kXBeginningEnd)
64− compare(index, data.result)
88+ compare(index, data.result)
67− // test for the getXFromContinuousIndex() function
91+ // test for the getXFromContinuousIndex() function
68− function test_getXFromContinuousIndex_data() {
92+ function test_getXFromContinuousIndex_data() {
69− return [ {tag:"at start",
93+ return [ {tag:"at start",
70− index: 0, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 0},
94+ index: 0, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 0},
71− {tag:"in startup",
95+ {tag:"in startup",
72− index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 100},
96+ index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 100},
73− {tag:"in startup with drawBuffer",
97+ {tag:"in startup with drawBuffer",
74− index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 0},
98+ index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 0},
75− {tag:"in the middle",
99+ {tag:"in the middle",
76− index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 350},
100+ index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 350},
77− {tag:"in the middle with drawBuffer",
101+ {tag:"in the middle with drawBuffer",
78− index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 250},
102+ index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 250},
79− {tag:"at end",
103+ {tag:"at end",
80− index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 600},
104+ index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 600},
81− {tag:"at end with drawBuffer",
105+ {tag:"at end with drawBuffer",
82− index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 500},
106+ index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 500},
86− function test_getXFromContinuousIndex(data) {
110+ function test_getXFromContinuousIndex(data) {
87− root.carouselWidth = data.carouselWidth
111+ root.carouselWidth = data.carouselWidth
88− root.tileWidth = data.tileWidth
112+ root.tileWidth = data.tileWidth
89− root.itemCount = data.itemCount
113+ root.itemCount = data.itemCount
91− var x = Carousel.getXFromContinuousIndex(data.index,
115+ var x = Carousel.getXFromContinuousIndex(data.index,
92− data.carouselWidth,
116+ data.carouselWidth,
93− contentWidth,
117+ contentWidth,
94− data.tileWidth,
118+ data.tileWidth,
95− gapToMiddlePhase,
119+ gapToMiddlePhase,
96− gapToEndPhase,
120+ gapToEndPhase,
97− data.drawBuffer)
121+ data.drawBuffer)
98− compare(x, data.result)
122+ compare(x, data.result)
101− // test for the getViewTranslation() function
125+ // test for the getViewTranslation() function
102− function test_getViewTranslation_data() {
126+ function test_getViewTranslation_data() {
103− return [ {tag:"at start - viewfactor 1",
127+ return [ {tag:"at start - viewfactor 1",
104− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 150},
128+ x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 150},
105− {tag:"at start - viewfactor 2",
129+ {tag:"at start - viewfactor 2",
106− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:2, result: 300},
130+ x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:2, result: 300},
107− {tag:"in startup",
131+ {tag:"in startup",
108− x: 100, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 50},
132+ x: 100, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 50},
109− {tag:"in the middle",
133+ {tag:"in the middle",
110− x: 350, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 0},
134+ x: 350, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 0},
111− {tag:"at end",
135+ {tag:"at end",
112− x: 600, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: -150},
136+ x: 600, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: -150},
116− function test_getViewTranslation(data) {
140+ function test_getViewTranslation(data) {
117− root.carouselWidth = data.carouselWidth
141+ root.carouselWidth = data.carouselWidth
118− root.tileWidth = data.tileWidth
142+ root.tileWidth = data.tileWidth
119− root.itemCount = data.itemCount
143+ root.itemCount = data.itemCount
121− var x = Carousel.getViewTranslation(data.x,
145+ var x = Carousel.getViewTranslation(data.x,
122− data.tileWidth,
146+ data.tileWidth,
123− root.gapToMiddlePhase,
147+ root.gapToMiddlePhase,
124− root.gapToEndPhase,
148+ root.gapToEndPhase,
125− data.translationXViewFactor)
149+ data.translationXViewFactor)
126− compare(x, data.result)
150+ compare(x, data.result)
129− // test for the getItemScale() function
153+ // test for the getItemScale() function
130− function test_getItemScale_data() {
154+ function test_getItemScale_data() {
131− return [ // tests for distance
155+ return [ // tests for distance
132− {distance: 0, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 1},
156+ {distance: 0, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 1},
133− {distance: 9, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
157+ {distance: 9, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
134− {distance: 999, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
158+ {distance: 999, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
135− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
159+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
136− {distance: 3, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.688230855}, // = 1 - (3^2.5 / 50)
160+ {distance: 3, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.688230855}, // = 1 - (3^2.5 / 50)
137− // tests for continuousIndex
161+ // tests for continuousIndex
138− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
162+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
139− {distance: 1, continuousIndex: 97, numberOfItems: 100, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
163+ {distance: 1, continuousIndex: 97, numberOfItems: 100, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
140− // tests for numberOfItems
164+ // tests for numberOfItems
141− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
165+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
142− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
166+ {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
143− // tests for scaleFactor
167+ // tests for scaleFactor
144− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
168+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
145− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 2, result: 0.99}, // = 1 - (1 / 100)
169+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 2, result: 0.99}, // = 1 - (1 / 100)
146− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 0.5, result: 0.96}, // = 1 - (1 / 25)
170+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 0.5, result: 0.96}, // = 1 - (1 / 25)
147− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
171+ {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
148− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 2, result: 0.996666666}, // = 1 - (1 / 300) - distanceToBounds is used
172+ {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 2, result: 0.996666666}, // = 1 - (1 / 300) - distanceToBounds is used
152− function test_getItemScale(data) {
176+ function test_getItemScale(data) {
153− var scale = Carousel.getItemScale(data.distance,
177+ var scale = Carousel.getItemScale(data.distance,
154− data.continuousIndex,
178+ data.continuousIndex,
155− data.numberOfItems,
179+ data.numberOfItems,
156− data.scaleFactor)
180+ data.scaleFactor)
157− compare(scale, data.result)
181+ compare(scale, data.result)
160− // test for the getItemTranslation() function
184+ // test for the getItemTranslation() function
161− function test_getItemTranslation_data() {
185+ function test_getItemTranslation_data() {
162− return [ // tests for index and selectedIndex
186+ return [ // tests for index and selectedIndex
163− {index: 1, selectedIndex: 1, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
187+ {index: 1, selectedIndex: 1, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
164− // tests if distance only affects the sign
188+ // tests if distance only affects the sign
165− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
189+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
166− {index: 1, selectedIndex: 2, distance: 99, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
190+ {index: 1, selectedIndex: 2, distance: 99, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
167− {index: 1, selectedIndex: 2, distance: -1, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
191+ {index: 1, selectedIndex: 2, distance: -1, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
168− {index: 1, selectedIndex: 2, distance: -99, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
192+ {index: 1, selectedIndex: 2, distance: -99, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
169− // tests for the scale
193+ // tests for the scale
170− {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
194+ {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
171− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
195+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
172− {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 1, maxTranslation: 10, result: 5},
196+ {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 1, maxTranslation: 10, result: 5},
173− // tests for maxScale
197+ // tests for maxScale
174− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 0.98, maxTranslation: 10, result: 9.8},
198+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 0.98, maxTranslation: 10, result: 9.8},
175− {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 0.95, maxTranslation: 10, result: 4.5},
199+ {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 0.95, maxTranslation: 10, result: 4.5},
176− {index: 1, selectedIndex: 2, distance: 1, scale: 0.8, maxScale: 0.93, maxTranslation: 10, result: 1.3},
200+ {index: 1, selectedIndex: 2, distance: 1, scale: 0.8, maxScale: 0.93, maxTranslation: 10, result: 1.3},
177− // test for maxTranslation
201+ // test for maxTranslation
178− {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 1, result: 0},
202+ {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 1, result: 0},
179− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 1, result: 1},
203+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 1, result: 1},
180− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
204+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
181− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 0, result: 0},
205+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 0, result: 0},
185− function test_getItemTranslation(data) {
209+ function test_getItemTranslation(data) {
186− var scale = Carousel.getItemTranslation(data.index,
210+ var scale = Carousel.getItemTranslation(data.index,
187− data.selectedIndex,
211+ data.selectedIndex,
188− data.distance,
212+ data.distance,
189− data.scale,
213+ data.scale,
190− data.maxScale,
214+ data.maxScale,
191− data.maxTranslation)
215+ data.maxTranslation)
192− compare(scale, data.result)
216+ compare(scale, data.result)