UiObject2Asserts.java revision 07b043dc3db83d6d20f0e8513b946830ab00e37b
1/*
2 * Copyright (C) 2015 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 */
16package com.android.tv.testing.uihelper;
17
18import static junit.framework.Assert.assertTrue;
19
20import android.support.test.uiautomator.SearchCondition;
21import android.support.test.uiautomator.UiObject2;
22
23/**
24 * Asserts for {@link UiObject2}s.
25 */
26public final class UiObject2Asserts {
27
28    /**
29     * Assert that {@code searchCondition} becomes true within
30     * {@value Constants#MAX_SHOW_DELAY_MILLIS} milliseconds.
31     *
32     * @param uiObject        the device under test.
33     * @param searchCondition the condition to wait for.
34     */
35    public static void assertWaitForCondition(UiObject2 uiObject,
36            SearchCondition<Boolean> searchCondition) {
37        assertWaitForCondition(uiObject, searchCondition, Constants.MAX_SHOW_DELAY_MILLIS);
38    }
39
40    /**
41     * Assert that {@code searchCondition} becomes true within {@code timeout} milliseconds.
42     *
43     * @param uiObject        the device under test.
44     * @param searchCondition the condition to wait for.
45     */
46    public static void assertWaitForCondition(UiObject2 uiObject,
47            SearchCondition<Boolean> searchCondition, long timeout) {
48        long adjustedTimeout = getAdjustedTimeout(timeout);
49        boolean result = uiObject.wait(searchCondition, adjustedTimeout);
50        assertTrue(searchCondition + " not true after " + timeout / 1000.0 + " seconds.", result);
51    }
52
53    public static long getAdjustedTimeout(long timeout) {
54        return timeout + Math.max(
55                Constants.MIN_EXTRA_TIMEOUT, (long) (timeout * Constants.EXTRA_TIMEOUT_PERCENT));
56    }
57
58    private UiObject2Asserts() {
59    }
60}
61