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