TableProtoSerializer_test.cpp revision 59e04c6f92da584b322c87072f18e6cab4de4c60
1/*
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 "ResourceTable.h"
18#include "proto/ProtoSerialize.h"
19#include "test/Builders.h"
20#include "test/Common.h"
21#include "test/Context.h"
22
23#include <gtest/gtest.h>
24
25namespace aapt {
26
27TEST(TableProtoSerializer, SerializeSinglePackage) {
28    std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
29    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
30            .setPackageId(u"com.app.a", 0x7f)
31            .addFileReference(u"@com.app.a:layout/main", ResourceId(0x7f020000),
32                              u"res/layout/main.xml")
33            .addReference(u"@com.app.a:layout/other", ResourceId(0x7f020001),
34                          u"@com.app.a:layout/main")
35            .addString(u"@com.app.a:string/text", {}, u"hi")
36            .addValue(u"@com.app.a:id/foo", {}, util::make_unique<Id>())
37            .build();
38
39    Symbol publicSymbol;
40    publicSymbol.state = SymbolState::kPublic;
41    ASSERT_TRUE(table->setSymbolState(test::parseNameOrDie(u"@com.app.a:layout/main"),
42                                      ResourceId(0x7f020000),
43                                      publicSymbol, context->getDiagnostics()));
44
45    Id* id = test::getValue<Id>(table.get(), u"@com.app.a:id/foo");
46    ASSERT_NE(nullptr, id);
47
48    // Make a plural.
49    std::unique_ptr<Plural> plural = util::make_unique<Plural>();
50    plural->values[Plural::One] = util::make_unique<String>(table->stringPool.makeRef(u"one"));
51    ASSERT_TRUE(table->addResource(test::parseNameOrDie(u"@com.app.a:plurals/hey"),
52                                   ConfigDescription{}, std::move(plural),
53                                   context->getDiagnostics()));
54
55    std::unique_ptr<pb::ResourceTable> pbTable = serializeTableToPb(table.get());
56    ASSERT_NE(nullptr, pbTable);
57
58    std::unique_ptr<ResourceTable> newTable = deserializeTableFromPb(*pbTable,
59                                                                     Source{ "test" },
60                                                                     context->getDiagnostics());
61    ASSERT_NE(nullptr, newTable);
62
63    Id* newId = test::getValue<Id>(newTable.get(), u"@com.app.a:id/foo");
64    ASSERT_NE(nullptr, newId);
65    EXPECT_EQ(id->isWeak(), newId->isWeak());
66
67    Maybe<ResourceTable::SearchResult> result = newTable->findResource(
68            test::parseNameOrDie(u"@com.app.a:layout/main"));
69    AAPT_ASSERT_TRUE(result);
70    EXPECT_EQ(SymbolState::kPublic, result.value().type->symbolStatus.state);
71    EXPECT_EQ(SymbolState::kPublic, result.value().entry->symbolStatus.state);
72}
73
74TEST(TableProtoSerializer, SerializeFileHeader) {
75    std::unique_ptr<IAaptContext> context = test::ContextBuilder().build();
76
77    ResourceFile f;
78    f.config = test::parseConfigOrDie("hdpi-v9");
79    f.name = test::parseNameOrDie(u"@com.app.a:layout/main");
80    f.source.path = "res/layout-hdpi-v9/main.xml";
81    f.exportedSymbols.push_back(SourcedResourceName{ test::parseNameOrDie(u"@+id/unchecked"), 23u });
82
83    const std::string expectedData = "1234";
84
85    std::unique_ptr<pb::CompiledFile> pbFile = serializeCompiledFileToPb(f);
86
87    std::string outputStr;
88    {
89        google::protobuf::io::StringOutputStream outStream(&outputStr);
90        CompiledFileOutputStream outFileStream(&outStream, pbFile.get());
91
92        ASSERT_TRUE(outFileStream.Write(expectedData.data(), expectedData.size()));
93        ASSERT_TRUE(outFileStream.Finish());
94    }
95
96    CompiledFileInputStream inFileStream(outputStr.data(), outputStr.size());
97    const pb::CompiledFile* newPbFile = inFileStream.CompiledFile();
98    ASSERT_NE(nullptr, newPbFile);
99
100    std::unique_ptr<ResourceFile> file = deserializeCompiledFileFromPb(*newPbFile, Source{ "test" },
101                                                                       context->getDiagnostics());
102    ASSERT_NE(nullptr, file);
103
104    std::string actualData((const char*)inFileStream.data(), inFileStream.size());
105    EXPECT_EQ(expectedData, actualData);
106    EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(inFileStream.data()) & 0x03);
107
108    ASSERT_EQ(1u, file->exportedSymbols.size());
109    EXPECT_EQ(test::parseNameOrDie(u"@+id/unchecked"), file->exportedSymbols[0].name);
110}
111
112} // namespace aapt
113