1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "java/AnnotationProcessor.h"
18 
19 #include "io/StringStream.h"
20 #include "test/Test.h"
21 #include "text/Printer.h"
22 
23 using ::aapt::io::StringOutputStream;
24 using ::aapt::text::Printer;
25 using ::testing::Eq;
26 using ::testing::HasSubstr;
27 using ::testing::Not;
28 
29 namespace aapt {
30 
TEST(AnnotationProcessorTest,EmitsDeprecated)31 TEST(AnnotationProcessorTest, EmitsDeprecated) {
32   const char* comment =
33       "Some comment, and it should contain a marker word, "
34       "something that marks this resource as nor needed. "
35       "{@deprecated That's the marker! }";
36 
37   AnnotationProcessor processor;
38   processor.AppendComment(comment);
39 
40   std::string annotations;
41   StringOutputStream out(&annotations);
42   Printer printer(&out);
43   processor.Print(&printer);
44   out.Flush();
45 
46   EXPECT_THAT(annotations, HasSubstr("@Deprecated"));
47 }
48 
TEST(AnnotationProcessorTest,EmitsSystemApiAnnotationAndRemovesFromComment)49 TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) {
50   AnnotationProcessor processor;
51   processor.AppendComment("@SystemApi This is a system API");
52 
53   std::string annotations;
54   StringOutputStream out(&annotations);
55   Printer printer(&out);
56   processor.Print(&printer);
57   out.Flush();
58 
59   EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi"));
60   EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi")));
61   EXPECT_THAT(annotations, HasSubstr("This is a system API"));
62 }
63 
TEST(AnnotationProcessorTest,EmitsTestApiAnnotationAndRemovesFromComment)64 TEST(AnnotationProcessorTest, EmitsTestApiAnnotationAndRemovesFromComment) {
65   AnnotationProcessor processor;
66   processor.AppendComment("@TestApi This is a test API");
67 
68   std::string annotations;
69   StringOutputStream out(&annotations);
70   Printer printer(&out);
71   processor.Print(&printer);
72   out.Flush();
73 
74   EXPECT_THAT(annotations, HasSubstr("@android.annotation.TestApi"));
75   EXPECT_THAT(annotations, Not(HasSubstr("@TestApi")));
76   EXPECT_THAT(annotations, HasSubstr("This is a test API"));
77 }
78 
TEST(AnnotationProcessorTest,NotEmitSystemApiAnnotation)79 TEST(AnnotationProcessorTest, NotEmitSystemApiAnnotation) {
80   AnnotationProcessor processor;
81   processor.AppendComment("@SystemApi This is a system API");
82 
83   std::string annotations;
84   StringOutputStream out(&annotations);
85   Printer printer(&out);
86   processor.Print(&printer, true /* strip_api_annotations */);
87   out.Flush();
88 
89   EXPECT_THAT(annotations, Not(HasSubstr("@android.annotation.SystemApi")));
90   EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi")));
91   EXPECT_THAT(annotations, HasSubstr("This is a system API"));
92 }
93 
TEST(AnnotationProcessor,ExtractsFirstSentence)94 TEST(AnnotationProcessor, ExtractsFirstSentence) {
95   EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence("This is the only sentence"),
96               Eq("This is the only sentence"));
97   EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence(
98                   "This is the\n  first sentence.  This is the rest of the paragraph."),
99               Eq("This is the\n  first sentence."));
100   EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence(
101                   "This is the first sentence with a {@link android.R.styleable.Theme}."),
102               Eq("This is the first sentence with a {@link android.R.styleable.Theme}."));
103 }
104 
105 }  // namespace aapt
106