summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/launcher3/util/Condition.java
blob: b564a1a8737adee8569fb4e40ac1ca45de6a0977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.android.launcher3.util;

import androidx.test.uiautomator.UiObject2;

import com.android.launcher3.MainThreadExecutor;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public interface Condition {

    boolean isTrue() throws Throwable;

    /**
     * Converts the condition to be run on UI thread.
     */
    static Condition runOnUiThread(final Condition condition) {
        final MainThreadExecutor executor = new MainThreadExecutor();
        return () -> {
            final AtomicBoolean value = new AtomicBoolean(false);
            final Throwable[] exceptions = new Throwable[1];
            final CountDownLatch latch = new CountDownLatch(1);
            executor.execute(() -> {
                try {
                    value.set(condition.isTrue());
                } catch (Throwable e) {
                    exceptions[0] = e;
                }

            });
            latch.await(1, TimeUnit.SECONDS);
            if (exceptions[0] != null) {
                throw exceptions[0];
            }
            return value.get();
        };
    }

    static Condition minChildCount(final UiObject2 obj, final int childCount) {
        return () -> obj.getChildCount() >= childCount;
    }
}