From 98ef2a8975e4529c09ebe70b3628e6a82a15e731 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Tue, 28 Jan 2014 14:30:29 -0800 Subject: [PATCH] Code cleanup: GoogleUtils --- .../endtoendtest/EndToEndTestRunner.java | 2 +- .../mytracks/endtoendtest/GoogleUtils.java | 535 ++++++++---------- .../endtoendtest/RunConfiguration.java | 4 +- .../common/ExportSingleTrackTest.java | 28 +- .../endtoendtest/common/ShareTest.java | 14 +- .../sync/MultiAccountsSyncTest.java | 16 +- .../sync/SyncDriveWithMyTracksTest.java | 2 +- .../sync/SyncMyTracksWithDriveTest.java | 2 +- .../endtoendtest/sync/SyncTestUtils.java | 4 +- 9 files changed, 289 insertions(+), 318 deletions(-) diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestRunner.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestRunner.java index 19ff0de9a..a73a1702c 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestRunner.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/EndToEndTestRunner.java @@ -43,7 +43,7 @@ public class EndToEndTestRunner extends InstrumentationTestRunner { EndToEndTestUtils.emulatorPort = Integer.parseInt(port); } catch (Exception e) { Log.e(EndToEndTestUtils.LOG_TAG, - "Unable to get emulator port parameter, use the default value", e); + "Unable to get emulator port parameter, use the default value.", e); } } Log.i(EndToEndTestUtils.LOG_TAG, "Emulator port: " + EndToEndTestUtils.emulatorPort); 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 2ec50a216..3867c4771 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 @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package com.google.android.apps.mytracks.endtoendtest; import com.google.android.apps.mytracks.Constants; @@ -26,6 +27,7 @@ import com.google.android.apps.mytracks.io.gdata.maps.XmlMapsGDataParserFactory; import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils; import com.google.android.apps.mytracks.io.spreadsheets.SendSpreadsheetsAsyncTask; import com.google.android.apps.mytracks.io.sync.SyncUtils; +import com.google.android.apps.mytracks.util.SystemUtils; import com.google.android.common.gdata.AndroidXmlParserFactory; import com.google.android.maps.mytracks.R; import com.google.api.client.auth.oauth2.BearerToken; @@ -35,7 +37,6 @@ import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccoun import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.drive.Drive; import com.google.api.services.drive.model.File; -import com.google.api.services.drive.model.FileList; import com.google.api.services.fusiontables.Fusiontables; import com.google.api.services.fusiontables.model.Table; import com.google.gdata.client.spreadsheet.SpreadsheetService; @@ -47,7 +48,6 @@ import com.google.wireless.gdata.parser.GDataParser; import android.accounts.Account; import android.accounts.AccountManager; -import android.app.Activity; import android.content.Context; import android.util.Log; @@ -58,374 +58,341 @@ import java.util.List; import java.util.Locale; /** - * Provides utilities to access Google Maps, Google Documents, Google Fusion - * Tables. + * Utilities for accessing Google Drive, Google Maps, Google Fusion Tables, and + * Google Spreadsheets. * * @author Youtao Liu */ public class GoogleUtils { - public static final String DOCUMENT_NAME_PREFIX = "My Tracks"; - // This is the preferred account. - 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; - private static final String WORK_SHEET_NAME = "Log"; - private static final String TRANCK_NAME_COLUMN = "Name"; + public static final String ACCOUNT_1 = "mytrackstest@gmail.com"; + public static final String ACCOUNT_2 = "mytrackstest2@gmail.com"; - private static final String TEST_TRACKS_QUERY = "'root' in parents and title contains '" + private static final String MY_TRACKS_PREFIX = "My Tracks"; + private static final String SPREADSHEETS_NAME = MY_TRACKS_PREFIX + "-" + + EndToEndTestUtils.activityType; + + private static final String SPREADSHEETS_WORKSHEET_NAME = "Log"; + private static final String SPREADSHEETS_TRANCK_NAME_COLUMN = "Name"; + private static final String DRIVE_TEST_FILES_QUERY = "'root' in parents and title contains '" + EndToEndTestUtils.TRACK_NAME_PREFIX + "' and trashed = false"; /** - * Gets the account to access Google Services. + * Deletes Google Drive test files. * - * @param context context used to get account - * @return the first account which is bound with current device + * @param context the context + * @param accountName the account name */ - private static Account getAccount(Context context) { - return AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE)[0]; - } - - /** - * Gets Google maps of a user. - * - * @param context used to get maps - * @param mapsClient the client to access Google Maps - * @return true means set successfully - */ - private static ArrayList getMaps(Context context, MapsClient mapsClient) { - String authToken = null; - ArrayList mapIds = new ArrayList(); - ArrayList mapData = new ArrayList(); - + public static void deleteDriveTestFiles(Context context, String accountName) { try { - authToken = AccountManager.get(context).blockingGetAuthToken(getAccount(context), - MapsConstants.SERVICE_NAME, false); - } catch (Exception e) { - Log.d(EndToEndTestUtils.LOG_TAG, "Unable to get auth token", e); - return mapData; - } - - GDataParser gDataParser = null; - try { - gDataParser = mapsClient.getParserForFeed(MapFeatureEntry.class, MapsClient.getMapsFeed(), - authToken); - gDataParser.init(); - while (gDataParser.hasMoreData()) { - MapFeatureEntry entry = (MapFeatureEntry) gDataParser.readNextEntry(null); - mapIds.add(MapsGDataConverter.getMapidForEntry(entry)); - mapData.add(MapsGDataConverter.getMapMetadataForEntry(entry)); - } - } catch (Exception e) { - Log.d(EndToEndTestUtils.LOG_TAG, "Unable to get maps", e); - } finally { - if (gDataParser != null) { - gDataParser.close(); - } - } - - return mapData; - } - - /** - * Searches a map in user's Google Maps. - * - * @param title the title of map - * @param activity activity to get context - * @param isDelete whether delete the map of this track in the Google Maps - * @return true means find the map - */ - private static boolean searchMapByTitle(String title, Activity activity, boolean isDelete) { - Context context = activity.getApplicationContext(); - MapsClient mapsClient = new MapsClient(GDataClientFactory.getGDataClient(context), - new XmlMapsGDataParserFactory(new AndroidXmlParserFactory())); - ArrayList mapData = getMaps(context, mapsClient); - for (MapsMapMetadata oneData : mapData) { - if (oneData.getDescription().indexOf(DOCUMENT_NAME_PREFIX) > -1 - && oneData.getTitle().equals(title)) { - if (isDelete) { - try { - mapsClient.deleteEntry(oneData.getGDataEditUri(), AccountManager.get(context) - .blockingGetAuthToken(getAccount(context), MapsConstants.SERVICE_NAME, false)); - return true; - } catch (Exception e) { - Log.d(EndToEndTestUtils.LOG_TAG, "Unable to drop map", e); - return false; - } - } - return true; - } - } - return false; - } - - /** - * Searches a map in user's Google Maps. - * - * @param title the title of map - * @param activity activity to get context - * @return true means find the map - */ - public static boolean searchMap(String title, Activity activity) { - return searchMapByTitle(title, activity, false); - } - - /** - * Searches a map in user's Google Maps and then delete it. - * - * @param title the title of map - * @param activity activity to get context - * @return true means find the map and delete it successfully - */ - public static boolean deleteMap(String title, Activity activity) { - return searchMapByTitle(title, activity, true); - } - - /** - * Removes old tracks created by MyTracks test. - * - * @param activity - * @param accountName - */ - public static void deleteTestTracksOnGoogleDrive(Activity activity, String accountName) { - try { - GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential( - activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE); - if (driveCredential == null) { + GoogleAccountCredential googleAccountCredential = SendToGoogleUtils + .getGoogleAccountCredential(context, accountName, SendToGoogleUtils.DRIVE_SCOPE); + if (googleAccountCredential == null) { return; } - Drive drive = SyncUtils.getDriveService(driveCredential); - com.google.api.services.drive.Drive.Files.List list = drive.files().list() - .setQ(TEST_TRACKS_QUERY); + Drive drive = SyncUtils.getDriveService(googleAccountCredential); + com.google.api.services.drive.Drive.Files.List list = drive.files() + .list().setQ(DRIVE_TEST_FILES_QUERY); List files = list.execute().getItems(); - for (Iterator iterator = files.iterator(); iterator.hasNext();) { + Iterator iterator = files.iterator(); + while (iterator.hasNext()) { File file = (File) iterator.next(); drive.files().delete(file.getId()).execute(); } } catch (Exception e) { - Log.e(EndToEndTestUtils.LOG_TAG, "Delete test tracks failed."); + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to delete Google Drive test files.", e); } } /** - * Removes old tracks on Google Maps created by MyTracks test. + * Deletes Google Maps of the first Google account. * - * @param activity + * @param context the context + * @param accountName the account name + * @param mapTitle the map title + * @return true if deletion is success. */ - public static void deleteTracksOnGoogleMaps(Activity activity) { - Context context = activity.getApplicationContext(); + public static boolean deleteMaps(Context context, String accountName, String mapTitle) { + Account account = getGoogleAccount(context, accountName); + if (account == null) { + return false; + } MapsClient mapsClient = new MapsClient(GDataClientFactory.getGDataClient(context), new XmlMapsGDataParserFactory(new AndroidXmlParserFactory())); - ArrayList mapData = getMaps(context, mapsClient); - for (MapsMapMetadata oneData : mapData) { - - try { - mapsClient.deleteEntry(oneData.getGDataEditUri(), AccountManager.get(context) - .blockingGetAuthToken(getAccount(context), MapsConstants.SERVICE_NAME, false)); - } catch (Exception e) { - Log.d(EndToEndTestUtils.LOG_TAG, "Unable to drop map", e); + ArrayList mapMetadata = getMaps(context, account, mapsClient); + for (MapsMapMetadata data : mapMetadata) { + if (data.getDescription().indexOf(MY_TRACKS_PREFIX) > -1 + && data.getTitle().equals(mapTitle)) { + try { + mapsClient.deleteEntry(data.getGDataEditUri(), AccountManager.get(context) + .blockingGetAuthToken(account, MapsConstants.SERVICE_NAME, false)); + return true; + } catch (Exception e) { + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to delete maps.", e); + return false; + } } } - } - - /** - * Delete spreadsheet which name is title. - * - * @param title the name of spreadsheet - * @param activity to get context - * @param accountName the name of Google account - * @return true means delete successfully - */ - public static boolean deleteSpreadsheetByTitle(String title, Activity activity, String accountName) { - try { - GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential( - activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE); - if (driveCredential == null) { - return false; - } - - Drive drive = SyncUtils.getDriveService(driveCredential); - com.google.api.services.drive.Drive.Files.List list = drive.files().list() - .setQ(String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, title)); - List files = list.execute().getItems(); - for (Iterator iterator = files.iterator(); iterator.hasNext();) { - File file = (File) iterator.next(); - drive.files().delete(file.getId()).execute(); - } - return true; - } catch (Exception e) { - Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed."); - } return false; } /** - * Searches docs in user's Google Documents. + * Deletes all Google Maps of the first Google account * - * @param title the title of doc - * @param activity to get context - * @param accountName the name of Google account - * @return the file list of the document, null means can not find the - * spreadsheets + * @param context the context + * @param accountName the account name */ - public static List searchAllSpreadsheetByTitle(String title, Activity activity, - String accountName) { + public static void deleteAllMaps(Context context, String accountName) { + Account account = getGoogleAccount(context, accountName); + if (account == null) { + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to get account."); + return; + } + MapsClient mapsClient = new MapsClient(GDataClientFactory.getGDataClient(context), + new XmlMapsGDataParserFactory(new AndroidXmlParserFactory())); + ArrayList mapMetadata = getMaps(context, account, mapsClient); + for (MapsMapMetadata oneData : mapMetadata) { + try { + mapsClient.deleteEntry(oneData.getGDataEditUri(), AccountManager.get(context) + .blockingGetAuthToken(account, MapsConstants.SERVICE_NAME, false)); + } catch (Exception e) { + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to delete maps.", e); + } + } + } + + /** + * Deletes Google Fusion Tables. + * + * @param context the context + * @param accountName the account name + * @param tableName the table name + * @return true if deletion is success. + */ + public static boolean deleteFusionTables(Context context, String accountName, String tableName) { try { - GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential( - activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE); - if (driveCredential == null) { + GoogleAccountCredential googleAccountCredential = SendToGoogleUtils + .getGoogleAccountCredential(context, accountName, SendToGoogleUtils.FUSION_TABLES_SCOPE); + if (googleAccountCredential == null) { + return false; + } + Fusiontables fusiontables = new Fusiontables.Builder( + AndroidHttp.newCompatibleTransport(), new GsonFactory(), googleAccountCredential).build(); + List tables = fusiontables.table().list().execute().getItems(); + Iterator
iterator = tables.iterator(); + while (iterator.hasNext()) { + Table table = (Table) iterator.next(); + String name = table.getName(); + if (name != null && name.equals(tableName)) { + fusiontables.table().delete(table.getTableId()).execute(); + return true; + } + } + } catch (Exception e) { + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to search fusion tables.", e); + } + return false; + } + + /** + * Searches Google Spreadsheets. + * + * @param context the context + * @param accountName the account name + * @return the list of spreadsheets matching the title. Null if unable to + * search. + */ + public static List searchSpreadsheets(Context context, String accountName) { + try { + GoogleAccountCredential googleAccountCredential = SendToGoogleUtils + .getGoogleAccountCredential(context, accountName, SendToGoogleUtils.DRIVE_SCOPE); + if (googleAccountCredential == null) { return null; } - Drive drive = SyncUtils.getDriveService(driveCredential); - com.google.api.services.drive.Drive.Files.List list = drive.files().list() - .setQ(String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, title)); - FileList result = list.execute(); - return result.getItems(); + Drive drive = SyncUtils.getDriveService(googleAccountCredential); + com.google.api.services.drive.Drive.Files.List list = drive.files().list().setQ(String.format( + Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, SPREADSHEETS_NAME)); + return list.execute().getItems(); } catch (Exception e) { - Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed."); + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to search spreadsheets.", e); } return null; } /** - * Searches a track title in a spreadsheet. + * Deletes Google Spreadsheets. * - * @param trackName the track name to search - * @param activity to get context - * @param spreadsheetTitle the title of spreadsheet - * @param isDelete whether delete the information of this track in the - * document - * @param accountName the name of Google account - * @return true means find the track name in the spreadsheet + * @param context the context + * @param accountName the account name + * @return true if deletion is success. */ - private static boolean searchTrackTitleInSpreadsheet(String trackName, Activity activity, - String spreadsheetTitle, boolean isDelete, String accountName) { + public static boolean deleteSpreadsheets(Context context, String accountName) { try { - // Get spreadsheet Id. - String spreadsheetId = searchAllSpreadsheetByTitle(spreadsheetTitle, activity, accountName) - .get(0).getId(); + GoogleAccountCredential googleAccountCredential = SendToGoogleUtils + .getGoogleAccountCredential(context, accountName, SendToGoogleUtils.DRIVE_SCOPE); + if (googleAccountCredential == null) { + return false; + } - // Get spreadsheet service. - SpreadsheetService spreadsheetService = new SpreadsheetService(spreadsheetTitle); + Drive drive = SyncUtils.getDriveService(googleAccountCredential); + com.google.api.services.drive.Drive.Files.List list = drive.files().list().setQ(String.format( + Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, SPREADSHEETS_NAME)); + List files = list.execute().getItems(); + Iterator iterator = files.iterator(); + while (iterator.hasNext()) { + File file = (File) iterator.next(); + drive.files().delete(file.getId()).execute(); + } + return true; + } catch (Exception e) { + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to delete spreadsheets.", e); + } + return false; + } + + /** + * Deletes Google Spreadsheets row. + * + * @param context the context + * @param accountName the account name + * @param trackName the track name + * @return true if deletion is success. + */ + public static boolean deleteSpreadsheetsRow( + Context context, String accountName, String trackName) { + try { + // Get spreadsheet Id + List files = searchSpreadsheets(context, accountName); + if (files == null || files.size() == 0) { + return false; + } + String spreadsheetId = files.get(0).getId(); + + // Get spreadsheet service + SpreadsheetService spreadsheetService = new SpreadsheetService( + "MyTracks-" + SystemUtils.getMyTracksVersion(context)); Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); - credential.setAccessToken(SendToGoogleUtils.getToken(activity.getApplicationContext(), - accountName, SendToGoogleUtils.SPREADSHEETS_SCOPE)); + credential.setAccessToken( + SendToGoogleUtils.getToken(context, accountName, SendToGoogleUtils.SPREADSHEETS_SCOPE)); spreadsheetService.setOAuth2Credentials(credential); - // Get work sheet. - WorksheetFeed feed = spreadsheetService.getFeed( - new URL(String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_WORKSHEETS_URI, - spreadsheetId)), WorksheetFeed.class); - List data = feed.getEntries(); - for (Iterator iterator = data.iterator(); iterator.hasNext();) { - WorksheetEntry worksheetEntry = (WorksheetEntry) iterator.next(); - String title = worksheetEntry.getTitle().getPlainText(); - if (title.equals(WORK_SHEET_NAME)) { - URL listFeedUrl = worksheetEntry.getListFeedUrl(); - List listFeed = spreadsheetService.getFeed(listFeedUrl, ListFeed.class) - .getEntries(); - for (Iterator iterator2 = listFeed.iterator(); iterator2.hasNext();) { - ListEntry listEntry = (ListEntry) iterator2.next(); - String name = listEntry.getCustomElements().getValue(TRANCK_NAME_COLUMN); + // Get work sheet + WorksheetFeed worksheetFeed = spreadsheetService.getFeed(new URL( + String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_WORKSHEETS_URI, spreadsheetId)), + WorksheetFeed.class); + Iterator worksheetEntryIterator = worksheetFeed.getEntries().iterator(); + while (worksheetEntryIterator.hasNext()) { + WorksheetEntry worksheetEntry = (WorksheetEntry) worksheetEntryIterator.next(); + String worksheetTitle = worksheetEntry.getTitle().getPlainText(); + if (worksheetTitle.equals(SPREADSHEETS_WORKSHEET_NAME)) { + URL url = worksheetEntry.getListFeedUrl(); + Iterator listEntryIterator = spreadsheetService.getFeed(url, ListFeed.class) + .getEntries().iterator(); + while (listEntryIterator.hasNext()) { + ListEntry listEntry = (ListEntry) listEntryIterator.next(); + String name = listEntry.getCustomElements().getValue(SPREADSHEETS_TRANCK_NAME_COLUMN); if (name.equals(trackName)) { - if (isDelete) { - listEntry.delete(); - } + listEntry.delete(); return true; } } } } } catch (Exception e) { - Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed."); + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to delete spreadsheets row.", e); } return false; } /** - * Searches and deletes a track in spreadsheet. - * - * @param title the track name to search - * @param activity to get context - * @return true means find and delete successfully - */ - public static boolean deleteTrackInSpreadSheet(String title, Activity activity, String account) { - return searchTrackTitleInSpreadsheet(title, activity, GoogleUtils.SPREADSHEET_NAME, true, - account); - } - - /** - * Searches a fusion table in user's Google tables. - * - * @param tableName name of fusion table to search - * @param context android context - * @param accountName name of account - * @param isDelete whether delete this track - * @return true means find and delete fusion table - */ - public static boolean searchFusionTableByTitle(String tableName, Context context, - String accountName, boolean isDelete) { - try { - GoogleAccountCredential credential = SendToGoogleUtils.getGoogleAccountCredential(context, - accountName, SendToGoogleUtils.FUSION_TABLES_SCOPE); - if (credential == null) { - return false; - } - Fusiontables fusiontables = new Fusiontables.Builder(AndroidHttp.newCompatibleTransport(), - new GsonFactory(), credential).build(); - List
tables = fusiontables.table().list().execute().getItems(); - for (Iterator
iterator = tables.iterator(); iterator.hasNext();) { - Table table = (Table) iterator.next(); - String title = table.getName(); - if (title.equals(tableName)) { - if (isDelete) { - fusiontables.table().delete(table.getTableId()).execute(); - } - return true; - } - } - - } catch (Exception e) { - Log.d(EndToEndTestUtils.LOG_TAG, "Failed when operate fusion table.", e); - } - return false; - } - - /** - * Checks whether the status of account is right to use. - * - * @return true means the status of account is good for sending + * Returns true if an account is available. */ public static boolean isAccountAvailable() { - // Check whether no account is binded with this device. + // Check the no account dialog if (EndToEndTestUtils.SOLO.waitForText( EndToEndTestUtils.activityMytracks.getString(R.string.send_google_no_account_title), 1, EndToEndTestUtils.SHORT_WAIT_TIME)) { EndToEndTestUtils.getButtonOnScreen( - EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true); + EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), false, true); return false; } - // Check whether need to choose account. + // Check the choose account dialog if (EndToEndTestUtils.SOLO.waitForText( EndToEndTestUtils.activityMytracks.getString(R.string.send_google_choose_account_title), 1, EndToEndTestUtils.SHORT_WAIT_TIME)) { - EndToEndTestUtils.SOLO.clickOnText(GoogleUtils.ACCOUNT_NAME_1); + EndToEndTestUtils.SOLO.clickOnText(ACCOUNT_1); EndToEndTestUtils.getButtonOnScreen( EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), false, true); } - // Check whether no account permission. + // Check the no account permission dialog if (EndToEndTestUtils.SOLO.waitForText( - EndToEndTestUtils.activityMytracks.getString(R.string.send_google_no_account_permission), - 1, EndToEndTestUtils.SHORT_WAIT_TIME)) { + EndToEndTestUtils.activityMytracks.getString(R.string.send_google_no_account_permission), 1, + EndToEndTestUtils.SHORT_WAIT_TIME)) { return false; } return true; } + /** + * Gets the google account. + * + * @param context the context + * @param accountName the account name + */ + private static Account getGoogleAccount(Context context, String accountName) { + Account[] accounts = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE); + for (Account account : accounts) { + if (account.name.equals(accountName)) { + return account; + } + } + return null; + } + + /** + * Gets Google Maps. + * + * @param context the context + * @param account the account + * @param mapsClient the map client + * @return a list of maps. + */ + private static ArrayList getMaps( + Context context, Account account, MapsClient mapsClient) { + String authToken = null; + ArrayList mapIds = new ArrayList(); + ArrayList mapMetadata = new ArrayList(); + + try { + authToken = AccountManager.get(context) + .blockingGetAuthToken(account, MapsConstants.SERVICE_NAME, false); + } catch (Exception e) { + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to get auth token.", e); + return mapMetadata; + } + + GDataParser gDataParser = null; + try { + gDataParser = mapsClient.getParserForFeed( + MapFeatureEntry.class, MapsClient.getMapsFeed(), authToken); + gDataParser.init(); + while (gDataParser.hasMoreData()) { + MapFeatureEntry entry = (MapFeatureEntry) gDataParser.readNextEntry(null); + mapIds.add(MapsGDataConverter.getMapidForEntry(entry)); + mapMetadata.add(MapsGDataConverter.getMapMetadataForEntry(entry)); + } + } catch (Exception e) { + Log.e(EndToEndTestUtils.LOG_TAG, "Unable to get maps.", e); + } finally { + if (gDataParser != null) { + gDataParser.close(); + } + } + return mapMetadata; + } } \ No newline at end of file diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/RunConfiguration.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/RunConfiguration.java index 60687b4b7..210c121d5 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/RunConfiguration.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/RunConfiguration.java @@ -123,9 +123,9 @@ public class RunConfiguration { EndToEndTestUtils.findMenuItem( EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_drive), true); boolean canRun = EndToEndTestUtils.SOLO.waitForText( - GoogleUtils.ACCOUNT_NAME_1, 1, EndToEndTestUtils.SHORT_WAIT_TIME) + GoogleUtils.ACCOUNT_1, 1, EndToEndTestUtils.SHORT_WAIT_TIME) && EndToEndTestUtils.SOLO.waitForText( - GoogleUtils.ACCOUNT_NAME_2, 1, EndToEndTestUtils.TINY_WAIT_TIME); + GoogleUtils.ACCOUNT_2, 1, EndToEndTestUtils.TINY_WAIT_TIME); EndToEndTestUtils.SOLO.clickOnText( EndToEndTestUtils.activityMytracks.getString(R.string.generic_cancel)); return canRun; diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/ExportSingleTrackTest.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/ExportSingleTrackTest.java index db14f96e9..4ea07a45d 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/ExportSingleTrackTest.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/ExportSingleTrackTest.java @@ -47,8 +47,9 @@ public class ExportSingleTrackTest extends ActivityInstrumentationTestCase2 fileList = GoogleUtils.searchAllSpreadsheetByTitle( - GoogleUtils.DOCUMENT_NAME_PREFIX + "-" + EndToEndTestUtils.activityType, - activityMyTracks, GoogleUtils.ACCOUNT_NAME_1); + List fileList = GoogleUtils.searchSpreadsheets( + activityMyTracks.getApplicationContext(), GoogleUtils.ACCOUNT_1); assertEquals(1, fileList.size()); } } @@ -112,11 +112,15 @@ public class ExportSingleTrackTest extends ActivityInstrumentationTestCase2