1#!/usr/bin/env python
2import mozunit
3from unittest import mock
4import pathlib
5
6from mozperftest.tests.support import get_running_env, EXAMPLE_TEST, temp_file
7from mozperftest.environment import SYSTEM, TEST, METRICS
8from mozperftest.utils import temp_dir
9
10
11HERE = pathlib.Path(__file__).parent
12LOGCAT = HERE / "data" / "logcat"
13
14
15def fetch(self, url):
16    return str(HERE / "fetched_artifact.zip")
17
18
19class FakeDevice:
20    def __init__(self, **args):
21        self.apps = []
22
23    def uninstall_app(self, apk_name):
24        return True
25
26    def install_app(self, apk, replace=True):
27        if apk not in self.apps:
28            self.apps.append(apk)
29
30    def is_app_installed(self, app_name):
31        return True
32
33    def get_logcat(self):
34        with LOGCAT.open() as f:
35            for line in f:
36                yield line
37
38
39@mock.patch("mozperftest.test.browsertime.runner.install_package")
40@mock.patch(
41    "mozperftest.test.noderunner.NodeRunner.verify_node_install", new=lambda x: True
42)
43@mock.patch("mozbuild.artifact_cache.ArtifactCache.fetch", new=fetch)
44@mock.patch(
45    "mozperftest.test.browsertime.runner.BrowsertimeRunner._setup_node_packages",
46    new=lambda x, y: None,
47)
48@mock.patch("mozperftest.system.android.ADBLoggedDevice", new=FakeDevice)
49def test_android_log(*mocked):
50    with temp_file() as logcat, temp_dir() as output:
51        args = {
52            "flavor": "mobile-browser",
53            "android-install-apk": ["this.apk"],
54            "android": True,
55            "console": True,
56            "android-timeout": 30,
57            "android-capture-adb": "stdout",
58            "android-capture-logcat": logcat,
59            "android-app-name": "org.mozilla.fenix",
60            "androidlog": True,
61            "output": output,
62            "browsertime-no-window-recorder": False,
63            "browsertime-viewport-size": "1234x567",
64            "tests": [EXAMPLE_TEST],
65        }
66
67        mach_cmd, metadata, env = get_running_env(**args)
68
69        with env.layers[SYSTEM] as sys, env.layers[TEST] as andro:
70            metadata = andro(sys(metadata))
71
72        # we want to drop the first result
73        metadata._results = metadata._results[1:]
74        with env.layers[METRICS] as metrics:
75            metadata = metrics(metadata)
76
77        assert pathlib.Path(output, "LogCatstd-output.json").exists()
78
79
80if __name__ == "__main__":
81    mozunit.main()
82