Add unlock method to make it can be run after the device is locked. And change some code style.

This commit is contained in:
youtaol
2012-12-13 18:18:45 +08:00
parent 612dac55d8
commit a90a2ec692
3 changed files with 141 additions and 126 deletions
@@ -53,7 +53,7 @@ import junit.framework.Assert;
* @author Youtao Liu * @author Youtao Liu
*/ */
public class EndToEndTestUtils { public class EndToEndTestUtils {
private static final String ANDROID_LOCAL_IP = "10.0.2.2"; private static final String ANDROID_LOCAL_IP = "10.0.2.2";
// usually 5554. // usually 5554.
public static int emulatorPort = 5554; public static int emulatorPort = 5554;
@@ -61,18 +61,16 @@ public class EndToEndTestUtils {
private static final int ORIENTATION_PORTRAIT = 1; private static final int ORIENTATION_PORTRAIT = 1;
private static final int ORIENTATION_LANDSCAPE = 0; private static final int ORIENTATION_LANDSCAPE = 0;
// Pause 200ms between each send. // Pause 200ms between each send.
private static final int PAUSE_DEFAULT = 200;
static final double START_LONGITUDE = 51; static final double START_LONGITUDE = 51;
static final double START_LATITUDE = -1.3f; static final double START_LATITUDE = -1.3f;
static final double DELTA_LONGITUDE = 0.0005f; static final double DELTA_LONGITUDE = 0.0005f;
static final double DELTA_LADITUDE = 0.0005f; static final double DELTA_LADITUDE = 0.0005f;
private static final String WAYPOINT_NAME = "testWaypoint"; private static final String WAYPOINT_NAME = "testWaypoint";
private static final String NO_GPS_MESSAGE_PREFIX = "GPS is not available"; private static final String NO_GPS_MESSAGE_PREFIX = "GPS is not available";
private static final String MOREOPTION_CLASSNAME = "com.android.internal.view.menu.ActionMenuPresenter$OverflowMenuButton"; private static final String MOREOPTION_CLASSNAME = "com.android.internal.view.menu.ActionMenuPresenter$OverflowMenuButton";
private static final String MENUITEM_CLASSNAME = "com.android.internal.view.menu.IconMenuItemView"; private static final String MENUITEM_CLASSNAME = "com.android.internal.view.menu.IconMenuItemView";
static final String DEFAULTACTIVITYTYPE = "TestActivity"; static final String DEFAULTACTIVITYTYPE = "TestActivity";
public static String activityType = DEFAULTACTIVITYTYPE; public static String activityType = DEFAULTACTIVITYTYPE;
static final String TRACK_NAME_PREFIX = "testTrackName"; static final String TRACK_NAME_PREFIX = "testTrackName";
@@ -83,36 +81,37 @@ public class EndToEndTestUtils {
static final String BACKUPS = "backups"; static final String BACKUPS = "backups";
static final String MENU_MORE = "More"; static final String MENU_MORE = "More";
static String trackName; static String trackName;
// Following is some check strings in English and Chinese // Following is some check strings in English and Chinese
private static final HashMap<String, String> RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL = new HashMap<String, String>(); private static final HashMap<String, String> RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL = new HashMap<String, String>();
private static final HashMap<String, String> KM_MULTILINGUAL = new HashMap<String, String>(); private static final HashMap<String, String> KM_MULTILINGUAL = new HashMap<String, String>();
private static final HashMap<String, String> MILE_MULTILINGUAL = new HashMap<String, String>(); private static final HashMap<String, String> MILE_MULTILINGUAL = new HashMap<String, String>();
public static String RELATIVE_STARTTIME_POSTFIX = ""; public static String RELATIVE_STARTTIME_POSTFIX = "";
public static String KM = ""; public static String KM = "";
public static String MILE = ""; public static String MILE = "";
public static int VERY_SHORT_WAIT_TIME = 500; public static final int TINY_WAIT_TIME = 200;
public static int SHORT_WAIT_TIME = 2000; public static final int VERY_SHORT_WAIT_TIME = 500;
public static int NORMAL_WAIT_TIME = 8000; public static final int SHORT_WAIT_TIME = 2000;
public static int LONG_WAIT_TIME = 15000; public static final int NORMAL_WAIT_TIME = 8000;
public static int SUPER_LONG_WAIT_TIME = 100000; public static final int LONG_WAIT_TIME = 15000;
public static final int SUPER_LONG_WAIT_TIME = 100000;
public static String deviceLanguage = ""; public static String deviceLanguage = "";
static { static {
RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("es", "mins ago"); RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("es", "mins ago");
RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("de", "Minuten"); RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("de", "Minuten");
RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("fr", "minute"); RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("fr", "minute");
RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("ar", "دقيقة"); RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("ar", "دقيقة");
RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("zh", "分钟前"); RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.put("zh", "分钟前");
KM_MULTILINGUAL.put("es", "km"); KM_MULTILINGUAL.put("es", "km");
KM_MULTILINGUAL.put("de", "km"); KM_MULTILINGUAL.put("de", "km");
KM_MULTILINGUAL.put("fr", "km"); KM_MULTILINGUAL.put("fr", "km");
KM_MULTILINGUAL.put("ar", "كم"); KM_MULTILINGUAL.put("ar", "كم");
KM_MULTILINGUAL.put("zh", "公里"); KM_MULTILINGUAL.put("zh", "公里");
MILE_MULTILINGUAL.put("es", "mi"); MILE_MULTILINGUAL.put("es", "mi");
MILE_MULTILINGUAL.put("de", "mi"); MILE_MULTILINGUAL.put("de", "mi");
MILE_MULTILINGUAL.put("fr", "mile"); MILE_MULTILINGUAL.put("fr", "mile");
@@ -133,20 +132,20 @@ public class EndToEndTestUtils {
public static final String LOG_TAG = "MyTracksTest"; public static final String LOG_TAG = "MyTracksTest";
private EndToEndTestUtils() {} private EndToEndTestUtils() {}
/** /**
* Checks the language, then sets the fields with right string. * Checks the language, then sets the fields with right string.
*/ */
private static void checkLanguage() { private static void checkLanguage() {
Locale locale = null; Locale locale = null;
Configuration config=null; Configuration config = null;
config = activityMytracks.getBaseContext().getResources().getConfiguration(); config = activityMytracks.getBaseContext().getResources().getConfiguration();
locale = new Locale("en"); locale = new Locale("en");
Locale.setDefault(locale); Locale.setDefault(locale);
config.locale = locale; config.locale = locale;
deviceLanguage = instrumentation.getContext().getResources().getConfiguration().locale
deviceLanguage = instrumentation.getContext().getResources().getConfiguration().locale.getLanguage(); .getLanguage();
if (RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.get(deviceLanguage) != null) { if (RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.get(deviceLanguage) != null) {
RELATIVE_STARTTIME_POSTFIX = RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.get(deviceLanguage); RELATIVE_STARTTIME_POSTFIX = RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.get(deviceLanguage);
KM = KM_MULTILINGUAL.get(deviceLanguage); KM = KM_MULTILINGUAL.get(deviceLanguage);
@@ -162,24 +161,24 @@ public class EndToEndTestUtils {
* Sends Gps data to emulator, and the start value has an offset. * Sends Gps data to emulator, and the start value has an offset.
* *
* @param number send times * @param number send times
* @param offset is used to compute the start latitude and longitude * @param offset is used to compute the start latitude and longitude
* @param pause pause interval between each sending * @param pause pause interval between each sending
*/ */
public static void sendGps(int number, int offset, int pause) { public static void sendGps(int number, int offset, int pause) {
if (number < 1) { if (number < 1) {
return; return;
} }
int pauseInterval = PAUSE_DEFAULT; int pauseInterval = TINY_WAIT_TIME;
if (pause != -1) { if (pause != -1) {
pauseInterval = pause; pauseInterval = pause;
} }
// If it's a real device, does not send simulated GPS signal. // If it's a real device, does not send simulated GPS signal.
if (!isEmulator) { if (!isEmulator) {
return; return;
} }
PrintStream out = null; PrintStream out = null;
Socket socket = null; Socket socket = null;
try { try {
@@ -193,7 +192,7 @@ public class EndToEndTestUtils {
latitude += DELTA_LADITUDE; latitude += DELTA_LADITUDE;
Thread.sleep(pauseInterval); Thread.sleep(pauseInterval);
} }
// Wait the GPS signal can be obtained by MyTracks. // Wait the GPS signal can be obtained by MyTracks.
Thread.sleep(SHORT_WAIT_TIME); Thread.sleep(SHORT_WAIT_TIME);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
System.exit(-1); System.exit(-1);
@@ -207,17 +206,17 @@ public class EndToEndTestUtils {
} }
} }
} }
/** /**
* Send Gps data to emulator. * Send Gps data to emulator.
* *
* @param number number of signals * @param number number of signals
* @param offset is used to compute the start latitude and longitude * @param offset is used to compute the start latitude and longitude
*/ */
public static void sendGps(int number, int offset) { public static void sendGps(int number, int offset) {
sendGps(number, offset, -1); sendGps(number, offset, -1);
} }
/** /**
* Send Gps data to emulator. * Send Gps data to emulator.
* *
@@ -226,17 +225,17 @@ public class EndToEndTestUtils {
public static void sendGps(int number) { public static void sendGps(int number) {
sendGps(number, 0, -1); sendGps(number, 0, -1);
} }
/** /**
* Sets the status whether the test is run on an emulator or not. * Sets the status whether the test is run on an emulator or not.
*/ */
private static void setIsEmulator() { private static void setIsEmulator() {
isEmulator = android.os.Build.MODEL.equals("google_sdk"); isEmulator = android.os.Build.MODEL.equals("google_sdk");
} }
/** /**
* Checks whether the Google Play Services need update. * Checks whether the Google Play Services need update.
*/ */
private static boolean isGooglePlayServicesLatest() { private static boolean isGooglePlayServicesLatest() {
return findTextView("Google Play services") == null; return findTextView("Google Play services") == null;
} }
@@ -285,8 +284,7 @@ public class EndToEndTestUtils {
Log.e(LOG_TAG, "Need update Google Play Services"); Log.e(LOG_TAG, "Need update Google Play Services");
} }
setIsEmulator(); setIsEmulator();
if ((getButtonOnScreen(activityMytracks.getString(R.string.eula_accept), if ((getButtonOnScreen(activityMytracks.getString(R.string.eula_accept), false, false) != null)) {
false, false) != null)) {
verifyFirstLaunch(); verifyFirstLaunch();
} else if (SOLO.waitForText( } else if (SOLO.waitForText(
// After reset setting, welcome page will show again. // After reset setting, welcome page will show again.
@@ -303,8 +301,7 @@ public class EndToEndTestUtils {
// Check the status of real phone. For emulator, we would fix GPS signal. // Check the status of real phone. For emulator, we would fix GPS signal.
if (!isEmulator) { if (!isEmulator) {
GoToMyLocationTest.findAndClickMyLocation(activityMyTracks); GoToMyLocationTest.findAndClickMyLocation(activityMyTracks);
hasGpsSingal = !SOLO.waitForText(NO_GPS_MESSAGE_PREFIX, 1, hasGpsSingal = !SOLO.waitForText(NO_GPS_MESSAGE_PREFIX, 1, SHORT_WAIT_TIME);
SHORT_WAIT_TIME);
SOLO.goBack(); SOLO.goBack();
} }
} else if (SOLO.waitForText( } else if (SOLO.waitForText(
@@ -319,7 +316,7 @@ public class EndToEndTestUtils {
stopRecording(true); stopRecording(true);
} }
} }
/** /**
* Checks if need reset preferred units. * Checks if need reset preferred units.
*/ */
@@ -330,7 +327,6 @@ public class EndToEndTestUtils {
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
} }
/** /**
* Rotates the given activity. * Rotates the given activity.
* *
@@ -369,18 +365,19 @@ public class EndToEndTestUtils {
sendGps(numberOfGpsData); sendGps(numberOfGpsData);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
stopRecording(true); stopRecording(true);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
if (showTrackList) { if (showTrackList) {
SOLO.goBack(); SOLO.goBack();
} }
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
} }
/** /**
* Creates a track which contains pause during recording. * Creates a track which contains pause during recording.
* *
* @param numberOfGpsData number of simulated Gps data before pause and after resume * @param numberOfGpsData number of simulated Gps data before pause and after
* resume
*/ */
public static void createTrackWithPause(int numberOfGpsData) { public static void createTrackWithPause(int numberOfGpsData) {
startRecording(); startRecording();
@@ -390,8 +387,7 @@ public class EndToEndTestUtils {
sendGps(numberOfGpsData, numberOfGpsData); sendGps(numberOfGpsData, numberOfGpsData);
stopRecording(true); stopRecording(true);
} }
/** /**
* Checks if there is no track in track list. For some tests need at least one * Checks if there is no track in track list. For some tests need at least one
* track, the method can save time to create a new track. * track, the method can save time to create a new track.
@@ -412,7 +408,7 @@ public class EndToEndTestUtils {
} }
return false; return false;
} }
/** /**
* Create a new track if the track is empty. * Create a new track if the track is empty.
* *
@@ -432,34 +428,34 @@ public class EndToEndTestUtils {
*/ */
public static void startRecording() { public static void startRecording() {
View startButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record); View startButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record);
if(startButton != null && startButton.isShown()) { if (startButton != null && startButton.isShown()) {
SOLO.clickOnView(startButton); SOLO.clickOnView(startButton);
} }
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
} }
/** /**
* Pauses recoding track. * Pauses recoding track.
*/ */
public static void pauseRecording() { public static void pauseRecording() {
View pauseButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record); View pauseButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record);
if(pauseButton != null && pauseButton.isShown()) { if (pauseButton != null && pauseButton.isShown()) {
SOLO.clickOnView(pauseButton); SOLO.clickOnView(pauseButton);
} }
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
} }
/** /**
* Resume recoding track. * Resume recoding track.
*/ */
public static void resumeRecording() { public static void resumeRecording() {
View startButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record); View startButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record);
if(startButton != null && startButton.isShown()) { if (startButton != null && startButton.isShown()) {
SOLO.clickOnView(startButton); SOLO.clickOnView(startButton);
} }
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
} }
/** /**
* Checks if the MyTracks is under recording. * Checks if the MyTracks is under recording.
* *
@@ -477,21 +473,20 @@ public class EndToEndTestUtils {
*/ */
public static void stopRecording(boolean isSave) { public static void stopRecording(boolean isSave) {
View stopButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_stop); View stopButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_stop);
if(stopButton != null && stopButton.isShown() ) { if (stopButton != null && stopButton.isShown()) {
SOLO.clickOnView(stopButton); SOLO.clickOnView(stopButton);
if (isSave) { if (isSave) {
SOLO.waitForText(activityMytracks.getString(R.string.generic_save), 1, 5000); SOLO.waitForText(activityMytracks.getString(R.string.generic_save), 1, 5000);
// Make every track name is unique to make sure every check can be // Make every track name is unique to make sure every check can be
// trusted. // trusted.
trackName = TRACK_NAME_PREFIX trackName = TRACK_NAME_PREFIX + System.currentTimeMillis();
+ System.currentTimeMillis();
SOLO.sendKey(KeyEvent.KEYCODE_DEL); SOLO.sendKey(KeyEvent.KEYCODE_DEL);
enterTextAvoidSoftKeyBoard(0, trackName); enterTextAvoidSoftKeyBoard(0, trackName);
enterTextAvoidSoftKeyBoard(1, activityType); enterTextAvoidSoftKeyBoard(1, activityType);
SOLO.clickOnText(activityMytracks.getString(R.string.generic_save)); SOLO.clickOnText(activityMytracks.getString(R.string.generic_save));
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
} }
} }
} }
/** /**
@@ -508,7 +503,7 @@ public class EndToEndTestUtils {
} }
} }
} }
/** /**
* Deletes all tracks. This method should be call when the TracksListActivity * Deletes all tracks. This method should be call when the TracksListActivity
* is shown. * is shown.
@@ -519,7 +514,7 @@ public class EndToEndTestUtils {
getButtonOnScreen(activityMytracks.getString(R.string.generic_ok), true, true); getButtonOnScreen(activityMytracks.getString(R.string.generic_ok), true, true);
} }
} }
/** /**
* Gets a kind of exported files. * Gets a kind of exported files.
* *
@@ -548,7 +543,7 @@ public class EndToEndTestUtils {
findMenuItem(activityMytracks.getString(R.string.menu_save), true); findMenuItem(activityMytracks.getString(R.string.menu_save), true);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
SOLO.clickOnText(trackKind.toUpperCase()); SOLO.clickOnText(trackKind.toUpperCase());
// rotateAllActivities(); // rotateAllActivities();
SOLO.waitForText(activityMytracks.getString(R.string.generic_success_title)); SOLO.waitForText(activityMytracks.getString(R.string.generic_success_title));
} }
@@ -560,22 +555,22 @@ public class EndToEndTestUtils {
* @param isClick whether click the button if find it * @param isClick whether click the button if find it
* @return the button to search, and null means can not find the button * @return the button to search, and null means can not find the button
*/ */
public static View getButtonOnScreen(String buttonName, boolean isWait,boolean isClick) { public static View getButtonOnScreen(String buttonName, boolean isWait, boolean isClick) {
View button = null; View button = null;
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
if (isWait) { if (isWait) {
SOLO.waitForText(buttonName); SOLO.waitForText(buttonName);
} }
// Find on action bar. // Find on action bar.
if (hasActionBar) { if (hasActionBar) {
ArrayList<View> allViews = SOLO.getViews(); ArrayList<View> allViews = SOLO.getViews();
for (View view : allViews) { for (View view : allViews) {
String className = view.getClass().getName(); String className = view.getClass().getName();
if(className.indexOf("ActionMenuItemView") > 0) { if (className.indexOf("ActionMenuItemView") > 0) {
String menuItemNameString = view.getContentDescription().toString(); String menuItemNameString = view.getContentDescription().toString();
if(menuItemNameString.equalsIgnoreCase(buttonName)) { if (menuItemNameString.equalsIgnoreCase(buttonName)) {
button = view; button = view;
break; break;
} }
@@ -584,24 +579,24 @@ public class EndToEndTestUtils {
} }
// Get all buttons and find. // Get all buttons and find.
if(button == null) { if (button == null) {
ArrayList<Button> currentButtons = SOLO.getCurrentButtons(); ArrayList<Button> currentButtons = SOLO.getCurrentButtons();
for (Button oneButton : currentButtons) { for (Button oneButton : currentButtons) {
String title = (String) oneButton.getText(); String title = (String) oneButton.getText();
if (title.equalsIgnoreCase(buttonName)) { if (title.equalsIgnoreCase(buttonName)) {
button = oneButton; button = oneButton;
} }
} }
} }
if(button != null && isClick) { if (button != null && isClick) {
SOLO.clickOnView(button); SOLO.clickOnView(button);
} }
if (button == null && isClick) { if (button == null && isClick) {
Log.d(LOG_TAG, "Don't find the button " + buttonName); Log.d(LOG_TAG, "Don't find the button " + buttonName);
} }
return button; return button;
} }
@@ -613,7 +608,7 @@ public class EndToEndTestUtils {
private static boolean setHasActionBar() { private static boolean setHasActionBar() {
try { try {
return activityMytracks.getActionBar() == null ? false : true; return activityMytracks.getActionBar() == null ? false : true;
}catch (Throwable e) { } catch (Throwable e) {
// For in Android which does not has action bar, here will meet a error. // For in Android which does not has action bar, here will meet a error.
return false; return false;
} }
@@ -667,7 +662,7 @@ public class EndToEndTestUtils {
findResult = SOLO.searchText(menuName, 1, true); findResult = SOLO.searchText(menuName, 1, true);
} else { } else {
// Non-ICS phone. // Non-ICS phone.
SOLO.sendKey(KeyEvent.KEYCODE_MENU); SOLO.sendKey(KeyEvent.KEYCODE_MENU);
if (SOLO.searchText(menuName, 1, true)) { if (SOLO.searchText(menuName, 1, true)) {
findResult = true; findResult = true;
} else if (getMoreOptionView() != null) { } else if (getMoreOptionView() != null) {
@@ -697,7 +692,7 @@ public class EndToEndTestUtils {
public static void showMenuItem() { public static void showMenuItem() {
showMenuItem(0); showMenuItem(0);
} }
/** /**
* Gets more menu items operation is different for different Android OS. When * Gets more menu items operation is different for different Android OS. When
* get overflow button view on action, it usually be able to click. But in * get overflow button view on action, it usually be able to click. But in
@@ -730,13 +725,14 @@ public class EndToEndTestUtils {
SOLO.sendKey(KeyEvent.KEYCODE_MENU); SOLO.sendKey(KeyEvent.KEYCODE_MENU);
} }
} }
/** /**
* Finds the more option menu. It should match following conditions: * Finds the more option menu. It should match following conditions:
* <ul> * <ul>
* <li>The view extends ImageButton.</li> * <li>The view extends ImageButton.</li>
* <li>The view name equals {@link #MOREOPTION_CLASSNAME}.</li> * <li>The view name equals {@link #MOREOPTION_CLASSNAME}.</li>
* </ul> * </ul>
*
* @return the more option view. Null means can not find it. * @return the more option view. Null means can not find it.
*/ */
private static View getMoreOptionView() { private static View getMoreOptionView() {
@@ -760,7 +756,7 @@ public class EndToEndTestUtils {
} }
return viewResult; return viewResult;
} }
/** /**
* Finds a displayed text view with specified text in a view. * Finds a displayed text view with specified text in a view.
* *
@@ -772,13 +768,13 @@ public class EndToEndTestUtils {
ArrayList<TextView> textViews = SOLO.getCurrentTextViews(parent); ArrayList<TextView> textViews = SOLO.getCurrentTextViews(parent);
for (TextView textView : textViews) { for (TextView textView : textViews) {
String textString = (String) textView.getText(); String textString = (String) textView.getText();
if (textView.isShown() && textString.endsWith(text)) { if (textView.isShown() && textString.endsWith(text)) {
return textView; return textView;
} }
} }
return null; return null;
} }
/** /**
* Finds a displayed text view with specified text. * Finds a displayed text view with specified text.
* *
@@ -798,7 +794,7 @@ public class EndToEndTestUtils {
} }
return null; return null;
} }
/** /**
* Finds a displayed text view with specified text and index number. * Finds a displayed text view with specified text and index number.
* *
@@ -833,13 +829,13 @@ public class EndToEndTestUtils {
public static ChartView getChartView() { public static ChartView getChartView() {
ArrayList<View> views = SOLO.getViews(); ArrayList<View> views = SOLO.getViews();
for (View view : views) { for (View view : views) {
if (view instanceof ChartView) { if (view instanceof ChartView) {
return (ChartView) view; return (ChartView) view;
} }
} }
return null; return null;
} }
/** /**
* Resets all settings of MyTracks. * Resets all settings of MyTracks.
* *
@@ -858,7 +854,7 @@ public class EndToEndTestUtils {
SOLO.goBack(); SOLO.goBack();
} }
} }
/** /**
* Finds a edit text and enter text in it. This method can hides soft key * Finds a edit text and enter text in it. This method can hides soft key
* board when input text. * board when input text.
@@ -867,12 +863,13 @@ public class EndToEndTestUtils {
* @param text to enter * @param text to enter
*/ */
public static void enterTextAvoidSoftKeyBoard(int editTextIndex, String text) { public static void enterTextAvoidSoftKeyBoard(int editTextIndex, String text) {
InputMethodManager imm = (InputMethodManager) activityMytracks.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) activityMytracks.getApplicationContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
EditText editText = SOLO.getEditText(editTextIndex); EditText editText = SOLO.getEditText(editTextIndex);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
SOLO.enterText(editText, text); SOLO.enterText(editText, text);
} }
/** /**
* Waits n milliseconds. * Waits n milliseconds.
* *
@@ -884,7 +881,7 @@ public class EndToEndTestUtils {
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.e(LOG_TAG, "Unable to sleep " + milliseconds, e); Log.e(LOG_TAG, "Unable to sleep " + milliseconds, e);
} }
} }
/** /**
* Checks whether the recording is not started. * Checks whether the recording is not started.
@@ -897,7 +894,8 @@ public class EndToEndTestUtils {
.isEnabled()); .isEnabled());
Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_recording))); Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_recording)));
Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_paused))); Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_paused)));
TextView totalTime = (TextView) SOLO.getCurrentActivity().findViewById(R.id.track_controller_total_time); TextView totalTime = (TextView) SOLO.getCurrentActivity().findViewById(
R.id.track_controller_total_time);
Assert.assertEquals(StringUtils.formatElapsedTimeWithHour(0), totalTime.getText().toString()); Assert.assertEquals(StringUtils.formatElapsedTimeWithHour(0), totalTime.getText().toString());
} }
@@ -933,14 +931,13 @@ public class EndToEndTestUtils {
Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_recording))); Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_recording)));
Assert.assertNotNull(findTextView(activityMytracks.getString(R.string.generic_paused))); Assert.assertNotNull(findTextView(activityMytracks.getString(R.string.generic_paused)));
String totalTimeOld = ((TextView) SOLO.getCurrentActivity() String totalTimeOld = ((TextView) SOLO.getCurrentActivity().findViewById(
.findViewById(R.id.track_controller_total_time)).getText().toString(); R.id.track_controller_total_time)).getText().toString();
sleep(2000); sleep(2000);
String totalTimeNew = ((TextView) SOLO.getCurrentActivity() String totalTimeNew = ((TextView) SOLO.getCurrentActivity().findViewById(
.findViewById(R.id.track_controller_total_time)).getText().toString(); R.id.track_controller_total_time)).getText().toString();
Assert.assertTrue(totalTimeOld.equalsIgnoreCase(totalTimeNew)); Assert.assertTrue(totalTimeOld.equalsIgnoreCase(totalTimeNew));
} }
/** /**
* Creates a way point during track recording. * Creates a way point during track recording.
@@ -967,7 +964,7 @@ public class EndToEndTestUtils {
} }
SOLO.goBack(); SOLO.goBack();
} }
/** /**
* Waits a text to disappear. * Waits a text to disappear.
*/ */
@@ -10,6 +10,8 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Environment; import android.os.Environment;
import android.util.Log; import android.util.Log;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@@ -122,8 +124,25 @@ public class BigTestUtils {
String oneInfo = (new java.util.Date()).toString() + ":" + memoryUsageString + ", " String oneInfo = (new java.util.Date()).toString() + ":" + memoryUsageString + ", "
+ batteryUsageString + ".\r\n"; + batteryUsageString + ".\r\n";
writeToFile(oneInfo, true); writeToFile(oneInfo, true);
Log.i(EndToEndTestUtils.LOG_TAG, oneInfo);
EndToEndTestUtils.sleep(interval); EndToEndTestUtils.sleep(interval);
} }
} }
/**
* Unlocks the device screen.
*/
public static void unlockDevice() {
// The menu item of setting should be found if the screen is not locked.
try {
Window wind = EndToEndTestUtils.SOLO.getCurrentActivity().getWindow();
wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
} catch (Exception e) {
Log.i(EndToEndTestUtils.LOG_TAG,
"Meet error when unlock device screen, may the device is not locked.");
}
}
} }
@@ -46,7 +46,7 @@ public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackLis
private static boolean isUserConfirmed = false; private static boolean isUserConfirmed = false;
private Context context; private Context context;
/** /**
* Default duration for each test is half hour. * Default duration for each test is one hour.
*/ */
private int TEST_DURATION_IN_MILLISECONDS = 30 * 60 * 1000; private int TEST_DURATION_IN_MILLISECONDS = 30 * 60 * 1000;
private int INTERVALE_TO_CHECK = 5 * 60 * 1000; private int INTERVALE_TO_CHECK = 5 * 60 * 1000;
@@ -61,9 +61,11 @@ public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackLis
instrumentation = getInstrumentation(); instrumentation = getInstrumentation();
trackListActivity = getActivity(); trackListActivity = getActivity();
context = trackListActivity.getApplicationContext(); context = trackListActivity.getApplicationContext();
instrumentation.waitForIdleSync();
EndToEndTestUtils.setupForDebug(instrumentation, trackListActivity); EndToEndTestUtils.setupForDebug(instrumentation, trackListActivity);
BigTestUtils.unlockDevice();
if (!isUserConfirmed) { if (!isUserConfirmed) {
userConfirmTest(); confirmTest();
} }
BigTestUtils.writeToFile("Test case start:" + getName() + "\r\n", true); BigTestUtils.writeToFile("Test case start:" + getName() + "\r\n", true);
} }
@@ -71,7 +73,7 @@ public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackLis
/** /**
* Prompts a dialog to make user confirm whether start the test. * Prompts a dialog to make user confirm whether start the test.
*/ */
private void userConfirmTest() { private void confirmTest() {
try { try {
trackListActivity.runOnUiThread(new Runnable() { trackListActivity.runOnUiThread(new Runnable() {
@Override @Override
@@ -110,54 +112,51 @@ public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackLis
* Tests the resource usage when display track list activity during recording. * Tests the resource usage when display track list activity during recording.
*/ */
public void testBatteryUsage_showTrackList() { public void testBatteryUsage_showTrackList() {
if (!runTest) { recordingLongTrack(true, "");
Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE);
}
EndToEndTestUtils.startRecording();
// Go back to track list.
EndToEndTestUtils.SOLO.goBack();
BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS);
EndToEndTestUtils.stopRecording(true);
} }
/** /**
* Tests the resource usage when display map view during recording. * Tests the resource usage when display map view during recording.
*/ */
public void testBatteryUsage_showMapView() { public void testBatteryUsage_showMapView() {
if (!runTest) { recordingLongTrack(false, trackListActivity.getString(R.string.track_detail_map_tab));
Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE);
}
EndToEndTestUtils.startRecording();
EndToEndTestUtils.SOLO.clickOnText(trackListActivity.getString(R.string.track_detail_map_tab));
BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS);
EndToEndTestUtils.stopRecording(true);
} }
/** /**
* Tests the resource usage when display chart view during recording. * Tests the resource usage when display chart view during recording.
*/ */
public void testBatteryUsage_showChartView() { public void testBatteryUsage_showChartView() {
if (!runTest) { recordingLongTrack(false, trackListActivity.getString(R.string.track_detail_chart_tab));
Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE);
}
EndToEndTestUtils.startRecording();
EndToEndTestUtils.SOLO
.clickOnText(trackListActivity.getString(R.string.track_detail_chart_tab));
BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS);
EndToEndTestUtils.stopRecording(true);
} }
/** /**
* Tests the resource usage when display stats view during recording. * Tests the resource usage when display stats view during recording.
*/ */
public void testBatteryUsage_showStatsView() { public void testBatteryUsage_showStatsView() {
recordingLongTrack(false, trackListActivity.getString(R.string.track_detail_stats_tab));
}
/**
* Records a long time track and keeps in the specified tab or in tracks list
* during recording.
*
* @param isShowTracksList true means show tracks list
* @param tabName the name of tab, only available when the first parameter is
* false.
*/
public void recordingLongTrack(boolean isShowTracksList, String tabName) {
if (!runTest) { if (!runTest) {
Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE); Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE);
} }
EndToEndTestUtils.startRecording(); EndToEndTestUtils.startRecording();
EndToEndTestUtils.SOLO if (isShowTracksList) {
.clickOnText(trackListActivity.getString(R.string.track_detail_stats_tab)); EndToEndTestUtils.SOLO.goBack();
} else {
EndToEndTestUtils.SOLO.clickOnText(tabName);
}
BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS); BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS);
BigTestUtils.unlockDevice();
EndToEndTestUtils.stopRecording(true); EndToEndTestUtils.stopRecording(true);
} }