Make test can run on Android 4.1. Initialize the status of GPS signal and then use it during test. Create a common method to enter text which can hides soft keyboard.

This commit is contained in:
youtaol
2012-07-27 16:51:18 +08:00
parent b6fd63a512
commit b052c34e76
5 changed files with 95 additions and 74 deletions
@@ -23,7 +23,6 @@ import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.EditText;
import java.util.ArrayList; import java.util.ArrayList;
@@ -166,18 +165,10 @@ public class CreateAndSendTrackTest extends ActivityInstrumentationTestCase2<Tra
EndToEndTestUtils.rotateAllActivities(); EndToEndTestUtils.rotateAllActivities();
EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.generic_save)); EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.generic_save));
sendKeys(KeyEvent.KEYCODE_DEL); sendKeys(KeyEvent.KEYCODE_DEL);
ArrayList<EditText> editTexts = EndToEndTestUtils.SOLO.getCurrentEditTexts();
EndToEndTestUtils.enterTextAvoidSoftKeyBoard(0, newTrackName);
EndToEndTestUtils.SOLO.enterText(editTexts.get(0), newTrackName); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(1, newType);
EndToEndTestUtils.SOLO.enterText(editTexts.get(1), newType); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(2, newDesc);
// In landscape, there are only two visible edit texts.
if (editTexts.size() > 2) {
EndToEndTestUtils.SOLO.enterText(editTexts.get(2), newDesc);
} else {
EndToEndTestUtils.SOLO.scrollDown();
editTexts = EndToEndTestUtils.SOLO.getCurrentEditTexts();
EndToEndTestUtils.SOLO.enterText(editTexts.get(editTexts.size() - 1), newDesc);
}
EndToEndTestUtils.SOLO.clickOnButton(activityMyTracks.getString(R.string.generic_save)); EndToEndTestUtils.SOLO.clickOnButton(activityMyTracks.getString(R.string.generic_save));
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
// Go back to track list. // Go back to track list.
@@ -289,7 +280,11 @@ public class CreateAndSendTrackTest extends ActivityInstrumentationTestCase2<Tra
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_markers), true); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_markers), true);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
assertTrue(EndToEndTestUtils.SOLO.getCurrentListViews().get(0).getCount() > 0); if (EndToEndTestUtils.hasGPSSingal) {
assertTrue(EndToEndTestUtils.SOLO.getCurrentListViews().get(0).getCount() > 0);
} else {
assertTrue(EndToEndTestUtils.SOLO.getCurrentListViews().get(0).getCount() == 0);
}
EndToEndTestUtils.SOLO.goBack(); EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.stopRecording(true); EndToEndTestUtils.stopRecording(true);
@@ -304,11 +299,12 @@ public class CreateAndSendTrackTest extends ActivityInstrumentationTestCase2<Tra
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_insert_marker), true); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_insert_marker), true);
// Rotate when show insert page. // Rotate when show insert page.
EndToEndTestUtils.rotateAllActivities(); EndToEndTestUtils.rotateAllActivities();
EndToEndTestUtils.SOLO.enterText(0, WAYPOINT_NAME); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(0, WAYPOINT_NAME);
EndToEndTestUtils.SOLO.clickOnButton(activityMyTracks.getString(R.string.generic_add)); EndToEndTestUtils.SOLO.clickOnButton(activityMyTracks.getString(R.string.generic_add));
if (!EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.marker_add_error), if (EndToEndTestUtils.hasGPSSingal) {
1, EndToEndTestUtils.SHORT_WAIT_TIME)) {
assertTrue(EndToEndTestUtils.SOLO.searchText(WAYPOINT_NAME)); assertTrue(EndToEndTestUtils.SOLO.searchText(WAYPOINT_NAME));
} else {
assertFalse(EndToEndTestUtils.SOLO.searchText(WAYPOINT_NAME));
} }
EndToEndTestUtils.SOLO.goBack(); EndToEndTestUtils.SOLO.goBack();
@@ -61,6 +61,7 @@ public class EndToEndTestUtils {
private static final float START_LATITUDE = -1.3f; private static final float START_LATITUDE = -1.3f;
private static final float DELTA_LONGITUDE = 0.0005f; private static final float DELTA_LONGITUDE = 0.0005f;
private static final float DELTA_LADITUDE = 0.0005f; private static final float DELTA_LADITUDE = 0.0005f;
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";
@@ -124,6 +125,7 @@ public class EndToEndTestUtils {
// Android OS. // Android OS.
static boolean hasActionBar = false; static boolean hasActionBar = false;
static boolean isEmulator = true; static boolean isEmulator = true;
static boolean hasGPSSingal = true;
static boolean isCheckedFirstLaunch = false; static boolean isCheckedFirstLaunch = false;
private EndToEndTestUtils() {} private EndToEndTestUtils() {}
@@ -201,10 +203,20 @@ public class EndToEndTestUtils {
*/ */
static void setupForAllTest(Instrumentation instrumentation, TrackListActivity activityMyTracks) { static void setupForAllTest(Instrumentation instrumentation, TrackListActivity activityMyTracks) {
setIsEmulator(); setIsEmulator();
EndToEndTestUtils.instrumentation = instrumentation; EndToEndTestUtils.instrumentation = instrumentation;
EndToEndTestUtils.activityMytracks = activityMyTracks; EndToEndTestUtils.activityMytracks = activityMyTracks;
SOLO = new Solo(EndToEndTestUtils.instrumentation, SOLO = new Solo(EndToEndTestUtils.instrumentation,
EndToEndTestUtils.activityMytracks); EndToEndTestUtils.activityMytracks);
// For emulator, we would fix GPS signal.
if(!isEmulator) {
GoToMyLocationTest.findAndClickMyLocation(activityMyTracks);
hasGPSSingal = !SOLO.waitForText(NO_GPS_MESSAGE_PREFIX, 1,
EndToEndTestUtils.SHORT_WAIT_TIME);
SOLO.goBack();
}
// Check if open MyTracks first time after install. If so, there would be a // Check if open MyTracks first time after install. If so, there would be a
// welcome view with accept buttons. And makes sure only check once. // welcome view with accept buttons. And makes sure only check once.
if (!isCheckedFirstLaunch) { if (!isCheckedFirstLaunch) {
@@ -216,9 +228,9 @@ public class EndToEndTestUtils {
activityMytracks.getString(R.string.welcome_title), 0, SHORT_WAIT_TIME)) { activityMytracks.getString(R.string.welcome_title), 0, SHORT_WAIT_TIME)) {
resetPreferredUnits(); resetPreferredUnits();
} }
hasActionBar = setHasActionBar();
checkLanguage(); checkLanguage();
isCheckedFirstLaunch = true; isCheckedFirstLaunch = true;
setHasActionBar();
deleteAllTracks(); deleteAllTracks();
resetAllSettings(activityMyTracks, false); resetAllSettings(activityMyTracks, false);
} else if (SOLO.waitForText( } else if (SOLO.waitForText(
@@ -320,6 +332,7 @@ public class EndToEndTestUtils {
if (showTrackList) { if (showTrackList) {
SOLO.goBack(); SOLO.goBack();
} }
instrumentation.waitForIdleSync();
} }
} }
@@ -328,7 +341,7 @@ public class EndToEndTestUtils {
*/ */
static void startRecording() { static void startRecording() {
if (hasActionBar) { if (hasActionBar) {
Button startButton = getButtonOnScreen(activityMytracks.getString(R.string.menu_record_track), false, false); View startButton = getButtonOnScreen(activityMytracks.getString(R.string.menu_record_track), false, false);
// In case a track is recording. // In case a track is recording.
if (startButton == null) { if (startButton == null) {
stopRecording(true); stopRecording(true);
@@ -420,9 +433,8 @@ public class EndToEndTestUtils {
* is shown. * is shown.
*/ */
static void deleteAllTracks() { static void deleteAllTracks() {
EndToEndTestUtils.findMenuItem(activityMytracks.getString(R.string.menu_delete_all), true); findMenuItem(activityMytracks.getString(R.string.menu_delete_all), true);
EndToEndTestUtils getButtonOnScreen(activityMytracks.getString(R.string.generic_ok), true, true);
.getButtonOnScreen(activityMytracks.getString(R.string.generic_ok), true, true);
} }
/** /**
@@ -465,22 +477,41 @@ 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
*/ */
static Button getButtonOnScreen(String buttonName, boolean isWait,boolean isClick) { static View getButtonOnScreen(String buttonName, boolean isWait,boolean isClick) {
View button = null;
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
if (isWait) { if (isWait) {
SOLO.waitForText(buttonName); SOLO.waitForText(buttonName);
} }
ArrayList<Button> currentButtons = SOLO.getCurrentButtons();
for (Button button : currentButtons) { if (hasActionBar) {
String title = (String) button.getText(); ArrayList<View> allViews = SOLO.getViews();
if (title.equalsIgnoreCase(buttonName)) { for (View view : allViews) {
if(isClick) { String className = view.getClass().getName();
SOLO.clickOnView(button); if(className.indexOf("ActionMenuItemView") > 0) {
String menuItemNameString = view.getContentDescription().toString();
if(menuItemNameString.equalsIgnoreCase(buttonName)) {
button = view;
break;
}
} }
return button;
} }
} }
return null;
ArrayList<Button> currentButtons = SOLO.getCurrentButtons();
for (Button oneButton : currentButtons) {
String title = (String) oneButton.getText();
if (title.equalsIgnoreCase(buttonName)) {
button = oneButton;
}
}
if(isClick) {
SOLO.clickOnView(button);
}
return button;
} }
/** /**
@@ -489,24 +520,12 @@ public class EndToEndTestUtils {
* @return false means can not check failed. * @return false means can not check failed.
*/ */
static boolean setHasActionBar() { static boolean setHasActionBar() {
instrumentation.waitForIdleSync(); try {
// If can find record button without pressing Menu, it should be an action return activityMytracks.getActionBar() == null ? false : true;
// bar. }catch (Throwable e) {
Button startButton = getButtonOnScreen(activityMytracks.getString(R.string.menu_record_track), false, false); // For in Android which does not has action bar, here will meet a error.
Button stopButton = getButtonOnScreen(activityMytracks.getString(R.string.menu_stop_recording), false, false); return false;
if (startButton != null || stopButton != null) {
hasActionBar = true;
} else {
showMenuItem();
if (SOLO.searchText(activityMytracks.getString(R.string.menu_record_track))
|| SOLO.searchText(activityMytracks.getString(R.string.menu_stop_recording))) {
hasActionBar = false;
} else {
return false;
}
SOLO.goBack();
} }
return true;
} }
/** /**
@@ -543,7 +562,7 @@ public class EndToEndTestUtils {
// ICS phone. // ICS phone.
if(hasActionBar) { if(hasActionBar) {
// Firstly find in action bar. // Firstly find in action bar.
Button button = getButtonOnScreen(menuName, false, false); View button = getButtonOnScreen(menuName, false, false);
if (button != null) { if (button != null) {
findResult = true; findResult = true;
if (click) { if (click) {
@@ -689,13 +708,17 @@ public class EndToEndTestUtils {
} }
/** /**
* Hides soft key board when input text in an exit text. * Finds a edit text and enter text in it. This method can hides soft key
* @param myEditText * board when input text.
* @param context *
* @param editTextIndex the index of edit text
* @param text to enter
*/ */
public static void hideSoftKeyBoard(EditText myEditText, Context context) { public static void enterTextAvoidSoftKeyBoard(int editTextIndex, String text) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) activityMytracks.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); EditText editText = SOLO.getEditText(editTextIndex);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
SOLO.enterText(editText, text);
} }
} }
@@ -18,7 +18,7 @@ package com.google.android.apps.mytracks.endtoendtest;
import com.google.android.apps.mytracks.TrackListActivity; import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.annotation.TargetApi; import android.app.Activity;
import android.app.Instrumentation; import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
import android.view.View; import android.view.View;
@@ -36,7 +36,6 @@ public class GoToMyLocationTest extends ActivityInstrumentationTestCase2<TrackLi
private Instrumentation instrumentation; private Instrumentation instrumentation;
private TrackListActivity activityMyTracks; private TrackListActivity activityMyTracks;
@TargetApi(8)
public GoToMyLocationTest() { public GoToMyLocationTest() {
super(TrackListActivity.class); super(TrackListActivity.class);
} }
@@ -53,9 +52,25 @@ public class GoToMyLocationTest extends ActivityInstrumentationTestCase2<TrackLi
* Tests the menu My Location. * Tests the menu My Location.
*/ */
public void testGotoMyLocation() { public void testGotoMyLocation() {
findAndClickMyLocation(activityMyTracks);
if (EndToEndTestUtils.isEmulator) {
EndToEndTestUtils.SOLO.waitForText(
activityMyTracks.getString(R.string.my_location_no_location), 1,
EndToEndTestUtils.SHORT_WAIT_TIME);
} else {
// TODO How to verify the location is shown on the map.
}
}
/**
* Finds the My Location view and click it.
*
* @param activity
*/
public static void findAndClickMyLocation(Activity activity) {
EndToEndTestUtils.createTrackIfEmpty(1, false); EndToEndTestUtils.createTrackIfEmpty(1, false);
EndToEndTestUtils.sendGps(30); EndToEndTestUtils.sendGps(30);
instrumentation.waitForIdleSync();
View myLocation = EndToEndTestUtils.SOLO.getCurrentActivity() View myLocation = EndToEndTestUtils.SOLO.getCurrentActivity()
.findViewById(R.id.map_my_location); .findViewById(R.id.map_my_location);
// Find the My Location button in another if null. // Find the My Location button in another if null.
@@ -64,20 +79,13 @@ public class GoToMyLocationTest extends ActivityInstrumentationTestCase2<TrackLi
for (ImageButton imageButton : aa) { for (ImageButton imageButton : aa) {
if (imageButton.getContentDescription() != null if (imageButton.getContentDescription() != null
&& imageButton.getContentDescription().equals( && imageButton.getContentDescription().equals(
activityMyTracks.getString(R.string.icon_my_location))) { activity.getString(R.string.icon_my_location))) {
myLocation = imageButton; myLocation = imageButton;
break; break;
} }
} }
} }
EndToEndTestUtils.SOLO.clickOnView(myLocation); EndToEndTestUtils.SOLO.clickOnView(myLocation);
if (EndToEndTestUtils.isEmulator) {
EndToEndTestUtils.SOLO.waitForText(
activityMyTracks.getString(R.string.my_location_no_location), 1,
EndToEndTestUtils.SHORT_WAIT_TIME);
} else {
// TODO How to verify the location is shown on the map.
}
} }
@Override @Override
@@ -22,9 +22,6 @@ import android.annotation.TargetApi;
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 android.widget.EditText;
import java.util.ArrayList;
/** /**
* Tests some menu items of MyTracks. * Tests some menu items of MyTracks.
@@ -88,8 +85,7 @@ public class MenuItemsTest extends ActivityInstrumentationTestCase2<TrackListAct
EndToEndTestUtils.createSimpleTrack(1); EndToEndTestUtils.createSimpleTrack(1);
EndToEndTestUtils.SOLO.goBack(); EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_search), true); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_search), true);
ArrayList<EditText> editTexts = EndToEndTestUtils.SOLO.getCurrentEditTexts(); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(0, EndToEndTestUtils.trackName);
EndToEndTestUtils.SOLO.enterText(editTexts.get(0), EndToEndTestUtils.trackName);
sendKeys(KeyEvent.KEYCODE_ENTER); sendKeys(KeyEvent.KEYCODE_ENTER);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
assertEquals(1, EndToEndTestUtils.SOLO.getCurrentListViews().size()); assertEquals(1, EndToEndTestUtils.SOLO.getCurrentListViews().size());
@@ -280,9 +280,7 @@ public class SettingTest extends ActivityInstrumentationTestCase2<TrackListActiv
// Changes the setting of default activity. // Changes the setting of default activity.
EndToEndTestUtils.SOLO.clickOnText(activityMyTracks EndToEndTestUtils.SOLO.clickOnText(activityMyTracks
.getString(R.string.settings_recording_default_activity)); .getString(R.string.settings_recording_default_activity));
EndToEndTestUtils.hideSoftKeyBoard(EndToEndTestUtils.SOLO.getEditText(0), EndToEndTestUtils.enterTextAvoidSoftKeyBoard(0, EndToEndTestUtils.DEFAULTACTIVITY);
activityMyTracks.getApplicationContext());
EndToEndTestUtils.SOLO.enterText(0, EndToEndTestUtils.DEFAULTACTIVITY);
EndToEndTestUtils EndToEndTestUtils
.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true); .getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true);