blob: d2daaeeca7db88198ae0360455e158f8e115e66c [file] [log] [blame]
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -07001/*
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
20namespace aapt {
21
22class XmlUriTestVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070023 public:
24 void visit(xml::Element* el) override {
25 for (const auto& attr : el->attributes) {
26 EXPECT_EQ(std::string(), attr.namespaceUri);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070027 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 EXPECT_EQ(std::string(), el->namespaceUri);
29 xml::Visitor::visit(el);
30 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070031
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 void visit(xml::Namespace* ns) override {
33 EXPECT_EQ(std::string(), ns->namespaceUri);
34 xml::Visitor::visit(ns);
35 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070036};
37
38class XmlNamespaceTestVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 public:
40 void visit(xml::Namespace* ns) override {
41 ADD_FAILURE() << "Detected namespace: " << ns->namespacePrefix << "=\""
42 << ns->namespaceUri << "\"";
43 xml::Visitor::visit(ns);
44 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070045};
46
47class XmlNamespaceRemoverTest : public ::testing::Test {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 public:
49 void SetUp() override {
50 mContext =
51 test::ContextBuilder().setCompilationPackage("com.app.test").build();
52 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 protected:
55 std::unique_ptr<IAaptContext> mContext;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070056};
57
58TEST_F(XmlNamespaceRemoverTest, RemoveUris) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 std::unique_ptr<xml::XmlResource> doc =
60 test::buildXmlDomForPackageName(mContext.get(), R"EOF(
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070061 <View xmlns:android="http://schemas.android.com/apk/res/android"
62 android:text="hello" />)EOF");
63
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 XmlNamespaceRemover remover;
65 ASSERT_TRUE(remover.consume(mContext.get(), doc.get()));
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 xml::Node* root = doc.get()->root.get();
68 ASSERT_NE(root, nullptr);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 XmlUriTestVisitor visitor;
71 root->accept(&visitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070072}
73
74TEST_F(XmlNamespaceRemoverTest, RemoveNamespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 std::unique_ptr<xml::XmlResource> doc =
76 test::buildXmlDomForPackageName(mContext.get(), R"EOF(
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070077 <View xmlns:android="http://schemas.android.com/apk/res/android"
78 xmlns:foo="http://schemas.android.com/apk/res/foo"
79 foo:bar="foobar"
80 android:text="hello" />)EOF");
81
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 XmlNamespaceRemover remover;
83 ASSERT_TRUE(remover.consume(mContext.get(), doc.get()));
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 xml::Node* root = doc.get()->root.get();
86 ASSERT_NE(root, nullptr);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 XmlNamespaceTestVisitor visitor;
89 root->accept(&visitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070090}
91
92TEST_F(XmlNamespaceRemoverTest, RemoveNestedNamespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 std::unique_ptr<xml::XmlResource> doc =
94 test::buildXmlDomForPackageName(mContext.get(), R"EOF(
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070095 <View xmlns:android="http://schemas.android.com/apk/res/android"
96 android:text="hello">
97 <View xmlns:foo="http://schemas.example.com/foo"
98 android:text="foo"/>
99 </View>)EOF");
100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 XmlNamespaceRemover remover;
102 ASSERT_TRUE(remover.consume(mContext.get(), doc.get()));
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 xml::Node* root = doc.get()->root.get();
105 ASSERT_NE(root, nullptr);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 XmlNamespaceTestVisitor visitor;
108 root->accept(&visitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700109}
110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111} // namespace aapt