Skip to content

Commit d4a7864

Browse files
author
Devota Aabel
committed
Added tests and cleanup
1 parent 064abd2 commit d4a7864

File tree

5 files changed

+74
-43
lines changed

5 files changed

+74
-43
lines changed

libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/AbbreviationCreator.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ class AbbreviationCreator extends NodeCreator<AbbreviationCreator.AbbreviationNo
2121
private Map<Integer, List<Integer>> abbreviations;
2222
private TextViewUtils textViewUtils;
2323

24-
AbbreviationCreator(TextViewUtils textViewUtils, AbbreviationVerifier abbreviationVerifier) {
24+
AbbreviationCreator(AbbreviationVerifier abbreviationVerifier, TextViewUtils textViewUtils,
25+
HashMap abbreviations) {
2526
super(abbreviationVerifier);
26-
this.abbreviations = new HashMap<>();
2727
this.textViewUtils = textViewUtils;
28+
this.abbreviations = abbreviations;
29+
}
30+
31+
AbbreviationCreator(AbbreviationVerifier abbreviationVerifier, TextViewUtils textViewUtils) {
32+
this(abbreviationVerifier, textViewUtils, new HashMap());
2833
}
2934

3035
AbbreviationCreator() {
31-
this(new TextViewUtils(), new AbbreviationVerifier());
36+
this(new AbbreviationVerifier(), new TextViewUtils());
3237
}
3338

3439
/**
@@ -39,7 +44,7 @@ class AbbreviationCreator extends NodeCreator<AbbreviationCreator.AbbreviationNo
3944
* @param bannerComponents object holding the abbreviation information
4045
* @param index in the list of BannerComponentNodes
4146
*/
42-
void addPriorityInfo(BannerComponents bannerComponents, int index) {
47+
private void addPriorityInfo(BannerComponents bannerComponents, int index) {
4348
Integer abbreviationPriority = bannerComponents.abbreviationPriority();
4449
if (abbreviations.get(abbreviationPriority) == null) {
4550
abbreviations.put(abbreviationPriority, new ArrayList<Integer>());
@@ -55,7 +60,8 @@ void addPriorityInfo(BannerComponents bannerComponents, int index) {
5560
* @param bannerComponentNodes containing the text to construct
5661
* @return the properly abbreviated string that will fit in the TextView
5762
*/
58-
String abbreviateBannerText(TextView textView, List<BannerComponentNode> bannerComponentNodes) {
63+
private String abbreviateBannerText(TextView textView, List<BannerComponentNode>
64+
bannerComponentNodes) {
5965
String bannerText = join(bannerComponentNodes);
6066

6167
if (abbreviations.isEmpty()) {
@@ -146,7 +152,7 @@ void preProcess(TextView textView, List<BannerComponentNode> bannerComponentNode
146152
* Class used by InstructionLoader to determine that a BannerComponent contains an abbreviation
147153
*/
148154
static class AbbreviationNode extends BannerComponentNode {
149-
boolean abbreviate;
155+
private boolean abbreviate;
150156

151157
AbbreviationNode(BannerComponents bannerComponents, int startIndex) {
152158
super(bannerComponents, startIndex);
@@ -160,5 +166,10 @@ public String toString() {
160166
void setAbbreviate(boolean abbreviate) {
161167
this.abbreviate = abbreviate;
162168
}
169+
170+
boolean getAbbreviate() {
171+
return abbreviate;
172+
}
173+
163174
}
164175
}

libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/BannerComponentTree.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class BannerComponentTree {
2929
* @param bannerComponents to parse
3030
* @return the list of nodes representing the bannerComponents
3131
*/
32-
List<BannerComponentNode> parseBannerComponents(List<BannerComponents> bannerComponents) {
32+
private List<BannerComponentNode> parseBannerComponents(List<BannerComponents> bannerComponents) {
3333
int length = 0;
3434
List<BannerComponentNode> bannerComponentNodes = new ArrayList<>();
3535

@@ -38,7 +38,7 @@ List<BannerComponentNode> parseBannerComponents(List<BannerComponents> bannerCom
3838

3939
for (NodeCreator nodeCreator : nodeCreators) {
4040
if (nodeCreator.isNodeType(components)) {
41-
node = nodeCreator.setupNode(components, bannerComponentNodes.size(), length - 1);
41+
node = nodeCreator.setupNode(components, bannerComponentNodes.size(), length);
4242
break;
4343
}
4444
}
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
import org.junit.Test;
99

1010
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.HashMap;
1113
import java.util.List;
1214

1315
import static org.junit.Assert.assertEquals;
1416
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.verify;
1518
import static org.mockito.Mockito.when;
1619

17-
public class AbbreviationCoordinatorTest extends BaseTest {
20+
public class AbbreviationCreatorTest extends BaseTest {
21+
1822
@Test
19-
public void onAbbreviateBannerText_textIsAbbreviated() {
23+
public void preProcess_abbreviate() {
2024
String abbreviation = "smtxt";
2125
BannerComponents bannerComponents =
22-
BannerComponentsFaker.bannerComponents()
26+
BannerComponentsFaker.bannerComponentsBuilder()
2327
.abbreviation(abbreviation)
2428
.abbreviationPriority(0)
2529
.build();
@@ -29,38 +33,40 @@ public void onAbbreviateBannerText_textIsAbbreviated() {
2933
when(abbreviationVerifier.isNodeType(bannerComponents)).thenReturn(true);
3034
when(textViewUtils.textFits(textView, abbreviation)).thenReturn(true);
3135
when(textViewUtils.textFits(textView, bannerComponents.text())).thenReturn(false);
32-
AbbreviationCreator abbreviationCoordinator = new AbbreviationCreator(textViewUtils, abbreviationVerifier);
33-
abbreviationCoordinator.addPriorityInfo(bannerComponents, 0);
34-
List<BannerComponentNode> bannerComponentNodes = new ArrayList<>();
35-
bannerComponentNodes.add(new AbbreviationCreator.AbbreviationNode(bannerComponents, 0));
36+
BannerComponentNode node = mock(AbbreviationCreator.AbbreviationNode.class);
37+
when(((AbbreviationCreator.AbbreviationNode) node).getAbbreviate()).thenReturn(true);
38+
when(node.toString()).thenReturn(abbreviation);
39+
AbbreviationCreator abbreviationCreator = new AbbreviationCreator(abbreviationVerifier, textViewUtils);
3640

37-
String abbreviatedTextFromCoordinator = abbreviationCoordinator.abbreviateBannerText(textView, bannerComponentNodes);
41+
abbreviationCreator.preProcess(textView, Collections.singletonList(node));
3842

39-
assertEquals(abbreviation, abbreviatedTextFromCoordinator);
43+
verify(textView).setText(abbreviation);
4044
}
4145

4246
@Test
43-
public void onAbbreviateBannerText_textIsNotAbbreviated() {
47+
public void setupNode() {
4448
String abbreviation = "smtxt";
45-
String text = "some text";
49+
int abbreviationPriority = 0;
4650
BannerComponents bannerComponents =
47-
BannerComponentsFaker.bannerComponents()
51+
BannerComponentsFaker.bannerComponentsBuilder()
4852
.abbreviation(abbreviation)
49-
.abbreviationPriority(0)
50-
.text(text)
53+
.abbreviationPriority(abbreviationPriority)
5154
.build();
5255
TextViewUtils textViewUtils = mock(TextViewUtils.class);
5356
TextView textView = mock(TextView.class);
5457
AbbreviationVerifier abbreviationVerifier = mock(AbbreviationVerifier.class);
5558
when(abbreviationVerifier.isNodeType(bannerComponents)).thenReturn(true);
56-
when(textViewUtils.textFits(textView, bannerComponents.text())).thenReturn(true);
57-
AbbreviationCreator abbreviationCoordinator = new AbbreviationCreator(textViewUtils, abbreviationVerifier);
58-
abbreviationCoordinator.addPriorityInfo(bannerComponents, 0);
59+
when(textViewUtils.textFits(textView, abbreviation)).thenReturn(true);
60+
when(textViewUtils.textFits(textView, bannerComponents.text())).thenReturn(false);
61+
HashMap<Integer, List<Integer>> abbreviations = new HashMap();
62+
AbbreviationCreator abbreviationCreator = new AbbreviationCreator(abbreviationVerifier,
63+
textViewUtils, abbreviations);
5964
List<BannerComponentNode> bannerComponentNodes = new ArrayList<>();
6065
bannerComponentNodes.add(new AbbreviationCreator.AbbreviationNode(bannerComponents, 0));
6166

62-
String abbreviatedTextFromCoordinator = abbreviationCoordinator.abbreviateBannerText(textView, bannerComponentNodes);
67+
abbreviationCreator.setupNode(bannerComponents, 0, 0);
6368

64-
assertEquals(text, abbreviatedTextFromCoordinator);
69+
assertEquals(abbreviations.size(), 1);
70+
assertEquals(abbreviations.get(abbreviationPriority).get(0), Integer.valueOf(0));
6571
}
6672
}

libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/instruction/BannerComponentTreeTest.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,46 @@
77
import org.junit.Test;
88
import org.mockito.InOrder;
99

10-
import java.util.ArrayList;
10+
import java.util.Collections;
1111
import java.util.List;
1212

1313
import static org.mockito.ArgumentMatchers.any;
14-
import static org.mockito.ArgumentMatchers.anyInt;
1514
import static org.mockito.Mockito.inOrder;
1615
import static org.mockito.Mockito.mock;
16+
import static org.mockito.Mockito.verify;
1717
import static org.mockito.Mockito.when;
1818

1919

2020
public class BannerComponentTreeTest {
2121

22+
@Test
23+
public void parseComponents() {
24+
BannerComponents bannerComponents = BannerComponentsFaker.bannerComponents();
25+
List<BannerComponents> bannerComponentsList = Collections.singletonList(bannerComponents);
26+
TestCreator testCreator = mock(TestCreator.class);
27+
when(testCreator.isNodeType(bannerComponents)).thenReturn(true);
28+
29+
new BannerComponentTree(bannerComponentsList, testCreator);
30+
31+
verify(testCreator).setupNode(bannerComponents, 0, 0);
32+
}
33+
2234
@Test
2335
public void loadInstruction() {
24-
List<BannerComponents> bannerComponentsList = new ArrayList<>();
25-
BannerComponents bannerComponents = BannerComponentsFaker.bannerComponentsWithAbbreviation();
26-
bannerComponentsList.add(bannerComponents);
36+
BannerComponents bannerComponents = BannerComponentsFaker.bannerComponents();
37+
List<BannerComponents> bannerComponentsList = Collections.singletonList(bannerComponents);
2738
TestNode testNode = mock(TestNode.class);
28-
TestCreator nodeCoordinator = mock(TestCreator.class);
29-
when(nodeCoordinator.isNodeType(bannerComponents)).thenReturn(true);
30-
when(nodeCoordinator.setupNode(any(BannerComponents.class), anyInt(), anyInt())).thenReturn(testNode);
39+
TestCreator testCreator = mock(TestCreator.class);
40+
when(testCreator.isNodeType(bannerComponents)).thenReturn(true);
41+
when(testCreator.setupNode(bannerComponents, 0, 0)).thenReturn(testNode);
3142
TextView textView = mock(TextView.class);
32-
BannerComponentTree bannerComponentTree = new BannerComponentTree(bannerComponentsList, nodeCoordinator);
43+
BannerComponentTree bannerComponentTree = new BannerComponentTree(bannerComponentsList, testCreator);
44+
3345
bannerComponentTree.loadInstruction(textView);
3446

35-
InOrder inOrder = inOrder(nodeCoordinator, nodeCoordinator);
36-
inOrder.verify(nodeCoordinator).preProcess(any(TextView.class), any(List.class));
37-
inOrder.verify(nodeCoordinator).postProcess(any(TextView.class), any(List.class));
47+
InOrder inOrder = inOrder(testCreator, testCreator);
48+
inOrder.verify(testCreator).preProcess(any(TextView.class), any(List.class));
49+
inOrder.verify(testCreator).postProcess(any(TextView.class), any(List.class));
3850
}
3951

4052
class TestNode extends BannerComponentNode {
@@ -51,7 +63,6 @@ boolean isNodeType(BannerComponents bannerComponents) {
5163
}
5264
}
5365

54-
5566
class TestCreator extends NodeCreator<TestNode, TestVerifier> {
5667
TestCreator(TestVerifier nodeVerifier) {
5768
super(nodeVerifier);
@@ -62,5 +73,4 @@ TestNode setupNode(BannerComponents components, int index, int startIndex) {
6273
return new TestNode(components, startIndex);
6374
}
6475
}
65-
6676
}

libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/instruction/BannerComponentsFaker.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
import com.mapbox.api.directions.v5.models.BannerComponents;
44

55
class BannerComponentsFaker {
6-
static BannerComponents.Builder bannerComponents() {
6+
static BannerComponents bannerComponents() {
7+
return bannerComponentsBuilder().build();
8+
}
9+
10+
static BannerComponents.Builder bannerComponentsBuilder() {
711
return BannerComponents.builder()
812
.type("some type")
913
.text("some text");
1014
}
1115

1216
static BannerComponents bannerComponentsWithAbbreviation() {
13-
return bannerComponents()
17+
return bannerComponentsBuilder()
1418
.abbreviationPriority(1)
1519
.abbreviation("abbreviation text")
1620
.build();

0 commit comments

Comments
 (0)