blob: 1340ada6d95385e71ba3e9b4e08b8cad995e0294 [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 <string>
20
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070021#include "io/StringInputStream.h"
Adam Lesinskice5e56e22016-10-21 17:56:45 -070022#include "test/Test.h"
23
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070024using ::aapt::io::StringInputStream;
Adam Lesinskifba0cf22017-06-29 17:53:36 -070025using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070026using ::testing::NotNull;
Adam Lesinskifba0cf22017-06-29 17:53:36 -070027using ::testing::SizeIs;
Adam Lesinskia45893a2017-05-30 15:19:02 -070028
Adam Lesinski75f3a552015-06-03 14:54:23 -070029namespace aapt {
30
Adam Lesinski75f3a552015-06-03 14:54:23 -070031TEST(XmlDomTest, Inflate) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070032 std::string input = R"(<?xml version="1.0" encoding="utf-8"?>
Adam Lesinskifba0cf22017-06-29 17:53:36 -070033 <Layout xmlns:android="http://schemas.android.com/apk/res/android"
34 android:layout_width="match_parent"
35 android:layout_height="wrap_content">
36 <TextView android:id="@+id/id"
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content" />
39 </Layout>)";
Adam Lesinski75f3a552015-06-03 14:54:23 -070040
Adam Lesinskice5e56e22016-10-21 17:56:45 -070041 StdErrDiagnostics diag;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070042 StringInputStream in(input);
43 std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, Source("test.xml"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070044 ASSERT_THAT(doc, NotNull());
Adam Lesinski75f3a552015-06-03 14:54:23 -070045
Adam Lesinskice5e56e22016-10-21 17:56:45 -070046 xml::Namespace* ns = xml::NodeCast<xml::Namespace>(doc->root.get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070047 ASSERT_THAT(ns, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070048 EXPECT_THAT(ns->namespace_uri, Eq(xml::kSchemaAndroid));
49 EXPECT_THAT(ns->namespace_prefix, Eq("android"));
Adam Lesinski75f3a552015-06-03 14:54:23 -070050}
51
Adam Lesinski48448e82017-04-26 15:13:52 -070052// Escaping is handled after parsing of the values for resource-specific values.
53TEST(XmlDomTest, ForwardEscapes) {
Adam Lesinskifba0cf22017-06-29 17:53:36 -070054 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(
55 <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)");
Adam Lesinskiac6edc52017-03-02 19:31:28 -080056
Adam Lesinskifba0cf22017-06-29 17:53:36 -070057 xml::Element* el = xml::FindRootElement(doc.get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070058 ASSERT_THAT(el, NotNull());
Adam Lesinskiac6edc52017-03-02 19:31:28 -080059
60 xml::Attribute* attr = el->FindAttribute({}, "pattern");
Adam Lesinskia45893a2017-05-30 15:19:02 -070061 ASSERT_THAT(attr, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070062 EXPECT_THAT(attr->value, Eq("\\\\d{5}"));
Adam Lesinskiac6edc52017-03-02 19:31:28 -080063
Adam Lesinski48448e82017-04-26 15:13:52 -070064 attr = el->FindAttribute({}, "value");
Adam Lesinskia45893a2017-05-30 15:19:02 -070065 ASSERT_THAT(attr, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070066 EXPECT_THAT(attr->value, Eq("\\?hello"));
Adam Lesinskiac6edc52017-03-02 19:31:28 -080067
Adam Lesinskifba0cf22017-06-29 17:53:36 -070068 ASSERT_THAT(el->children, SizeIs(1u));
69
Adam Lesinskiac6edc52017-03-02 19:31:28 -080070 xml::Text* text = xml::NodeCast<xml::Text>(el->children[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070071 ASSERT_THAT(text, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -070072 EXPECT_THAT(text->text, Eq("\\\\d{5}"));
73}
74
75TEST(XmlDomTest, XmlEscapeSequencesAreParsed) {
76 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(<element value="&quot;" />)");
77
78 xml::Element* el = xml::FindRootElement(doc.get());
79 ASSERT_THAT(el, NotNull());
80
81 xml::Attribute* attr = el->FindAttribute({}, "value");
82 ASSERT_THAT(attr, NotNull());
83
84 EXPECT_THAT(attr->value, Eq("\""));
Adam Lesinskiac6edc52017-03-02 19:31:28 -080085}
86
Adam Lesinskice5e56e22016-10-21 17:56:45 -070087} // namespace aapt