1# Copyright (C) 2023 The Android Open Source Project 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 15load("@bazel_skylib//lib:paths.bzl", "paths") 16load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") 17load("@rules_testing//lib:truth.bzl", "matching") 18load("@rules_testing//lib:util.bzl", "util") 19load(":framework_resources.bzl", "framework_resources") 20 21def _exists(unused_value): 22 return True 23 24_exist_matcher = matching.custom( 25 "matcher to check that a set is not empty", 26 _exists, 27) 28 29def _test_native_providers(name): 30 util.helper_target( 31 framework_resources, 32 name = name + "_subject", 33 manifest = "AndroidManifest.xml", 34 resource_files = ["res/values/attrs.xml"], 35 resource_zips = ["resource_zip.zip"], 36 ) 37 analysis_test( 38 name = name, 39 impl = _test_native_providers_impl, 40 target = name + "_subject", 41 ) 42 43def _test_starlark_rule(name): 44 util.helper_target( 45 framework_resources, 46 name = name + "_subject", 47 manifest = "AndroidManifest.xml", 48 resource_files = ["res/values/attrs.xml"], 49 resource_zips = ["resource_zip.zip"], 50 ) 51 analysis_test( 52 name = name, 53 impl = _test_starlark_rule_impl, 54 target = name + "_subject" + "_RESOURCES_DO_NOT_USE", 55 ) 56 57def _test_native_providers_impl(env, target): 58 env.expect.that_target(target).default_outputs().contains_predicate( 59 matching.file_basename_equals(target.label.name + ".apk"), 60 ) 61 env.expect.that_target(target).output_group("classjar").contains_predicate(_exist_matcher) 62 env.expect.that_target(target).output_group("srcjar").contains_predicate(_exist_matcher) 63 env.expect.that_target(target).output_group("resource_apk").contains_predicate(_exist_matcher) 64 65def _test_starlark_rule_impl(env, target): 66 for mnemonic in [ 67 "FixAndroidManifest", 68 "UnzipResourceZips", 69 "CompileAndroidResources", 70 "ExcludeDefaultResources", 71 "AaptLinkFrameworkRes", 72 "FrameworkResSrcJar", 73 "StarlarkRClassGenerator", 74 "TouchFakeProtoManifest", 75 ]: 76 # Tautology, but the test will fail if the action doesn't exit. 77 env.expect.that_target(target).action_named(mnemonic).mnemonic().equals(mnemonic) 78 79 # Providers 80 env.expect.that_target(target).has_provider(AndroidApplicationResourceInfo) 81 env.expect.that_target(target).output_group("classjar").contains_predicate(_exist_matcher) 82 env.expect.that_target(target).output_group("srcjar").contains_predicate(_exist_matcher) 83 env.expect.that_target(target).output_group("resource_apk").contains_predicate(_exist_matcher) 84 85def framework_resources_test_suite(name): 86 test_suite( 87 name = name, 88 tests = [ 89 _test_native_providers, 90 _test_starlark_rule, 91 ], 92 ) 93