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