Kiểm thử ứng dụng trên thiết bị có thể gập lại

Thiết bị có thể gập lại có nhiều tính năng và chức năng độc đáo, cần kiểm thử theo cách chuyên biệt. Hãy kiểm thử ứng dụng của bạn trên thiết bị có thể gập lại có màn hình nhỏ và lớn, với các thiết bị ở trạng thái gập lại và mở ra, theo hướng dọc và ngang, ở tư thế đứng và nằm, cũng như ở chế độ nhiều cửa sổ. Xem các nguyên tắc về Chất lượng ứng dụng trên màn hình lớn để biết thêm thông tin.

FoldingFeature

Thư viện Jetpack WindowManager sẽ thông báo cho ứng dụng khi tư thế của thiết bị có thể gập lại thay đổi để bạn có thể thay đổi bố cục cho ứng dụng của mình.

Cấu phần phần mềm window-testing chứa quy tắc JUnit4 WindowLayoutInfoPublisherRule, cho phép bạn phát hành WindowInfoLayout tuỳ chỉnh để mô phỏng FoldingFeature trong các phép kiểm thử.

Để kiểm thử trạng thái của một tính năng gập, trước tiên, hãy xác định lớp kiểm thử và quy tắc kiểm thử:

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

Tiếp theo, bạn có thể mô phỏng một tính năng gập, chẳng hạn như màn hình có thể gập lại đang mở một nửa với đường gập ngang ở chính giữa có chiều rộng bằng 0:

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

Sau đó, hãy sử dụng WindowLayoutInfoPublisherRule để phát hành WindowLayoutInfo tuỳ chỉnh:

Kotlin

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

Java

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

Cuối cùng, hãy dùng trình so khớp Espresso hiện có để kiểm tra xem bố cục của hoạt động có đúng như dự kiến hay không.

Ví dụ sau đây mô phỏng FoldingFeature có bản lề dọc HALF_OPENED ở giữa màn hình, sau đó dùng trình so khớp để kiểm tra xem bố cục có phải là bố cục dự kiến hay không:

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

Thay đổi về cấu hình

Nếu ứng dụng của bạn xử lý các thay đổi về cấu hình theo phương thức lập trình bằng phương thức gọi lại onConfigurationChanged(), hãy kiểm tra để đảm bảo rằng ứng dụng nhanh chóng phản hồi các thay đổi về cấu hình, đặc biệt là thao tác xoay thiết bị theo hướng dọc và ngang.

Để đảm bảo ứng dụng của bạn nhận được thông báo về những thay đổi đối với hướng và kích thước màn hình, hãy chỉ định các chế độ cài đặt cấu hình sau trong phần tử tệp kê khai activity:configChanges:

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

Tài nguyên khác