1/* 2 * Copyright (C) 2008 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 17package android.util; 18 19import junit.framework.Assert; 20 21import android.test.InstrumentationTestCase; 22import android.test.TouchUtils; 23import android.view.View; 24 25/** 26 * When entering touch mode via touch, the tests can be flaky. These asserts 27 * are more flexible (allowing up to MAX_ATTEMPTS touches to enter touch mode via touch or 28 * tap) until we can find a way to solve the flakiness. 29 */ 30public class TouchModeFlexibleAsserts { 31 32 private static int MAX_ATTEMPTS = 2; 33 34 private static int MAX_DELAY_MILLIS = 2000; 35 36 public static void assertInTouchModeAfterClick(InstrumentationTestCase test, View viewToTouch) { 37 int numAttemptsAtTouchMode = 0; 38 while (numAttemptsAtTouchMode < MAX_ATTEMPTS && 39 !viewToTouch.isInTouchMode()) { 40 TouchUtils.clickView(test, viewToTouch); 41 numAttemptsAtTouchMode++; 42 } 43 Assert.assertTrue("even after " + MAX_ATTEMPTS + " clicks, did not enter " 44 + "touch mode", viewToTouch.isInTouchMode()); 45 //Assert.assertEquals("number of touches to enter touch mode", 1, numAttemptsAtTouchMode); 46 } 47 48 public static void assertInTouchModeAfterTap(InstrumentationTestCase test, View viewToTouch) { 49 int numAttemptsAtTouchMode = 0; 50 while (numAttemptsAtTouchMode < MAX_ATTEMPTS && 51 !viewToTouch.isInTouchMode()) { 52 TouchUtils.tapView(test, viewToTouch); 53 numAttemptsAtTouchMode++; 54 } 55 Assert.assertTrue("even after " + MAX_ATTEMPTS + " taps, did not enter " 56 + "touch mode", viewToTouch.isInTouchMode()); 57 //Assert.assertEquals("number of touches to enter touch mode", 1, numAttemptsAtTouchMode); 58 } 59 60 public static void assertNotInTouchModeAfterKey(InstrumentationTestCase test, int keyCode, View checkForTouchMode) { 61 test.sendKeys(keyCode); 62 int amountLeft = MAX_DELAY_MILLIS; 63 64 while (checkForTouchMode.isInTouchMode() && amountLeft > 0) { 65 amountLeft -= 200; 66 try { 67 Thread.sleep(200); 68 } catch (InterruptedException e) { 69 throw new RuntimeException(e); 70 } 71 } 72 Assert.assertFalse("even after waiting " + MAX_DELAY_MILLIS + " millis after " 73 + "pressing key event, still in touch mode", checkForTouchMode.isInTouchMode()); 74 } 75} 76