From 3c4cc122ebd778ee21aa3a137fa9af77c21ed935 Mon Sep 17 00:00:00 2001 From: Zhi Dou Date: Wed, 16 Aug 2023 15:14:48 +0000 Subject: [PATCH] Add demo code for testing This change add demo for using test mode flag lib in self-instrumentation tests. There are two options. The option 1 is to create two versions, prod and test, of the library using flags. The prod version library uses prod flag lib, and the test version library uses test flag lib. The option 2 is to dynamically link to the flag lib in the library. The flag lib is used as a stub lib and is just used for building purpose. The in the finally binary which uses the library, the binary should statically link to the right version of the flag lib. Bug: 295561701 Test: atest AconfigDemoUnitTests1 && atest AconfigDemoUnitTests2 Change-Id: Ie571a260d9042933032c702095808dbf59d1697c --- samples/AconfigDemo/Android.bp | 120 +++++++++++++++++- .../tests/unittests/AconfigDemoUnitTests1.xml | 33 +++++ .../tests/unittests/AconfigDemoUnitTests2.xml | 33 +++++ .../AconfigDemo/tests/unittests/Android.bp | 47 +++++-- .../tests/unittests/AndroidManifest.xml | 32 +++++ 5 files changed, 247 insertions(+), 18 deletions(-) create mode 100644 samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests1.xml create mode 100644 samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests2.xml create mode 100644 samples/AconfigDemo/tests/unittests/AndroidManifest.xml diff --git a/samples/AconfigDemo/Android.bp b/samples/AconfigDemo/Android.bp index a85aaef8e..c36ebf89d 100644 --- a/samples/AconfigDemo/Android.bp +++ b/samples/AconfigDemo/Android.bp @@ -2,8 +2,8 @@ package { default_applicable_licenses: ["Android-Apache-2.0"], } -android_app { - name: "AconfigDemoActivity", +java_defaults { + name: "AconfigDemoActivityDefault", manifest: "AndroidManifest.xml", srcs: [ "src/**/*.java" @@ -11,7 +11,6 @@ android_app { platform_apis: true, certificate: "platform", static_libs: [ - "ContentLibs", "dagger2", "jsr330", ], @@ -30,6 +29,14 @@ android_app { plugins: ["dagger2-compiler"] } +android_app { + name: "AconfigDemoActivity", + defaults: ["AconfigDemoActivityDefault"], + static_libs: [ + "ContentLibs", + ] +} + aconfig_declarations { name: "aconfig_demo_flags", package: "com.example.android.aconfig.demo.flags", @@ -49,18 +56,24 @@ filegroup { ], } -java_library { - name: "ContentLibs", +java_defaults { + name: "ContentLibsDefault", + sdk_version: "current", srcs: [ ":ContentLibsFile", ], - sdk_version: "current", + libs: ["jsr330"], +} + +java_library { + name: "ContentLibs", + defaults: ["ContentLibsDefault"], static_libs: [ "aconfig_demo_flags_java_lib", ], - libs: ["jsr330"], } + cc_aconfig_library { name: "aconfig_demo_flags_c_lib", aconfig_declarations: "aconfig_demo_flags", @@ -103,3 +116,96 @@ rust_ffi_shared { "libaconfig_demo_flags_rust", ] } + +// Test setup + +// Create test verion of the jave flag library +// It needs to use the same aconfig_declarations as +// the production one +java_aconfig_library { + name: "aconfig_demo_flags_java_lib_test", + aconfig_declarations: "aconfig_demo_flags", + // host_supported is set to true here for test running + // one host, in tests/unittests/Android.bp + host_supported: true, + test: true +} + + +// Option 1 +// Create a test version of the library under testing +// The test version of this library shares the common +// settings in a java_defaults target with the production +// version library. The test version library statically +// links to the test flag library, and the production +// version library links to the production version flag +// library. + +java_library { + name: "ContentLibsTest1", + defaults: ["ContentLibsDefault"], + static_libs: [ + "aconfig_demo_flags_java_lib_test", + ], +} + +// Create the test version of the android_app. This app is used +// for self-instrumentation test. This app uses the test version +// of the library which uses flags. +// Please check tests/unittests/Android.bp:AconfigDemoUnitTests1 +android_app { + name: "AconfigDemoActivityTest1", + defaults: ["AconfigDemoActivityDefault"], + optimize: { + enabled: false, + }, + static_libs: [ + "ContentLibsTest1", + ] +} + +// Option 2 +// Instead of creating two verions of the library containing flags, +// it could work that create one version of the library and dynamically +// link to the flag library. The flag library here just works as a stub +// library for the purpose of building. The real flag library should be +// statically linked in the final binary. + + +// This library is created to demonstrate dynamically linking to flag +// library +java_library { + name: "ContentLibs2", + defaults: ["ContentLibsDefault"], + sdk_version: "current", + libs: [ + // link the flag library for building purpose + "aconfig_demo_flags_java_lib", + ], +} + +// This app is created to demonstrate the production version of the app +// statically links to the production version of the flag library. +android_app { + name: "AconfigDemoActivity2", + defaults: ["AconfigDemoActivityDefault"], + static_libs: [ + "ContentLibs2", + "aconfig_demo_flags_java_lib", + ] +} + +// This app is created to demonstrate the test version of app uses the same +// ContentLibs2 library, but links to test version of the flag library. +// Please check tests/unittests/Android.bp:AconfigDemoUnitTests2 +android_app { + name: "AconfigDemoActivityTest2", + defaults: ["AconfigDemoActivityDefault"], + optimize: { + enabled: false, + }, + static_libs: [ + "ContentLibs2", + "aconfig_demo_flags_java_lib", + ] +} diff --git a/samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests1.xml b/samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests1.xml new file mode 100644 index 000000000..996f82563 --- /dev/null +++ b/samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests1.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests2.xml b/samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests2.xml new file mode 100644 index 000000000..0fe257447 --- /dev/null +++ b/samples/AconfigDemo/tests/unittests/AconfigDemoUnitTests2.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/AconfigDemo/tests/unittests/Android.bp b/samples/AconfigDemo/tests/unittests/Android.bp index 627ea0456..ddd553edf 100644 --- a/samples/AconfigDemo/tests/unittests/Android.bp +++ b/samples/AconfigDemo/tests/unittests/Android.bp @@ -1,26 +1,19 @@ -java_aconfig_library { - name: "aconfig_demo_flags_lib_test", - aconfig_declarations: "aconfig_demo_flags", - host_supported: true, - test: true -} - java_library_host { - name: "ContentLibs_test", + name: "ContentLibsHost", srcs: [ ":ContentLibsFile", ], static_libs: [ - "aconfig_demo_flags_lib_test", + "aconfig_demo_flags_java_lib_test", ], libs: ["jsr330"], } java_test_host { - name: "ContentLibs_test_host", + name: "ContentLibsTestHost", srcs: ["*.java"], static_libs: [ - "ContentLibs_test", + "ContentLibsHost", "junit", "flag-junit-base", ], @@ -29,3 +22,35 @@ java_test_host { }, test_suites: ["general-tests"], } + +android_test { + name: "AconfigDemoUnitTests1", + srcs: ["*.java"], + certificate: "platform", + static_libs: [ + "junit", + "androidx.test.runner", + "flag-junit-base", + "platform-test-annotations", + ], + manifest: "AndroidManifest.xml", + test_config: "AconfigDemoUnitTests1.xml", + data: [":AconfigDemoActivityTest1"], + instrumentation_for: "AconfigDemoActivityTest1", +} + +android_test { + name: "AconfigDemoUnitTests2", + srcs: ["*.java"], + certificate: "platform", + static_libs: [ + "junit", + "androidx.test.runner", + "flag-junit-base", + "platform-test-annotations", + ], + manifest: "AndroidManifest.xml", + test_config: "AconfigDemoUnitTests2.xml", + data: [":AconfigDemoActivityTest2"], + instrumentation_for: "AconfigDemoActivityTest2", +} \ No newline at end of file diff --git a/samples/AconfigDemo/tests/unittests/AndroidManifest.xml b/samples/AconfigDemo/tests/unittests/AndroidManifest.xml new file mode 100644 index 000000000..6a59f639e --- /dev/null +++ b/samples/AconfigDemo/tests/unittests/AndroidManifest.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + +