Skip to content

Commit dfa18d2

Browse files
authored
feat: migrate onClick out of the layout files, add testing (#2328)
* feat(test): Add tests and refactor demos to use ViewBinding This commit introduces a suite of Espresso tests for several demo activities and refactors numerous activities to use ViewBinding, eliminating `findViewById` and `onClick` XML attributes. - **Testing Infrastructure:** - Adds `MapIdlingResource` for both Java and Kotlin test sources to synchronize Espresso tests with map camera movements, preventing flaky tests. - Introduces custom Truth subjects (`LatLngSubject`, `LatLngBoundsSubject`) for more readable and precise assertions on map-related objects, with built-in tolerance for floating-point comparisons. - Adds new dependencies for testing: `espresso-idling-resource`, `truth`, and `uiautomator`. - Increases Gradle daemon heap size to support test execution. - **New Espresso Tests:** - Adds comprehensive tests for `GroundOverlayDemoActivity`, `CameraClampingDemoActivity`, `IndoorDemoActivity`, `CameraDemoActivity`, and `VisibleRegionDemoActivity` in both Java and Kotlin modules. - Tests verify UI interactions, camera behavior, and state changes within the demo activities. - **Code Refactoring:** - Enables ViewBinding in the `java-app`, `kotlin-app`, and `common-ui` modules. - Refactors all major demo activities (e.g., `GroundOverlayDemoActivity`, `MarkerDemoActivity`, `CircleDemoActivity`, `UiSettingsDemoActivity`) to use ViewBinding. - Replaces `android:onClick` XML attributes with programmatic `setOnClickListener` calls, improving code organization and maintainability. - Enhances Javadoc and KDoc in `GroundOverlayDemoActivity` to better explain the concepts and implementation details, improving its educational value. * feat(androidTest): Refactor GroundOverlayDemoActivityTest and add MapProvider Refactors the GroundOverlayDemoActivityTest to improve reliability and readability. This commit introduces a `MapProvider` interface and a base `MapDemoActivityTest` class to abstract away common map initialization logic. Key changes include: - Add a `MapProvider` interface to ensure activities provide a `GoogleMap` object. - Create an abstract `MapDemoActivityTest` to handle common test setup, including map initialization and idling resources (WIP). - Update `GroundOverlayDemoActivityTest` to extend `MapDemoActivityTest`, simplifying its setup logic. - Replace `Thread.sleep()` calls with `idlingResource.waitForIdle()`. - Update `GroundOverlayDemoActivity` to use `lateinit` for overlays, preventing nullable types. - Add `maps-utils-ktx` dependency for idiomatic spherical offset calculations. * fix(test): Improve CircleDemoActivityTest readability and precision Enhances the `testLongClickAddsNewCircle` in `CircleDemoActivityTest` by adding detailed comments that explain the test's flow and the critical reason for camera movements: to minimize precision loss during LatLng to screen coordinate conversion for accurate click event simulation. This commit also includes the necessary `LatLngSubject` extension, introducing a `isWith(tolerance).of(target)` assertion. This new assertion allows for more precise and readable verification of spherical distances between `LatLng` objects, directly supporting the improved test logic in `CircleDemoActivityTest`. * feat(java): Align CircleDemoActivity and tests with Kotlin version * refactor(test): Extend MapDemoActivityTest and improve synchronization Refactors the java version of `GroundOverlayDemoActivityTest` to extend the common `MapDemoActivityTest` base class. This change centralizes test setup, teardown, and map interaction logic, removing redundant code and improving maintainability. - `GroundOverlayDemoActivity` now implements the `MapProvider` interface to correctly expose the map instance and its ready state to the test framework. - Replaces `Thread.sleep()` calls in `CircleDemoActivityTest` and `GroundOverlayDemoActivityTest` with `idlingResource.waitForIdle()`. This creates more reliable tests by waiting for map events to complete rather than using fixed delays. - Adds a `waitForIdle(long)` overload to `MapIdlingResource` to handle cases requiring a brief, controlled pause after the map has settled. * chore: fix missing copyright headers * refactor(kotlin): Address lint warnings This commit applies several small refactorings to the Kotlin demo activities, focusing on code modernization and resolving lint issues. - In `MarkerDemoActivity`, replaces `Math.max()` with the more idiomatic Kotlin function `coerceAtLeast()`. - Simplifies trigonometric function calls by using top-level `sin` and `cos` from the Kotlin standard library instead of `Math`. - Removes unused imports from `MarkerDemoActivity`. - In `LayersDemoActivity`, adds `@SuppressLint("MissingPermission")` to the `onMyLocationToggled` method to address a lint warning. * refactor(ui): Use android:name for FragmentContainerView This commit updates the layout files to use the `android:name` attribute instead of the `class` attribute for `FragmentContainerView` to declare the fragment class. This is the modern, recommended practice. It also removes minor whitespace and `onClick` attributes that are no longer necessary. - Replaces the `class` attribute with `android:name` for all instances of `SupportMapFragment` and `SupportStreetViewPanoramaFragment` within `FragmentContainerView` tags across multiple demo layouts. - Removes unnecessary `onClick` attributes from checkboxes in `layers_demo.xml`. - Cleans up minor whitespace in `camera_clamping_demo.xml`, `street_view_panorama_navigation_demo.xml`, `indoor_demo.xml`, and `ground_overlay_demo.xml`.
1 parent c624297 commit dfa18d2

File tree

90 files changed

+3305
-1165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3305
-1165
lines changed

ApiDemos/project/common-ui/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ android {
4141
)
4242
}
4343
}
44+
lint {
45+
abortOnError = false
46+
}
47+
buildFeatures {
48+
viewBinding = true
49+
}
4450
compileOptions {
4551
sourceCompatibility = JavaVersion.VERSION_17
4652
targetCompatibility = JavaVersion.VERSION_17

ApiDemos/project/common-ui/src/main/res/layout-land/snapshot_demo.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
-->
1717

1818
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:id="@+id/map_container"
1920
android:layout_width="match_parent"
2021
android:layout_height="match_parent"
2122
android:orientation="vertical">
@@ -56,13 +57,10 @@
5657
<Button
5758
android:layout_width="wrap_content"
5859
android:layout_height="wrap_content"
59-
android:onClick="onScreenshot"
6060
android:text="@string/snapshot_take_button" />
6161

6262
<Button
63-
android:layout_width="wrap_content"
6463
android:layout_height="wrap_content"
65-
android:onClick="onClearScreenshot"
66-
android:text="@string/snapshot_clear_button" />
64+
android:text="@string/snapshot_clear_button"/>
6765
</LinearLayout>
6866
</LinearLayout>

ApiDemos/project/common-ui/src/main/res/layout/background_color_customization_demo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
android:layout_width="0dp"
3838
android:layout_height="0dp"
3939
map:backgroundColor="#fff0b2dd"
40-
class="com.google.android.gms.maps.SupportMapFragment"
40+
android:name="com.google.android.gms.maps.SupportMapFragment"
4141
app:layout_constraintTop_toBottomOf="@+id/top_bar"
4242
app:layout_constraintBottom_toBottomOf="parent"
4343
app:layout_constraintStart_toStartOf="parent"

ApiDemos/project/common-ui/src/main/res/layout/background_color_customization_programmatic_demo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<androidx.fragment.app.FragmentContainerView
4040
android:id="@+id/map"
41-
class="com.google.android.gms.maps.SupportMapFragment"
41+
android:name="com.google.android.gms.maps.SupportMapFragment"
4242
android:layout_width="0dp"
4343
android:layout_height="0dp"
4444
app:layout_constraintBottom_toBottomOf="parent"

ApiDemos/project/common-ui/src/main/res/layout/basic_demo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<androidx.fragment.app.FragmentContainerView
3434
android:id="@+id/map"
35-
class="com.google.android.gms.maps.SupportMapFragment"
35+
android:name="com.google.android.gms.maps.SupportMapFragment"
3636
android:layout_width="match_parent"
3737
android:layout_height="match_parent"
3838
app:layout_constraintBottom_toBottomOf="parent"

ApiDemos/project/common-ui/src/main/res/layout/camera_clamping_demo.xml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
app:layout_constraintEnd_toEndOf="parent"
2929
app:layout_constraintStart_toStartOf="parent"
3030
app:layout_constraintTop_toTopOf="parent"
31-
app:title="@string/background_color_customization_demo_label"
31+
app:title="@string/camera_clamping_demo_label"
3232
app:titleTextColor="?attr/colorOnPrimary" />
3333
<androidx.fragment.app.FragmentContainerView
3434
app:layout_constraintStart_toStartOf="parent"
3535
app:layout_constraintEnd_toEndOf="parent"
3636
app:layout_constraintTop_toBottomOf="@+id/container"
3737
app:layout_constraintBottom_toBottomOf="parent"
3838
android:id="@+id/map"
39-
class="com.google.android.gms.maps.SupportMapFragment"
39+
android:name="com.google.android.gms.maps.SupportMapFragment"
4040
android:layout_width="fill_parent"
4141
android:layout_height="fill_parent"
4242
map:cameraMaxZoomPreference="14.0"
@@ -68,21 +68,18 @@
6868
android:id="@+id/clamp_min_zoom"
6969
android:layout_width="wrap_content"
7070
android:layout_height="wrap_content"
71-
android:onClick="onSetMinZoomClamp"
7271
android:text="@string/clamp_min_zoom" />
7372

7473
<com.google.android.material.button.MaterialButton
7574
android:id="@+id/clamp_max_zoom"
7675
android:layout_width="wrap_content"
7776
android:layout_height="wrap_content"
78-
android:onClick="onSetMaxZoomClamp"
7977
android:text="@string/clamp_max_zoom" />
8078

8179
<com.google.android.material.button.MaterialButton
8280
android:id="@+id/clamp_zoom_reset"
8381
android:layout_width="wrap_content"
8482
android:layout_height="wrap_content"
85-
android:onClick="onMinMaxZoomClampReset"
8683
android:text="@string/clamp_zoom_reset" />
8784
</LinearLayout>
8885

@@ -96,22 +93,19 @@
9693
android:layout_width="wrap_content"
9794
android:layout_height="wrap_content"
9895
android:layout_weight="0.5"
99-
android:onClick="onClampToAdelaide"
10096
android:text="@string/clamp_latlng_adelaide" />
10197

10298
<com.google.android.material.button.MaterialButton
10399
android:id="@+id/clamp_latlng_pacific"
104100
android:layout_width="wrap_content"
105101
android:layout_height="wrap_content"
106102
android:layout_weight="0.5"
107-
android:onClick="onClampToPacific"
108103
android:text="@string/clamp_latlng_pacific" />
109104

110105
<com.google.android.material.button.MaterialButton
111106
android:id="@+id/clamp_latlng_reset"
112107
android:layout_width="wrap_content"
113108
android:layout_height="wrap_content"
114-
android:onClick="onLatLngClampReset"
115109
android:text="@string/clamp_latlng_reset" />
116110
</LinearLayout>
117111

ApiDemos/project/common-ui/src/main/res/layout/camera_demo.xml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<androidx.fragment.app.FragmentContainerView
3434
android:id="@+id/map"
35-
class="com.google.android.gms.maps.SupportMapFragment"
35+
android:name="com.google.android.gms.maps.SupportMapFragment"
3636
android:layout_width="match_parent"
3737
android:layout_height="match_parent" />
3838

@@ -55,17 +55,15 @@
5555
android:id="@+id/stop_animation"
5656
android:layout_width="wrap_content"
5757
android:layout_height="wrap_content"
58-
android:onClick="onStopAnimation"
5958
android:text="@string/stop_animation" />
6059

6160
<ToggleButton
6261
android:id="@+id/animate"
6362
android:layout_width="wrap_content"
6463
android:layout_height="wrap_content"
6564
android:checked="true"
66-
android:onClick="onToggleAnimate"
67-
android:textOff="@string/animate"
68-
android:textOn="@string/animate" />
65+
android:textOn="@string/animate"
66+
android:textOff="@string/animate" />
6967
</LinearLayout>
7068

7169
<RelativeLayout
@@ -126,15 +124,13 @@
126124
android:layout_width="wrap_content"
127125
android:layout_height="wrap_content"
128126
android:minWidth="48dp"
129-
android:onClick="onZoomIn"
130127
android:text="@string/zoom_in" />
131128

132129
<com.google.android.material.button.MaterialButton
133130
android:id="@+id/zoom_out"
134131
android:layout_width="wrap_content"
135132
android:layout_height="wrap_content"
136133
android:minWidth="48dp"
137-
android:onClick="onZoomOut"
138134
android:text="@string/zoom_out" />
139135
</LinearLayout>
140136

@@ -149,15 +145,13 @@
149145
android:layout_width="wrap_content"
150146
android:layout_height="wrap_content"
151147
android:minWidth="48dp"
152-
android:onClick="onTiltMore"
153148
android:text="@string/tilt_more" />
154149

155150
<com.google.android.material.button.MaterialButton
156151
android:id="@+id/tilt_less"
157152
android:layout_width="wrap_content"
158153
android:layout_height="wrap_content"
159154
android:minWidth="48dp"
160-
android:onClick="onTiltLess"
161155
android:text="@string/tilt_less" />
162156
</LinearLayout>
163157
</LinearLayout>
@@ -175,12 +169,11 @@
175169
android:id="@+id/duration_toggle"
176170
android:layout_width="wrap_content"
177171
android:layout_height="wrap_content"
178-
android:onClick="onToggleCustomDuration"
179172
android:text="@string/duration" />
180173

181174
<SeekBar
182175
android:id="@+id/duration_bar"
183-
android:layout_width="match_parent"
176+
android:layout_width="0dp"
184177
android:layout_height="wrap_content"
185178
android:layout_weight="1"
186179
android:max="5000" />

ApiDemos/project/common-ui/src/main/res/layout/circle_demo.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,14 @@
127127
android:layout_width="wrap_content"
128128
android:layout_height="wrap_content"
129129
android:checked="true"
130-
android:onClick="toggleClickability"
131130
android:text="@string/clickable" />
132131
</TableRow>
133132

134133
</TableLayout>
135134

136135
<androidx.fragment.app.FragmentContainerView
137136
android:id="@+id/map"
138-
class="com.google.android.gms.maps.SupportMapFragment"
137+
android:name="com.google.android.gms.maps.SupportMapFragment"
139138
android:layout_width="match_parent"
140139
android:layout_height="match_parent" />
141140

ApiDemos/project/common-ui/src/main/res/layout/cloud_styling_basic_demo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
<androidx.fragment.app.FragmentContainerView
3636
android:id="@+id/map"
37-
class="com.google.android.gms.maps.SupportMapFragment"
37+
android:name="com.google.android.gms.maps.SupportMapFragment"
3838
android:layout_width="match_parent"
3939
android:layout_height="match_parent"
4040
map:cameraTargetLat="47.6089945"

ApiDemos/project/common-ui/src/main/res/layout/events_demo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<androidx.fragment.app.FragmentContainerView
3333
android:id="@+id/map"
34-
class="com.google.android.gms.maps.SupportMapFragment"
34+
android:name="com.google.android.gms.maps.SupportMapFragment"
3535
android:layout_width="match_parent"
3636
android:layout_height="match_parent"
3737
app:layout_constraintTop_toBottomOf="@+id/top_bar" />

0 commit comments

Comments
 (0)