Change code style of test GoogleUtils and related code.

This commit is contained in:
youtaol
2012-07-10 17:16:07 +08:00
parent 4cf7c4b32c
commit 9e5bd5e379
6 changed files with 100 additions and 66 deletions
@@ -396,8 +396,7 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask {
* @param setTableId true to set the table id * @param setTableId true to set the table id
* @return true if success. * @return true if success.
*/ */
@VisibleForTesting private boolean sendQuery(String query, boolean setTableId) {
public boolean sendQuery(String query, boolean setTableId) {
Log.d(TAG, "SendQuery: " + query); Log.d(TAG, "SendQuery: " + query);
if (isCancelled()) { if (isCancelled()) {
@@ -113,7 +113,7 @@ public class CreateAndSendTrackTest extends ActivityInstrumentationTestCase2<Tra
} }
} }
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.send_google_send_now)); EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.send_google_send_now), true, true);
// If no account is binded with this device. // If no account is binded with this device.
if (EndToEndTestUtils.SOLO.waitForText(activityMyTracks if (EndToEndTestUtils.SOLO.waitForText(activityMyTracks
@@ -131,14 +131,16 @@ public class CreateAndSendTrackTest extends ActivityInstrumentationTestCase2<Tra
assertTrue(EndToEndTestUtils.SOLO.waitForText(activityMyTracks assertTrue(EndToEndTestUtils.SOLO.waitForText(activityMyTracks
.getString(R.string.share_track_share_url))); .getString(R.string.share_track_share_url)));
EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true); EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);
// Check whether all data is correct on Google Map, Documents, and Spreadsheet. // Check whether all data is correct on Google Map, Documents, and Spreadsheet.
assertTrue(GoogleUtils.searchMapByTitle(EndToEndTestUtils.trackName, activityMyTracks, true)); assertTrue(GoogleUtils.deleteMap(EndToEndTestUtils.trackName, activityMyTracks));
assertTrue(GoogleUtils.searchFusionTableByTitle(EndToEndTestUtils.TRACK_NAME_PREFIX, activityMyTracks)); assertTrue(GoogleUtils.searchFusionTableByTitle(EndToEndTestUtils.TRACK_NAME_PREFIX,
assertTrue(GoogleUtils.searchTrackTitleInSpreadsheet(EndToEndTestUtils.trackName, activityMyTracks, "My Tracks-" + EndToEndTestUtils.DEFAULTACTIVITY, true)); activityMyTracks));
GoogleUtils.dropFusionTables(EndToEndTestUtils.trackName, activityMyTracks); assertTrue(GoogleUtils.searchTrackTitleInSpreadsheet(EndToEndTestUtils.trackName,
activityMyTracks, GoogleUtils.SPREADSHEET_NAME, true));
assertTrue(GoogleUtils.dropFusionTables(EndToEndTestUtils.trackName, activityMyTracks));
} }
} }
/** /**
@@ -64,6 +64,7 @@ public class DeleteTest extends ActivityInstrumentationTestCase2<TrackListActivi
EndToEndTestUtils.rotateAllActivities(); EndToEndTestUtils.rotateAllActivities();
EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true); EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
assertTrue(EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.track_list_empty_message)));
// There is no track now. // There is no track now.
trackListView = EndToEndTestUtils.SOLO.getCurrentListViews(); trackListView = EndToEndTestUtils.SOLO.getCurrentListViews();
assertEquals(0, trackListView.get(0).getCount()); assertEquals(0, trackListView.get(0).getCount());
@@ -188,6 +188,7 @@ public class EndToEndTestUtils {
checkLanguage(); checkLanguage();
EndToEndTestUtils.isCheckedFirstLaunch = true; EndToEndTestUtils.isCheckedFirstLaunch = true;
EndToEndTestUtils.setHasActionBar(); EndToEndTestUtils.setHasActionBar();
EndToEndTestUtils.resetAllSettings(activityMyTracks, false);
} else if (EndToEndTestUtils.SOLO.waitForText( } else if (EndToEndTestUtils.SOLO.waitForText(
// After reset setting, welcome page will show again. // After reset setting, welcome page will show again.
activityMytracks.getString(R.string.welcome_title), 0, 500)) { activityMytracks.getString(R.string.welcome_title), 0, 500)) {
@@ -359,8 +360,6 @@ public class EndToEndTestUtils {
+ System.currentTimeMillis(); + System.currentTimeMillis();
SOLO.sendKey(KeyEvent.KEYCODE_DEL); SOLO.sendKey(KeyEvent.KEYCODE_DEL);
SOLO.enterText(0, trackName); SOLO.enterText(0, trackName);
SOLO.clickOnEditText(1);
SOLO.sendKey(KeyEvent.KEYCODE_DEL);
SOLO.enterText(1, DEFAULTACTIVITY); SOLO.enterText(1, DEFAULTACTIVITY);
if(!EndToEndTestUtils.isEmulator) { if(!EndToEndTestUtils.isEmulator) {
// Close soft keyboard. // Close soft keyboard.
@@ -529,7 +528,7 @@ public class EndToEndTestUtils {
} }
/** /**
* Get more menu items operation is different for different Android OS. * Gets more menu items operation is different for different Android OS.
*/ */
public static void showMoreMenuItem() { public static void showMoreMenuItem() {
if (hasActionBar) { if (hasActionBar) {
@@ -62,38 +62,38 @@ import java.util.Locale;
* @author Youtao Liu * @author Youtao Liu
*/ */
public class GoogleUtils { public class GoogleUtils {
private static String authToken;
private static ArrayList<String> mapIds = new ArrayList<String>();
private static ArrayList<MapsMapMetadata> mapData;
private static MapsClient mapsClient;
private static Account account; private static Account account;
public static final String LOG_TAG = "MyTracksTest"; public static final String LOG_TAG = "MyTracksTest";
public static final String DOCUMENT_NAME_PREFIX = "My Tracks";
public static final String SPREADSHEET_NAME = DOCUMENT_NAME_PREFIX + "-" + EndToEndTestUtils.DEFAULTACTIVITY;
private static Account getAccount(Context context) {
return account == null ? account = AccountManager.get(context).getAccountsByType(
Constants.ACCOUNT_TYPE)[0] : account;
}
/** /**
* Get Google maps of a user. * Gets Google maps of a user.
* *
* @param context used to get maps * @param context used to get maps
* @param mapsClient the client to access Google Maps
* @return true means set successfully * @return true means set successfully
*/ */
private static boolean getMaps(Context context) { private static ArrayList<MapsMapMetadata> getMaps(Context context, MapsClient mapsClient) {
account = AccountManager.get(context).getAccountsByType(
Constants.ACCOUNT_TYPE)[0];
// Reset the per request states // Reset the per request states
authToken = null; String authToken = null;
mapIds = new ArrayList<String>(); ArrayList<String> mapIds = new ArrayList<String>();
mapData = new ArrayList<MapsMapMetadata>(); ArrayList<MapsMapMetadata> mapData = new ArrayList<MapsMapMetadata>();
try { try {
authToken = AccountManager.get(context).blockingGetAuthToken(account, authToken = AccountManager.get(context).blockingGetAuthToken(getAccount(context),
MapsConstants.SERVICE_NAME, false); MapsConstants.SERVICE_NAME, false);
} catch (Exception e) { } catch (Exception e) {
Log.d(LOG_TAG, "Unable to get auth token", e); Log.d(LOG_TAG, "Unable to get auth token", e);
return false; return mapData;
} }
mapsClient = new MapsClient(GDataClientFactory.getGDataClient(context),
new XmlMapsGDataParserFactory(new AndroidXmlParserFactory()));
GDataParser gDataParser = null; GDataParser gDataParser = null;
try { try {
gDataParser = mapsClient.getParserForFeed(MapFeatureEntry.class, MapsClient.getMapsFeed(), gDataParser = mapsClient.getParserForFeed(MapFeatureEntry.class, MapsClient.getMapsFeed(),
@@ -112,7 +112,7 @@ public class GoogleUtils {
} }
} }
return true; return mapData;
} }
/** /**
@@ -123,25 +123,51 @@ public class GoogleUtils {
* @param isDelete whether delete the map of this track in the Google Maps * @param isDelete whether delete the map of this track in the Google Maps
* @return true means find the map * @return true means find the map
*/ */
static boolean searchMapByTitle(String title, Activity activity, boolean isDelete) { private static boolean searchMapByTitle(String title, Activity activity, boolean isDelete) {
if (getMaps(activity.getApplicationContext())) { Context context = activity.getApplicationContext();
for (MapsMapMetadata oneData : mapData) { MapsClient mapsClient = new MapsClient(GDataClientFactory.getGDataClient(context),
if (oneData.getDescription().indexOf("My Tracks") > -1 new XmlMapsGDataParserFactory(new AndroidXmlParserFactory()));
&& oneData.getTitle().indexOf(title) > -1) { ArrayList<MapsMapMetadata> mapData = getMaps(context, mapsClient);
if (isDelete) { for (MapsMapMetadata oneData : mapData) {
try { if (oneData.getDescription().indexOf(DOCUMENT_NAME_PREFIX) > -1
mapsClient.deleteEntry(oneData.getGDataEditUri(), authToken); && oneData.getTitle().equals(title)) {
} catch (Exception e) { if (isDelete) {
Log.d(LOG_TAG, "Unable to drop map", e); try {
return false; mapsClient.deleteEntry(oneData.getGDataEditUri(), AccountManager.get(context).blockingGetAuthToken(getAccount(context),
} MapsConstants.SERVICE_NAME, false));
return true;
} catch (Exception e) {
Log.d(LOG_TAG, "Unable to drop map", e);
return false;
} }
return true;
} }
return true;
} }
} }
return false; 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);
}
/** /**
* Searches a doc in user's Google Documents. * Searches a doc in user's Google Documents.
@@ -150,17 +176,15 @@ public class GoogleUtils {
* @param activity to get context * @param activity to get context
* @return the entry of the document, null means can not find the spreadsheet. * @return the entry of the document, null means can not find the spreadsheet.
*/ */
private static Entry searchSpeadsheetByTitle(String title, Activity activity) { private static Entry searchSepeadsheetByTitle(String title, Activity activity) {
account = AccountManager.get(activity.getApplicationContext()).getAccountsByType( Context context = activity.getApplicationContext();
Constants.ACCOUNT_TYPE)[0];
DocumentsClient documentsClient = new DocumentsClient( DocumentsClient documentsClient = new DocumentsClient(
GDataClientFactory.getGDataClient(activity.getApplicationContext()), GDataClientFactory.getGDataClient(context),
new XmlDocsGDataParserFactory(new AndroidXmlParserFactory())); new XmlDocsGDataParserFactory(new AndroidXmlParserFactory()));
try { try {
String documentsAuthToken = AccountManager.get(activity.getApplicationContext()) String documentsAuthToken = AccountManager.get(context)
.blockingGetAuthToken(account, documentsClient.getServiceName(), false); .blockingGetAuthToken(getAccount(context), documentsClient.getServiceName(), false);
String uri = String.format(Locale.US, SendDocsUtils.GET_SPREADSHEET_BY_TITLE_URI, String uri = String.format(Locale.US, SendDocsUtils.GET_SPREADSHEET_BY_TITLE_URI,
URLEncoder.encode(title, "utf-8")); URLEncoder.encode(title, "utf-8"));
GDataParser gDataParser = documentsClient.getParserForFeed(Entry.class, uri, GDataParser gDataParser = documentsClient.getParserForFeed(Entry.class, uri,
@@ -182,7 +206,7 @@ public class GoogleUtils {
/** /**
* Search a track title in a spreadsheet. * Searches a track title in a spreadsheet.
* *
* @param title the track name to search * @param title the track name to search
* @param activity to get context * @param activity to get context
@@ -190,14 +214,18 @@ public class GoogleUtils {
* @param isDelete whether delete the information of this track in the document * @param isDelete whether delete the information of this track in the document
* @return true means find the track name in the spreadsheet * @return true means find the track name in the spreadsheet
*/ */
static boolean searchTrackTitleInSpreadsheet(String title, Activity activity, String spreadsheetTitle, boolean isDelete) { public static boolean searchTrackTitleInSpreadsheet(String title, Activity activity, String spreadsheetTitle, boolean isDelete) {
String spreadsheetId = searchSpeadsheetByTitle(spreadsheetTitle, activity).getId().replace(SendDocsUtils.SPREADSHEET_ID_PREFIX, ""); String spreadsheetId = searchSepeadsheetByTitle(spreadsheetTitle, activity).getId().replace(SendDocsUtils.SPREADSHEET_ID_PREFIX, "");
if(spreadsheetId == null) {
Log.d(LOG_TAG, "Unable to find the spreadsheet -- " + spreadsheetTitle);
return false;
}
Context context = activity.getApplicationContext(); Context context = activity.getApplicationContext();
try { try {
SpreadsheetsClient spreadsheetsClient = new SpreadsheetsClient( SpreadsheetsClient spreadsheetsClient = new SpreadsheetsClient(
GDataClientFactory.getGDataClient(context), new XmlDocsGDataParserFactory(new AndroidXmlParserFactory())); GDataClientFactory.getGDataClient(context), new XmlDocsGDataParserFactory(new AndroidXmlParserFactory()));
String spreadsheetsAuthToken = AccountManager.get(activity.getApplicationContext()).blockingGetAuthToken( String spreadsheetsAuthToken = AccountManager.get(activity.getApplicationContext()).blockingGetAuthToken(
account, spreadsheetsClient.getServiceName(), false); getAccount(context), spreadsheetsClient.getServiceName(), false);
String weekSheetId = SendDocsUtils.getWorksheetId(spreadsheetId, spreadsheetsClient, spreadsheetsAuthToken); String weekSheetId = SendDocsUtils.getWorksheetId(spreadsheetId, spreadsheetsClient, spreadsheetsAuthToken);
String worksheetUri = String.format(Locale.US, SendDocsUtils.GET_WORKSHEET_URI, URLEncoder.encode(spreadsheetId, "utf-8"), String worksheetUri = String.format(Locale.US, SendDocsUtils.GET_WORKSHEET_URI, URLEncoder.encode(spreadsheetId, "utf-8"),
@@ -229,12 +257,12 @@ public class GoogleUtils {
* @param activity to get context * @param activity to get context
* @return true means find the fusion table * @return true means find the fusion table
*/ */
static boolean searchFusionTableByTitle(String title, Activity activity) { public static boolean searchFusionTableByTitle(String title, Activity activity) {
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 unique.
if ((response != null) && response.parseAsString().indexOf(title) > 0) { if (response != null && response.parseAsString().indexOf(title) > 0) {
return true; return true;
} }
} catch (Exception e) { } catch (Exception e) {
@@ -244,28 +272,32 @@ public class GoogleUtils {
} }
/** /**
* Remove 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
*/ */
static void dropFusionTables(String title, Activity activity) { public static boolean dropFusionTables(String title, Activity activity) {
Context context = activity.getApplicationContext(); Context context = activity.getApplicationContext();
HttpResponse response = sendFusionTableQuery("SHOW TABLES", context); HttpResponse response = sendFusionTableQuery("SHOW TABLES", context);
String[] rowsTable; String[] rowsTable;
try { try {
rowsTable = response.parseAsString().split("\n"); rowsTable = response.parseAsString().split("\n");
for (String string : rowsTable) { for (String row : rowsTable) {
String firstColumn = row.split(",")[0];
String secondColumn = row.split(",")[1];
// If the first column is all figure, it is the table id. // If the first column is all figure, it is the table id.
String regularExpression = "^[0-9]*$"; String regularExpression = "^[0-9]*$";
if (string.split(",")[0].matches(regularExpression) && string.indexOf(title) > -1) { if (firstColumn.matches(regularExpression) && secondColumn.equals(title)) {
sendFusionTableQuery("DROP TABLE " + string.split(",")[0], context); sendFusionTableQuery("DROP TABLE " + row.split(",")[0], context);
return true;
} }
} }
} catch (IOException e) { } catch (IOException e) {
Log.d(LOG_TAG, "Failed when delete all fusion tables.", e); Log.d(LOG_TAG, "Failed when delete all fusion tables.", e);
} }
return false;
} }
/** /**
@@ -277,9 +309,7 @@ public class GoogleUtils {
*/ */
private static HttpResponse sendFusionTableQuery(String query, Context context) { private static HttpResponse sendFusionTableQuery(String query, Context context) {
try { try {
account = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE)[0]; String fusionTableAuthToken = AccountManager.get(context).blockingGetAuthToken(getAccount(context),
String fusionTableAuthToken = AccountManager.get(context).blockingGetAuthToken(account,
SendFusionTablesUtils.SERVICE, false); SendFusionTablesUtils.SERVICE, false);
GenericUrl url = new GenericUrl(SendFusionTablesAsyncTask.FUSION_TABLES_BASE_URL); GenericUrl url = new GenericUrl(SendFusionTablesAsyncTask.FUSION_TABLES_BASE_URL);
@@ -67,9 +67,12 @@ public class MenuItemsTest extends ActivityInstrumentationTestCase2<TrackListAct
EndToEndTestUtils.createTrackIfEmpty(1, false); EndToEndTestUtils.createTrackIfEmpty(1, false);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
// Menu in TrackDetailActivity. // Menu in TrackDetailActivity.
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_sensor_state), true); // When there is no sensor connected this menu will be hidden.
EndToEndTestUtils.SOLO.waitForText(activityMyTracks if (EndToEndTestUtils
.getString(R.string.sensor_state_last_sensor_time)); .findMenuItem(activityMyTracks.getString(R.string.menu_sensor_state), true)) {
EndToEndTestUtils.SOLO.waitForText(activityMyTracks
.getString(R.string.sensor_state_last_sensor_time));
}
EndToEndTestUtils.SOLO.goBack(); EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_help), true); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_help), true);