~unity-team/unity8/trunk

« back to all changes in this revision

Viewing changes to tests/qmltests/Components/tst_Carousel.qml

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.0
 
18
import QtTest 1.0
 
19
import "../../../Components/carousel.js" as Carousel
 
20
 
 
21
TestCase {
 
22
    name: "Carousel"
 
23
 
 
24
    property real carouselWidth
 
25
    property int itemCount
 
26
    property real tileWidth
 
27
 
 
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
 
36
 
 
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},
 
42
                 {tag:"in startup",
 
43
                  x: 100, carouselWidth:400, tileWidth:100, itemCount:10, result: 2},
 
44
                 {tag:"in the middle",
 
45
                  x: 350, carouselWidth:400, tileWidth:100, itemCount:10, result: 5},
 
46
                 {tag:"at end",
 
47
                  x: 600, carouselWidth:400, tileWidth:100, itemCount:10, result: 9},
 
48
               ]
 
49
    }
 
50
 
 
51
    function test_getContinuousIndex(data) {
 
52
        carouselWidth = data.carouselWidth
 
53
        tileWidth = data.tileWidth
 
54
        itemCount = data.itemCount
 
55
 
 
56
        var index = Carousel.getContinuousIndex(data.x,
 
57
                                                data.tileWidth,
 
58
                                                gapToMiddlePhase,
 
59
                                                gapToEndPhase,
 
60
                                                kGapEnd,
 
61
                                                kMiddleIndex,
 
62
                                                kXBeginningEnd)
 
63
        compare(index, data.result)
 
64
    }
 
65
 
 
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},
 
70
                 {tag:"in startup",
 
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},
 
74
                 {tag:"in the middle",
 
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},
 
78
                 {tag:"at end",
 
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},
 
82
               ]
 
83
    }
 
84
 
 
85
    function test_getXFromContinuousIndex(data) {
 
86
        carouselWidth = data.carouselWidth
 
87
        tileWidth = data.tileWidth
 
88
        itemCount = data.itemCount
 
89
 
 
90
        var x = Carousel.getXFromContinuousIndex(data.index,
 
91
                                                 data.carouselWidth,
 
92
                                                 contentWidth,
 
93
                                                 data.tileWidth,
 
94
                                                 gapToMiddlePhase,
 
95
                                                 gapToEndPhase,
 
96
                                                 data.drawBuffer)
 
97
        compare(x, data.result)
 
98
    }
 
99
 
 
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},
 
106
                 {tag:"in startup",
 
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},
 
110
                 {tag:"at end",
 
111
                  x: 600, carouselWidth:400, tileWidth:100, itemCount:10, translationXViewFactor:1, result: -150},
 
112
               ]
 
113
    }
 
114
 
 
115
    function test_getViewTranslation(data) {
 
116
        carouselWidth = data.carouselWidth
 
117
        tileWidth = data.tileWidth
 
118
        itemCount = data.itemCount
 
119
 
 
120
        var x = Carousel.getViewTranslation(data.x,
 
121
                                            data.tileWidth,
 
122
                                            gapToMiddlePhase,
 
123
                                            gapToEndPhase,
 
124
                                            data.translationXViewFactor)
 
125
        compare(x, data.result)
 
126
    }
 
127
 
 
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
 
148
               ]
 
149
    }
 
150
 
 
151
    function test_getItemScale(data) {
 
152
        var scale = Carousel.getItemScale(data.distance,
 
153
                                          data.continuousIndex,
 
154
                                          data.numberOfItems,
 
155
                                          data.scaleFactor)
 
156
        compare(scale, data.result)
 
157
    }
 
158
 
 
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},
 
180
               ]
 
181
    }
 
182
 
 
183
    function test_getItemTranslation(data) {
 
184
        var scale = Carousel.getItemTranslation(data.distance,
 
185
                                                data.scale,
 
186
                                                data.maxScale,
 
187
                                                data.maxTranslation)
 
188
        compare(scale, data.result)
 
189
    }
 
190
}