blob: 6e248475749a289991b30b461a1b429ed7b3654e [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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 "Resolver.h"
18#include "ResourceTable.h"
19#include "ResourceValues.h"
20#include "SourceXmlPullParser.h"
21#include "Util.h"
22#include "XmlFlattener.h"
23
24#include <androidfw/AssetManager.h>
25#include <androidfw/ResourceTypes.h>
26#include <gtest/gtest.h>
27#include <sstream>
28#include <string>
29
Adam Lesinskica2fc352015-04-03 12:08:26 -070030using namespace android;
31
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
34constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
35
36class XmlFlattenerTest : public ::testing::Test {
37public:
38 virtual void SetUp() override {
39 std::shared_ptr<ResourceTable> table = std::make_shared<ResourceTable>();
40 table->setPackage(u"android");
41 table->setPackageId(0x01);
42
43 table->addResource(ResourceName{ {}, ResourceType::kAttr, u"id" },
44 ResourceId{ 0x01010000 }, {}, {},
Adam Lesinskica2fc352015-04-03 12:08:26 -070045 util::make_unique<Attribute>(false, ResTable_map::TYPE_ANY));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
47 table->addResource(ResourceName{ {}, ResourceType::kId, u"test" },
48 ResourceId{ 0x01020000 }, {}, {}, util::make_unique<Id>());
49
50 mFlattener = std::make_shared<XmlFlattener>(
Adam Lesinskica2fc352015-04-03 12:08:26 -070051 std::make_shared<Resolver>(table, std::make_shared<AssetManager>()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052 }
53
Adam Lesinskica2fc352015-04-03 12:08:26 -070054 ::testing::AssertionResult testFlatten(std::istream& in, ResXMLTree* outTree) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055 std::stringstream input(kXmlPreamble);
56 input << in.rdbuf() << std::endl;
57 std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(input);
58 BigBuffer outBuffer(1024);
59 if (!mFlattener->flatten(Source{ "test" }, xmlParser, &outBuffer, {})) {
60 return ::testing::AssertionFailure();
61 }
62
63 std::unique_ptr<uint8_t[]> data = util::copy(outBuffer);
Adam Lesinskica2fc352015-04-03 12:08:26 -070064 if (outTree->setTo(data.get(), outBuffer.size(), true) != NO_ERROR) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065 return ::testing::AssertionFailure();
66 }
67 return ::testing::AssertionSuccess();
68 }
69
70 std::shared_ptr<XmlFlattener> mFlattener;
71};
72
73TEST_F(XmlFlattenerTest, ParseSimpleView) {
74 std::stringstream input;
75 input << "<View xmlns:android=\"http://schemas.android.com/apk/res/android\"" << std::endl
76 << " android:id=\"@id/test\">" << std::endl
77 << "</View>" << std::endl;
78
Adam Lesinskica2fc352015-04-03 12:08:26 -070079 ResXMLTree tree;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080 ASSERT_TRUE(testFlatten(input, &tree));
81
Adam Lesinskica2fc352015-04-03 12:08:26 -070082 while (tree.next() != ResXMLTree::END_DOCUMENT) {
83 ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084 }
85}
86
87} // namespace aapt