Commit 4107f387 authored by pedrosimonetti's avatar pedrosimonetti Committed by Commit bot

Adding callback for finished property animations.

This CL extends the ChromeAnimation class to notify
Animatable interfaces that a particular property
animation has finished.

BUG=

Review URL: https://codereview.chromium.org/1801363006

Cr-Commit-Position: refs/heads/master@{#381854}
parent fba25cf5
...@@ -407,6 +407,9 @@ public abstract class OverlayPanelAnimation extends OverlayPanelBase ...@@ -407,6 +407,9 @@ public abstract class OverlayPanelAnimation extends OverlayPanelBase
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
/** /**
* Steps the animation forward and updates all the animated values. * Steps the animation forward and updates all the animated values.
* @param time The current time of the app in ms. * @param time The current time of the app in ms.
......
...@@ -131,4 +131,6 @@ public class ContextualSearchIconSpriteControl implements ...@@ -131,4 +131,6 @@ public class ContextualSearchIconSpriteControl implements
} }
} }
@Override
public void onPropertyAnimationFinished(AnimationType prop) {}
} }
...@@ -238,6 +238,9 @@ public class ContextualSearchPeekPromoControl extends OverlayPanelInflater ...@@ -238,6 +238,9 @@ public class ContextualSearchPeekPromoControl extends OverlayPanelInflater
} }
} }
@Override
public void onPropertyAnimationFinished(AnimationType prop) {}
/** /**
* Updates the UI for the appearance animation. * Updates the UI for the appearance animation.
* *
......
...@@ -219,6 +219,7 @@ public class ChromeAnimation<T> { ...@@ -219,6 +219,7 @@ public class ChromeAnimation<T> {
private long mDuration; private long mDuration;
private long mStartDelay; private long mStartDelay;
private boolean mDelayStartValue; private boolean mDelayStartValue;
private boolean mHasFinished;
private Interpolator mInterpolator = getDecelerateInterpolator(); private Interpolator mInterpolator = getDecelerateInterpolator();
/** /**
...@@ -313,6 +314,7 @@ public class ChromeAnimation<T> { ...@@ -313,6 +314,7 @@ public class ChromeAnimation<T> {
* Starts the animation and calls setProperty() with the initial value. * Starts the animation and calls setProperty() with the initial value.
*/ */
public void start() { public void start() {
mHasFinished = false;
mCurrentTime = 0; mCurrentTime = 0;
update(0); update(0);
} }
...@@ -321,10 +323,12 @@ public class ChromeAnimation<T> { ...@@ -321,10 +323,12 @@ public class ChromeAnimation<T> {
* @return Whether or not this current animation is finished. * @return Whether or not this current animation is finished.
*/ */
public boolean finished() { public boolean finished() {
if (mCurrentTime >= mDuration + mStartDelay) { if (!mHasFinished && mCurrentTime >= mDuration + mStartDelay) {
return true; mHasFinished = true;
onPropertyAnimationFinished();
} }
return false;
return mHasFinished;
} }
/** /**
...@@ -342,6 +346,11 @@ public class ChromeAnimation<T> { ...@@ -342,6 +346,11 @@ public class ChromeAnimation<T> {
* @param p The current animated value based on the current time and the Interpolator. * @param p The current animated value based on the current time and the Interpolator.
*/ */
public abstract void setProperty(float p); public abstract void setProperty(float p);
/**
* The abstract method that gets called when the property animation finished.
*/
public abstract void onPropertyAnimationFinished();
} }
/** /**
...@@ -359,6 +368,12 @@ public class ChromeAnimation<T> { ...@@ -359,6 +368,12 @@ public class ChromeAnimation<T> {
*/ */
public void setProperty(T prop, float val); public void setProperty(T prop, float val);
/**
* Notifies that the animation for a certain property has finished.
*
* @param prop The property that has finished animating.
*/
public void onPropertyAnimationFinished(T prop);
} }
/** /**
...@@ -391,6 +406,11 @@ public class ChromeAnimation<T> { ...@@ -391,6 +406,11 @@ public class ChromeAnimation<T> {
mAnimatedObject.setProperty(mProperty, p); mAnimatedObject.setProperty(mProperty, p);
} }
@Override
public void onPropertyAnimationFinished() {
mAnimatedObject.onPropertyAnimationFinished(mProperty);
}
/** /**
* Helper method to add an {@link ChromeAnimation.AnimatableAnimation} * Helper method to add an {@link ChromeAnimation.AnimatableAnimation}
* to a {@link ChromeAnimation} * to a {@link ChromeAnimation}
......
...@@ -340,6 +340,9 @@ public class ToolbarSwipeLayout extends Layout implements Animatable<ToolbarSwip ...@@ -340,6 +340,9 @@ public class ToolbarSwipeLayout extends Layout implements Animatable<ToolbarSwip
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
@Override @Override
protected SceneLayer getSceneLayer() { protected SceneLayer getSceneLayer() {
return mSceneLayer; return mSceneLayer;
......
...@@ -344,4 +344,7 @@ public class CompositorButton ...@@ -344,4 +344,7 @@ public class CompositorButton
// Do nothing. // Do nothing.
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
} }
...@@ -961,4 +961,7 @@ public class LayoutTab implements ChromeAnimation.Animatable<LayoutTab.Property> ...@@ -961,4 +961,7 @@ public class LayoutTab implements ChromeAnimation.Animatable<LayoutTab.Property>
break; break;
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
} }
...@@ -403,6 +403,9 @@ public class SimpleAnimationLayout ...@@ -403,6 +403,9 @@ public class SimpleAnimationLayout
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
@Override @Override
protected SceneLayer getSceneLayer() { protected SceneLayer getSceneLayer() {
return mSceneLayer; return mSceneLayer;
......
...@@ -1180,6 +1180,9 @@ public class StackLayout extends Layout implements Animatable<StackLayout.Proper ...@@ -1180,6 +1180,9 @@ public class StackLayout extends Layout implements Animatable<StackLayout.Proper
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
/** /**
* Called by the stacks whenever they start an animation. * Called by the stacks whenever they start an animation.
*/ */
......
...@@ -518,4 +518,7 @@ public class StackTab implements ChromeAnimation.Animatable<StackTab.Property> { ...@@ -518,4 +518,7 @@ public class StackTab implements ChromeAnimation.Animatable<StackTab.Property> {
break; break;
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
} }
...@@ -504,6 +504,9 @@ public class StripLayoutTab ...@@ -504,6 +504,9 @@ public class StripLayoutTab
} }
} }
@Override
public void onPropertyAnimationFinished(Property prop) {}
private void resetCloseRect() { private void resetCloseRect() {
RectF closeRect = getCloseRect(); RectF closeRect = getCloseRect();
mCloseButton.setWidth(closeRect.width()); mCloseButton.setWidth(closeRect.width());
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.compositor.layouts;
import static org.chromium.chrome.browser.compositor.layouts.ChromeAnimation.AnimatableAnimation.createAnimation;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.compositor.layouts.ChromeAnimation.Animatable;
/**
* Unit tests for {@link org.chromium.chrome.browser.compositor.layouts.ChromeAnimation}.
*/
public class ChromeAnimationTest extends InstrumentationTestCase
implements Animatable<ChromeAnimationTest.Property> {
protected enum Property {
FAST_ANIMATION,
SLOW_ANIMATION
}
private static final long FAST_DURATION = 100;
private static final long SLOW_DURATION = 1000;
private ChromeAnimation<Animatable<?>> mAnimations;
private boolean mHasFinishedFastAnimation;
private boolean mHasFinishedSlowAnimation;
@Override
protected void setUp() throws Exception {
super.setUp();
mHasFinishedFastAnimation = false;
mHasFinishedSlowAnimation = false;
}
@Override
public void setProperty(Property prop, float val) {}
@Override
public void onPropertyAnimationFinished(Property prop) {
if (prop == Property.FAST_ANIMATION) {
mHasFinishedFastAnimation = true;
} else if (prop == Property.SLOW_ANIMATION) {
mHasFinishedSlowAnimation = true;
}
}
/**
* Creates an {@link org.chromium.chrome.browser.compositor.layouts.ChromeAnimation.Animatable}
* and adds it to the animation.
* Automatically sets the start value at the beginning of the animation.
*
* @param <T> The Enum type of the Property being used
* @param object The object being animated
* @param prop The property being animated
* @param start The starting value of the animation
* @param end The ending value of the animation
* @param duration The duration of the animation in ms
* @param startTime The start time in ms
*/
private <T extends Enum<?>> void addToAnimation(Animatable<T> object, T prop,
float start, float end, long duration, long startTime) {
ChromeAnimation.Animation<Animatable<?>> component = createAnimation(
object, prop, start, end, duration, startTime, false,
ChromeAnimation.getDecelerateInterpolator());
addToAnimation(component);
}
/**
* Appends an Animation to the current animation set and starts it immediately. If the set is
* already finished or doesn't exist, the animation set is also started.
*/
private void addToAnimation(ChromeAnimation.Animation<Animatable<?>> component) {
if (mAnimations == null || mAnimations.finished()) {
mAnimations = new ChromeAnimation<>();
mAnimations.start();
}
component.start();
mAnimations.add(component);
}
@SmallTest
@Feature({"ContextualSearch"})
public void testConcurrentAnimationsFinishSeparately() {
addToAnimation(this, Property.FAST_ANIMATION, 0.f, 1.f, FAST_DURATION, 0);
addToAnimation(this, Property.SLOW_ANIMATION, 0.f, 1.f, SLOW_DURATION, 0);
// Update the animation with the current time. This will internally set the initial
// time of the animation to |now|.
long now = SystemClock.uptimeMillis();
mAnimations.update(now);
// Advances time to check that the fast animation will finish first.
mAnimations.update(now + FAST_DURATION);
assertTrue(mHasFinishedFastAnimation);
assertFalse(mHasFinishedSlowAnimation);
// Advances time to check that all animations are finished.
mAnimations.update(now + SLOW_DURATION);
assertTrue(mHasFinishedFastAnimation);
assertTrue(mHasFinishedSlowAnimation);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment