1
1
/*
2
- * Copyright 2020 Google Inc.
3
- *
2
+ * Copyright 2025 Google LLC
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package com.google.maps.android.data ;
16
+ package com.google.maps.android.data
17
17
18
- import com.google.android.gms.maps.model.LatLng;
18
+ import com.google.android.gms.maps.model.LatLng
19
+ import com.google.common.truth.Truth.assertThat
20
+ import org.junit.Test
21
+ import java.util.Observable
22
+ import java.util.Observer
19
23
20
- import org.junit.Test;
24
+ class FeatureTest {
21
25
22
- import java.util.ArrayList;
23
- import java.util.Arrays;
24
- import java.util.HashMap;
25
- import java.util.Map;
26
+ // Test subclass to access protected members
27
+ private class TestFeature (
28
+ geometry : Geometry <* >? ,
29
+ id : String? ,
30
+ properties : Map <String , String >?
31
+ ) : Feature(geometry, id, properties) {
32
+ public override var id: String?
33
+ get() = super .id
34
+ set(value) {
35
+ super .id = value
36
+ }
26
37
27
- import static org.junit.Assert .* ;
38
+ public override var geometry: Geometry <* >?
39
+ get() = super .geometry
40
+ set(value) {
41
+ super .geometry = value
42
+ }
43
+
44
+ public override fun setProperty (property : String , propertyValue : String ): String? {
45
+ return super .setProperty(property, propertyValue)
46
+ }
47
+
48
+ public override fun removeProperty (property : String ): String? {
49
+ return super .removeProperty(property)
50
+ }
51
+ }
52
+
53
+ @Test
54
+ fun `getId returns correct id` () {
55
+ var feature: Feature = Feature (null , " Pirate" , null )
56
+ assertThat(feature.id).isEqualTo(" Pirate" )
57
+ feature = Feature (null , null , null )
58
+ assertThat(feature.id).isNull()
59
+ }
60
+
61
+ @Test
62
+ fun `properties work as expected` () {
63
+ val properties = mapOf (" Color" to " Red" , " Width" to " 3" )
64
+ val feature = Feature (null , null , properties)
65
+
66
+ assertThat(feature.hasProperty(" llama" )).isFalse()
67
+ assertThat(feature.hasProperty(" Color" )).isTrue()
68
+ assertThat(feature.getProperty(" Color" )).isEqualTo(" Red" )
69
+ assertThat(feature.hasProperties()).isTrue()
70
+ assertThat(feature.propertyKeys).containsExactly(" Color" , " Width" )
71
+ }
72
+
73
+ @Test
74
+ fun `protected property methods work as expected` () {
75
+ val testFeature = TestFeature (null , null , mutableMapOf (" Color" to " Red" , " Width" to " 3" ))
76
+
77
+ assertThat(testFeature.removeProperty(" Width" )).isEqualTo(" 3" )
78
+ assertThat(testFeature.hasProperty(" Width" )).isFalse()
79
+
80
+ assertThat(testFeature.setProperty(" Width" , " 10" )).isNull()
81
+ assertThat(testFeature.getProperty(" Width" )).isEqualTo(" 10" )
82
+
83
+ assertThat(testFeature.setProperty(" Width" , " 500" )).isEqualTo(" 10" )
84
+ assertThat(testFeature.getProperty(" Width" )).isEqualTo(" 500" )
85
+ }
28
86
29
- public class FeatureTest {
30
87
@Test
31
- public void testGetId() {
32
- Feature feature = new Feature (null , " Pirate" , null );
33
- assertNotNull(feature.getId());
34
- assertEquals(" Pirate" , feature.getId());
35
- feature = new Feature (null , null , null );
36
- assertNull(feature.getId());
88
+ fun `geometry works as expected` () {
89
+ val feature = Feature (null , null , null )
90
+ assertThat(feature.hasGeometry()).isFalse()
91
+ assertThat(feature.geometry).isNull()
92
+
93
+ val point = Point (LatLng (0.0 , 0.0 ))
94
+ val featureWithPoint = Feature (point, null , null )
95
+ assertThat(featureWithPoint.hasGeometry()).isTrue()
96
+ assertThat(featureWithPoint.geometry).isEqualTo(point)
37
97
}
38
98
39
99
@Test
40
- public void testProperty() {
41
- Map <String , String > properties = new HashMap <> ();
42
- properties.put(" Color" , " Red" );
43
- properties.put(" Width" , " 3" );
44
- Feature feature = new Feature (null , null , properties);
45
- assertFalse(feature.hasProperty(" llama" ));
46
- assertTrue(feature.hasProperty(" Color" ));
47
- assertEquals(" Red" , feature.getProperty(" Color" ));
48
- assertTrue(feature.hasProperty(" Width" ));
49
- assertEquals(" 3" , feature.getProperty(" Width" ));
50
- assertNull(feature.removeProperty(" banana" ));
51
- assertEquals(" 3" , feature.removeProperty(" Width" ));
52
- assertNull(feature.setProperty(" Width" , " 10" ));
53
- assertEquals(" 10" , feature.setProperty(" Width" , " 500" ));
100
+ fun `protected geometry setter works` () {
101
+ val testFeature = TestFeature (null , null , null )
102
+ val point = Point (LatLng (0.0 , 0.0 ))
103
+ testFeature.geometry = point
104
+ assertThat(testFeature.geometry).isEqualTo(point)
54
105
}
55
106
56
107
@Test
57
- public void testGeometry() {
58
- Feature feature = new Feature (null , null , null );
59
- assertNull(feature.getGeometry());
60
- Point point = new Point (new LatLng (0 , 0 ));
61
- feature.setGeometry(point);
62
- assertEquals(point, feature.getGeometry());
63
- feature.setGeometry(null );
64
- assertNull(feature.getGeometry());
65
-
66
- LineString lineString =
67
- new LineString (
68
- new ArrayList <> (Arrays .asList(new LatLng (0 , 0 ), new LatLng (50 , 50 ))));
69
- feature = new Feature (lineString, null , null );
70
- assertEquals(lineString, feature.getGeometry());
71
- feature.setGeometry(point);
72
- assertEquals(point, feature.getGeometry());
73
- feature.setGeometry(null );
74
- assertNull(feature.getGeometry());
75
- feature.setGeometry(lineString);
76
- assertEquals(lineString, feature.getGeometry());
108
+ fun `observable notifies on change` () {
109
+ val feature = TestFeature (null , null , null )
110
+ val observer = TestObserver ()
111
+ feature.addObserver(observer)
112
+
113
+ feature.setProperty(" key" , " value" )
114
+ assertThat(observer.wasUpdated).isTrue()
115
+ observer.wasUpdated = false // reset
116
+
117
+ feature.removeProperty(" key" )
118
+ assertThat(observer.wasUpdated).isTrue()
119
+ observer.wasUpdated = false // reset
120
+
121
+ feature.geometry = Point (LatLng (1.0 , 1.0 ))
122
+ assertThat(observer.wasUpdated).isTrue()
123
+ }
124
+
125
+ class TestObserver : Observer {
126
+ var wasUpdated = false
127
+ override fun update (o : Observable ? , arg : Any? ) {
128
+ wasUpdated = true
129
+ }
77
130
}
78
- }
131
+ }
0 commit comments