Share via


折りたたみ型デバイス向けの Jetpack Compose テストを作成する

Jetpack Compose には、Espresso が従来のビュー システムで使用されるのと同じように、レイアウトをテストするのに使用できるテスト API が用意されています。 また、Jetpack Window Manager には、 オブジェクトのシミュレートを容易にするテスト ライブラリも用意されています。 これらのリソースを使って、折りたたみ型デュアルスクリーン デバイス向けの Compose プロジェクトでインストルメント化された UI テストを作成できます。

ComposeTesting ライブラリ

折りたたみ型のテストを作成するために必要なコード量を減らすために、テスト キットの一部である ComposeTesting ライブラリを使うこともできます。 このライブラリには、FoldingFeature オブジェクトをシミュレートするユーティリティ メソッドと、スワイプ ジェスチャや文字列リソース ヘルパー メソッドなどの Compose UI テストに便利な機能が含まれています。

テストで ComposeTesting ライブラリを使うには、この記事のコード スニペットの [ComposeTesting ライブラリを使用] タブをクリックしてください。

セットアップ

  1. androidTest ディレクトリに新しいテスト クラス ファイルを作成します。 これは、後でテスト規則とテスト用にコード スニペットに追加する場所です。

  2. 最上位の build.gradle ファイルに mavenCentral() リポジトリがあることを確認します。

    allprojects {
        repositories {
            google()
            mavenCentral()
         }
    }
    
  3. 次の依存関係をモジュールレベル build.gradle ファイルに追加します (現在のバージョンは、ここに示されているものとは異なる場合があります)。

    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.3.0"
    androidTestImplementation "androidx.window:window-testing:1.0.0"
    
    // Only necessary if Jetpack Window Manager isn't imported for implementation
    androidTestImplementation "androidx.window:window:1.0.0"
    
  4. compileSdkVersionが API 33 に設定され、 targetSdkVersion がモジュール レベルの build.gradle ファイルで API 32 以降に設定されていることを確認します。

    android { 
        compileSdkVersion 33
    
        defaultConfig { 
            targetSdkVersion 32
        } 
        ... 
    }
    
  5. Compose チェックを実行したり、折りたたみ機能をシミュレートしたりできる TestRule を作成します。 これを行うには、発行元ルールと Android Compose テスト ルールの WindowLayoutInfo 2 つのルールを連結します。

    private val composeTestRule = createAndroidComposeRule<MainActivity>()
    private val publisherRule = WindowLayoutInfoPublisherRule()
    
    @get:Rule
    val testRule: TestRule
    
    init {
        testRule = RuleChain.outerRule(publisherRule).around(composeTestRule)
    }
    

テストを記述する方法

折りたたみ型デュアルスクリーン デバイス用の Compose テストを記述するには、次の 4 つの手順を実行します。

  1. テストの内容を設定します
  2. FoldingFeature をシミュレートします
  3. テストするノードを見つけます
  4. ノードに対してアサーションまたはアクションを実行します

次のコード スニペットでは、縦方向の FoldingFeature がある場合に、"ペイン 1" と "ペイン 2" のテキスト要素が TwoPaneSample コンポーザブルに表示されるかどうかを確認するテスト例を示します。

@Test
fun sample_verticalFoldingFeature_showsTwoPanes() {
    // 1. Optional: set the content of the test (default is MainActivity content)
    composeTestRule.activity.setContent {
        TwoPaneSample()
    }

    // 2. Simulate a vertical FoldingFeature
    composeTestRule.activityRule.scenario.onActivity { activity ->
        val verticalFoldingFeature = FoldingFeature(
            activity = activity,
            orientation = FoldingFeature.Orientation.VERTICAL
        )
        val windowLayoutInfo = TestWindowLayoutInfo(listOf(verticalFoldingFeature))
        publisherRule.overrideWindowLayoutInfo(windowLayoutInfo)
    }

    // 3. Find the nodes to test and 4. Perform assertions on the nodes
    composeTestRule.onNodeWithText("pane 1").assertIsDisplayed()
    composeTestRule.onNodeWithText("pane 2").assertIsDisplayed()
}

独自のテストを記述する際、必要に応じて、各手順をカスタマイズできます。 任意のコンポーザブルは setContent ラムダに入ることができ、setContent の位置、サイズ、向き、および状態を変更できます。

リソース

Jetpack Compose と Jetpack Window Manager を使用したテストについて詳しくは、次のリソースを参照してください。