diff options
Diffstat (limited to 'test/rules/android_local_test/java')
9 files changed, 333 insertions, 0 deletions
diff --git a/test/rules/android_local_test/java/com/starlark_resources/AndroidManifest.xml b/test/rules/android_local_test/java/com/starlark_resources/AndroidManifest.xml new file mode 100644 index 0000000..978e2c8 --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/AndroidManifest.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.starlark_resources"> + <uses-sdk + android:minSdkVersion="21" + android:targetSdkVersion="24" /> +</manifest> diff --git a/test/rules/android_local_test/java/com/starlark_resources/BUILD b/test/rules/android_local_test/java/com/starlark_resources/BUILD new file mode 100644 index 0000000..f5824cd --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/BUILD @@ -0,0 +1,177 @@ +# Tests that run on head android_local_test rule to verify Starlark resource processing pipeline. + +load( + "//rules:rules.bzl", + "android_library", + "android_local_test", +) +load( + "//test/rules/android_local_test:test.bzl", + "rule_test", +) + +package( + default_applicable_licenses = ["//:license"], + default_visibility = ["//visibility:private"], +) + +licenses(["notice"]) + +android_local_test( + name = "no_deps_with_resources", + srcs = ["SampleTest.java"], + manifest = "AndroidManifest.xml", + resource_files = glob(["res/**"]), + test_class = "com.starlark_resources.SampleTest", + deps = [ + "@robolectric//bazel:android-all", + "@rules_android_maven//:androidx_test_core", + "@rules_android_maven//:androidx_test_ext_junit", + "@rules_android_maven//:junit_junit", + "@rules_android_maven//:org_robolectric_robolectric", + ], +) + +rule_test( + name = "no_deps_with_resources_rule_test", + target_under_test = ":no_deps_with_resources", +) + +# TODO(b/161179595): Add test to exercise resource_configuration_filter wiring. + +# TODO(aarmin): Add test to exercise densities and manifest_values wiring. + +android_library( + name = "resource_processing", + assets = ["assets/bar.txt"], + assets_dir = "assets", + manifest = "AndroidManifest.xml", + resource_files = glob(["res/**"]), +) + +android_local_test( + name = "single_resource_dep_without_manifest", + srcs = ["SampleTest.java"], + test_class = "com.starlark_resources.SampleTest", + deps = [ + ":resource_processing", + "@robolectric//bazel:android-all", + "@rules_android_maven//:androidx_test_core", + "@rules_android_maven//:androidx_test_ext_junit", + "@rules_android_maven//:junit_junit", + "@rules_android_maven//:org_robolectric_robolectric", + ], +) + +rule_test( + name = "single_resource_dep_without_manifest_rule_test", + target_under_test = ":single_resource_dep_without_manifest", +) + +android_local_test( + name = "single_resource_dep", + srcs = ["SampleTest.java"], + manifest = "AndroidManifest.xml", + test_class = "com.starlark_resources.SampleTest", + deps = [ + ":resource_processing", + "@robolectric//bazel:android-all", + "@rules_android_maven//:androidx_test_core", + "@rules_android_maven//:androidx_test_ext_junit", + "@rules_android_maven//:junit_junit", + "@rules_android_maven//:org_robolectric_robolectric", + ], +) + +rule_test( + name = "single_resource_dep_rule_test", + target_under_test = ":single_resource_dep", +) + +android_library( + name = "resources_with_dep_with_res", + assets = ["assets/foo.txt"], + assets_dir = "assets", + manifest = "AndroidManifest.xml", + resource_files = glob(["another_res/**"]), + deps = [":resource_processing"], +) + +android_local_test( + name = "multiple_resource_deps", + srcs = ["SampleTestMultipleDeps.java"], + manifest = "AndroidManifest.xml", + test_class = "com.starlark_resources.SampleTestMultipleDeps", + deps = [ + ":resources_with_dep_with_res", + "@robolectric//bazel:android-all", + "@rules_android_maven//:androidx_test_core", + "@rules_android_maven//:androidx_test_ext_junit", + "@rules_android_maven//:junit_junit", + "@rules_android_maven//:org_robolectric_robolectric", + ], +) + +rule_test( + name = "multiple_resource_deps_rule_test", + target_under_test = ":multiple_resource_deps", +) + +android_library( + name = "resource_processing_with_neverlink", + manifest = "AndroidManifest.xml", + neverlink = True, + resource_files = glob(["res/**"]), +) + +android_local_test( + name = "depends_on_neverlink_lib", + srcs = ["SampleTestNeverlinkDep.java"], + manifest = "AndroidManifest.xml", + test_class = "com.starlark_resources.SampleTestNeverlinkDep", + deps = [ + ":resource_processing_with_neverlink", + "@robolectric//bazel:android-all", + "@rules_android_maven//:androidx_test_core", + "@rules_android_maven//:androidx_test_ext_junit", + "@rules_android_maven//:junit_junit", + "@rules_android_maven//:org_robolectric_robolectric", + ], +) + +rule_test( + name = "depends_on_neverlink_lib_rule_test", + expect_resources = False, + target_under_test = ":depends_on_neverlink_lib", +) + +android_local_test( + name = "manifest_values_low_minsdk", + srcs = ["SampleTest.java"], + manifest = "AndroidManifest.xml", + manifest_values = {"minSdkVersion": "15"}, + resource_files = glob(["res/**"]), + test_class = "com.starlark_resources.SampleTest", + deps = [ + "@robolectric//bazel:android-all", + "@rules_android_maven//:androidx_test_core", + "@rules_android_maven//:androidx_test_ext_junit", + "@rules_android_maven//:junit_junit", + "@rules_android_maven//:org_robolectric_robolectric", + ], +) + +android_local_test( + name = "manifest_values_low_minsdk_no_manifest", + srcs = ["SampleTest.java"], + manifest_values = {"minSdkVersion": "15"}, + resource_files = glob(["res/**"]), + test_class = "com.starlark_resources.SampleTest", + deps = [ + "@robolectric//bazel:android-all", + "@rules_android_maven//:androidx_test_core", + "@rules_android_maven//:androidx_test_ext_junit", + "@rules_android_maven//:junit_junit", + "@rules_android_maven//:org_robolectric_robolectric", + ], +) diff --git a/test/rules/android_local_test/java/com/starlark_resources/SampleTest.java b/test/rules/android_local_test/java/com/starlark_resources/SampleTest.java new file mode 100644 index 0000000..32529df --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/SampleTest.java @@ -0,0 +1,43 @@ +/** + * Copyright 2021 The Bazel Authors. All rights reserved. + * + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * <p>http://www.apache.org/licenses/LICENSE-2.0 + * + * <p>Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.starlark_resources; + +import static org.junit.Assert.assertEquals; + +import android.content.Context; +import android.content.res.Resources; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class SampleTest { + private Context targetContext; + private Resources resources; + + @Before + public void setup() throws Exception { + targetContext = ApplicationProvider.getApplicationContext(); + resources = targetContext.getResources(); + } + + @Test + public void test() throws Exception { + assertEquals("Check package name", "com.starlark_resources", targetContext.getPackageName()); + assertEquals( + "Check resource `a_string`", "Hello World!", resources.getString(R.string.a_string)); + } +} diff --git a/test/rules/android_local_test/java/com/starlark_resources/SampleTestMultipleDeps.java b/test/rules/android_local_test/java/com/starlark_resources/SampleTestMultipleDeps.java new file mode 100644 index 0000000..7091594 --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/SampleTestMultipleDeps.java @@ -0,0 +1,45 @@ +/** + * Copyright 2021 The Bazel Authors. All rights reserved. + * + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * <p>http://www.apache.org/licenses/LICENSE-2.0 + * + * <p>Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.starlark_resources; + +import static org.junit.Assert.assertEquals; + +import android.content.Context; +import android.content.res.Resources; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class SampleTestMultipleDeps { + private Context targetContext; + private Resources resources; + + @Before + public void setup() throws Exception { + targetContext = ApplicationProvider.getApplicationContext(); + resources = targetContext.getResources(); + } + + @Test + public void test() throws Exception { + assertEquals("Check package name", "com.starlark_resources", targetContext.getPackageName()); + assertEquals( + "Check resource `a_string`", "Hello World!", resources.getString(R.string.a_string)); + assertEquals( + "Check resource `another_res`", "Another Res!", resources.getString(R.string.another_res)); + } +} diff --git a/test/rules/android_local_test/java/com/starlark_resources/SampleTestNeverlinkDep.java b/test/rules/android_local_test/java/com/starlark_resources/SampleTestNeverlinkDep.java new file mode 100644 index 0000000..3da5dca --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/SampleTestNeverlinkDep.java @@ -0,0 +1,51 @@ +/** + * Copyright 2021 The Bazel Authors. All rights reserved. + * + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * <p>http://www.apache.org/licenses/LICENSE-2.0 + * + * <p>Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.starlark_resources; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.content.Context; +import android.content.res.Resources; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class SampleTestNeverlinkDep { + private Context targetContext; + private Resources resources; + + @Before + public void setup() throws Exception { + targetContext = ApplicationProvider.getApplicationContext(); + resources = targetContext.getResources(); + } + + @Test + public void test() throws Exception { + assertEquals("Check package name", "com.starlark_resources", targetContext.getPackageName()); + try { + // This line should throw an exception since there are no resources inherited from + // neverlink deps. + assertEquals( + "Check resource `a_string`", "Hello World!", resources.getString(R.string.a_string)); + } catch(NoClassDefFoundError e) { + return; + } + fail("Expected NoClassDefFoundError exception"); + } +} diff --git a/test/rules/android_local_test/java/com/starlark_resources/another_res/values/strings.xml b/test/rules/android_local_test/java/com/starlark_resources/another_res/values/strings.xml new file mode 100644 index 0000000..554ea5e --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/another_res/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="another_res">Another Res!</string> +</resources> diff --git a/test/rules/android_local_test/java/com/starlark_resources/assets/bar.txt b/test/rules/android_local_test/java/com/starlark_resources/assets/bar.txt new file mode 100644 index 0000000..5716ca5 --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/assets/bar.txt @@ -0,0 +1 @@ +bar diff --git a/test/rules/android_local_test/java/com/starlark_resources/assets/foo.txt b/test/rules/android_local_test/java/com/starlark_resources/assets/foo.txt new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/assets/foo.txt @@ -0,0 +1 @@ +foo diff --git a/test/rules/android_local_test/java/com/starlark_resources/res/values/strings.xml b/test/rules/android_local_test/java/com/starlark_resources/res/values/strings.xml new file mode 100644 index 0000000..2686d09 --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/res/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="a_string">Hello World!</string> +</resources> |