Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b856e485 authored by vadimt's avatar vadimt
Browse files

Not crash when UI objects disappear during visible objects verification

Bug: 146785144
Change-Id: I5ccac4871c683413c27753774adc432e5e654e27
parent 0fd48b51
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.Configurator;
import androidx.test.uiautomator.Direction;
import androidx.test.uiautomator.StaleObjectException;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;
@@ -311,12 +312,21 @@ public final class LauncherInstrumentation {
    private String getVisiblePackages() {
        return mDevice.findObjects(By.textStartsWith(""))
                .stream()
                .map(object -> object.getApplicationPackage())
                .map(LauncherInstrumentation::getApplicationPackageSafe)
                .distinct()
                .filter(pkg -> !"com.android.systemui".equals(pkg))
                .filter(pkg -> pkg != null && !"com.android.systemui".equals(pkg))
                .collect(Collectors.joining(", "));
    }

    private static String getApplicationPackageSafe(UiObject2 object) {
        try {
            return object.getApplicationPackage();
        } catch (StaleObjectException e) {
            // We are looking at all object in the system; external ones can suddenly go away.
            return null;
        }
    }

    private String getVisibleStateMessage() {
        if (hasLauncherObject(CONTEXT_MENU_RES_ID)) return "Context Menu";
        if (hasLauncherObject(WIDGETS_RES_ID)) return "Widgets";