From 47ba1cbf4b6ae52661b80bf605703eb29f607f91 Mon Sep 17 00:00:00 2001 From: youtaol Date: Mon, 23 Jul 2012 17:56:54 +0800 Subject: [PATCH] Change some code to make test stable. Add a method in EndToEndTestUtils to hide soft keyboard when input text in edit text. Add method deleteAllTracks() in EndToEndTestUtils. Add 4 fields for different wait time in EndToEndTestUtils. Change method showMoreMenuItem to showMenuItem in EndToEndTestUtils. --- .../endtoendtest/CreateAndSendTrackTest.java | 55 ++++---- .../endtoendtest/EndToEndTestUtils.java | 132 +++++++++++++----- .../mytracks/endtoendtest/SettingTest.java | 12 +- 3 files changed, 131 insertions(+), 68 deletions(-) diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/CreateAndSendTrackTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/CreateAndSendTrackTest.java index 427499adc..e2146c527 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/CreateAndSendTrackTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/CreateAndSendTrackTest.java @@ -113,33 +113,38 @@ public class CreateAndSendTrackTest extends ActivityInstrumentationTestCase2 allActivities = SOLO.getAllOpenedActivities(); - for (Activity activity : allActivities) { - rotateActivity(activity); + if (hasActionBar) { + ArrayList allActivities = SOLO.getAllOpenedActivities(); + for (Activity activity : allActivities) { + rotateActivity(activity); + } } - instrumentation.waitForIdleSync(); } @@ -517,9 +535,11 @@ public class EndToEndTestUtils { * @param click true means need click this menu * @return true if find this menu */ - static boolean findMenuItem(String menuName, boolean click) { - boolean findResult = false; - + static boolean findMenuItem(String menuName, boolean click) {boolean findResult = false; + boolean isMoreMenuOpened = false; + + // ICS phone. + if(hasActionBar) { // Firstly find in action bar. Button button = getButtonOnScreen(menuName, false, false); if (button != null) { @@ -530,36 +550,72 @@ public class EndToEndTestUtils { } return findResult; } - - showMoreMenuItem(); + showMenuItem(); + findResult = SOLO.searchText(menuName); + } else { + // Non-ICS phone. + SOLO.sendKey(KeyEvent.KEYCODE_MENU); if (SOLO.searchText(menuName)) { findResult = true; } else if (SOLO.searchText(MENU_MORE)) { SOLO.clickOnText(MENU_MORE); findResult = SOLO.searchText(menuName); + isMoreMenuOpened = true; } - - if (findResult && click) { - SOLO.clickOnText(menuName); - instrumentation.waitForIdleSync(); - } else { + } + + if (findResult && click) { + SOLO.clickOnText(menuName); + instrumentation.waitForIdleSync(); + } else { + // Quit more menu list if opened. + if (isMoreMenuOpened) { SOLO.goBack(); } - return findResult; + // Quit menu list. + SOLO.goBack(); } + return findResult; +} /** - * Gets more menu items operation is different for different Android OS. + * Show menu item list. */ - public static void showMoreMenuItem() { + public static void showMenuItem() { + showMenuItem(0); + } + + /** + * 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 + * some situation, will meet an error when click it. So catch it and try + * again. In most situation, the second click will be pass. + * + * @param depth control the depth of recursion to prevent dead circulation + */ + private static void showMenuItem(int depth) { + // ICS phone. if (hasActionBar) { + instrumentation.waitForIdleSync(); View moreButton = getMoreOptionView(); + // ICS phone without menu key. if (moreButton != null) { - SOLO.clickOnView(moreButton); + try { + SOLO.clickOnView(moreButton); + } catch (Throwable e) { + if (depth < 5 && e.getMessage().indexOf("Click can not be completed") > -1) { + showMenuItem(depth++); + } + } return; - } + } else { + // ICS phone with menu key. + SOLO.sendKey(KeyEvent.KEYCODE_MENU); + } + } else { + // Non-ICS phone with menu key. + SOLO.sendKey(KeyEvent.KEYCODE_MENU); } - SOLO.sendKey(KeyEvent.KEYCODE_MENU); } /** @@ -629,5 +685,15 @@ public class EndToEndTestUtils { SOLO.goBack(); } } + + /** + * Hides soft key board when input text in an exit text. + * @param myEditText + * @param context + */ + public static void hideSoftKeyBoard(EditText myEditText, Context context) { + InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(myEditText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); + } } \ No newline at end of file diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java index 5c66cdf49..0e88e3de1 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/endtoendtest/SettingTest.java @@ -280,13 +280,12 @@ public class SettingTest extends ActivityInstrumentationTestCase2