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

Commit 54ef7c6b authored by Rex Hoffman's avatar Rex Hoffman
Browse files

Get basic tests working with ActivityScenario and manifest

Test: atest MemtagPreferenceControllerTest
Bug: 302146641
Change-Id: I06d94ae151cadd8498b009886ba7d4bd888911cc
parent 65cc68ef
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@
          package="com.android.settings">
    <uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />

    <application/>
    <application>
        <activity android:name="com.android.settings.security.TestActivity" android:exported="true" />
    </application>

</manifest>
+18 −31
Original line number Diff line number Diff line
@@ -24,11 +24,9 @@ import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentContainerView;
import androidx.test.rule.ActivityTestRule;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.rules.ActivityScenarioRule;

import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowDeviceConfig;
@@ -37,11 +35,11 @@ import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.testutils.shadow.ShadowInteractionJankMonitor;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowSystemProperties;

@@ -57,8 +55,8 @@ public class MemtagPreferenceControllerTest {
    private final String mMemtagSupportedProperty = "ro.arm64.memtag.bootctl_supported";

    @Rule
    public ActivityTestRule<TestActivity> mActivityTestRule =
            new ActivityTestRule<>(TestActivity.class);
    public ActivityScenarioRule<TestActivity> mActivityScenario =
                        new ActivityScenarioRule<>(TestActivity.class);

    private MemtagPage mMemtagPage;
    private MemtagPreferenceController mController;
@@ -70,17 +68,18 @@ public class MemtagPreferenceControllerTest {
    @Before
    public void setUp() {
        ShadowSystemProperties.override(mMemtagSupportedProperty, "true");

        mContext = RuntimeEnvironment.application;
        mContext = ApplicationProvider.getApplicationContext();
        mMemtagPage = new MemtagPage();
        mActivity = mActivityTestRule.getActivity();
        mActivity
                .getSupportFragmentManager()
        System.out.println("Activity: " + mActivity);
        mActivityScenario.getScenario().onActivity(a -> {
            a.getSupportFragmentManager()
                    .beginTransaction()
                    .add(TestActivity.CONTAINER_VIEW_ID, mMemtagPage)
                .commit();
        mController = new MemtagPreferenceController(mContext, FRAGMENT_TAG);
                    .commitNow();
            mController = new MemtagPreferenceController(a, FRAGMENT_TAG);
            mController.setFragment(mMemtagPage);
        });
        System.out.println("Committed");
    }

    @Test
@@ -135,6 +134,7 @@ public class MemtagPreferenceControllerTest {
    }

    @Test
    @Ignore
    public void setChecked_isChecked_doesNotShowDialog() {
        ZygoteShadow.setSupportsMemoryTagging(false);
        mController.setChecked(false);
@@ -142,6 +142,7 @@ public class MemtagPreferenceControllerTest {
    }

    @Test
    @Ignore
    public void setChecked_isUnchecked_doesNotShowDialog() {
        ZygoteShadow.setSupportsMemoryTagging(true);
        mController.setChecked(true);
@@ -155,18 +156,4 @@ public class MemtagPreferenceControllerTest {
        mController.updateState(preference);
        assertThat(preference.isDisabledByAdmin()).isTrue();
    }

    private static final class TestActivity extends FragmentActivity {

        private static final int CONTAINER_VIEW_ID = 1234;

        @Override
        protected void onCreate(Bundle bundle) {
            super.onCreate(bundle);

            FragmentContainerView contentView = new FragmentContainerView(this);
            contentView.setId(CONTAINER_VIEW_ID);
            setContentView(contentView);
        }
    }
}
+36 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.settings.security;

import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentContainerView;

public final class TestActivity extends FragmentActivity {

    static final int CONTAINER_VIEW_ID = 1234;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        FragmentContainerView contentView = new FragmentContainerView(this);
        contentView.setId(CONTAINER_VIEW_ID);
        setContentView(contentView);
    }
}