[android] - add integration test suite

This commit is contained in:
tobrun
2019-03-15 13:40:28 +01:00
committed by Tobrun
parent 5ea450b14b
commit 76df7d9cee
16 changed files with 378 additions and 306 deletions

View File

@@ -0,0 +1,60 @@
package com.mapbox.mapboxsdk.integration
import android.content.Intent
import android.support.test.InstrumentationRegistry
import android.support.test.uiautomator.*
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.junit.Before
const val LAUNCH_TIMEOUT = 2500L
private const val TEST_APP_PACKAGE = "com.mapbox.mapboxsdk.testapp"
abstract class BaseIntegrationTest {
protected lateinit var device: UiDevice
@Before
open fun beforeTest() {
// Initialize UiDevice instance
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
// Start from the home screen
device.pressHome()
// Wait for launcher
val launcherPackage: String = device.launcherPackageName
MatcherAssert.assertThat(launcherPackage, CoreMatchers.notNullValue())
device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT)
// Launch the app
val context = InstrumentationRegistry.getInstrumentation().context
val intent = context.packageManager.getLaunchIntentForPackage(TEST_APP_PACKAGE).apply {
// Clear out any previous instances
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
context.startActivity(intent)
// Wait for the app to appear
device.wait(
Until.hasObject(By.pkg(TEST_APP_PACKAGE).depth(0)),
LAUNCH_TIMEOUT
)
}
fun openFeature(featureName: String) {
scrollRecyclerViewTo(featureName)
device.findObject(UiSelector().text(featureName)).clickAndWaitForNewWindow()
device.waitForIdle(LAUNCH_TIMEOUT)
}
fun scrollRecyclerViewTo(recycleItem: String) {
val appView = UiScrollable(UiSelector().scrollable(true))
appView.scrollIntoView(UiSelector().text(recycleItem))
}
fun pressHomeReturnWithRecentApps(){
device.pressRecentApps()
device.findObject(UiSelector().text(InstrumentationRegistry.getTargetContext().getString(InstrumentationRegistry.getTargetContext().applicationInfo.labelRes))).click()
}
}

View File

@@ -0,0 +1,59 @@
package com.mapbox.mapboxsdk.integration
import android.support.test.filters.LargeTest
import android.support.test.runner.AndroidJUnit4
import android.support.test.uiautomator.UiSelector
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
/**
* Regression test that validates MapFragment integration on the backstack
*/
@RunWith(AndroidJUnit4::class)
class FragmentBackStackTest : BaseIntegrationTest() {
@Before
override fun beforeTest() {
super.beforeTest()
openFeature("Backstack Map Fragment")
}
@Test
@LargeTest
fun backPressedOnBackStack() {
device.waitForIdle()
clickReplaceFragmentButton()
device.waitForIdle()
backPressBackStack()
}
@Test
@LargeTest
fun backPressedOnBackStackResumed(){
device.waitForIdle()
clickReplaceFragmentButton()
device.waitForIdle()
pressHomeReturnWithRecentApps()
device.waitForIdle()
backPressBackStack()
}
@Test
@LargeTest
fun finishOnBackStack() {
device.waitForIdle()
clickReplaceFragmentButton()
device.waitForIdle()
device.findObject(UiSelector().description("Navigate up")).click()
}
private fun clickReplaceFragmentButton(){
device.findObject(UiSelector().text("REPLACE WITH EMPTY FRAGMENT")).click()
}
private fun backPressBackStack(){
device.pressBack() // pops fragment, showing map
device.pressBack() // finish activity
}
}

View File

@@ -0,0 +1,27 @@
package com.mapbox.mapboxsdk.integration
import android.support.test.filters.LargeTest
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
/**
* Regression test that validates reopening an Activity with a GLSurfaceView
*/
@RunWith(AndroidJUnit4::class)
class GLSurfaceViewReopenTest : BaseIntegrationTest() {
@Before
override fun beforeTest() {
super.beforeTest()
openFeature("Simple Map")
}
@Test
@LargeTest
fun reopenSimpleMapActivity() {
pressHomeReturnWithRecentApps()
device.waitForIdle()
}
}

View File

@@ -0,0 +1,30 @@
package com.mapbox.mapboxsdk.integration
import android.support.test.filters.LargeTest
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
/**
* Regression test that validates if a GLSurfaceView surface can be recreated without crashing.
*/
@RunWith(AndroidJUnit4::class)
class GLSurfaceViewReuseTest : BaseIntegrationTest() {
@Before
override fun beforeTest() {
super.beforeTest()
openFeature("RecyclerView GLSurfaceView")
}
@Test
@LargeTest
fun scrollRecylerView() {
device.waitForIdle()
scrollRecyclerViewTo("Twenty-one")
device.waitForIdle()
scrollRecyclerViewTo("One")
device.waitForIdle()
}
}

View File

@@ -0,0 +1,29 @@
package com.mapbox.mapboxsdk.integration
import android.support.test.filters.LargeTest
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class OrientationChangeTest : BaseIntegrationTest() {
@Before
override fun beforeTest() {
super.beforeTest()
openFeature("Simple Map")
}
@Test
@LargeTest
fun rotateSimpleMap() {
device.setOrientationLeft()
device.waitForIdle()
device.setOrientationNatural()
device.waitForIdle()
device.setOrientationRight()
device.waitForIdle()
device.setOrientationNatural()
}
}

View File

@@ -0,0 +1,29 @@
package com.mapbox.mapboxsdk.integration
import android.support.test.filters.LargeTest
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.Thread.sleep
/**
* Regression test that validates reopening an Activity with a TextureView
*/
@RunWith(AndroidJUnit4::class)
class TextureViewReopenTest : BaseIntegrationTest() {
@Before
override fun beforeTest() {
super.beforeTest()
openFeature("TextureView debug")
}
@Test
@LargeTest
fun reopenTextureViewDebugActivity() {
pressHomeReturnWithRecentApps()
device.waitForIdle()
sleep(LAUNCH_TIMEOUT)
}
}

View File

@@ -0,0 +1,29 @@
package com.mapbox.mapboxsdk.integration
import android.support.test.filters.LargeTest
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
/**
* Regression test that validates if a GLSurfaceView surface can be recreated without crashing.
*/
@RunWith(AndroidJUnit4::class)
class TextureViewReuseTest : BaseIntegrationTest() {
@Before
override fun beforeTest() {
super.beforeTest()
openFeature("RecyclerView TextureView")
}
@Test
@LargeTest
fun scrollRecylerView() {
scrollRecyclerViewTo("Twenty-one")
device.waitForIdle()
scrollRecyclerViewTo("One")
device.waitForIdle()
}
}

View File

@@ -0,0 +1,37 @@
package com.mapbox.mapboxsdk.integration
import android.support.test.filters.LargeTest
import android.support.test.runner.AndroidJUnit4
import android.support.test.uiautomator.UiSelector
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
/**
* Regression test that validates MapFragment integration with a ViewPager
*/
@RunWith(AndroidJUnit4::class)
class ViewPagerScrollTest : BaseIntegrationTest() {
@Before
override fun beforeTest() {
super.beforeTest()
openFeature("ViewPager")
}
@Test
@LargeTest
fun scrollViewPager() {
for (i in 1..5) {
clickTab(i)
}
for (i in 4 downTo 0) {
clickTab(i)
}
}
private fun clickTab(index: Int) {
device.findObject(UiSelector().text("Page $index")).click()
}
}

View File

@@ -1,72 +0,0 @@
package com.mapbox.mapboxsdk.maps
import android.content.Intent
import android.support.test.InstrumentationRegistry
import android.support.test.InstrumentationRegistry.getTargetContext
import android.support.test.filters.SdkSuppress
import android.support.test.runner.AndroidJUnit4
import android.support.test.uiautomator.By
import android.support.test.uiautomator.UiDevice
import android.support.test.uiautomator.UiSelector
import android.support.test.uiautomator.Until
import org.hamcrest.CoreMatchers.notNullValue
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
private const val BASIC_SAMPLE_PACKAGE = "com.mapbox.mapboxsdk.testapp"
private const val LAUNCH_TIMEOUT = 5000L
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class GLSurfaceViewReopenTest {
private lateinit var device: UiDevice
@Before
fun startSimpleMapActivityFromHomeScreen() {
// Initialize UiDevice instance
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
// Start from the home screen
device.pressHome()
// Wait for launcher
val launcherPackage: String = device.launcherPackageName
assertThat(launcherPackage, notNullValue())
device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT)
// Launch the app
val context = InstrumentationRegistry.getInstrumentation().context
val intent = context.packageManager.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE).apply {
// Clear out any previous instances
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
context.startActivity(intent)
// Wait for the app to appear
device.wait(
Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT
)
// open SimpleMapActivity
device.findObject(UiSelector().text("Simple Map")).clickAndWaitForNewWindow()
// wait for idle
device.waitForIdle(LAUNCH_TIMEOUT)
}
@Test
fun reopenSimpleMapActivity() {
// press recent apps button to pause the app
device.pressRecentApps()
// return to app
device.findObject(UiSelector().text(getTargetContext().getString(getTargetContext().applicationInfo.labelRes))).click()
// wait for idle
device.waitForIdle(LAUNCH_TIMEOUT)
}
}

View File

@@ -1,41 +0,0 @@
package com.mapbox.mapboxsdk.maps;
import com.mapbox.mapboxsdk.testapp.action.OrientationAction;
import com.mapbox.mapboxsdk.testapp.action.WaitAction;
import com.mapbox.mapboxsdk.testapp.activity.BaseTest;
import com.mapbox.mapboxsdk.testapp.activity.camera.CameraAnimationTypeActivity;
import org.junit.Test;
import static com.mapbox.mapboxsdk.testapp.action.OrientationAction.orientationLandscape;
import static com.mapbox.mapboxsdk.testapp.action.OrientationAction.orientationLandscapeReverse;
import static com.mapbox.mapboxsdk.testapp.action.OrientationAction.orientationPortrait;
import static com.mapbox.mapboxsdk.testapp.action.OrientationAction.orientationPortraitReverse;
public class OrientationTest extends BaseTest {
@Test
public void testChangeDeviceOrientation() {
OrientationAction.invoke(orientationLandscape());
WaitAction.invoke(2200);
OrientationAction.invoke(orientationPortrait());
WaitAction.invoke(2500);
OrientationAction.invoke(orientationLandscapeReverse());
WaitAction.invoke(500);
OrientationAction.invoke(orientationPortraitReverse());
WaitAction.invoke(1250);
OrientationAction.invoke(orientationLandscape());
WaitAction.invoke(750);
OrientationAction.invoke(orientationPortrait());
WaitAction.invoke(950);
OrientationAction.invoke(orientationLandscapeReverse());
OrientationAction.invoke(orientationPortraitReverse());
OrientationAction.invoke(orientationLandscape());
OrientationAction.invoke(orientationPortrait());
}
@Override
protected Class getActivityClass() {
return CameraAnimationTypeActivity.class;
}
}

View File

@@ -1,70 +0,0 @@
package com.mapbox.mapboxsdk.maps
import android.content.Intent
import android.support.test.InstrumentationRegistry
import android.support.test.filters.SdkSuppress
import android.support.test.runner.AndroidJUnit4
import android.support.test.uiautomator.*
import org.hamcrest.CoreMatchers.notNullValue
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
private const val BASIC_SAMPLE_PACKAGE = "com.mapbox.mapboxsdk.testapp"
private const val LAUNCH_TIMEOUT = 5000L
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class TextureViewReopenTest {
private lateinit var device: UiDevice
@Before
fun startSimpleMapActivityFromHomeScreen() {
// Initialize UiDevice instance
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
// Start from the home screen
device.pressHome()
// Wait for launcher
val launcherPackage: String = device.launcherPackageName
assertThat(launcherPackage, notNullValue())
device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT)
// Launch the app
val context = InstrumentationRegistry.getInstrumentation().context
val intent = context.packageManager.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE).apply {
// Clear out any previous instances
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
context.startActivity(intent)
// Wait for the app to appear
device.wait(
Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT
)
// open TextureView debug activity
val appView = UiScrollable(UiSelector().scrollable(true))
appView.scrollIntoView(UiSelector().text("TextureView debug"))
device.findObject(UiSelector().text("TextureView debug")).clickAndWaitForNewWindow()
// wait for idle
device.waitForIdle(LAUNCH_TIMEOUT)
}
@Test
fun reopenTextureViewDebugActivity() {
// press recent apps button
device.pressRecentApps()
// return to app
device.findObject(UiSelector().text(InstrumentationRegistry.getTargetContext().getString(InstrumentationRegistry.getTargetContext().applicationInfo.labelRes))).click()
// wait for idle
device.waitForIdle(LAUNCH_TIMEOUT)
}
}

View File

@@ -176,7 +176,7 @@
android:label="@string/activity_map_fragment_backstack">
<meta-data
android:name="@string/category"
android:value="@string/category_fragment" />
android:value="@string/category_integration" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
@@ -399,7 +399,7 @@
android:label="@string/activity_viewpager">
<meta-data
android:name="@string/category"
android:value="@string/category_fragment" />
android:value="@string/category_integration" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
@@ -900,7 +900,7 @@
android:label="@string/activity_recyclerview_textureview">
<meta-data
android:name="@string/category"
android:value="@string/category_maplayout" />
android:value="@string/category_integration" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
@@ -911,7 +911,7 @@
android:label="@string/activity_recyclerview_glsurfaceview">
<meta-data
android:name="@string/category"
android:value="@string/category_maplayout" />
android:value="@string/category_integration" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
@@ -922,7 +922,7 @@
android:label="@string/activity_nested_viewpager">
<meta-data
android:name="@string/category"
android:value="@string/category_fragment" />
android:value="@string/category_integration" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />

View File

@@ -1,117 +0,0 @@
package com.mapbox.mapboxsdk.testapp.activity.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import com.mapbox.mapboxsdk.testapp.R;
/**
* Test activity showcasing using the Android SDK ViewPager API to show MapFragments.
*/
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager viewPager;
private MapFragmentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
adapter = new MapFragmentAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int currentPosition = viewPager.getCurrentItem();
SupportMapFragment mapFragment;
if (Math.abs(0 - currentPosition) <= 1) {
mapFragment = (SupportMapFragment) adapter.instantiateItem(viewPager, 0);
mapFragment.getMapAsync(mapboxMap -> {
mapboxMap.setStyle(Style.MAPBOX_STREETS);
});
}
if (Math.abs(1 - currentPosition) <= 1) {
mapFragment = (SupportMapFragment) adapter.instantiateItem(viewPager, 1);
mapFragment.getMapAsync(mapboxMap -> {
mapboxMap.setStyle(Style.DARK);
});
}
if (Math.abs(2 - currentPosition) <= 1) {
mapFragment = (SupportMapFragment) adapter.instantiateItem(viewPager, 2);
mapFragment.getMapAsync(mapboxMap -> {
mapboxMap.setStyle(Style.SATELLITE);
});
}
}
static class MapFragmentAdapter extends FragmentStatePagerAdapter {
private static int NUM_ITEMS = 3;
MapFragmentAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
SupportMapFragment fragment = null;
MapboxMapOptions options = new MapboxMapOptions();
options.textureMode(true);
switch (position) {
case 0:
options.camera(new CameraPosition.Builder().target(new LatLng(34.920526, 102.634774)).zoom(3).build());
fragment = SupportMapFragment.newInstance(options);
fragment.getMapAsync(mapboxMap -> {
mapboxMap.setStyle(Style.MAPBOX_STREETS);
});
break;
case 1:
options.camera(new CameraPosition.Builder().target(new LatLng(62.326440, 92.764913)).zoom(3).build());
fragment = SupportMapFragment.newInstance(options);
fragment.getMapAsync(mapboxMap -> {
mapboxMap.setStyle(Style.DARK);
});
break;
case 2:
options.camera(new CameraPosition.Builder().target(new LatLng(-25.007786, 133.623852)).zoom(3).build());
fragment = SupportMapFragment.newInstance(options);
fragment.getMapAsync(mapboxMap -> {
mapboxMap.setStyle(Style.SATELLITE);
});
break;
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
}

View File

@@ -0,0 +1,71 @@
package com.mapbox.mapboxsdk.testapp.activity.fragment
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v7.app.AppCompatActivity
import com.mapbox.mapboxsdk.camera.CameraPosition
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.maps.MapboxMapOptions
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.maps.SupportMapFragment
import com.mapbox.mapboxsdk.testapp.R
import kotlinx.android.synthetic.main.activity_viewpager.*
/**
* Test activity showcasing using the Android SDK ViewPager API to show MapFragments.
*/
class ViewPagerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_viewpager)
viewPager.adapter = MapFragmentAdapter(supportFragmentManager)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
val currentPosition = viewPager!!.currentItem
for (i in 0 until FRAGMENT_COUNT) {
if (Math.abs(i - currentPosition) <= 1) {
viewPager.adapter?.instantiateItem(viewPager, 0)
break
}
}
}
internal class MapFragmentAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) {
override fun getCount(): Int {
return FRAGMENT_COUNT
}
override fun getItem(position: Int): Fragment {
return when (position) {
0 -> createFragment(Style.MAPBOX_STREETS, LatLng(34.920526, 102.634774))
1 -> createFragment(Style.DARK, LatLng(62.326440, 92.764913))
2 -> createFragment(Style.SATELLITE, LatLng(-25.007786, 133.623852))
3 -> createFragment(Style.LIGHT, LatLng(-19.922070, -43.939676))
4 -> createFragment(Style.OUTDOORS, LatLng(46.881020, 9.098098))
else -> createFragment(Style.TRAFFIC_DAY, LatLng(40.722517, -74.000029))
}
}
private fun createFragment(@Style.StyleUrl style: String, latLng: LatLng): Fragment {
val options = MapboxMapOptions().camera(CameraPosition.Builder().target(latLng).zoom(3.0).build())
val fragment = SupportMapFragment.newInstance(options)
fragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(style) }
return fragment
}
override fun getPageTitle(position: Int): CharSequence? {
return "Page $position"
}
}
companion object {
const val FRAGMENT_COUNT = 6
}
}

View File

@@ -5,7 +5,7 @@
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.testapp.view.MapViewPager
android:id="@+id/viewpager"
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">

View File

@@ -15,4 +15,5 @@
<string name="category_storage">Storage</string>
<string name="category_textureview">Texture View</string>
<string name="category_location">Location</string>
<string name="category_integration">_Integration</string>
</resources>