blob: 79030be5b38c3a662f4a763118b18a5b237f8b20 [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
30namespace aapt {
31
32constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
33
34class XmlFlattenerTest : public ::testing::Test {
35public:
36 virtual void SetUp() override {
37 std::shared_ptr<ResourceTable> table = std::make_shared<ResourceTable>();
38 table->setPackage(u"android");
39 table->setPackageId(0x01);
40
41 table->addResource(ResourceName{ {}, ResourceType::kAttr, u"id" },
42 ResourceId{ 0x01010000 }, {}, {},
43 util::make_unique<Attribute>(false, android::ResTable_map::TYPE_ANY));
44
45 table->addResource(ResourceName{ {}, ResourceType::kId, u"test" },
46 ResourceId{ 0x01020000 }, {}, {}, util::make_unique<Id>());
47
48 mFlattener = std::make_shared<XmlFlattener>(
49 std::make_shared<Resolver>(table, std::make_shared<android::AssetManager>()));
50 }
51
52 ::testing::AssertionResult testFlatten(std::istream& in, android::ResXMLTree* outTree) {
53 std::stringstream input(kXmlPreamble);
54 input << in.rdbuf() << std::endl;
55 std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(input);
56 BigBuffer outBuffer(1024);
57 if (!mFlattener->flatten(Source{ "test" }, xmlParser, &outBuffer, {})) {
58 return ::testing::AssertionFailure();
59 }
60
61 std::unique_ptr<uint8_t[]> data = util::copy(outBuffer);
62 if (outTree->setTo(data.get(), outBuffer.size(), true) != android::NO_ERROR) {
63 return ::testing::AssertionFailure();
64 }
65 return ::testing::AssertionSuccess();
66 }
67
68 std::shared_ptr<XmlFlattener> mFlattener;
69};
70
71TEST_F(XmlFlattenerTest, ParseSimpleView) {
72 std::stringstream input;
73 input << "<View xmlns:android=\"http://schemas.android.com/apk/res/android\"" << std::endl
74 << " android:id=\"@id/test\">" << std::endl
75 << "</View>" << std::endl;
76
77 android::ResXMLTree tree;
78 ASSERT_TRUE(testFlatten(input, &tree));
79
80 while (tree.next() != android::ResXMLTree::END_DOCUMENT) {
81 ASSERT_NE(tree.getEventType(), android::ResXMLTree::BAD_DOCUMENT);
82 }
83}
84
85} // namespace aapt