5

So, I have problems with testing my Google Map functionality. In my activty I have callback onMapClick(LatLng latlng) in which there is method to show/hide toolbar. Now, I would like to test it, but I have no clue how to perform click on the map. I tried to use this:

onView(withContentDescription("Mapa Google")).perform(click());

and this:

UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject map = device.findObject(new UiSelector()
        .descriptionContains("Mapa Google"));
map.click();

but it seams that, it doesn't work. Do you know how can I test this kind of behavior?

Panczur
  • 633
  • 1
  • 9
  • 26
  • 2
    Possible duplicate of: [Using Espresso to Unit Test Google Maps](https://stackoverflow.com/questions/29924564/using-espresso-to-unit-test-google-maps) – jeprubio May 16 '21 at 11:08

3 Answers3

5

The best way is UiAutomator library , https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html

private UiDevice uiDevice = UiDevice.getInstance(getInstrumentation());
private UiObject mMarker1 = uiDevice.findObject(new UiSelector().descriptionContains("The title of marker"));
try {
    mMarker1.click();
} catch (UiObjectNotFoundException e) {

    e.printStackTrace();
}

for taping on map you can use this

uiDevice.click(x, y);

x and y are the coordinates

Ara Hakobyan
  • 1,682
  • 1
  • 10
  • 21
  • I hope it'll help you, if you have any question please ask me – Ara Hakobyan May 12 '17 at 14:17
  • Allright, so after about 2 hours I finally manage to do what I want. Thanks for help, I use uiDevice.click(x, y); to tap on map and it works :D But for some reasons I have to call Thread.sleep(4000); after that click, becouse otherwise my tests falls with error that toolbar is visible. I make my tollbar invisible by calling setVisibility(View.GONE); in onMapClick() callback. Also I have disabled all animations like it says [here](https://product.reverb.com/disabling-animations-in-espresso-for-android-testing-de17f7cf236f) Is it normal that after uiDevice.click(x, y); I have to wait? – Panczur May 12 '17 at 15:36
  • 1
    It's normal for Espresso library :D try Xamarin Ui Testing it is more comfortable – Ara Hakobyan May 15 '17 at 08:10
0

Google Map Android version 2, Normally we use it with fragment. From your map fragment add map sync listener to the method "getMapAsync" then when "onMapReady" is call then set map click callback there

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
    public void onMapClick(LatLng point){
        // do something here
    }
});

See the document api for detail use https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#setOnMapClickListener(com.google.android.gms.maps.GoogleMap.OnMapClickListener)

vsatkh
  • 179
  • 2
  • 10
  • 1
    Yeah, I know how it works ;) I'm not tring to implement OnMapClickListener. I already did that. I want to do some android instrumented tests [link](https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html), so I need to somehow perform autometed click on map to check, that after that click, my toolbar will hide – Panczur May 12 '17 at 14:03
  • As the answer of the follow link http://stackoverflow.com/questions/29924564/using-espresso-to-unit-test-google-maps "onView(withContentDescription("Google Map")).perform(click());" should work find with Espresso – vsatkh May 12 '17 at 14:28
0

From my knowledge and research you cannot simulate directly a map click, or marker click for that matter. If you really want to achieve this you could try loading your map in a webview and simulate a click in javascript through the MapsEvents API. At least for markers... (I know its not necessarily what your looking for but the block you're trying to test can be moved from OnMapClickListener to OnMarkerClickListener. If it works there, there is no reason why it wouldn't work in the new callback)

If I find a better working solution I'll update my answer.

//In V2 version:

event.trigger(mapMarker[i], 'click');

//In V3 version:

google.maps.event.trigger(mapMarker[i], 'click');

Alex T
  • 163
  • 1
  • 9