@@ -16,179 +16,232 @@
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
20−
21+import Unity.Test 0.1 as UT
23− name: "Carousel"
24+ width: 700
25− property real carouselWidth
26+
26− property int itemCount
27+ Carousel {
27− property real tileWidth
28+ anchors.fill: parent
29− property real contentWidth: itemCount * tileWidth
30+ model: 40
30− // following variables are copied from Carousel.qml
31+
31− // I'm not using the variables directly from there, as then tileWidth and others would be affected as well
32+ property int createdDelegates: 0
32− property real gapToMiddlePhase: Math.min(carouselWidth / 2 - tileWidth / 2, (contentWidth - carouselWidth) / 2)
33+ property int destroyedDelegates: 0
33− property real gapToEndPhase: contentWidth - carouselWidth - gapToMiddlePhase
34+
34− property real kMiddleIndex: (carouselWidth / 2) / tileWidth - 0.5
35+ itemComponent: BaseCarouselDelegate {
35− property real kGapEnd: kMiddleIndex * (1 - gapToEndPhase / gapToMiddlePhase)
36+ Rectangle {
36− property real kXBeginningEnd: 1 / tileWidth + kMiddleIndex / gapToMiddlePhase
37+ anchors.fill: parent
37−
38+ border.color: "black"
38− // test for the getContinuousIndex() function
39+ color: "red"
39− function test_getContinuousIndex_data() {
40+ Component.onCompleted: carousel.createdDelegates++
40− // testing for 10 items of size 100 pixel
41+ Component.onDestruction: carousel.destroyedDelegates++
41− return [ {tag:"at start",
42+ }
42− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, result: 0},
43+ }
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},
51−
45+ UT.UnityTestCase {
52− function test_getContinuousIndex(data) {
46+ id: root
53− root.carouselWidth = data.carouselWidth
47+ name: "Carousel"
54− root.tileWidth = data.tileWidth
48+ when: windowShown
55− root.itemCount = data.itemCount
49+
56−
50+ property real carouselWidth
57− var index = Carousel.getContinuousIndex(data.x,
51+ property int itemCount
52+ property real tileWidth
54+ property real contentWidth: itemCount * tileWidth
55+ // following variables are copied from Carousel.qml
56+ // I'm not using the variables directly from there, as then tileWidth and others would be affected as well
57+ property real gapToMiddlePhase: Math.min(carouselWidth / 2 - tileWidth / 2, (contentWidth - carouselWidth) / 2)
58+ property real gapToEndPhase: contentWidth - carouselWidth - gapToMiddlePhase
59+ property real kMiddleIndex: (carouselWidth / 2) / tileWidth - 0.5
60+ property real kGapEnd: kMiddleIndex * (1 - gapToEndPhase / gapToMiddlePhase)
61+ property real kXBeginningEnd: 1 / tileWidth + kMiddleIndex / gapToMiddlePhase
63+ // This test needs to run first since it tests a corner case condition
64+ // to where the carousel is just being created
65+ // That is why it has a 1 in the name
66+ function test_1onlyNeededItemsCreated() {
67+ tryCompare(carousel, "createdDelegates", 10);
68+ compare(carousel.destroyedDelegates, 0);
71+ // test for the getContinuousIndex() function
72+ function test_getContinuousIndex_data() {
73+ // testing for 10 items of size 100 pixel
74+ return [ {tag:"at start",
75+ x: 0, carouselWidth:400, tileWidth:100, itemCount:10, result: 0},
77+ x: 100, carouselWidth:400, tileWidth:100, itemCount:10, result: 2},
78+ {tag:"in the middle",
79+ x: 350, carouselWidth:400, tileWidth:100, itemCount:10, result: 5},
81+ x: 600, carouselWidth:400, tileWidth:100, itemCount:10, result: 9},
85+ function test_getContinuousIndex(data) {
86+ root.carouselWidth = data.carouselWidth
87+ root.tileWidth = data.tileWidth
88+ root.itemCount = data.itemCount
90+ var index = Carousel.getContinuousIndex(data.x,
97+ compare(index, data.result)
100+ // test for the getXFromContinuousIndex() function
101+ function test_getXFromContinuousIndex_data() {
102+ return [ {tag:"at start",
103+ index: 0, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 0},
105+ index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 100},
106+ {tag:"in startup with drawBuffer",
107+ index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 0},
108+ {tag:"in the middle",
109+ index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 350},
110+ {tag:"in the middle with drawBuffer",
111+ index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 250},
113+ index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 600},
114+ {tag:"at end with drawBuffer",
115+ index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 500},
119+ function test_getXFromContinuousIndex(data) {
120+ root.carouselWidth = data.carouselWidth
121+ root.tileWidth = data.tileWidth
122+ root.itemCount = data.itemCount
124+ var x = Carousel.getXFromContinuousIndex(data.index,
131+ compare(x, data.result)
134+ // test for the getViewTranslation() function
135+ function test_getViewTranslation_data() {
136+ return [ {tag:"at start - viewfactor 1",
137+ x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 150},
138+ {tag:"at start - viewfactor 2",
139+ x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:2, result: 300},
141+ x: 100, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 50},
142+ {tag:"in the middle",
143+ x: 350, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 0},
145+ x: 600, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: -150},
149+ function test_getViewTranslation(data) {
150+ root.carouselWidth = data.carouselWidth
151+ root.tileWidth = data.tileWidth
152+ root.itemCount = data.itemCount
154+ var x = Carousel.getViewTranslation(data.x,
58 data.tileWidth,
155 data.tileWidth,
59− gapToMiddlePhase,
156+ root.gapToMiddlePhase,
60− gapToEndPhase,
157+ root.gapToEndPhase,
61− kGapEnd,
158+ data.translationXViewFactor)
62− kMiddleIndex,
159+ compare(x, data.result)
63− kXBeginningEnd)
160+ }
64− compare(index, data.result)
161+
65− }
162+ // test for the getItemScale() function
66−
163+ function test_getItemScale_data() {
67− // test for the getXFromContinuousIndex() function
164+ return [ // tests for distance
68− function test_getXFromContinuousIndex_data() {
165+ {distance: 0, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 1},
69− return [ {tag:"at start",
166+ {distance: 9, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
70− index: 0, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 0},
167+ {distance: 999, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
71− {tag:"in startup",
168+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
72− index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 100},
169+ {distance: 3, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.688230855}, // = 1 - (3^2.5 / 50)
73− {tag:"in startup with drawBuffer",
170+ // tests for continuousIndex
74− index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 0},
171+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
75− {tag:"in the middle",
172+ {distance: 1, continuousIndex: 97, numberOfItems: 100, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
76− index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 350},
173+ // tests for numberOfItems
77− {tag:"in the middle with drawBuffer",
174+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
78− index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 250},
175+ {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
79− {tag:"at end",
176+ // tests for scaleFactor
80− index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 600},
177+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
81− {tag:"at end with drawBuffer",
178+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 2, result: 0.99}, // = 1 - (1 / 100)
82− index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 500},
179+ {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 0.5, result: 0.96}, // = 1 - (1 / 25)
83− ]
180+ {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
84− }
181+ {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 2, result: 0.996666666}, // = 1 - (1 / 300) - distanceToBounds is used
86− function test_getXFromContinuousIndex(data) {
183+ }
87− root.carouselWidth = data.carouselWidth
184+
88− root.tileWidth = data.tileWidth
185+ function test_getItemScale(data) {
89− root.itemCount = data.itemCount
186+ var scale = Carousel.getItemScale(data.distance,
90−
187+ data.continuousIndex,
91− var x = Carousel.getXFromContinuousIndex(data.index,
188+ data.numberOfItems,
92− data.carouselWidth,
189+ data.scaleFactor)
93− contentWidth,
190+ compare(scale, data.result)
94− data.tileWidth,
191+ }
95− gapToMiddlePhase,
192+
96− gapToEndPhase,
193+ // test for the getItemTranslation() function
97− data.drawBuffer)
194+ function test_getItemTranslation_data() {
98− compare(x, data.result)
195+ return [ // tests for index and selectedIndex
99− }
196+ {index: 1, selectedIndex: 1, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
100−
197+ // tests if distance only affects the sign
101− // test for the getViewTranslation() function
198+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
102− function test_getViewTranslation_data() {
199+ {index: 1, selectedIndex: 2, distance: 99, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
103− return [ {tag:"at start - viewfactor 1",
200+ {index: 1, selectedIndex: 2, distance: -1, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
104− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 150},
201+ {index: 1, selectedIndex: 2, distance: -99, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
105− {tag:"at start - viewfactor 2",
202+ // tests for the scale
106− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:2, result: 300},
203+ {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
107− {tag:"in startup",
204+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
108− x: 100, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 50},
205+ {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 1, maxTranslation: 10, result: 5},
109− {tag:"in the middle",
206+ // tests for maxScale
110− x: 350, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 0},
207+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 0.98, maxTranslation: 10, result: 9.8},
111− {tag:"at end",
208+ {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 0.95, maxTranslation: 10, result: 4.5},
112− x: 600, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: -150},
209+ {index: 1, selectedIndex: 2, distance: 1, scale: 0.8, maxScale: 0.93, maxTranslation: 10, result: 1.3},
113− ]
210+ // test for maxTranslation
114− }
211+ {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 1, result: 0},
115−
212+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 1, result: 1},
116− function test_getViewTranslation(data) {
213+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
117− root.carouselWidth = data.carouselWidth
214+ {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 0, result: 0},
118− root.tileWidth = data.tileWidth
215+ ]
119− root.itemCount = data.itemCount
216+ }
121− var x = Carousel.getViewTranslation(data.x,
218+ function test_getItemTranslation(data) {
122− data.tileWidth,
219+ var scale = Carousel.getItemTranslation(data.index,
123− root.gapToMiddlePhase,
220+ data.selectedIndex,
124− root.gapToEndPhase,
221+ data.distance,
125− data.translationXViewFactor)
222+ data.scale,
126− compare(x, data.result)
223+ data.maxScale,
127− }
224+ data.maxTranslation)
128−
225+ compare(scale, data.result)
129− // test for the getItemScale() function
226+ }
130− function test_getItemScale_data() {
227+
131− return [ // tests for distance
228+ function test_lastItemXPosition() {
132− {distance: 0, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 1},
229+ var carouselList = findChild(carousel, "listView");
133− {distance: 9, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
230+ var stepAnimation = findInvisibleChild(carouselList, "stepAnimation");
134− {distance: 999, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
231+ var stepDiff = 0;
135− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
232+ stepAnimation.onRunningChanged.connect(function() { if (stepAnimation.running) stepDiff = Math.abs(carouselList.contentX - stepAnimation.to); })
136− {distance: 3, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.688230855}, // = 1 - (3^2.5 / 50)
233+ var overshootOnce = false;
137− // tests for continuousIndex
234+ while (!overshootOnce) {
138− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
235+ overshootOnce = carouselList.realContentX > carouselList.realContentWidth - carouselList.realWidth;
139− {distance: 1, continuousIndex: 97, numberOfItems: 100, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
236+ mouseFlick(carousel, carousel.width - units.gu(1),
140− // tests for numberOfItems
237+ carousel.height / 2,
141− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
238+ units.gu(2),
142− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
239+ carousel.height / 2);
143− // tests for scaleFactor
240+ tryCompare(carouselList, "moving", false);
144− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
241+ }
145− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 2, result: 0.99}, // = 1 - (1 / 100)
242+ verify(stepDiff < 1);
146− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 0.5, result: 0.96}, // = 1 - (1 / 25)
243+ var x = carouselList.getXFromContinuousIndex(carousel.model - 1);
147− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
244+ verify(Math.abs(x - carouselList.contentX) < 1);
148− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 2, result: 0.996666666}, // = 1 - (1 / 300) - distanceToBounds is used
245+ }
152− function test_getItemScale(data) {
153− var scale = Carousel.getItemScale(data.distance,
154− data.continuousIndex,
157− compare(scale, data.result)
160− // test for the getItemTranslation() function
161− function test_getItemTranslation_data() {
162− return [ // tests for index and selectedIndex
163− {index: 1, selectedIndex: 1, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
164− // tests if distance only affects the sign
165− {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},
167− {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},
169− // tests for the scale
170− {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},
172− {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 1, maxTranslation: 10, result: 5},
173− // tests for maxScale
174− {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},
176− {index: 1, selectedIndex: 2, distance: 1, scale: 0.8, maxScale: 0.93, maxTranslation: 10, result: 1.3},
177− // test for maxTranslation
178− {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},
180− {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},
185− function test_getItemTranslation(data) {
186− var scale = Carousel.getItemTranslation(data.index,
191− data.maxTranslation)
192− compare(scale, data.result)