test-android-resource-usage.sh (4988B)
1 #!/bin/bash 2 # This Source Code Form is subject to the terms of the Mozilla Public 3 # License, v. 2.0. If a copy of the MPL was not distributed with this 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 ACTIVITY="org.mozilla.fenix.HomeActivity" 6 TOOLBAR_BOUNDS_ID="toolbar" 7 TABS_TRAY_BUTTON_BOUNDS_ID="counter_box" 8 9 XML_FILE=$TESTING_DIR/window_dump.xml 10 XMLSTARLET_CMD=${XMLSTARLET:-xmlstarlet} 11 TEST_TIME=$1 12 13 URL_MOZILLA="https://www.mozilla.org/" 14 15 if [[ $BROWSER_BINARY == *"chrome"* ]]; then 16 ACTIVITY="com.google.android.apps.chrome.Main" 17 TOOLBAR_BOUNDS_ID="search_box_text" 18 TABS_TRAY_BUTTON_BOUNDS_ID="tab_switcher_button" 19 fi 20 21 TAP_X=0 22 TAP_Y=0 23 24 calculate_tap_coords() { 25 x1=$(($(echo "$1" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}'))) 26 x2=$(($(echo "$1" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}'))) 27 sum_x=$(($x1+$x2)) 28 29 y1=$(($(echo "$1" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}'))) 30 y2=$(($(echo "$1" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}'))) 31 sum_y=$(($y1+$y2)) 32 33 TAP_X=$(($sum_x/2)) 34 TAP_Y=$(($sum_y/2)) 35 } 36 37 tap_at_coords(){ 38 adb shell input tap $TAP_X $TAP_Y 39 } 40 41 adb shell pm clear $BROWSER_BINARY 42 adb shell am start -n "$BROWSER_BINARY/$ACTIVITY" 43 sleep 4 44 45 if [[ $BROWSER_BINARY == *"chrome"* ]]; then 46 # navigate away from the first run prompt 47 adb shell uiautomator dump 48 adb pull /sdcard/window_dump.xml $XML_FILE 49 sleep 1 50 51 DISMISS_BOUNDS=$($XMLSTARLET_CMD sel -t -v 'string(//node[@resource-id = "'$BROWSER_BINARY':id/signin_fre_dismiss_button"]/@bounds)' "$XML_FILE") 52 sleep 1 53 54 calculate_tap_coords $DISMISS_BOUNDS 55 tap_at_coords 56 sleep 2 57 58 # navigate away from privacy notice 59 adb shell uiautomator dump 60 adb pull /sdcard/window_dump.xml $XML_FILE 61 sleep 1 62 63 DISMISS_BOUNDS=$($XMLSTARLET_CMD sel -t -v 'string(//node[@resource-id = "'$BROWSER_BINARY':id/ack_button"]/@bounds)' "$XML_FILE") 64 sleep 1 65 66 calculate_tap_coords $DISMISS_BOUNDS 67 tap_at_coords 68 sleep 1 69 fi 70 71 adb shell uiautomator dump 72 adb pull /sdcard/window_dump.xml $XML_FILE 73 sleep 1 74 75 # calculate toolbar coordinates 76 TOOLBAR_BOUNDS=$($XMLSTARLET_CMD sel -t -v '//node[@resource-id = "'$BROWSER_BINARY':id/'$TOOLBAR_BOUNDS_ID'"]/@bounds' $XML_FILE) 77 sleep 1 78 79 calculate_tap_coords $TOOLBAR_BOUNDS 80 TOOLBAR_X_COORDINATE=$TAP_X 81 TOOLBAR_Y_COORDINATE=$TAP_Y 82 83 # calculate tabs tray coordinates 84 TABS_TRAY_BUTTON_BOUNDS=$($XMLSTARLET_CMD sel -t -v '//node[@resource-id = "'$BROWSER_BINARY':id/'$TABS_TRAY_BUTTON_BOUNDS_ID'"]/@bounds' $XML_FILE) 85 sleep 1 86 87 calculate_tap_coords $TABS_TRAY_BUTTON_BOUNDS 88 TABS_TRAY_BUTTON_X_COORDINATE=$TAP_X 89 TABS_TRAY_BUTTON_Y_COORDINATE=$TAP_Y 90 91 adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE 92 sleep 2 93 94 adb shell uiautomator dump 95 adb pull /sdcard/window_dump.xml $XML_FILE 96 97 # calculate new tab button coordinates 98 if [[ $BROWSER_BINARY == *"chrome"* ]]; then 99 ADD_TAB_BUTTON_BOUNDS=$($XMLSTARLET_CMD sel -t -v '//node[@resource-id="new_tab_view_button"]/@bounds' $XML_FILE) 100 else 101 ADD_TAB_BUTTON_BOUNDS=$($XMLSTARLET_CMD sel -t -v '//node[@content-desc="Add tab"]/@bounds' $XML_FILE) 102 fi 103 sleep 1 104 105 calculate_tap_coords $ADD_TAB_BUTTON_BOUNDS 106 ADD_TAB_BUTTON_X_COORDINATE=$TAP_X 107 ADD_TAB_BUTTON_Y_COORDINATE=$TAP_Y 108 109 rm $XML_FILE 110 111 # go back to main page to start testing 112 adb shell input keyevent KEYCODE_BACK 113 sleep 1 114 115 function tapToFocusToolbar() { 116 adb shell input tap $TOOLBAR_X_COORDINATE $TOOLBAR_Y_COORDINATE 117 sleep 2 118 } 119 120 function inputTextToToolbar() { 121 adb shell input text $1 122 sleep 2 123 } 124 125 function tapEnterAndWait5s() { 126 adb shell input keyevent 66 127 sleep 5 128 } 129 130 function tapEnterAndWait10s() { 131 adb shell input keyevent 66 132 sleep 10 133 } 134 135 function performScrollDown() { 136 adb shell input swipe 500 500 500 300 137 adb shell input swipe 500 500 500 300 138 adb shell input swipe 500 500 500 300 139 sleep 2 140 } 141 142 function performScrollUp() { 143 adb shell input swipe 500 300 500 500 144 adb shell input swipe 500 300 500 500 145 adb shell input swipe 500 300 500 500 146 sleep 2 147 } 148 149 function tapToOpenTabsTray() { 150 adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE 151 sleep 2 152 } 153 154 function tapToAddTab() { 155 adb shell input tap $ADD_TAB_BUTTON_X_COORDINATE $ADD_TAB_BUTTON_Y_COORDINATE 156 sleep 3 157 } 158 159 function addTab() { 160 tapToOpenTabsTray 161 tapToAddTab 162 } 163 164 function surfingSingleSite() { 165 tapToFocusToolbar 166 inputTextToToolbar $1 167 tapEnterAndWait10s 168 performScrollDown 169 performScrollUp 170 } 171 172 function appToBackground() { 173 adb shell input keyevent KEYCODE_HOME 174 sleep 2 175 } 176 177 surfingSingleSite $URL_MOZILLA 178 179 if [ "$RUN_BACKGROUND" = True ]; then 180 appToBackground 181 fi 182 183 # at this point our system is ready, the buttons' coordinates are generated 184 # test starts after this line 185 touch $TESTING_DIR/test_start.signal 186 sleep $(($TEST_TIME+10)) # wait 10 mins in the background 187 touch $TESTING_DIR/test_end.signal 188 adb shell am force-stop $BROWSER_BINARY