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