| 1 |
/* |
| 2 |
* Copyright 2012, 2014 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser 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.1 |
| 19 |
import Lomiri.Components 1.3 |
| 20 |
|
| 21 |
/*! |
| 22 |
\qmltype LomiriTestCase |
| 23 |
\inqmlmodule Lomiri.Test 1.3 |
| 24 |
\ingroup lomiri-test |
| 25 |
\brief The LomiriTestCase class expands the default TestCase class. |
| 26 |
|
| 27 |
\b{This component is under heavy development.} |
| 28 |
|
| 29 |
This class extends the default QML TestCase class which is available in QtTest 1.0. |
| 30 |
*/ |
| 31 |
TestCase { |
| 32 |
TestUtil { |
| 33 |
id:util |
| 34 |
} |
| 35 |
|
| 36 |
/*! |
| 37 |
This property holds the inner TestUtil instance. |
| 38 |
*/ |
| 39 |
property alias testUtil: util |
| 40 |
|
| 41 |
/*! |
| 42 |
Find a child from the item based on the objectName. |
| 43 |
*/ |
| 44 |
function findChild(obj,objectName) { |
| 45 |
var childrenQueue = new Array(0); |
| 46 |
childrenQueue.push(obj) |
| 47 |
while (childrenQueue.length > 0) { |
| 48 |
if (childrenQueue[0].objectName == objectName) { |
| 49 |
return childrenQueue[0] |
| 50 |
} |
| 51 |
for (var i in childrenQueue[0].children) { |
| 52 |
childrenQueue.push(childrenQueue[0].children[i]) |
| 53 |
} |
| 54 |
childrenQueue.splice(0, 1); |
| 55 |
} |
| 56 |
return null; |
| 57 |
} |
| 58 |
|
| 59 |
/*! |
| 60 |
Find a non-visual child such as QtObject based on objectName. |
| 61 |
*/ |
| 62 |
function findInvisibleChild(obj,objectName) { |
| 63 |
var childrenQueue = new Array(0); |
| 64 |
childrenQueue.push(obj) |
| 65 |
while (childrenQueue.length > 0) { |
| 66 |
if (childrenQueue[0].objectName == objectName) { |
| 67 |
return childrenQueue[0] |
| 68 |
} |
| 69 |
for (var i in childrenQueue[0].data) { |
| 70 |
childrenQueue.push(childrenQueue[0].data[i]) |
| 71 |
} |
| 72 |
childrenQueue.splice(0, 1); |
| 73 |
} |
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
/*! |
| 78 |
Finds a visible child of an \a item having a given \a property with a given |
| 79 |
\a value. |
| 80 |
*/ |
| 81 |
function findChildWithProperty(item, property, value) { |
| 82 |
var childrenQueue = new Array(0); |
| 83 |
childrenQueue.push(item) |
| 84 |
while (childrenQueue.length > 0) { |
| 85 |
var child = childrenQueue[0]; |
| 86 |
if (child.hasOwnProperty(property) && (child[property] === value)) { |
| 87 |
return child; |
| 88 |
} |
| 89 |
for (var i in childrenQueue[0].children) { |
| 90 |
childrenQueue.push(childrenQueue[0].children[i]) |
| 91 |
} |
| 92 |
childrenQueue.splice(0, 1); |
| 93 |
} |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
/*! |
| 98 |
Returns the center point of the \a item. |
| 99 |
*/ |
| 100 |
function centerOf(item) { |
| 101 |
if (!item) { |
| 102 |
return undefined; |
| 103 |
} |
| 104 |
|
| 105 |
var center = Qt.point(item.width / 2, item.height / 2); |
| 106 |
return center; |
| 107 |
} |
| 108 |
|
| 109 |
/*! |
| 110 |
Move Mouse from x,y to distance of dx, dy divided to steps with a stepdelay (ms) and buttons. |
| 111 |
*/ |
| 112 |
function mouseMoveSlowly(item,x,y,dx,dy,steps,stepdelay,buttons) { |
| 113 |
mouseMove(item, x, y, buttons); |
| 114 |
var abs_dx = Math.abs(dx) |
| 115 |
var abs_dy = Math.abs(dy) |
| 116 |
var step_dx = dx / steps; |
| 117 |
var step_dy = dy /steps; |
| 118 |
|
| 119 |
var ix = 0; |
| 120 |
var iy = 0; |
| 121 |
|
| 122 |
for (var step=0; step < steps; step++) { |
| 123 |
if (Math.abs(ix) < abs_dx) { |
| 124 |
ix += step_dx; |
| 125 |
} |
| 126 |
if (Math.abs(iy) < abs_dy) { |
| 127 |
iy += step_dy; |
| 128 |
} |
| 129 |
mouseMove(item, x + ix, y + iy, stepdelay, buttons); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/*! |
| 134 |
\qmlmethod LomiriTestCase::flick(item, x, y, dx, dy, pressTimeout = -1, steps = -1, button = Qt.LeftButton, modifiers = Qt.NoModifiers, delay = -1) |
| 135 |
|
| 136 |
The function produces a flick event when executed on Flickables. When used |
| 137 |
on other components it provides the same functionality as \c mouseDrag() |
| 138 |
function. The optional \a pressTimeout parameter can be used to introduce |
| 139 |
a small delay between the mouse press and the first mouse move. Setting a |
| 140 |
negative or zero value will disable the timeout. |
| 141 |
|
| 142 |
The default flick velocity is built up using 5 move points. This can be altered |
| 143 |
by setting a positive value to \a steps parameter. The bigger the number the |
| 144 |
longer the flick will be. When a negative or zero value is given, the default |
| 145 |
of 5 move points will be used. |
| 146 |
|
| 147 |
\note The function can be used to select a text in a TextField or TextArea by |
| 148 |
specifying at least 400 millisecods to \a pressTimeout. |
| 149 |
*/ |
| 150 |
function flick(item, x, y, dx, dy, pressTimeout, steps, button, modifiers, delay) { |
| 151 |
if (item === undefined || item.x === undefined || item.y === undefined) |
| 152 |
return |
| 153 |
if (button === undefined) |
| 154 |
button = Qt.LeftButton |
| 155 |
if (modifiers === undefined) |
| 156 |
modifiers = Qt.NoModifier |
| 157 |
if (steps === undefined || steps <= 0) |
| 158 |
steps = 4; |
| 159 |
//See bug#1549256 |
| 160 |
//--> delay === 0 it not guaranteed to trigger the flicking logic in QQuickFlickable |
| 161 |
if (delay === undefined) |
| 162 |
delay = 1; |
| 163 |
// make sure we have at least two move steps so the flick will be sensed |
| 164 |
steps += 1; |
| 165 |
|
| 166 |
var ddx = dx / steps; |
| 167 |
var ddy = dy / steps; |
| 168 |
|
| 169 |
mousePress(item, x, y, button, modifiers, delay); |
| 170 |
if (pressTimeout !== undefined && pressTimeout > 0) { |
| 171 |
wait(pressTimeout); |
| 172 |
} |
| 173 |
// mouse moves are all processed immediately, without delay in between events |
| 174 |
mouseMoveSlowly(item, x, y, dx, dy, steps, delay, button); |
| 175 |
mouseRelease(item, x + dx, y + dy, button, modifiers); |
| 176 |
// empty event buffer |
| 177 |
wait(200); |
| 178 |
} |
| 179 |
|
| 180 |
/*! |
| 181 |
\qmlmethod LomiriTestCase::mouseLongPress(item, x, y, button = Qt.LeftButton, modifiers = Qt.NoModifiers, delay = -1) |
| 182 |
|
| 183 |
Simulates a long press on a mouse \a button with an optional \a modifier |
| 184 |
on an \a item. The position is defined by \a x and \a y. If \a delay is |
| 185 |
specified, the test will wait the specified amount of milliseconds before |
| 186 |
the press. |
| 187 |
|
| 188 |
The position given by \a x and \a y is transformed from the co-ordinate |
| 189 |
system of \a item into window co-ordinates and then delivered. |
| 190 |
If \a item is obscured by another item, or a child of \a item occupies |
| 191 |
that position, then the event will be delivered to the other item instead. |
| 192 |
*/ |
| 193 |
function mouseLongPress(item, x, y, button, modifiers, delay) { |
| 194 |
mousePress(item, x, y, button, modifiers, delay); |
| 195 |
// the delay is taken from QQuickMouseArea, add few milliseconds to it to make sure we have |
| 196 |
// the long press triggered |
| 197 |
wait(900); |
| 198 |
} |
| 199 |
|
| 200 |
/*! |
| 201 |
Keeps executing a given parameter-less function until it returns the given |
| 202 |
expected result or the timemout is reached (in which case a test failure |
| 203 |
is generated) |
| 204 |
*/ |
| 205 |
|
| 206 |
function tryCompareFunction(func, expectedResult, timeout) { |
| 207 |
var timeSpent = 0 |
| 208 |
var success = false |
| 209 |
var actualResult |
| 210 |
if (timeout == undefined) { |
| 211 |
timeout = 5000; |
| 212 |
} |
| 213 |
while (timeSpent < timeout && !success) { |
| 214 |
actualResult = func() |
| 215 |
success = qtest_compareInternal(actualResult, expectedResult) |
| 216 |
if (success === false) { |
| 217 |
wait(50) |
| 218 |
timeSpent += 50 |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
var act = qtest_results.stringify(actualResult) |
| 223 |
var exp = qtest_results.stringify(expectedResult) |
| 224 |
if (!qtest_results.compare(success, |
| 225 |
"function returned unexpected result", |
| 226 |
act, exp, |
| 227 |
util.callerFile(), util.callerLine())) { |
| 228 |
throw new Error("QtQuickTest::fail") |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/*! |
| 233 |
Convenience function to allow typing a full string instead of single characters |
| 234 |
*/ |
| 235 |
function typeString(string) { |
| 236 |
for (var i = 0; i < string.length; i++) { |
| 237 |
keyClick(string[i]); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/*! |
| 242 |
Warning message formatter, uses file name, line and column numbers to build up the message. |
| 243 |
*/ |
| 244 |
function warningFormat(line, column, message) { |
| 245 |
return util.callerFile() + ":" + line + ":" + column + ": " + message; |
| 246 |
} |
| 247 |
|
| 248 |
/*! |
| 249 |
Wait for animations of the header and the style inside the header to finish. |
| 250 |
The MainView that has the header that may animate must be passed as an argument. |
| 251 |
*/ |
| 252 |
function waitForHeaderAnimation(mainView) { |
| 253 |
var header = findChild(mainView, "MainView_Header"); |
| 254 |
verify(header !== null, "Could not find header."); |
| 255 |
var headerStyle = findChild(header, "PageHeadStyle"); |
| 256 |
verify(headerStyle !== null, "Could not find header style."); |
| 257 |
|
| 258 |
// Wait for the header to start to move: |
| 259 |
wait(50); |
| 260 |
// Wait for animation of the style inside the header (when pushing/popping): |
| 261 |
tryCompareFunction(function(){ return headerStyle.animating }, false); |
| 262 |
// Wait for the header to finish showing/hiding: |
| 263 |
tryCompareFunction(function(){ return header.moving }, false); |
| 264 |
} |
| 265 |
} |