Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Espresso to Unit Test Google Maps

I am using Espresso to do some UI testing on my app. I have a fragment with a map, and I display some items on it that I get through a call to my backend.

When I click on a marker, I'm doing some UI things

Is there any way I can do unit testing on my map with espresso ?

like image 798
user2230304 Avatar asked Apr 28 '15 16:04

user2230304


People also ask

Does espresso support test recording?

The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app.

Is Espresso unit test?

Use Espresso to write concise, beautiful, and reliable Android UI tests. The core API is small, predictable, and easy to learn and yet remains open for customization.

What is Espresso UI Test?

Espresso is an open source android user interface (UI) testing framework developed by Google. The term Espresso is of Italian origin, meaning Coffee. Espresso is a simple, efficient and flexible testing framework.


1 Answers

Short Answer: With espresso is not really possible. A solution might be to use UIAutomator: https://developer.android.com/tools/testing-support-library/index.html#UIAutomator https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html

So you need to:

1) add gradle dependencies:

dependencies { androidTestCompile 'com.android.support.test:runner:0.2' androidTestCompile 'com.android.support.test:rules:0.2' androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' } 

2) make sure you add at least title to your markers even if you're not using it.

3) Write the test, code is smth like this:

UiDevice device = UiDevice.getInstance(getInstrumentation()); UiObject marker = device.findObject(new UiSelector().descriptionContains("marker title")); marker.click(); 

Explanation:

GoogleMap generates UI and also makes it accessible i.e. map content can be seen as a tree of accessibility node info.

This is tree is a virtual view tree, it does not represent the real view tree. We will come to this later

By default the contentDescription of the map is "Google Map" and that of markers is "{markerTitle}. {markerSnippet}".

Then the question is so why not using espresso:

onView(withContentDescription("marker title. ")).perform(click()); ?

Because it won't find it, however :

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

will work just fine.

So how come UIAutomator works and Espresso doesn't?

Because they use different view trees.

UIAutomator uses tree of accessibility node info provided by AccessibilityService, while Espresso uses the view hierarchy and thus processes all children of any ViewGroup. The accessibility node info and the view hierarchy may or may not map one-to-one. In this case

onView(withContentDescription("Google Map"))

finds not a ViewGroup but a TextureView, which is not known to have children so Espresso cannot know what is drawn in there.

Voila! :)

like image 86
kalin Avatar answered Oct 13 '22 20:10

kalin