Files
android_frameworks_base/tools/aapt2/java/AnnotationProcessor_test.cpp
Paul Duffin 0a2e8716fc aapt2: Use android.annotation.Hide instead of @hide
Previously, `aapt2` would simply pass an `@hide` doctag through to the
generated `Manifest.java` or `R.java` files and would rely on Metalava
to do the right thing with it. While Metalava does currently support
that it now considers an `@hide` doc tag that is not a block tag, i.e.
start on its own line, as invalid and will report it as an error.

Rather than add custom code to move the `@hide` doctag to the correct
location, or require changes to the resource files where the `@hide`
doctags are written this change leverages the existing support for
handling annotations like `@SystemApi` to replace the `@hide` doctag
with an `@android.annotation.Hide` annotation.

Tests were added to `AnnotationProcessor_test.cpp` to test the handling
of `@hide` and other tests that were broken by this change were fixed.
One test was actually invalid as it specified `@TestApi` without
`@hide` so that too was fixed.

This change did cause a slight change to the contents of the
`framework-doc-system-stubs` as previously the Javadoc comments
still included `@hide` doctags but this change removes them.

Flag: EXEMPT build refactoring
Bug: 444151586
Test: m checkapi
Change-Id: I01c476ef2f47261001d8c89d0218897e45336b40
2025-09-11 09:01:51 +00:00

204 lines
6.9 KiB
C++

/*
* Copyright (C) 2015 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.
*/
#include "java/AnnotationProcessor.h"
#include "io/StringStream.h"
#include "test/Test.h"
#include "text/Printer.h"
using ::aapt::io::StringOutputStream;
using ::aapt::text::Printer;
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::Not;
namespace aapt {
TEST(AnnotationProcessorTest, EmitsDeprecated) {
const char* comment =
"Some comment, and it should contain a marker word, "
"something that marks this resource as nor needed. "
"{@deprecated That's the marker! }";
AnnotationProcessor processor;
processor.AppendComment(comment);
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@Deprecated"));
}
TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) {
AnnotationProcessor processor;
processor.AppendComment("@SystemApi This is a system API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi"));
EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi")));
EXPECT_THAT(annotations, HasSubstr("This is a system API"));
}
TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationParamsAndRemovesFromComment) {
AnnotationProcessor processor;
processor.AppendComment("@SystemApi (p1=k1,p2=k2) This is a system API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi(p1=k1,p2=k2)"));
EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi")));
EXPECT_THAT(annotations, HasSubstr("This is a system API"));
}
TEST(AnnotationProcessorTest, EmitsFlaggedApiAnnotationAndRemovesFromComment) {
AnnotationProcessor processor;
processor.AppendComment("@FlaggedApi This is a flagged API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@android.annotation.FlaggedApi"));
EXPECT_THAT(annotations, Not(HasSubstr("@FlaggedApi")));
EXPECT_THAT(annotations, HasSubstr("This is a flagged API"));
}
TEST(AnnotationProcessorTest, EmitsFlaggedApiAnnotationParamsAndRemovesFromComment) {
AnnotationProcessor processor;
processor.AppendComment("@FlaggedApi (\"android.flags.my_flag\") This is a flagged API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@android.annotation.FlaggedApi(\"android.flags.my_flag\")"));
EXPECT_THAT(annotations, Not(HasSubstr("@FlaggedApi")));
EXPECT_THAT(annotations, HasSubstr("This is a flagged API"));
}
TEST(AnnotationProcessorTest, EmitsTestApiAnnotationAndRemovesFromComment) {
AnnotationProcessor processor;
processor.AppendComment("@TestApi This is a test API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@android.annotation.TestApi"));
EXPECT_THAT(annotations, Not(HasSubstr("@TestApi")));
EXPECT_THAT(annotations, HasSubstr("This is a test API"));
}
TEST(AnnotationProcessorTest, EmitsHideAnnotationAndRemovesFromComment) {
AnnotationProcessor processor;
processor.AppendComment("@hide This is an internal API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@android.annotation.Hide"));
EXPECT_THAT(annotations, Not(HasSubstr("@hide")));
EXPECT_THAT(annotations, HasSubstr("This is an internal API"));
}
TEST(AnnotationProcessorTest, EmitsSystemApiAndHideAnnotationAndRemovesFromComment) {
AnnotationProcessor processor;
processor.AppendComment("@SystemApi @hide This is a system API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi"));
EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi")));
EXPECT_THAT(annotations, HasSubstr("@android.annotation.Hide"));
EXPECT_THAT(annotations, Not(HasSubstr("@hide")));
EXPECT_THAT(annotations, HasSubstr("This is a system API"));
}
TEST(AnnotationProcessorTest, NotEmitSystemApiAnnotation) {
AnnotationProcessor processor;
processor.AppendComment("@SystemApi This is a system API");
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer, true /* strip_api_annotations */);
out.Flush();
EXPECT_THAT(annotations, Not(HasSubstr("@android.annotation.SystemApi")));
EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi")));
EXPECT_THAT(annotations, HasSubstr("This is a system API"));
}
TEST(AnnotationProcessorTest, DoNotAddApiAnnotations) {
AnnotationProcessor processor;
processor.AppendComment(
"@SystemApi This is a system API\n"
"@FlaggedApi This is a flagged API\n"
"@TestApi This is a test API\n"
"@deprecated Deprecate me\n", /*add_api_annotations=*/
false);
std::string annotations;
StringOutputStream out(&annotations);
Printer printer(&out);
processor.Print(&printer);
out.Flush();
EXPECT_THAT(annotations, Not(HasSubstr("@android.annotation.SystemApi")));
EXPECT_THAT(annotations, Not(HasSubstr("@android.annotation.FlaggedApi")));
EXPECT_THAT(annotations, Not(HasSubstr("@android.annotation.TestApi")));
EXPECT_THAT(annotations, Not(HasSubstr("@Deprecated")));
}
TEST(AnnotationProcessorTest, ExtractsFirstSentence) {
EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence("This is the only sentence"),
Eq("This is the only sentence"));
EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence(
"This is the\n first sentence. This is the rest of the paragraph."),
Eq("This is the\n first sentence."));
EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence(
"This is the first sentence with a {@link android.R.styleable.Theme}."),
Eq("This is the first sentence with a {@link android.R.styleable.Theme}."));
}
} // namespace aapt