Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 07e4d42b authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "resource flagging on xml elements" into main

parents 7f1c0c30 74684035
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ android_test {
    name: "ResourceFlaggingTests",
    srcs: [
        "src/**/*.java",
        ":resource-flagging-test-app-r-java",
    ],
    platform_apis: true,
    certificate: "platform",
+49 −29
Original line number Diff line number Diff line
@@ -19,15 +19,22 @@ package com.android.resourceflaggingtests;
import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.ApkAssets;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.FileUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;

import com.android.intenal.flaggedresources.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -46,7 +53,14 @@ public class ResourceFlaggingTest {
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getTargetContext();
        AssetManager assets = new AssetManager();
        assertThat(assets.addAssetPath(extractApkAndGetPath(R.raw.resapp))).isNotEqualTo(0);
        assets.setApkAssets(
                new ApkAssets[]{
                        ApkAssets.loadFromPath(
                                extractApkAndGetPath(
                                        com.android.resourceflaggingtests.R.raw.resapp
                                )
                        )
                }, true);

        final DisplayMetrics dm = new DisplayMetrics();
        dm.setToDefaults();
@@ -55,54 +69,60 @@ public class ResourceFlaggingTest {

    @Test
    public void testFlagDisabled() {
        assertThat(getBoolean("res1")).isTrue();
        assertThat(mResources.getBoolean(R.bool.bool1)).isTrue();
    }

    @Test
    public void testFlagEnabled() {
        assertThat(getBoolean("res2")).isTrue();
        assertThat(mResources.getBoolean(R.bool.bool2)).isTrue();
    }

    @Test
    public void testFlagEnabledDifferentCompilationUnit() {
        assertThat(getBoolean("res3")).isTrue();
        assertThat(mResources.getBoolean(R.bool.bool3)).isTrue();
    }

    @Test
    public void testFlagDisabledStringArrayElement() {
        assertThat(getStringArray("strarr1")).isEqualTo(new String[]{"one", "two", "three"});
        assertThat(mResources.getStringArray(R.array.strarr1))
                .isEqualTo(new String[]{"one", "two", "three"});
    }

    @Test
    public void testFlagDisabledIntArrayElement() {
        assertThat(getIntArray("intarr1")).isEqualTo(new int[]{1, 2, 3});
        assertThat(mResources.getIntArray(R.array.intarr1)).isEqualTo(new int[]{1, 2, 3});
    }

    private boolean getBoolean(String name) {
        int resId = mResources.getIdentifier(
                name,
                "bool",
                "com.android.intenal.flaggedresources");
        assertThat(resId).isNotEqualTo(0);
        return mResources.getBoolean(resId);
    @Test
    public void testLayoutWithDisabledElements() {
        LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.layout1, null);
        assertThat(ll).isNotNull();
        assertThat((View) ll.findViewById(R.id.text1)).isNotNull();
        assertThat((View) ll.findViewById(R.id.disabled_text)).isNull();
        assertThat((View) ll.findViewById(R.id.text2)).isNotNull();
    }

    private String[] getStringArray(String name) {
        int resId = mResources.getIdentifier(
                name,
                "array",
                "com.android.intenal.flaggedresources");
        assertThat(resId).isNotEqualTo(0);
        return mResources.getStringArray(resId);
    private LayoutInflater getLayoutInflater() {
        ContextWrapper c = new ContextWrapper(mContext) {
            private LayoutInflater mInflater;

            @Override
            public Resources getResources() {
                return mResources;
            }

    private int[] getIntArray(String name) {
        int resId = mResources.getIdentifier(
                name,
                "array",
                "com.android.intenal.flaggedresources");
        assertThat(resId).isNotEqualTo(0);
        return mResources.getIntArray(resId);
            @Override
            public Object getSystemService(String name) {
                if (LAYOUT_INFLATER_SERVICE.equals(name)) {
                    if (mInflater == null) {
                        mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
                    }
                    return mInflater;
                }
                return super.getSystemService(name);
            }
        };
        return LayoutInflater.from(c);
    }

    private String extractApkAndGetPath(int id) throws Exception {
+9 −0
Original line number Diff line number Diff line
@@ -306,6 +306,7 @@ struct ResourceFileFlattenerOptions {
  OutputFormat output_format = OutputFormat::kApk;
  std::unordered_set<std::string> extensions_to_not_compress;
  std::optional<std::regex> regex_to_not_compress;
  FeatureFlagValues feature_flag_values;
};

// A sampling of public framework resource IDs.
@@ -672,6 +673,13 @@ bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archiv
              }
            }

            FeatureFlagsFilterOptions flags_filter_options;
            flags_filter_options.flags_must_be_readonly = true;
            FeatureFlagsFilter flags_filter(options_.feature_flag_values, flags_filter_options);
            if (!flags_filter.Consume(context_, doc.get())) {
              return 1;
            }

            error |= !FlattenXml(context_, *doc, dst_path, options_.keep_raw_values,
                                 false /*utf16*/, options_.output_format, archive_writer);
          }
@@ -1926,6 +1934,7 @@ class Linker {
        static_cast<bool>(options_.generate_proguard_rules_path);
    file_flattener_options.output_format = options_.output_format;
    file_flattener_options.do_not_fail_on_missing_resources = options_.merge_only;
    file_flattener_options.feature_flag_values = options_.feature_flag_values;

    ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set);
    if (!file_flattener.Flatten(table, writer)) {
+10 −2
Original line number Diff line number Diff line
@@ -30,12 +30,14 @@ genrule {
        "res/values/bools2.xml",
        "res/values/ints.xml",
        "res/values/strings.xml",
        "res/layout/layout1.xml",
    ],
    out: [
        "values_bools.arsc.flat",
        "values_bools2.arsc.flat",
        "values_ints.arsc.flat",
        "values_strings.arsc.flat",
        "layout_layout1.xml.flat",
    ],
    cmd: "$(location aapt2) compile $(in) -o $(genDir) " +
        "--feature-flags test.package.falseFlag:ro=false,test.package.trueFlag:ro=true",
@@ -52,7 +54,10 @@ genrule {
    out: [
        "resapp.apk",
    ],
    cmd: "$(location aapt2) link -o $(out) --manifest $(in)",
    cmd: "$(location aapt2) link -o $(out) --manifest $(in) " +
        "-I $(location :current_android_jar) " +
        "--feature-flags test.package.falseFlag:ro=false,test.package.trueFlag:ro=true",
    tool_files: [":current_android_jar"],
}

genrule {
@@ -66,7 +71,10 @@ genrule {
    out: [
        "resource-flagging-java/com/android/intenal/flaggedresources/R.java",
    ],
    cmd: "$(location aapt2) link -o $(genDir)/resapp.apk --java $(genDir)/resource-flagging-java --manifest $(in)",
    cmd: "$(location aapt2) link -o $(genDir)/resapp.apk --java $(genDir)/resource-flagging-java --manifest $(in) " +
        "-I $(location :current_android_jar) " +
        "--feature-flags test.package.falseFlag:ro=false,test.package.trueFlag:ro=true",
    tool_files: [":current_android_jar"],
}

java_genrule {
+18 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/disabled_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:featureFlag="test.package.falseFlag" />
    <TextView android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:featureFlag="test.package.trueFlag" />
</LinearLayout>
 No newline at end of file
Loading