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_LANDSCAPE = 0;
// Pause 200ms between each send.
private static final int PAUSE_DEFAULT = 200;
static final double START_LONGITUDE = 51;
static final double START_LATITUDE = -1.3f;
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 MENUITEM_CLASSNAME = "com.android.internal.view.menu.IconMenuItemView";
static final String DEFAULTACTIVITYTYPE = "TestActivity";
public static String activityType = DEFAULTACTIVITYTYPE;
static final String TRACK_NAME_PREFIX = "testTrackName";
@@ -92,11 +90,12 @@ public class EndToEndTestUtils {
public static String KM = "";
public static String MILE = "";
public static int VERY_SHORT_WAIT_TIME = 500;
public static int SHORT_WAIT_TIME = 2000;
public static int NORMAL_WAIT_TIME = 8000;
public static int LONG_WAIT_TIME = 15000;
public static int SUPER_LONG_WAIT_TIME = 100000;
public static final int TINY_WAIT_TIME = 200;
public static final int VERY_SHORT_WAIT_TIME = 500;
public static final int SHORT_WAIT_TIME = 2000;
public static final int NORMAL_WAIT_TIME = 8000;
public static final int LONG_WAIT_TIME = 15000;
public static final int SUPER_LONG_WAIT_TIME = 100000;
public static String deviceLanguage = "";
@@ -139,14 +138,14 @@ public class EndToEndTestUtils {
*/
private static void checkLanguage() {
Locale locale = null;
Configuration config=null;
config = activityMytracks.getBaseContext().getResources().getConfiguration();
Configuration config = null;
config = activityMytracks.getBaseContext().getResources().getConfiguration();
locale = new Locale("en");
Locale.setDefault(locale);
config.locale = locale;
deviceLanguage = instrumentation.getContext().getResources().getConfiguration().locale.getLanguage();
deviceLanguage = instrumentation.getContext().getResources().getConfiguration().locale
.getLanguage();
if (RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.get(deviceLanguage) != null) {
RELATIVE_STARTTIME_POSTFIX = RELATIVE_STARTTIME_POSTFIX_MULTILINGUAL.get(deviceLanguage);
KM = KM_MULTILINGUAL.get(deviceLanguage);
@@ -170,7 +169,7 @@ public class EndToEndTestUtils {
return;
}
int pauseInterval = PAUSE_DEFAULT;
int pauseInterval = TINY_WAIT_TIME;
if (pause != -1) {
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() {
return findTextView("Google Play services") == null;
}
@@ -285,8 +284,7 @@ public class EndToEndTestUtils {
Log.e(LOG_TAG, "Need update Google Play Services");
}
setIsEmulator();
if ((getButtonOnScreen(activityMytracks.getString(R.string.eula_accept),
false, false) != null)) {
if ((getButtonOnScreen(activityMytracks.getString(R.string.eula_accept), false, false) != null)) {
verifyFirstLaunch();
} else if (SOLO.waitForText(
// 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.
if (!isEmulator) {
GoToMyLocationTest.findAndClickMyLocation(activityMyTracks);
hasGpsSingal = !SOLO.waitForText(NO_GPS_MESSAGE_PREFIX, 1,
SHORT_WAIT_TIME);
hasGpsSingal = !SOLO.waitForText(NO_GPS_MESSAGE_PREFIX, 1, SHORT_WAIT_TIME);
SOLO.goBack();
}
} else if (SOLO.waitForText(
@@ -330,7 +327,6 @@ public class EndToEndTestUtils {
instrumentation.waitForIdleSync();
}
/**
* Rotates the given activity.
*
@@ -380,7 +376,8 @@ public class EndToEndTestUtils {
/**
* 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) {
startRecording();
@@ -391,7 +388,6 @@ public class EndToEndTestUtils {
stopRecording(true);
}
/**
* 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.
@@ -432,7 +428,7 @@ public class EndToEndTestUtils {
*/
public static void startRecording() {
View startButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record);
if(startButton != null && startButton.isShown()) {
if (startButton != null && startButton.isShown()) {
SOLO.clickOnView(startButton);
}
instrumentation.waitForIdleSync();
@@ -443,7 +439,7 @@ public class EndToEndTestUtils {
*/
public static void pauseRecording() {
View pauseButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record);
if(pauseButton != null && pauseButton.isShown()) {
if (pauseButton != null && pauseButton.isShown()) {
SOLO.clickOnView(pauseButton);
}
instrumentation.waitForIdleSync();
@@ -454,7 +450,7 @@ public class EndToEndTestUtils {
*/
public static void resumeRecording() {
View startButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_record);
if(startButton != null && startButton.isShown()) {
if (startButton != null && startButton.isShown()) {
SOLO.clickOnView(startButton);
}
instrumentation.waitForIdleSync();
@@ -477,14 +473,13 @@ public class EndToEndTestUtils {
*/
public static void stopRecording(boolean isSave) {
View stopButton = SOLO.getCurrentActivity().findViewById(R.id.track_controller_stop);
if(stopButton != null && stopButton.isShown() ) {
if (stopButton != null && stopButton.isShown()) {
SOLO.clickOnView(stopButton);
if (isSave) {
SOLO.waitForText(activityMytracks.getString(R.string.generic_save), 1, 5000);
// Make every track name is unique to make sure every check can be
// trusted.
trackName = TRACK_NAME_PREFIX
+ System.currentTimeMillis();
trackName = TRACK_NAME_PREFIX + System.currentTimeMillis();
SOLO.sendKey(KeyEvent.KEYCODE_DEL);
enterTextAvoidSoftKeyBoard(0, trackName);
enterTextAvoidSoftKeyBoard(1, activityType);
@@ -548,7 +543,7 @@ public class EndToEndTestUtils {
findMenuItem(activityMytracks.getString(R.string.menu_save), true);
instrumentation.waitForIdleSync();
SOLO.clickOnText(trackKind.toUpperCase());
// rotateAllActivities();
// rotateAllActivities();
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
* @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;
instrumentation.waitForIdleSync();
@@ -573,9 +568,9 @@ public class EndToEndTestUtils {
ArrayList<View> allViews = SOLO.getViews();
for (View view : allViews) {
String className = view.getClass().getName();
if(className.indexOf("ActionMenuItemView") > 0) {
if (className.indexOf("ActionMenuItemView") > 0) {
String menuItemNameString = view.getContentDescription().toString();
if(menuItemNameString.equalsIgnoreCase(buttonName)) {
if (menuItemNameString.equalsIgnoreCase(buttonName)) {
button = view;
break;
}
@@ -584,7 +579,7 @@ public class EndToEndTestUtils {
}
// Get all buttons and find.
if(button == null) {
if (button == null) {
ArrayList<Button> currentButtons = SOLO.getCurrentButtons();
for (Button oneButton : currentButtons) {
String title = (String) oneButton.getText();
@@ -594,7 +589,7 @@ public class EndToEndTestUtils {
}
}
if(button != null && isClick) {
if (button != null && isClick) {
SOLO.clickOnView(button);
}
@@ -613,7 +608,7 @@ public class EndToEndTestUtils {
private static boolean setHasActionBar() {
try {
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.
return false;
}
@@ -737,6 +732,7 @@ public class EndToEndTestUtils {
* <li>The view extends ImageButton.</li>
* <li>The view name equals {@link #MOREOPTION_CLASSNAME}.</li>
* </ul>
*
* @return the more option view. Null means can not find it.
*/
private static View getMoreOptionView() {
@@ -867,7 +863,8 @@ public class EndToEndTestUtils {
* @param text to enter
*/
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);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
SOLO.enterText(editText, text);
@@ -897,7 +894,8 @@ public class EndToEndTestUtils {
.isEnabled());
Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_recording)));
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());
}
@@ -933,15 +931,14 @@ public class EndToEndTestUtils {
Assert.assertNull(findTextView(activityMytracks.getString(R.string.generic_recording)));
Assert.assertNotNull(findTextView(activityMytracks.getString(R.string.generic_paused)));
String totalTimeOld = ((TextView) SOLO.getCurrentActivity()
.findViewById(R.id.track_controller_total_time)).getText().toString();
String totalTimeOld = ((TextView) SOLO.getCurrentActivity().findViewById(
R.id.track_controller_total_time)).getText().toString();
sleep(2000);
String totalTimeNew = ((TextView) SOLO.getCurrentActivity()
.findViewById(R.id.track_controller_total_time)).getText().toString();
String totalTimeNew = ((TextView) SOLO.getCurrentActivity().findViewById(
R.id.track_controller_total_time)).getText().toString();
Assert.assertTrue(totalTimeOld.equalsIgnoreCase(totalTimeNew));
}
/**
* Creates a way point during track recording.
*
@@ -10,6 +10,8 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import java.io.File;
import java.io.FileOutputStream;
@@ -122,8 +124,25 @@ public class BigTestUtils {
String oneInfo = (new java.util.Date()).toString() + ":" + memoryUsageString + ", "
+ batteryUsageString + ".\r\n";
writeToFile(oneInfo, true);
Log.i(EndToEndTestUtils.LOG_TAG, oneInfo);
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 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 INTERVALE_TO_CHECK = 5 * 60 * 1000;
@@ -61,9 +61,11 @@ public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackLis
instrumentation = getInstrumentation();
trackListActivity = getActivity();
context = trackListActivity.getApplicationContext();
instrumentation.waitForIdleSync();
EndToEndTestUtils.setupForDebug(instrumentation, trackListActivity);
BigTestUtils.unlockDevice();
if (!isUserConfirmed) {
userConfirmTest();
confirmTest();
}
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.
*/
private void userConfirmTest() {
private void confirmTest() {
try {
trackListActivity.runOnUiThread(new Runnable() {
@Override
@@ -110,54 +112,51 @@ public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackLis
* Tests the resource usage when display track list activity during recording.
*/
public void testBatteryUsage_showTrackList() {
if (!runTest) {
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);
recordingLongTrack(true, "");
}
/**
* Tests the resource usage when display map view during recording.
*/
public void testBatteryUsage_showMapView() {
if (!runTest) {
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);
recordingLongTrack(false, trackListActivity.getString(R.string.track_detail_map_tab));
}
/**
* Tests the resource usage when display chart view during recording.
*/
public void testBatteryUsage_showChartView() {
if (!runTest) {
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);
recordingLongTrack(false, trackListActivity.getString(R.string.track_detail_chart_tab));
}
/**
* Tests the resource usage when display stats view during recording.
*/
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) {
Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE);
}
EndToEndTestUtils.startRecording();
EndToEndTestUtils.SOLO
.clickOnText(trackListActivity.getString(R.string.track_detail_stats_tab));
if (isShowTracksList) {
EndToEndTestUtils.SOLO.goBack();
} else {
EndToEndTestUtils.SOLO.clickOnText(tabName);
}
BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS);
BigTestUtils.unlockDevice();
EndToEndTestUtils.stopRecording(true);
}