Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 30 | using namespace android; |
| 31 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
| 34 | constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
| 35 | |
| 36 | class XmlFlattenerTest : public ::testing::Test { |
| 37 | public: |
| 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 Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 45 | util::make_unique<Attribute>(false, ResTable_map::TYPE_ANY)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | |
| 47 | table->addResource(ResourceName{ {}, ResourceType::kId, u"test" }, |
| 48 | ResourceId{ 0x01020000 }, {}, {}, util::make_unique<Id>()); |
| 49 | |
| 50 | mFlattener = std::make_shared<XmlFlattener>( |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 51 | std::make_shared<Resolver>(table, std::make_shared<AssetManager>())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 54 | ::testing::AssertionResult testFlatten(std::istream& in, ResXMLTree* outTree) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | 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 Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 64 | if (outTree->setTo(data.get(), outBuffer.size(), true) != NO_ERROR) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | return ::testing::AssertionFailure(); |
| 66 | } |
| 67 | return ::testing::AssertionSuccess(); |
| 68 | } |
| 69 | |
| 70 | std::shared_ptr<XmlFlattener> mFlattener; |
| 71 | }; |
| 72 | |
| 73 | TEST_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 Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 79 | ResXMLTree tree; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | ASSERT_TRUE(testFlatten(input, &tree)); |
| 81 | |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 82 | while (tree.next() != ResXMLTree::END_DOCUMENT) { |
| 83 | ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | } // namespace aapt |