blob: 4adb21cf98305b682b2de89b2ade13bb672f7699 [file] [log] [blame]
Adam Lesinski5eeaadd2016-08-25 12:26:56 -07001/*
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
20namespace aapt {
21
22TEST(InlineXmlFormatParserTest, PassThrough) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070023 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
24 std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070025 <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 Lesinskicacb28f2016-10-19 12:18:14 -070031 InlineXmlFormatParser parser;
32 ASSERT_TRUE(parser.consume(context.get(), doc.get()));
33 EXPECT_EQ(0u, parser.getExtractedInlineXmlDocuments().size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070034}
35
36TEST(InlineXmlFormatParserTest, ExtractOneXmlResource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
38 std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070039 <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 Lesinskicacb28f2016-10-19 12:18:14 -070048 doc->file.name = test::parseNameOrDie("layout/main");
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 InlineXmlFormatParser parser;
51 ASSERT_TRUE(parser.consume(context.get(), doc.get()));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 // One XML resource should have been extracted.
54 EXPECT_EQ(1u, parser.getExtractedInlineXmlDocuments().size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 xml::Element* el = xml::findRootElement(doc.get());
57 ASSERT_NE(nullptr, el);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 EXPECT_EQ("View1", el->name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 // The <aapt:attr> tag should be extracted.
62 EXPECT_EQ(nullptr, el->findChild(xml::kSchemaAapt, "attr"));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 // 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 Lesinski5eeaadd2016-08-25 12:26:56 -070067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 ResourceNameRef nameRef;
69 ASSERT_TRUE(ResourceUtils::parseReference(attr->value, &nameRef));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 xml::XmlResource* extractedDoc =
72 parser.getExtractedInlineXmlDocuments()[0].get();
73 ASSERT_NE(nullptr, extractedDoc);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 // 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 Lesinski5eeaadd2016-08-25 12:26:56 -070079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 // 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 Lesinski5eeaadd2016-08-25 12:26:56 -070085}
86
87TEST(InlineXmlFormatParserTest, ExtractTwoXmlResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
89 std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(R"EOF(
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070090 <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 Lesinskicacb28f2016-10-19 12:18:14 -0700103 doc->file.name = test::parseNameOrDie("layout/main");
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 InlineXmlFormatParser parser;
106 ASSERT_TRUE(parser.consume(context.get(), doc.get()));
107 ASSERT_EQ(2u, parser.getExtractedInlineXmlDocuments().size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 xml::Element* el = xml::findRootElement(doc.get());
110 ASSERT_NE(nullptr, el);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 EXPECT_EQ("View1", el->name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 xml::Attribute* attrText = el->findAttribute(xml::kSchemaAndroid, "text");
115 ASSERT_NE(nullptr, attrText);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 xml::Attribute* attrDrawable =
118 el->findAttribute(xml::kSchemaAndroid, "drawable");
119 ASSERT_NE(nullptr, attrDrawable);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 // The two extracted resources should have different names.
122 EXPECT_NE(attrText->value, attrDrawable->value);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 // The child <aapt:attr> elements should be gone.
125 EXPECT_EQ(nullptr, el->findChild(xml::kSchemaAapt, "attr"));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 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 Lesinski5eeaadd2016-08-25 12:26:56 -0700133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 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 Lesinski5eeaadd2016-08-25 12:26:56 -0700140}
141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142} // namespace aapt