| /* | |
| * Copyright 2026 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 "link/FlaggedBagVersioner.h" | |
| #include "SdkConstants.h" | |
| #include "test/Test.h" | |
| using ::testing::Eq; | |
| using ::testing::Ne; | |
| using ::testing::NotNull; | |
| using ::testing::SizeIs; | |
| namespace aapt { | |
| class FlaggedBagVersionerTest : public ::testing::Test { | |
| public: | |
| void SetUp() override { | |
| // Default context with minSDK less than the required version | |
| context_ = test::ContextBuilder() | |
| .SetCompilationPackage("com.app") | |
| .SetPackageId(0x7f) | |
| .SetPackageType(PackageType::kApp) | |
| .SetMinSdkVersion(MIN_FLAGGED_BAG_ELEMENTS_SDK - 1) | |
| .Build(); | |
| } | |
| protected: | |
| std::unique_ptr<IAaptContext> context_; | |
| }; | |
| TEST_F(FlaggedBagVersionerTest, NoFlaggedElements_NoChange) { | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| .AddValue("com.app:style/style1", | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 16)) | |
| .Build()) | |
| .Build(); | |
| FeatureFlagValues feature_flags; | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify no new values were added (still only 1 config value) | |
| auto* entry = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", {}); | |
| ASSERT_THAT(entry, NotNull()); | |
| EXPECT_THAT(entry->entries, SizeIs(1)); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_MinSdkSufficient_NoChange) { | |
| // Set minSDK to MIN_FLAGGED_BAG_ELEMENTS_SDK + 1. | |
| context_ = test::ContextBuilder() | |
| .SetCompilationPackage("com.app") | |
| .SetPackageId(0x7f) | |
| .SetPackageType(PackageType::kApp) | |
| .SetMinSdkVersion(MIN_FLAGGED_BAG_ELEMENTS_SDK + 1) | |
| .Build(); | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| .AddValue("com.app:style/style1", | |
| test::StyleBuilder() | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1)) | |
| .Build()) | |
| .Build(); | |
| // Mark the item as flagged | |
| auto* style = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", {}); | |
| ASSERT_THAT(style, NotNull()); | |
| ASSERT_FALSE(style->entries.empty()); | |
| style->entries[0].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style->entries[0].value->SetFlagStatus(FlagStatus::RWFlag); | |
| FeatureFlagValues feature_flags; | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify no split (still only 1 config value, and flag is NOT stripped because SDK is high | |
| // enough) | |
| auto* result_style = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", {}); | |
| ASSERT_THAT(result_style, NotNull()); | |
| ASSERT_FALSE(result_style->entries.empty()); | |
| ASSERT_TRUE(result_style->entries[0].value->GetFlag().has_value()); | |
| EXPECT_THAT(result_style->entries[0].value->GetFlag()->name, Eq("package.flag")); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_SplitStyle) { | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| .AddValue("com.app:style/style1", | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 16)) | |
| // flagged true | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1)) | |
| // flagged false | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0)) | |
| .AddItem("android:attr/textColor", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_COLOR_ARGB8, | |
| 0xff0000)) // unflagged fallback | |
| .Build()) | |
| .Build(); | |
| // Mark the items as flagged | |
| auto* style = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", {}); | |
| ASSERT_THAT(style, NotNull()); | |
| // Find "enabled" entries and flag them | |
| int enabled_count = 0; | |
| for (auto& entry : style->entries) { | |
| if (entry.key.name->entry == "enabled") { | |
| if (enabled_count == 0) { | |
| entry.value->SetFlag(FeatureFlagAttribute{"package.flag", false}); // normal | |
| entry.value->SetFlagStatus(FlagStatus::RWFlag); | |
| enabled_count++; | |
| } else { | |
| entry.value->SetFlag(FeatureFlagAttribute{"package.flag", true}); // negated | |
| entry.value->SetFlagStatus(FlagStatus::RWFlag); | |
| } | |
| } | |
| } | |
| // Define feature flags: "package.flag" is enabled (true) | |
| FeatureFlagValues feature_flags; | |
| feature_flags.emplace("package.flag", FeatureFlagProperties{false, true}); | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify that we now have TWO config values for style1: | |
| // 1. The fallback (default config, e.g. v0 or whatever the original was) | |
| // 2. The split (v37.2) | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:style/style1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| ASSERT_THAT(res_entry->values, SizeIs(2)); | |
| // Find the fallback and the split | |
| ResourceConfigValue* fallback = nullptr; | |
| ResourceConfigValue* split = nullptr; | |
| for (auto& val : res_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_THAT(fallback, NotNull()); | |
| ASSERT_THAT(split, NotNull()); | |
| // Verify Split (v37.2): | |
| // It should be UNCHANGED (contains all original entries, with flags intact) | |
| auto* split_style = ValueCast<Style>(split->value.get()); | |
| ASSERT_THAT(split_style, NotNull()); | |
| EXPECT_THAT(split_style->entries, SizeIs(4)); | |
| EXPECT_TRUE(split_style->HasFlaggedElements()); | |
| EXPECT_TRUE(split->uses_readwrite_feature_flags); | |
| // Verify Fallback (v0): | |
| // - "textSize" (unflagged) should remain. | |
| // - "textColor" (unflagged fallback) should remain. | |
| // - "enabled" (flagged true) should remain, but its flag should be STRIPPED. | |
| // - "enabled" (flagged false/negated) should be REMOVED. | |
| auto* fallback_style = ValueCast<Style>(fallback->value.get()); | |
| ASSERT_THAT(fallback_style, NotNull()); | |
| EXPECT_FALSE(fallback_style->HasFlaggedElements()); | |
| EXPECT_FALSE(fallback->uses_readwrite_feature_flags); | |
| // We expect 3 entries in fallback: textSize, textColor, and the enabled (true) entry. | |
| EXPECT_THAT(fallback_style->entries, SizeIs(3)); | |
| bool found_textSize = false; | |
| bool found_textColor = false; | |
| bool found_enabled = false; | |
| for (const auto& entry : fallback_style->entries) { | |
| if (entry.key.name->entry == "textSize") { | |
| found_textSize = true; | |
| } else if (entry.key.name->entry == "textColor") { | |
| found_textColor = true; | |
| } else if (entry.key.name->entry == "enabled") { | |
| found_enabled = true; | |
| // Verify flag is stripped | |
| EXPECT_FALSE(entry.value->GetFlag().has_value()); | |
| EXPECT_THAT(entry.value->GetFlagStatus(), Eq(FlagStatus::NoFlag)); | |
| // Verify it kept the "true" value (which was enabled) | |
| auto* prim = ValueCast<BinaryPrimitive>(entry.value.get()); | |
| ASSERT_THAT(prim, NotNull()); | |
| EXPECT_THAT(prim->value.data, Eq(1u)); | |
| } | |
| } | |
| EXPECT_TRUE(found_textSize); | |
| EXPECT_TRUE(found_textColor); | |
| EXPECT_TRUE(found_enabled); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_SplitStyle_RemoveUnflaggedFallback) { | |
| // Test that if we have a flagged entry and an unflagged fallback entry for the SAME attribute, | |
| // and the flag is enabled (so we keep the flagged entry), the unflagged fallback is removed. | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| .AddValue("com.app:style/style1", | |
| test::StyleBuilder() | |
| // unflagged fallback (false) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0)) | |
| // flagged (true) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1)) | |
| .Build()) | |
| .Build(); | |
| auto* style = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", {}); | |
| ASSERT_THAT(style, NotNull()); | |
| // Flag the second entry | |
| style->entries[1].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style->entries[1].value->SetFlagStatus(FlagStatus::RWFlag); | |
| FeatureFlagValues feature_flags; | |
| feature_flags.emplace("package.flag", FeatureFlagProperties{false, true}); // enabled | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:style/style1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| ASSERT_THAT(res_entry->values, SizeIs(2)); | |
| ResourceConfigValue* fallback = nullptr; | |
| for (auto& val : res_entry->values) { | |
| if (val->config.sdkVersion == 0) { | |
| fallback = val.get(); | |
| } | |
| } | |
| ASSERT_THAT(fallback, NotNull()); | |
| auto* fallback_style = ValueCast<Style>(fallback->value.get()); | |
| ASSERT_THAT(fallback_style, NotNull()); | |
| // In the fallback, we should ONLY have the flagged-then-stripped entry (which is true). | |
| // The unflagged fallback (which was false) should have been removed because the flagged one was | |
| // enabled. | |
| ASSERT_THAT(fallback_style->entries, SizeIs(1)); | |
| EXPECT_THAT(fallback_style->entries[0].key.name->entry, Eq("enabled")); | |
| auto* prim = ValueCast<BinaryPrimitive>(fallback_style->entries[0].value.get()); | |
| ASSERT_THAT(prim, NotNull()); | |
| EXPECT_THAT(prim->value.data, Eq(1u)); // Kept the "true" one, removed the "false" fallback! | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_SplitArray) { | |
| // Create an Array | |
| auto array = util::make_unique<Array>(); | |
| auto item1 = test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 1); // unflagged | |
| auto item2 = test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 2); // flagged true | |
| item2->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| item2->SetFlagStatus(FlagStatus::RWFlag); | |
| auto item3 = | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 3); // flagged false (negated) | |
| item3->SetFlag(FeatureFlagAttribute{"package.flag", true}); | |
| item3->SetFlagStatus(FlagStatus::RWFlag); | |
| auto item4 = test::BuildPrimitive(android::Res_value::TYPE_INT_DEC, 4); // unflagged | |
| array->elements.push_back(std::move(item1)); | |
| array->elements.push_back(std::move(item2)); | |
| array->elements.push_back(std::move(item3)); | |
| array->elements.push_back(std::move(item4)); | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder().AddValue("com.app:array/array1", std::move(array)).Build(); | |
| FeatureFlagValues feature_flags; | |
| feature_flags.emplace("package.flag", FeatureFlagProperties{false, true}); // enabled | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:array/array1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| ASSERT_THAT(res_entry->values, SizeIs(2)); | |
| ResourceConfigValue* fallback = nullptr; | |
| ResourceConfigValue* split = nullptr; | |
| for (auto& val : res_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_THAT(fallback, NotNull()); | |
| ASSERT_THAT(split, NotNull()); | |
| // Verify Split (v37.2): | |
| auto* split_array = ValueCast<Array>(split->value.get()); | |
| ASSERT_THAT(split_array, NotNull()); | |
| EXPECT_THAT(split_array->elements, SizeIs(4)); | |
| EXPECT_TRUE(split_array->HasFlaggedElements()); | |
| // Verify Fallback (v0): | |
| // - item1 (1) should remain. | |
| // - item2 (2, flagged true) should remain, but flag stripped. | |
| // - item3 (3, flagged false) should be removed. | |
| // - item4 (4) should remain. | |
| auto* fallback_array = ValueCast<Array>(fallback->value.get()); | |
| ASSERT_THAT(fallback_array, NotNull()); | |
| EXPECT_FALSE(fallback_array->HasFlaggedElements()); | |
| ASSERT_THAT(fallback_array->elements, SizeIs(3)); | |
| auto* prim1 = ValueCast<BinaryPrimitive>(fallback_array->elements[0].get()); | |
| ASSERT_THAT(prim1, NotNull()); | |
| EXPECT_THAT(prim1->value.data, Eq(1u)); | |
| auto* prim2 = ValueCast<BinaryPrimitive>(fallback_array->elements[1].get()); | |
| ASSERT_THAT(prim2, NotNull()); | |
| EXPECT_THAT(prim2->value.data, Eq(2u)); | |
| EXPECT_FALSE(prim2->GetFlag().has_value()); | |
| auto* prim4 = ValueCast<BinaryPrimitive>(fallback_array->elements[2].get()); | |
| ASSERT_THAT(prim4, NotNull()); | |
| EXPECT_THAT(prim4->value.data, Eq(4u)); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, MultipleFallbacksDifferentSdkVersionsTakesHighestSdkBase) { | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| .AddValue("com.app:style/style1", | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 16)) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1)) | |
| .Build()) | |
| .AddValue("com.app:style/style1", test::ParseConfigOrDie("v24"), {}, | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 18)) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0)) | |
| .Build()) | |
| .Build(); | |
| // Mark the items as flagged | |
| auto* style_v0 = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", {}); | |
| ASSERT_THAT(style_v0, NotNull()); | |
| style_v0->entries[1].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style_v0->entries[1].value->SetFlagStatus(FlagStatus::RWFlag); | |
| auto config_v24 = test::ParseConfigOrDie("v24"); | |
| auto* style_v24 = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", config_v24); | |
| ASSERT_THAT(style_v24, NotNull()); | |
| style_v24->entries[1].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style_v24->entries[1].value->SetFlagStatus(FlagStatus::RWFlag); | |
| FeatureFlagValues feature_flags; | |
| feature_flags.emplace("package.flag", FeatureFlagProperties{false, true}); // enabled | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify that we now have configs in style1: | |
| // 1. Fallback v0 | |
| // 2. Fallback v24 | |
| // 3. Split v37.2 (cloned from v24 since v24 has highest SDK version) | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:style/style1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| ASSERT_THAT(res_entry->values, SizeIs(3)); | |
| ResourceConfigValue* fallback_v0 = nullptr; | |
| ResourceConfigValue* fallback_v24 = nullptr; | |
| ResourceConfigValue* split = nullptr; | |
| for (auto& val : res_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 == 24) { | |
| fallback_v24 = val.get(); | |
| } else if (val->config.sdkVersion == 0) { | |
| fallback_v0 = val.get(); | |
| } | |
| } | |
| ASSERT_THAT(fallback_v0, NotNull()); | |
| ASSERT_THAT(fallback_v24, NotNull()); | |
| ASSERT_THAT(split, NotNull()); | |
| // Verify Split (v37.2) is cloned from v24 and contains flags intact | |
| auto* split_style = ValueCast<Style>(split->value.get()); | |
| ASSERT_THAT(split_style, NotNull()); | |
| EXPECT_TRUE(split_style->HasFlaggedElements()); | |
| EXPECT_TRUE(split->uses_readwrite_feature_flags); | |
| // Verify that its "enabled" value is the one from v24 (which was false/0) | |
| for (const auto& entry : split_style->entries) { | |
| if (entry.key.name->entry == "enabled") { | |
| auto* prim = ValueCast<BinaryPrimitive>(entry.value.get()); | |
| ASSERT_THAT(prim, NotNull()); | |
| EXPECT_THAT(prim->value.data, Eq(0u)); | |
| } | |
| } | |
| // Verify Fallbacks are stripped of flags | |
| auto* fallback_v0_style = ValueCast<Style>(fallback_v0->value.get()); | |
| ASSERT_THAT(fallback_v0_style, NotNull()); | |
| EXPECT_FALSE(fallback_v0_style->HasFlaggedElements()); | |
| EXPECT_FALSE(fallback_v0->uses_readwrite_feature_flags); | |
| auto* fallback_v24_style = ValueCast<Style>(fallback_v24->value.get()); | |
| ASSERT_THAT(fallback_v24_style, NotNull()); | |
| EXPECT_FALSE(fallback_v24_style->HasFlaggedElements()); | |
| EXPECT_FALSE(fallback_v24->uses_readwrite_feature_flags); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_MinSdkLow_ConfigSufficient_NoSplit) { | |
| // Set minSdkVersion less than the required version | |
| context_ = test::ContextBuilder() | |
| .SetCompilationPackage("com.app") | |
| .SetPackageId(0x7f) | |
| .SetPackageType(PackageType::kApp) | |
| .SetMinSdkVersion(MIN_FLAGGED_BAG_ELEMENTS_SDK - 1) | |
| .Build(); | |
| // Create an entry with a value having sufficient config version. | |
| android::ConfigDescription sufficient_config; | |
| sufficient_config.sdkVersion = MIN_FLAGGED_BAG_ELEMENTS_SDK; | |
| sufficient_config.minorVersion = MIN_FLAGGED_BAG_ELEMENTS_MINOR_VERSION; | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| .AddValue("com.app:style/style1", sufficient_config, {}, | |
| test::StyleBuilder() | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1)) | |
| .Build()) | |
| .Build(); | |
| // Mark the item as flagged | |
| auto* style = | |
| test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", sufficient_config); | |
| ASSERT_THAT(style, NotNull()); | |
| ASSERT_FALSE(style->entries.empty()); | |
| style->entries[0].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style->entries[0].value->SetFlagStatus(FlagStatus::RWFlag); | |
| FeatureFlagValues feature_flags; | |
| FlaggedBagVersioner versioner(feature_flags); | |
| // Process the table | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify that it did not split (still only 1 config value for style1) | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:style/style1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| ASSERT_THAT(res_entry->values, SizeIs(1)); | |
| // Verify that the flag is NOT stripped (still has flag) | |
| auto* result_style = | |
| test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", sufficient_config); | |
| ASSERT_THAT(result_style, NotNull()); | |
| ASSERT_FALSE(result_style->entries.empty()); | |
| ASSERT_TRUE(result_style->entries[0].value->GetFlag().has_value()); | |
| EXPECT_THAT(result_style->entries[0].value->GetFlag()->name, Eq("package.flag")); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_SplitStyle_ExistingSplit_NoOverwrite) { | |
| // Set minSdkVersion less than the required version | |
| context_ = test::ContextBuilder() | |
| .SetCompilationPackage("com.app") | |
| .SetPackageId(0x7f) | |
| .SetPackageType(PackageType::kApp) | |
| .SetMinSdkVersion(MIN_FLAGGED_BAG_ELEMENTS_SDK - 1) | |
| .Build(); | |
| android::ConfigDescription split_config; | |
| split_config.sdkVersion = MIN_FLAGGED_BAG_ELEMENTS_SDK; | |
| split_config.minorVersion = MIN_FLAGGED_BAG_ELEMENTS_MINOR_VERSION; | |
| android::ConfigDescription v24_config; | |
| v24_config.sdkVersion = 24; | |
| // Create a resource table with an existing value at config v37-n2, and a | |
| // value at config v24. | |
| // We want to add a flagged value at v24 and verify that it doesn't overwrite | |
| // our existing value at v37-n2. | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| .AddValue("com.app:style/style1", split_config, {}, | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 16)) | |
| .Build()) | |
| .AddValue("com.app:style/style1", v24_config, {}, | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 18)) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0)) | |
| .Build()) | |
| .Build(); | |
| // Mark the item in v24 as flagged | |
| auto* style_v24 = test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", v24_config); | |
| ASSERT_THAT(style_v24, NotNull()); | |
| style_v24->entries[1].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style_v24->entries[1].value->SetFlagStatus(FlagStatus::RWFlag); | |
| FeatureFlagValues feature_flags; | |
| feature_flags.emplace("package.flag", FeatureFlagProperties{false, true}); // enabled | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify that we now have TWO configs in style1 (v37-n2 and v24) and we | |
| // DO NOT have v37-n2 added as a new split. | |
| // i.e. values size is 2, and v37-n2 values remain exactly as original. | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:style/style1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| ASSERT_THAT(res_entry->values, SizeIs(2)); | |
| // Verify v37-n2 is untouched (it has only 1 entry: textSize=16) | |
| auto* split_val = | |
| test::GetValueForConfig<Style>(table.get(), "com.app:style/style1", split_config); | |
| ASSERT_THAT(split_val, NotNull()); | |
| EXPECT_THAT(split_val->entries, SizeIs(1)); | |
| EXPECT_FALSE(split_val->HasFlaggedElements()); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_MultipleProducts_OnlyOneSplits) { | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| // Product "foo" has flagged elements in v0 -> should split | |
| .Add(NewResourceBuilder(test::ParseNameOrDie("com.app:style/style1")) | |
| .SetValue( | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 16)) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1)) | |
| .Build(), | |
| {}, "foo") | |
| .SetAllowMangled(true) | |
| .Build()) | |
| // Product "bar" has NO flagged elements in v0 -> should NOT split | |
| .Add(NewResourceBuilder(test::ParseNameOrDie("com.app:style/style1")) | |
| .SetValue( | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 18)) | |
| .Build(), | |
| {}, "bar") | |
| .SetAllowMangled(true) | |
| .Build()) | |
| .Build(); | |
| // Find and flag the elements | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:style/style1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| Style* style_foo = nullptr; | |
| for (auto& val : res_entry->values) { | |
| if (val->product == "foo" && val->config == android::ConfigDescription{}) { | |
| style_foo = ValueCast<Style>(val->value.get()); | |
| } | |
| } | |
| ASSERT_THAT(style_foo, NotNull()); | |
| style_foo->entries[1].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style_foo->entries[1].value->SetFlagStatus(FlagStatus::RWFlag); | |
| FeatureFlagValues feature_flags; | |
| feature_flags.emplace("package.flag", FeatureFlagProperties{false, true}); // enabled | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify: | |
| // For product "foo": | |
| // 1. Fallback v0 (stripped) | |
| // 2. Split v37.2 (intact) | |
| // For product "bar": | |
| // Only v0 remains, no split v37.2 is created. | |
| ASSERT_THAT(res_entry->values, SizeIs(3)); // foo (v0), foo (v37.2), bar (v0) | |
| ResourceConfigValue* foo_v0 = nullptr; | |
| ResourceConfigValue* foo_split = nullptr; | |
| ResourceConfigValue* bar_v0 = nullptr; | |
| for (auto& val : res_entry->values) { | |
| if (val->product == "foo") { | |
| if (val->config.sdkVersion == MIN_FLAGGED_BAG_ELEMENTS_SDK && | |
| val->config.minorVersion == MIN_FLAGGED_BAG_ELEMENTS_MINOR_VERSION) { | |
| foo_split = val.get(); | |
| } else if (val->config.sdkVersion == 0) { | |
| foo_v0 = val.get(); | |
| } | |
| } else if (val->product == "bar") { | |
| if (val->config.sdkVersion == 0) { | |
| bar_v0 = val.get(); | |
| } | |
| } | |
| } | |
| ASSERT_THAT(foo_v0, NotNull()); | |
| ASSERT_THAT(foo_split, NotNull()); | |
| ASSERT_THAT(bar_v0, NotNull()); | |
| // Verify foo split has flagged elements and uses readwrite flags | |
| auto* foo_split_style = ValueCast<Style>(foo_split->value.get()); | |
| ASSERT_THAT(foo_split_style, NotNull()); | |
| EXPECT_TRUE(foo_split_style->HasFlaggedElements()); | |
| EXPECT_TRUE(foo_split->uses_readwrite_feature_flags); | |
| // Verify foo fallback is stripped | |
| auto* foo_v0_style = ValueCast<Style>(foo_v0->value.get()); | |
| ASSERT_THAT(foo_v0_style, NotNull()); | |
| EXPECT_FALSE(foo_v0_style->HasFlaggedElements()); | |
| EXPECT_FALSE(foo_v0->uses_readwrite_feature_flags); | |
| // Verify bar remains unchanged | |
| auto* bar_v0_style = ValueCast<Style>(bar_v0->value.get()); | |
| ASSERT_THAT(bar_v0_style, NotNull()); | |
| EXPECT_FALSE(bar_v0_style->HasFlaggedElements()); | |
| EXPECT_FALSE(bar_v0->uses_readwrite_feature_flags); | |
| } | |
| TEST_F(FlaggedBagVersionerTest, FlaggedElements_MultipleProducts_BothSplit) { | |
| std::unique_ptr<ResourceTable> table = | |
| test::ResourceTableBuilder() | |
| // Product "foo" has flagged elements in v0 -> should split | |
| .Add(NewResourceBuilder(test::ParseNameOrDie("com.app:style/style1")) | |
| .SetValue( | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 16)) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1)) | |
| .Build(), | |
| {}, "foo") | |
| .SetAllowMangled(true) | |
| .Build()) | |
| // Product "bar" has flagged elements in v0 -> should split | |
| .Add(NewResourceBuilder(test::ParseNameOrDie("com.app:style/style1")) | |
| .SetValue( | |
| test::StyleBuilder() | |
| .AddItem("android:attr/textSize", | |
| test::BuildPrimitive(android::Res_value::TYPE_DIMENSION, 18)) | |
| .AddItem("android:attr/enabled", | |
| test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0)) | |
| .Build(), | |
| {}, "bar") | |
| .SetAllowMangled(true) | |
| .Build()) | |
| .Build(); | |
| // Find and flag the elements | |
| auto search_result = table->FindResource(test::ParseNameOrDie("com.app:style/style1")); | |
| ASSERT_TRUE(search_result.has_value()); | |
| auto* res_entry = search_result->entry; | |
| ASSERT_THAT(res_entry, NotNull()); | |
| Style* style_foo = nullptr; | |
| Style* style_bar = nullptr; | |
| for (auto& val : res_entry->values) { | |
| if (val->config == android::ConfigDescription{}) { | |
| if (val->product == "foo") { | |
| style_foo = ValueCast<Style>(val->value.get()); | |
| } else if (val->product == "bar") { | |
| style_bar = ValueCast<Style>(val->value.get()); | |
| } | |
| } | |
| } | |
| ASSERT_THAT(style_foo, NotNull()); | |
| ASSERT_THAT(style_bar, NotNull()); | |
| style_foo->entries[1].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style_foo->entries[1].value->SetFlagStatus(FlagStatus::RWFlag); | |
| style_bar->entries[1].value->SetFlag(FeatureFlagAttribute{"package.flag", false}); | |
| style_bar->entries[1].value->SetFlagStatus(FlagStatus::RWFlag); | |
| FeatureFlagValues feature_flags; | |
| feature_flags.emplace("package.flag", FeatureFlagProperties{false, true}); // enabled | |
| FlaggedBagVersioner versioner(feature_flags); | |
| ASSERT_TRUE(versioner.Process(context_.get(), table.get())); | |
| // Verify: | |
| // Both products foo and bar should have: | |
| // 1. Fallback v0 (stripped) | |
| // 2. Split v37.2 (intact) | |
| // Total values = 4. | |
| ASSERT_THAT(res_entry->values, SizeIs(4)); | |
| ResourceConfigValue* foo_v0 = nullptr; | |
| ResourceConfigValue* foo_split = nullptr; | |
| ResourceConfigValue* bar_v0 = nullptr; | |
| ResourceConfigValue* bar_split = nullptr; | |
| for (auto& val : res_entry->values) { | |
| if (val->product == "foo") { | |
| if (val->config.sdkVersion == MIN_FLAGGED_BAG_ELEMENTS_SDK && | |
| val->config.minorVersion == MIN_FLAGGED_BAG_ELEMENTS_MINOR_VERSION) { | |
| foo_split = val.get(); | |
| } else if (val->config.sdkVersion == 0) { | |
| foo_v0 = val.get(); | |
| } | |
| } else if (val->product == "bar") { | |
| if (val->config.sdkVersion == MIN_FLAGGED_BAG_ELEMENTS_SDK && | |
| val->config.minorVersion == MIN_FLAGGED_BAG_ELEMENTS_MINOR_VERSION) { | |
| bar_split = val.get(); | |
| } else if (val->config.sdkVersion == 0) { | |
| bar_v0 = val.get(); | |
| } | |
| } | |
| } | |
| ASSERT_THAT(foo_v0, NotNull()); | |
| ASSERT_THAT(foo_split, NotNull()); | |
| ASSERT_THAT(bar_v0, NotNull()); | |
| ASSERT_THAT(bar_split, NotNull()); | |
| // Verify foo split | |
| auto* foo_split_style = ValueCast<Style>(foo_split->value.get()); | |
| ASSERT_THAT(foo_split_style, NotNull()); | |
| EXPECT_TRUE(foo_split_style->HasFlaggedElements()); | |
| EXPECT_TRUE(foo_split->uses_readwrite_feature_flags); | |
| // Verify foo fallback | |
| auto* foo_v0_style = ValueCast<Style>(foo_v0->value.get()); | |
| ASSERT_THAT(foo_v0_style, NotNull()); | |
| EXPECT_FALSE(foo_v0_style->HasFlaggedElements()); | |
| EXPECT_FALSE(foo_v0->uses_readwrite_feature_flags); | |
| // Verify bar split | |
| auto* bar_split_style = ValueCast<Style>(bar_split->value.get()); | |
| ASSERT_THAT(bar_split_style, NotNull()); | |
| EXPECT_TRUE(bar_split_style->HasFlaggedElements()); | |
| EXPECT_TRUE(bar_split->uses_readwrite_feature_flags); | |
| // Verify bar fallback | |
| auto* bar_v0_style = ValueCast<Style>(bar_v0->value.get()); | |
| ASSERT_THAT(bar_v0_style, NotNull()); | |
| EXPECT_FALSE(bar_v0_style->HasFlaggedElements()); | |
| EXPECT_FALSE(bar_v0->uses_readwrite_feature_flags); | |
| } | |
| } // namespace aapt |