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

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

Merge "Clean up instrumentation tests"

parents 56f13d5a 0047944c
Loading
Loading
Loading
Loading
+0 −59
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.support.test.uiautomator.UiDevice;

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

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

@RunWith(AndroidJUnit4.class)
@SmallTest
public class DisplaySettingsTest {

    private Instrumentation mInstrumentation;
    private Context mContext;
    private UiDevice mDevice;

    @Before
    public void setUp() {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mContext = mInstrumentation.getTargetContext();
        mDevice = UiDevice.getInstance(mInstrumentation);
    }

    @Test
    public void launchBrightnessLevel_shouldNotCrash() {
        mInstrumentation.startActivitySync(
                new Intent(mContext, DisplaySettings.class));
        onView(withText(mContext.getString(R.string.brightness))).perform(click());
        // should not crash
        mDevice.pressBack(); // dismiss the brightness dialog
    }
}
+0 −110
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

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

import android.app.Activity;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.app.Instrumentation.ActivityResult;
import android.content.Context;
import android.content.Intent;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;

import com.google.android.setupcompat.PartnerCustomizationLayout;
import com.google.android.setupcompat.template.FooterBarMixin;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@MediumTest
public class EncryptionInterstitialTest {

    private Instrumentation mInstrumentation;
    private Context mContext;
    private TestActivityMonitor mActivityMonitor;

    @Before
    public void setUp() {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mContext = mInstrumentation.getTargetContext();
        mActivityMonitor = new TestActivityMonitor();
        mInstrumentation.addMonitor(mActivityMonitor);
    }

    @After
    public void tearDown() {
        mInstrumentation.removeMonitor(mActivityMonitor);
    }

    @Test
    public void clickYes_shouldRequirePassword() {
        final Activity activity = mInstrumentation.startActivitySync(
                new Intent(mContext, EncryptionInterstitial.class)
                        .putExtra("extra_unlock_method_intent", new Intent("test.unlock.intent")));
        final PartnerCustomizationLayout layout = activity.findViewById(R.id.setup_wizard_layout);
        layout.getMixin(FooterBarMixin.class).getPrimaryButtonView().performClick();

        mActivityMonitor.waitForActivityWithTimeout(1000);
        assertEquals(1, mActivityMonitor.getHits());

        assertTrue(mActivityMonitor.mMatchedIntent.getBooleanExtra(
                EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, false));
    }

    @Test
    public void clickNo_shouldNotRequirePassword() {
        final Activity activity = mInstrumentation.startActivitySync(
                new Intent(mContext, EncryptionInterstitial.class)
                        .putExtra("extra_unlock_method_intent", new Intent("test.unlock.intent")));
        final PartnerCustomizationLayout layout = activity.findViewById(R.id.setup_wizard_layout);
        layout.getMixin(FooterBarMixin.class).getSecondaryButtonView().performClick();

        mActivityMonitor.waitForActivityWithTimeout(1000);
        assertEquals(1, mActivityMonitor.getHits());

        assertFalse(mActivityMonitor.mMatchedIntent.getBooleanExtra(
                EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, true));
    }

    private static class TestActivityMonitor extends ActivityMonitor {

        Intent mMatchedIntent = null;

        @Override
        public ActivityResult onStartActivity(Intent intent) {
            if ("test.unlock.intent".equals(intent.getAction())) {
                mMatchedIntent = intent;
                return new ActivityResult(Activity.RESULT_OK, null);
            }
            return null;
        }
    }
}
+0 −126
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import android.app.ActivityManager;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;

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

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

@RunWith(AndroidJUnit4.class)
@SmallTest
public class ManagedAccessSettingsLowRamTest {

    private Instrumentation mInstrumentation;
    private Context mTargetContext;

    @Before
    public void setUp() {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mTargetContext = mInstrumentation.getTargetContext();
    }

    @Test
    public void testManagedAccessOptionsVisibility() throws Exception {
        mInstrumentation.startActivitySync(new Intent(mTargetContext,
                com.android.settings.Settings.AppAndNotificationDashboardActivity.class));
        onView(withText(mTargetContext.getString(R.string.expand_button_title))).perform(click());
        onView(withText(mTargetContext.getString(R.string.special_access))).perform(click());

        String[] managedServiceLabels = new String[] {"Do Not Disturb access",
                "VR helper services", "Notification access", "Picture-in-picture"};
        for (String label : managedServiceLabels) {
            if (ActivityManager.isLowRamDeviceStatic()) {
                onView(withText(label)).check(doesNotExist());
            } else {
                onView(withText(label)).check(matches(isDisplayed()));
            }
        }
    }

    @Test
    public void launchNotificationSetting_onlyWorksIfNotLowRam() {
        final Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);

        mInstrumentation.startActivitySync(intent);

        final String label = "This feature is not available on this device";
        if (ActivityManager.isLowRamDeviceStatic()) {
            onView(withText(label)).check(matches(isDisplayed()));
        } else {
            onView(withText(label)).check(doesNotExist());
        }
    }

    @Test
    public void launchDndSetting_onlyWorksIfNotLowRam() {
        final Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);

        mInstrumentation.startActivitySync(intent);

        final String label = "This feature is not available on this device";
        if (ActivityManager.isLowRamDeviceStatic()) {
            onView(withText(label)).check(matches(isDisplayed()));
        } else {
            onView(withText(label)).check(doesNotExist());
        }
    }

    @Test
    public void launchVrSetting_onlyWorksIfNotLowRam() {
        final Intent intent = new Intent(Settings.ACTION_VR_LISTENER_SETTINGS);

        mInstrumentation.startActivitySync(intent);

        final String label = "This feature is not available on this device";
        if (ActivityManager.isLowRamDeviceStatic()) {
            onView(withText(label)).check(matches(isDisplayed()));
        } else {
            onView(withText(label)).check(doesNotExist());
        }
    }

    @Test
    public void launchPictureInPictureSetting_onlyWorksIfNotLowRam() {
        final Intent intent = new Intent(Settings.ACTION_PICTURE_IN_PICTURE_SETTINGS);

        mInstrumentation.startActivitySync(intent);

        final String label = "This feature is not available on this device";
        if (ActivityManager.isLowRamDeviceStatic()) {
            onView(withText(label)).check(matches(isDisplayed()));
        } else {
            onView(withText(label)).check(doesNotExist());
        }
    }
}
+0 −81
Original line number Diff line number Diff line
@@ -16,12 +16,6 @@

package com.android.settings;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

import static junit.framework.Assert.fail;

import android.app.Instrumentation;
@@ -29,8 +23,6 @@ import android.app.UiAutomation;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
@@ -40,10 +32,6 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class RegulatoryInfoDisplayActivityTest {
@@ -82,73 +70,4 @@ public class RegulatoryInfoDisplayActivityTest {
            return;
        }
    }

    @Test
    public void launchRegulatoryInfo_shouldNotCrash() {
        final Context context = mInstrumentation.getTargetContext();
        final boolean hasRegulatoryInfo = context.getResources()
                .getBoolean(R.bool.config_show_regulatory_info);

        if (!hasRegulatoryInfo) {
            return;
        }
        // Launch intent
        mInstrumentation.startActivitySync(mRegulatoryInfoIntent);

        onView(withId(R.id.regulatoryInfo))
                .inRoot(isDialog())
                .check(matches(isDisplayed()));
    }

    @Test
    public void launchRegulatoryInfo_withInfoImage_shouldDisplay() throws IOException {
        // TODO: Remove "setenforce 0" when selinux rules is updated to give read permission for
        // regulatory info.
        mUiAutomation.executeShellCommand("setenforce 0");

        final boolean tempFileCreated = ensureRegulatoryInfoImageExists();
        try {
            final Context context = mInstrumentation.getTargetContext();
            final boolean hasRegulatoryInfo = context.getResources()
                    .getBoolean(R.bool.config_show_regulatory_info);

            if (!hasRegulatoryInfo) {
                return;
            }
            // Launch intent
            mInstrumentation.startActivitySync(mRegulatoryInfoIntent);

            onView(withId(R.id.regulatoryInfo))
                    .inRoot(isDialog())
                    .check(matches(isDisplayed()));
        } finally {
            if (tempFileCreated) {
                final String filename =
                        RegulatoryInfoDisplayActivity.getRegulatoryInfoImageFileName();
                new File(filename).delete();
                Log.d(TAG, "Deleting temp file " + filename);
            }
        }
    }

    /**
     * Ensures regulatory label image exists on disk.
     *
     * @return true if a test image is created.
     */
    private boolean ensureRegulatoryInfoImageExists() throws IOException {
        final String filename = RegulatoryInfoDisplayActivity.getRegulatoryInfoImageFileName();
        if (new File(filename).exists()) {
            return false;
        }
        Log.d(TAG, "Creating temp file " + filename);
        final Bitmap bitmap = Bitmap.createBitmap(400 /* width */, 400 /* height */,
                Bitmap.Config.ARGB_8888);
        final FileOutputStream out = new FileOutputStream(filename);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100 /* quality */, out);
        out.close();
        return true;
    }


}
+0 −130
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 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;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.test.ActivityInstrumentationTestCase2;

import java.util.List;

/**
 * Tests for the Settings operator/manufacturer hook.
 *
 * Running all tests:
 *
 *   make SettingsTests
 *   adb push SettingsTests.apk /system/app/SettingsTests.apk
 *   adb shell am instrument \
 *    -w com.android.settings.tests/android.test.InstrumentationTestRunner
 */
public class SettingsHookTests extends ActivityInstrumentationTestCase2<Settings> {

    private static final String PACKAGE_NAME = "com.android.settings.tests.unit";

    private static final String KEY_SETTINGS_ROOT = "parent";
    private static final String KEY_SETTINGS_OPERATOR = "operator_settings";
    private static final String KEY_SETTINGS_MANUFACTURER = "manufacturer_settings";

    private static final String INTENT_OPERATOR_HOOK = "com.android.settings.OPERATOR_APPLICATION_SETTING";
    private static final String INTENT_MANUFACTURER_HOOK = "com.android.settings.MANUFACTURER_APPLICATION_SETTING";

    private Settings mSettings;

    public SettingsHookTests() {
        super("com.android.settings", Settings.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mSettings = getActivity();
    }

    /**
     * Test that the operator/manufacturer settings hook test application is
     * available and that it's installed in the device's system image.
     */
    public void testSettingsHookTestAppAvailable() throws Exception {
        Context context = mSettings.getApplicationContext();
        PackageManager pm = context.getPackageManager();
        ApplicationInfo applicationInfo = pm.getApplicationInfo(PACKAGE_NAME, 0);
        assertTrue((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
    }

    /**
     * Test that the operator test activity has registered an intent-filter for
     * an action named 'android.settings.OPERATOR_APPLICATION_SETTING'.
     */
    public void testOperatorIntentFilter() {
        boolean result = false;
        Context context = mSettings.getApplicationContext();
        PackageManager pm = context.getPackageManager();
        Intent intent = new Intent(INTENT_OPERATOR_HOOK);
        List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : list) {
            if (resolveInfo.activityInfo.packageName.equals(PACKAGE_NAME)) {
                result = true;
            }
        }
        assertTrue("Intent-filter not found", result);
    }

    /**
     * Test that the manufacturer test activity has registered an intent-filter
     * for an action named 'android.settings.MANUFACTURER_APPLICATION_SETTING'.
     */
    public void testManufacturerIntentFilter() {
        boolean result = false;
        Context context = mSettings.getApplicationContext();
        PackageManager pm = context.getPackageManager();
        Intent intent = new Intent(INTENT_MANUFACTURER_HOOK);
        List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : list) {
            if (resolveInfo.activityInfo.packageName.equals(PACKAGE_NAME)) {
                result = true;
            }
        }
        assertTrue("Intent-filter not found", result);
    }

    /**
     * Test that the operator preference is available in the Settings
     * application.
     */
    public void testOperatorPreferenceAvailable() {
// TODO: fix this test case to work with fragments
//        PreferenceGroup root = (PreferenceGroup)mSettings.findPreference(KEY_SETTINGS_ROOT);
//        Preference operatorPreference = root.findPreference(KEY_SETTINGS_OPERATOR);
//        assertNotNull(operatorPreference);
    }

    /**
     * Test that the manufacturer preference is available in the Settings
     * application.
     */
    public void testManufacturerPreferenceAvailable() {
// TODO: fix this test case to work with fragments
//        PreferenceGroup root = (PreferenceGroup)mSettings.findPreference(KEY_SETTINGS_ROOT);
//        Preference manufacturerHook = root.findPreference(KEY_SETTINGS_MANUFACTURER);
//        assertNotNull(manufacturerHook);
    }

}
Loading