Cómo probar tu app en dispositivos plegables

Los dispositivos plegables tienen funciones y capacidades únicas que requieren pruebas especializadas. Prueba tu app en plegables grandes y pequeños, con el dispositivo plegado y desplegado, en orientación vertical y horizontal, en posiciones de mesa y de libro, y en el modo multiventana. Si deseas obtener más información, consulta los lineamientos de calidad de apps para pantallas grandes.

FoldingFeature

La biblioteca de Jetpack WindowManager notifica a tu app cuando cambia la posición de un dispositivo plegable para que puedas el diseño de la app.

El artefacto window-testing incluye la regla WindowLayoutInfoPublisherRule de JUnit4 que te permite publicar un WindowInfoLayout personalizado a fin de simular un objeto FoldingFeature en las pruebas.

Si deseas probar el estado de un atributo de plegado, primero define una clase de prueba y las reglas de prueba:

Kotlin

import androidx.window.layout.FoldingFeature.Orientation.Companion.HORIZONTAL
import androidx.window.layout.FoldingFeature.Orientation.Companion.VERTICAL
import androidx.window.layout.FoldingFeature.State.Companion.FLAT
import androidx.window.layout.FoldingFeature.State.Companion.HALF_OPENED
import androidx.window.testing.layout.FoldingFeature
import androidx.window.testing.layout.TestWindowLayoutInfo
import androidx.window.testing.layout.WindowLayoutInfoPublisherRule

@RunWith(AndroidJUnit4::class)
class DisplayFeaturesActivityTest {
    private val activityRule = ActivityScenarioRule(DisplayFeaturesActivity::class.java)
    private val publisherRule = WindowLayoutInfoPublisherRule()

    @get:Rule
    val testRule: TestRule

    init {
        testRule = RuleChain.outerRule(publisherRule).around(activityRule)
    }

    @Test myTest() {
       // TODO
    }
}

Java

import static androidx.window.layout.FoldingFeature.Orientation.HORIZONTAL;
import static androidx.window.layout.FoldingFeature.Orientation.VERTICAL;
import static androidx.window.layout.FoldingFeature.State.FLAT;
import static androidx.window.layout.FoldingFeature.State.HALF_OPENED;
import static androidx.window.testing.layout.DisplayFeatureTesting.createFoldingFeature;
import static androidx.window.testing.layout.WindowLayoutInfoTesting.createWindowLayoutInfo;
import androidx.window.layout.FoldingFeature;
import androidx.window.layout.WindowLayoutInfo;
import androidx.window.testing.layout.WindowLayoutInfoPublisherRule;

@RunWith(AndroidJUnit4.class)
public class DisplayFeaturesActivityJavaTest {
    private WindowLayoutInfoPublisherRule publisherRule = new WindowLayoutInfoPublisherRule();

    @Rule public TestRule testRule;

    public DisplayFeaturesActivityJavaTest() {
        testRule = RuleChain.outerRule(publisherRule).around(activityRule);
    };

     @Test
     public void myTest() {
         // TODO
     }
}

A continuación, puedes simular un atributo de plegado, como una pantalla plegable semiabierta con un pliegue horizontal centrado de ancho nulo:

Kotlin

val feature = FoldingFeature(
                  activity = activity,
                  state = HALF_OPENED,
                  orientation = HORIZONTAL)

val expected = TestWindowLayoutInfo(listOf(feature))

Java

FoldingFeature feature = createFoldingFeature(
                             activity,
                             -1,
                             0,
                             HALF_OPENED,
                             HORIZONTAL);

WindowLayoutInfo expected = createWindowLayoutInfo(
                                Collections.singletonList(feature)
                            );

Luego, usa la regla WindowLayoutInfoPublisherRule para publicar la WindowLayoutInfo personalizada:

Kotlin

@Test
myTest() {
    ...
    publisherRule.overrideWindowLayoutInfo(expected)
    ...
}

Java

@Test
public void myTest() {
    ...
    publisherRule.overrideWindowLayoutInfo(expected);
    ...
}

Por último, comprueba si el diseño de la actividad se comporta como se espera con los comparadores de Espresso disponibles.

En el siguiente ejemplo, se simula un objeto FoldingFeature con una bisagra vertical en estado HALF_OPENED en el centro de la pantalla y, luego, se usa un comparador para verificar que el diseño sea el esperado:

Kotlin

@Test
fun testDeviceOpen_Vertical() {
    activityRule.scenario.onActivity { activity ->
        val feature = FoldingFeature(
                          activity = activity,
                          state = HALF_OPENED,
                          orientation = VERTICAL)
        val expected = TestWindowLayoutInfo(listOf(feature))
        publisherRule.overrideWindowLayoutInfo(expected)
    }

    // Checks that start_layout is on the left of end_layout with a vertical folding feature.
    onView(withId(R.id.start_layout))
        .check(isCompletelyLeftOf(withId(R.id.end_layout)))
}

Java

@Test
public void testDeviceOpen_Vertical() {
    activityRule
        .getScenario()
        .onActivity(
            activity -> {
                FoldingFeature feature = createFoldingFeature(
                                             activity,
                                             -1,
                                             0,
                                             HALF_OPENED,
                                             VERTICAL);

                WindowLayoutInfo expected = createWindowLayoutInfo(
                                                Collections.singletonList(feature)
                                            );

                publisherRule.overrideWindowLayoutInfo(expected);
            });

    // Checks that start_layout is on the left of end_layout with a vertical folding feature.
    onView(withId(R.id.start_layout))
        .check(isCompletelyLeftOf(withId(R.id.end_layout)));
}

Cambios de configuración

Si tu app controla cambios de configuración de manera programática con el método de devolución de llamada onConfigurationChanged(), verifica que responda de inmediato a los cambios de configuración, en especial, a la rotación del dispositivo entre las orientaciones vertical y horizontal.

Para asegurarte de que tu app reciba notificaciones sobre cambios de orientación y tamaño de visualización, especifica los siguientes parámetros de configuración en el elemento activity:configChanges del manifiesto:

android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"

Recursos adicionales