blob: a176c03a6432534c7fcc11cdfbc06838360bb01e [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"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070019#include "test/Test.h"
20
21namespace aapt {
22
23class XmlUriTestVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025 XmlUriTestVisitor() = default;
26
27 void Visit(xml::Element* el) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 for (const auto& attr : el->attributes) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 EXPECT_EQ(std::string(), attr.namespace_uri);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070030 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 EXPECT_EQ(std::string(), el->namespace_uri);
32 xml::Visitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 void Visit(xml::Namespace* ns) override {
36 EXPECT_EQ(std::string(), ns->namespace_uri);
37 xml::Visitor::Visit(ns);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039
40 private:
41 DISALLOW_COPY_AND_ASSIGN(XmlUriTestVisitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070042};
43
44class XmlNamespaceTestVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 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);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053
54 private:
55 DISALLOW_COPY_AND_ASSIGN(XmlNamespaceTestVisitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070056};
57
58class XmlNamespaceRemoverTest : public ::testing::Test {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 public:
60 void SetUp() override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 context_ =
62 test::ContextBuilder().SetCompilationPackage("com.app.test").Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 protected:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 std::unique_ptr<IAaptContext> context_;
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070067};
68
69TEST_F(XmlNamespaceRemoverTest, RemoveUris) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 test::BuildXmlDomForPackageName(context_.get(), R"EOF(
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070072 <View xmlns:android="http://schemas.android.com/apk/res/android"
73 android:text="hello" />)EOF");
74
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 XmlNamespaceRemover remover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 ASSERT_TRUE(remover.Consume(context_.get(), doc.get()));
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 xml::Node* root = doc.get()->root.get();
79 ASSERT_NE(root, nullptr);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 XmlUriTestVisitor visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 root->Accept(&visitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070083}
84
85TEST_F(XmlNamespaceRemoverTest, RemoveNamespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 test::BuildXmlDomForPackageName(context_.get(), R"EOF(
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070088 <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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 XmlNamespaceRemover remover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 ASSERT_TRUE(remover.Consume(context_.get(), doc.get()));
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 xml::Node* root = doc.get()->root.get();
97 ASSERT_NE(root, nullptr);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 XmlNamespaceTestVisitor visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 root->Accept(&visitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700101}
102
103TEST_F(XmlNamespaceRemoverTest, RemoveNestedNamespaces) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 test::BuildXmlDomForPackageName(context_.get(), R"EOF(
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700106 <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
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 XmlNamespaceRemover remover;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 ASSERT_TRUE(remover.Consume(context_.get(), doc.get()));
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 xml::Node* root = doc.get()->root.get();
116 ASSERT_NE(root, nullptr);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 XmlNamespaceTestVisitor visitor;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 root->Accept(&visitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -0700120}
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122} // namespace aapt