Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
wwm0609 committed May 23, 2017
1 parent 7b11883 commit 1ebbd00
Show file tree
Hide file tree
Showing 30 changed files with 1,333 additions and 234 deletions.
25 changes: 24 additions & 1 deletion amigo-lib/build.gradle
Expand Up @@ -2,6 +2,8 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

apply from: './jacoco.gradle'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
Expand All @@ -17,6 +19,9 @@ android {
cppFlags "-std=c++11"
}
}
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}

externalNativeBuild {
Expand All @@ -25,14 +30,32 @@ android {
}
}

buildTypes {
debug {
debuggable true
testCoverageEnabled = true
}
}

productFlavors {
amigoTest {
minSdkVersion 8 // espresso runs on only 8+
}
}

lintOptions {
abortOnError false
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2')
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile "org.robolectric:robolectric:3.3.1"
testCompile 'commons-codec:commons-codec:1.4'
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito:2.2.0'
}

def GROUP = 'me.ele'
Expand Down
41 changes: 41 additions & 0 deletions amigo-lib/jacoco.gradle
@@ -0,0 +1,41 @@
apply plugin: 'jacoco'

jacoco {
toolVersion "0.7.6.201602180812"
}
// run ./gradlew clean createDebugCoverageReport jacocoTestReport

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports"

reports {
xml.enabled = true
html.enabled = true
}

def fileFilter = ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'android/**/*.*',
'**/Lambda$*.class', //Retrolambda
'**/Lambda.class',
'**/*Lambda.class',
'**/*Lambda*.class',
'**/*Lambda*.*',
'**/*Builder.*',
'**/*_MembersInjector.class', //Dagger2 generated code
'**/*_MembersInjector*.*', //Dagger2 generated code
'**/*_*Factory*.*', //Dagger2 generated code
'**/*Component*.*', //Dagger2 generated code
'**/*Module*.*' //Dagger2 generated code
]
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"

sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.projectDir, includes:
['**/*.exec' , '**/*.ec'])
}
10 changes: 10 additions & 0 deletions amigo-lib/src/androidTest/AndroidManifest.xml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="me.ele.amigo">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

</manifest>
@@ -0,0 +1,66 @@
package me.ele.amigo;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import me.ele.amigo.utils.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class AmigoClassLoaderTest {

@Test
public void testAmigoClassLoader() throws Exception {
AmigoIntegrationTest integrationTest = new AmigoIntegrationTest();
integrationTest.testRunPatchApplication();

Context appContext = InstrumentationRegistry.getTargetContext();
String workingChecksum = Amigo.getWorkingPatchApkChecksum(appContext);
AmigoClassLoader classLoader = AmigoClassLoader.newInstance(appContext, workingChecksum);
Assert.assertNotNull(classLoader.loadClass("me.ele.app.amigo.HomeActivity"));
try {
// load unknown class
classLoader.loadClass("me.ele.app.amigo.HomeActivity1");
} catch (Exception e) {
Assert.assertEquals(ClassNotFoundException.class, e.getClass());
}
Assert.assertNotNull(classLoader.findResource("AndroidManifest.xml"));
}

@Test
public void testJoinPath() throws IOException {
Context appContext = InstrumentationRegistry.getTargetContext();
File tempDir = new File(appContext.getFilesDir(), "test_join_path");
FileUtils.removeFile(tempDir);
tempDir.mkdir();

Assert.assertEquals(null, AmigoClassLoader.joinPath(tempDir));

File file1 = new File(tempDir, "a.txt");
file1.createNewFile();
Assert.assertEquals(true,
Arrays.asList(AmigoClassLoader.joinPath(tempDir).split(File.pathSeparator))
.containsAll(Collections.singletonList(file1.getAbsolutePath())));

File file2 = new File(tempDir, "b.txt");
file2.createNewFile();
Assert.assertEquals(true,
Arrays.asList(AmigoClassLoader.joinPath(tempDir).split(File.pathSeparator))
.containsAll(
Arrays.asList(file1.getAbsolutePath(), file2.getAbsolutePath())));

File file3 = new File(tempDir, "c.txt");
file3.createNewFile();
Assert.assertEquals(true,
Arrays.asList(AmigoClassLoader.joinPath(tempDir).split(File.pathSeparator))
.containsAll(Arrays.asList(file1.getAbsolutePath(), file2.getAbsolutePath(),
file3.getAbsolutePath())));
}
}

0 comments on commit 1ebbd00

Please sign in to comment.