LoginSignup
0
0

More than 5 years have passed since last update.

UIテストでsuspend/resume

Posted at

suspendしてbackgroundに入る

これは簡単で、UiDevice#pressHomeを呼び出せばできます:

    public static void enterBackground() {
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        device.pressHome();
    }

resumeしてforegroundに戻る

UiDevice#pressRecentAppsでアプリ一覧を出して、そこでアプリケーションの表示名を探してclickします。
しかし、Recent AppsのUIレイアウトに依存するので、今後UIが変わった場合は対応する必要があります。下記の方法だと一応API14~API28までは動きます。

    public static void enterForeground() throws Exception {
        Context context = InstrumentationRegistry.getTargetContext();
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        device.pressRecentApps();
        UiObject object = device.findObject(new UiSelector().description(context.getString(context.getApplicationInfo().labelRes)));
        object.click();
    }

参考:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0