Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 17 | #include "xml/XmlDom.h" |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "test/Test.h" |
| 23 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 24 | using ::testing::Eq; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 25 | using ::testing::NotNull; |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 26 | using ::testing::SizeIs; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 27 | |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 28 | namespace aapt { |
| 29 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | constexpr const char* kXmlPreamble = |
| 31 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 32 | |
| 33 | TEST(XmlDomTest, Inflate) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | std::stringstream in(kXmlPreamble); |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 35 | 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 Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 43 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 44 | const Source source("test.xml"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | StdErrDiagnostics diag; |
| 46 | std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, source); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 47 | ASSERT_THAT(doc, NotNull()); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | xml::Namespace* ns = xml::NodeCast<xml::Namespace>(doc->root.get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 50 | ASSERT_THAT(ns, NotNull()); |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 51 | EXPECT_THAT(ns->namespace_uri, Eq(xml::kSchemaAndroid)); |
| 52 | EXPECT_THAT(ns->namespace_prefix, Eq("android")); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 55 | // Escaping is handled after parsing of the values for resource-specific values. |
| 56 | TEST(XmlDomTest, ForwardEscapes) { |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 57 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"( |
| 58 | <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)"); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 60 | xml::Element* el = xml::FindRootElement(doc.get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 61 | ASSERT_THAT(el, NotNull()); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 62 | |
| 63 | xml::Attribute* attr = el->FindAttribute({}, "pattern"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 64 | ASSERT_THAT(attr, NotNull()); |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 65 | EXPECT_THAT(attr->value, Eq("\\\\d{5}")); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 66 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 67 | attr = el->FindAttribute({}, "value"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 68 | ASSERT_THAT(attr, NotNull()); |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 69 | EXPECT_THAT(attr->value, Eq("\\?hello")); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 70 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 71 | ASSERT_THAT(el->children, SizeIs(1u)); |
| 72 | |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 73 | xml::Text* text = xml::NodeCast<xml::Text>(el->children[0].get()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 74 | ASSERT_THAT(text, NotNull()); |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 75 | EXPECT_THAT(text->text, Eq("\\\\d{5}")); |
| 76 | } |
| 77 | |
| 78 | TEST(XmlDomTest, XmlEscapeSequencesAreParsed) { |
| 79 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(<element value=""" />)"); |
| 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 Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | } // namespace aapt |