Espresso Device API を使用して画面構成の変更をテストする

Espresso Device API を使用して、デバイスの回転や画面の展開など、一般的な構成変更が行われたときにアプリをテストします。Espresso Device API は、Jetpack Compose テストルールとともにデバイスレベルのアクションをシミュレートするのに推奨されるツールです。Jetpack Compose の UI テストの作成を初めて行う場合は、Compose レイアウトのテストをご覧ください。

Espresso Device API を使用すると、仮想デバイスで構成の変更をシミュレートし、テストを同期的に実行できます。そのため、一度に 1 つの UI アクションまたはアサーションのみが実行され、テスト結果の信頼性が高まります。Espresso を使用した UI テストの作成が初めての場合は、ドキュメントをご覧ください。

Espresso Device API を使用するには、次のものが必要です。

  • Android Studio Iguana 以降
  • Android Gradle プラグイン 8.3 以降
  • Android Emulator 33.1.10 以降
  • API レベル 24 以上を実行する Android 仮想デバイス

Espresso Device API 用にプロジェクトを設定する

Espresso Device API をサポートするようにプロジェクトを設定する手順は次のとおりです。

  1. テストがテストデバイスにコマンドを渡せるようにするには、androidTest ソースセットのマニフェスト ファイルに INTERNET 権限と ACCESS_NETWORK_STATE 権限を追加します。

      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
  2. gradle.properties ファイルで enableEmulatorControl 試験運用版フラグを有効にします。

      android.experimental.androidTest.enableEmulatorControl=true
    
  3. モジュール レベルのビルド スクリプトで emulatorControl オプションを有効にします。

    Kotlin

      testOptions {
        emulatorControl {
          enable = true
        }
      }
      

    Groovy

      testOptions {
        emulatorControl {
          enable = true
        }
      }
      
  4. モジュール レベルのビルド スクリプトで、Espresso Device ライブラリをプロジェクトにインポートします。

    Kotlin

    dependencies {
      androidTestImplementation("androidx.test.espresso:espresso-device:1.0.1")
    }

    Groovy

    dependencies {
      androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1'
    }

一般的な構成変更に対してテストする

Espresso Device API には、デバイス構成の変更をシミュレートするために使用できる画面の向きと折りたたみ式デバイスの状態が複数あります。次の例は、Compose テストルールを使用して、これらのデバイスの状態をトリガーし、結果として得られる UI の変更を確認する方法を示しています。

画面の回転に対するテスト

デバイスの画面が回転したときにアプリがどうなるかをテストする例を次に示します。

  1. まず、Compose テストルールを定義し、デバイスを一貫した開始状態(縦向きモードなど)に設定します。

    import androidx.compose.ui.test.assertIsDisplayed
    import androidx.compose.ui.test.assertDoesNotExist
    import androidx.compose.ui.test.junit4.createComposeRule
    import androidx.compose.ui.test.onNodeWithTag
    import androidx.test.espresso.device.EspressoDevice.onDevice
    import androidx.test.espresso.device.action.ScreenOrientation
    import androidx.test.espresso.device.rules.ScreenOrientationRule
    import org.junit.Rule
    import org.junit.Test
    
    class MyConfigurationTest {
    
        // 1. Define the Compose test rule
        @get:Rule
        val composeTestRule = createComposeRule()
    
        // 2. Define the Espresso Device rule for a consistent starting state
        @get:Rule
        val screenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)
    }
    
  2. テスト実行中にデバイスを横向きに設定するテストを作成します。

    @Test
    fun myRotationTest() {
      ...
      // Sets the device to landscape orientation during test execution.
      onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
      ...
    }
    
  3. 画面が回転したら、composeTestRule を使用して、コンポーザブルが新しい状態に想定どおりに適応していることを確認します。

    @Test
    fun myRotationTest() {
      ...
      // Sets the device to landscape orientation during test execution.
      onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
      composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
      composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist()
    }
    

画面の展開に対するテスト

折りたたみ式デバイスで画面が開いた場合にアプリがどうなるかをテストする例を次に示します。

  1. まず、onDevice().setClosedMode() を呼び出して、デバイスが折りたたまれた状態でテストします。コンポーザブルがコンパクトな画面幅に対応していることを確認します。

    @Test
    fun myUnfoldedTest() {
      onDevice().setClosedMode()
      composeTestRule.onNodeWithTag("BottomBar").assertIsDisplayed()
      composeTestRule.onNodeWithTag("NavRail").assertDoesNotExist()
      ...
    }
    
  2. 完全に開いた状態に遷移させるには、onDevice().setFlatMode() を呼び出します。コンポーザブルが展開されたサイズクラスに適応していることを確認します。

    @Test
    fun myUnfoldedTest() {
      onDevice().setClosedMode()
      ...
      onDevice().setFlatMode()
      composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
      composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist()
    }
    

テストに必要なデバイスを指定する

折りたたみ式ではないデバイスで折りたたみ操作を行うテストを実行すると、テストが失敗する可能性があります。実行中のデバイスに関連するテストのみを実行するには、@RequiresDeviceMode アノテーションを使用します。テストランナーは、テスト対象の構成をサポートしていないデバイスでのテストの実行を自動的にスキップします。デバイス要件ルールは、各テストまたはテストクラス全体に追加できます。

たとえば、テストをフラット構成への展開をサポートするデバイスでのみ実行するように指定するには、次の @RequiresDeviceMode コードをテストに追加します。

@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
  ...
}