2016-08-25 12:26:56 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "compile/InlineXmlFormatParser.h"
|
2016-10-21 17:56:45 -07:00
|
|
|
|
2016-08-25 12:26:56 -07:00
|
|
|
#include "test/Test.h"
|
|
|
|
|
2017-08-09 10:54:23 -07:00
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::IsNull;
|
|
|
|
using ::testing::Not;
|
|
|
|
using ::testing::NotNull;
|
|
|
|
using ::testing::SizeIs;
|
|
|
|
using ::testing::StrEq;
|
|
|
|
|
2016-08-25 12:26:56 -07:00
|
|
|
namespace aapt {
|
|
|
|
|
|
|
|
TEST(InlineXmlFormatParserTest, PassThrough) {
|
2016-10-21 17:56:45 -07:00
|
|
|
std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
|
2017-08-09 10:54:23 -07:00
|
|
|
std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(
|
2016-08-25 12:26:56 -07:00
|
|
|
<View xmlns:android="http://schemas.android.com/apk/res/android">
|
|
|
|
<View android:text="hey">
|
|
|
|
<View android:id="hi" />
|
|
|
|
</View>
|
2017-08-09 10:54:23 -07:00
|
|
|
</View>)");
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
InlineXmlFormatParser parser;
|
2016-10-21 17:56:45 -07:00
|
|
|
ASSERT_TRUE(parser.Consume(context.get(), doc.get()));
|
2017-08-09 10:54:23 -07:00
|
|
|
EXPECT_THAT(parser.GetExtractedInlineXmlDocuments(), SizeIs(0u));
|
2016-08-25 12:26:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(InlineXmlFormatParserTest, ExtractOneXmlResource) {
|
2016-10-21 17:56:45 -07:00
|
|
|
std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
|
2017-08-09 10:54:23 -07:00
|
|
|
std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(
|
2016-08-25 12:26:56 -07:00
|
|
|
<View1 xmlns:android="http://schemas.android.com/apk/res/android"
|
|
|
|
xmlns:aapt="http://schemas.android.com/aapt">
|
|
|
|
<aapt:attr name="android:text">
|
|
|
|
<View2 android:text="hey">
|
|
|
|
<View3 android:id="hi" />
|
|
|
|
</View2>
|
|
|
|
</aapt:attr>
|
2017-08-09 10:54:23 -07:00
|
|
|
</View1>)");
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
doc->file.name = test::ParseNameOrDie("layout/main");
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
InlineXmlFormatParser parser;
|
2016-10-21 17:56:45 -07:00
|
|
|
ASSERT_TRUE(parser.Consume(context.get(), doc.get()));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// One XML resource should have been extracted.
|
2017-08-09 10:54:23 -07:00
|
|
|
EXPECT_THAT(parser.GetExtractedInlineXmlDocuments(), SizeIs(1u));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2017-08-09 10:54:23 -07:00
|
|
|
xml::Element* el = doc->root.get();
|
|
|
|
ASSERT_THAT(el, NotNull());
|
|
|
|
EXPECT_THAT(el->name, StrEq("View1"));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// The <aapt:attr> tag should be extracted.
|
2017-08-09 10:54:23 -07:00
|
|
|
EXPECT_THAT(el->FindChild(xml::kSchemaAapt, "attr"), IsNull());
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// The 'android:text' attribute should be set with a reference.
|
2016-10-21 17:56:45 -07:00
|
|
|
xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "text");
|
2017-08-09 10:54:23 -07:00
|
|
|
ASSERT_THAT(attr, NotNull());
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
ResourceNameRef name_ref;
|
|
|
|
ASSERT_TRUE(ResourceUtils::ParseReference(attr->value, &name_ref));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2017-08-09 10:54:23 -07:00
|
|
|
xml::XmlResource* extracted_doc = parser.GetExtractedInlineXmlDocuments()[0].get();
|
|
|
|
ASSERT_THAT(extracted_doc, NotNull());
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// Make sure the generated reference is correct.
|
2017-08-09 10:54:23 -07:00
|
|
|
EXPECT_THAT(extracted_doc->file.name, Eq(name_ref));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// Verify the structure of the extracted XML.
|
2017-08-09 10:54:23 -07:00
|
|
|
el = extracted_doc->root.get();
|
|
|
|
ASSERT_THAT(el, NotNull());
|
|
|
|
EXPECT_THAT(el->name, StrEq("View2"));
|
|
|
|
EXPECT_THAT(el->FindChild({}, "View3"), NotNull());
|
2016-08-25 12:26:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(InlineXmlFormatParserTest, ExtractTwoXmlResources) {
|
2016-10-21 17:56:45 -07:00
|
|
|
std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
|
2017-08-09 10:54:23 -07:00
|
|
|
std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(
|
2016-08-25 12:26:56 -07:00
|
|
|
<View1 xmlns:android="http://schemas.android.com/apk/res/android"
|
|
|
|
xmlns:aapt="http://schemas.android.com/aapt">
|
|
|
|
<aapt:attr name="android:text">
|
|
|
|
<View2 android:text="hey">
|
|
|
|
<View3 android:id="hi" />
|
|
|
|
</View2>
|
|
|
|
</aapt:attr>
|
|
|
|
|
|
|
|
<aapt:attr name="android:drawable">
|
|
|
|
<vector />
|
|
|
|
</aapt:attr>
|
2017-08-09 10:54:23 -07:00
|
|
|
</View1>)");
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
doc->file.name = test::ParseNameOrDie("layout/main");
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
InlineXmlFormatParser parser;
|
2016-10-21 17:56:45 -07:00
|
|
|
ASSERT_TRUE(parser.Consume(context.get(), doc.get()));
|
2017-08-09 10:54:23 -07:00
|
|
|
ASSERT_THAT(parser.GetExtractedInlineXmlDocuments(), SizeIs(2u));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2017-08-09 10:54:23 -07:00
|
|
|
xml::Element* el = doc->root.get();
|
|
|
|
ASSERT_THAT(el, NotNull());
|
|
|
|
EXPECT_THAT(el->name, StrEq("View1"));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
xml::Attribute* attr_text = el->FindAttribute(xml::kSchemaAndroid, "text");
|
2017-08-09 10:54:23 -07:00
|
|
|
ASSERT_THAT(attr_text, NotNull());
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2017-08-09 10:54:23 -07:00
|
|
|
xml::Attribute* attr_drawable = el->FindAttribute(xml::kSchemaAndroid, "drawable");
|
|
|
|
ASSERT_THAT(attr_drawable, NotNull());
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// The two extracted resources should have different names.
|
2017-08-09 10:54:23 -07:00
|
|
|
EXPECT_THAT(attr_text->value, Not(Eq(attr_drawable->value)));
|
2016-08-25 12:26:56 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// The child <aapt:attr> elements should be gone.
|
2017-08-09 10:54:23 -07:00
|
|
|
EXPECT_THAT(el->FindChild(xml::kSchemaAapt, "attr"), IsNull());
|
|
|
|
|
|
|
|
xml::XmlResource* extracted_doc_text = parser.GetExtractedInlineXmlDocuments()[0].get();
|
|
|
|
ASSERT_THAT(extracted_doc_text, NotNull());
|
|
|
|
ASSERT_THAT(extracted_doc_text->root, NotNull());
|
|
|
|
EXPECT_THAT(extracted_doc_text->root->name, StrEq("View2"));
|
|
|
|
|
|
|
|
xml::XmlResource* extracted_doc_drawable = parser.GetExtractedInlineXmlDocuments()[1].get();
|
|
|
|
ASSERT_THAT(extracted_doc_drawable, NotNull());
|
|
|
|
ASSERT_THAT(extracted_doc_drawable->root, NotNull());
|
|
|
|
EXPECT_THAT(extracted_doc_drawable->root->name, StrEq("vector"));
|
2016-08-25 12:26:56 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
} // namespace aapt
|