From 4a8cecd0f66f3cdd4144d47c110ae53f6eda8794 Mon Sep 17 00:00:00 2001 From: Zhi Dou Date: Fri, 28 Jul 2023 21:38:35 +0000 Subject: [PATCH] Add usage of test mode Add usage of test mode of java_aconfig_library. Add local host unit test of contentlibs Test: atest ContentLibs_test_host --host Bug: 287644619 Change-Id: Ib99a3f2b692d3251d445a2e188325ee0b0b6b268 --- samples/AconfigDemo/Android.bp | 9 +++- .../AconfigDemo/tests/unittests/Android.bp | 31 +++++++++++++ .../unittests/StaticContentUnitTests.java | 45 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 samples/AconfigDemo/tests/unittests/Android.bp create mode 100644 samples/AconfigDemo/tests/unittests/StaticContentUnitTests.java diff --git a/samples/AconfigDemo/Android.bp b/samples/AconfigDemo/Android.bp index a407f3493..7f17e8c69 100644 --- a/samples/AconfigDemo/Android.bp +++ b/samples/AconfigDemo/Android.bp @@ -39,10 +39,17 @@ java_aconfig_library { aconfig_declarations: "aconfig_demo_flags", } +filegroup { + name: "ContentLibsFile", + srcs: [ + "lib/**/*.java", + ], +} + java_library { name: "ContentLibs", srcs: [ - "lib/**/*.java", + ":ContentLibsFile", ], sdk_version: "current", static_libs: [ diff --git a/samples/AconfigDemo/tests/unittests/Android.bp b/samples/AconfigDemo/tests/unittests/Android.bp new file mode 100644 index 000000000..627ea0456 --- /dev/null +++ b/samples/AconfigDemo/tests/unittests/Android.bp @@ -0,0 +1,31 @@ +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", + srcs: [ + ":ContentLibsFile", + ], + static_libs: [ + "aconfig_demo_flags_lib_test", + ], + libs: ["jsr330"], +} + +java_test_host { + name: "ContentLibs_test_host", + srcs: ["*.java"], + static_libs: [ + "ContentLibs_test", + "junit", + "flag-junit-base", + ], + test_options: { + unit_test: true, + }, + test_suites: ["general-tests"], +} diff --git a/samples/AconfigDemo/tests/unittests/StaticContentUnitTests.java b/samples/AconfigDemo/tests/unittests/StaticContentUnitTests.java new file mode 100644 index 000000000..71597eff3 --- /dev/null +++ b/samples/AconfigDemo/tests/unittests/StaticContentUnitTests.java @@ -0,0 +1,45 @@ +/* + * 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. + */ + +package com.example.android.aconfig.demo; + +import static org.junit.Assert.assertEquals; + +import android.platform.test.flag.junit.MockFlagsRule; +import com.example.android.aconfig.demo.flags.Flags; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class StaticContentUnitTests { + @Rule public final MockFlagsRule mMockFlagsRule = new MockFlagsRule(); + + @Test + public void staticContent_enable_staticFlag_disable_thirdFlag() throws Exception { + mMockFlagsRule.enableFlags(Flags.FLAG_APPEND_STATIC_CONTENT); + mMockFlagsRule.disableFlags(Flags.FLAG_THIRD_FLAG); + StaticContent statiContent = new StaticContent(); + String ret = statiContent.getContent(); + StringBuilder expected = new StringBuilder(); + expected.append("The flag: appendStaticContent is ON!!\n\n"); + expected.append("The flag: thirdFlag is OFF!!\n\n"); + assertEquals("Expected message", expected.toString(), ret); + } +} +