blob: 3c8b72d3cb2ca0ae0d1ece3bbab9b6b50494efb3 [file] [log] [blame]
Ryan Mitchell479fa392019-01-02 17:15:39 -08001/*
2 * Copyright (C) 2018 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.h"
18
19#include "LoadedApk.h"
20#include "test/Test.h"
21
22using testing::Eq;
23using testing::Ne;
24
25namespace aapt {
26
27using LinkTest = CommandTestFixture;
28
29TEST_F(LinkTest, RemoveRawXmlStrings) {
30 StdErrDiagnostics diag;
31 const std::string compiled_files_dir = GetTestPath("compiled");
32 ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)",
33 compiled_files_dir, &diag));
34
35 const std::string out_apk = GetTestPath("out.apk");
36 std::vector<std::string> link_args = {
37 "--manifest", GetDefaultManifest(),
38 "-o", out_apk,
39 };
40
41 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
42
43 // Load the binary xml tree
44 android::ResXMLTree tree;
45 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
46 AssertLoadXml(apk.get(), "res/xml/test.xml", &tree);
47
48 // Check that the raw string index has not been assigned
49 EXPECT_THAT(tree.getAttributeValueStringID(0), Eq(-1));
50}
51
52TEST_F(LinkTest, KeepRawXmlStrings) {
53 StdErrDiagnostics diag;
54 const std::string compiled_files_dir = GetTestPath("compiled");
55 ASSERT_TRUE(CompileFile(GetTestPath("res/xml/test.xml"), R"(<Item AgentCode="007"/>)",
56 compiled_files_dir, &diag));
57
58 const std::string out_apk = GetTestPath("out.apk");
59 std::vector<std::string> link_args = {
60 "--manifest", GetDefaultManifest(),
61 "-o", out_apk,
62 "--keep-raw-values"
63 };
64
65 ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
66
67 // Load the binary xml tree
68 android::ResXMLTree tree;
69 std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
70 AssertLoadXml(apk.get(), "res/xml/test.xml", &tree);
71
72 // Check that the raw string index has been set to the correct string pool entry
73 int32_t raw_index = tree.getAttributeValueStringID(0);
74 ASSERT_THAT(raw_index, Ne(-1));
75 EXPECT_THAT(util::GetString(tree.getStrings(), static_cast<size_t>(raw_index)), Eq("007"));
76}
77
78} // namespace aapt