| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.inputmethod.stresstest; |
| 18 |
|
| 19 |
import static com.android.compatibility.common.util.SystemUtil.eventually; |
| 20 |
|
| 21 |
import static com.google.common.truth.Truth.assertWithMessage; |
| 22 |
|
| 23 |
import android.view.View; |
| 24 |
import android.view.WindowInsets; |
| 25 |
|
| 26 |
import androidx.test.platform.app.InstrumentationRegistry; |
| 27 |
|
| 28 |
import java.util.concurrent.Callable; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
import java.util.concurrent.atomic.AtomicReference; |
| 31 |
|
| 32 |
|
| 33 |
public final class ImeStressTestUtil { |
| 34 |
|
| 35 |
private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(5); |
| 36 |
|
| 37 |
private ImeStressTestUtil() { |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
public static boolean isImeShown(View view) { |
| 42 |
WindowInsets insets = view.getRootWindowInsets(); |
| 43 |
return insets.isVisible(WindowInsets.Type.ime()); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
public static <V> V callOnMainSync(Callable<V> callable) { |
| 48 |
AtomicReference<V> result = new AtomicReference<>(); |
| 49 |
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 50 |
try { |
| 51 |
result.set(callable.call()); |
| 52 |
} catch (Exception e) { |
| 53 |
throw new RuntimeException("Exception was thrown", e); |
| 54 |
} |
| 55 |
}); |
| 56 |
return result.get(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static void waitOnMainUntil(String message, Callable<Boolean> pred) { |
| 65 |
eventually(() -> assertWithMessage(message).that(pred.call()).isTrue(), TIMEOUT); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
public static void waitOnMainUntilImeIsShown(View view) { |
| 70 |
eventually(() -> assertWithMessage("IME should be shown").that( |
| 71 |
callOnMainSync(() -> isImeShown(view))).isTrue(), TIMEOUT); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public static void waitOnMainUntilImeIsHidden(View view) { |
| 76 |
|
| 77 |
eventually(() -> assertWithMessage("IME should be hidden").that( |
| 78 |
callOnMainSync(() -> isImeShown(view))).isFalse(), TIMEOUT); |
| 79 |
} |
| 80 |
} |