@@ -1,292 +0,0 @@
1−/*
2− * Copyright 2013 Canonical Ltd.
3− *
4− * This program is free software; you can redistribute it and/or modify
5− * it under the terms of the GNU General Public License as published by
6− * the Free Software Foundation; version 3.
7− *
8− * This program is distributed in the hope that it will be useful,
9− * but WITHOUT ANY WARRANTY; without even the implied warranty of
10− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11− * GNU General Public License for more details.
12− *
13− * You should have received a copy of the GNU General Public License
14− * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− */
16−
17−import QtQuick 2.4
18−import QtTest 1.0
19−import "../../../qml/Components/"
20−import "../../../qml/Components/carousel.js" as Carousel
21−import Unity.Test 0.1 as UT
22−
23−Item {
24− width: 700
25− height: 400
26−
27− Carousel {
28− anchors.fill: parent
29− id: carousel
30−
31− property int createdDelegates: 0
32− property int destroyedDelegates: 0
33−
34− itemComponent: BaseCarouselDelegate {
35− objectName: "carouselDelegate" + index
36−
37− Rectangle {
38− anchors.fill: parent
39− border.color: "black"
40− color: "red"
41− Component.onCompleted: carousel.createdDelegates++
42− Component.onDestruction: carousel.destroyedDelegates++
43− }
44− }
45− }
46−
47− SignalSpy {
48− id: spy
49− }
50−
51− UT.UnityTestCase {
52− id: root
53− name: "Carousel"
54− when: windowShown
55−
56− property real carouselWidth
57− property int itemCount
58− property real tileWidth
59−
60− property Item listView: findChild(carousel, "listView")
61−
62− property real contentWidth: itemCount * tileWidth
63− // following variables are copied from Carousel.qml
64− // I'm not using the variables directly from there, as then tileWidth and others would be affected as well
65− property real gapToMiddlePhase: Math.min(carouselWidth / 2 - tileWidth / 2, (contentWidth - carouselWidth) / 2)
66− property real gapToEndPhase: contentWidth - carouselWidth - gapToMiddlePhase
67− property real kMiddleIndex: (carouselWidth / 2) / tileWidth - 0.5
68− property real kGapEnd: kMiddleIndex * (1 - gapToEndPhase / gapToMiddlePhase)
69− property real kXBeginningEnd: 1 / tileWidth + kMiddleIndex / gapToMiddlePhase
70−
71− function init() {
72− carousel.model = 40;
73− waitForRendering(carousel)
74− listView.positionViewAtBeginning();
75− }
76−
77− function cleanup() {
78− carousel.model = 0;
79− carousel.createdDelegates = 0;
80− carousel.destroyedDelegates = 0;
81− spy.target = null;
82− spy.signalName = "";
83− }
84−
85− // This test needs to run first since it tests a corner case condition
86− // to where the carousel is just being created
87− // That is why it has a 1 in the name
88− function test_1onlyNeededItemsCreated() {
89− tryCompare(carousel, "createdDelegates", 9);
90− compare(carousel.destroyedDelegates, 0);
91− }
92−
93− // test for the getContinuousIndex() function
94− function test_getContinuousIndex_data() {
95− // testing for 10 items of size 100 pixel
96− return [ {tag:"at start",
97− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, result: 0},
98− {tag:"in startup",
99− x: 100, carouselWidth:400, tileWidth:100, itemCount:10, result: 2},
100− {tag:"in the middle",
101− x: 350, carouselWidth:400, tileWidth:100, itemCount:10, result: 5},
102− {tag:"at end",
103− x: 600, carouselWidth:400, tileWidth:100, itemCount:10, result: 9},
104− ]
105− }
106−
107− function test_getContinuousIndex(data) {
108− root.carouselWidth = data.carouselWidth
109− root.tileWidth = data.tileWidth
110− root.itemCount = data.itemCount
111−
112− var index = Carousel.getContinuousIndex(data.x,
113− data.tileWidth,
114− gapToMiddlePhase,
115− gapToEndPhase,
116− kGapEnd,
117− kMiddleIndex,
118− kXBeginningEnd)
119− compare(index, data.result)
120− }
121−
122− // test for the getXFromContinuousIndex() function
123− function test_getXFromContinuousIndex_data() {
124− return [ {tag:"at start",
125− index: 0, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 0},
126− {tag:"in startup",
127− index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 100},
128− {tag:"in startup with drawBuffer",
129− index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 0},
130− {tag:"in the middle",
131− index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 350},
132− {tag:"in the middle with drawBuffer",
133− index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 250},
134− {tag:"at end",
135− index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 600},
136− {tag:"at end with drawBuffer",
137− index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 500},
138− ]
139− }
140−
141− function test_getXFromContinuousIndex(data) {
142− root.carouselWidth = data.carouselWidth
143− root.tileWidth = data.tileWidth
144− root.itemCount = data.itemCount
145−
146− var x = Carousel.getXFromContinuousIndex(data.index,
147− data.carouselWidth,
148− contentWidth,
149− data.tileWidth,
150− gapToMiddlePhase,
151− gapToEndPhase,
152− data.drawBuffer)
153− compare(x, data.result)
154− }
155−
156− // test for the getViewTranslation() function
157− function test_getViewTranslation_data() {
158− return [ {tag:"at start - viewfactor 1",
159− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 150},
160− {tag:"at start - viewfactor 2",
161− x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:2, result: 300},
162− {tag:"in startup",
163− x: 100, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 50},
164− {tag:"in the middle",
165− x: 350, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 0},
166− {tag:"at end",
167− x: 600, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: -150},
168− ]
169− }
170−
171− function test_getViewTranslation(data) {
172− root.carouselWidth = data.carouselWidth
173− root.tileWidth = data.tileWidth
174− root.itemCount = data.itemCount
175−
176− var x = Carousel.getViewTranslation(data.x,
177− data.tileWidth,
178− root.gapToMiddlePhase,
179− root.gapToEndPhase,
180− data.translationXViewFactor)
181− compare(x, data.result)
182− }
183−
184− // test for the getItemScale() function
185− function test_getItemScale_data() {
186− return [ // tests for distance
187− {distance: 0, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 1},
188− {distance: 9, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
189− {distance: 999, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
190− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
191− {distance: 3, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.688230855}, // = 1 - (3^2.5 / 50)
192− // tests for continuousIndex
193− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
194− {distance: 1, continuousIndex: 97, numberOfItems: 100, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
195− // tests for numberOfItems
196− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
197− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
198− // tests for scaleFactor
199− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
200− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 2, result: 0.99}, // = 1 - (1 / 100)
201− {distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 0.5, result: 0.96}, // = 1 - (1 / 25)
202− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
203− {distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 2, result: 0.996666666}, // = 1 - (1 / 300) - distanceToBounds is used
204− ]
205− }
206−
207− function test_getItemScale(data) {
208− var scale = Carousel.getItemScale(data.distance,
209− data.continuousIndex,
210− data.numberOfItems,
211− data.scaleFactor)
212− compare(scale, data.result)
213− }
214−
215− // test for the getItemTranslation() function
216− function test_getItemTranslation_data() {
217− return [ // tests for index and selectedIndex
218− {index: 1, selectedIndex: 1, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
219− // tests if distance only affects the sign
220− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
221− {index: 1, selectedIndex: 2, distance: 99, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
222− {index: 1, selectedIndex: 2, distance: -1, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
223− {index: 1, selectedIndex: 2, distance: -99, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
224− // tests for the scale
225− {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
226− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
227− {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 1, maxTranslation: 10, result: 5},
228− // tests for maxScale
229− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 0.98, maxTranslation: 10, result: 9.8},
230− {index: 1, selectedIndex: 2, distance: 1, scale: 0.5, maxScale: 0.95, maxTranslation: 10, result: 4.5},
231− {index: 1, selectedIndex: 2, distance: 1, scale: 0.8, maxScale: 0.93, maxTranslation: 10, result: 1.3},
232− // test for maxTranslation
233− {index: 1, selectedIndex: 2, distance: 1, scale: 1, maxScale: 1, maxTranslation: 1, result: 0},
234− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 1, result: 1},
235− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
236− {index: 1, selectedIndex: 2, distance: 1, scale: 0, maxScale: 1, maxTranslation: 0, result: 0},
237− ]
238− }
239−
240− function test_getItemTranslation(data) {
241− var scale = Carousel.getItemTranslation(data.index,
242− data.selectedIndex,
243− data.distance,
244− data.scale,
245− data.maxScale,
246− data.maxTranslation)
247− compare(scale, data.result)
248− }
249−
250− function test_lastItemXPosition() {
251− var carouselList = findChild(carousel, "listView");
252− var stepAnimation = findInvisibleChild(carouselList, "stepAnimation");
253− var stepDiff = 0;
254− stepAnimation.onRunningChanged.connect(function() { if (stepAnimation.running) stepDiff = Math.abs(carouselList.contentX - stepAnimation.to); })
255− var overshootOnce = false;
256− while (!overshootOnce) {
257− overshootOnce = carouselList.realContentX > carouselList.realContentWidth - carouselList.realWidth;
258− mouseFlick(carousel, carousel.width - units.gu(1),
259− carousel.height / 2,
260− units.gu(2),
261− carousel.height / 2);
262− tryCompare(carouselList, "moving", false);
263− }
264− verify(stepDiff < 1);
265− var x = carouselList.getXFromContinuousIndex(carousel.model - 1);
266− verify(Math.abs(x - carouselList.contentX) < 1);
267− }
268−
269− function test_activate() {
270− var carouselList = findChild(carousel, "listView");
271− var carouselItem = findChild(carousel, "carouselDelegate3")
272− verify(carouselItem, "Could not find delegate");
273−
274− spy.signalName = "clicked";
275− spy.target = carouselItem;
276− mouseClick(carouselItem);
277− tryCompare(carouselList, "moving", false);
278− tryCompare(carouselItem, "explicitlyScaled", true);
279− mouseClick(carouselItem);
280− spy.wait();
281−
282− spy.signalName = "pressAndHold";
283− spy.target = carouselItem;
284− mouseClick(carouselItem);
285− tryCompare(carouselList, "moving", false);
286− tryCompare(carouselItem, "explicitlyScaled", true);
287− mousePress(carouselItem);
288− spy.wait();
289− mouseRelease(carouselItem);
290− }
291− }
292−}