Debian Codegraph @

tests/InputMethodStressTest/src/com/android/inputmethod/stresstest/ImeStressTestUtil.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 /** Utility methods for IME stress test. */
33 public final class ImeStressTestUtil {
34
35 private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(5);
36
37 private ImeStressTestUtil() {
38 }
39
40 /** Checks if the IME is shown on the window that the given view belongs to. */
41 public static boolean isImeShown(View view) {
42 WindowInsets insets = view.getRootWindowInsets();
43 return insets.isVisible(WindowInsets.Type.ime());
44 }
45
46 /** Calls the callable on the main thread and returns the result. */
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 * Waits until {@code pred} returns true, or throws on timeout.
61 *
62 * <p>The given {@code pred} will be called on the main thread.
63 */
64 public static void waitOnMainUntil(String message, Callable<Boolean> pred) {
65 eventually(() -> assertWithMessage(message).that(pred.call()).isTrue(), TIMEOUT);
66 }
67
68 /** Waits until IME is shown, or throws on timeout. */
69 public static void waitOnMainUntilImeIsShown(View view) {
70 eventually(() -> assertWithMessage("IME should be shown").that(
71 callOnMainSync(() -> isImeShown(view))).isTrue(), TIMEOUT);
72 }
73
74 /** Waits until IME is hidden, or throws on timeout. */
75 public static void waitOnMainUntilImeIsHidden(View view) {
76 //eventually(() -> assertThat(callOnMainSync(() -> isImeShown(view))).isFalse(), TIMEOUT);
77 eventually(() -> assertWithMessage("IME should be hidden").that(
78 callOnMainSync(() -> isImeShown(view))).isFalse(), TIMEOUT);
79 }
80 }