-
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 view interface for progress callback
- Loading branch information
Showing
9 changed files
with
185 additions
and
54 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
16 changes: 16 additions & 0 deletions
16
library/src/main/java/com/zoonref/viewbehavior/PercentageChildView.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,16 @@ | ||
package com.zoonref.viewbehavior; | ||
|
||
/** | ||
* Created by zoonooz on 15/1/2018 AD. | ||
* Interface for child view to let behavior send the progress | ||
*/ | ||
|
||
public interface PercentageChildView { | ||
|
||
/** | ||
* This will be called on behavior updated. | ||
* @param behavior Changing behavior object | ||
* @param progress 0.0 to 1.0 progress of behavior animation | ||
*/ | ||
void onPercentageBehaviorChange(PercentageViewBehavior behavior, float progress); | ||
} |
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
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
27 changes: 1 addition & 26 deletions
27
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
38 changes: 38 additions & 0 deletions
38
library/src/test/java/com/zoonref/viewbehavior/BehaviorTest.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,38 @@ | ||
package com.zoonref.viewbehavior; | ||
|
||
import android.app.Activity; | ||
import android.support.design.widget.CoordinatorLayout; | ||
import android.view.View; | ||
|
||
import com.zoonref.viewbehavior.view.TestPercentageChildView; | ||
|
||
import org.junit.Before; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
/** | ||
* Created by zoonooz on 15/1/2018 AD. | ||
* Best test class | ||
*/ | ||
@RunWith(RobolectricTestRunner.class) | ||
public abstract class BehaviorTest { | ||
|
||
CoordinatorLayout coordinatorLayout; | ||
View firstView; | ||
TestPercentageChildView 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 TestPercentageChildView(activity); | ||
|
||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200); | ||
coordinatorLayout.addView(firstView, params); | ||
coordinatorLayout.addView(secondView, params); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
library/src/test/java/com/zoonref/viewbehavior/PercentageViewBehaviorTest.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,90 @@ | ||
package com.zoonref.viewbehavior; | ||
|
||
import android.support.design.widget.CoordinatorLayout; | ||
|
||
import org.junit.Test; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
|
||
/** | ||
* Created by zoonooz on 15/1/2018 AD. | ||
* Test {@link PercentageViewBehavior} | ||
*/ | ||
public class PercentageViewBehaviorTest extends BehaviorTest { | ||
|
||
@Test | ||
public void dependsOnX() { | ||
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder() | ||
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_X) | ||
.targetValue(100) | ||
.targetX(100) | ||
.targetY(200) | ||
.build(); | ||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200); | ||
params.setBehavior(behavior); | ||
secondView.setLayoutParams(params); | ||
|
||
// move first view by 50% | ||
firstView.setX(50); | ||
coordinatorLayout.requestLayout(); | ||
assertEquals(0.5f, secondView.getCurrentPercent()); | ||
} | ||
|
||
@Test | ||
public void dependsOnY() { | ||
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder() | ||
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_Y) | ||
.targetValue(100) | ||
.targetX(100) | ||
.targetY(200) | ||
.build(); | ||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200); | ||
params.setBehavior(behavior); | ||
secondView.setLayoutParams(params); | ||
|
||
// move first view by 50% | ||
firstView.setY(50); | ||
coordinatorLayout.requestLayout(); | ||
assertEquals(0.5f, secondView.getCurrentPercent()); | ||
} | ||
|
||
@Test | ||
public void dependsOnWidth() { | ||
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder() | ||
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_WIDTH) | ||
.targetValue(160) | ||
.targetX(100) | ||
.targetY(200) | ||
.build(); | ||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200); | ||
params.setBehavior(behavior); | ||
secondView.setLayoutParams(params); | ||
|
||
// resize first view's width | ||
CoordinatorLayout.LayoutParams resizeParams = new CoordinatorLayout.LayoutParams(firstView.getLayoutParams()); | ||
resizeParams.width = 160; | ||
firstView.setLayoutParams(resizeParams); | ||
coordinatorLayout.requestLayout(); | ||
assertEquals(1.0f, secondView.getCurrentPercent()); | ||
} | ||
|
||
@Test | ||
public void dependsOnHeight() { | ||
SimpleViewBehavior behavior = new SimpleViewBehavior.Builder() | ||
.dependsOn(firstView.getId(), SimpleViewBehavior.DEPEND_TYPE_HEIGHT) | ||
.targetValue(100) | ||
.targetX(100) | ||
.targetY(200) | ||
.build(); | ||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(320, 200); | ||
params.setBehavior(behavior); | ||
secondView.setLayoutParams(params); | ||
|
||
// resize first view's height | ||
CoordinatorLayout.LayoutParams resizeParams = new CoordinatorLayout.LayoutParams(firstView.getLayoutParams()); | ||
resizeParams.height = 100; | ||
firstView.setLayoutParams(resizeParams); | ||
coordinatorLayout.requestLayout(); | ||
assertEquals(1.0f, secondView.getCurrentPercent()); | ||
} | ||
} |
27 changes: 1 addition & 26 deletions
27
library/src/test/java/com/zoonref/viewbehavior/SimpleViewBehaviorTest.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
30 changes: 30 additions & 0 deletions
30
library/src/test/java/com/zoonref/viewbehavior/view/TestPercentageChildView.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,30 @@ | ||
package com.zoonref.viewbehavior.view; | ||
|
||
import android.content.Context; | ||
import android.view.View; | ||
|
||
import com.zoonref.viewbehavior.PercentageChildView; | ||
import com.zoonref.viewbehavior.PercentageViewBehavior; | ||
|
||
/** | ||
* Created by zoonooz on 15/1/2018 AD. | ||
* {@link PercentageChildView} implementation for testing | ||
*/ | ||
|
||
public class TestPercentageChildView extends View implements PercentageChildView { | ||
|
||
private float currentPercent; | ||
|
||
public TestPercentageChildView(Context context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
public void onPercentageBehaviorChange(PercentageViewBehavior behavior, float progress) { | ||
currentPercent = progress; | ||
} | ||
|
||
public float getCurrentPercent() { | ||
return currentPercent; | ||
} | ||
} |