简体   繁体   中英

Android Instrumentation Test for multiple screen sizes / layouts

I have some screen that are split-screen on large screens, but single screen in small screens, as described here: http://developer.android.com/training/basics/fragments/fragment-ui.html

I am attempting to write a test case that runs all the functions of my application, and I have mocked all the network calls, and so forth.

The only remaining question I know I have is if there is a proper way to test the multiple layouts.

Right now, I would need to manually run the test case on various AVD with the configurations I want to test, and I have calls in this format:

if( uiDevice.getDisplaySizeDp().x < 600) {
    // we are using the standard layout, so the fragment was opened on top of the stack, instead of side-by-side
    // press Back to get back to the list of objects
    pressBack();
}

I am running Espresso tests with AndroidJUnit4 and android.support.test.runner.AndroidJUnitRunner .

The question is: Is there a standard/documented method I can share with my team to handle the different layout qualifiers: sw600dp , w900dp , landscape , and so forth.

Alternatively is there a way to specify which test cases would be run for a device that matches the qualifiers?

Update based on drfrag01 answer:

I think I'm looking more for something that might be able to automatically pick which test cases to run, once the runner starts up on the device. MY best scenario would probably be something where I add annotations @SW600 or @Normal, and then when the test are running on the device, the @SW600 are skipped for a small phone, instead of me setting up all the suites.

It look like this may not be possible, without a custom Test Runner.

I use the following -

/**
 * Determine if the device is a tablet (i.e. it has a large screen).
 *
 * @param context The calling context.
 */

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}


/**
 * Determine if the device is in landscape or portrait mode. Returns true for portrait, and false
 * for landscape.
 */
public static boolean isPortrait(Context context) {
    return context.getResources().getConfiguration().screenHeightDp > context.getResources().getConfiguration().screenWidthDp;
}

You can filter test cases that need to run by

  1. Organizing them in Suites and running the suites.

For Example

./gradlew -Pandroid.testInstrumentationRunnerArguments.class=com.mycompany.foo.test.Suites.PortraitFriendlyTestSuite connectedAndroidTest --info

  1. Annotating them with for example "@TabletTest" or "@PortraitOnly" and filtering them in a test run.

For Example

./gradlew -Pandroid.testInstrumentationRunnerArguments.annotation=com.mycompany.foo.Annotations.PortraitOnly connectedAndroidTest --info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM