summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Geometry/Curve.html
blob: 5afebdf5a0e94b1fde76816956507f9da4df3efd (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var curve;
    var components = [new OpenLayers.Geometry.Point(10,10), 
                new OpenLayers.Geometry.Point(0,0)];
        
    function test_Curve_constructor (t) {
        t.plan( 3 );
        curve = new OpenLayers.Geometry.Curve();
        t.ok( curve instanceof OpenLayers.Geometry.Curve, "new OpenLayers.Geometry.Curve returns curve object" );
        t.eq( curve.CLASS_NAME, "OpenLayers.Geometry.Curve", "curve.CLASS_NAME is set correctly");
        t.eq( curve.components, [], "curve.components is set correctly");
    }

    function test_Curve_constructor (t) {
        t.plan( 2 );
        curve = new OpenLayers.Geometry.Curve(components);
        t.ok( curve instanceof OpenLayers.Geometry.Curve, "new OpenLayers.Geometry.Curve returns curve object" );
        t.eq( curve.components.length, 2, "curve.components.length is set correctly");
    }

    function test_Curve_clone (t) {
        t.plan( 2 );
        curve = new OpenLayers.Geometry.Curve(components);
        curve2 = curve.clone(); 
        t.ok( curve2 instanceof OpenLayers.Geometry.Curve, "curve.clone() returns curve object" );
        t.eq( curve2.components.length, 2, "curve2.components.length is set correctly");
    }

    function test_Curve_calculateBounds(t) {
        t.plan( 17 );


        var curve = new OpenLayers.Geometry.Curve();
        curve.calculateBounds();
        t.eq(curve.bounds, null, "bounds null when no components");
        
        var p1 = new OpenLayers.Geometry.Point(10,20);
        var p2 = new OpenLayers.Geometry.Point(30,40);
        
        var components = [p1, p2];
        var curve = new OpenLayers.Geometry.Curve(components);
        
        curve.calculateBounds();
        
        t.eq(curve.bounds.left, 10, "good left bounds");
        t.eq(curve.bounds.bottom, 20, "good bottom bounds");
        t.eq(curve.bounds.right, 30, "good right bounds");
        t.eq(curve.bounds.top, 40, "good top bounds");

        var newPoint = new OpenLayers.Geometry.Point(60,70);
        curve.addComponent(newPoint);
        curve.calculateBounds();
        
        t.eq(curve.bounds.left, 10, "good left bounds");
        t.eq(curve.bounds.bottom, 20, "good bottom bounds");
        t.eq(curve.bounds.right, 60, "good right bounds");
        t.eq(curve.bounds.top, 70, "good top bounds");

    //nullifying the bounds

      //before calculation
        curve = new OpenLayers.Geometry.Curve(components);
        curve.bounds = null;        
        curve.calculateBounds();
        
        t.eq(curve.bounds.left, 10, "good left bounds");
        t.eq(curve.bounds.bottom, 20, "good bottom bounds");
        t.eq(curve.bounds.right, 30, "good right bounds");
        t.eq(curve.bounds.top, 40, "good top bounds");

      //before addComponent
        curve.bounds = null;
        curve.addComponent(newPoint);
        curve.calculateBounds();
        
        t.eq(curve.bounds.left, 10, "good left bounds");
        t.eq(curve.bounds.bottom, 20, "good bottom bounds");
        t.eq(curve.bounds.right, 60, "good right bounds");
        t.eq(curve.bounds.top, 70, "good top bounds");

    }

    function test_Curve_addComponent (t) {
        t.plan( 8 );
        curve = new OpenLayers.Geometry.Curve(components);
        curve.addComponent(new OpenLayers.Geometry.Point(20,30));
        bounds = curve.getBounds();
        t.eq( curve.components.length, 3, "new point added to array" );
        t.eq( bounds.top, 30, "top bound is 30 after addComponent" );
        t.eq( bounds.right, 20, "right bound is 20 after addComponent" );
        curve.addComponent(new OpenLayers.Geometry.Point(-20,-30), 1);
        bounds = curve.getBounds();
        t.eq( curve.components.length, 4, "new point added to array" );
        t.eq( bounds.bottom, -30, "bottom bound is -30 after 2nd addComponent" );
        t.eq( bounds.left, -20, "left bound is 20 after 2nd addComponent" );
        t.eq( curve.components[1].x, -20,  "new point.lon is -20 (index worked)" );
        t.eq( curve.components[1].y, -30,  "new point.lat is -30 (index worked)" );
    }

    function test_Curve_removeComponent (t) {
        t.plan( 4 );
        curve = new OpenLayers.Geometry.Curve(components);
        curve.removeComponent(curve.components[1]);
        t.eq( curve.components.length, 1, "curve.components.length is smaller after removeComponent" );
        t.eq( curve.bounds, null, "curve.bounds nullified after removeComponent (for recalculation)" );
        bounds = curve.getBounds();
        t.eq( bounds.left, 10, "left bound is 10 after removeComponent" );
        t.eq( bounds.bottom, 10, "bottom bound is 10 after removeComponent" );
    }

    function test_Curve_getLength (t) {
        t.plan( 4 );

      //no components
        curve = new OpenLayers.Geometry.Curve();
        curve.components = null;
        t.eq(curve.getLength(), 0, "curve with no components has length 0");

      //empty components
        curve.components = [];
        t.eq(curve.getLength(), 0, "curve with empty components has length 0");

      //single point curve
        curve.components = [ new OpenLayers.Geometry.Point(0,0) ];        
        t.eq(curve.getLength(), 0, "curve with only one point has length 0");

      //multipoint
        var newcomponents = [ new OpenLayers.Geometry.Point(0,0),
                        new OpenLayers.Geometry.Point(0,10),
                        new OpenLayers.Geometry.Point(20,10),
                        new OpenLayers.Geometry.Point(20,-10)
        ];
        
        curve = new OpenLayers.Geometry.Curve(newcomponents);
        t.eq(curve.getLength(), 50, "curve.getLength returns a reasonably accurate length" );
    }
    
    function test_Curve_destroy(t) {
        t.plan(1);
        
        var curve = new OpenLayers.Geometry.Curve();
        curve.components = {};
        
        curve.destroy();
        
        t.ok( curve.components == null, "components is cleared well in destruction");
    }
    

  </script>
</head>
<body>
</body>
</html>