summaryrefslogtreecommitdiffstats
path: root/js/src/tests/lib/remote.py
blob: a98fd052f4839758d27161a30f3b8b7cb82d7e05 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


# remote.py -- Python library for Android devices.

import os
import posixpath


def push_libs(options, device, dest_dir):
    # This saves considerable time in pushing unnecessary libraries
    # to the device but needs to be updated if the dependencies change.
    required_libs = [
        "libnss3.so",
        "libmozglue.so",
        "libnspr4.so",
        "libplc4.so",
        "libplds4.so",
    ]

    for file in os.listdir(options.local_lib):
        if file in required_libs:
            local_file = os.path.join(options.local_lib, file)
            remote_file = posixpath.join(dest_dir, file)
            assert os.path.isfile(local_file)
            device.push(local_file, remote_file)
            device.chmod(remote_file)


def push_progs(options, device, progs, dest_dir):
    assert isinstance(progs, list)
    for local_file in progs:
        remote_file = posixpath.join(dest_dir, os.path.basename(local_file))
        assert os.path.isfile(local_file)
        device.push(local_file, remote_file)
        device.chmod(remote_file)


def init_remote_dir(device, path):
    device.rm(path, recursive=True, force=True)
    device.mkdir(path, parents=True)


# We only have one device per test run.
DEVICE = None


def init_device(options):
    # Initialize the device
    global DEVICE

    assert options.remote and options.js_shell

    if DEVICE is not None:
        return DEVICE

    from mozdevice import ADBDeviceFactory, ADBError, ADBTimeoutError

    try:
        if not options.local_lib:
            # if not specified, use the local directory containing
            # the js binary to find the necessary libraries.
            options.local_lib = posixpath.dirname(options.js_shell)

        DEVICE = ADBDeviceFactory(
            device=options.device_serial, test_root=options.remote_test_root
        )

        init_remote_dir(DEVICE, options.remote_test_root)

        bin_dir = posixpath.join(options.remote_test_root, "bin")
        tests_dir = posixpath.join(options.remote_test_root, "tests")
        temp_dir = posixpath.join(options.remote_test_root, "tmp")
        # Push js shell and libraries.
        init_remote_dir(DEVICE, tests_dir)
        init_remote_dir(DEVICE, bin_dir)
        init_remote_dir(DEVICE, temp_dir)
        push_libs(options, DEVICE, bin_dir)
        push_progs(options, DEVICE, [options.js_shell], bin_dir)
        # update options.js_shell to point to the js binary on the device
        options.js_shell = os.path.join(bin_dir, "js")

        return DEVICE

    except (ADBError, ADBTimeoutError):
        print("TEST-UNEXPECTED-FAIL | remote.py : Device initialization failed")
        raise