blob: 031801e1dd2fc73793c16f7546d9192ff973fe6f [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 Lesinski75f3a552015-06-03 14:54:23 -070024namespace aapt {
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026constexpr const char* kXmlPreamble =
27 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski75f3a552015-06-03 14:54:23 -070028
29TEST(XmlDomTest, Inflate) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 std::stringstream in(kXmlPreamble);
Adam Lesinskic8956882017-06-29 17:53:36 -070031 in << R"(
32 <Layout xmlns:android="http://schemas.android.com/apk/res/android"
33 android:layout_width="match_parent"
34 android:layout_height="wrap_content">
35 <TextView android:id="@+id/id"
36 android:layout_width="wrap_content"
37 android:layout_height="wrap_content" />
38 </Layout>)";
Adam Lesinski75f3a552015-06-03 14:54:23 -070039
Adam Lesinskic8956882017-06-29 17:53:36 -070040 const Source source("test.xml");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 StdErrDiagnostics diag;
42 std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, source);
43 ASSERT_NE(doc, nullptr);
Adam Lesinski75f3a552015-06-03 14:54:23 -070044
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 xml::Namespace* ns = xml::NodeCast<xml::Namespace>(doc->root.get());
46 ASSERT_NE(ns, nullptr);
47 EXPECT_EQ(ns->namespace_uri, xml::kSchemaAndroid);
48 EXPECT_EQ(ns->namespace_prefix, "android");
Adam Lesinski75f3a552015-06-03 14:54:23 -070049}
50
Adam Lesinski48448e82017-04-26 15:13:52 -070051// Escaping is handled after parsing of the values for resource-specific values.
52TEST(XmlDomTest, ForwardEscapes) {
Adam Lesinskic8956882017-06-29 17:53:36 -070053 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(
54 <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)");
Adam Lesinskiac6edc52017-03-02 19:31:28 -080055
56 xml::Element* el = xml::FindRootElement(doc->root.get());
57 ASSERT_NE(nullptr, el);
58
59 xml::Attribute* attr = el->FindAttribute({}, "pattern");
60 ASSERT_NE(nullptr, attr);
Adam Lesinski48448e82017-04-26 15:13:52 -070061 EXPECT_EQ("\\\\d{5}", attr->value);
Adam Lesinskiac6edc52017-03-02 19:31:28 -080062
Adam Lesinski48448e82017-04-26 15:13:52 -070063 attr = el->FindAttribute({}, "value");
64 ASSERT_NE(nullptr, attr);
65 EXPECT_EQ("\\?hello", attr->value);
Adam Lesinskiac6edc52017-03-02 19:31:28 -080066
Adam Lesinskiac6edc52017-03-02 19:31:28 -080067 xml::Text* text = xml::NodeCast<xml::Text>(el->children[0].get());
68 ASSERT_NE(nullptr, text);
Adam Lesinski48448e82017-04-26 15:13:52 -070069 EXPECT_EQ("\\\\d{5}", text->text);
Adam Lesinskiac6edc52017-03-02 19:31:28 -080070}
71
Adam Lesinskic8956882017-06-29 17:53:36 -070072TEST(XmlDomTest, XmlEscapeSequencesAreParsed) {
73 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(<element value="&quot;" />)");
74
75 xml::Element* el = xml::FindRootElement(doc.get());
76 ASSERT_NE(nullptr, el);
77
78 xml::Attribute* attr = el->FindAttribute({}, "value");
79 ASSERT_NE(nullptr, attr);
80 EXPECT_EQ("\"", attr->value);
81}
82
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083} // namespace aapt