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

Commit 87dac36d authored by Hyundo Moon's avatar Hyundo Moon
Browse files

OPP: Enable/disable only necessary activities in tests

When running OPP activity related tests, the tests enabled/disabled
unnecessary activities including which should always be enabled.

As a result, device could not send a file via Bluetooth until reboot
since BluetoothOppLauncherActivity was disabled after any of those
tests.

This CL makes only nessceary activities be enabled/disabled during
the tests.

Bug: 324185238
Test: atest BluetoothOppBtEnableActivityTest \
            BluetoothOppBtEnablingActivityTest \
            BluetoothOppLauncherActivityTest \
            BluetoothOppReceiverTest \
            BluetoothOppTransferActivityTest \
            BluetoothOppTransferHistoryTest \
            BluetoothOppUtilityTest \
            IncomingFileConfirmActivityTest
Change-Id: If2930858d122ee20b8ebf5c1c381018182478f93
parent 5c664cbc
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -61,7 +61,6 @@ public class BluetoothOppBtEnableActivityTest {
        mIntent = new Intent();
        mIntent.setClass(mTargetContext, BluetoothOppBtEnableActivity.class);
        Intents.init();
        BluetoothOppTestUtils.enableOppActivities(true, mTargetContext);
        TestUtils.setUpUiTest();
    }

@@ -69,7 +68,6 @@ public class BluetoothOppBtEnableActivityTest {
    public void tearDown() throws Exception {
        TestUtils.tearDownUiTest();
        Intents.release();
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }

    @Test
+0 −2
Original line number Diff line number Diff line
@@ -80,7 +80,6 @@ public class BluetoothOppBtEnablingActivityTest {
        mIntent.setClass(mTargetContext, BluetoothOppBtEnablingActivity.class);

        mRealTimeoutValue = BluetoothOppBtEnablingActivity.sBtEnablingTimeoutMs;
        BluetoothOppTestUtils.enableOppActivities(true, mTargetContext);
        TestUtils.setUpUiTest();
    }

@@ -89,7 +88,6 @@ public class BluetoothOppBtEnablingActivityTest {
        TestUtils.tearDownUiTest();
        BluetoothMethodProxy.setInstanceForTesting(null);
        BluetoothOppBtEnablingActivity.sBtEnablingTimeoutMs = mRealTimeoutValue;
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }

    @Ignore("b/277594572")
+0 −2
Original line number Diff line number Diff line
@@ -87,7 +87,6 @@ public class BluetoothOppLauncherActivityTest {
        mIntent = new Intent();
        mIntent.setClass(mTargetContext, BluetoothOppLauncherActivity.class);

        BluetoothOppTestUtils.enableOppActivities(true, mTargetContext);
        TestUtils.setUpUiTest();

        BluetoothOppManager.setInstance(mBluetoothOppManager);
@@ -100,7 +99,6 @@ public class BluetoothOppLauncherActivityTest {
        BluetoothMethodProxy.setInstanceForTesting(null);
        BluetoothOppManager.setInstance(null);
        Intents.release();
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }

    @Test
+62 −31
Original line number Diff line number Diff line
@@ -90,8 +90,6 @@ public class BluetoothOppReceiverTest {
        mReceiver = new BluetoothOppReceiver();

        Intents.init();

        BluetoothOppTestUtils.enableOppActivities(true, mContext);
        TestUtils.setUpUiTest();
    }

@@ -114,24 +112,41 @@ public class BluetoothOppReceiverTest {
        Intent intent = new Intent();
        intent.setAction(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        ActivityScenario<BluetoothOppBtEnableActivity> activityScenario
                = ActivityScenario.launch(BluetoothOppBtEnableActivity.class);
        activityScenario.onActivity(activity -> {

        try {
            BluetoothOppTestUtils.enableActivity(
                    BluetoothOppBtEnableActivity.class, true, mContext);
            ActivityScenario<BluetoothOppBtEnableActivity> activityScenario =
                    ActivityScenario.launch(BluetoothOppBtEnableActivity.class);
            activityScenario.onActivity(
                    activity -> {
                        mReceiver.onReceive(mContext, intent);
                    });
            doNothing().when(bluetoothOppManager).startTransfer(eq(device));
            verify(bluetoothOppManager).startTransfer(eq(device));
            BluetoothOppManager.setInstance(null);
        } finally {
            BluetoothOppTestUtils.enableActivity(
                    BluetoothOppBtEnableActivity.class, false, mContext);
        }
    }

    @Test
    public void onReceive_withActionIncomingFileConfirm_startsIncomingFileConfirmActivity() {
        mSetFlagsRule.disableFlags(Flags.FLAG_OPP_START_ACTIVITY_DIRECTLY_FROM_NOTIFICATION);
        try {
            BluetoothOppTestUtils.enableActivity(
                    BluetoothOppIncomingFileConfirmActivity.class, true, mContext);

            Intent intent = new Intent();
            intent.setAction(Constants.ACTION_INCOMING_FILE_CONFIRM);
            intent.setData(Uri.parse("content:///not/important"));
            mReceiver.onReceive(mContext, intent);
            intended(hasComponent(BluetoothOppIncomingFileConfirmActivity.class.getName()));
        } finally {
            BluetoothOppTestUtils.enableActivity(
                    BluetoothOppIncomingFileConfirmActivity.class, false, mContext);
        }
    }

    @Test
@@ -161,28 +176,44 @@ public class BluetoothOppReceiverTest {
    @Test
    public void onReceive_withActionOutboundTransfer_startsTransferHistoryActivity() {
        mSetFlagsRule.disableFlags(Flags.FLAG_OPP_START_ACTIVITY_DIRECTLY_FROM_NOTIFICATION);
        try {
            BluetoothOppTestUtils.enableActivity(BluetoothOppTransferHistory.class, true, mContext);

            Intent intent = new Intent();
            intent.setAction(Constants.ACTION_OPEN_OUTBOUND_TRANSFER);
            intent.setData(Uri.parse("content:///not/important"));
        intending(anyIntent()).respondWith(
            intending(anyIntent())
                    .respondWith(
                            new Instrumentation.ActivityResult(Activity.RESULT_OK, new Intent()));

            mReceiver.onReceive(mContext, intent);
            intended(hasComponent(BluetoothOppTransferHistory.class.getName()));
            intended(hasExtra(Constants.EXTRA_DIRECTION, BluetoothShare.DIRECTION_OUTBOUND));
        } finally {
            BluetoothOppTestUtils.enableActivity(
                    BluetoothOppTransferHistory.class, false, mContext);
        }
    }

    @Test
    public void onReceive_withActionInboundTransfer_startsTransferHistoryActivity() {
        mSetFlagsRule.disableFlags(Flags.FLAG_OPP_START_ACTIVITY_DIRECTLY_FROM_NOTIFICATION);
        try {
            BluetoothOppTestUtils.enableActivity(BluetoothOppTransferHistory.class, true, mContext);

            Intent intent = new Intent();
            intent.setAction(Constants.ACTION_OPEN_INBOUND_TRANSFER);
            intent.setData(Uri.parse("content:///not/important"));
        intending(anyIntent()).respondWith(
            intending(anyIntent())
                    .respondWith(
                            new Instrumentation.ActivityResult(Activity.RESULT_OK, new Intent()));
            mReceiver.onReceive(mContext, intent);
            intended(hasComponent(BluetoothOppTransferHistory.class.getName()));
            intended(hasExtra(Constants.EXTRA_DIRECTION, BluetoothShare.DIRECTION_INBOUND));
        } finally {
            BluetoothOppTestUtils.enableActivity(
                    BluetoothOppTransferHistory.class, false, mContext);
        }
    }

    @Test
+15 −26
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import android.database.Cursor;

import org.mockito.internal.util.MockUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@@ -115,35 +114,25 @@ public class BluetoothOppTestUtils {
    }

    /**
     * Enable/Disable all activities in Opp for testing
     * Enable/Disable an activity for testing
     *
     * @param activityClass the activity class to enable/disable
     * @param enable true to enable, false to disable
     * @param mTargetContext target context
     */
    public static void enableOppActivities(boolean enable, Context mTargetContext) {
        int enabledState = enable ? COMPONENT_ENABLED_STATE_ENABLED
                : COMPONENT_ENABLED_STATE_DEFAULT;
    public static void enableActivity(Class activityClass, boolean enable, Context mTargetContext) {
        int enabledState =
                enable ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DEFAULT;

        mTargetContext.getPackageManager().setApplicationEnabledSetting(
        mTargetContext
                .getPackageManager()
                .setApplicationEnabledSetting(
                        mTargetContext.getPackageName(), enabledState, DONT_KILL_APP);

        // All activities to be test
        Class[] activities = {
                BluetoothOppTransferActivity.class,
                BluetoothOppBtEnableActivity.class,
                BluetoothOppBtEnablingActivity.class,
                BluetoothOppBtErrorActivity.class,
                BluetoothOppIncomingFileConfirmActivity.class,
                BluetoothOppTransferHistory.class,
                BluetoothOppLauncherActivity.class,
        };

        Arrays.stream(activities).forEach(activityClass -> {
        ComponentName activityName = new ComponentName(mTargetContext, activityClass);
            mTargetContext.getPackageManager().setComponentEnabledSetting(
                    activityName, enabledState, DONT_KILL_APP);
        });

        mTargetContext
                .getPackageManager()
                .setComponentEnabledSetting(activityName, enabledState, DONT_KILL_APP);
    }
}
Loading