1 package com.android.launcher3.util; 2 3 import androidx.test.uiautomator.UiObject2; 4 5 import com.android.launcher3.MainThreadExecutor; 6 7 import java.util.concurrent.CountDownLatch; 8 import java.util.concurrent.TimeUnit; 9 import java.util.concurrent.atomic.AtomicBoolean; 10 11 public interface Condition { 12 isTrue()13 boolean isTrue() throws Throwable; 14 15 /** 16 * Converts the condition to be run on UI thread. 17 */ runOnUiThread(final Condition condition)18 static Condition runOnUiThread(final Condition condition) { 19 final MainThreadExecutor executor = new MainThreadExecutor(); 20 return () -> { 21 final AtomicBoolean value = new AtomicBoolean(false); 22 final Throwable[] exceptions = new Throwable[1]; 23 final CountDownLatch latch = new CountDownLatch(1); 24 executor.execute(() -> { 25 try { 26 value.set(condition.isTrue()); 27 } catch (Throwable e) { 28 exceptions[0] = e; 29 } 30 31 }); 32 latch.await(1, TimeUnit.SECONDS); 33 if (exceptions[0] != null) { 34 throw exceptions[0]; 35 } 36 return value.get(); 37 }; 38 } 39 minChildCount(final UiObject2 obj, final int childCount)40 static Condition minChildCount(final UiObject2 obj, final int childCount) { 41 return () -> obj.getChildCount() >= childCount; 42 } 43 } 44