2
* Copyright 2013 Canonical Ltd.
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.
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.
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/>.
19
import "../../../Components/carousel.js" as Carousel
24
property real carouselWidth
25
property int itemCount
26
property real tileWidth
28
property real contentWidth: itemCount * tileWidth
29
// following variables are copied from Carousel.qml
30
// I'm not using the variables directly from there, as then tileWidth and others would be affected as well
31
property real gapToMiddlePhase: Math.min(carouselWidth / 2 - tileWidth / 2, (contentWidth - carouselWidth) / 2)
32
property real gapToEndPhase: contentWidth - carouselWidth - gapToMiddlePhase
33
property real kMiddleIndex: (carouselWidth / 2) / tileWidth - 0.5
34
property real kGapEnd: kMiddleIndex * (1 - gapToEndPhase / gapToMiddlePhase)
35
property real kXBeginningEnd: 1 / tileWidth + kMiddleIndex / gapToMiddlePhase
37
// test for the getContinuousIndex() function
38
function test_getContinuousIndex_data() {
39
// testing for 10 items of size 100 pixel
40
return [ {tag:"at start",
41
x: 0, carouselWidth:400, tileWidth:100, itemCount:10, result: 0},
43
x: 100, carouselWidth:400, tileWidth:100, itemCount:10, result: 2},
45
x: 350, carouselWidth:400, tileWidth:100, itemCount:10, result: 5},
47
x: 600, carouselWidth:400, tileWidth:100, itemCount:10, result: 9},
51
function test_getContinuousIndex(data) {
52
carouselWidth = data.carouselWidth
53
tileWidth = data.tileWidth
54
itemCount = data.itemCount
56
var index = Carousel.getContinuousIndex(data.x,
63
compare(index, data.result)
66
// test for the getXFromContinuousIndex() function
67
function test_getXFromContinuousIndex_data() {
68
return [ {tag:"at start",
69
index: 0, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 0},
71
index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 100},
72
// {tag:"in startup with drawBuffer",
73
// index: 2, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 0},
75
index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 350},
76
// {tag:"in the middle with drawBuffer",
77
// index: 5, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 250},
79
index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:0, result: 600},
80
// {tag:"at end with drawBuffer",
81
// index: 9, carouselWidth:400, tileWidth:100, itemCount:10, drawBuffer:100, result: 500},
85
function test_getXFromContinuousIndex(data) {
86
carouselWidth = data.carouselWidth
87
tileWidth = data.tileWidth
88
itemCount = data.itemCount
90
var x = Carousel.getXFromContinuousIndex(data.index,
97
compare(x, data.result)
100
// test for the getViewTranslation() function
101
function test_getViewTranslation_data() {
102
return [ {tag:"at start - viewfactor 1",
103
x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 150},
104
{tag:"at start - viewfactor 2",
105
x: 0, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:2, result: 300},
107
x: 100, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 50},
108
{tag:"in the middle",
109
x: 350, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: 0},
111
x: 600, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: -150},
115
function test_getViewTranslation(data) {
116
carouselWidth = data.carouselWidth
117
tileWidth = data.tileWidth
118
itemCount = data.itemCount
120
var x = Carousel.getViewTranslation(data.x,
124
data.translationXViewFactor)
125
compare(x, data.result)
128
// test for the getItemScale() function
129
function test_getItemScale_data() {
130
return [ // tests for distance
131
{distance: 0, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 1},
132
{distance: 9, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
133
{distance: 999, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.01},
134
{distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
135
{distance: 3, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.688230855}, // = 1 - (3^2.5 / 50)
136
// tests for continuousIndex
137
{distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
138
{distance: 1, continuousIndex: 97, numberOfItems: 100, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
139
// tests for numberOfItems
140
{distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
141
{distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
142
// tests for scaleFactor
143
{distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 1, result: 0.98}, // = 1 - (1 / 50)
144
{distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 2, result: 0.99}, // = 1 - (1 / 100)
145
{distance: 1, continuousIndex: 50, numberOfItems: 100, scaleFactor: 0.5, result: 0.96}, // = 1 - (1 / 25)
146
{distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 1, result: 0.99}, // = 1 - (1 / 100) - distanceToBounds is used
147
{distance: 1, continuousIndex: 50, numberOfItems: 53, scaleFactor: 2, result: 0.996666666}, // = 1 - (1 / 300) - distanceToBounds is used
151
function test_getItemScale(data) {
152
var scale = Carousel.getItemScale(data.distance,
153
data.continuousIndex,
156
compare(scale, data.result)
159
// test for the getItemTranslation() function
160
function test_getItemTranslation_data() {
161
return [ // tests if distance only affects the sign
162
{distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
163
// {distance: 99, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
164
{distance: 0, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
165
{distance: -1, scale: 0, maxScale: 1, maxTranslation: 10, result: -10},
166
// tests for the scale
167
{distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
168
{distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
169
{distance: 1, scale: 0.5, maxScale: 1, maxTranslation: 10, result: 5},
170
// tests for maxScale
171
{distance: 1, scale: 1, maxScale: 1, maxTranslation: 10, result: 0},
172
{distance: 1, scale: 1, maxScale: 2, maxTranslation: 10, result: 10},
173
// {distance: 1, scale: 1, maxScale: 0, maxTranslation: 10, result: 0},
174
// {distance: 1, scale: 1, maxScale: 99, maxTranslation: 10, result: 10},
175
// test for maxTranslation
176
{distance: 1, scale: 1, maxScale: 1, maxTranslation: 1, result: 0},
177
{distance: 1, scale: 0, maxScale: 1, maxTranslation: 1, result: 1},
178
{distance: 1, scale: 0, maxScale: 1, maxTranslation: 10, result: 10},
179
{distance: 1, scale: 0, maxScale: 1, maxTranslation: 0, result: 0},
183
function test_getItemTranslation(data) {
184
var scale = Carousel.getItemTranslation(data.distance,
188
compare(scale, data.result)