merge branches

This commit is contained in:
Jimmy Shih
2013-02-20 13:05:48 -08:00
5 changed files with 230 additions and 191 deletions
@@ -76,7 +76,11 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
return false; return false;
} }
Drive drive = SyncUtils.getDriveService(credential); Drive drive = SyncUtils.getDriveService(credential);
String folderId = SyncUtils.getMyTracksFolder(context, drive).getId(); File folder = SyncUtils.getMyTracksFolder(context, drive);
if (folder == null) {
return false;
}
String folderId = folder.getId();
if (folderId == null) { if (folderId == null) {
return false; return false;
} }
@@ -38,6 +38,7 @@ import com.google.api.client.http.ByteArrayContent;
import com.google.api.services.drive.Drive; import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File; import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList; import com.google.api.services.drive.model.FileList;
import com.google.common.annotations.VisibleForTesting;
import com.google.gdata.client.spreadsheet.SpreadsheetService; import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.ListEntry; import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.WorksheetEntry; import com.google.gdata.data.spreadsheet.WorksheetEntry;
@@ -66,10 +67,12 @@ public class SendSpreadsheetsAsyncTask extends AbstractSendAsyncTask {
GOOGLE_SPREADSHEET_MIME_TYPE = "application/vnd.google-apps.spreadsheet"; GOOGLE_SPREADSHEET_MIME_TYPE = "application/vnd.google-apps.spreadsheet";
private static final String private static final String
OPENDOCUMENT_SPREADSHEET_MIME_TYPE = "application/x-vnd.oasis.opendocument.spreadsheet"; OPENDOCUMENT_SPREADSHEET_MIME_TYPE = "application/x-vnd.oasis.opendocument.spreadsheet";
private static final String GET_SPREADSHEET_QUERY = @VisibleForTesting
public static final String GET_SPREADSHEET_QUERY =
"'root' in parents and title = '%s' and mimeType = '" + GOOGLE_SPREADSHEET_MIME_TYPE "'root' in parents and title = '%s' and mimeType = '" + GOOGLE_SPREADSHEET_MIME_TYPE
+ "' and trashed = false"; + "' and trashed = false";
private static final String @VisibleForTesting
public static final String
GET_WORKSHEETS_URI = "https://spreadsheets.google.com/feeds/worksheets/%s/private/full"; GET_WORKSHEETS_URI = "https://spreadsheets.google.com/feeds/worksheets/%s/private/full";
private static final int PROGRESS_GET_SPREADSHEET_ID = 0; private static final int PROGRESS_GET_SPREADSHEET_ID = 0;
@@ -115,7 +115,11 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
driveAccountName = account.name; driveAccountName = account.name;
} }
String folderId = SyncUtils.getMyTracksFolder(context, drive).getId(); File folder = SyncUtils.getMyTracksFolder(context, drive);
if (folder == null) {
return;
}
String folderId = folder.getId();
if (folderId == null) { if (folderId == null) {
return; return;
} }
@@ -24,17 +24,30 @@ import com.google.android.apps.mytracks.io.gdata.maps.MapsConstants;
import com.google.android.apps.mytracks.io.gdata.maps.MapsGDataConverter; import com.google.android.apps.mytracks.io.gdata.maps.MapsGDataConverter;
import com.google.android.apps.mytracks.io.gdata.maps.MapsMapMetadata; import com.google.android.apps.mytracks.io.gdata.maps.MapsMapMetadata;
import com.google.android.apps.mytracks.io.gdata.maps.XmlMapsGDataParserFactory; 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.ApiAdapterFactory; import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.SystemUtils; import com.google.android.apps.mytracks.util.SystemUtils;
import com.google.android.common.gdata.AndroidXmlParserFactory; import com.google.android.common.gdata.AndroidXmlParserFactory;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.GoogleHeaders; import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.MethodOverride; import com.google.api.client.googleapis.MethodOverride;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.http.GenericUrl; import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.InputStreamContent; import com.google.api.client.http.InputStreamContent;
import com.google.wireless.gdata.data.Entry; 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.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.data.spreadsheet.WorksheetFeed;
import com.google.wireless.gdata.parser.GDataParser; import com.google.wireless.gdata.parser.GDataParser;
import android.accounts.Account; import android.accounts.Account;
@@ -45,8 +58,11 @@ import android.util.Log;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
/** /**
* Provides utilities to access Google Maps, Google Documents, Google Fusion * Provides utilities to access Google Maps, Google Documents, Google Fusion
@@ -55,17 +71,24 @@ import java.util.List;
* @author Youtao Liu * @author Youtao Liu
*/ */
public class GoogleUtils { public class GoogleUtils {
public static final String DOCUMENT_NAME_PREFIX = "My Tracks"; 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_1 = "mytrackstest@gmail.com";
public static final String ACCOUNT_NAME_2 = "mytrackstest2@gmail.com"; public static final String ACCOUNT_NAME_2 = "mytrackstest2@gmail.com";
public static final String SPREADSHEET_NAME = DOCUMENT_NAME_PREFIX + "-" + EndToEndTestUtils.activityType; public static final String SPREADSHEET_NAME = DOCUMENT_NAME_PREFIX + "-"
+ EndToEndTestUtils.activityType;
private static final String APP_NAME_PREFIX = "Google-MyTracks-"; private static final String APP_NAME_PREFIX = "Google-MyTracks-";
private static final String CONTENT_TYPE = "application/x-www-form-urlencoded"; private static final String CONTENT_TYPE = "application/x-www-form-urlencoded";
private static final String FUSION_TABLES_BASE_URL = "https://www.google.com/fusiontables/api/query"; private static final String FUSION_TABLES_BASE_URL = "https://www.google.com/fusiontables/api/query";
private static final String GDATA_VERSION = "2"; private static final String GDATA_VERSION = "2";
private static final String WORK_SHEET_NAME = "Log";
private static final String TRANCK_NAME_COLUMN = "Name";
private static final String TEST_TRACKS_QUERY = "'root' in parents and title contains '"
+ EndToEndTestUtils.TRACK_NAME_PREFIX + "' and trashed = false";
/** /**
* Gets the account to access Google Services. * Gets the account to access Google Services.
* *
@@ -75,7 +98,7 @@ public class GoogleUtils {
private static Account getAccount(Context context) { private static Account getAccount(Context context) {
return AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE)[0]; return AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE)[0];
} }
/** /**
* Gets Google maps of a user. * Gets Google maps of a user.
* *
@@ -135,8 +158,8 @@ public class GoogleUtils {
&& oneData.getTitle().equals(title)) { && oneData.getTitle().equals(title)) {
if (isDelete) { if (isDelete) {
try { try {
mapsClient.deleteEntry(oneData.getGDataEditUri(), AccountManager.get(context).blockingGetAuthToken(getAccount(context), mapsClient.deleteEntry(oneData.getGDataEditUri(), AccountManager.get(context)
MapsConstants.SERVICE_NAME, false)); .blockingGetAuthToken(getAccount(context), MapsConstants.SERVICE_NAME, false));
return true; return true;
} catch (Exception e) { } catch (Exception e) {
Log.d(EndToEndTestUtils.LOG_TAG, "Unable to drop map", e); Log.d(EndToEndTestUtils.LOG_TAG, "Unable to drop map", e);
@@ -148,7 +171,7 @@ public class GoogleUtils {
} }
return false; return false;
} }
/** /**
* Searches a map in user's Google Maps. * Searches a map in user's Google Maps.
* *
@@ -159,7 +182,7 @@ public class GoogleUtils {
public static boolean searchMap(String title, Activity activity) { public static boolean searchMap(String title, Activity activity) {
return searchMapByTitle(title, activity, false); return searchMapByTitle(title, activity, false);
} }
/** /**
* Searches a map in user's Google Maps and then delete it. * Searches a map in user's Google Maps and then delete it.
* *
@@ -172,170 +195,147 @@ public class GoogleUtils {
} }
/** /**
* Searches a doc in user's Google Documents. * Removes old tracks created by MyTracks test.
* *
* @param title the title of doc * @param activity
* @param activity to get context * @param accountName
* @return the entry of the document, null means can not find the spreadsheet.
*/ */
public static Entry searchSpreadsheetByTitle(String title, Activity activity) { public static void deleteTestTracksOnGoogleDrive(Activity activity, String accountName) {
// TODO change to using OAuth2 and the new spreadsheet gdata library try {
GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential(
// Context context = activity.getApplicationContext(); activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE);
// DocumentsClient documentsClient = new DocumentsClient( if (driveCredential == null) {
// GDataClientFactory.getGDataClient(context), return;
// new XmlDocsGDataParserFactory(new AndroidXmlParserFactory())); }
//
// try { Drive drive = SyncUtils.getDriveService(driveCredential);
// String documentsAuthToken = AccountManager.get(context) com.google.api.services.drive.Drive.Files.List list = drive.files().list()
// .blockingGetAuthToken(getAccount(context), documentsClient.getServiceName(), false); .setQ(TEST_TRACKS_QUERY);
// String uri = String.format(Locale.US, SendDocsUtils.GET_SPREADSHEET_BY_TITLE_URI, List<File> files = list.execute().getItems();
// URLEncoder.encode(title, "utf-8")); for (Iterator<File> iterator = files.iterator(); iterator.hasNext();) {
// GDataParser gDataParser = documentsClient.getParserForFeed(Entry.class, uri, File file = (File) iterator.next();
// documentsAuthToken); drive.files().delete(file.getId()).execute();
// gDataParser.init(); }
// } catch (Exception e) {
// while (gDataParser.hasMoreData()) { Log.e(EndToEndTestUtils.LOG_TAG, "Delete test tracks failed.");
// Entry entry = gDataParser.readNextEntry(null); }
// String entryTitle = entry.getTitle();
// if (entryTitle.equals(title)) {
// return entry;
// }
// }
// } catch (Exception e) {
// Log.d(EndToEndTestUtils.LOG_TAG, "Unable to fetch spreadsheet.", e);
// }
return null;
} }
/** /**
* Delete spreadsheet which name is title. * Delete spreadsheet which name is title.
* *
* @param title the name of spreadsheet * @param title the name of spreadsheet
* @param activity to get context * @param activity to get context
* @param accountName the name of Google account
* @return true means delete successfully
*/ */
public static void deleteSpreadsheetByTitle(String title, Activity activity) { public static boolean deleteSpreadsheetByTitle(String title, Activity activity, String accountName) {
// TODO change to using OAuth2 and the new spreadsheet gdata library try {
// Context context = activity.getApplicationContext(); GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential(
// DocumentsClient documentsClient = new DocumentsClient( activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE);
// GDataClientFactory.getGDataClient(context), new XmlDocsGDataParserFactory( if (driveCredential == null) {
// new AndroidXmlParserFactory())); return false;
// }
// try {
// String documentsAuthToken = AccountManager.get(context).blockingGetAuthToken( Drive drive = SyncUtils.getDriveService(driveCredential);
// getAccount(context), documentsClient.getServiceName(), false); com.google.api.services.drive.Drive.Files.List list = drive.files().list()
// String uri = String.format(Locale.US, SendDocsUtils.GET_SPREADSHEET_BY_TITLE_URI, .setQ(String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, title));
// URLEncoder.encode(title, "utf-8")); List<File> files = list.execute().getItems();
// GDataParser gDataParser = documentsClient.getParserForFeed(Entry.class, uri, for (Iterator<File> iterator = files.iterator(); iterator.hasNext();) {
// documentsAuthToken); File file = (File) iterator.next();
// gDataParser.init(); drive.files().delete(file.getId()).execute();
// }
// while (gDataParser.hasMoreData()) { return true;
// Entry entry = gDataParser.readNextEntry(null); } catch (Exception e) {
// String entryTitle = entry.getTitle(); Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed.");
// if (entryTitle.equals(title)) { }
// documentsClient.deleteEntry(entry.getEditUri(), documentsAuthToken); return false;
// Log.d(EndToEndTestUtils.LOG_TAG, "Delete one spreadsheet.");
// }
// }
// } catch (Exception e) {
// Log.e(EndToEndTestUtils.LOG_TAG, "Unable to fetch spreadsheet.", e);
// }
} }
/** /**
* Searches docs in user's Google Documents. * Searches docs in user's Google Documents.
* *
* @param title the title of doc * @param title the title of doc
* @param activity to get context * @param activity to get context
* @return the entry of the document, null means can not find the spreadsheet. * @param accountName the name of Google account
* @return the file list of the document, null means can not find the
* spreadsheets
*/ */
public static List<Entry> searchAllSpreadsheetByTitle(String title, Activity activity) { public static List<File> searchAllSpreadsheetByTitle(String title, Activity activity,
List<Entry> docs = new ArrayList<Entry>(); String accountName) {
// Context context = activity.getApplicationContext(); try {
// DocumentsClient documentsClient = new DocumentsClient( GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential(
// GDataClientFactory.getGDataClient(context), activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE);
// new XmlDocsGDataParserFactory(new AndroidXmlParserFactory())); if (driveCredential == null) {
// return null;
// try { }
// String documentsAuthToken = AccountManager.get(context)
// .blockingGetAuthToken(getAccount(context), documentsClient.getServiceName(), false); Drive drive = SyncUtils.getDriveService(driveCredential);
// String uri = String.format(Locale.US, SendDocsUtils.GET_SPREADSHEET_BY_TITLE_URI, com.google.api.services.drive.Drive.Files.List list = drive.files().list()
// URLEncoder.encode(title, "utf-8")); .setQ(String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, title));
// GDataParser gDataParser = documentsClient.getParserForFeed(Entry.class, uri, FileList result = list.execute();
// documentsAuthToken); return result.getItems();
// gDataParser.init(); } catch (Exception e) {
// Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed.");
// while (gDataParser.hasMoreData()) { }
// Entry entry = gDataParser.readNextEntry(null); return null;
// String entryTitle = entry.getTitle();
// if (entryTitle.equals(title)) {
// docs.add(entry);
// }
// }
// } catch (Exception e) {
// Log.d(EndToEndTestUtils.LOG_TAG, "Unable to fetch spreadsheet.", e);
// }
return docs;
} }
/** /**
* Searches a track title in a spreadsheet. * Searches a track title in a spreadsheet.
* *
* @param title the track name to search * @param trackName the track name to search
* @param activity to get context * @param activity to get context
* @param spreadsheetTitle the title of spreadsheet * @param spreadsheetTitle the title of spreadsheet
* @param isDelete whether delete the information of this track in the document * @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 * @return true means find the track name in the spreadsheet
*/ */
private static boolean searchTrackTitleInSpreadsheet(String title, Activity activity, String spreadsheetTitle, boolean isDelete) { private static boolean searchTrackTitleInSpreadsheet(String trackName, Activity activity,
// String spreadsheetId = searchSpreadsheetByTitle(spreadsheetTitle, activity).getId().replace(SendDocsUtils.SPREADSHEET_ID_PREFIX, ""); String spreadsheetTitle, boolean isDelete, String accountName) {
// if(spreadsheetId == null) { try {
// Log.d(EndToEndTestUtils.LOG_TAG, "Unable to find the spreadsheet -- " + spreadsheetTitle); // Get spreadsheet Id.
// return false; String spreadsheetId = searchAllSpreadsheetByTitle(spreadsheetTitle, activity, accountName)
// } .get(0).getId();
// Context context = activity.getApplicationContext();
// try { // Get spreadsheet service.
// SpreadsheetsClient spreadsheetsClient = new SpreadsheetsClient( SpreadsheetService spreadsheetService = new SpreadsheetService(spreadsheetTitle);
// GDataClientFactory.getGDataClient(context), new XmlDocsGDataParserFactory(new AndroidXmlParserFactory())); Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
// String spreadsheetsAuthToken = AccountManager.get(activity.getApplicationContext()).blockingGetAuthToken( credential.setAccessToken(SendToGoogleUtils.getToken(activity.getApplicationContext(),
// getAccount(context), spreadsheetsClient.getServiceName(), false); accountName, SendToGoogleUtils.SPREADSHEET_SCOPE));
// spreadsheetService.setOAuth2Credentials(credential);
// String weekSheetId = SendDocsUtils.getWorksheetId(spreadsheetId, spreadsheetsClient, spreadsheetsAuthToken);
// String worksheetUri = String.format(Locale.US, SendDocsUtils.GET_WORKSHEET_URI, URLEncoder.encode(spreadsheetId, "utf-8"), // Get work sheet.
// weekSheetId); WorksheetFeed feed = spreadsheetService.getFeed(
// new URL(String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_WORKSHEETS_URI,
// GDataParser gDataParser = spreadsheetsClient.getParserForFeed(Feed.class, worksheetUri, spreadsheetId)), WorksheetFeed.class);
// spreadsheetsAuthToken); List<WorksheetEntry> data = feed.getEntries();
// gDataParser.init(); for (Iterator<WorksheetEntry> iterator = data.iterator(); iterator.hasNext();) {
// while (gDataParser.hasMoreData()) { WorksheetEntry worksheetEntry = (WorksheetEntry) iterator.next();
// Entry entry = gDataParser.readNextEntry(null); String title = worksheetEntry.getTitle().getPlainText();
// String entryTitle = entry.getTitle(); if (title.equals(WORK_SHEET_NAME)) {
// if (entryTitle.indexOf(title) > -1) { URL listFeedUrl = worksheetEntry.getListFeedUrl();
// if (isDelete) { List<ListEntry> listFeed = spreadsheetService.getFeed(listFeedUrl, ListFeed.class)
// spreadsheetsClient.deleteEntry(entry.getEditUri(), spreadsheetsAuthToken); .getEntries();
// } for (Iterator<ListEntry> iterator2 = listFeed.iterator(); iterator2.hasNext();) {
// return true; ListEntry listEntry = (ListEntry) iterator2.next();
// } String name = listEntry.getCustomElements().getValue(TRANCK_NAME_COLUMN);
// } if (name.equals(trackName)) {
// } catch (Exception e) { if (isDelete) {
// Log.d(EndToEndTestUtils.LOG_TAG, "Unable to fetch content of spreadsheet.", e); listEntry.delete();
// } }
return true;
}
}
}
}
} catch (Exception e) {
Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed.");
}
return false; return false;
} }
/**
* Searches a track in spreadsheet.
*
* @param title the track name to search
* @param activity to get context
* @return true means find the track name in the spreadsheet
*/
public static boolean searchTrackInSpreadSheet(String title, Activity activity) {
return searchTrackTitleInSpreadsheet(title, activity, GoogleUtils.SPREADSHEET_NAME, false);
}
/** /**
* Searches and deletes a track in spreadsheet. * Searches and deletes a track in spreadsheet.
* *
@@ -343,8 +343,9 @@ public class GoogleUtils {
* @param activity to get context * @param activity to get context
* @return true means find and delete successfully * @return true means find and delete successfully
*/ */
public static boolean deleteTrackInSpreadSheet(String title, Activity activity) { public static boolean deleteTrackInSpreadSheet(String title, Activity activity, String account) {
return searchTrackTitleInSpreadsheet(title, activity, GoogleUtils.SPREADSHEET_NAME, true); return searchTrackTitleInSpreadsheet(title, activity, GoogleUtils.SPREADSHEET_NAME, true,
account);
} }
/** /**
@@ -358,20 +359,21 @@ public class GoogleUtils {
Context context = activity.getApplicationContext(); Context context = activity.getApplicationContext();
try { try {
HttpResponse response = sendFusionTableQuery("SHOW TABLES", context); HttpResponse response = sendFusionTableQuery("SHOW TABLES", context);
// We can use index of method to check new table for every track name is unique. // We can use index of method to check new table for every track name is
if (response != null && response.parseAsString().indexOf(title) > 0) { // unique.
return true; if (response != null && response.parseAsString().indexOf(title) > 0) {
return true;
} }
} catch (Exception e) { } catch (Exception e) {
Log.d(EndToEndTestUtils.LOG_TAG, "Unable to query fusion table.", e); Log.d(EndToEndTestUtils.LOG_TAG, "Unable to query fusion table.", e);
} }
return false; return false;
} }
/** /**
* Drops one fusion table which contain the string in title of current user. * Drops one fusion table which contain the string in title of current user.
* *
* @param title the title of a track to drop * @param title the title of a track to drop
* @param activity to get context * @param activity to get context
* @return the result of drop * @return the result of drop
*/ */
@@ -388,7 +390,7 @@ public class GoogleUtils {
// The first column is the table id. // The first column is the table id.
if (secondColumn.equals(title)) { if (secondColumn.equals(title)) {
sendFusionTableQuery("DROP TABLE " + firstColumn, context); sendFusionTableQuery("DROP TABLE " + firstColumn, context);
return true; return true;
} }
} }
} catch (IOException e) { } catch (IOException e) {
@@ -396,7 +398,7 @@ public class GoogleUtils {
} }
return false; return false;
} }
/** /**
* Sends query to operate fusion tables. * Sends query to operate fusion tables.
* *
@@ -406,8 +408,8 @@ public class GoogleUtils {
*/ */
private static HttpResponse sendFusionTableQuery(String query, Context context) { private static HttpResponse sendFusionTableQuery(String query, Context context) {
try { try {
String fusionTableAuthToken = AccountManager.get(context).blockingGetAuthToken(getAccount(context), String fusionTableAuthToken = AccountManager.get(context).blockingGetAuthToken(
SendFusionTablesUtils.SERVICE, false); getAccount(context), SendFusionTablesUtils.SERVICE, false);
GenericUrl url = new GenericUrl(FUSION_TABLES_BASE_URL); GenericUrl url = new GenericUrl(FUSION_TABLES_BASE_URL);
String sql = "sql=" + query; String sql = "sql=" + query;
@@ -419,8 +421,7 @@ public class GoogleUtils {
.createRequestFactory(new MethodOverride())).buildPostRequest(url, inputStreamContent); .createRequestFactory(new MethodOverride())).buildPostRequest(url, inputStreamContent);
GoogleHeaders headers = new GoogleHeaders(); GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName(APP_NAME_PREFIX headers.setApplicationName(APP_NAME_PREFIX + SystemUtils.getMyTracksVersion(context));
+ SystemUtils.getMyTracksVersion(context));
headers.setGDataVersion(GDATA_VERSION); headers.setGDataVersion(GDATA_VERSION);
headers.setGoogleLogin(fusionTableAuthToken); headers.setGoogleLogin(fusionTableAuthToken);
headers.setContentType(CONTENT_TYPE); headers.setContentType(CONTENT_TYPE);
@@ -434,19 +435,19 @@ public class GoogleUtils {
return null; return null;
} }
} }
/** /**
* Checks whether the status of account is right to use. * Checks whether the status of account is right to use.
* *
* @return true means the status of account is good for sending * @return true means the status of account is good for sending
*/ */
public static boolean isAccountAvailable() { public static boolean isAccountAvailable() {
// Check whether no account is binded with this device. // Check whether no account is binded with this device.
if (EndToEndTestUtils.SOLO.waitForText( if (EndToEndTestUtils.SOLO.waitForText(
EndToEndTestUtils.activityMytracks.getString(R.string.send_google_no_account_title), 1, EndToEndTestUtils.activityMytracks.getString(R.string.send_google_no_account_title), 1,
EndToEndTestUtils.SHORT_WAIT_TIME)) { EndToEndTestUtils.SHORT_WAIT_TIME)) {
EndToEndTestUtils.getButtonOnScreen(EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, EndToEndTestUtils.getButtonOnScreen(
true); EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), true, true);
return false; return false;
} }
@@ -454,17 +455,18 @@ public class GoogleUtils {
if (EndToEndTestUtils.SOLO.waitForText( if (EndToEndTestUtils.SOLO.waitForText(
EndToEndTestUtils.activityMytracks.getString(R.string.send_google_choose_account_title), 1, EndToEndTestUtils.activityMytracks.getString(R.string.send_google_choose_account_title), 1,
EndToEndTestUtils.SHORT_WAIT_TIME)) { EndToEndTestUtils.SHORT_WAIT_TIME)) {
EndToEndTestUtils.getButtonOnScreen(EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), false, EndToEndTestUtils.SOLO.clickOnText(GoogleUtils.ACCOUNT_NAME_1);
true); EndToEndTestUtils.getButtonOnScreen(
EndToEndTestUtils.activityMytracks.getString(R.string.generic_ok), false, true);
} }
// Check whether no account permission. // Check whether no account permission.
if (EndToEndTestUtils.SOLO.waitForText( if (EndToEndTestUtils.SOLO.waitForText(
EndToEndTestUtils.activityMytracks.getString(R.string.send_google_no_account_permission), 1, EndToEndTestUtils.activityMytracks.getString(R.string.send_google_no_account_permission),
EndToEndTestUtils.SHORT_WAIT_TIME)) { 1, EndToEndTestUtils.SHORT_WAIT_TIME)) {
return false; return false;
} }
return true; return true;
} }
@@ -16,13 +16,16 @@
package com.google.android.apps.mytracks.endtoendtest; package com.google.android.apps.mytracks.endtoendtest;
import com.google.android.apps.mytracks.TrackListActivity; import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.sync.SyncTestUtils;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.google.wireless.gdata.data.Entry; import com.google.api.services.drive.model.File;
import android.app.Instrumentation; import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
import android.widget.CheckBox; import android.widget.CheckBox;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -45,22 +48,29 @@ public class SendToGoogleTest extends ActivityInstrumentationTestCase2<TrackList
super.setUp(); super.setUp();
instrumentation = getInstrumentation(); instrumentation = getInstrumentation();
activityMyTracks = getActivity(); activityMyTracks = getActivity();
GoogleUtils.deleteTestTracksOnGoogleDrive(activityMyTracks, GoogleUtils.ACCOUNT_NAME_1);
EndToEndTestUtils.setupForAllTest(instrumentation, activityMyTracks); EndToEndTestUtils.setupForAllTest(instrumentation, activityMyTracks);
} }
/** /**
* Check all services and send to google. * Checks all services and send to google.
*
* @throws GoogleAuthException
* @throws IOException
*/ */
public void testCreateAndSendTrack_send() { public void testCreateAndSendTrack_send() throws IOException, GoogleAuthException {
EndToEndTestUtils.createTrackIfEmpty(1, false); EndToEndTestUtils.createTrackIfEmpty(1, false);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
checkSendTrackToGoogle(); checkSendTrackToGoogle();
} }
/** /**
* Check all services and send to google. * Checks all services and send to google.
*
* @throws GoogleAuthException
* @throws IOException
*/ */
public void testCreateAndSendTrack_sendPausedTrack() { public void testCreateAndSendTrack_sendPausedTrack() throws IOException, GoogleAuthException {
EndToEndTestUtils.deleteAllTracks(); EndToEndTestUtils.deleteAllTracks();
EndToEndTestUtils.createTrackWithPause(3); EndToEndTestUtils.createTrackWithPause(3);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
@@ -75,7 +85,7 @@ public class SendToGoogleTest extends ActivityInstrumentationTestCase2<TrackList
String testActivity = "(TestActivity)"; String testActivity = "(TestActivity)";
EndToEndTestUtils.activityType = testActivity; EndToEndTestUtils.activityType = testActivity;
GoogleUtils.deleteSpreadsheetByTitle("My Tracks-" + EndToEndTestUtils.activityType, GoogleUtils.deleteSpreadsheetByTitle("My Tracks-" + EndToEndTestUtils.activityType,
EndToEndTestUtils.activityMytracks); EndToEndTestUtils.activityMytracks, GoogleUtils.ACCOUNT_NAME_1);
EndToEndTestUtils.createSimpleTrack(1, false); EndToEndTestUtils.createSimpleTrack(1, false);
boolean result = sendToGoogle(); boolean result = sendToGoogle();
result = result && sendToGoogle(); result = result && sendToGoogle();
@@ -83,26 +93,32 @@ public class SendToGoogleTest extends ActivityInstrumentationTestCase2<TrackList
// Result is true mean has account bound with this device and send // Result is true mean has account bound with this device and send
// successful. // successful.
if (result) { if (result) {
List<Entry> spreadsheetEntry = GoogleUtils List<File> fileList = GoogleUtils.searchAllSpreadsheetByTitle(
.searchAllSpreadsheetByTitle(GoogleUtils.DOCUMENT_NAME_PREFIX + "-" GoogleUtils.DOCUMENT_NAME_PREFIX + "-" + EndToEndTestUtils.activityType,
+ EndToEndTestUtils.activityType, activityMyTracks); activityMyTracks, GoogleUtils.ACCOUNT_NAME_1);
assertEquals(1, spreadsheetEntry.size()); assertEquals(1, fileList.size());
} }
} }
/** /**
* Checks the process of sending track to google. * Checks the process of sending track to google.
*
* @throws GoogleAuthException
* @throws IOException
*/ */
private void checkSendTrackToGoogle() { private void checkSendTrackToGoogle() throws IOException, GoogleAuthException {
if (!sendToGoogle()) { if (!sendToGoogle()) {
return; return;
} }
// Check whether all data is correct on Google Map, Documents, and // Check whether all data is correct on Google Drive, Maps, Documents, and
// Spreadsheet. // Spreadsheet.
assertTrue(SyncTestUtils.checkFile(EndToEndTestUtils.trackName, true,
SyncTestUtils.getGoogleDrive(activityMyTracks.getApplicationContext())));
assertTrue(GoogleUtils.deleteMap(EndToEndTestUtils.trackName, activityMyTracks)); assertTrue(GoogleUtils.deleteMap(EndToEndTestUtils.trackName, activityMyTracks));
assertTrue(GoogleUtils.searchFusionTableByTitle(EndToEndTestUtils.TRACK_NAME_PREFIX, assertTrue(GoogleUtils.searchFusionTableByTitle(EndToEndTestUtils.TRACK_NAME_PREFIX,
activityMyTracks)); activityMyTracks));
assertTrue(GoogleUtils.deleteTrackInSpreadSheet(EndToEndTestUtils.trackName, activityMyTracks)); assertTrue(GoogleUtils.deleteTrackInSpreadSheet(EndToEndTestUtils.trackName, activityMyTracks,
GoogleUtils.ACCOUNT_NAME_1));
assertTrue(GoogleUtils.dropFusionTables(EndToEndTestUtils.trackName, activityMyTracks)); assertTrue(GoogleUtils.dropFusionTables(EndToEndTestUtils.trackName, activityMyTracks));
} }
@@ -114,14 +130,16 @@ public class SendToGoogleTest extends ActivityInstrumentationTestCase2<TrackList
private boolean sendToGoogle() { private boolean sendToGoogle() {
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_send_google), true); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_send_google), true);
EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.send_google_title)); EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.send_google_title));
instrumentation.waitForIdleSync();
ArrayList<CheckBox> checkBoxs = EndToEndTestUtils.SOLO.getCurrentCheckBoxes(); ArrayList<CheckBox> checkBoxs = EndToEndTestUtils.SOLO.getCurrentCheckBoxes();
for (int i = 0; i < checkBoxs.size(); i++) { for (int i = 0; i < checkBoxs.size(); i++) {
if (!checkBoxs.get(i).isChecked()) { if (!checkBoxs.get(i).isChecked()) {
EndToEndTestUtils.SOLO.clickOnCheckBox(i); EndToEndTestUtils.SOLO.clickOnView(checkBoxs.get(i));
instrumentation.waitForIdleSync();
} }
} }
if (checkBoxs.size() < 3) { if (checkBoxs.size() < 4) {
EndToEndTestUtils.SOLO.scrollDown(); EndToEndTestUtils.SOLO.scrollDown();
checkBoxs = EndToEndTestUtils.SOLO.getCurrentCheckBoxes(); checkBoxs = EndToEndTestUtils.SOLO.getCurrentCheckBoxes();
@@ -136,6 +154,7 @@ public class SendToGoogleTest extends ActivityInstrumentationTestCase2<TrackList
EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.send_google_send_now), EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.send_google_send_now),
true, true); true, true);
instrumentation.waitForIdleSync();
if (!GoogleUtils.isAccountAvailable()) { if (!GoogleUtils.isAccountAvailable()) {
return false; return false;
} }
@@ -143,10 +162,17 @@ public class SendToGoogleTest extends ActivityInstrumentationTestCase2<TrackList
// Following check the process of "Send to Google". // Following check the process of "Send to Google".
assertTrue(EndToEndTestUtils.SOLO.waitForText(activityMyTracks assertTrue(EndToEndTestUtils.SOLO.waitForText(activityMyTracks
.getString(R.string.generic_progress_title))); .getString(R.string.generic_progress_title)));
// Waiting the send is finish.
// Waiting the send is finish. Should double check it for the progress
// dialog may disappear and display again while switch to next send(There
// are four sends currently, there are send to drive, maps, fusion table and
// spreadsheet).
while (EndToEndTestUtils.SOLO.waitForText( while (EndToEndTestUtils.SOLO.waitForText(
activityMyTracks.getString(R.string.generic_progress_title), 1, activityMyTracks.getString(R.string.generic_progress_title), 1,
EndToEndTestUtils.SHORT_WAIT_TIME)) {} EndToEndTestUtils.SHORT_WAIT_TIME)
|| EndToEndTestUtils.SOLO.waitForText(
activityMyTracks.getString(R.string.generic_progress_title), 1,
EndToEndTestUtils.SHORT_WAIT_TIME)) {}
// Check whether the result dialog is display. // Check whether the result dialog is display.
assertTrue(EndToEndTestUtils.SOLO.waitForText(activityMyTracks assertTrue(EndToEndTestUtils.SOLO.waitForText(activityMyTracks
@@ -155,7 +181,7 @@ public class SendToGoogleTest extends ActivityInstrumentationTestCase2<TrackList
.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true); .getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);
return true; return true;
} }
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
EndToEndTestUtils.activityType = EndToEndTestUtils.DEFAULTACTIVITYTYPE; EndToEndTestUtils.activityType = EndToEndTestUtils.DEFAULTACTIVITYTYPE;