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

Commit 87b3c0dc authored by Kenny Root's avatar Kenny Root Committed by Android Git Automerger
Browse files

am 08d9d9a4: Merge "Runtime resource overlay, iteration 1."

* commit '08d9d9a4':
  Runtime resource overlay, iteration 1.
parents 4430670f 08d9d9a4
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
# Dummy makefile to halt recursive directory traversal.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
+10 −0
Original line number Diff line number Diff line
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := tests

LOCAL_PACKAGE_NAME := OverlayTest

LOCAL_SRC_FILES := $(call all-java-files-under, src)

include $(BUILD_PACKAGE)
+10 −0
Original line number Diff line number Diff line
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.overlaytest">
    <uses-permission android:name="android.permission.RUN_INSTRUMENTATION"/>
    <application>
        <uses-library android:name="android.test.runner"/>
    </application>
    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.android.overlaytest"
        android:label="Runtime resource overlay tests"/>
</manifest>
+118 −0
Original line number Diff line number Diff line
package com.android.overlaytest;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.test.AndroidTestCase;
import java.io.InputStream;
import java.util.Locale;

public abstract class OverlayBaseTest extends AndroidTestCase {
    private Resources mResources;
    protected boolean mWithOverlay; // will be set by subclasses

    protected void setUp() {
        mResources = getContext().getResources();
    }

    private int calculateRawResourceChecksum(int resId) throws Throwable {
        InputStream input = null;
        try {
            input = mResources.openRawResource(resId);
            int ch, checksum = 0;
            while ((ch = input.read()) != -1) {
                checksum = (checksum + ch) % 0xffddbb00;
            }
            return checksum;
        } finally {
            input.close();
        }
    }

    private void setLocale(String code) {
        Locale locale = new Locale(code);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        mResources.updateConfiguration(config, mResources.getDisplayMetrics());
    }

    private void assertResource(int resId, boolean ewo, boolean ew) throws Throwable {
        boolean expected = mWithOverlay ? ew : ewo;
        boolean actual = mResources.getBoolean(resId);
        assertEquals(expected, actual);
    }

    private void assertResource(int resId, String ewo, String ew) throws Throwable {
        String expected = mWithOverlay ? ew : ewo;
        String actual = mResources.getString(resId);
        assertEquals(expected, actual);
    }

    private void assertResource(int resId, int[] ewo, int[] ew) throws Throwable {
        int[] expected = mWithOverlay ? ew : ewo;
        int[] actual = mResources.getIntArray(resId);
        assertEquals("length:", expected.length, actual.length);
        for (int i = 0; i < actual.length; ++i) {
            assertEquals("index " + i + ":", actual[i], expected[i]);
        }
    }

    public void testBooleanOverlay() throws Throwable {
        // config_automatic_brightness_available has overlay (default config)
        final int resId = com.android.internal.R.bool.config_automatic_brightness_available;
        assertResource(resId, false, true);
    }

    public void testBoolean() throws Throwable {
        // config_bypass_keyguard_if_slider_open has no overlay
        final int resId = com.android.internal.R.bool.config_bypass_keyguard_if_slider_open;
        assertResource(resId, true, true);
    }

    public void testStringOverlay() throws Throwable {
        // phoneTypeCar has an overlay (default config), which shouldn't shadow
        // the Swedish translation
        final int resId = com.android.internal.R.string.phoneTypeCar;
        setLocale("sv_SE");
        assertResource(resId, "Bil", "Bil");
    }

    public void testStringSwedishOverlay() throws Throwable {
        // phoneTypeWork has overlay (no default config, only for lang=sv)
        final int resId = com.android.internal.R.string.phoneTypeWork;
        setLocale("en_US");
        assertResource(resId, "Work", "Work");
        setLocale("sv_SE");
        assertResource(resId, "Arbete", "Jobb");
    }

    public void testString() throws Throwable {
        // phoneTypeHome has no overlay
        final int resId = com.android.internal.R.string.phoneTypeHome;
        setLocale("en_US");
        assertResource(resId, "Home", "Home");
        setLocale("sv_SE");
        assertResource(resId, "Hem", "Hem");
    }

    public void testIntegerArrayOverlay() throws Throwable {
        // config_scrollBarrierVibePattern has overlay (default config)
        final int resId = com.android.internal.R.array.config_scrollBarrierVibePattern;
        assertResource(resId, new int[]{0, 15, 10, 10}, new int[]{100, 200, 300});
    }

    public void testIntegerArray() throws Throwable {
        // config_virtualKeyVibePattern has no overlay
        final int resId = com.android.internal.R.array.config_virtualKeyVibePattern;
        final int[] expected = {0, 10, 20, 30};
        assertResource(resId, expected, expected);
    }

    public void testAsset() throws Throwable {
        // drawable/default_background.jpg has overlay (default config)
        final int resId = com.android.internal.R.drawable.default_wallpaper;
        int actual = calculateRawResourceChecksum(resId);
        int expected = mWithOverlay ? 0x000051da : 0x0014ebce;
        assertEquals(expected, actual);
    }
}
+7 −0
Original line number Diff line number Diff line
package com.android.overlaytest;

public class WithOverlayTest extends OverlayBaseTest {
    public WithOverlayTest() {
        mWithOverlay = true;
    }
}
Loading