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
@@ -61,7 +61,6 @@ 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;
@@ -72,7 +71,6 @@ public class EndToEndTestUtils {
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";
@@ -92,11 +90,12 @@ public class EndToEndTestUtils {
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 = "";
@@ -139,14 +138,14 @@ public class EndToEndTestUtils {
*/ */
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);
@@ -170,7 +169,7 @@ public class EndToEndTestUtils {
return; return;
} }
int pauseInterval = PAUSE_DEFAULT; int pauseInterval = TINY_WAIT_TIME;
if (pause != -1) { if (pause != -1) {
pauseInterval = pause; pauseInterval = pause;
} }
@@ -235,8 +234,8 @@ public class EndToEndTestUtils {
} }
/** /**
* 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(
@@ -330,7 +327,6 @@ public class EndToEndTestUtils {
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
} }
/** /**
* Rotates the given activity. * Rotates the given activity.
* *
@@ -380,7 +376,8 @@ public class EndToEndTestUtils {
/** /**
* 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();
@@ -391,7 +388,6 @@ public class EndToEndTestUtils {
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.
@@ -432,7 +428,7 @@ 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();
@@ -443,7 +439,7 @@ public class EndToEndTestUtils {
*/ */
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();
@@ -454,7 +450,7 @@ public class EndToEndTestUtils {
*/ */
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();
@@ -477,14 +473,13 @@ 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);
@@ -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,7 +555,7 @@ 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();
@@ -573,9 +568,9 @@ public class EndToEndTestUtils {
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,7 +579,7 @@ 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();
@@ -594,7 +589,7 @@ public class EndToEndTestUtils {
} }
} }
if(button != null && isClick) { if (button != null && isClick) {
SOLO.clickOnView(button); SOLO.clickOnView(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;
} }
@@ -737,6 +732,7 @@ public class EndToEndTestUtils {
* <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() {
@@ -867,7 +863,8 @@ 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);
@@ -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,15 +931,14 @@ 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.
* *
@@ -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);
} }