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 using ::testing::NotNull;
22 using ::testing::SizeIs;
23 using ::testing::StrEq;
24 
25 namespace aapt {
26 
27 class XmlUriTestVisitor : public xml::Visitor {
28  public:
29   XmlUriTestVisitor() = default;
30 
Visit(xml::Element * el)31   void Visit(xml::Element* el) override {
32     EXPECT_THAT(el->namespace_decls, SizeIs(0u));
33 
34     for (const auto& attr : el->attributes) {
35       EXPECT_THAT(attr.namespace_uri, StrEq(""));
36     }
37     EXPECT_THAT(el->namespace_uri, StrEq(""));
38 
39     xml::Visitor::Visit(el);
40   }
41 
42  private:
43   DISALLOW_COPY_AND_ASSIGN(XmlUriTestVisitor);
44 };
45 
46 class XmlNamespaceTestVisitor : public xml::Visitor {
47  public:
48   XmlNamespaceTestVisitor() = default;
49 
Visit(xml::Element * el)50   void Visit(xml::Element* el) override {
51     EXPECT_THAT(el->namespace_decls, SizeIs(0u));
52     xml::Visitor::Visit(el);
53   }
54 
55  private:
56   DISALLOW_COPY_AND_ASSIGN(XmlNamespaceTestVisitor);
57 };
58 
59 class XmlNamespaceRemoverTest : public ::testing::Test {
60  public:
SetUp()61   void SetUp() override {
62     context_ = test::ContextBuilder().SetCompilationPackage("com.app.test").Build();
63   }
64 
65  protected:
66   std::unique_ptr<IAaptContext> context_;
67 };
68 
TEST_F(XmlNamespaceRemoverTest,RemoveUris)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->root.get();
79   ASSERT_THAT(root, NotNull());
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->root.get();
97   ASSERT_THAT(root, NotNull());
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->root.get();
116   ASSERT_THAT(root, NotNull());
117 
118   XmlNamespaceTestVisitor visitor;
119   root->Accept(&visitor);
120 }
121 
122 }  // namespace aapt
123