Tester votre application sur des appareils pliables

Les appareils pliables présentent des fonctionnalités et capacités uniques qui nécessitent des tests spécialisés. Testez votre application sur des écrans pliables petits et grands, en mode plié et déplié, en orientation portrait et paysage, en position à plat et livre, et en mode multifenêtre. Pour en savoir plus, consultez les consignes relatives à la qualité des applications sur grand écran.

FoldingFeature

La bibliothèque Jetpack WindowManager informe votre application lorsque la position d'un appareil pliable a changé. Vous pouvez ainsi modifier la mise en page de l'application.

L'artefact window-testing inclut la règle JUnit4 WindowLayoutInfoPublisherRule qui vous permet de publier une WindowInfoLayout personnalisée pour simuler une FoldingFeature dans les tests.

Pour vérifier l'état d'une fonctionnalité de pliage, commencez par définir une classe de test et les règles du test :

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
     }
}

Vous pouvez ensuite simuler une fonctionnalité de pliage, comme un écran pliable à moitié ouvert avec un pli horizontal centré de largeur zéro :

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)
                            );

Ensuite, utilisez WindowLayoutInfoPublisherRule pour publier l'élément WindowLayoutInfo personnalisé :

Kotlin

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

Java

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

Enfin, vérifiez si la mise en page de l'activité se comporte comme prévu à l'aide des outils de mise en correspondance Espresso disponibles.

L'exemple suivant simule une FoldingFeature avec une charnière verticale HALF_OPENED au centre de l'écran, puis utilise une mise en correspondance pour vérifier si la mise en page est bien celle attendue :

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)));
}

Modifications de la configuration

Si votre application gère de manière automatisée les modifications de configuration avec la méthode de rappel onConfigurationChanged(), vérifiez qu'elle réagit rapidement aux changements de configuration, en particulier la rotation de l'appareil en mode portrait ou paysage.

Pour vous assurer que votre application est informée des changements d'orientation et de taille d'écran, spécifiez les paramètres de configuration suivants dans l'élément manifeste activity:configChanges :

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

Ressources supplémentaires