blob: 348796c98c227ef86342271a748333b915478290 [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"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070019#include "test/Test.h"
20
21namespace aapt {
22
23TEST(InlineXmlFormatParserTest, PassThrough) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
25 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"EOF(
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070026 <View xmlns:android="http://schemas.android.com/apk/res/android">
27 <View android:text="hey">
28 <View android:id="hi" />
29 </View>
30 </View>)EOF");
31
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 InlineXmlFormatParser parser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 ASSERT_TRUE(parser.Consume(context.get(), doc.get()));
34 EXPECT_EQ(0u, parser.GetExtractedInlineXmlDocuments().size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070035}
36
37TEST(InlineXmlFormatParserTest, ExtractOneXmlResource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
39 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"EOF(
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070040 <View1 xmlns:android="http://schemas.android.com/apk/res/android"
41 xmlns:aapt="http://schemas.android.com/aapt">
42 <aapt:attr name="android:text">
43 <View2 android:text="hey">
44 <View3 android:id="hi" />
45 </View2>
46 </aapt:attr>
47 </View1>)EOF");
48
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 doc->file.name = test::ParseNameOrDie("layout/main");
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 InlineXmlFormatParser parser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 ASSERT_TRUE(parser.Consume(context.get(), doc.get()));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 // One XML resource should have been extracted.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 EXPECT_EQ(1u, parser.GetExtractedInlineXmlDocuments().size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070056
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 xml::Element* el = xml::FindRootElement(doc.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 ASSERT_NE(nullptr, el);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 EXPECT_EQ("View1", el->name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 // The <aapt:attr> tag should be extracted.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 EXPECT_EQ(nullptr, el->FindChild(xml::kSchemaAapt, "attr"));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 // The 'android:text' attribute should be set with a reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "text");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 ASSERT_NE(nullptr, attr);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 ResourceNameRef name_ref;
70 ASSERT_TRUE(ResourceUtils::ParseReference(attr->value, &name_ref));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 xml::XmlResource* extracted_doc =
73 parser.GetExtractedInlineXmlDocuments()[0].get();
74 ASSERT_NE(nullptr, extracted_doc);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 // Make sure the generated reference is correct.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 EXPECT_EQ(name_ref.package, extracted_doc->file.name.package);
78 EXPECT_EQ(name_ref.type, extracted_doc->file.name.type);
79 EXPECT_EQ(name_ref.entry, extracted_doc->file.name.entry);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 // Verify the structure of the extracted XML.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 el = xml::FindRootElement(extracted_doc);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 ASSERT_NE(nullptr, el);
84 EXPECT_EQ("View2", el->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 EXPECT_NE(nullptr, el->FindChild({}, "View3"));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070086}
87
88TEST(InlineXmlFormatParserTest, ExtractTwoXmlResources) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
90 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"EOF(
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070091 <View1 xmlns:android="http://schemas.android.com/apk/res/android"
92 xmlns:aapt="http://schemas.android.com/aapt">
93 <aapt:attr name="android:text">
94 <View2 android:text="hey">
95 <View3 android:id="hi" />
96 </View2>
97 </aapt:attr>
98
99 <aapt:attr name="android:drawable">
100 <vector />
101 </aapt:attr>
102 </View1>)EOF");
103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 doc->file.name = test::ParseNameOrDie("layout/main");
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 InlineXmlFormatParser parser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 ASSERT_TRUE(parser.Consume(context.get(), doc.get()));
108 ASSERT_EQ(2u, parser.GetExtractedInlineXmlDocuments().size());
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 xml::Element* el = xml::FindRootElement(doc.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 ASSERT_NE(nullptr, el);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 EXPECT_EQ("View1", el->name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 xml::Attribute* attr_text = el->FindAttribute(xml::kSchemaAndroid, "text");
116 ASSERT_NE(nullptr, attr_text);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 xml::Attribute* attr_drawable =
119 el->FindAttribute(xml::kSchemaAndroid, "drawable");
120 ASSERT_NE(nullptr, attr_drawable);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 // The two extracted resources should have different names.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 EXPECT_NE(attr_text->value, attr_drawable->value);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 // The child <aapt:attr> elements should be gone.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 EXPECT_EQ(nullptr, el->FindChild(xml::kSchemaAapt, "attr"));
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 xml::XmlResource* extracted_doc_text =
129 parser.GetExtractedInlineXmlDocuments()[0].get();
130 ASSERT_NE(nullptr, extracted_doc_text);
131 el = xml::FindRootElement(extracted_doc_text);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 ASSERT_NE(nullptr, el);
133 EXPECT_EQ("View2", el->name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 xml::XmlResource* extracted_doc_drawable =
136 parser.GetExtractedInlineXmlDocuments()[1].get();
137 ASSERT_NE(nullptr, extracted_doc_drawable);
138 el = xml::FindRootElement(extracted_doc_drawable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 ASSERT_NE(nullptr, el);
140 EXPECT_EQ("vector", el->name);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700141}
142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143} // namespace aapt