-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit test and builder for animation behavior
- Loading branch information
Showing
3 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
library/src/test/java/com/zoonref/viewbehavior/AnimationViewBehaviorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.zoonref.viewbehavior; | ||
|
||
import android.app.Activity; | ||
import android.support.design.widget.CoordinatorLayout; | ||
import android.view.View; | ||
import android.view.animation.ScaleAnimation; | ||
|
||
import com.zoonref.viewbehavior.shadow.ShadowAnimation; | ||
|
||
import static junit.framework.Assert.*; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.shadow.api.Shadow; | ||
|
||
/** | ||
* Created by zoonooz on 6/1/2018 AD. | ||
* Test {@link AnimationViewBehavior} | ||
*/ | ||
@RunWith(RobolectricTestRunner.class) | ||
@Config(shadows = {ShadowAnimation.class}) | ||
public class AnimationViewBehaviorTest { | ||
|
||
private CoordinatorLayout coordinatorLayout; | ||
private View firstView; | ||
private View secondView; | ||
|
||
@Before | ||
public void setup() { | ||
Activity activity = Robolectric.setupActivity(Activity.class); | ||
activity.setTheme(R.style.Theme_AppCompat); | ||
coordinatorLayout = new CoordinatorLayout(activity); | ||
activity.setContentView(coordinatorLayout); | ||
firstView = new View(activity); | ||
secondView = new View(activity); | ||
|
||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200); | ||
coordinatorLayout.addView(firstView, params); | ||
coordinatorLayout.addView(secondView, params); | ||
} | ||
|
||
@Test | ||
public void animations() { | ||
ScaleAnimation animation = new ScaleAnimation(1f, 0f, 1f, 0f); | ||
AnimationViewBehavior behavior = new AnimationViewBehavior.Builder() | ||
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_Y) | ||
.targetValue(100) | ||
.animation(animation) | ||
.build(); | ||
|
||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200); | ||
params.setBehavior(behavior); | ||
secondView.setLayoutParams(params); | ||
|
||
ShadowAnimation shadowAnimation = Shadow.extract(animation); | ||
|
||
firstView.setY(50); | ||
coordinatorLayout.requestLayout(); | ||
assertEquals(500L, shadowAnimation.getLastTimeGetTransform()); | ||
|
||
firstView.setY(100); | ||
coordinatorLayout.requestLayout(); | ||
assertEquals(1000L, shadowAnimation.getLastTimeGetTransform()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
library/src/test/java/com/zoonref/viewbehavior/shadow/ShadowAnimation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.zoonref.viewbehavior.shadow; | ||
|
||
import android.view.animation.Animation; | ||
import android.view.animation.Transformation; | ||
|
||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
import org.robolectric.annotation.RealObject; | ||
import org.robolectric.shadow.api.Shadow; | ||
|
||
/** | ||
* Created by zoonooz on 6/1/2018 AD. | ||
* Custom Robolectric shadow for Animation | ||
*/ | ||
@Implements(Animation.class) | ||
public class ShadowAnimation { | ||
|
||
@RealObject | ||
private Animation realAnimation; | ||
|
||
// save the time of the last getTransformation() for test | ||
private long lastTimeGetTransform; | ||
|
||
@Implementation | ||
public boolean getTransformation(long currentTime, Transformation outTransformation) { | ||
lastTimeGetTransform = currentTime; | ||
return Shadow.directlyOn(realAnimation, Animation.class).getTransformation(currentTime, outTransformation); | ||
} | ||
|
||
public long getLastTimeGetTransform() { | ||
return lastTimeGetTransform; | ||
} | ||
} |