Home | History | Annotate | Download | only in link
      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 
     19 #include "test/Test.h"
     20 
     21 namespace aapt {
     22 
     23 class XmlUriTestVisitor : public xml::Visitor {
     24  public:
     25   XmlUriTestVisitor() = default;
     26 
     27   void Visit(xml::Element* el) override {
     28     for (const auto& attr : el->attributes) {
     29       EXPECT_EQ(std::string(), attr.namespace_uri);
     30     }
     31     EXPECT_EQ(std::string(), el->namespace_uri);
     32     xml::Visitor::Visit(el);
     33   }
     34 
     35   void Visit(xml::Namespace* ns) override {
     36     EXPECT_EQ(std::string(), ns->namespace_uri);
     37     xml::Visitor::Visit(ns);
     38   }
     39 
     40  private:
     41   DISALLOW_COPY_AND_ASSIGN(XmlUriTestVisitor);
     42 };
     43 
     44 class XmlNamespaceTestVisitor : public xml::Visitor {
     45  public:
     46   XmlNamespaceTestVisitor() = default;
     47 
     48   void Visit(xml::Namespace* ns) override {
     49     ADD_FAILURE() << "Detected namespace: " << ns->namespace_prefix << "=\""
     50                   << ns->namespace_uri << "\"";
     51     xml::Visitor::Visit(ns);
     52   }
     53 
     54  private:
     55   DISALLOW_COPY_AND_ASSIGN(XmlNamespaceTestVisitor);
     56 };
     57 
     58 class XmlNamespaceRemoverTest : public ::testing::Test {
     59  public:
     60   void SetUp() override {
     61     context_ =
     62         test::ContextBuilder().SetCompilationPackage("com.app.test").Build();
     63   }
     64 
     65  protected:
     66   std::unique_ptr<IAaptContext> context_;
     67 };
     68 
     69 TEST_F(XmlNamespaceRemoverTest, RemoveUris) {
     70   std::unique_ptr<xml::XmlResource> doc =
     71       test::BuildXmlDomForPackageName(context_.get(), R"EOF(
     72             <View xmlns:android="http://schemas.android.com/apk/res/android"
     73                   android:text="hello" />)EOF");
     74 
     75   XmlNamespaceRemover remover;
     76   ASSERT_TRUE(remover.Consume(context_.get(), doc.get()));
     77 
     78   xml::Node* root = doc.get()->root.get();
     79   ASSERT_NE(root, nullptr);
     80 
     81   XmlUriTestVisitor visitor;
     82   root->Accept(&visitor);
     83 }
     84 
     85 TEST_F(XmlNamespaceRemoverTest, RemoveNamespaces) {
     86   std::unique_ptr<xml::XmlResource> doc =
     87       test::BuildXmlDomForPackageName(context_.get(), R"EOF(
     88             <View xmlns:android="http://schemas.android.com/apk/res/android"
     89                   xmlns:foo="http://schemas.android.com/apk/res/foo"
     90                   foo:bar="foobar"
     91                   android:text="hello" />)EOF");
     92 
     93   XmlNamespaceRemover remover;
     94   ASSERT_TRUE(remover.Consume(context_.get(), doc.get()));
     95 
     96   xml::Node* root = doc.get()->root.get();
     97   ASSERT_NE(root, nullptr);
     98 
     99   XmlNamespaceTestVisitor visitor;
    100   root->Accept(&visitor);
    101 }
    102 
    103 TEST_F(XmlNamespaceRemoverTest, RemoveNestedNamespaces) {
    104   std::unique_ptr<xml::XmlResource> doc =
    105       test::BuildXmlDomForPackageName(context_.get(), R"EOF(
    106             <View xmlns:android="http://schemas.android.com/apk/res/android"
    107                   android:text="hello">
    108               <View xmlns:foo="http://schemas.example.com/foo"
    109                     android:text="foo"/>
    110             </View>)EOF");
    111 
    112   XmlNamespaceRemover remover;
    113   ASSERT_TRUE(remover.Consume(context_.get(), doc.get()));
    114 
    115   xml::Node* root = doc.get()->root.get();
    116   ASSERT_NE(root, nullptr);
    117 
    118   XmlNamespaceTestVisitor visitor;
    119   root->Accept(&visitor);
    120 }
    121 
    122 }  // namespace aapt
    123