blob: f0122e8c617a39bd27ad809daf2701adc660a4ad [file] [log] [blame]
Adam Lesinski75f3a552015-06-03 14:54:23 -07001/*
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
Adam Lesinski467f1712015-11-16 17:35:44 -080017#include "xml/XmlDom.h"
Adam Lesinski75f3a552015-06-03 14:54:23 -070018
Adam Lesinski75f3a552015-06-03 14:54:23 -070019#include <sstream>
20#include <string>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "test/Test.h"
23
Adam Lesinskifba0cf22017-06-29 17:53:36 -070024using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070025using ::testing::NotNull;
Adam Lesinskifba0cf22017-06-29 17:53:36 -070026using ::testing::SizeIs;
Adam Lesinskia45893a2017-05-30 15:19:02 -070027
Adam Lesinski75f3a552015-06-03 14:54:23 -070028namespace aapt {
29
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030constexpr const char* kXmlPreamble =
31 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski75f3a552015-06-03 14:54:23 -070032
33TEST(XmlDomTest, Inflate) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 std::stringstream in(kXmlPreamble);
Adam Lesinskifba0cf22017-06-29 17:53:36 -070035 in << R"(
36 <Layout xmlns:android="http://schemas.android.com/apk/res/android"
37 android:layout_width="match_parent"
38 android:layout_height="wrap_content">
39 <TextView android:id="@+id/id"
40 android:layout_width="wrap_content"
41 android:layout_height="wrap_content" />
42 </Layout>)";
Adam Lesinski75f3a552015-06-03 14:54:23 -070043
Adam Lesinskifba0cf22017-06-29 17:53:36 -070044 const Source source("test.xml");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 StdErrDiagnostics diag;
46 std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, source);
Adam Lesinskia45893a2017-05-30 15:19:02 -070047 ASSERT_THAT(doc, NotNull());
Adam Lesinski75f3a552015-06-03 14:54:23 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 xml::Namespace* ns = xml::NodeCast<xml::Namespace>(doc->root.get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070050 ASSERT_THAT(ns, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070051 EXPECT_THAT(ns->namespace_uri, Eq(xml::kSchemaAndroid));
52 EXPECT_THAT(ns->namespace_prefix, Eq("android"));
Adam Lesinski75f3a552015-06-03 14:54:23 -070053}
54
Adam Lesinski48448e82017-04-26 15:13:52 -070055// Escaping is handled after parsing of the values for resource-specific values.
56TEST(XmlDomTest, ForwardEscapes) {
Adam Lesinskifba0cf22017-06-29 17:53:36 -070057 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(
58 <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)");
Adam Lesinskiac6edc52017-03-02 19:31:28 -080059
Adam Lesinskifba0cf22017-06-29 17:53:36 -070060 xml::Element* el = xml::FindRootElement(doc.get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070061 ASSERT_THAT(el, NotNull());
Adam Lesinskiac6edc52017-03-02 19:31:28 -080062
63 xml::Attribute* attr = el->FindAttribute({}, "pattern");
Adam Lesinskia45893a2017-05-30 15:19:02 -070064 ASSERT_THAT(attr, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070065 EXPECT_THAT(attr->value, Eq("\\\\d{5}"));
Adam Lesinskiac6edc52017-03-02 19:31:28 -080066
Adam Lesinski48448e82017-04-26 15:13:52 -070067 attr = el->FindAttribute({}, "value");
Adam Lesinskia45893a2017-05-30 15:19:02 -070068 ASSERT_THAT(attr, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070069 EXPECT_THAT(attr->value, Eq("\\?hello"));
Adam Lesinskiac6edc52017-03-02 19:31:28 -080070
Adam Lesinskifba0cf22017-06-29 17:53:36 -070071 ASSERT_THAT(el->children, SizeIs(1u));
72
Adam Lesinskiac6edc52017-03-02 19:31:28 -080073 xml::Text* text = xml::NodeCast<xml::Text>(el->children[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070074 ASSERT_THAT(text, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070075 EXPECT_THAT(text->text, Eq("\\\\d{5}"));
76}
77
78TEST(XmlDomTest, XmlEscapeSequencesAreParsed) {
79 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(<element value="&quot;" />)");
80
81 xml::Element* el = xml::FindRootElement(doc.get());
82 ASSERT_THAT(el, NotNull());
83
84 xml::Attribute* attr = el->FindAttribute({}, "value");
85 ASSERT_THAT(attr, NotNull());
86
87 EXPECT_THAT(attr->value, Eq("\""));
Adam Lesinskiac6edc52017-03-02 19:31:28 -080088}
89
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090} // namespace aapt