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

Commit 5a910978 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add tests for layout overlaying"

parents a59b8ae7 df9e7329
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2019 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.
  -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout android:id="@id/view_1"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

        <com.android.overlaytest.view.TestTextView
            android:id="@id/view_2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            app:customAttribute="none"/>

        <com.android.overlaytest.view.TestTextView
            android:id="@id/view_3"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            app:customAttribute="none" />
    </LinearLayout>
</LinearLayout>
 No newline at end of file
+12 −0
Original line number Diff line number Diff line
@@ -56,4 +56,16 @@
        <item>17</item>
        <item>19</item>
    </integer-array>

    <attr name="customAttribute" />
    <id name="view_1" />
    <id name="view_2" />
    <id name="view_3" />

    <!-- Stabilize the ids of attributes and ids used in test layouts so that they differ from the
     overlay resource ids -->
    <public type="attr" name="customAttribute" id="0x7f200000"/>
    <public type="id" name="view_1" id="0x7f210000"/>
    <public type="id" name="view_2" id="0x7f210001"/>
    <public type="id" name="view_3" id="0x7f210002"/>
</resources>
+53 −1
Original line number Diff line number Diff line
@@ -18,20 +18,26 @@ package com.android.overlaytest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.LocaleList;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;

import androidx.test.InstrumentationRegistry;

import com.android.internal.util.ArrayUtils;
import com.android.overlaytest.view.TestTextView;

import org.junit.Before;
import org.junit.Ignore;
@@ -45,6 +51,7 @@ import java.util.Locale;

@Ignore
public abstract class OverlayBaseTest {
    private Context mContext;
    private Resources mResources;
    private final int mMode;
    static final int MODE_NO_OVERLAY = 0;
@@ -61,7 +68,8 @@ public abstract class OverlayBaseTest {

    @Before
    public void setUp() {
        mResources = InstrumentationRegistry.getContext().getResources();
        mContext = InstrumentationRegistry.getContext();
        mResources = mContext.getResources();
    }

    private int calculateRawResourceChecksum(int resId) throws Throwable {
@@ -321,6 +329,50 @@ public abstract class OverlayBaseTest {
        assertEquals("com.android.overlaytest", contents);
    }

    @Test
    public void testRewrite() throws Throwable {
        final TypedValue result = new TypedValue();
        mResources.getValue(R.string.str, result, true);
        assertEquals(result.resourceId & 0xff000000, 0x7f000000);
    }

    @Test
    public void testOverlayLayout() throws Throwable {
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        final View layout = inflater.inflate(R.layout.layout, null);
        assertNotNull(layout.findViewById(R.id.view_1));

        final TestTextView view2 = layout.findViewById(R.id.view_2);
        assertNotNull(view2);
        switch (mMode) {
            case MODE_NO_OVERLAY:
                assertEquals("none", view2.getCustomAttributeValue());
                break;
            case MODE_SINGLE_OVERLAY:
                assertEquals("single", view2.getCustomAttributeValue());
                break;
            case MODE_MULTIPLE_OVERLAYS:
                assertEquals("multiple", view2.getCustomAttributeValue());
                break;
            default:
                fail("Unknown mode " + mMode);
        }

        final TestTextView view3 = layout.findViewById(R.id.view_3);
        assertNotNull(view3);
        switch (mMode) {
            case MODE_NO_OVERLAY:
                assertEquals("none", view3.getCustomAttributeValue());
                break;
            case MODE_SINGLE_OVERLAY:
            case MODE_MULTIPLE_OVERLAYS:
                assertEquals("overlaid", view3.getCustomAttributeValue());
                break;
            default:
                fail("Unknown mode " + mMode);
        }
    }

    /*
     * testMatrix* tests
     *
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.overlaytest.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

public class TestTextView extends TextView {

    private final String mCustomAttributeValue;

    public TestTextView(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.textViewStyle, 0);
    }

    public TestTextView(Context context, AttributeSet attrs,
            int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        int[] testResources = new int[]{com.android.overlaytest.R.attr.customAttribute};
        final Resources.Theme theme = context.getTheme();
        TypedArray typedArray = theme.obtainStyledAttributes(attrs, testResources, defStyleAttr,
                defStyleRes);
        mCustomAttributeValue = typedArray.getString(0);
    }

    public String getCustomAttributeValue() {
        return mCustomAttributeValue;
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -19,5 +19,6 @@
        android:versionCode="1"
        android:versionName="1.0">
        <application android:hasCode="false" />
        <overlay android:targetPackage="com.android.overlaytest" />
        <overlay android:targetPackage="com.android.overlaytest"
                 android:resourcesMap="@xml/overlays"/>
</manifest>
Loading