move shareable test classes into new separate section: testShared
This allows some of the mock classes to be shared across Robolectric and emulator tests.
This commit is contained in:
parent
d7022dd498
commit
2350b4e694
@ -189,6 +189,16 @@ android {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
test {
|
||||||
|
java.srcDirs += "$projectDir/src/testShared/java"
|
||||||
|
}
|
||||||
|
|
||||||
|
androidTest {
|
||||||
|
java.srcDirs += "$projectDir/src/testShared/java"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lintOptions {
|
lintOptions {
|
||||||
checkReleaseBuilds false
|
checkReleaseBuilds false
|
||||||
abortOnError true
|
abortOnError true
|
||||||
|
@ -28,23 +28,18 @@ import android.util.Log;
|
|||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.fdroid.fdroid.BuildConfig;
|
import org.fdroid.fdroid.BuildConfig;
|
||||||
import org.fdroid.fdroid.RepoXMLHandler;
|
|
||||||
import org.fdroid.fdroid.data.Apk;
|
import org.fdroid.fdroid.data.Apk;
|
||||||
import org.fdroid.fdroid.data.App;
|
import org.fdroid.fdroid.data.App;
|
||||||
import org.fdroid.fdroid.data.Repo;
|
import org.fdroid.fdroid.data.Repo;
|
||||||
import org.fdroid.fdroid.data.RepoPushRequest;
|
import org.fdroid.fdroid.data.RepoPushRequest;
|
||||||
import org.fdroid.fdroid.mock.MockRepo;
|
import org.fdroid.fdroid.mock.RepoDetails;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RobolectricGradleTestRunner;
|
import org.robolectric.RobolectricGradleTestRunner;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.shadows.ShadowLog;
|
import org.robolectric.shadows.ShadowLog;
|
||||||
import org.xml.sax.InputSource;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import org.xml.sax.XMLReader;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -55,16 +50,11 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.parsers.SAXParser;
|
|
||||||
import javax.xml.parsers.SAXParserFactory;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
// TODO: Use sdk=24 when Robolectric supports this
|
// TODO: Use sdk=24 when Robolectric supports this
|
||||||
@Config(constants = BuildConfig.class, sdk = 23)
|
@Config(constants = BuildConfig.class, sdk = 23)
|
||||||
@ -835,41 +825,6 @@ public class RepoXMLHandlerTest {
|
|||||||
assertEquals(apks.size(), apkCount);
|
assertEquals(apks.size(), apkCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class RepoDetails implements RepoXMLHandler.IndexReceiver {
|
|
||||||
|
|
||||||
public String name;
|
|
||||||
public String description;
|
|
||||||
public String signingCert;
|
|
||||||
public int maxAge;
|
|
||||||
public int version;
|
|
||||||
public long timestamp;
|
|
||||||
|
|
||||||
public List<Apk> apks = new ArrayList<>();
|
|
||||||
public List<App> apps = new ArrayList<>();
|
|
||||||
public List<RepoPushRequest> repoPushRequestList = new ArrayList<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void receiveRepo(String name, String description, String signingCert, int maxage, int version, long timestamp) {
|
|
||||||
this.name = name;
|
|
||||||
this.description = description;
|
|
||||||
this.signingCert = signingCert;
|
|
||||||
this.maxAge = maxage;
|
|
||||||
this.version = version;
|
|
||||||
this.timestamp = timestamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void receiveApp(App app, List<Apk> packages) {
|
|
||||||
apks.addAll(packages);
|
|
||||||
apps.add(app);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void receiveRepoPushRequest(RepoPushRequest repoPushRequest) {
|
|
||||||
repoPushRequestList.add(repoPushRequest);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private RepoDetails getFromFile(String indexFilename) {
|
private RepoDetails getFromFile(String indexFilename) {
|
||||||
return getFromFile(indexFilename, Repo.PUSH_REQUEST_IGNORE);
|
return getFromFile(indexFilename, Repo.PUSH_REQUEST_IGNORE);
|
||||||
@ -877,28 +832,9 @@ public class RepoXMLHandlerTest {
|
|||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private RepoDetails getFromFile(String indexFilename, int pushRequests) {
|
private RepoDetails getFromFile(String indexFilename, int pushRequests) {
|
||||||
try {
|
Log.i(TAG, "test file: " + getClass().getClassLoader().getResource(indexFilename));
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(indexFilename);
|
||||||
factory.setNamespaceAware(true);
|
return RepoDetails.getFromFile(inputStream, pushRequests);
|
||||||
SAXParser parser = factory.newSAXParser();
|
|
||||||
XMLReader reader = parser.getXMLReader();
|
|
||||||
RepoDetails repoDetails = new RepoDetails();
|
|
||||||
MockRepo mockRepo = new MockRepo(100, pushRequests);
|
|
||||||
RepoXMLHandler handler = new RepoXMLHandler(mockRepo, repoDetails);
|
|
||||||
reader.setContentHandler(handler);
|
|
||||||
Log.i(TAG, "test file: " + getClass().getClassLoader().getResource(indexFilename));
|
|
||||||
InputStream input = getClass().getClassLoader().getResourceAsStream(indexFilename);
|
|
||||||
InputSource is = new InputSource(new BufferedInputStream(input));
|
|
||||||
reader.parse(is);
|
|
||||||
return repoDetails;
|
|
||||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
fail();
|
|
||||||
|
|
||||||
// Satisfies the compiler, but fail() will always throw a runtime exception so we never
|
|
||||||
// reach this return statement.
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeResourceToObbDir(String assetName) throws IOException {
|
private void writeResourceToObbDir(String assetName) throws IOException {
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
package org.fdroid.fdroid.mock;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.fdroid.fdroid.RepoXMLHandler;
|
||||||
|
import org.fdroid.fdroid.data.Apk;
|
||||||
|
import org.fdroid.fdroid.data.App;
|
||||||
|
import org.fdroid.fdroid.data.RepoPushRequest;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import org.xml.sax.XMLReader;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import javax.xml.parsers.SAXParser;
|
||||||
|
import javax.xml.parsers.SAXParserFactory;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
public class RepoDetails implements RepoXMLHandler.IndexReceiver {
|
||||||
|
public static final String TAG = "RepoDetails";
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
public String description;
|
||||||
|
public String signingCert;
|
||||||
|
public int maxAge;
|
||||||
|
public int version;
|
||||||
|
public long timestamp;
|
||||||
|
|
||||||
|
public List<Apk> apks = new ArrayList<>();
|
||||||
|
public List<App> apps = new ArrayList<>();
|
||||||
|
public List<RepoPushRequest> repoPushRequestList = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveRepo(String name, String description, String signingCert, int maxage, int version, long timestamp) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.signingCert = signingCert;
|
||||||
|
this.maxAge = maxage;
|
||||||
|
this.version = version;
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveApp(App app, List<Apk> packages) {
|
||||||
|
apks.addAll(packages);
|
||||||
|
apps.add(app);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveRepoPushRequest(RepoPushRequest repoPushRequest) {
|
||||||
|
repoPushRequestList.add(repoPushRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static RepoDetails getFromFile(InputStream inputStream, int pushRequests) {
|
||||||
|
try {
|
||||||
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||||
|
factory.setNamespaceAware(true);
|
||||||
|
SAXParser parser = factory.newSAXParser();
|
||||||
|
XMLReader reader = parser.getXMLReader();
|
||||||
|
RepoDetails repoDetails = new RepoDetails();
|
||||||
|
MockRepo mockRepo = new MockRepo(100, pushRequests);
|
||||||
|
RepoXMLHandler handler = new RepoXMLHandler(mockRepo, repoDetails);
|
||||||
|
reader.setContentHandler(handler);
|
||||||
|
InputSource is = new InputSource(new BufferedInputStream(inputStream));
|
||||||
|
reader.parse(is);
|
||||||
|
return repoDetails;
|
||||||
|
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail();
|
||||||
|
|
||||||
|
// Satisfies the compiler, but fail() will always throw a runtime exception so we never
|
||||||
|
// reach this return statement.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user