From 880f153ed20eb2e45733ed1c945fb12ee1c6c01c Mon Sep 17 00:00:00 2001 From: youtaol Date: Wed, 30 Jan 2013 21:39:15 +0800 Subject: [PATCH] Add some test cases about sync feature. --- .../apps/mytracks/io/sync/SyncUtils.java | 7 +- .../endtoendtest/EndToEndTestUtils.java | 2 - .../mytracks/endtoendtest/GoogleUtils.java | 3 +- .../mytracks/endtoendtest/SettingTest.java | 4 +- .../sync/MultiAccountsSyncTest.java | 122 ++++++++++++++++++ .../sync/SyncDriveWithMyTracksTest.java | 35 +++-- .../sync/SyncMyTracksWithDriveTest.java | 56 +++++++- .../endtoendtest/sync/SyncTestUtils.java | 70 ++++++++-- 8 files changed, 267 insertions(+), 32 deletions(-) create mode 100644 MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/sync/MultiAccountsSyncTest.java diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java index 53e71a149..40aa84998 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java @@ -38,6 +38,7 @@ import com.google.api.services.drive.DriveScopes; import com.google.api.services.drive.model.File; import com.google.api.services.drive.model.FileList; import com.google.api.services.drive.model.ParentReference; +import com.google.common.annotations.VisibleForTesting; import android.accounts.Account; import android.accounts.AccountManager; @@ -74,9 +75,13 @@ public class SyncUtils { public static final String KML_MIME_TYPE = "application/vnd.google-earth.kml+xml"; public static final String GET_KML_FILES_QUERY = "'%s' in parents and mimeType = '" + KML_MIME_TYPE + "' and trashed = false"; + + public static final String GET_MYTRACKS_FOLDER_QUERY = "'%s' in parents and mimeType = '" + + KML_MIME_TYPE + "' and trashed = false"; private static final String FOLDER_MIME_TYPE = "application/vnd.google-apps.folder"; - private static final String GET_MY_TRACKS_FOLDER_QUERY = + @VisibleForTesting + public static final String GET_MY_TRACKS_FOLDER_QUERY = "'root' in parents and title = '%s' and mimeType = '" + FOLDER_MIME_TYPE + "' and trashed = false"; diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java index 3dda01a13..2798b4225 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestUtils.java @@ -136,8 +136,6 @@ public class EndToEndTestUtils { public static boolean isCheckedFirstLaunch = false; public static boolean isGooglePlayServicesLatest = true; - private EndToEndTestUtils() {} - /** * Checks the language, then sets the fields with right string. */ diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/GoogleUtils.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/GoogleUtils.java index 78911bb23..fa3feb179 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/GoogleUtils.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/GoogleUtils.java @@ -64,7 +64,8 @@ import java.util.Locale; */ public class GoogleUtils { public static final String DOCUMENT_NAME_PREFIX = "My Tracks"; - public static final String ACCOUNT_NAME = "mytrackstest@gmail.com"; + public static final String ACCOUNT_NAME_1 = "mytrackstest@gmail.com"; + public static final String ACCOUNT_NAME_2 = "mytrackstest2@gmail.com"; public static final String SPREADSHEET_NAME = DOCUMENT_NAME_PREFIX + "-" + EndToEndTestUtils.activityType; /** diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java index e14530d2c..918dafa79 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java @@ -412,9 +412,9 @@ public class SettingTest extends ActivityInstrumentationTestCase2 { + + public static Drive drive; + private Instrumentation instrumentation; + private TrackListActivity trackListActivity; + + public MultiAccountsSyncTest() { + super(TrackListActivity.class); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + instrumentation = getInstrumentation(); + trackListActivity = getActivity(); + EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity); + drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1); + EndToEndTestUtils.deleteAllTracks(); + } + + /** + * Tests sync tracks with Google Drive of two accounts. + * + * @throws IOException + */ + public void testSyncTracksWithMultiAccounts() throws IOException { + // Create tracks with first track. + EndToEndTestUtils.createSimpleTrack(0, true); + + // Create tracks with second track. + SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_2); + EndToEndTestUtils.createSimpleTrack(0, true); + + // Sync with Google Drive and then check it of the second account. + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_2); + SyncTestUtils.checkFilesNumber(drive); + + // Check Google Drive of the first account. + SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1); + SyncTestUtils.checkFilesNumber(drive); + } + + /** + * Creates three tracks and the deletes one in one account, and then deletes + * another one in another account. Keeps one tracks, then sync with two + * accounts. + * + * @throws IOException + */ + public void testDeleteTracksWithMultiAccounts() throws IOException { + EndToEndTestUtils.createSimpleTrack(0, true); + EndToEndTestUtils.createSimpleTrack(0, true); + EndToEndTestUtils.createSimpleTrack(0, true); + + // Delete one track. + EndToEndTestUtils.SOLO.clickOnView(EndToEndTestUtils.SOLO.getCurrentListViews().get(0) + .getChildAt(0)); + EndToEndTestUtils.SOLO.clickOnMenuItem(EndToEndTestUtils.activityMytracks + .getString(R.string.menu_delete)); + + // Switch account and delete another track. + SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_2); + EndToEndTestUtils.SOLO.clickOnView(EndToEndTestUtils.SOLO.getCurrentListViews().get(0) + .getChildAt(0)); + EndToEndTestUtils.SOLO.clickOnMenuItem(EndToEndTestUtils.activityMytracks + .getString(R.string.menu_delete)); + + // Check Google Drive of the first account. + SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1); + SyncTestUtils.checkFilesNumber(drive); + + // Check Google Drive of the second account. + SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_2); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_2); + SyncTestUtils.checkFilesNumber(drive); + + } + +} 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 70bb3552f..95a515b86 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 @@ -48,8 +48,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< instrumentation = getInstrumentation(); trackListActivity = getActivity(); EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity); - SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME); - drive = SyncTestUtils.getGoogleDrive(trackListActivity.getApplicationContext()); + drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1); } /** @@ -61,7 +60,23 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< EndToEndTestUtils.deleteAllTracks(); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.checkFilesNumber(0, drive); + SyncTestUtils.checkFilesNumber(drive); + } + + /** + * Deletes one track in MyTracks and checks it in Google Drive. + * + * @throws IOException + */ + public void testDeleteOneTracksInMyTracks() throws IOException { + EndToEndTestUtils.createTrackIfEmpty(2, false); + EndToEndTestUtils.SOLO.clickOnMenuItem(EndToEndTestUtils.activityMytracks + .getString(R.string.menu_delete)); + EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks + .getString(R.string.generic_ok)); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + SyncTestUtils.checkFile(EndToEndTestUtils.trackName, false, drive); } /** @@ -76,7 +91,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< EndToEndTestUtils.createSimpleTrack(3, true); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.checkFilesNumber(2, drive); + SyncTestUtils.checkFilesNumber(drive); } /** @@ -90,8 +105,9 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< // Sync this track. EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - assertTrue(SyncTestUtils.checkFile(EndToEndTestUtils.trackName, true, drive)); - String oldTrack = SyncTestUtils.getContentOfFile(SyncTestUtils.getFile(EndToEndTestUtils.trackName, drive), drive); + SyncTestUtils.checkFile(EndToEndTestUtils.trackName, true, drive); + String oldTrack = SyncTestUtils.getContentOfFile( + SyncTestUtils.getFile(EndToEndTestUtils.trackName, drive), drive); String oldTrackName = EndToEndTestUtils.trackName; @@ -117,9 +133,10 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2< EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); // Check. - assertTrue(SyncTestUtils.checkFile(oldTrackName, false, drive)); - assertTrue(SyncTestUtils.checkFile(newTrackName, true, drive)); - String newTrack = SyncTestUtils.getContentOfFile(SyncTestUtils.getFile(newTrackName,drive), drive); + SyncTestUtils.checkFile(oldTrackName, false, drive); + 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); 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 d5fd32527..8791e04fb 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 @@ -20,6 +20,7 @@ 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 com.google.api.services.drive.model.File; import android.app.Instrumentation; import android.test.ActivityInstrumentationTestCase2; @@ -47,25 +48,70 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2< instrumentation = getInstrumentation(); trackListActivity = getActivity(); EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity); - SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME); - drive = SyncTestUtils.getGoogleDrive(trackListActivity.getApplicationContext()); + drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1); } /** - * Deletes all tracks in Google Drive and checks in MyTracks. + * Deletes all tracks on Google Drive and checks in MyTracks. * * @throws IOException */ - public void testDeleteAllTracksInDrive() throws IOException { + public void testDeleteAllTracksOnDrive() throws IOException { EndToEndTestUtils.deleteAllTracks(); EndToEndTestUtils.createTrackIfEmpty(0, true); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); - SyncTestUtils.checkFilesNumber(1, drive); + SyncTestUtils.checkFilesNumber(drive); SyncTestUtils.removeKMLFiles(drive); EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); SyncTestUtils.checkTracksNumber(0); } + + /** + * Deletes one file on Google Drive and checks it in MyTracks. + * + * @throws IOException + */ + public void testDeleteOneFileOnDrive() throws IOException { + EndToEndTestUtils.deleteAllTracks(); + instrumentation.waitForIdleSync(); + EndToEndTestUtils.createSimpleTrack(0, true); + EndToEndTestUtils.createSimpleTrack(0, true); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + SyncTestUtils.checkFilesNumber(drive); + + // Remove one track from Google Drive + File file = SyncTestUtils.getFile(EndToEndTestUtils.trackName, drive); + SyncTestUtils.removeFile(file, drive); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + SyncTestUtils.checkFilesNumber(drive); + SyncTestUtils.checkTracksNumber(1); + } + + /** + * Tests deleting and creating MyTracks folder on Google Dive by MyTracks. + * + * @throws IOException + */ + public void testCreateMyTracksOnDrive() throws IOException { + EndToEndTestUtils.deleteAllTracks(); + instrumentation.waitForIdleSync(); + EndToEndTestUtils.createSimpleTrack(0, true); + EndToEndTestUtils.createSimpleTrack(0, true); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + instrumentation.waitForIdleSync(); + SyncTestUtils.checkFilesNumber(drive); + File folder = SyncTestUtils.getMyTracksFolder(trackListActivity.getApplicationContext(), drive); + assertNotNull(folder); + + SyncTestUtils.removeFile(folder, drive); + EndToEndTestUtils.findMenuItem( + EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); + SyncTestUtils.checkFilesNumber(drive); + } } 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 90aa9caaa..6da96aac0 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,18 +20,22 @@ 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.googleapis.json.GoogleJsonResponseException; 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 com.google.api.services.drive.model.FileList; import android.content.Context; +import android.util.Log; import android.widget.CheckBox; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -47,12 +51,22 @@ public class SyncTestUtils { public static final String KML_FILE_POSTFIX = ".kml"; public static final long MAX_TIME_TO_WAIT_SYNC = 50000; + /** + * Setups sync tests. + * + * @param accountName the name of account + * @return drive object of Goolge Drive + */ + public static Drive setUpForSyncTest(String accountName) { + SyncTestUtils.enableSync(accountName); + return SyncTestUtils.getGoogleDrive(EndToEndTestUtils.activityMytracks.getApplicationContext()); + } + /** * Gets drive object of Google Drive. * * @param context the context of application * @return drive drive object of Google Drive - */ public static Drive getGoogleDrive(Context context) { String googleAccount = PreferencesUtils.getString(context, R.string.google_account_key, @@ -62,7 +76,7 @@ public class SyncTestUtils { } /** - * Queries files from Google Drive. + * Queries KML files from Google Drive. * * @param context the context of application * @param drive drive object of Google Drive @@ -70,7 +84,11 @@ public class SyncTestUtils { * @throws IOException */ public static List updateDriveFiles(Context context, Drive drive) throws IOException { - String folderId = SyncUtils.getMyTracksFolder(context, drive); + File folder = SyncTestUtils.getMyTracksFolder(context, drive); + if ( folder == null) { + return new ArrayList(); + } + String folderId = folder.getId(); return drive.files().list() .setQ(String.format(Locale.US, SyncUtils.GET_KML_FILES_QUERY, folderId)).execute() .getItems(); @@ -164,26 +182,29 @@ public class SyncTestUtils { EndToEndTestUtils.SHORT_WAIT_TIME); EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks .getString(R.string.generic_ok)); - - EndToEndTestUtils.SOLO.goBack(); - EndToEndTestUtils.SOLO.goBack(); } + EndToEndTestUtils.SOLO.goBack(); + EndToEndTestUtils.SOLO.goBack(); } /** * 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, Drive drive) throws IOException { + public static void checkFilesNumber(Drive drive) throws IOException { long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime < MAX_TIME_TO_WAIT_SYNC) { - List files = updateDriveFiles( - EndToEndTestUtils.activityMytracks.getApplicationContext(), drive); - if (files.size() == number) { - return; + try { + int trackNumber = EndToEndTestUtils.SOLO.getCurrentListViews().get(0).getCount(); + List files = updateDriveFiles( + EndToEndTestUtils.activityMytracks.getApplicationContext(), drive); + if (files.size() == trackNumber) { + return; + } + } catch (GoogleJsonResponseException e) { + EndToEndTestUtils.sleep(EndToEndTestUtils.SHORT_WAIT_TIME); } } Assert.fail(); @@ -222,6 +243,7 @@ public class SyncTestUtils { return true; } } + Assert.fail(); return false; } @@ -247,4 +269,28 @@ public class SyncTestUtils { br.close(); return fileContent; } + + /** + * Gets the MyTracks folder on Google Drive. + * + * @param context context of application + * @param drive drive object of Google Drive + * @return the MyTracks folder on Google Drive + */ + public static File getMyTracksFolder(Context context, Drive drive) { + try { + String folderName = context.getString(R.string.my_tracks_app_name); + com.google.api.services.drive.Drive.Files.List list = drive.files().list() + .setQ(String.format(Locale.US, SyncUtils.GET_MY_TRACKS_FOLDER_QUERY, folderName)); + FileList result = list.execute(); + for (File file : result.getItems()) { + if (file.getTitle().equals(folderName)) { + return file; + } + } + } catch (IOException e) { + Log.e(EndToEndTestUtils.LOG_TAG, "IOException", e); + } + return null; + } }