summaryrefslogtreecommitdiff
path: root/automated/lib/android-test-lib
blob: b66d0da2ba6c01e5b8a585f1638a7eea6e34c342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/sh

initialize_adb() {
    adb devices
    if [ -z "${SN}" ]; then
        number="$(adb devices | grep -wc 'device')"
        if [ "${number}" -gt 1 ]; then
            warn_msg "Device not specified; define SN or use '-s'"
            error_msg "More than one device or emulator found"
        elif [ "${number}" -eq 1 ]; then
            SN="$(adb get-serialno)"
            export SN
        else
            error_msg "Device NOT found"
        fi
    fi

    if adb -s "${SN}" shell echo "Testing adb connectivity"; then
        info_msg "Connected to device ${SN} successfully"
    else
        error_msg "Unable to connect to device ${SN}"
    fi
}

wait_boot_completed() {
    timeout="$1"
    [ "$#" -ne 1 ] && error_msg "Usage: wait_for_boot_completed timeout_in_seconds"
    end=$(( $(date +%s) + timeout ))

    boot_completed=false
    while [ "$(date +%s)" -lt "$end" ]; do
        if adb -s "${SN}" shell getprop sys.boot_completed | grep "1"; then
            boot_completed=true
            break
        else
            sleep 3
        fi
    done

    if "${boot_completed}"; then
        info_msg "Target booted up completely."
    else
        error_msg "wait_boot_completed timed out after ${timeout} seconds"
    fi
}

wait_homescreen() {
    timeout="$1"
    [ "$#" -ne 1 ] && error_msg "Usage: wait_homescreen timeout_in_seconds"
    end=$(( $(date +%s) + timeout ))

    homescreen_displayed=false
    while [ "$(date +%s)" -lt "$end" ]; do
        if adb -s "${SN}" logcat -sd ActivityManager:I | grep "Displayed com.android.launcher"; then
            homescreen_displayed=true
            break
        else
            sleep 3
        fi
    done

    if "${homescreen_displayed}"; then
        info_msg "Target booted to homescreen successfully."
    else
        error_msg "wait_homescreen timed out after ${timeout} seconds"
    fi
}

detect_abi() {
    # "| tr -d '\r'" is needed here, refer to the below issue.
    # https://code.google.com/p/android/issues/detail?id=2482
    abi="$(adb -s "${SN}" shell uname -m | tr -d '\r')"
    case $abi in
      armv7|armv7l|armv7el|armv7lh) abi="armeabi" ;;
      arm64|armv8|arm64-v8a|aarch64) abi="arm64" ;;
      *) error_msg "Unknown architecture" ;;
    esac
    info_msg "ABI: ${abi}"
}

install() {
    file_path="$1"
    file_name="$(basename "${file_path}")"

    if adb -s "${SN}" shell mount | grep system | grep -q ro; then
        # Remounts the /system partition on the device read-write
        info_msg "/system partition is read-only, remounting it read-write..."
        adb -s "${SN}" remount
    fi

    info_msg "Installing ${file_name}"
    adb -s "${SN}" push "${file_path}"  "/system/bin/"
    adb -s "${SN}" shell chmod 755  "/system/bin/${file_name}"
}

pull_output() {
    device_output="$1"
    host_output="$2"

    info_msg "Pulling output from devcie ${SN}"
    adb -s "${SN}" pull "${device_output}" "${host_output}"
}