This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from square/ray/back-from-up
Ray/back from up
- Loading branch information
Showing
2 changed files
with
205 additions
and
1 deletion.
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
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,204 @@ | ||
/* | ||
* Copyright 2013 Square Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package flow; | ||
|
||
import java.util.Arrays; | ||
import org.junit.Test; | ||
|
||
import static org.fest.assertions.api.Assertions.assertThat; | ||
|
||
public class FlowTest { | ||
static class Uno { | ||
} | ||
|
||
static class Dos implements HasParent<Uno> { | ||
@Override public Uno getParent() { | ||
return new Uno(); | ||
} | ||
} | ||
|
||
static class Tres implements HasParent<Dos> { | ||
@Override public Dos getParent() { | ||
return new Dos(); | ||
} | ||
} | ||
|
||
Backstack lastStack; | ||
Flow.Direction lastDirection; | ||
|
||
class FlowListener implements Flow.Listener { | ||
@Override public void go(Backstack backstack, Flow.Direction direction) { | ||
lastStack = backstack; | ||
lastDirection = direction; | ||
} | ||
} | ||
|
||
@Test public void oneTwoThree() { | ||
Backstack backstack = Backstack.single(new Uno()); | ||
Flow flow = new Flow(backstack, new FlowListener()); | ||
|
||
flow.goTo(new Dos()); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Dos.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.FORWARD); | ||
|
||
flow.goTo(new Tres()); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Tres.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.FORWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Dos.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Uno.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isFalse(); | ||
} | ||
|
||
@Test public void noUpNoUps() { | ||
Backstack backstack = Backstack.single(new Uno()); | ||
Flow flow = new Flow(backstack, new FlowListener()); | ||
assertThat(flow.goUp()).isFalse(); | ||
assertThat(lastStack).isNull(); | ||
assertThat(lastDirection).isNull(); | ||
} | ||
|
||
@Test public void upAndDown() { | ||
Backstack backstack = Backstack.single(new Tres()); | ||
Flow flow = new Flow(backstack, new FlowListener()); | ||
|
||
assertThat(flow.goBack()).isFalse(); | ||
|
||
assertThat(flow.goUp()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Dos.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goUp()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Uno.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goUp()).isFalse(); | ||
} | ||
|
||
@Test public void backStackAddAllIsPushy() { | ||
Backstack backstack = | ||
Backstack.emptyBuilder().addAll(Arrays.<Object>asList("Able", "Baker", "Charlie")).build(); | ||
assertThat(backstack.size()).isEqualTo(3); | ||
|
||
Flow flow = new Flow(backstack, new FlowListener()); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isEqualTo("Baker"); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isEqualTo("Able"); | ||
|
||
assertThat(flow.goBack()).isFalse(); | ||
} | ||
|
||
@Test public void replaceBuildsBackStackFromUpLinks() { | ||
Backstack backstack = Backstack.emptyBuilder() | ||
.addAll(Arrays.<Object>asList("Able", "Baker", "Charlie", "Delta")) | ||
.build(); | ||
Flow flow = new Flow(backstack, new FlowListener()); | ||
|
||
flow.replaceTo(new Tres()); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Tres.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Dos.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isInstanceOf(Uno.class); | ||
assertThat(lastDirection).isSameAs(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isFalse(); | ||
} | ||
|
||
@Test public void resetGoesBack() { | ||
Backstack backstack = Backstack.emptyBuilder() | ||
.addAll(Arrays.<Object>asList("Able", "Baker", "Charlie", "Delta")) | ||
.build(); | ||
Flow flow = new Flow(backstack, new FlowListener()); | ||
|
||
assertThat(backstack.size()).isEqualTo(4); | ||
|
||
flow.resetTo("Charlie"); | ||
assertThat(lastStack.current().getScreen()).isEqualTo("Charlie"); | ||
assertThat(lastStack.size()).isEqualTo(3); | ||
assertThat(lastDirection).isEqualTo(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isEqualTo("Baker"); | ||
assertThat(lastDirection).isEqualTo(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isEqualTo("Able"); | ||
assertThat(lastDirection).isEqualTo(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isFalse(); | ||
} | ||
|
||
static class Picky { | ||
final String value; | ||
|
||
Picky(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Picky picky = (Picky) o; | ||
return value.equals(picky.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
} | ||
|
||
@Test public void resetCallsEquals() { | ||
Backstack backstack = Backstack.emptyBuilder() | ||
.addAll(Arrays.<Object>asList(new Picky("Able"), new Picky("Baker"), new Picky("Charlie"), | ||
new Picky("Delta"))) | ||
.build(); | ||
Flow flow = new Flow(backstack, new FlowListener()); | ||
|
||
assertThat(backstack.size()).isEqualTo(4); | ||
|
||
flow.resetTo(new Picky("Charlie")); | ||
assertThat(lastStack.current().getScreen()).isEqualTo(new Picky("Charlie")); | ||
assertThat(lastStack.size()).isEqualTo(3); | ||
assertThat(lastDirection).isEqualTo(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isEqualTo(new Picky("Baker")); | ||
assertThat(lastDirection).isEqualTo(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isTrue(); | ||
assertThat(lastStack.current().getScreen()).isEqualTo(new Picky("Able")); | ||
assertThat(lastDirection).isEqualTo(Flow.Direction.BACKWARD); | ||
|
||
assertThat(flow.goBack()).isFalse(); | ||
} | ||
} |