1 package com.android.launcher3.util; 2 3 import android.support.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 abstract class Condition { 12 isTrue()13 public abstract boolean isTrue() throws Throwable; 14 15 /** 16 * Converts the condition to be run on UI thread. 17 */ runOnUiThread(final Condition condition)18 public static Condition runOnUiThread(final Condition condition) { 19 final MainThreadExecutor executor = new MainThreadExecutor(); 20 return new Condition() { 21 @Override 22 public boolean isTrue() throws Throwable { 23 final AtomicBoolean value = new AtomicBoolean(false); 24 final Throwable[] exceptions = new Throwable[1]; 25 final CountDownLatch latch = new CountDownLatch(1); 26 executor.execute(new Runnable() { 27 @Override 28 public void run() { 29 try { 30 value.set(condition.isTrue()); 31 } catch (Throwable e) { 32 exceptions[0] = e; 33 } 34 35 } 36 }); 37 latch.await(1, TimeUnit.SECONDS); 38 if (exceptions[0] != null) { 39 throw exceptions[0]; 40 } 41 return value.get(); 42 } 43 }; 44 } 45 46 public static Condition minChildCount(final UiObject2 obj, final int childCount) { 47 return new Condition() { 48 @Override 49 public boolean isTrue() { 50 return obj.getChildCount() >= childCount; 51 } 52 }; 53 } 54 } 55