| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 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 "ResourceParser.h" |
| 18 | #include "ResourceTable.h" |
| 19 | #include "ResourceValues.h" |
| 20 | #include "XmlPullParser.h" |
| 21 | |
| 22 | #include "java/AnnotationProcessor.h" |
| 23 | |
| 24 | #include "test/Builders.h" |
| 25 | #include "test/Context.h" |
| 26 | |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
| 31 | struct AnnotationProcessorTest : public ::testing::Test { |
| 32 | std::unique_ptr<IAaptContext> mContext; |
| 33 | ResourceTable mTable; |
| 34 | |
| 35 | void SetUp() override { |
| 36 | mContext = test::ContextBuilder().build(); |
| 37 | } |
| 38 | |
| 39 | ::testing::AssertionResult parse(const StringPiece& str) { |
| 40 | ResourceParserOptions options; |
| 41 | ResourceParser parser(mContext->getDiagnostics(), &mTable, Source{}, ConfigDescription{}, |
| 42 | options); |
| 43 | std::stringstream in; |
| 44 | in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str; |
| 45 | XmlPullParser xmlParser(in); |
| 46 | if (parser.parse(&xmlParser)) { |
| 47 | return ::testing::AssertionSuccess(); |
| 48 | } |
| 49 | return ::testing::AssertionFailure(); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | TEST_F(AnnotationProcessorTest, EmitsDeprecated) { |
| 54 | ASSERT_TRUE(parse(R"EOF( |
| 55 | <resources> |
| 56 | <declare-styleable name="foo"> |
| 57 | <!-- Some comment, and it should contain |
| 58 | a marker word, something that marks |
| 59 | this resource as nor needed. |
| 60 | {@deprecated That's the marker! } --> |
| 61 | <attr name="autoText" format="boolean" /> |
| 62 | </declare-styleable> |
| 63 | </resources>)EOF")); |
| 64 | |
| 65 | Attribute* attr = test::getValue<Attribute>(&mTable, u"@attr/autoText"); |
| 66 | ASSERT_NE(nullptr, attr); |
| 67 | |
| 68 | AnnotationProcessor processor; |
| 69 | processor.appendComment(attr->getComment()); |
| 70 | |
| 71 | std::stringstream result; |
| 72 | processor.writeToStream(&result, ""); |
| 73 | std::string annotations = result.str(); |
| 74 | |
| 75 | EXPECT_NE(std::string::npos, annotations.find("@Deprecated")); |
| 76 | } |
| 77 | |
| 78 | } // namespace aapt |
| 79 | |
| 80 | |