Skip to content

Commit 3b1ab57

Browse files
committed
Add unit test for Activity's Intent generation with Fragment
1 parent 2ee251d commit 3b1ab57

7 files changed

Lines changed: 209 additions & 13 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.generation;
17+
18+
import android.app.Activity;
19+
20+
import org.androidannotations.annotations.EActivity;
21+
22+
@EActivity
23+
public class ActivityInManifest extends Activity {
24+
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.androidannotations.generation;
2+
3+
import java.io.File;
4+
5+
import org.androidannotations.AndroidAnnotationProcessor;
6+
import org.androidannotations.utils.AAProcessorTestHelper;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class ActivityIntentFragmentTest extends AAProcessorTestHelper {
11+
12+
private static final String INTENT_FRAGMENT_SIGNATURE = "public static ActivityInManifest_.IntentBuilder_ intent(Fragment fragment)";
13+
14+
@Before
15+
public void setup() {
16+
addProcessor(AndroidAnnotationProcessor.class);
17+
}
18+
19+
@Test
20+
public void activity_intent_minSdkFroyo_compileWithFroyo() {
21+
addManifestProcessorParameter(ActivityIntentFragmentTest.class, "AndroidManifestMinFroyo.xml");
22+
CompileResult result = compileFiles(ActivityInManifest.class);
23+
File generatedFile = toGeneratedFile(ActivityInManifest.class);
24+
25+
assertCompilationSuccessful(result);
26+
assertGeneratedClassDoesntContains(generatedFile, INTENT_FRAGMENT_SIGNATURE);
27+
}
28+
29+
@Test
30+
public void activity_intent_minSdkFroyo_compileWithJB() {
31+
// To simulate comilation with SDK > 11, we add Fragment on classpath
32+
addManifestProcessorParameter(ActivityIntentFragmentTest.class, "AndroidManifestMinFroyo.xml");
33+
CompileResult result = compileFiles(toPath(ActivityIntentFragmentTest.class, "Fragment.java"), ActivityInManifest.class);
34+
File generatedFile = toGeneratedFile(ActivityInManifest.class);
35+
36+
assertCompilationSuccessful(result);
37+
assertGeneratedClassDoesntContains(generatedFile, INTENT_FRAGMENT_SIGNATURE);
38+
}
39+
40+
@Test
41+
public void activity_intent_minSdkJB_compileWithJB() {
42+
// To simulate comilation with SDK > 11, we add Fragment on classpath
43+
addManifestProcessorParameter(ActivityIntentFragmentTest.class, "AndroidManifestMinJB.xml");
44+
CompileResult result = compileFiles(toPath(ActivityIntentFragmentTest.class, "Fragment.java"), ActivityInManifest.class);
45+
File generatedFile = toGeneratedFile(ActivityInManifest.class);
46+
47+
assertCompilationSuccessful(result);
48+
assertGeneratedClassContains(generatedFile, INTENT_FRAGMENT_SIGNATURE);
49+
}
50+
51+
}

‎AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/utils/AAProcessorTestHelper.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void addManifestProcessorParameter(Class<?> classOfPackagingContainingMan
2626
}
2727

2828
public void addManifestProcessorParameter(Class<?> classOfPackagingContainingManifest, String manifestFileName) {
29-
String manifestPath = classOfPackagingContainingManifest.getResource(manifestFileName).getPath();
29+
String manifestPath = toPath(classOfPackagingContainingManifest, manifestFileName);
3030
addProcessorParameter("androidManifestFile", manifestPath);
3131
}
3232

@@ -35,4 +35,8 @@ public File toGeneratedFile(Class<?> compiledClass) {
3535
return output;
3636
}
3737

38+
public String toPath(Class<?> classOfPackagingContainingFile, String filename) {
39+
return classOfPackagingContainingFile.getResource(filename).getPath();
40+
}
41+
3842
}

‎AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/utils/ProcessorTestHelper.java‎

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ public CompileResult(List<Diagnostic<? extends JavaFileObject>> diagnostics) {
5858
protected static final String SOURCE_FILE_SUFFIX = ".java";
5959
protected static final String OUTPUT_DIRECTORY = "target/generated-test";
6060

61+
public static void assertGeneratedClassContains(File output, String value) {
62+
String[] outputContent = getContents(output);
63+
for (String line : outputContent) {
64+
if (line.contains(value)) {
65+
return;
66+
}
67+
}
68+
fail("Expected value \"" + value + "\" couldn't be found in file " + output.getAbsolutePath());
69+
}
70+
71+
public static void assertGeneratedClassDoesntContains(File output, String value) {
72+
String[] outputContent = getContents(output);
73+
for (String line : outputContent) {
74+
if (line.contains(value)) {
75+
fail("Value \"" + value + "\" shouldn't be in file " + output.getAbsolutePath());
76+
}
77+
}
78+
}
79+
6180
public static void assertOutput(File expectedResult, File output) {
6281
String[] expectedContent = getContents(expectedResult);
6382
String[] outputContent = getContents(output);
@@ -245,6 +264,20 @@ public CompileResult compileFiles(Type... compilationUnits) {
245264
return compileFiles(files);
246265
}
247266

267+
public CompileResult compileFiles(Object... elements) {
268+
assert elements != null;
269+
270+
List<File> files = new ArrayList<File>();
271+
for (Object element : elements) {
272+
if (element instanceof Type) {
273+
addCollection(files, (Type) element);
274+
} else if (element instanceof String) {
275+
files.add(new File((String) element));
276+
}
277+
}
278+
return compileFiles(files);
279+
}
280+
248281
public CompileResult compileFiles(File... compilationUnits) {
249282
return compileFiles(Arrays.asList(compilationUnits));
250283
}
@@ -334,20 +367,24 @@ private <T extends Type> void addCollection(List<File> files, T... compilationUn
334367
return;
335368
}
336369
for (T element : compilationUnits) {
337-
assert element != null;
370+
addCollection(files, element);
371+
}
372+
}
338373

339-
if (element instanceof Class<?>) {
340-
File file = toFile((Class<?>) element);
341-
if (file != null) {
342-
files.add(file);
343-
} else {
344-
// These are innerclasses, etc ... that should not be
345-
// defined in this way
346-
}
347-
} else if (element instanceof Package) {
348-
ClassFinder classFinder = new ClassFinder();
349-
addCollection(files, classFinder.findClassesInPackage(((Package) element).getName()));
374+
private <T extends Type> void addCollection(List<File> files, T element) {
375+
assert element != null;
376+
377+
if (element instanceof Class<?>) {
378+
File file = toFile((Class<?>) element);
379+
if (file != null) {
380+
files.add(file);
381+
} else {
382+
// These are innerclasses, etc ... that should not be
383+
// defined in this way
350384
}
385+
} else if (element instanceof Package) {
386+
ClassFinder classFinder = new ClassFinder();
387+
addCollection(files, classFinder.findClassesInPackage(((Package) element).getName()));
351388
}
352389
}
353390

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2010-2013 eBusiness Information, Excilys Group
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
use this file except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed To in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
License for the specific language governing permissions and limitations under
15+
the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
package="org.androidannotations.testprocessor"
19+
android:versionCode="1"
20+
android:versionName="1.0" >
21+
22+
<uses-sdk
23+
android:minSdkVersion="8"
24+
android:targetSdkVersion="18" />
25+
26+
<application>
27+
<activity android:name="org.androidannotations.generation.ActivityInManifest_" />
28+
</application>
29+
30+
</manifest>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2010-2013 eBusiness Information, Excilys Group
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
use this file except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed To in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
License for the specific language governing permissions and limitations under
15+
the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
package="org.androidannotations.testprocessor"
19+
android:versionCode="1"
20+
android:versionName="1.0" >
21+
22+
<uses-sdk
23+
android:minSdkVersion="18"
24+
android:targetSdkVersion="18" />
25+
26+
<application>
27+
<activity android:name="org.androidannotations.generation.ActivityInManifest_" />
28+
</application>
29+
30+
</manifest>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package android.app;
2+
3+
import android.content.Intent;
4+
5+
/**
6+
* We have to put this on resources folder because we want to add it to
7+
* classpath only on some unit tests methods
8+
*/
9+
public class Fragment {
10+
11+
public Activity getActivity() {
12+
return null;
13+
}
14+
15+
public void startActivityForResult(Intent intent, int flag) {
16+
17+
}
18+
19+
}

0 commit comments

Comments
 (0)