在折疊式裝置上測試應用程式

折疊式裝置具備獨特功能,需要執行特殊測試。在裝置折疊和展開、直向和橫向螢幕方向、桌面及書籍型態以及多視窗模式下,在小螢幕和大螢幕折疊式裝置上測試應用程式。詳情請參閱「大型螢幕應用程式品質」指南。

FoldingFeature

當折疊式裝置的型態發生變化時,Jetpack WindowManager 程式庫會通知應用程式,以便您修改應用程式的版面配置。

window-testing 構件包含 WindowLayoutInfoPublisherRule JUnit4 規則,可讓您發布自訂 WindowInfoLayout 來模擬測試中的 FoldingFeature

如要測試折疊功能的狀態,請先定義測試類別和測試規則:

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

接下來,您可以模擬折疊功能,例如半開啟的折疊式裝置螢幕(其居中的水平折疊寬度為零):

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

然後使用 WindowLayoutInfoPublisherRule 發布自訂 WindowLayoutInfo

Kotlin

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

Java

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

最後,使用提供的 Espresso 比對器檢查活動版面配置是否運作正常。

下面是模擬 FoldingFeature 的範例,其螢幕中心有一個 HALF_OPENED 垂直轉軸,然後使用比對器檢查該版面配置是否符合預期:

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

設定變更

如果您的應用程式使用 onConfigurationChanged() 回呼方法以程式輔助方式處理設定變更,請確認應用程式會立即回應設定變更,尤其是在直向和橫向螢幕方向下旋轉裝置時。

為確保向應用程式告知螢幕方向和顯示大小變更,請在 activity:configChanges 資訊清單元素中指定下列設定:

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

其他資源