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 | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 24 | namespace aapt { |
| 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | constexpr const char* kXmlPreamble = |
| 27 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 28 | |
| 29 | TEST(XmlDomTest, Inflate) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | std::stringstream in(kXmlPreamble); |
Adam Lesinski | c895688 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 31 | 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 Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | c895688 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 40 | const Source source("test.xml"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | StdErrDiagnostics diag; |
| 42 | std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, source); |
| 43 | ASSERT_NE(doc, nullptr); |
Adam Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | 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 Lesinski | 75f3a55 | 2015-06-03 14:54:23 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 51 | // Escaping is handled after parsing of the values for resource-specific values. |
| 52 | TEST(XmlDomTest, ForwardEscapes) { |
Adam Lesinski | c895688 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 53 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"( |
| 54 | <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)"); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 55 | |
| 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 Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 61 | EXPECT_EQ("\\\\d{5}", attr->value); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 63 | attr = el->FindAttribute({}, "value"); |
| 64 | ASSERT_NE(nullptr, attr); |
| 65 | EXPECT_EQ("\\?hello", attr->value); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 66 | |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 67 | xml::Text* text = xml::NodeCast<xml::Text>(el->children[0].get()); |
| 68 | ASSERT_NE(nullptr, text); |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 69 | EXPECT_EQ("\\\\d{5}", text->text); |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Adam Lesinski | c895688 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 72 | TEST(XmlDomTest, XmlEscapeSequencesAreParsed) { |
| 73 | std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"(<element value=""" />)"); |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | } // namespace aapt |