blob: 8d0fc54abbc48af13880f98a5d77d03d1986ea7f [file]
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <regex>
#include <sstream>
#include <string>
#include <vector>
#include "Diagnostics.h"
#include "LoadedApk.h"
#include "SdkConstants.h"
#include "cmd/Dump.h"
#include "io/StringStream.h"
#include "test/Common.h"
#include "test/Test.h"
#include "text/Printer.h"
using ::aapt::io::StringOutputStream;
using ::aapt::text::Printer;
using testing::Eq;
using testing::Ne;
namespace aapt {
struct StyleItem {
std::string name;
std::string value;
std::optional<std::string> flag;
};
struct TestResource {
std::string type;
std::string name;
std::string value;
std::optional<std::string> flag;
std::optional<std::string> style_parent;
std::vector<StyleItem> style_items;
std::vector<std::string> extra_attrs;
std::vector<StyleItem> array_items;
};
std::string BuildResourcesXml(const std::vector<TestResource>& resources) {
std::stringstream ss;
ss << "<resources xmlns:android=\"http://schemas.android.com/apk/res/android\">\n";
for (const auto& res : resources) {
ss << " <" << res.type << " name=\"" << res.name << "\"";
for (const auto& attr : res.extra_attrs) {
ss << " " << attr;
}
if (res.style_parent) {
ss << " parent=\"" << *res.style_parent << "\"";
}
if (res.flag) {
ss << " android:featureFlag=\"" << *res.flag << "\"";
}
if (res.type == "style") {
ss << ">\n";
for (const auto& item : res.style_items) {
ss << " <item name=\"" << item.name << "\"";
if (item.flag) {
ss << " android:featureFlag=\"" << *item.flag << "\"";
}
ss << ">" << item.value << "</item>\n";
}
ss << " </style>\n";
} else if (res.type == "string-array" || res.type == "integer-array" || res.type == "array") {
ss << ">\n";
for (const auto& item : res.array_items) {
ss << " <item";
if (item.flag) {
ss << " android:featureFlag=\"" << *item.flag << "\"";
}
ss << ">" << item.value << "</item>\n";
}
ss << " </" << res.type << ">\n";
} else {
ss << ">" << res.value << "</" << res.type << ">\n";
}
}
ss << "</resources>";
return ss.str();
}
using FlaggedResourcesTest = CommandTestFixture;
static android::NoOpDiagnostics noop_diag;
void DumpStringPoolToString(LoadedApk* loaded_apk, std::string* output) {
StringOutputStream output_stream(output);
Printer printer(&output_stream);
DumpStringsCommand command(&printer, &noop_diag);
ASSERT_EQ(command.Dump(loaded_apk), 0);
output_stream.Flush();
}
void DumpResourceTableToString(LoadedApk* loaded_apk, std::string* output) {
StringOutputStream output_stream(output);
Printer printer(&output_stream);
DumpTableCommand command(&printer, &noop_diag);
ASSERT_EQ(command.Dump(loaded_apk), 0);
output_stream.Flush();
}
void DumpChunksToString(LoadedApk* loaded_apk, std::string* output) {
StringOutputStream output_stream(output);
Printer printer(&output_stream);
DumpChunks command(&printer, &noop_diag);
ASSERT_EQ(command.Dump(loaded_apk), 0);
output_stream.Flush();
}
void DumpXmlTreeToString(LoadedApk* loaded_apk, std::string file, std::string* output) {
StringOutputStream output_stream(output);
Printer printer(&output_stream);
auto xml = loaded_apk->LoadXml(file, &noop_diag);
ASSERT_NE(xml, nullptr);
Debug::DumpXml(*xml, &printer);
output_stream.Flush();
}
TEST_F(FlaggedResourcesTest, DisabledStringRemovedFromPool) {
auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
std::string output;
DumpStringPoolToString(loaded_apk.get(), &output);
std::string excluded = "DONTFIND";
ASSERT_EQ(output.find(excluded), std::string::npos);
}
TEST_F(FlaggedResourcesTest, DisabledResourcesRemovedFromTable) {
auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
auto table = loaded_apk->GetResourceTable();
EXPECT_FALSE(
table->FindResource(test::ParseNameOrDie("com.android.intenal.flaggedresources:bool/bool4")));
EXPECT_FALSE(table->FindResource(
test::ParseNameOrDie("com.android.intenal.flaggedresources:string/str1")));
EXPECT_FALSE(table->FindResource(
test::ParseNameOrDie("com.android.intenal.flaggedresources:layout/layout2")));
EXPECT_FALSE(table->FindResource(
test::ParseNameOrDie("com.android.intenal.flaggedresources:drawable/removedpng")));
}
TEST_F(FlaggedResourcesTest, DisabledResourcesInRJava) {
auto r_path = file::BuildPath({android::base::GetExecutableDirectory(), "resource-flagging-java",
"com", "android", "intenal", "flaggedresources", "R.java"});
std::string r_contents;
::android::base::ReadFileToString(r_path, &r_contents);
ASSERT_NE(r_contents.find("public static final int bool4"), std::string::npos);
ASSERT_NE(r_contents.find("public static final int str1"), std::string::npos);
}
TEST_F(FlaggedResourcesTest, TwoValuesSameDisabledFlag) {
test::TestDiagnosticsImpl diag;
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml = BuildResourcesXml({{"bool", "bool1", "false", "test.package.falseFlag"},
{"bool", "bool1", "true", "test.package.falseFlag"}});
ASSERT_FALSE(CompileFile(
GetTestPath("res/values/values.xml"), xml, compiled_files_dir, &diag,
{"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
ASSERT_TRUE(
diag.GetLog().contains("duplicate flag disabled value for resource 'bool/bool1' with config "
"'' and flag 'test.package.falseFlag'"));
}
TEST_F(FlaggedResourcesTest, TwoValuesSameDisabledFlagDifferentFiles) {
test::TestDiagnosticsImpl diag;
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml1 = BuildResourcesXml({{"bool", "bool1", "false", "test.package.falseFlag"}});
ASSERT_TRUE(CompileFile(
GetTestPath("res/values/values1.xml"), xml1, compiled_files_dir, &diag,
{"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
std::string xml2 = BuildResourcesXml({{"bool", "bool1", "true", "test.package.falseFlag"}});
ASSERT_TRUE(CompileFile(
GetTestPath("res/values/values2.xml"), xml2, compiled_files_dir, &diag,
{"--feature-flags", "test.package.falseFlag:ro=false,test.package.trueFlag:ro=true"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(),
"-o",
out_apk,
};
ASSERT_FALSE(Link(link_args, compiled_files_dir, &diag));
ASSERT_TRUE(
diag.GetLog().contains("duplicate flag_disabled_values value for resource 'bool/bool1' with "
"config '' and flag 'test.package.falseFlag'"));
}
TEST_F(FlaggedResourcesTest, EnabledXmlElementAttributeRemoved) {
auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
std::string output;
DumpXmlTreeToString(loaded_apk.get(), "res/layout-v36.1/layout1.xml", &output);
ASSERT_TRUE(output.contains("FIND_ME"));
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagInXmlGetsFlagged) {
auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
std::string output;
DumpChunksToString(loaded_apk.get(), &output);
SCOPED_TRACE(output);
// The actual line looks something like:
// [ResTable_entry] id: 0x0000 name: layout1 keyIndex: 14 size: 8 flags: 0x0010
//
// This regex matches that line and captures the name and the flag value for checking.
std::regex regex("[0-9a-zA-Z:_\\]\\[ ]+name: ([0-9a-zA-Z]+)[0-9a-zA-Z: ]+flags: (0x\\d{4})");
std::smatch match;
std::stringstream ss(output);
std::string line;
bool found = false;
int fields_flagged = 0;
while (std::getline(ss, line)) {
bool first_line = false;
if (line.contains("config: v36")) {
std::getline(ss, line);
first_line = true;
}
if (!line.contains("flags")) {
continue;
}
if (std::regex_search(line, match, regex) && (match.size() == 3)) {
unsigned int hex_value;
std::stringstream hex_ss;
hex_ss << std::hex << match[2];
hex_ss >> hex_value;
if (hex_value & android::ResTable_entry::FLAG_USES_FEATURE_FLAGS) {
fields_flagged++;
if (first_line && match[1] == "layout1") {
found = true;
}
}
}
}
ASSERT_TRUE(found) << "No entry for layout1 at v36 with FLAG_USES_FEATURE_FLAGS bit set";
// There should only be 2 entry that has the FLAG_USES_FEATURE_FLAGS bit of flags set to 1, the
// three versions of the layout file that has flags
ASSERT_EQ(fields_flagged, 3);
}
TEST_F(FlaggedResourcesTest, FlagInXmlNotRecognized) {
const std::string compiled_files_dir = GetTestPath("compiled");
ASSERT_TRUE(
CompileFile(GetTestPath("res/layout/mylayout.xml"),
R"(<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:featureFlag="test.package.myFlag"/>
</LinearLayout>)",
compiled_files_dir, &noop_diag, {}));
const std::string manifest_file = GetTestPath("AndroidManifest.xml");
WriteFile(manifest_file, R"(
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aapt.command.test">
<overlay android:targetPackage="com.example.target" />
</manifest>)");
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
manifest_file,
"-o",
out_apk,
};
test::TestDiagnosticsImpl diag;
ASSERT_FALSE(Link(link_args, compiled_files_dir, &diag));
ASSERT_TRUE(
diag.GetLog().contains("res/layout/mylayout.xml:2: error: element 'TextView' has "
"flag 'test.package.myFlag' not found in flags from --feature_flags "
"parameter"));
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagChunk) {
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml =
BuildResourcesXml({{"string", "text1", "foobar", "test.package.rwFlag"},
{"string", "text2", "foobar", std::nullopt},
{"string", "text3", "@string/text2", "test.package.rwFlag"}});
ASSERT_TRUE(CompileFile(GetTestPath("res/values/strings.xml"), xml, compiled_files_dir,
&noop_diag, {"--feature-flags", "test.package.rwFlag"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(),
"-o",
out_apk,
};
ASSERT_TRUE(Link(link_args, compiled_files_dir, &noop_diag));
auto loaded_apk = LoadedApk::LoadApkFromPath(out_apk, &noop_diag);
auto table = loaded_apk->GetResourceTable();
// 1. Verify text1
auto search_result1 =
table->FindResource(test::ParseNameOrDie("com.aapt.command.test:string/text1"));
ASSERT_TRUE(search_result1.has_value());
auto entry1 = search_result1->entry;
ASSERT_EQ(entry1->values.size(), 1);
ASSERT_EQ(entry1->readwrite_flag_values.size(), 1);
auto val1 = entry1->readwrite_flag_values[0].get();
EXPECT_EQ(val1->uses_readwrite_feature_flags, true);
auto string_val1 = ValueCast<String>(val1->value.get());
ASSERT_NE(string_val1, nullptr);
EXPECT_EQ(*string_val1->value, "foobar");
ASSERT_TRUE(string_val1->GetFlag().has_value());
EXPECT_EQ(string_val1->GetFlag()->name, "test.package.rwFlag");
EXPECT_FALSE(string_val1->GetFlag()->negated);
EXPECT_EQ(string_val1->GetFlagStatus(), FlagStatus::RWFlag);
// 2. Verify text2
auto search_result2 =
table->FindResource(test::ParseNameOrDie("com.aapt.command.test:string/text2"));
ASSERT_TRUE(search_result2.has_value());
auto entry2 = search_result2->entry;
EXPECT_TRUE(entry2->readwrite_flag_values.empty());
ASSERT_EQ(entry2->values.size(), 1);
auto val2 = entry2->values[0].get();
auto string_val2 = ValueCast<String>(val2->value.get());
ASSERT_NE(string_val2, nullptr);
EXPECT_EQ(*string_val2->value, "foobar");
EXPECT_FALSE(string_val2->GetFlag().has_value());
// 3. Verify text3
auto search_result3 =
table->FindResource(test::ParseNameOrDie("com.aapt.command.test:string/text3"));
ASSERT_TRUE(search_result3.has_value());
auto entry3 = search_result3->entry;
ASSERT_EQ(entry3->values.size(), 1);
ASSERT_EQ(entry3->readwrite_flag_values.size(), 1);
auto val3 = entry3->readwrite_flag_values[0].get();
EXPECT_EQ(val3->uses_readwrite_feature_flags, true);
auto ref_val3 = ValueCast<Reference>(val3->value.get());
ASSERT_NE(ref_val3, nullptr);
ASSERT_TRUE(ref_val3->id.has_value());
ASSERT_TRUE(entry2->id.has_value());
EXPECT_EQ(ref_val3->id.value(), entry2->id.value());
ASSERT_TRUE(ref_val3->GetFlag().has_value());
EXPECT_EQ(ref_val3->GetFlag()->name, "test.package.rwFlag");
EXPECT_FALSE(ref_val3->GetFlag()->negated);
EXPECT_EQ(ref_val3->GetFlagStatus(), FlagStatus::RWFlag);
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagsOnOverlaySucceeds) {
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml = BuildResourcesXml({{"string", "text1", "foobar", "test.package.rwFlag"}});
ASSERT_TRUE(CompileFile(GetTestPath("res/values/strings.xml"), xml, compiled_files_dir,
&noop_diag, {"--feature-flags", "test.package.rwFlag"}));
const std::string manifest_file = GetTestPath("AndroidManifest.xml");
WriteFile(manifest_file, R"(
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aapt.command.test">
<overlay android:targetPackage="com.example.target" />
</manifest>)");
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
manifest_file,
"-o",
out_apk,
};
test::TestDiagnosticsImpl diag;
ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));
}
TEST_F(FlaggedResourcesTest, RODisabledManifestElementRemoved) {
auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "resapp.apk"});
auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
auto xml = loaded_apk->LoadXml("AndroidManifest.xml", &noop_diag);
ASSERT_NE(xml, nullptr);
auto manifest_el = xml->root.get();
ASSERT_NE(manifest_el, nullptr);
for (const auto& child : manifest_el->children) {
if (auto el = xml::NodeCast<xml::Element>(child.get())) {
if (el->name == "uses-permission") {
auto attr = el->FindAttribute(xml::kSchemaAndroid, "name");
if (attr) {
EXPECT_NE(attr->value, "android.permission.CAMERA");
}
}
}
}
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagPreservedThroughStaticLib) {
using namespace android;
StdErrDiagnostics diag;
// 1. Compile a resource with a read/write flag into a static library
const std::string static_lib_compiled_files_dir = GetTestPath("static-lib-compiled");
ASSERT_TRUE(CompileFile(GetTestPath("res/values/strings.xml"),
R"(<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string name="text1" android:featureFlag="test.package.rwFlag">default_val</string>
</resources>)",
static_lib_compiled_files_dir, &diag,
{"--feature-flags", "test.package.rwFlag"}));
const std::string static_lib_apk = GetTestPath("static_lib.apk");
std::vector<std::string> link_args = {
"--manifest", GetDefaultManifest("com.android.lib"), "--static-lib", "-o", static_lib_apk,
};
ASSERT_TRUE(Link(link_args, static_lib_compiled_files_dir, &diag));
// 2. Link the static library into the main application package
const std::string app_compiled_files_dir = GetTestPath("app-compiled");
ASSERT_TRUE(CompileFile(GetTestPath("res/values/values.xml"),
R"(<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="bar"/>
</resources>)",
app_compiled_files_dir, &diag));
const std::string out_apk = GetTestPath("out.apk");
link_args = {"--manifest", GetDefaultManifest("com.android.app"), "-o", out_apk, static_lib_apk};
ASSERT_TRUE(Link(link_args, app_compiled_files_dir, &diag));
// 3. Load the generated APK and verify that the flag is preserved
auto loaded_apk = LoadedApk::LoadApkFromPath(out_apk, &noop_diag);
std::string output;
DumpChunksToString(loaded_apk.get(), &output);
// Verify that the flag list and the flagged chunk are present in the final APK
ASSERT_TRUE(output.contains("test.package.rwFlag"));
ASSERT_TRUE(output.contains("[RES_TABLE_FLAGGED]"));
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagOnStyleElementsBeforeAvailable) {
aapt::StdErrDiagnostics diagnostics;
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml =
BuildResourcesXml({{.type = "string", .name = "text2", .value = "foobar"},
{.type = "style",
.name = "style1",
.style_items = {{"android:textSize", "16sp"},
{"android:enabled", "true", "test.package.rwFlag"},
{"android:enabled", "false", "!test.package.rwFlag"},
{"android:textColor", "#80ff0000"}}}});
ASSERT_TRUE(CompileFile(GetTestPath("res/values/styles.xml"), xml, compiled_files_dir,
&diagnostics, {"--feature-flags", "test.package.rwFlag"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(kDefaultPackageName, SDK_BAKLAVA),
"-o",
out_apk,
};
ASSERT_TRUE(Link(link_args, compiled_files_dir, &noop_diag));
auto loaded_apk = LoadedApk::LoadApkFromPath(out_apk, &noop_diag);
auto search_result = loaded_apk->GetResourceTable()->FindResource(
test::ParseNameOrDie("com.aapt.command.test:style/style1"));
ASSERT_TRUE(search_result.has_value());
auto* entry = search_result->entry;
ASSERT_EQ(entry->values.size(), 2);
// Find the fallback and the split
ResourceConfigValue* fallback = nullptr;
ResourceConfigValue* split = nullptr;
for (auto& val : entry->values) {
if (val->config.sdkVersion == MIN_FLAGGED_BAG_ELEMENTS_SDK &&
val->config.minorVersion == MIN_FLAGGED_BAG_ELEMENTS_MINOR_VERSION) {
split = val.get();
} else if (val->config.sdkVersion == 0) {
fallback = val.get();
}
}
ASSERT_NE(fallback, nullptr);
ASSERT_NE(split, nullptr);
// Verify Split (v37.2) has flagged elements intact
auto* split_style = ValueCast<Style>(split->value.get());
ASSERT_NE(split_style, nullptr);
EXPECT_TRUE(split_style->HasFlaggedElements());
// Verify Fallback (v0) is stripped
auto* fallback_style = ValueCast<Style>(fallback->value.get());
ASSERT_NE(fallback_style, nullptr);
EXPECT_FALSE(fallback_style->HasFlaggedElements());
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagOnStyleElementsConfigSDKHighEnough) {
aapt::StdErrDiagnostics diagnostics;
const std::string compiled_files_dir = GetTestPath("compiled");
auto path =
android::base::StringPrintf("res/values-V%d/styles.xml", MIN_FLAGGED_BAG_ELEMENTS_SDK);
std::string xml =
BuildResourcesXml({{.type = "string", .name = "text2", .value = "foobar"},
{.type = "style",
.name = "style1",
.style_items = {{"android:enabled", "true", "test.package.rwFlag"}}}});
ASSERT_TRUE(CompileFile(GetTestPath(path), xml, compiled_files_dir, &diagnostics,
{"--feature-flags", "test.package.rwFlag"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(),
"-o",
out_apk,
};
ASSERT_TRUE(Link(link_args, compiled_files_dir, &noop_diag));
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagOnStyleElementsConfigMinorSDKHighEnough) {
aapt::StdErrDiagnostics diagnostics;
const std::string compiled_files_dir = GetTestPath("compiled");
auto path =
android::base::StringPrintf("res/values-v%d.1/styles.xml", MIN_FLAGGED_BAG_ELEMENTS_SDK);
std::string xml =
BuildResourcesXml({{.type = "string", .name = "text2", .value = "foobar"},
{.type = "style",
.name = "style1",
.style_items = {{"android:enabled", "true", "test.package.rwFlag"}}}});
ASSERT_TRUE(CompileFile(GetTestPath(path), xml, compiled_files_dir, &diagnostics,
{"--feature-flags", "test.package.rwFlag"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(),
"-o",
out_apk,
};
ASSERT_TRUE(Link(link_args, compiled_files_dir, &noop_diag));
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagOnStyleElementsMinSDKHighEnough) {
aapt::StdErrDiagnostics diagnostics;
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml =
BuildResourcesXml({{.type = "string", .name = "text2", .value = "foobar"},
{.type = "style",
.name = "style1",
.style_items = {{"android:enabled", "true", "test.package.rwFlag"}}}});
ASSERT_TRUE(CompileFile(GetTestPath("res/values-/styles.xml"), xml, compiled_files_dir,
&diagnostics, {"--feature-flags", "test.package.rwFlag"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(kDefaultPackageName, MIN_FLAGGED_BAG_ELEMENTS_SDK + 1),
"-o",
out_apk,
};
ASSERT_TRUE(Link(link_args, compiled_files_dir, &noop_diag));
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagOnStyleElements) {
aapt::StdErrDiagnostics diagnostics;
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml = BuildResourcesXml(
{{.type = "attr", .name = "mySize", .extra_attrs = {"format=\"dimension\""}},
{.type = "attr", .name = "myEnabled", .extra_attrs = {"format=\"boolean\""}},
{.type = "attr", .name = "myColor", .extra_attrs = {"format=\"color\""}},
{.type = "string", .name = "text2", .value = "foobar"},
{.type = "style",
.name = "style1",
.style_items = {{"mySize", "16sp"},
{"myEnabled", "true", "test.package.rwFlag"},
{"myEnabled", "false", "!test.package.rwFlag"},
{"myColor", "#80ff0000"}}}});
ASSERT_TRUE(CompileFile(GetTestPath("res/values/styles.xml"), xml, compiled_files_dir,
&diagnostics, {"--feature-flags", "test.package.rwFlag"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(kDefaultPackageName, MIN_FLAGGED_BAG_ELEMENTS_SDK + 1),
"-o",
out_apk,
};
ASSERT_TRUE(Link(link_args, compiled_files_dir, &noop_diag));
auto loaded_apk = LoadedApk::LoadApkFromPath(out_apk, &noop_diag);
auto actual_style =
test::GetValue<Style>(loaded_apk->GetResourceTable(), "com.aapt.command.test:style/style1");
ASSERT_NE(actual_style, nullptr);
EXPECT_TRUE(actual_style->HasFlaggedElements());
int flagged_count = 0;
int negated_count = 0;
int normal_count = 0;
int textSize_count = 0;
int textColor_count = 0;
for (const auto& entry : actual_style->entries) {
ASSERT_TRUE(entry.key.name.has_value());
if (entry.key.name->entry == "myEnabled") {
if (entry.value->GetFlag()) {
flagged_count++;
EXPECT_EQ(entry.value->GetFlag()->name, "test.package.rwFlag");
EXPECT_EQ(entry.value->GetFlagStatus(), FlagStatus::RWFlag);
if (entry.value->GetFlag()->negated) {
negated_count++;
} else {
normal_count++;
}
}
} else if (entry.key.name->entry == "mySize") {
textSize_count++;
EXPECT_FALSE(entry.value->GetFlag());
} else if (entry.key.name->entry == "myColor") {
textColor_count++;
EXPECT_FALSE(entry.value->GetFlag());
}
}
EXPECT_EQ(flagged_count, 2);
EXPECT_EQ(negated_count, 1);
EXPECT_EQ(normal_count, 1);
EXPECT_EQ(textSize_count, 1);
EXPECT_EQ(textColor_count, 1);
}
TEST_F(FlaggedResourcesTest, ReadWriteFlagOnArrayElements) {
aapt::StdErrDiagnostics diagnostics;
const std::string compiled_files_dir = GetTestPath("compiled");
std::string xml =
BuildResourcesXml({{.type = "string-array",
.name = "array1",
.array_items = {{.value = "item1"},
{.value = "item2", .flag = "test.package.rwFlag"},
{.value = "item3", .flag = "!test.package.rwFlag"},
{.value = "item4"}}}});
ASSERT_TRUE(CompileFile(GetTestPath("res/values/arrays.xml"), xml, compiled_files_dir,
&diagnostics, {"--feature-flags", "test.package.rwFlag"}));
const std::string out_apk = GetTestPath("out.apk");
std::vector<std::string> link_args = {
"--manifest",
GetDefaultManifest(kDefaultPackageName, MIN_FLAGGED_BAG_ELEMENTS_SDK + 1),
"-o",
out_apk,
};
ASSERT_TRUE(Link(link_args, compiled_files_dir, &noop_diag));
auto loaded_apk = LoadedApk::LoadApkFromPath(out_apk, &noop_diag);
auto actual_array =
test::GetValue<Array>(loaded_apk->GetResourceTable(), "com.aapt.command.test:array/array1");
ASSERT_NE(actual_array, nullptr);
EXPECT_TRUE(actual_array->HasFlaggedElements());
ASSERT_EQ(actual_array->elements.size(), 4);
// Validate element 0
EXPECT_FALSE(actual_array->elements[0]->GetFlag());
// Validate element 1
ASSERT_TRUE(actual_array->elements[1]->GetFlag());
EXPECT_EQ(actual_array->elements[1]->GetFlag()->name, "test.package.rwFlag");
EXPECT_FALSE(actual_array->elements[1]->GetFlag()->negated);
EXPECT_EQ(actual_array->elements[1]->GetFlagStatus(), FlagStatus::RWFlag);
// Validate element 2
ASSERT_TRUE(actual_array->elements[2]->GetFlag());
EXPECT_EQ(actual_array->elements[2]->GetFlag()->name, "test.package.rwFlag");
EXPECT_TRUE(actual_array->elements[2]->GetFlag()->negated);
EXPECT_EQ(actual_array->elements[2]->GetFlagStatus(), FlagStatus::RWFlag);
// Validate element 3
EXPECT_FALSE(actual_array->elements[3]->GetFlag());
}
} // namespace aapt