aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src
diff options
context:
space:
mode:
authorbananeweizen <bananeweizen@gmx.de>2011-11-04 08:00:03 +0100
committerbananeweizen <bananeweizen@gmx.de>2011-11-04 08:00:03 +0100
commit0355dc60db51d600d8634f2f82143dad08202117 (patch)
treebbd08aaea404783ba5dcaea38b43626fa8021ab4 /tests/src
parente7ea1bdd8cc235a883c3a90e7f2ebd1f90676c3c (diff)
downloadcgeo-0355dc60db51d600d8634f2f82143dad08202117.zip
cgeo-0355dc60db51d600d8634f2f82143dad08202117.tar.gz
cgeo-0355dc60db51d600d8634f2f82143dad08202117.tar.bz2
refactoring for #310
* split import into 2 activies (files and mails) * move non activity stuff into new class * split tests accordingly * fix manifest
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/cgeo/geocaching/cgeogpxesTest.java161
-rw-r--r--tests/src/cgeo/geocaching/files/GPXImporterTest.java170
2 files changed, 170 insertions, 161 deletions
diff --git a/tests/src/cgeo/geocaching/cgeogpxesTest.java b/tests/src/cgeo/geocaching/cgeogpxesTest.java
index 90ce201..bc0f99e 100644
--- a/tests/src/cgeo/geocaching/cgeogpxesTest.java
+++ b/tests/src/cgeo/geocaching/cgeogpxesTest.java
@@ -1,89 +1,14 @@
package cgeo.geocaching;
-import cgeo.geocaching.test.R;
-
-import android.os.Handler;
-import android.os.Message;
import android.test.ActivityInstrumentationTestCase2;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
public class cgeogpxesTest extends ActivityInstrumentationTestCase2<cgeogpxes> {
- private int listId;
private cgeogpxes importGpxActivity = new cgeogpxes();
- private File tempDir;
- private TestHandler importStepHandler = new TestHandler();
- private TestHandler progressHandler = new TestHandler();
public cgeogpxesTest() {
super("cgeo.geocaching", cgeogpxes.class);
}
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- tempDir = new File(System.getProperty("java.io.tmpdir"), "cgeogpxesTest");
- tempDir.mkdir();
-
- // workaround to get storage initialized
- cgeoapplication.getInstance().getAllHistoricCachesCount();
- listId = cgeoapplication.getInstance().createList("cgeogpxesTest");
- }
-
- @Override
- protected void tearDown() throws Exception {
- cgeoapplication.getInstance().dropStored(listId);
- cgeoapplication.getInstance().removeList(listId);
- deleteDirectory(tempDir);
- super.tearDown();
- }
-
-
- private void copyResourceToFile(int resourceId, File file) throws IOException {
- final InputStream is = getInstrumentation().getContext().getResources().openRawResource(resourceId);
- final FileOutputStream os = new FileOutputStream(file);
-
- try {
- byte[] buffer = new byte[4096];
- int byteCount;
- while ((byteCount = is.read(buffer)) >= 0) {
- os.write(buffer, 0, byteCount);
- }
- } finally {
- os.close();
- is.close();
- }
- }
-
- private void deleteDirectory(File dir) {
- for (File f : dir.listFiles()) {
- if (f.isFile()) {
- f.delete();
- } else if (f.isDirectory()) {
- deleteDirectory(f);
- }
- }
- dir.delete();
- }
-
- static class TestHandler extends Handler {
- List<Message> messages = new ArrayList<Message>();
-
- @Override
- public void handleMessage(Message msg) {
- Message msg1 = new Message();
- msg1.copyFrom(msg);
- messages.add(msg1);
- }
- }
-
public void testFileNameMatches() {
assertTrue(importGpxActivity.filenameBelongsToList("1234567.gpx"));
assertTrue(importGpxActivity.filenameBelongsToList("1234567.GPX"));
@@ -100,90 +25,4 @@ public class cgeogpxesTest extends ActivityInstrumentationTestCase2<cgeogpxes> {
assertFalse(importGpxActivity.filenameBelongsToList("1234567-wpts.gpx"));
}
- public void testGetWaypointsFileForGpx() {
- assertEquals(new File("1234567-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("1234567.gpx")));
- assertEquals(new File("/mnt/sdcard/1234567-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sdcard/1234567.gpx")));
- assertEquals(new File("/mnt/sdcard/1-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sdcard/1.gpx")));
- assertEquals(new File("/mnt/sd.card/1-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sd.card/1.gpx")));
- assertEquals(new File("1234567.9-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("1234567.9.gpx")));
- assertEquals(new File("1234567-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("1234567.GPX")));
- assertEquals(new File("gpx.gpx-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("gpx.gpx.gpx")));
- assertNull(cgeogpxes.getWaypointsFileForGpx(new File("123.gpy")));
- assertNull(cgeogpxes.getWaypointsFileForGpx(new File("gpx")));
- assertNull(cgeogpxes.getWaypointsFileForGpx(new File(".gpx")));
- assertNull(cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sdcard/.gpx")));
- }
-
- public void testImportGpx() throws IOException {
- File gc31j2h = new File(tempDir, "gc31j2h.gpx");
- copyResourceToFile(R.raw.gc31j2h, gc31j2h);
-
- cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
- importThread.run();
-
- assertEquals(3, importStepHandler.messages.size());
- assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
- assertEquals(cgeogpxes.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(1).what);
- assertEquals(cgeogpxes.IMPORT_STEP_FINISHED, importStepHandler.messages.get(2).what);
- cgSearch search = (cgSearch) importStepHandler.messages.get(2).obj;
- assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes());
-
- cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
- assertNotNull(cache);
-
- // can't assert, for whatever reason the waypoints are remembered in DB
- // assertNull(cache.waypoints);
- }
-
- public void testImportGpxWithWaypoints() throws IOException {
- File gc31j2h = new File(tempDir, "gc31j2h.gpx");
- copyResourceToFile(R.raw.gc31j2h, gc31j2h);
- copyResourceToFile(R.raw.gc31j2h_wpts, new File(tempDir, "gc31j2h-wpts.gpx"));
-
- cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
- importThread.run();
-
- assertEquals(4, importStepHandler.messages.size());
- assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
- assertEquals(cgeogpxes.IMPORT_STEP_READ_WPT_FILE, importStepHandler.messages.get(1).what);
- assertEquals(cgeogpxes.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(2).what);
- assertEquals(cgeogpxes.IMPORT_STEP_FINISHED, importStepHandler.messages.get(3).what);
- cgSearch search = (cgSearch) importStepHandler.messages.get(3).obj;
- assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes());
-
- cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
- assertNotNull(cache);
- assertEquals(2, cache.getWaypoints().size());
- }
-
- public void testImportLoc() throws IOException {
- File oc5952 = new File(tempDir, "oc5952.loc");
- copyResourceToFile(R.raw.oc5952_loc, oc5952);
-
- cgeogpxes.ImportLocFileThread importThread = new cgeogpxes.ImportLocFileThread(oc5952, listId, importStepHandler, progressHandler);
- importThread.run();
-
- assertEquals(3, importStepHandler.messages.size());
- assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
- assertEquals(cgeogpxes.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(1).what);
- assertEquals(cgeogpxes.IMPORT_STEP_FINISHED, importStepHandler.messages.get(2).what);
- cgSearch search = (cgSearch) importStepHandler.messages.get(2).obj;
- assertEquals(Collections.singletonList("OC5952"), search.getGeocodes());
-
- cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("OC5952");
- assertNotNull(cache);
- }
-
- public void testImportGpxError() throws IOException {
- File gc31j2h = new File(tempDir, "gc31j2h.gpx");
- copyResourceToFile(R.raw.gc31j2h_err, gc31j2h);
-
- cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
- importThread.run();
-
- assertEquals(2, importStepHandler.messages.size());
- assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
- assertEquals(cgeogpxes.IMPORT_STEP_FINISHED_WITH_ERROR, importStepHandler.messages.get(1).what);
- }
-
}
diff --git a/tests/src/cgeo/geocaching/files/GPXImporterTest.java b/tests/src/cgeo/geocaching/files/GPXImporterTest.java
new file mode 100644
index 0000000..720b777
--- /dev/null
+++ b/tests/src/cgeo/geocaching/files/GPXImporterTest.java
@@ -0,0 +1,170 @@
+package cgeo.geocaching.files;
+
+import cgeo.geocaching.cgCache;
+import cgeo.geocaching.cgSearch;
+import cgeo.geocaching.cgeoapplication;
+import cgeo.geocaching.test.R;
+
+import android.os.Handler;
+import android.os.Message;
+import android.test.InstrumentationTestCase;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class GPXImporterTest extends InstrumentationTestCase {
+ private TestHandler importStepHandler = new TestHandler();
+ private TestHandler progressHandler = new TestHandler();
+ private int listId;
+ private File tempDir;
+
+ public void testGetWaypointsFileForGpx() {
+ assertEquals(new File("1234567-wpts.gpx"), GPXImporter.getWaypointsFileForGpx(new File("1234567.gpx")));
+ assertEquals(new File("/mnt/sdcard/1234567-wpts.gpx"), GPXImporter.getWaypointsFileForGpx(new File("/mnt/sdcard/1234567.gpx")));
+ assertEquals(new File("/mnt/sdcard/1-wpts.gpx"), GPXImporter.getWaypointsFileForGpx(new File("/mnt/sdcard/1.gpx")));
+ assertEquals(new File("/mnt/sd.card/1-wpts.gpx"), GPXImporter.getWaypointsFileForGpx(new File("/mnt/sd.card/1.gpx")));
+ assertEquals(new File("1234567.9-wpts.gpx"), GPXImporter.getWaypointsFileForGpx(new File("1234567.9.gpx")));
+ assertEquals(new File("1234567-wpts.gpx"), GPXImporter.getWaypointsFileForGpx(new File("1234567.GPX")));
+ assertEquals(new File("gpx.gpx-wpts.gpx"), GPXImporter.getWaypointsFileForGpx(new File("gpx.gpx.gpx")));
+ assertNull(GPXImporter.getWaypointsFileForGpx(new File("123.gpy")));
+ assertNull(GPXImporter.getWaypointsFileForGpx(new File("gpx")));
+ assertNull(GPXImporter.getWaypointsFileForGpx(new File(".gpx")));
+ assertNull(GPXImporter.getWaypointsFileForGpx(new File("/mnt/sdcard/.gpx")));
+ }
+
+ public void testImportGpx() throws IOException {
+ File gc31j2h = new File(tempDir, "gc31j2h.gpx");
+ copyResourceToFile(R.raw.gc31j2h, gc31j2h);
+
+ GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
+ importThread.run();
+
+ assertEquals(3, importStepHandler.messages.size());
+ assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
+ assertEquals(GPXImporter.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(1).what);
+ assertEquals(GPXImporter.IMPORT_STEP_FINISHED, importStepHandler.messages.get(2).what);
+ cgSearch search = (cgSearch) importStepHandler.messages.get(2).obj;
+ assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes());
+
+ cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
+ assertNotNull(cache);
+
+ // can't assert, for whatever reason the waypoints are remembered in DB
+ // assertNull(cache.waypoints);
+ }
+
+ public void testImportGpxWithWaypoints() throws IOException {
+ File gc31j2h = new File(tempDir, "gc31j2h.gpx");
+ copyResourceToFile(R.raw.gc31j2h, gc31j2h);
+ copyResourceToFile(R.raw.gc31j2h_wpts, new File(tempDir, "gc31j2h-wpts.gpx"));
+
+ GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
+ importThread.run();
+
+ assertEquals(4, importStepHandler.messages.size());
+ assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
+ assertEquals(GPXImporter.IMPORT_STEP_READ_WPT_FILE, importStepHandler.messages.get(1).what);
+ assertEquals(GPXImporter.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(2).what);
+ assertEquals(GPXImporter.IMPORT_STEP_FINISHED, importStepHandler.messages.get(3).what);
+ cgSearch search = (cgSearch) importStepHandler.messages.get(3).obj;
+ assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes());
+
+ cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
+ assertNotNull(cache);
+ assertEquals(2, cache.getWaypoints().size());
+ }
+
+ public void testImportLoc() throws IOException {
+ File oc5952 = new File(tempDir, "oc5952.loc");
+ copyResourceToFile(R.raw.oc5952_loc, oc5952);
+
+ GPXImporter.ImportLocFileThread importThread = new GPXImporter.ImportLocFileThread(oc5952, listId, importStepHandler, progressHandler);
+ importThread.run();
+
+ assertEquals(3, importStepHandler.messages.size());
+ assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
+ assertEquals(GPXImporter.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(1).what);
+ assertEquals(GPXImporter.IMPORT_STEP_FINISHED, importStepHandler.messages.get(2).what);
+ cgSearch search = (cgSearch) importStepHandler.messages.get(2).obj;
+ assertEquals(Collections.singletonList("OC5952"), search.getGeocodes());
+
+ cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("OC5952");
+ assertNotNull(cache);
+ }
+
+ public void testImportGpxError() throws IOException {
+ File gc31j2h = new File(tempDir, "gc31j2h.gpx");
+ copyResourceToFile(R.raw.gc31j2h_err, gc31j2h);
+
+ GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
+ importThread.run();
+
+ assertEquals(2, importStepHandler.messages.size());
+ assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what);
+ assertEquals(GPXImporter.IMPORT_STEP_FINISHED_WITH_ERROR, importStepHandler.messages.get(1).what);
+ }
+
+ private void copyResourceToFile(int resourceId, File file) throws IOException {
+ final InputStream is = getInstrumentation().getContext().getResources().openRawResource(resourceId);
+ final FileOutputStream os = new FileOutputStream(file);
+
+ try {
+ byte[] buffer = new byte[4096];
+ int byteCount;
+ while ((byteCount = is.read(buffer)) >= 0) {
+ os.write(buffer, 0, byteCount);
+ }
+ } finally {
+ os.close();
+ is.close();
+ }
+ }
+
+ static class TestHandler extends Handler {
+ List<Message> messages = new ArrayList<Message>();
+
+ @Override
+ public void handleMessage(Message msg) {
+ Message msg1 = new Message();
+ msg1.copyFrom(msg);
+ messages.add(msg1);
+ }
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ tempDir = new File(System.getProperty("java.io.tmpdir"), "cgeogpxesTest");
+ tempDir.mkdir();
+
+ // workaround to get storage initialized
+ cgeoapplication.getInstance().getAllHistoricCachesCount();
+ listId = cgeoapplication.getInstance().createList("cgeogpxesTest");
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ cgeoapplication.getInstance().dropStored(listId);
+ cgeoapplication.getInstance().removeList(listId);
+ deleteDirectory(tempDir);
+ super.tearDown();
+ }
+
+ private void deleteDirectory(File dir) {
+ for (File f : dir.listFiles()) {
+ if (f.isFile()) {
+ f.delete();
+ } else if (f.isDirectory()) {
+ deleteDirectory(f);
+ }
+ }
+ dir.delete();
+ }
+
+}