~ahayzen/volleyball2d/ai-angle-and-concentration

« back to all changes in this revision

Viewing changes to tests/unit/tst_game_ball.qml

  • Committer: Andrew Hayzen
  • Date: 2014-12-14 18:49:37 UTC
  • Revision ID: ahayzen@gmail.com-20141214184937-e9201tuaegyn2eqt
* Add qmlunittests for the ball

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014
 
3
 *      Andrew Hayzen <ahayzen@gmail.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; version 3.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
import Bacon2D 1.0
 
19
import QtQuick 2.3
 
20
import QtTest 1.0
 
21
import Ubuntu.Components 1.1
 
22
import "../../components"
 
23
 
 
24
// See more details @ http://doc.qt.io/qt-5/qml-qttest-testcase.html
 
25
// Ensure qml-module-qttest is installed
 
26
// Execute tests with:
 
27
//   qmltestrunner
 
28
 
 
29
Item {
 
30
    /*
 
31
     * Basic game scene with a ball in the air and ground underneath
 
32
     * If the ball collides with anything it will store that in the
 
33
     * property collidedWith
 
34
     */
 
35
    Scene {
 
36
        id: scene
 
37
        height: units.gu(20)
 
38
        gravity: Qt.point(0, 20)
 
39
        pixelsPerMeter: units.gu(3)
 
40
        physics: true
 
41
        running: false
 
42
        viewport: Viewport {
 
43
        }
 
44
        width: units.gu(20)
 
45
 
 
46
        Ball {
 
47
            id: ball
 
48
            x: 5
 
49
            y: 100
 
50
 
 
51
            property var collidedWith
 
52
 
 
53
            onContact: collidedWith = other
 
54
        }
 
55
 
 
56
        Wall {
 
57
            id: ground
 
58
            anchors {
 
59
                bottom: parent.bottom
 
60
                left: parent.left
 
61
                right: parent.right
 
62
            }
 
63
            height: units.gu(1)
 
64
        }
 
65
    }
 
66
 
 
67
    TestCase {
 
68
        name: "GameBall"
 
69
 
 
70
        function init() {
 
71
            console.debug(">> init");
 
72
            compare(scene.running, false, "Scene.Running was not false");
 
73
            fuzzyCompare(ball.y, 100, 1, "ball.y was not at start position");
 
74
            console.debug("<< init");
 
75
        }
 
76
 
 
77
        function cleanup() {
 
78
            // Reset the ball position and any game states
 
79
            console.debug(">> cleanup");
 
80
            scene.running = false
 
81
            ball.y = 100
 
82
            console.debug("<< cleanup");
 
83
        }
 
84
 
 
85
        function initTestCase() {
 
86
            console.debug(">> initTestCase");
 
87
            console.debug("<< initTestCase");
 
88
        }
 
89
 
 
90
        function cleanupTestCase() {
 
91
            console.debug(">> cleanupTestCase");
 
92
            console.debug("<< cleanupTestCase");
 
93
        }
 
94
 
 
95
        function test_ball_collides_with_ground() {
 
96
            // When the game is running, test the ball collides with the ground
 
97
            scene.running = true;
 
98
 
 
99
            tryCompare(ball, "collidedWith", ground, 2000,
 
100
                       "Ball did not collided with wall")
 
101
        }
 
102
 
 
103
        function test_ball_falls() {
 
104
            // When the game is running, test the ball falls
 
105
            scene.running = true;
 
106
 
 
107
            wait(100);  // wait for the ball to fall
 
108
 
 
109
            // Need to check ball.y != 100 so expect failure
 
110
            expectFail("", "Ball.y should change (it is falling)")
 
111
 
 
112
            fuzzyCompare(ball.y, 100, 1, "Ball.y equals inital y")
 
113
        }
 
114
 
 
115
        function test_ball_stationary() {
 
116
            // When the game is not running, test the ball remains stationary
 
117
 
 
118
            wait(100);  // wait to check the ball doesn't fall
 
119
 
 
120
            // fuzzyCompare as ball.y could be 100.abc
 
121
            fuzzyCompare(ball.y, 100, 1, "Ball.y doesn't equal inital y")
 
122
        }
 
123
    }
 
124
}
 
125