Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 android.animation; |
| 17 | |
| 18 | import android.os.Handler; |
| 19 | import android.test.ActivityInstrumentationTestCase2; |
| 20 | import android.test.suitebuilder.annotation.SmallTest; |
Abodunrinwa Toki | 1ef0801 | 2015-09-04 11:42:16 +0100 | [diff] [blame^] | 21 | import android.test.suitebuilder.annotation.Suppress; |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.HashMap; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
Abodunrinwa Toki | 1ef0801 | 2015-09-04 11:42:16 +0100 | [diff] [blame^] | 26 | @Suppress // Failing |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 27 | public class AutoCancelTest extends ActivityInstrumentationTestCase2<BasicAnimatorActivity> { |
| 28 | |
| 29 | boolean mAnimX1Canceled = false; |
| 30 | boolean mAnimXY1Canceled = false; |
| 31 | boolean mAnimX2Canceled = false; |
| 32 | boolean mAnimXY2Canceled = false; |
| 33 | |
| 34 | private static final long START_DELAY = 100; |
| 35 | private static final long DELAYED_START_DURATION = 200; |
| 36 | private static final long FUTURE_TIMEOUT = 1000; |
| 37 | |
| 38 | HashMap<Animator, Boolean> mCanceledMap = new HashMap<Animator, Boolean>(); |
| 39 | |
| 40 | public AutoCancelTest() { |
| 41 | super(BasicAnimatorActivity.class); |
| 42 | } |
| 43 | |
| 44 | ObjectAnimator setupAnimator(long startDelay, String... properties) { |
| 45 | ObjectAnimator returnVal; |
| 46 | if (properties.length == 1) { |
| 47 | returnVal = ObjectAnimator.ofFloat(this, properties[0], 0, 1); |
| 48 | } else { |
| 49 | PropertyValuesHolder[] pvhArray = new PropertyValuesHolder[properties.length]; |
| 50 | for (int i = 0; i < properties.length; i++) { |
| 51 | pvhArray[i] = PropertyValuesHolder.ofFloat(properties[i], 0, 1); |
| 52 | } |
| 53 | returnVal = ObjectAnimator.ofPropertyValuesHolder(this, pvhArray); |
| 54 | } |
| 55 | returnVal.setAutoCancel(true); |
| 56 | returnVal.setStartDelay(startDelay); |
| 57 | returnVal.addListener(mCanceledListener); |
| 58 | return returnVal; |
| 59 | } |
| 60 | |
| 61 | private void setupAnimators(long startDelay, boolean startLater, final FutureWaiter future) |
| 62 | throws Exception { |
| 63 | // Animators to be auto-canceled |
| 64 | final ObjectAnimator animX1 = setupAnimator(startDelay, "x"); |
| 65 | final ObjectAnimator animY1 = setupAnimator(startDelay, "y"); |
| 66 | final ObjectAnimator animXY1 = setupAnimator(startDelay, "x", "y"); |
| 67 | final ObjectAnimator animXZ1 = setupAnimator(startDelay, "x", "z"); |
| 68 | |
| 69 | animX1.start(); |
| 70 | animY1.start(); |
| 71 | animXY1.start(); |
| 72 | animXZ1.start(); |
| 73 | |
| 74 | final ObjectAnimator animX2 = setupAnimator(0, "x"); |
| 75 | animX2.addListener(new AnimatorListenerAdapter() { |
| 76 | @Override |
| 77 | public void onAnimationStart(Animator animation) { |
| 78 | // We expect only animX1 to be canceled at this point |
| 79 | if (mCanceledMap.get(animX1) == null || |
| 80 | mCanceledMap.get(animX1) != true || |
| 81 | mCanceledMap.get(animY1) != null || |
| 82 | mCanceledMap.get(animXY1) != null || |
| 83 | mCanceledMap.get(animXZ1) != null) { |
| 84 | future.set(false); |
| 85 | } |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | final ObjectAnimator animXY2 = setupAnimator(0, "x", "y"); |
| 90 | animXY2.addListener(new AnimatorListenerAdapter() { |
| 91 | @Override |
| 92 | public void onAnimationStart(Animator animation) { |
| 93 | // We expect only animXY1 to be canceled at this point |
| 94 | if (mCanceledMap.get(animXY1) == null || |
| 95 | mCanceledMap.get(animXY1) != true || |
| 96 | mCanceledMap.get(animY1) != null || |
| 97 | mCanceledMap.get(animXZ1) != null) { |
| 98 | future.set(false); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onAnimationEnd(Animator animation) { |
| 105 | // Release future if not done already via failures during start |
| 106 | future.release(); |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | if (startLater) { |
| 111 | Handler handler = new Handler(); |
| 112 | handler.postDelayed(new Runnable() { |
| 113 | @Override |
| 114 | public void run() { |
| 115 | animX2.start(); |
| 116 | animXY2.start(); |
| 117 | } |
| 118 | }, DELAYED_START_DURATION); |
| 119 | } else { |
| 120 | animX2.start(); |
| 121 | animXY2.start(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @SmallTest |
| 126 | public void testAutoCancel() throws Exception { |
| 127 | final FutureWaiter future = new FutureWaiter(); |
| 128 | getActivity().runOnUiThread(new Runnable() { |
| 129 | @Override |
| 130 | public void run() { |
| 131 | try { |
| 132 | setupAnimators(0, false, future); |
| 133 | } catch (Exception e) { |
| 134 | future.setException(e); |
| 135 | } |
| 136 | } |
| 137 | }); |
| 138 | assertTrue(future.get(FUTURE_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 139 | } |
| 140 | |
| 141 | @SmallTest |
| 142 | public void testAutoCancelDelayed() throws Exception { |
| 143 | final FutureWaiter future = new FutureWaiter(); |
| 144 | getActivity().runOnUiThread(new Runnable() { |
| 145 | @Override |
| 146 | public void run() { |
| 147 | try { |
| 148 | setupAnimators(START_DELAY, false, future); |
| 149 | } catch (Exception e) { |
| 150 | future.setException(e); |
| 151 | } |
| 152 | } |
| 153 | }); |
| 154 | assertTrue(future.get(FUTURE_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 155 | } |
| 156 | |
| 157 | @SmallTest |
| 158 | public void testAutoCancelTestLater() throws Exception { |
| 159 | final FutureWaiter future = new FutureWaiter(); |
| 160 | getActivity().runOnUiThread(new Runnable() { |
| 161 | @Override |
| 162 | public void run() { |
| 163 | try { |
| 164 | setupAnimators(0, true, future); |
| 165 | } catch (Exception e) { |
| 166 | future.setException(e); |
| 167 | } |
| 168 | } |
| 169 | }); |
| 170 | assertTrue(future.get(FUTURE_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 171 | } |
| 172 | |
| 173 | @SmallTest |
| 174 | public void testAutoCancelDelayedTestLater() throws Exception { |
| 175 | final FutureWaiter future = new FutureWaiter(); |
| 176 | getActivity().runOnUiThread(new Runnable() { |
| 177 | @Override |
| 178 | public void run() { |
| 179 | try { |
| 180 | setupAnimators(START_DELAY, true, future); |
| 181 | } catch (Exception e) { |
| 182 | future.setException(e); |
| 183 | } |
| 184 | } |
| 185 | }); |
| 186 | assertTrue(future.get(FUTURE_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 187 | } |
| 188 | |
| 189 | private AnimatorListenerAdapter mCanceledListener = new AnimatorListenerAdapter() { |
| 190 | @Override |
| 191 | public void onAnimationCancel(Animator animation) { |
| 192 | mCanceledMap.put(animation, true); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | public void setX(float x) {} |
| 197 | |
| 198 | public void setY(float y) {} |
| 199 | |
| 200 | public void setZ(float z) {} |
| 201 | } |
| 202 | |
| 203 | |