可折叠设备具有独特的特性和功能,需要进行专门测试。分别在小屏和大屏可折叠设备上、设备折叠和展开状态、纵向和横向模式、桌面和图书折叠状态以及多窗口模式下测试您的应用。如需了解详情,请参阅大屏设备应用质量指南。
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 匹配器检查 activity 布局的行为是否符合预期。
以下示例模拟了 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"