| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "compile/InlineXmlFormatParser.h" |
| 18 | #include "test/Test.h" |
| 19 | |
| 20 | namespace aapt { |
| 21 | |
| 22 | TEST(InlineXmlFormatParserTest, PassThrough) { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 23 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 24 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF( |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 25 | <View xmlns:android="http://schemas.android.com/apk/res/android"> |
| 26 | <View android:text="hey"> |
| 27 | <View android:id="hi" /> |
| 28 | </View> |
| 29 | </View>)EOF"); |
| 30 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | InlineXmlFormatParser parser; |
| 32 | ASSERT_TRUE(parser.consume(context.get(), doc.get())); |
| 33 | EXPECT_EQ(0u, parser.getExtractedInlineXmlDocuments().size()); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | TEST(InlineXmlFormatParserTest, ExtractOneXmlResource) { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 38 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF( |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 39 | <View1 xmlns:android="http://schemas.android.com/apk/res/android" |
| 40 | xmlns:aapt="http://schemas.android.com/aapt"> |
| 41 | <aapt:attr name="android:text"> |
| 42 | <View2 android:text="hey"> |
| 43 | <View3 android:id="hi" /> |
| 44 | </View2> |
| 45 | </aapt:attr> |
| 46 | </View1>)EOF"); |
| 47 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | doc->file.name = test::parseNameOrDie("layout/main"); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 49 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | InlineXmlFormatParser parser; |
| 51 | ASSERT_TRUE(parser.consume(context.get(), doc.get())); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 52 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | // One XML resource should have been extracted. |
| 54 | EXPECT_EQ(1u, parser.getExtractedInlineXmlDocuments().size()); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 55 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | xml::Element* el = xml::findRootElement(doc.get()); |
| 57 | ASSERT_NE(nullptr, el); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 58 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | EXPECT_EQ("View1", el->name); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 60 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | // The <aapt:attr> tag should be extracted. |
| 62 | EXPECT_EQ(nullptr, el->findChild(xml::kSchemaAapt, "attr")); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 63 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | // The 'android:text' attribute should be set with a reference. |
| 65 | xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, "text"); |
| 66 | ASSERT_NE(nullptr, attr); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 67 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | ResourceNameRef nameRef; |
| 69 | ASSERT_TRUE(ResourceUtils::parseReference(attr->value, &nameRef)); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 70 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | xml::XmlResource* extractedDoc = |
| 72 | parser.getExtractedInlineXmlDocuments()[0].get(); |
| 73 | ASSERT_NE(nullptr, extractedDoc); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 74 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | // Make sure the generated reference is correct. |
| 76 | EXPECT_EQ(nameRef.package, extractedDoc->file.name.package); |
| 77 | EXPECT_EQ(nameRef.type, extractedDoc->file.name.type); |
| 78 | EXPECT_EQ(nameRef.entry, extractedDoc->file.name.entry); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 79 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | // Verify the structure of the extracted XML. |
| 81 | el = xml::findRootElement(extractedDoc); |
| 82 | ASSERT_NE(nullptr, el); |
| 83 | EXPECT_EQ("View2", el->name); |
| 84 | EXPECT_NE(nullptr, el->findChild({}, "View3")); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST(InlineXmlFormatParserTest, ExtractTwoXmlResources) { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 89 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF( |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 90 | <View1 xmlns:android="http://schemas.android.com/apk/res/android" |
| 91 | xmlns:aapt="http://schemas.android.com/aapt"> |
| 92 | <aapt:attr name="android:text"> |
| 93 | <View2 android:text="hey"> |
| 94 | <View3 android:id="hi" /> |
| 95 | </View2> |
| 96 | </aapt:attr> |
| 97 | |
| 98 | <aapt:attr name="android:drawable"> |
| 99 | <vector /> |
| 100 | </aapt:attr> |
| 101 | </View1>)EOF"); |
| 102 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | doc->file.name = test::parseNameOrDie("layout/main"); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 104 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | InlineXmlFormatParser parser; |
| 106 | ASSERT_TRUE(parser.consume(context.get(), doc.get())); |
| 107 | ASSERT_EQ(2u, parser.getExtractedInlineXmlDocuments().size()); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 108 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | xml::Element* el = xml::findRootElement(doc.get()); |
| 110 | ASSERT_NE(nullptr, el); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 111 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | EXPECT_EQ("View1", el->name); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 113 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | xml::Attribute* attrText = el->findAttribute(xml::kSchemaAndroid, "text"); |
| 115 | ASSERT_NE(nullptr, attrText); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 116 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | xml::Attribute* attrDrawable = |
| 118 | el->findAttribute(xml::kSchemaAndroid, "drawable"); |
| 119 | ASSERT_NE(nullptr, attrDrawable); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 120 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | // The two extracted resources should have different names. |
| 122 | EXPECT_NE(attrText->value, attrDrawable->value); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 123 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | // The child <aapt:attr> elements should be gone. |
| 125 | EXPECT_EQ(nullptr, el->findChild(xml::kSchemaAapt, "attr")); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 126 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | xml::XmlResource* extractedDocText = |
| 128 | parser.getExtractedInlineXmlDocuments()[0].get(); |
| 129 | ASSERT_NE(nullptr, extractedDocText); |
| 130 | el = xml::findRootElement(extractedDocText); |
| 131 | ASSERT_NE(nullptr, el); |
| 132 | EXPECT_EQ("View2", el->name); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 133 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | xml::XmlResource* extractedDocDrawable = |
| 135 | parser.getExtractedInlineXmlDocuments()[1].get(); |
| 136 | ASSERT_NE(nullptr, extractedDocDrawable); |
| 137 | el = xml::findRootElement(extractedDocDrawable); |
| 138 | ASSERT_NE(nullptr, el); |
| 139 | EXPECT_EQ("vector", el->name); |
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | } // namespace aapt |