Skip to content

Commit c2c6343

Browse files
author
Hussein Aladeen
committed
test(project): Add more deep linking tests
1 parent 8d9dcef commit c2c6343

File tree

12 files changed

+201
-17
lines changed

12 files changed

+201
-17
lines changed

extra/gradle/libraries.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ ext {
2828
hamcrestVersion = '1.4-atlassian-1'
2929
espressoVersion = '2.2.2'
3030
mockitoVersion = '1.10.19'
31+
compileTestingVersion = '0.10'
32+
googleTruthVersion = '0.30'
3133

3234
javaPoetVersion = '1.8.0'
3335
autoCommon = '0.8'
@@ -54,7 +56,9 @@ ext {
5456
testRule : "com.android.support.test:rules:${tesetRunnerVersion}",
5557
hamcrest : "org.hamcrest:hamcrest-library:${hamcrestVersion}",
5658
espresso : "com.android.support.test.espresso:espresso-core:${espressoVersion}",
57-
mockito : "org.mockito:mockito-core:${mockitoVersion}"
59+
mockito : "org.mockito:mockito-core:${mockitoVersion}",
60+
compileTesting : "com.google.testing.compile:compile-testing:${compileTestingVersion}",
61+
googleTruth : "com.google.truth:truth:${googleTruthVersion}"
5862
]
5963

6064
}

flowr-annotations/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
apply plugin: 'java'
22
apply plugin: 'com.github.dcendents.android-maven'
3+
apply plugin: 'checkstyle'
4+
apply from: "../extra/checkstyle/checkstyle_java_library.gradle"
35

46
group = libraryGroup
57
version = libraryVersion

flowr-compiler/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import org.gradle.internal.jvm.Jvm
2+
13
apply plugin: 'java'
24
apply plugin: 'com.github.dcendents.android-maven'
5+
apply plugin: 'checkstyle'
6+
apply from: "../extra/checkstyle/checkstyle_java_library.gradle"
37

48
group = libraryGroup
59
version = libraryVersion
@@ -12,9 +16,14 @@ dependencies {
1216
compile libraries.autoCommon
1317
compile libraries.autoService
1418

19+
testCompile project(':flowr-mock-classes')
20+
testCompile testLibraries.junit
1521
testCompile testLibraries.hamcrest
1622
testCompile testLibraries.junit
1723
testCompile testLibraries.mockito
24+
testCompile testLibraries.compileTesting
25+
testCompile testLibraries.googleTruth
26+
testCompile files(Jvm.current().getToolsJar())
1827
}
1928

2029

flowr-compiler/src/main/java/com/fueled/flowr/compilers/DeepLinkAnnotationCompiler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import javax.annotation.processing.Processor;
1616
import javax.annotation.processing.RoundEnvironment;
1717
import javax.annotation.processing.SupportedAnnotationTypes;
18+
import javax.annotation.processing.SupportedSourceVersion;
19+
import javax.lang.model.SourceVersion;
1820
import javax.lang.model.element.Element;
1921
import javax.lang.model.element.Modifier;
2022
import javax.lang.model.element.TypeElement;
@@ -26,6 +28,7 @@
2628
*/
2729
@SupportedAnnotationTypes({"com.fueled.flowr.annotations.DeepLink",
2830
"com.fueled.flowr.annotations.DeepLinkHandler"})
31+
@SupportedSourceVersion(SourceVersion.RELEASE_7)
2932
@AutoService(Processor.class)
3033
public class DeepLinkAnnotationCompiler extends AbstractProcessor {
3134

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.fueled.flowr.compilers;
22

3-
import org.junit.Before;
3+
import com.google.testing.compile.JavaFileObjects;
4+
45
import org.junit.Test;
56

6-
import java.util.ArrayList;
7-
import java.util.List;
7+
import java.util.Arrays;
8+
9+
import javax.tools.JavaFileObject;
810

9-
import static org.junit.Assert.assertEquals;
11+
import static com.google.common.truth.Truth.assertAbout;
12+
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
1013

1114
/**
1215
* Created by julienFueled on 5/25/17.
@@ -15,16 +18,39 @@ public class DeepLinkAnnotationCompilerTest {
1518

1619
private static final String TEST_PACKAGE = "com.fueled.flowr.sample";
1720

18-
private DeepLinkAnnotationCompiler compiler;
21+
private static final JavaFileObject TEST_DEEP_LINK_FRAGMENT = JavaFileObjects
22+
.forSourceString("HomeFragment", "package " + TEST_PACKAGE + ";\n" +
23+
"import com.fueled.flowr.AbstractFlowrFragment;\n" +
24+
"import com.fueled.flowr.annotations.DeepLink;\n" +
25+
"@DeepLink(value = {\"/test\"})\n" +
26+
"public class HomeFragment extends AbstractFlowrFragment {\n" +
27+
"}");
28+
29+
private static final JavaFileObject TEST_DEEP_LINK_HANDLER = JavaFileObjects
30+
.forSourceString("TestDeepLinkHandler", "package " + TEST_PACKAGE + ";\n" +
31+
"import com.fueled.flowr.annotations.DeepLinkHandler;\n" +
32+
"@DeepLinkHandler\n" +
33+
"public interface TestDeepLinkHandler {\n" +
34+
"}");
35+
36+
private static final JavaFileObject TEST_DEEP_GENERATED_HANDLER = JavaFileObjects
37+
.forSourceString("TestDeepLinkHandlerImpl", "package com.fueled.flowr.sample;\n" +
38+
"import com.fueled.flowr.internal.AbstractFlowrDeepLinkHandler;\n" +
39+
"public final class TestDeepLinkHandlerImpl extends AbstractFlowrDeepLinkHandler {\n" +
40+
" public TestDeepLinkHandlerImpl() {\n" +
41+
" addFragment(\"/test\", HomeFragment.class);\n" +
42+
" }\n" +
43+
"}");
1944

20-
@Before
21-
public void setup() {
22-
compiler = new DeepLinkAnnotationCompiler();
23-
}
2445

2546
@Test
2647
public void process() throws Exception {
27-
48+
assertAbout(javaSources())
49+
.that(Arrays.asList(TEST_DEEP_LINK_FRAGMENT, TEST_DEEP_LINK_HANDLER))
50+
.processedWith(new DeepLinkAnnotationCompiler())
51+
.compilesWithoutError()
52+
.and()
53+
.generatesSources(TEST_DEEP_GENERATED_HANDLER);
2854
}
2955

3056
}

flowr-mock-classes/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apply plugin: 'java'
2+
3+
dependencies {
4+
compile fileTree(dir: 'libs', include: ['*.jar'])
5+
}
6+
7+
sourceCompatibility = "1.7"
8+
targetCompatibility = "1.7"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.fueled.flowr;
2+
3+
/**
4+
* Mock AbstractFlowrFragment class used for compiler tests.
5+
*/
6+
public class AbstractFlowrFragment {
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.fueled.flowr.internal;
2+
3+
/**
4+
* Mock AbstractFlowrDeepLinkHandler class used for compiler tests.
5+
*/
6+
public class AbstractFlowrDeepLinkHandler {
7+
8+
protected void addFragment(String url, Class clazz) {
9+
10+
}
11+
12+
}

flowr/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ dependencies {
3232
// Test
3333
testCompile testLibraries.hamcrest
3434
testCompile testLibraries.junit
35+
testCompile testLibraries.mockito
3536
}

flowr/src/main/java/com/fueled/flowr/internal/AbstractFlowrDeepLinkHandler.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Bundle;
66
import android.support.annotation.NonNull;
77
import android.support.annotation.Nullable;
8+
import android.support.annotation.VisibleForTesting;
89
import android.support.v4.app.Fragment;
910

1011
import com.fueled.flowr.Flowr;
@@ -52,13 +53,13 @@ protected void addFragment(String url, Class<? extends T> fragment) {
5253
* @return a bundle containing all path variables and parameters as strings.
5354
*/
5455
@Nullable
55-
private static Bundle bundleUriInfo(Uri uri, String pattern) {
56+
private Bundle bundleUriInfo(Uri uri, String pattern) {
5657
String regex = getRegexPattern(pattern);
5758
String path = uri.getPath();
5859
Matcher m = Pattern.compile(regex).matcher(path);
5960

6061
if (m.matches()) {
61-
Bundle data = new Bundle();
62+
Bundle data = getNewBundle();
6263
data.putString(Flowr.DEEP_LINK_URL, uri.toString());
6364
Iterator<String> params = getNamedGroupCandidates(regex).iterator();
6465

@@ -76,11 +77,11 @@ private static Bundle bundleUriInfo(Uri uri, String pattern) {
7677
return null;
7778
}
7879

79-
private static String getRegexPattern(String pattern) {
80+
private String getRegexPattern(String pattern) {
8081
return pattern.replaceAll("\\{(.+?)\\}", "(?<$1>\\.+?)");
8182
}
8283

83-
private static List<String> getNamedGroupCandidates(String regex) {
84+
private List<String> getNamedGroupCandidates(String regex) {
8485
List<String> namedGroups = new ArrayList<>();
8586

8687
Matcher m = NAMED_PARAM_PATTERN.matcher(regex);
@@ -93,11 +94,16 @@ private static List<String> getNamedGroupCandidates(String regex) {
9394
return namedGroups;
9495
}
9596

97+
@VisibleForTesting
98+
protected Bundle getNewBundle() {
99+
return new Bundle();
100+
}
101+
96102
/**
97103
* @inheritDoc
98104
*/
99105
@Nullable
100-
public FlowrDeepLinkInfo getDeepLinkInfoForIntent(@NonNull Intent intent) {
106+
public FlowrDeepLinkInfo<T> getDeepLinkInfoForIntent(@NonNull Intent intent) {
101107
Uri uri = intent.getData();
102108

103109
if (uri != null) {

0 commit comments

Comments
 (0)