XmlDom_test.cpp revision 48448e8a310e72eb2846ad0f86672ce4f0b47e47
175f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski/* 275f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * Copyright (C) 2015 The Android Open Source Project 375f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * 475f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 575f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * you may not use this file except in compliance with the License. 675f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * You may obtain a copy of the License at 775f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * 875f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 975f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * 1075f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * Unless required by applicable law or agreed to in writing, software 1175f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 1275f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1375f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * See the License for the specific language governing permissions and 1475f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski * limitations under the License. 1575f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski */ 1675f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 17467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "xml/XmlDom.h" 1875f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 1975f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski#include <sstream> 2075f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski#include <string> 2175f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 22ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include "test/Test.h" 23ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 2475f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinskinamespace aapt { 2575f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 26ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinskiconstexpr const char* kXmlPreamble = 27ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; 2875f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 2975f3a55cc569a9b61f540a85d9828e91bdca5047Adam LesinskiTEST(XmlDomTest, Inflate) { 30ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::stringstream in(kXmlPreamble); 31ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski in << R"EOF( 3275f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski <Layout xmlns:android="http://schemas.android.com/apk/res/android" 3375f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski android:layout_width="match_parent" 3475f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski android:layout_height="wrap_content"> 3575f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski <TextView android:id="@+id/id" 3675f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski android:layout_width="wrap_content" 3775f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski android:layout_height="wrap_content" /> 3875f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski </Layout> 3975f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski )EOF"; 4075f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 41ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski const Source source = {"test.xml"}; 42ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski StdErrDiagnostics diag; 43ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, source); 44ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_NE(doc, nullptr); 4575f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 46ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski xml::Namespace* ns = xml::NodeCast<xml::Namespace>(doc->root.get()); 47ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski ASSERT_NE(ns, nullptr); 48ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(ns->namespace_uri, xml::kSchemaAndroid); 49ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski EXPECT_EQ(ns->namespace_prefix, "android"); 5075f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski} 5175f3a55cc569a9b61f540a85d9828e91bdca5047Adam Lesinski 5248448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski// Escaping is handled after parsing of the values for resource-specific values. 5348448e8a310e72eb2846ad0f86672ce4f0b47e47Adam LesinskiTEST(XmlDomTest, ForwardEscapes) { 5448448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(R"EOF( 5548448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)EOF"); 56ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski 57ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski xml::Element* el = xml::FindRootElement(doc->root.get()); 58ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski ASSERT_NE(nullptr, el); 59ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski 60ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski xml::Attribute* attr = el->FindAttribute({}, "pattern"); 61ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski ASSERT_NE(nullptr, attr); 6248448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski EXPECT_EQ("\\\\d{5}", attr->value); 63ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski 6448448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski attr = el->FindAttribute({}, "value"); 6548448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski ASSERT_NE(nullptr, attr); 6648448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski EXPECT_EQ("\\?hello", attr->value); 67ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski 68ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski ASSERT_EQ(1u, el->children.size()); 69ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski xml::Text* text = xml::NodeCast<xml::Text>(el->children[0].get()); 70ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski ASSERT_NE(nullptr, text); 7148448e8a310e72eb2846ad0f86672ce4f0b47e47Adam Lesinski EXPECT_EQ("\\\\d{5}", text->text); 72ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski} 73ac6edc501b61e14e3b70ccbbd4d8ed112d92b96cAdam Lesinski 74ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski} // namespace aapt 75