blob: c1aa10b89a2fce2d95f05c570b6fe2e8cb4781d1 [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 Lesinskia45893a2017-05-30 15:19:02 -070024using ::testing::NotNull;
25
Adam Lesinski75f3a552015-06-03 14:54:23 -070026namespace aapt {
27
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028constexpr const char* kXmlPreamble =
29 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
Adam Lesinski75f3a552015-06-03 14:54:23 -070030
31TEST(XmlDomTest, Inflate) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 std::stringstream in(kXmlPreamble);
33 in << R"EOF(
Adam Lesinski75f3a552015-06-03 14:54:23 -070034 <Layout xmlns:android="http://schemas.android.com/apk/res/android"
35 android:layout_width="match_parent"
36 android:layout_height="wrap_content">
37 <TextView android:id="@+id/id"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content" />
40 </Layout>
41 )EOF";
42
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 const Source source = {"test.xml"};
44 StdErrDiagnostics diag;
45 std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, source);
Adam Lesinskia45893a2017-05-30 15:19:02 -070046 ASSERT_THAT(doc, NotNull());
Adam Lesinski75f3a552015-06-03 14:54:23 -070047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 xml::Namespace* ns = xml::NodeCast<xml::Namespace>(doc->root.get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070049 ASSERT_THAT(ns, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 EXPECT_EQ(ns->namespace_uri, xml::kSchemaAndroid);
51 EXPECT_EQ(ns->namespace_prefix, "android");
Adam Lesinski75f3a552015-06-03 14:54:23 -070052}
53
Adam Lesinski48448e82017-04-26 15:13:52 -070054// Escaping is handled after parsing of the values for resource-specific values.
55TEST(XmlDomTest, ForwardEscapes) {
56 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"EOF(
57 <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)EOF");
Adam Lesinskiac6edc52017-03-02 19:31:28 -080058
59 xml::Element* el = xml::FindRootElement(doc->root.get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070060 ASSERT_THAT(el, NotNull());
Adam Lesinskiac6edc52017-03-02 19:31:28 -080061
62 xml::Attribute* attr = el->FindAttribute({}, "pattern");
Adam Lesinskia45893a2017-05-30 15:19:02 -070063 ASSERT_THAT(attr, NotNull());
Adam Lesinski48448e82017-04-26 15:13:52 -070064 EXPECT_EQ("\\\\d{5}", attr->value);
Adam Lesinskiac6edc52017-03-02 19:31:28 -080065
Adam Lesinski48448e82017-04-26 15:13:52 -070066 attr = el->FindAttribute({}, "value");
Adam Lesinskia45893a2017-05-30 15:19:02 -070067 ASSERT_THAT(attr, NotNull());
Adam Lesinski48448e82017-04-26 15:13:52 -070068 EXPECT_EQ("\\?hello", attr->value);
Adam Lesinskiac6edc52017-03-02 19:31:28 -080069
70 ASSERT_EQ(1u, el->children.size());
Adam Lesinskiac6edc52017-03-02 19:31:28 -080071 xml::Text* text = xml::NodeCast<xml::Text>(el->children[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -070072 ASSERT_THAT(text, NotNull());
Adam Lesinski48448e82017-04-26 15:13:52 -070073 EXPECT_EQ("\\\\d{5}", text->text);
Adam Lesinskiac6edc52017-03-02 19:31:28 -080074}
75
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076} // namespace aapt