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

Commit 16bf00a0 authored by Jeremy Meyer's avatar Jeremy Meyer Committed by Android (Google) Code Review
Browse files

Merge "First pass at flagged resources" into main

parents f8915564 211bec28
Loading
Loading
Loading
Loading
+75 −0
Original line number Diff line number Diff line
// Copyright (C) 2024 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.

package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
    default_team: "trendy_team_android_resources",
}

genrule {
    name: "resource-flagging-test-app-resources-compile",
    tools: ["aapt2"],
    srcs: [
        "flagged_resources_res/values/bools.xml",
    ],
    out: ["values_bools.arsc.flat"],
    cmd: "$(location aapt2) compile $(in) -o $(genDir) " +
        "--feature-flags test.package.falseFlag:ro=false,test.package.trueFlag:ro=true",
}

genrule {
    name: "resource-flagging-test-app-apk",
    tools: ["aapt2"],
    // The first input file in the list must be the manifest
    srcs: [
        "TestAppAndroidManifest.xml",
        ":resource-flagging-test-app-resources-compile",
    ],
    out: ["resapp.apk"],
    cmd: "$(location aapt2) link -o $(out) --manifest $(in)",
}

java_genrule {
    name: "resource-flagging-apk-as-resource",
    srcs: [
        ":resource-flagging-test-app-apk",
    ],
    out: ["apks_as_resources.res.zip"],
    tools: ["soong_zip"],

    cmd: "mkdir -p $(genDir)/res/raw && " +
        "cp $(in) $(genDir)/res/raw/$$(basename $(in)) && " +
        "$(location soong_zip) -o $(out) -C $(genDir)/res -D $(genDir)/res",
}

android_test {
    name: "ResourceFlaggingTests",
    srcs: [
        "src/**/*.java",
    ],
    platform_apis: true,
    certificate: "platform",
    static_libs: [
        "androidx.test.rules",
        "testng",
        "compatibility-device-util-axt",
    ],
    resource_zips: [":resource-flagging-apk-as-resource"],
    test_suites: ["device-tests"],
}
+31 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) 2024 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.
  -->

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.resourceflaggingtests">

    <application>
        <uses-library android:name="android.test.runner" />
    </application>

    <instrumentation
        android:name="androidx.test.runner.AndroidJUnitRunner"
        android:targetPackage="com.android.resourceflaggingtests"
        android:label="Resource Flagging Tests" />

</manifest>
 No newline at end of file
+4 −0
Original line number Diff line number Diff line
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.intenal.flaggedresources">
    <application/>
</manifest>
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <bool name="res1">true</bool>
    <bool name="res1" android:featureFlag="test.package.falseFlag">false</bool>

    <bool name="res2">false</bool>
    <bool name="res2" android:featureFlag="test.package.trueFlag">true</bool>
</resources>
 No newline at end of file
+81 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.
 */

package com.android.resourceflaggingtests;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.FileUtils;
import android.util.DisplayMetrics;

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

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.io.InputStream;

@RunWith(JUnit4.class)
@SmallTest
public class ResourceFlaggingTest {
    private Context mContext;
    private Resources mResources;

    @Before
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getTargetContext();
        AssetManager assets = new AssetManager();
        assertThat(assets.addAssetPath(extractApkAndGetPath(R.raw.resapp))).isNotEqualTo(0);

        final DisplayMetrics dm = new DisplayMetrics();
        dm.setToDefaults();
        mResources = new Resources(assets, dm, new Configuration());
    }

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

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

    private boolean getBoolean(String name) {
        int resId = mResources.getIdentifier(name, "bool", "com.android.intenal.flaggedresources");
        assertThat(resId).isNotEqualTo(0);
        return mResources.getBoolean(resId);
    }

    private String extractApkAndGetPath(int id) throws Exception {
        final Resources resources = mContext.getResources();
        try (InputStream is = resources.openRawResource(id)) {
            File path = new File(mContext.getFilesDir(), resources.getResourceEntryName(id));
            path.deleteOnExit();
            FileUtils.copyToFileOrThrow(is, path);
            return path.getAbsolutePath();
        }
    }
}
Loading