diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncDriveWithMyTracksTest.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncDriveWithMyTracksTest.java index d1c724316..70bb3552f 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncDriveWithMyTracksTest.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncDriveWithMyTracksTest.java @@ -19,28 +19,24 @@ import com.google.android.apps.mytracks.TrackListActivity; import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils; import com.google.android.apps.mytracks.endtoendtest.GoogleUtils; import com.google.android.maps.mytracks.R; -import com.google.api.client.http.GenericUrl; -import com.google.api.client.http.HttpResponse; -import com.google.api.services.drive.model.File; +import com.google.api.services.drive.Drive; import android.app.Instrumentation; import android.test.ActivityInstrumentationTestCase2; import android.view.KeyEvent; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; /** - * Tests the two-ways sync of MyTracks and Google Drive. + * Tests how Google Drive to sync with MyTracks. * * @author Youtao Liu */ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2 { + public static Drive drive; private Instrumentation instrumentation; - private TrackListActivity activityMyTracks; + private TrackListActivity trackListActivity; public SyncDriveWithMyTracksTest() { super(TrackListActivity.class); @@ -50,14 +46,14 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< protected void setUp() throws Exception { super.setUp(); instrumentation = getInstrumentation(); - activityMyTracks = getActivity(); - EndToEndTestUtils.setupForAllTest(instrumentation, activityMyTracks); + trackListActivity = getActivity(); + EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity); SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME); + drive = SyncTestUtils.getGoogleDrive(trackListActivity.getApplicationContext()); } /** - * Deletes all tracks in MyTracks and checks in Google Drive. Then creates one - * tracks in MyTracks and checks it in Google Drive. + * Deletes all tracks in MyTracks and checks in Google Drive. * * @throws IOException */ @@ -65,12 +61,12 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< EndToEndTestUtils.deleteAllTracks(); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.checkFilesNumber(0); + SyncTestUtils.checkFilesNumber(0, drive); } /** - * Creates one empty track and non-empty track in MyTracks and then check them - * in Google Drive. + * Creates one empty track and one non-empty track in MyTracks and then check + * them in Google Drive. * * @throws IOException */ @@ -80,12 +76,11 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< EndToEndTestUtils.createSimpleTrack(3, true); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.checkFilesNumber(2); + SyncTestUtils.checkFilesNumber(2, drive); } /** - * Creates one empty track and non-empty track in MyTracks and then check them - * in Google Drive. + * Edits a tracks in MyTracks and checks it on Google Drive after sync. * * @throws IOException */ @@ -95,25 +90,24 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< // Sync this track. EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.updateDriveData(activityMyTracks.getApplicationContext()); - assertTrue(SyncTestUtils.checkFile(EndToEndTestUtils.trackName, true)); - String oldTrack = getContentOfFile(SyncTestUtils.getFile(EndToEndTestUtils.trackName)); + assertTrue(SyncTestUtils.checkFile(EndToEndTestUtils.trackName, true, drive)); + String oldTrack = SyncTestUtils.getContentOfFile(SyncTestUtils.getFile(EndToEndTestUtils.trackName, drive), drive); String oldTrackName = EndToEndTestUtils.trackName; // Edit this track. EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.trackName); - EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_edit), true); + EndToEndTestUtils.findMenuItem(trackListActivity.getString(R.string.menu_edit), true); String newTrackName = EndToEndTestUtils.TRACK_NAME_PREFIX + "_new" + System.currentTimeMillis(); String newType = EndToEndTestUtils.activityType + newTrackName; String newDesc = "desc" + newTrackName; instrumentation.waitForIdleSync(); - EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.generic_save)); + EndToEndTestUtils.SOLO.waitForText(trackListActivity.getString(R.string.generic_save)); sendKeys(KeyEvent.KEYCODE_DEL); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(0, newTrackName); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(1, newType); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(2, newDesc); - EndToEndTestUtils.SOLO.clickOnButton(activityMyTracks.getString(R.string.generic_save)); + EndToEndTestUtils.SOLO.clickOnButton(trackListActivity.getString(R.string.generic_save)); instrumentation.waitForIdleSync(); EndToEndTestUtils.SOLO.goBack(); instrumentation.waitForIdleSync(); @@ -121,34 +115,14 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< // Sync again. EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.updateDriveData(activityMyTracks.getApplicationContext()); - assertFalse(SyncTestUtils.checkFile(oldTrackName, false)); - assertTrue(SyncTestUtils.checkFile(newTrackName, true)); - String newTrack = getContentOfFile(SyncTestUtils.getFile(newTrackName)); // Check. + assertTrue(SyncTestUtils.checkFile(oldTrackName, false, drive)); + assertTrue(SyncTestUtils.checkFile(newTrackName, true, drive)); + String newTrack = SyncTestUtils.getContentOfFile(SyncTestUtils.getFile(newTrackName,drive), drive); assertNotSame(oldTrack, newTrack); assertTrue(newTrack.indexOf(newTrackName) > 0); assertTrue(newTrack.indexOf(newType) > 0); assertTrue(newTrack.indexOf(newDesc) > 0); } - - private String getContentOfFile(File file) throws IOException { - SyncTestUtils.updateDriveData(activityMyTracks.getApplicationContext()); - HttpResponse resp = SyncTestUtils.drive.getRequestFactory() - .buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute(); - InputStream response = resp.getContent(); - BufferedReader br = new BufferedReader(new InputStreamReader(response)); - - StringBuilder sb = new StringBuilder(); - - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - String fileContent = sb.toString(); - - br.close(); - return fileContent; - } } diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncMyTracksWithDriveTest.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncMyTracksWithDriveTest.java index c80122547..d5fd32527 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncMyTracksWithDriveTest.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncMyTracksWithDriveTest.java @@ -19,6 +19,7 @@ import com.google.android.apps.mytracks.TrackListActivity; import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils; import com.google.android.apps.mytracks.endtoendtest.GoogleUtils; import com.google.android.maps.mytracks.R; +import com.google.api.services.drive.Drive; import android.app.Instrumentation; import android.test.ActivityInstrumentationTestCase2; @@ -26,14 +27,15 @@ import android.test.ActivityInstrumentationTestCase2; import java.io.IOException; /** - * Tests the two-ways sync of MyTracks and Google Drive. + * Tests how MyTracks to sync with Google Drive. * * @author Youtao Liu */ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2 { + public static Drive drive; private Instrumentation instrumentation; - private TrackListActivity activityMyTracks; + private TrackListActivity trackListActivity; public SyncMyTracksWithDriveTest() { super(TrackListActivity.class); @@ -43,9 +45,10 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2< protected void setUp() throws Exception { super.setUp(); instrumentation = getInstrumentation(); - activityMyTracks = getActivity(); - EndToEndTestUtils.setupForAllTest(instrumentation, activityMyTracks); + trackListActivity = getActivity(); + EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity); SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME); + drive = SyncTestUtils.getGoogleDrive(trackListActivity.getApplicationContext()); } /** @@ -58,12 +61,11 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2< EndToEndTestUtils.createTrackIfEmpty(0, true); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.checkFilesNumber(1); - - SyncTestUtils.removeKMLFiles(); + SyncTestUtils.checkFilesNumber(1, drive); + + SyncTestUtils.removeKMLFiles(drive); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); SyncTestUtils.checkTracksNumber(0); } - } diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncTestUtils.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncTestUtils.java index adfe53abc..90aa9caaa 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncTestUtils.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/SyncTestUtils.java @@ -20,13 +20,18 @@ import com.google.android.apps.mytracks.io.sync.SyncUtils; import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.maps.mytracks.R; import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; +import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpResponse; import com.google.api.services.drive.Drive; import com.google.api.services.drive.model.File; import android.content.Context; import android.widget.CheckBox; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.List; import java.util.Locale; @@ -38,25 +43,35 @@ import junit.framework.Assert; * @author Youtao Liu */ public class SyncTestUtils { - public static Drive drive; - public static String folderId; - public static List files; + public static final String KML_FILE_POSTFIX = ".kml"; public static final long MAX_TIME_TO_WAIT_SYNC = 50000; /** - * Updates the Drive object and query files from Google Drive. + * Gets drive object of Google Drive. * - * @param context - * @throws IOException + * @param context the context of application + * @return drive drive object of Google Drive + */ - public static void updateDriveData(Context context) throws IOException { + public static Drive getGoogleDrive(Context context) { String googleAccount = PreferencesUtils.getString(context, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT); GoogleAccountCredential credential = SyncUtils.getCredential(context, googleAccount); - drive = SyncUtils.getDriveService(credential); - folderId = SyncUtils.getMyTracksFolder(context, drive); - files = drive.files().list() + return SyncUtils.getDriveService(credential); + } + + /** + * Queries files from Google Drive. + * + * @param context the context of application + * @param drive drive object of Google Drive + * @return KML file on Google drive + * @throws IOException + */ + public static List updateDriveFiles(Context context, Drive drive) throws IOException { + String folderId = SyncUtils.getMyTracksFolder(context, drive); + return drive.files().list() .setQ(String.format(Locale.US, SyncUtils.GET_KML_FILES_QUERY, folderId)).execute() .getItems(); } @@ -65,40 +80,46 @@ public class SyncTestUtils { * Finds whether a file is existed by the name of track. * * @param trackName name of track + * @param drive drive object of Google Drive + * @return the file be found + * @throws IOException */ - public static File getFile(String trackName) { - File resultFile = null; + public static File getFile(String trackName, Drive drive) throws IOException { + List files = updateDriveFiles(EndToEndTestUtils.activityMytracks.getApplicationContext(), + drive); for (int i = 0; i < files.size(); i++) { File file = files.get(i); String title = file.getTitle(); if (title.equals(trackName + KML_FILE_POSTFIX)) { - resultFile = file; - break; + return file; } } - return resultFile; + return null; } /** * Removes all KML files on Google Drive. * + * @param drive drive object of Google Drive * @throws IOException */ - public static void removeKMLFiles() throws IOException { + public static void removeKMLFiles(Drive drive) throws IOException { + List files = SyncTestUtils.updateDriveFiles( + EndToEndTestUtils.activityMytracks.getApplicationContext(), drive); for (int i = 0; i < files.size(); i++) { File file = files.get(i); - removeFile(file); + removeFile(file, drive); } - } /** * Removes one file on Google Drive. * * @param file the file to remove + * @param drive drive object of Google Drive * @throws IOException */ - public static void removeFile(File file) throws IOException { + public static void removeFile(File file, Drive drive) throws IOException { drive.files().trash(file.getId()).execute(); } @@ -153,12 +174,14 @@ public class SyncTestUtils { * Checks the files number on Google Drive * * @param number number of files on Google Drive + * @param drive drive object of Google Drive * @throws IOException */ - public static void checkFilesNumber(int number) throws IOException { + public static void checkFilesNumber(int number, Drive drive) throws IOException { long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime < MAX_TIME_TO_WAIT_SYNC) { - updateDriveData(EndToEndTestUtils.activityMytracks.getApplicationContext()); + List files = updateDriveFiles( + EndToEndTestUtils.activityMytracks.getApplicationContext(), drive); if (files.size() == number) { return; } @@ -186,17 +209,42 @@ public class SyncTestUtils { * Checks one file on Google Drive * * @param trackName the name of track - * @param expectStatus true means this track should be existed + * @param shouldExist true means this track should be existed + * @param drive drive object of Google Drive + * @return true means the actual result is same as expectation * @throws IOException */ - public static boolean checkFile(String trackName, boolean expectStatus) throws IOException { + public static boolean checkFile(String trackName, boolean shouldExist, Drive drive) + throws IOException { long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime < MAX_TIME_TO_WAIT_SYNC) { - updateDriveData(EndToEndTestUtils.activityMytracks.getApplicationContext()); - if ((getFile(trackName) != null) == expectStatus) { + if ((getFile(trackName, drive) != null) == shouldExist) { return true; } } return false; } + + /** + * Gets the content of a file on Google Drive. + * + * @param file file to read + * @param drive drive object of Google Drive + * @return the string content of the file + * @throws IOException + */ + public static String getContentOfFile(File file, Drive drive) throws IOException { + HttpResponse resp = drive.getRequestFactory() + .buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute(); + InputStream response = resp.getContent(); + BufferedReader br = new BufferedReader(new InputStreamReader(response)); + StringBuilder sb = new StringBuilder(); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + String fileContent = sb.toString(); + br.close(); + return fileContent; + } }