summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Kinetic.html
blob: ba50b5370a5620e4401ae8cd9c84efd5bc09170a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">

    function test_Kinetic (t) {
        t.plan(17);
        var finish = false;
        var results = {
            110: {x: -2.7, y: -3.6, end: false},
            120: {x: -2.1, y: -2.8, end: false},
            130: {x: -1.5, y: -2.0, end: false},
            140: {x: -0.9, y: -1.2, end: false},
            150: {x: -0.3, y: -0.4, end: true}
        };

        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };
        
        var interval = 10; // arbitrary value for tests 

        var originalLoopAnimation = OpenLayers.Animation.start;
        OpenLayers.Animation.start = function(callback) {
            while (!finish) {
                var time = new Date().getTime();
                Date.prototype.getTime = function() { return time+interval };
                callback();
            }
        };

        var kinetic = new OpenLayers.Kinetic({
            deceleration: 0.01
        });
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:30, y:40});

        t.eq(measure.speed, 0.5, "correct speed");
        t.eq(measure.theta, Math.PI - Math.atan(40/30), "correct angle");

        // fake timer id    
        kinetic.timerId = 0;
        kinetic.move(measure, function(x, y, end) {
            var result = results[new Date().getTime()];
            t.eq(Math.round(x * 1000) / 1000, result.x, "correct x");
            t.eq(Math.round(y * 1000) / 1000, result.y, "correct y");
            t.eq(end, result.end, "correct end");
            finish = end;
        });
        
        Date.prototype.getTime = originalGetTime;
        OpenLayers.Animation.start = originalLoopAnimation;
    }

    function test_Angle (t) {
        t.plan(8);
        var results = [
            {speed: 0.5, theta: Math.round((Math.PI - Math.atan(40/30)) * 1000000) / 1000000},
            {speed: 0.5, theta: Math.round((Math.PI + Math.atan(40/30)) * 1000000) / 1000000},
            {speed: 0.5, theta: Math.round((- Math.atan(40/30)) * 1000000) / 1000000},
            {speed: 0.5, theta: Math.round((Math.atan(40/30)) * 1000000) / 1000000}
        ];

        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:30, y:40});

        t.eq(measure.speed, results[0].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[0].theta, "correct angle");


        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:30, y:-40});

        t.eq(measure.speed, results[1].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[1].theta, "correct angle");


        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:-30, y:-40});

        t.eq(measure.speed, results[2].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[2].theta, "correct angle");

        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:-30, y:40});

        t.eq(measure.speed, results[3].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[3].theta, "correct angle");

        Date.prototype.getTime = originalGetTime;
    }
  </script>
</head>
<body>
    <div id="map" style="width: 600px; height: 300px;"/>
    <div style="display: none;"><div id="invisimap"></div></div>
</body>
</html>