Change code style of sync test cases.

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