Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #include "link/Linkers.h" |
| 18 | #include "test/Test.h" |
| 19 | |
| 20 | namespace aapt { |
| 21 | |
| 22 | class XmlUriTestVisitor : public xml::Visitor { |
| 23 | public: |
| 24 | void visit(xml::Element* el) override { |
| 25 | for (const auto& attr : el->attributes) { |
| 26 | EXPECT_EQ(std::string(), attr.namespaceUri); |
| 27 | } |
| 28 | EXPECT_EQ(std::string(), el->namespaceUri); |
| 29 | xml::Visitor::visit(el); |
| 30 | } |
| 31 | |
| 32 | void visit(xml::Namespace* ns) override { |
| 33 | EXPECT_EQ(std::string(), ns->namespaceUri); |
| 34 | xml::Visitor::visit(ns); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | class XmlNamespaceTestVisitor : public xml::Visitor { |
| 39 | public: |
| 40 | void visit(xml::Namespace* ns) override { |
| 41 | ADD_FAILURE() << "Detected namespace: " |
| 42 | << ns->namespacePrefix << "=\"" << ns->namespaceUri << "\""; |
| 43 | xml::Visitor::visit(ns); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | class XmlNamespaceRemoverTest : public ::testing::Test { |
| 48 | public: |
| 49 | void SetUp() override { |
| 50 | mContext = test::ContextBuilder() |
| 51 | .setCompilationPackage("com.app.test") |
| 52 | .build(); |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | std::unique_ptr<IAaptContext> mContext; |
| 57 | }; |
| 58 | |
| 59 | TEST_F(XmlNamespaceRemoverTest, RemoveUris) { |
| 60 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
| 61 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 62 | android:text="hello" />)EOF"); |
| 63 | |
| 64 | XmlNamespaceRemover remover; |
| 65 | ASSERT_TRUE(remover.consume(mContext.get(), doc.get())); |
| 66 | |
| 67 | xml::Node* root = doc.get()->root.get(); |
| 68 | ASSERT_NE(root, nullptr); |
| 69 | |
| 70 | XmlUriTestVisitor visitor; |
| 71 | root->accept(&visitor); |
| 72 | } |
| 73 | |
| 74 | TEST_F(XmlNamespaceRemoverTest, RemoveNamespaces) { |
| 75 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
| 76 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 77 | xmlns:foo="http://schemas.android.com/apk/res/foo" |
| 78 | foo:bar="foobar" |
| 79 | android:text="hello" />)EOF"); |
| 80 | |
| 81 | XmlNamespaceRemover remover; |
| 82 | ASSERT_TRUE(remover.consume(mContext.get(), doc.get())); |
| 83 | |
| 84 | xml::Node* root = doc.get()->root.get(); |
| 85 | ASSERT_NE(root, nullptr); |
| 86 | |
| 87 | XmlNamespaceTestVisitor visitor; |
| 88 | root->accept(&visitor); |
| 89 | } |
| 90 | |
| 91 | TEST_F(XmlNamespaceRemoverTest, RemoveNestedNamespaces) { |
| 92 | std::unique_ptr<xml::XmlResource> doc = test::buildXmlDomForPackageName(mContext.get(), R"EOF( |
| 93 | <View xmlns:android="http://schemas.android.com/apk/res/android" |
| 94 | android:text="hello"> |
| 95 | <View xmlns:foo="http://schemas.example.com/foo" |
| 96 | android:text="foo"/> |
| 97 | </View>)EOF"); |
| 98 | |
| 99 | XmlNamespaceRemover remover; |
| 100 | ASSERT_TRUE(remover.consume(mContext.get(), doc.get())); |
| 101 | |
| 102 | xml::Node* root = doc.get()->root.get(); |
| 103 | ASSERT_NE(root, nullptr); |
| 104 | |
| 105 | XmlNamespaceTestVisitor visitor; |
| 106 | root->accept(&visitor); |
| 107 | } |
| 108 | |
| 109 | } // namespace aapt |