aconfig: add Java integration tests

Add integration tests for Java. This test setup verifies that

  - the build system calls aconfig to generate a Java library
  - the Java test compiles against the auto-generated library
  - the auto-generated code returns expected values

Similar integration tests for C++ and Rust will be added in follow-up
CLs.

Note: the build does not currently support specifying that
tests/*.values should be applied, so the test flags will all be assigned
the defaults. A later CL will fix this.

Bug: b/283911467
Test: atest aconfig.test aconfig.test.java
Change-Id: Ia365e209261f4935a23e2dac9ef0ab5b60f76e52
diff --git a/tools/aconfig/Android.bp b/tools/aconfig/Android.bp
index b8cce29..fd10001 100644
--- a/tools/aconfig/Android.bp
+++ b/tools/aconfig/Android.bp
@@ -2,6 +2,8 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
+// host binary: aconfig
+
 rust_protobuf_host {
     name: "libaconfig_protos",
     protos: ["protos/aconfig.proto"],
@@ -39,3 +41,42 @@
         "libitertools",
     ],
 }
+
+// integration tests: java
+
+device_config_definitions {
+    name: "aconfig.test.flags",
+    namespace: "com.android.aconfig.test",
+    srcs: ["tests/test.aconfig"],
+}
+
+device_config_values {
+    name: "aconfig.test.flag.values",
+    namespace: "com.android.aconfig.test",
+    srcs: [
+        "tests/first.values",
+        "tests/second.values",
+    ],
+}
+
+device_config_value_set {
+    name: "aconfig.test.flag.value_set",
+    values: [
+        "aconfig.test.flag.values",
+    ],
+}
+
+android_test {
+    name: "aconfig.test.java",
+    srcs: [
+        "tests/**/*.java",
+        ":aconfig.test.flags{.srcjar}",
+    ],
+    manifest: "tests/AndroidManifest.xml",
+    certificate: "platform",
+    static_libs: [
+        "androidx.test.rules",
+        "testng",
+    ],
+    test_suites: ["device-tests"],
+}
diff --git a/tools/aconfig/src/codegen_java.rs b/tools/aconfig/src/codegen_java.rs
index dfd6766..cf025cb 100644
--- a/tools/aconfig/src/codegen_java.rs
+++ b/tools/aconfig/src/codegen_java.rs
@@ -59,7 +59,7 @@
     let device_config_flag = codegen::create_device_config_ident(package, &item.name)
         .expect("values checked at cache creation time");
     ClassElement {
-        method_name: item.name.clone(),
+        method_name: item.name.replace('-', "_"),
         readwrite: item.permission == Permission::ReadWrite,
         default_value: if item.state == FlagState::Enabled {
             "true".to_string()
diff --git a/tools/aconfig/src/test.rs b/tools/aconfig/src/test.rs
index cc19c6a..76ef005 100644
--- a/tools/aconfig/src/test.rs
+++ b/tools/aconfig/src/test.rs
@@ -24,17 +24,17 @@
         crate::commands::create_cache(
             "com.android.aconfig.test",
             vec![Input {
-                source: Source::File("testdata/test.aconfig".to_string()),
-                reader: Box::new(include_bytes!("../testdata/test.aconfig").as_slice()),
+                source: Source::File("tests/test.aconfig".to_string()),
+                reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
             }],
             vec![
                 Input {
-                    source: Source::File("testdata/first.values".to_string()),
-                    reader: Box::new(include_bytes!("../testdata/first.values").as_slice()),
+                    source: Source::File("tests/first.values".to_string()),
+                    reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
                 },
                 Input {
-                    source: Source::File("testdata/test.aconfig".to_string()),
-                    reader: Box::new(include_bytes!("../testdata/second.values").as_slice()),
+                    source: Source::File("tests/test.aconfig".to_string()),
+                    reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
                 },
             ],
         )
diff --git a/tools/aconfig/tests/AconfigTest.java b/tools/aconfig/tests/AconfigTest.java
new file mode 100644
index 0000000..5db490b
--- /dev/null
+++ b/tools/aconfig/tests/AconfigTest.java
@@ -0,0 +1,37 @@
+import static com.android.aconfig.test.Flags.disabled_ro;
+import static com.android.aconfig.test.Flags.disabled_rw;
+import static com.android.aconfig.test.Flags.enabled_ro;
+import static com.android.aconfig.test.Flags.enabled_rw;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class AconfigTest {
+    @Test
+    public void testDisabledReadOnlyFlag() {
+        assertFalse(disabled_ro());
+    }
+
+    @Test
+    public void testEnabledReadOnlyFlag() {
+        // TODO: change to assertTrue(enabled_ro()) when the build supports reading tests/*.values
+        // (currently all flags are assigned the default READ_ONLY + DISABLED)
+        assertFalse(enabled_ro());
+    }
+
+    @Test
+    public void testDisabledReadWriteFlag() {
+        assertFalse(disabled_rw());
+    }
+
+    @Test
+    public void testEnabledReadWriteFlag() {
+        // TODO: change to assertTrue(enabled_rw()) when the build supports reading tests/*.values
+        // (currently all flags are assigned the default READ_ONLY + DISABLED)
+        assertFalse(enabled_rw());
+    }
+}
diff --git a/tools/aconfig/tests/AndroidManifest.xml b/tools/aconfig/tests/AndroidManifest.xml
new file mode 100644
index 0000000..04002e6
--- /dev/null
+++ b/tools/aconfig/tests/AndroidManifest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2023 The Android Open Source Project
+
+     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
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="aconfig.test.java">
+
+    <uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />
+
+    <application>
+        <uses-library android:name="android.test.runner"/>
+    </application>
+
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+        android:targetPackage="aconfig.test.java"
+        android:label="aconfig integration tests (java)" />
+</manifest>
diff --git a/tools/aconfig/testdata/first.values b/tools/aconfig/tests/first.values
similarity index 100%
rename from tools/aconfig/testdata/first.values
rename to tools/aconfig/tests/first.values
diff --git a/tools/aconfig/testdata/second.values b/tools/aconfig/tests/second.values
similarity index 100%
rename from tools/aconfig/testdata/second.values
rename to tools/aconfig/tests/second.values
diff --git a/tools/aconfig/testdata/test.aconfig b/tools/aconfig/tests/test.aconfig
similarity index 100%
rename from tools/aconfig/testdata/test.aconfig
rename to tools/aconfig/tests/test.aconfig