11package com.mapcode.map
22
33import android.Manifest
4+ import androidx.compose.ui.semantics.SemanticsProperties
45import androidx.compose.ui.semantics.SemanticsPropertyKey
56import androidx.compose.ui.test.*
67import androidx.compose.ui.test.junit4.createComposeRule
8+ import androidx.compose.ui.text.TextRange
79import androidx.test.rule.GrantPermissionRule
810import assertk.assertThat
911import assertk.assertions.isEqualTo
@@ -50,7 +52,7 @@ class MapScreenTest {
5052
5153 composeTestRule.onNodeWithText(" Mapcode" ).performClick()
5254
53- assertThat(useCase.clipboard).isEqualTo(" AAA AB.XY" )
55+ assertThat(useCase.clipboard).isEqualTo(" AB.XY" )
5456 }
5557
5658 @Test
@@ -64,13 +66,13 @@ class MapScreenTest {
6466 setMapScreenAsContent()
6567 viewModel.onCameraMoved(0.0 , 0.0 , 0f )
6668
67- composeTestRule.onNodeWithText(" AAA AB.XY" ).performClick()
69+ composeTestRule.onNodeWithText(" AB.XY" ).performClick()
6870
69- assertThat(useCase.clipboard).isEqualTo(" AAA AB.XY" )
71+ assertThat(useCase.clipboard).isEqualTo(" AB.XY" )
7072 }
7173
7274 @Test
73- fun show_snack_bar_when_copying_mapcode () {
75+ fun show_snackbar_when_copying_mapcode () {
7476 useCase.knownLocations.add(
7577 FakeLocation (
7678 0.0 , 0.0 , addresses = emptyList(), mapcodes = listOf (Mapcode (" AB.XY" , Territory .AAA ))
@@ -81,7 +83,7 @@ class MapScreenTest {
8183
8284 setMapScreenAsContent()
8385
84- composeTestRule.onNodeWithText(" AAA AB.XY" ).performClick()
86+ composeTestRule.onNodeWithText(" AB.XY" ).performClick()
8587
8688 composeTestRule.waitForIdle()
8789
@@ -356,7 +358,7 @@ class MapScreenTest {
356358 }
357359
358360 @Test
359- fun show_snack_bar_if_fail_to_get_current_location () {
361+ fun show_snackbar_if_fail_to_get_current_location () {
360362 useCase.currentLocation = null
361363 setMapScreenAsContent()
362364
@@ -388,7 +390,7 @@ class MapScreenTest {
388390
389391 composeTestRule.onNodeWithContentDescription(" Share mapcode" ).performClick()
390392
391- assertThat(useCase.sharedText).isEqualTo(" AAA AB.XY" )
393+ assertThat(useCase.sharedText).isEqualTo(" AB.XY" )
392394 }
393395
394396 @Test
@@ -491,6 +493,136 @@ class MapScreenTest {
491493 composeTestRule.onNodeWithText(" Must be a number!" ).assertDoesNotExist()
492494 }
493495
496+ @Test
497+ fun hide_dropdown_if_no_matching_addresses () {
498+ useCase.matchingAddresses[" address" ] = emptyList()
499+
500+ setMapScreenAsContent()
501+ composeTestRule.onNodeWithText(" Enter address or mapcode" ).apply {
502+ performTextClearance()
503+ performTextInput(" address" )
504+ }
505+
506+ composeTestRule.waitForIdle()
507+ composeTestRule.onNodeWithTag(" address_dropdown" ).assertDoesNotExist()
508+ }
509+
510+ @Test
511+ fun show_matching_addresses_in_dropdown_when_typing () {
512+ useCase.matchingAddresses[" address" ] = listOf (" Street 1" , " Street 2" )
513+
514+ setMapScreenAsContent()
515+ composeTestRule.onNodeWithText(" Enter address or mapcode" ).apply {
516+ performTextClearance()
517+ performTextInput(" address" )
518+ }
519+
520+ composeTestRule.waitForIdle()
521+ composeTestRule.onNodeWithTag(" address_dropdown" ).assertIsDisplayed()
522+ composeTestRule.onNodeWithText(" Street 1" ).assertIsDisplayed()
523+ composeTestRule.onNodeWithText(" Street 2" ).assertIsDisplayed()
524+ }
525+
526+ @Test
527+ fun hide_dropdown_if_not_typing_address () {
528+ setMapScreenAsContent()
529+
530+ composeTestRule.onNodeWithText(" Enter address or mapcode" )
531+
532+ composeTestRule.waitForIdle()
533+ composeTestRule.onNodeWithTag(" address_dropdown" ).assertDoesNotExist()
534+ }
535+
536+ @Test
537+ fun clear_address_focus_when_submitting_address_query () {
538+ useCase.knownLocations.add(FakeLocation (1.0 , 1.0 , addresses = listOf (" address" ), mapcodes = emptyList()))
539+ setMapScreenAsContent()
540+
541+ composeTestRule.onNodeWithText(" Enter address or mapcode" ).apply {
542+ performTextClearance()
543+ performTextInput(" address" )
544+ performImeAction()
545+ }
546+
547+ composeTestRule.waitForIdle()
548+ composeTestRule.onNodeWithText(" Enter address or mapcode" ).assertIsNotFocused()
549+ }
550+
551+ @Test
552+ fun clear_address_focus_when_choosing_address_in_dropdown () {
553+ useCase.matchingAddresses[" address" ] = listOf (" Street 1" )
554+
555+ setMapScreenAsContent()
556+
557+ composeTestRule.onNodeWithText(" Enter address or mapcode" ).apply {
558+ performTextClearance()
559+ performTextInput(" address" )
560+ }
561+
562+ composeTestRule.waitForIdle()
563+ composeTestRule.onNodeWithText(" Street 1" ).performClick()
564+ composeTestRule.onNodeWithText(" Enter address or mapcode" ).assertIsNotFocused()
565+ }
566+
567+ @Test
568+ fun do_not_focus_address_when_opening_app () {
569+ setMapScreenAsContent()
570+ composeTestRule.waitForIdle()
571+ composeTestRule.onNodeWithText(" Enter address or mapcode" ).assertIsNotFocused()
572+ }
573+
574+ @Test
575+ fun select_latitude_text_when_focussing () {
576+ useCase.knownLocations.add(
577+ FakeLocation (0.0 , 0.0 , emptyList(), mapcodes = emptyList())
578+ )
579+ setMapScreenAsContent()
580+ viewModel.onCameraMoved(0.0 , 0.0 , 1f )
581+
582+ composeTestRule.onNodeWithText(" Latitude (Y)" ).apply {
583+ performClick()
584+ assert (SemanticsMatcher .expectValue(SemanticsProperties .TextSelectionRange , TextRange (0 , 9 )))
585+ }
586+ }
587+
588+ @Test
589+ fun select_longitude_text_when_focussing () {
590+ useCase.knownLocations.add(
591+ FakeLocation (0.0 , 0.0 , emptyList(), mapcodes = emptyList())
592+ )
593+ setMapScreenAsContent()
594+ viewModel.onCameraMoved(0.0 , 0.0 , 1f )
595+
596+ composeTestRule.onNodeWithText(" Longitude (X)" ).apply {
597+ performClick()
598+ assert (SemanticsMatcher .expectValue(SemanticsProperties .TextSelectionRange , TextRange (0 , 9 )))
599+ }
600+ }
601+
602+ @Test
603+ fun copy_location_to_clipboard_when_tapping_latitude_header () {
604+ setMapScreenAsContent()
605+ viewModel.onCameraMoved(1.0 , 2.0 , 1f )
606+
607+ composeTestRule.onNode(
608+ hasText(" Latitude (Y)" ).and (hasTestTag(" latlngtextfield" )),
609+ useUnmergedTree = true
610+ ).performClick()
611+ assertThat(useCase.clipboard).isEqualTo(" 1.0,2.0" )
612+ }
613+
614+ @Test
615+ fun copy_location_to_clipboard_when_tapping_longitude_header () {
616+ setMapScreenAsContent()
617+ viewModel.onCameraMoved(1.0 , 2.0 , 1f )
618+
619+ composeTestRule.onNode(
620+ hasText(" Longitude (X)" ).and (hasTestTag(" latlngtextfield" )),
621+ useUnmergedTree = true
622+ ).performClick()
623+ assertThat(useCase.clipboard).isEqualTo(" 1.0,2.0" )
624+ }
625+
494626 private fun setMapScreenAsContent () {
495627 composeTestRule.setContent {
496628 MapScreen (viewModel = viewModel, renderGoogleMaps = false )
0 commit comments