1+ package org.scottishtecharmy.soundscape
2+
3+ import androidx.compose.foundation.layout.WindowInsets
4+ import androidx.compose.foundation.layout.safeDrawing
5+ import androidx.compose.foundation.layout.windowInsetsPadding
6+ import androidx.compose.runtime.Composable
7+ import androidx.compose.ui.Modifier
8+ import androidx.compose.ui.test.junit4.createComposeRule
9+ import androidx.navigation.NavController
10+ import androidx.preference.PreferenceManager
11+ import androidx.test.platform.app.InstrumentationRegistry
12+ import kotlinx.coroutines.flow.MutableStateFlow
13+ import org.scottishtecharmy.soundscape.ui.theme.SoundscapeTheme
14+ import org.junit.Rule
15+ import org.junit.Test
16+ import org.scottishtecharmy.soundscape.MainActivity.Companion.ACCESSIBLE_MAP_KEY
17+ import org.scottishtecharmy.soundscape.database.local.model.MarkerEntity
18+ import org.scottishtecharmy.soundscape.database.local.model.RouteEntity
19+ import org.scottishtecharmy.soundscape.database.local.model.RouteWithMarkers
20+ import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
21+ import org.scottishtecharmy.soundscape.screens.home.BottomButtonFunctions
22+ import org.scottishtecharmy.soundscape.screens.home.RouteFunctions
23+ import org.scottishtecharmy.soundscape.screens.home.StreetPreviewFunctions
24+ import org.scottishtecharmy.soundscape.screens.home.data.LocationDescription
25+ import org.scottishtecharmy.soundscape.screens.home.home.Home
26+ import org.scottishtecharmy.soundscape.screens.home.placesnearby.PlacesNearbyUiState
27+ import org.scottishtecharmy.soundscape.screens.markers_routes.screens.addandeditroutescreen.AddAndEditRouteScreen
28+ import org.scottishtecharmy.soundscape.screens.markers_routes.screens.addandeditroutescreen.AddAndEditRouteUiState
29+ import org.scottishtecharmy.soundscape.screens.markers_routes.screens.routedetailsscreen.RouteDetailsScreen
30+ import org.scottishtecharmy.soundscape.screens.markers_routes.screens.routedetailsscreen.RouteDetailsUiState
31+ import org.scottishtecharmy.soundscape.services.RoutePlayerState
32+ import org.scottishtecharmy.soundscape.utils.processMaps
33+ import org.scottishtecharmy.soundscape.viewmodels.home.HomeState
34+
35+ // This is very helpful:
36+ // https://developer.android.com/develop/ui/compose/testing/testing-cheatsheet
37+ class DocumentationScreens {
38+ @get:Rule
39+ val composeTestRule = createComposeRule()
40+
41+ private val location = LngLatAlt (
42+ - 4.3178027 ,
43+ 55.9410791
44+ )
45+
46+ private val routeToShops = RouteWithMarkers (
47+ RouteEntity (
48+ name = " Route to shops" ,
49+ description = " " ,
50+ routeId = 1L
51+ ),
52+ listOf (
53+ MarkerEntity (
54+ name = " Craigton Road" ,
55+ longitude = - 4.3239319 ,
56+ latitude = 55.9446396 ,
57+ markerId = 1L
58+ ),
59+ MarkerEntity (
60+ name = " Clober Road" ,
61+ longitude = - 4.3210534 ,
62+ latitude = 55.9417227 ,
63+ markerId = 2L
64+ ),
65+ MarkerEntity (
66+ name = " Douglas Street" ,
67+ longitude = - 4.3194968 ,
68+ latitude = 55.9406974 ,
69+ markerId = 3L
70+ ),
71+ MarkerEntity (
72+ name = " Underpass to shops" ,
73+ longitude = - 4.3175668 ,
74+ latitude = 55.9399973 ,
75+ markerId = 4L
76+ )
77+ )
78+ )
79+
80+ private fun runScreenTest (
81+ screenshotFileName : String ,
82+ testCode : @Composable () -> Unit ) {
83+
84+ val targetContext = InstrumentationRegistry .getInstrumentation().targetContext
85+
86+ // Unpack map assets
87+ processMaps(targetContext)
88+
89+ val themeStateFlow = MutableStateFlow (ThemeState ())
90+ composeTestRule.setContent {
91+ SoundscapeTheme (themeStateFlow) {
92+ testCode()
93+ }
94+ }
95+
96+ // Delay to allow the maps to load
97+ Thread .sleep(5000 )
98+
99+ // Capture screenshot of the root composable
100+ ScreenshotUtils .captureAndSaveScreenshot(
101+ context = targetContext, // Use target context
102+ filename = screenshotFileName
103+ )
104+ }
105+
106+ @Test
107+ fun homeScreen (){
108+
109+ val targetContext = InstrumentationRegistry .getInstrumentation().targetContext
110+
111+ val sharedPreferences = PreferenceManager .getDefaultSharedPreferences(targetContext)
112+ // Use accessible map
113+ sharedPreferences.edit().putBoolean(ACCESSIBLE_MAP_KEY , true ).apply ()
114+
115+ runScreenTest(" homeScreen" ) {
116+ Home (
117+ state = HomeState (
118+ location = location,
119+ heading = 45.0F ,
120+
121+ ),
122+ onNavigate = {},
123+ preferences = sharedPreferences,
124+ onMapLongClick = { false },
125+ bottomButtonFunctions = BottomButtonFunctions (null ),
126+ getCurrentLocationDescription = {
127+ LocationDescription (
128+ name = " Milngavie" ,
129+ location = location
130+ )
131+ },
132+ searchText = " " ,
133+ onToggleSearch = { },
134+ onSearchTextChange = { },
135+ rateSoundscape = { },
136+ routeFunctions = RouteFunctions (viewModel = null ),
137+ streetPreviewFunctions = StreetPreviewFunctions (viewModel = null ),
138+ modifier = Modifier .windowInsetsPadding(WindowInsets .safeDrawing)
139+ )
140+ }
141+ }
142+
143+ @Test
144+ fun homeScreenWithRoute (){
145+ val targetContext = InstrumentationRegistry .getInstrumentation().targetContext
146+
147+ val sharedPreferences = PreferenceManager .getDefaultSharedPreferences(targetContext)
148+ // Use accessible map
149+ sharedPreferences.edit().putBoolean(ACCESSIBLE_MAP_KEY , true ).apply ()
150+
151+ val routePlayerState = RoutePlayerState (
152+ routeData = routeToShops,
153+ currentWaypoint = 0
154+ )
155+
156+ runScreenTest(" homeScreenWithRoute" ) {
157+ Home (
158+ state = HomeState (
159+ location = location,
160+ heading = 45.0F ,
161+ currentRouteData = routePlayerState
162+ ),
163+ onNavigate = {},
164+ preferences = sharedPreferences,
165+ onMapLongClick = { false },
166+ bottomButtonFunctions = BottomButtonFunctions (null ),
167+ getCurrentLocationDescription = {
168+ LocationDescription (
169+ name = " Milngavie" ,
170+ location = location
171+ )
172+ },
173+ searchText = " " ,
174+ onToggleSearch = { },
175+ onSearchTextChange = { },
176+ rateSoundscape = { },
177+ routeFunctions = RouteFunctions (viewModel = null ),
178+ streetPreviewFunctions = StreetPreviewFunctions (viewModel = null ),
179+ modifier = Modifier .windowInsetsPadding(WindowInsets .safeDrawing)
180+ )
181+ }
182+ }
183+
184+ @Test
185+ fun routeDetailsScreen (){
186+ val targetContext = InstrumentationRegistry .getInstrumentation().targetContext
187+
188+ runScreenTest(" routeDetails" ) {
189+ RouteDetailsScreen (
190+ navController = NavController (targetContext),
191+ routeId = 1 ,
192+ modifier = Modifier .windowInsetsPadding(WindowInsets .safeDrawing),
193+ uiState = RouteDetailsUiState (
194+ route = routeToShops
195+ ),
196+ routePlayerState = RoutePlayerState (
197+ null ,
198+ 0
199+ ),
200+ getRouteById = { },
201+ startRoute = { },
202+ stopRoute = { },
203+ shareRoute = { },
204+ clearErrorMessage = { },
205+ userLocation = location,
206+ heading = 45.0F
207+ )
208+ }
209+ }
210+ @Test
211+ fun editScreen () {
212+ val targetContext = InstrumentationRegistry .getInstrumentation().targetContext
213+
214+ val route = routeToShops
215+ val members: MutableList <LocationDescription > =
216+ emptyList<LocationDescription >().toMutableList()
217+ for ((index, marker) in route.markers.withIndex()) {
218+ members.add(
219+ LocationDescription (
220+ name = marker.name,
221+ location = LngLatAlt (marker.longitude, marker.latitude),
222+ orderId = index.toLong(),
223+ )
224+ )
225+ }
226+
227+ val uiState = AddAndEditRouteUiState (
228+ name = " To shops" ,
229+ description = " Route to shops" ,
230+ routeMembers = members,
231+ )
232+
233+ runScreenTest(" routeEdit" ) {
234+ AddAndEditRouteScreen (
235+ navController = NavController (targetContext),
236+ modifier = Modifier .windowInsetsPadding(WindowInsets .safeDrawing),
237+ uiState = uiState,
238+ userLocation = location,
239+ onClearErrorMessage = { },
240+ onResetDoneAction = { },
241+ onNameChange = { },
242+ onDescriptionChange = { },
243+ onDeleteRoute = { },
244+ onEditComplete = { },
245+ onClickFolder = { _, _ -> },
246+ onClickBack = { },
247+ onSelectLocation = { },
248+ createAndAddMarker = { _, _, _ -> },
249+ editRoute = true ,
250+ routeObjectId = 1 ,
251+ placesNearbyUiState = PlacesNearbyUiState ()
252+ )
253+ }
254+ }
255+ }
0 commit comments