1 /* 2 * Copyright (C) 2009 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 android.view.animation.cts; 18 19 import android.app.Instrumentation; 20 import android.cts.util.PollingCheck; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.view.animation.Animation; 24 import android.view.animation.LayoutAnimationController; 25 26 /** 27 * The utility methods for animation test. 28 */ 29 public final class AnimationTestUtils { 30 /** timeout delta when wait in case the system is sluggish */ 31 private static final long TIMEOUT_DELTA = 1000; 32 33 /** 34 * no public constructor since this is a utility class 35 */ 36 private AnimationTestUtils() { 37 38 } 39 40 /** 41 * Assert run an animation successfully. Timeout is duration of animation. 42 * 43 * @param instrumentation to run animation. 44 * @param view view window to run animation. 45 * @param animation will be run. 46 */ 47 public static void assertRunAnimation(final Instrumentation instrumentation, 48 final View view, final Animation animation) { 49 assertRunAnimation(instrumentation, view, animation, animation.getDuration()); 50 } 51 52 /** 53 * Assert run an animation successfully. 54 * 55 * @param instrumentation to run animation. 56 * @param view window to run animation. 57 * @param animation will be run. 58 * @param duration in milliseconds. 59 */ 60 public static void assertRunAnimation(final Instrumentation instrumentation, 61 final View view, final Animation animation, final long duration) { 62 63 instrumentation.runOnMainSync(new Runnable() { 64 public void run() { 65 view.startAnimation(animation); 66 } 67 }); 68 69 // check whether it has started 70 new PollingCheck() { 71 @Override 72 protected boolean check() { 73 return animation.hasStarted(); 74 } 75 }.run(); 76 77 // check whether it has ended after duration 78 new PollingCheck(duration + TIMEOUT_DELTA) { 79 @Override 80 protected boolean check() { 81 return animation.hasEnded(); 82 } 83 }.run(); 84 85 instrumentation.waitForIdleSync(); 86 } 87 88 /** 89 * Assert run an AbsListView with LayoutAnimationController successfully. 90 * @param instrumentation 91 * @param view 92 * @param controller 93 * @param duration 94 * @throws InterruptedException 95 */ 96 public static void assertRunController(final Instrumentation instrumentation, 97 final ViewGroup view, final LayoutAnimationController controller, 98 final long duration) throws InterruptedException { 99 100 instrumentation.runOnMainSync(new Runnable() { 101 public void run() { 102 view.setLayoutAnimation(controller); 103 view.requestLayout(); 104 } 105 }); 106 107 // LayoutAnimationController.isDone() always returns true, it's no use for stopping 108 // the running, so just using sleeping fixed time instead. we reported issue 1799434 for it. 109 Thread.sleep(duration + TIMEOUT_DELTA); 110 } 111 } 112