Add some test cases about sync feature.

This commit is contained in:
youtaol
2013-01-30 21:39:15 +08:00
parent bfe2e21db4
commit 880f153ed2
8 changed files with 267 additions and 32 deletions
@@ -38,6 +38,7 @@ import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import com.google.api.services.drive.model.ParentReference;
import com.google.common.annotations.VisibleForTesting;
import android.accounts.Account;
import android.accounts.AccountManager;
@@ -74,9 +75,13 @@ public class SyncUtils {
public static final String KML_MIME_TYPE = "application/vnd.google-earth.kml+xml";
public static final String GET_KML_FILES_QUERY = "'%s' in parents and mimeType = '"
+ KML_MIME_TYPE + "' and trashed = false";
public static final String GET_MYTRACKS_FOLDER_QUERY = "'%s' in parents and mimeType = '"
+ KML_MIME_TYPE + "' and trashed = false";
private static final String FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
private static final String GET_MY_TRACKS_FOLDER_QUERY =
@VisibleForTesting
public static final String GET_MY_TRACKS_FOLDER_QUERY =
"'root' in parents and title = '%s' and mimeType = '" + FOLDER_MIME_TYPE
+ "' and trashed = false";
@@ -136,8 +136,6 @@ public class EndToEndTestUtils {
public static boolean isCheckedFirstLaunch = false;
public static boolean isGooglePlayServicesLatest = true;
private EndToEndTestUtils() {}
/**
* Checks the language, then sets the fields with right string.
*/
@@ -64,7 +64,8 @@ import java.util.Locale;
*/
public class GoogleUtils {
public static final String DOCUMENT_NAME_PREFIX = "My Tracks";
public static final String ACCOUNT_NAME = "mytrackstest@gmail.com";
public static final String ACCOUNT_NAME_1 = "mytrackstest@gmail.com";
public static final String ACCOUNT_NAME_2 = "mytrackstest2@gmail.com";
public static final String SPREADSHEET_NAME = DOCUMENT_NAME_PREFIX + "-" + EndToEndTestUtils.activityType;
/**
@@ -412,9 +412,9 @@ public class SettingTest extends ActivityInstrumentationTestCase2<TrackListActiv
.getString(R.string.settings_google_account_title));
// Whether test account is bound.
if (EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME, 1,
if (EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_1, 1,
EndToEndTestUtils.TINY_WAIT_TIME)) {
EndToEndTestUtils.SOLO.clickOnText(GoogleUtils.ACCOUNT_NAME);
EndToEndTestUtils.SOLO.clickOnText(GoogleUtils.ACCOUNT_NAME_1);
instrumentation.waitForIdleSync();
assertTrue(EndToEndTestUtils.findTextView(
activityMyTracks.getString(R.string.settings_google_drive_sync_title)).isEnabled());
@@ -0,0 +1,122 @@
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.endtoendtest.sync;
import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.apps.mytracks.endtoendtest.GoogleUtils;
import com.google.android.maps.mytracks.R;
import com.google.api.services.drive.Drive;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import java.io.IOException;
/**
* Tests the situation when user use multiple account in MyTracks.
*
* @author Youtao Liu
*/
public class MultiAccountsSyncTest extends ActivityInstrumentationTestCase2<TrackListActivity> {
public static Drive drive;
private Instrumentation instrumentation;
private TrackListActivity trackListActivity;
public MultiAccountsSyncTest() {
super(TrackListActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
instrumentation = getInstrumentation();
trackListActivity = getActivity();
EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity);
drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1);
EndToEndTestUtils.deleteAllTracks();
}
/**
* Tests sync tracks with Google Drive of two accounts.
*
* @throws IOException
*/
public void testSyncTracksWithMultiAccounts() throws IOException {
// Create tracks with first track.
EndToEndTestUtils.createSimpleTrack(0, true);
// Create tracks with second track.
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_2);
EndToEndTestUtils.createSimpleTrack(0, true);
// Sync with Google Drive and then check it of the second account.
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_2);
SyncTestUtils.checkFilesNumber(drive);
// Check Google Drive of the first account.
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1);
SyncTestUtils.checkFilesNumber(drive);
}
/**
* Creates three tracks and the deletes one in one account, and then deletes
* another one in another account. Keeps one tracks, then sync with two
* accounts.
*
* @throws IOException
*/
public void testDeleteTracksWithMultiAccounts() throws IOException {
EndToEndTestUtils.createSimpleTrack(0, true);
EndToEndTestUtils.createSimpleTrack(0, true);
EndToEndTestUtils.createSimpleTrack(0, true);
// Delete one track.
EndToEndTestUtils.SOLO.clickOnView(EndToEndTestUtils.SOLO.getCurrentListViews().get(0)
.getChildAt(0));
EndToEndTestUtils.SOLO.clickOnMenuItem(EndToEndTestUtils.activityMytracks
.getString(R.string.menu_delete));
// Switch account and delete another track.
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_2);
EndToEndTestUtils.SOLO.clickOnView(EndToEndTestUtils.SOLO.getCurrentListViews().get(0)
.getChildAt(0));
EndToEndTestUtils.SOLO.clickOnMenuItem(EndToEndTestUtils.activityMytracks
.getString(R.string.menu_delete));
// Check Google Drive of the first account.
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_1);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1);
SyncTestUtils.checkFilesNumber(drive);
// Check Google Drive of the second account.
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME_2);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_2);
SyncTestUtils.checkFilesNumber(drive);
}
}
@@ -48,8 +48,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
instrumentation = getInstrumentation();
trackListActivity = getActivity();
EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity);
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME);
drive = SyncTestUtils.getGoogleDrive(trackListActivity.getApplicationContext());
drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1);
}
/**
@@ -61,7 +60,23 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
EndToEndTestUtils.deleteAllTracks();
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkFilesNumber(0, drive);
SyncTestUtils.checkFilesNumber(drive);
}
/**
* Deletes one track in MyTracks and checks it in Google Drive.
*
* @throws IOException
*/
public void testDeleteOneTracksInMyTracks() throws IOException {
EndToEndTestUtils.createTrackIfEmpty(2, false);
EndToEndTestUtils.SOLO.clickOnMenuItem(EndToEndTestUtils.activityMytracks
.getString(R.string.menu_delete));
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
.getString(R.string.generic_ok));
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkFile(EndToEndTestUtils.trackName, false, drive);
}
/**
@@ -76,7 +91,7 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
EndToEndTestUtils.createSimpleTrack(3, true);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkFilesNumber(2, drive);
SyncTestUtils.checkFilesNumber(drive);
}
/**
@@ -90,8 +105,9 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
// Sync this track.
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
assertTrue(SyncTestUtils.checkFile(EndToEndTestUtils.trackName, true, drive));
String oldTrack = SyncTestUtils.getContentOfFile(SyncTestUtils.getFile(EndToEndTestUtils.trackName, drive), drive);
SyncTestUtils.checkFile(EndToEndTestUtils.trackName, true, drive);
String oldTrack = SyncTestUtils.getContentOfFile(
SyncTestUtils.getFile(EndToEndTestUtils.trackName, drive), drive);
String oldTrackName = EndToEndTestUtils.trackName;
@@ -117,9 +133,10 @@ public class SyncDriveWithMyTracksTest extends ActivityInstrumentationTestCase2<
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
// Check.
assertTrue(SyncTestUtils.checkFile(oldTrackName, false, drive));
assertTrue(SyncTestUtils.checkFile(newTrackName, true, drive));
String newTrack = SyncTestUtils.getContentOfFile(SyncTestUtils.getFile(newTrackName,drive), drive);
SyncTestUtils.checkFile(oldTrackName, false, drive);
SyncTestUtils.checkFile(newTrackName, true, drive);
String newTrack = SyncTestUtils.getContentOfFile(SyncTestUtils.getFile(newTrackName, drive),
drive);
assertNotSame(oldTrack, newTrack);
assertTrue(newTrack.indexOf(newTrackName) > 0);
assertTrue(newTrack.indexOf(newType) > 0);
@@ -20,6 +20,7 @@ import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.apps.mytracks.endtoendtest.GoogleUtils;
import com.google.android.maps.mytracks.R;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
@@ -47,25 +48,70 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2<
instrumentation = getInstrumentation();
trackListActivity = getActivity();
EndToEndTestUtils.setupForAllTest(instrumentation, trackListActivity);
SyncTestUtils.enableSync(GoogleUtils.ACCOUNT_NAME);
drive = SyncTestUtils.getGoogleDrive(trackListActivity.getApplicationContext());
drive = SyncTestUtils.setUpForSyncTest(GoogleUtils.ACCOUNT_NAME_1);
}
/**
* Deletes all tracks in Google Drive and checks in MyTracks.
* Deletes all tracks on Google Drive and checks in MyTracks.
*
* @throws IOException
*/
public void testDeleteAllTracksInDrive() throws IOException {
public void testDeleteAllTracksOnDrive() throws IOException {
EndToEndTestUtils.deleteAllTracks();
EndToEndTestUtils.createTrackIfEmpty(0, true);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkFilesNumber(1, drive);
SyncTestUtils.checkFilesNumber(drive);
SyncTestUtils.removeKMLFiles(drive);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkTracksNumber(0);
}
/**
* Deletes one file on Google Drive and checks it in MyTracks.
*
* @throws IOException
*/
public void testDeleteOneFileOnDrive() throws IOException {
EndToEndTestUtils.deleteAllTracks();
instrumentation.waitForIdleSync();
EndToEndTestUtils.createSimpleTrack(0, true);
EndToEndTestUtils.createSimpleTrack(0, true);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkFilesNumber(drive);
// Remove one track from Google Drive
File file = SyncTestUtils.getFile(EndToEndTestUtils.trackName, drive);
SyncTestUtils.removeFile(file, drive);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkFilesNumber(drive);
SyncTestUtils.checkTracksNumber(1);
}
/**
* Tests deleting and creating MyTracks folder on Google Dive by MyTracks.
*
* @throws IOException
*/
public void testCreateMyTracksOnDrive() throws IOException {
EndToEndTestUtils.deleteAllTracks();
instrumentation.waitForIdleSync();
EndToEndTestUtils.createSimpleTrack(0, true);
EndToEndTestUtils.createSimpleTrack(0, true);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
instrumentation.waitForIdleSync();
SyncTestUtils.checkFilesNumber(drive);
File folder = SyncTestUtils.getMyTracksFolder(trackListActivity.getApplicationContext(), drive);
assertNotNull(folder);
SyncTestUtils.removeFile(folder, drive);
EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
SyncTestUtils.checkFilesNumber(drive);
}
}
@@ -20,18 +20,22 @@ import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import android.content.Context;
import android.util.Log;
import android.widget.CheckBox;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -47,12 +51,22 @@ public class SyncTestUtils {
public static final String KML_FILE_POSTFIX = ".kml";
public static final long MAX_TIME_TO_WAIT_SYNC = 50000;
/**
* Setups sync tests.
*
* @param accountName the name of account
* @return drive object of Goolge Drive
*/
public static Drive setUpForSyncTest(String accountName) {
SyncTestUtils.enableSync(accountName);
return SyncTestUtils.getGoogleDrive(EndToEndTestUtils.activityMytracks.getApplicationContext());
}
/**
* Gets drive object of Google Drive.
*
* @param context the context of application
* @return drive drive object of Google Drive
*/
public static Drive getGoogleDrive(Context context) {
String googleAccount = PreferencesUtils.getString(context, R.string.google_account_key,
@@ -62,7 +76,7 @@ public class SyncTestUtils {
}
/**
* Queries files from Google Drive.
* Queries KML files from Google Drive.
*
* @param context the context of application
* @param drive drive object of Google Drive
@@ -70,7 +84,11 @@ public class SyncTestUtils {
* @throws IOException
*/
public static List<File> updateDriveFiles(Context context, Drive drive) throws IOException {
String folderId = SyncUtils.getMyTracksFolder(context, drive);
File folder = SyncTestUtils.getMyTracksFolder(context, drive);
if ( folder == null) {
return new ArrayList<File>();
}
String folderId = folder.getId();
return drive.files().list()
.setQ(String.format(Locale.US, SyncUtils.GET_KML_FILES_QUERY, folderId)).execute()
.getItems();
@@ -164,26 +182,29 @@ public class SyncTestUtils {
EndToEndTestUtils.SHORT_WAIT_TIME);
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
.getString(R.string.generic_ok));
EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.SOLO.goBack();
}
EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.SOLO.goBack();
}
/**
* Checks the files number on Google Drive
*
* @param number number of files on Google Drive
* @param drive drive object of Google Drive
* @throws IOException
*/
public static void checkFilesNumber(int number, Drive drive) throws IOException {
public static void checkFilesNumber(Drive drive) throws IOException {
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < MAX_TIME_TO_WAIT_SYNC) {
List<File> files = updateDriveFiles(
EndToEndTestUtils.activityMytracks.getApplicationContext(), drive);
if (files.size() == number) {
return;
try {
int trackNumber = EndToEndTestUtils.SOLO.getCurrentListViews().get(0).getCount();
List<File> files = updateDriveFiles(
EndToEndTestUtils.activityMytracks.getApplicationContext(), drive);
if (files.size() == trackNumber) {
return;
}
} catch (GoogleJsonResponseException e) {
EndToEndTestUtils.sleep(EndToEndTestUtils.SHORT_WAIT_TIME);
}
}
Assert.fail();
@@ -222,6 +243,7 @@ public class SyncTestUtils {
return true;
}
}
Assert.fail();
return false;
}
@@ -247,4 +269,28 @@ public class SyncTestUtils {
br.close();
return fileContent;
}
/**
* Gets the MyTracks folder on Google Drive.
*
* @param context context of application
* @param drive drive object of Google Drive
* @return the MyTracks folder on Google Drive
*/
public static File getMyTracksFolder(Context context, Drive drive) {
try {
String folderName = context.getString(R.string.my_tracks_app_name);
com.google.api.services.drive.Drive.Files.List list = drive.files().list()
.setQ(String.format(Locale.US, SyncUtils.GET_MY_TRACKS_FOLDER_QUERY, folderName));
FileList result = list.execute();
for (File file : result.getItems()) {
if (file.getTitle().equals(folderName)) {
return file;
}
}
} catch (IOException e) {
Log.e(EndToEndTestUtils.LOG_TAG, "IOException", e);
}
return null;
}
}