Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRODSUP-497 | Android | Changes recommended as part of issue 3767 in … #1

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.content.Context;
import android.support.annotation.RestrictTo;
import android.view.View;
import android.view.ViewGroup;

Expand All @@ -19,7 +20,7 @@
import java.util.List;
import java.util.Map;

import static com.reactnativenavigation.utils.CollectionUtils.merge;
import static com.reactnativenavigation.utils.CollectionUtils.*;

@SuppressWarnings("ResourceType")
public class NavigationAnimator extends BaseAnimator {
Expand Down Expand Up @@ -69,6 +70,20 @@ public void onAnimationEnd(Animator animation) {
set.start();
}

public void cancelPushAnimations() {
for (View view : runningPushAnimations.keySet()) {
runningPushAnimations.get(view).cancel();
runningPushAnimations.remove(view);
}
}

@RestrictTo(RestrictTo.Scope.TESTS)
public void endPushAnimation(View view) {
if (runningPushAnimations.containsKey(view)) {
runningPushAnimations.get(view).end();
}
}

public void pop(View view, NestedAnimationsOptions pop, Runnable onAnimationEnd) {
if (runningPushAnimations.containsKey(view)) {
runningPushAnimations.get(view).cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ private void addChildToStack(ViewController child, View view, Options resolvedOp
}

public void setRoot(List<ViewController> children, CommandListener listener) {
animator.cancelPushAnimations();
if (children.size() == 1) {
backButtonHelper.clear(CollectionUtils.last(children));
push(CollectionUtils.last(children), new CommandListenerAdapter() {
Expand Down Expand Up @@ -275,7 +276,7 @@ public void popTo(ViewController viewController, Options mergeOptions, CommandLi
return;
}


animator.cancelPushAnimations();
String currentControlId;
for (int i = stack.size() - 2; i >= 0; i--) {
currentControlId = stack.get(i).getId();
Expand All @@ -297,6 +298,7 @@ public void popToRoot(Options mergeOptions, CommandListener listener) {
return;
}

animator.cancelPushAnimations();
Iterator<String> iterator = stack.iterator();
iterator.next();
while (stack.size() > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.*;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -283,6 +281,18 @@ public void onSuccess(String childId) {
});
}

@Test
public void setRoot_doesNotCrashWhenCalledInQuickSuccession() {
disablePushAnimation(child1);
uut.setRoot(Collections.singletonList(child1), new CommandListenerAdapter());
uut.setRoot(Collections.singletonList(child2), new CommandListenerAdapter());
uut.setRoot(Collections.singletonList(child3), new CommandListenerAdapter());
animator.endPushAnimation(child2.getView());
animator.endPushAnimation(child3.getView());

assertContainsOnlyId(child3.getId());
}

@Test
public synchronized void pop() {
disablePushAnimation(child1, child2);
Expand All @@ -297,6 +307,18 @@ public void onSuccess(String childId) {
});
}

@Test
public void pop_screenCurrentlyBeingPushedIsPopped() {
disablePushAnimation(child1, child2);
uut.push(child1, Mockito.mock(CommandListenerAdapter.class));
uut.push(child2, Mockito.mock(CommandListenerAdapter.class));

uut.push(child3, Mockito.mock(CommandListenerAdapter.class));
uut.pop(Options.EMPTY, Mockito.mock(CommandListenerAdapter.class));
assertThat(uut.size()).isEqualTo(2);
assertContainsOnlyId(child1.getId(), child2.getId());
}

@Test
public void pop_appliesOptionsAfterPop() {
uut.push(child1, new CommandListenerAdapter());
Expand Down Expand Up @@ -622,6 +644,19 @@ public void onSuccess(String childId) {
});
}

@Test
public void popTo_pushAnimationIsCancelled() {
disablePushAnimation(child1, child2);
uut.push(child1, Mockito.mock(CommandListenerAdapter.class));
uut.push(child2, Mockito.mock(CommandListenerAdapter.class));

ViewGroup pushed = child3.getView();
uut.push(child3, Mockito.mock(CommandListenerAdapter.class));
uut.popTo(child1, Options.EMPTY, Mockito.mock(CommandListenerAdapter.class));
animator.endPushAnimation(pushed);
assertContainsOnlyId(child1.getId());
}

@Test
public void popToRoot_PopsEverythingAboveFirstController() {
child1.options.animations.push.enabled = new Bool(false);
Expand Down Expand Up @@ -707,6 +742,19 @@ public void popToRoot_optionsAreMergedOnTopChild() {
verify(child1, times(0)).mergeOptions(mergeOptions);
}

@Test
public void popToRoot_screenPushedBeforePopAnimationCompletesIsPopped() {
disablePushAnimation(child1, child2);
uut.push(child1, Mockito.mock(CommandListenerAdapter.class));
uut.push(child2, Mockito.mock(CommandListenerAdapter.class));

ViewGroup pushed = child3.getView();
uut.push(child3, Mockito.mock(CommandListenerAdapter.class));
uut.popToRoot(Options.EMPTY, Mockito.mock(CommandListenerAdapter.class));
animator.endPushAnimation(pushed);
assertContainsOnlyId(child1.getId());
}

@Test
public void findControllerById_ReturnsSelfOrChildrenById() {
assertThat(uut.findController("123")).isNull();
Expand Down