折りたたみ式デバイスには、特別なテストを必要とする独自の機能があります。小さな画面と大きな画面の折りたたみ式デバイスでのアプリのテストを、折りたたんだ状態と広げた状態、縦向きと横向き、テーブルトップ形状とブック形状、マルチウィンドウ モードで行います。詳しくは、大画面のアプリの品質に関するガイドラインをご覧ください。
FoldingFeature
Jetpack WindowManager ライブラリは、折りたたみ式デバイスの形状が変更されるとアプリに通知するため、アプリのレイアウトを変更できます。
window-testing
アーティファクトには WindowLayoutInfoPublisherRule
JUnit4 ルールが含まれており、テストで FoldingFeature
をシミュレートするカスタムの WindowInfoLayout
を公開できます。
折りたたみ機能のステータスをテストするには、まずテストクラスとテストルールを定義します。
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 マッチャーを使用して、アクティビティ レイアウトが期待どおりに動作するかどうかを確認します。
次の例では、画面の中央の垂直ヒンジが HALF_OPENED
である場合の FoldingFeature
をシミュレートして、マッチャーを使用してレイアウトが想定どおりであるかを確認します。
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"