blob: df4920fde37f27536fdb13c31f05e85078ef91dd [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
Adam Lesinski6b372992017-08-09 10:54:23 -070021using ::testing::NotNull;
22using ::testing::SizeIs;
23using ::testing::StrEq;
24
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070025namespace aapt {
26
27class XmlUriTestVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 XmlUriTestVisitor() = default;
30
31 void Visit(xml::Element* el) override {
Adam Lesinski6b372992017-08-09 10:54:23 -070032 EXPECT_THAT(el->namespace_decls, SizeIs(0u));
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070033
Adam Lesinski6b372992017-08-09 10:54:23 -070034 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);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041
42 private:
43 DISALLOW_COPY_AND_ASSIGN(XmlUriTestVisitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070044};
45
46class XmlNamespaceTestVisitor : public xml::Visitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 XmlNamespaceTestVisitor() = default;
49
Adam Lesinski6b372992017-08-09 10:54:23 -070050 void Visit(xml::Element* el) override {
51 EXPECT_THAT(el->namespace_decls, SizeIs(0u));
52 xml::Visitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054
55 private:
56 DISALLOW_COPY_AND_ASSIGN(XmlNamespaceTestVisitor);
Alexandria Cornwalla7cc3f12016-08-16 13:33:32 -070057};
58
59class XmlNamespaceRemoverTest : public ::testing::Test {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 public:
61 void SetUp() override {
Adam Lesinski6b372992017-08-09 10:54:23 -070062 context_ = 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 Lesinski6b372992017-08-09 10:54:23 -070078 xml::Node* root = doc->root.get();
79 ASSERT_THAT(root, NotNull());
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 Lesinski6b372992017-08-09 10:54:23 -070096 xml::Node* root = doc->root.get();
97 ASSERT_THAT(root, NotNull());
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 Lesinski6b372992017-08-09 10:54:23 -0700115 xml::Node* root = doc->root.get();
116 ASSERT_THAT(root, NotNull());
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