1# Copyright 2018 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Rule adapter for android_instrumentation_test."""
15
16load(":adapters/base.bzl", "make_adapter")
17load(":launcher.bzl", "make_launcher")
18load(":launcher_direct.bzl", "make_direct_launcher")
19load(":providers.bzl", "MIAppInfo")
20load(":utils.bzl", "utils")
21load("//rules/flags:flags.bzl", "flags")
22
23def _aspect_attrs():
24    """Attrs of the rule requiring traversal by the aspect."""
25    return ["test_app", "support_apps"]
26
27def _adapt(target, ctx):
28    is_mac = select({
29        "//conditions:default": "no",
30        "@platforms//os:macos": "yes",
31    })
32    if is_mac == "yes":
33        fail("mobile-install does not support running tests on mac, check b/134172473 for more details")
34
35    # TODO(b/): Tests have yet to be optimized so, this is an irrelevant error.
36    # if flags.get(ctx).enable_splits:
37    #     fail("mobile-install does not support running tests for split apks, check b/139762843 for more details! To run tests with mobile-install without splits, pass --define=enable_splits=False")
38
39    launcher = utils.isolated_declare_file(ctx, ctx.label.name + "_mi/launcher")
40
41    test_app = ctx.rule.attr.test_app
42
43    # TODO(manalinandan): Re-enable direct deploy for test.
44    # if _flags.get(ctx).use_direct_deploy:
45    if False:
46        mi_app_launch_info = make_direct_launcher(
47            ctx,
48            test_app[MIAppInfo],
49            launcher,
50            test_args = ctx.rule.attr.args,
51            test_support_apps = ctx.rule.attr.support_apps,
52            use_adb_root = flags.get(ctx).use_adb_root,
53            is_test = True,
54        )
55    else:
56        googplayservices_container_app = None
57        test_support_apps = []
58        for support_app in ctx.rule.attr.support_apps:
59            # Checks if the support_apps is an android_binary rule and 'GoogPlayServices' is present in the label
60            # This implies there is a GoogPlayServices container binary in the dependency
61            if MIAppInfo in support_app and "GoogPlayServices" in str(support_app.label):
62                googplayservices_container_app = support_app
63            elif MIAppInfo in support_app:
64                test_support_apps.append(support_app[MIAppInfo].apk)
65        mi_app_launch_info = make_launcher(
66            ctx,
67            test_app[MIAppInfo],
68            launcher,
69            test_args = ctx.rule.attr.args,
70            test_support_apks = test_support_apps,
71            googplayservices_container_app = googplayservices_container_app,
72            use_adb_root = flags.get(ctx).use_adb_root,
73            is_test = True,
74        )
75    return [OutputGroupInfo(
76        mobile_install_INTERNAL_ = depset(mi_app_launch_info.runfiles).to_list(),
77        mobile_install_launcher_INTERNAL_ = [mi_app_launch_info.launcher],
78    )]
79
80android_instrumentation_test = make_adapter(_aspect_attrs, _adapt)
81