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

Commit 42904120 authored by My Name's avatar My Name Committed by Hieu Dang
Browse files

Fix Opp Activities Bugs

Bug: 260295342
Bug: 237467631
Bug: 260188543
Tag: #refactor
Test: atest BluetoothOppBtEnableActivityTest
Test: atest BluetoothOppBtEnablingActivityTest
Test: atest BluetoothOppIncomingFileConfirmActivityTest
Test: atest BluetoothOppLauncherActivityTest
Change-Id: Ie5b57bc8789223d71c6ace4f4878e46a186c8fb3
parent 948397b9
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -58,13 +58,13 @@ public class BluetoothOppBtEnableActivityTest {
        mIntent = new Intent();
        mIntent.setClass(mTargetContext, BluetoothOppBtEnableActivity.class);
        Intents.init();
        enableActivity(true);
        BluetoothOppTestUtils.enableOppActivities(true, mTargetContext);
    }

    @After
    public void tearDown() {
        Intents.release();
        enableActivity(false);
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }

    @Test
+2 −2
Original line number Diff line number Diff line
@@ -76,14 +76,14 @@ public class BluetoothOppBtEnablingActivityTest {
        mIntent.setClass(mTargetContext, BluetoothOppBtEnablingActivity.class);

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

    @After
    public void tearDown() {
        BluetoothMethodProxy.setInstanceForTesting(null);
        BluetoothOppBtEnablingActivity.sBtEnablingTimeoutMs = mRealTimeoutValue;
        enableActivity(false);
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }

    @Test
+23 −75
Original line number Diff line number Diff line
@@ -16,10 +16,7 @@

package com.android.bluetooth.opp;

import static android.content.pm.PackageManager.DONT_KILL_APP;
import static android.service.pm.PackageProto.UserInfoProto.COMPONENT_ENABLED_STATE_DEFAULT;
import static android.service.pm.PackageProto.UserInfoProto.COMPONENT_ENABLED_STATE_ENABLED;

import static androidx.lifecycle.Lifecycle.State.DESTROYED;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
@@ -36,16 +33,13 @@ import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.test.ActivityInstrumentationTestCase2;

import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

@@ -67,8 +61,7 @@ import java.util.ArrayList;
import java.util.List;

@RunWith(AndroidJUnit4.class)
public class BluetoothOppIncomingFileConfirmActivityTest extends
        ActivityInstrumentationTestCase2<BluetoothOppIncomingFileConfirmActivity> {
public class BluetoothOppIncomingFileConfirmActivityTest {
    @Mock
    Cursor mCursor;
    @Spy
@@ -81,10 +74,6 @@ public class BluetoothOppIncomingFileConfirmActivityTest extends

    boolean mDestroyed;

    public BluetoothOppIncomingFileConfirmActivityTest() {
        super(BluetoothOppIncomingFileConfirmActivity.class);
    }

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
@@ -98,8 +87,6 @@ public class BluetoothOppIncomingFileConfirmActivityTest extends
        mIntent = new Intent();
        mIntent.setClass(mTargetContext, BluetoothOppIncomingFileConfirmActivity.class);
        mIntent.setData(dataUrl);
        setActivityIntent(mIntent);
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());

        doReturn(mCursor).when(mBluetoothMethodProxy).contentResolverQuery(any(), eq(dataUrl),
                eq(null), eq(null),
@@ -134,13 +121,13 @@ public class BluetoothOppIncomingFileConfirmActivityTest extends
                        BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED)
        ));

        enableActivity(true);
        BluetoothOppTestUtils.enableOppActivities(true, mTargetContext);
    }

    @After
    public void tearDown() {
        BluetoothMethodProxy.setInstanceForTesting(null);
        enableActivity(false);
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }

    @Test
@@ -148,10 +135,13 @@ public class BluetoothOppIncomingFileConfirmActivityTest extends
            throws InterruptedException {
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        BluetoothOppIncomingFileConfirmActivity activity = getActivity();
        ActivityScenario<BluetoothOppIncomingFileConfirmActivity> activityScenario
                = ActivityScenario.launch(mIntent);
        activityScenario.onActivity(activity -> {});

        // To work around (possibly) Epresso's bug. The dialog button is clicked (no error throw)
        // but onClick() is not triggered. It works normally if sleep for a few seconds
        // To work around (possibly) ActivityScenario's bug.
        // The dialog button is clicked (no error throw) but onClick() is not triggered.
        // It works normally if sleep for a few seconds
        Thread.sleep(3_000);
        onView(withText(mTargetContext.getText(R.string.incoming_file_confirm_cancel).toString()))
                .inRoot(isDialog()).check(matches(isDisplayed())).perform(click());
@@ -168,10 +158,11 @@ public class BluetoothOppIncomingFileConfirmActivityTest extends
            throws InterruptedException {
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        BluetoothOppIncomingFileConfirmActivity activity = getActivity();
        ActivityScenario.launch(mIntent);

        // To work around (possibly) Espresso's bug. The dialog button is clicked (no error throw)
        // but onClick() is not triggered. It works normally if sleep for a few seconds
        // To work around (possibly) ActivityScenario's bug.
        // The dialog button is clicked (no error throw) but onClick() is not triggered.
        // It works normally if sleep for a few seconds
        Thread.sleep(3_000);
        onView(withText(mTargetContext.getText(R.string.incoming_file_confirm_ok).toString()))
                .inRoot(isDialog()).check(matches(isDisplayed())).perform(click());
@@ -186,60 +177,17 @@ public class BluetoothOppIncomingFileConfirmActivityTest extends
    @Test
    public void onTimeout_sendIntentWithUSER_CONFIRMATION_TIMEOUT_ACTION_finish() throws Exception {
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);
        ActivityScenario<BluetoothOppIncomingFileConfirmActivity> scenario =
                ActivityScenario.launch(mIntent);

        mDestroyed = false;
        BluetoothOppIncomingFileConfirmActivity activity = getActivity();

        activity.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                mDestroyed = true;
            }
        });
        assertThat(mDestroyed).isFalse();
        assertThat(scenario.getState()).isNotEqualTo(Lifecycle.State.DESTROYED);
        Intent in = new Intent(BluetoothShare.USER_CONFIRMATION_TIMEOUT_ACTION);
        mTargetContext.sendBroadcast(in);

        // To work around (possibly) Espresso's bug. The dialog button is clicked (no error throw)
        // but onClick() is not triggered. It works normally if sleep for a few seconds
        // To work around (possibly) ActivityScenario's bug.
        // The dialog button is clicked (no error throw) but onClick() is not triggered.
        // It works normally if sleep for a few seconds
        Thread.sleep(3_000);
        assertThat(mDestroyed).isTrue();
    }

    private void enableActivity(boolean enable) {
        int enabledState = enable ? COMPONENT_ENABLED_STATE_ENABLED
                : COMPONENT_ENABLED_STATE_DEFAULT;

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

        ComponentName activityName = new ComponentName(mTargetContext,
                BluetoothOppIncomingFileConfirmActivity.class);
        mTargetContext.getPackageManager().setComponentEnabledSetting(
                activityName, enabledState, DONT_KILL_APP);
        assertThat(scenario.getState()).isEqualTo(Lifecycle.State.DESTROYED);
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ public class BluetoothOppLauncherActivityTest {
        mIntent = new Intent();
        mIntent.setClass(mTargetContext, BluetoothOppLauncherActivity.class);

        enableActivity(true);
        BluetoothOppTestUtils.enableOppActivities(true, mTargetContext);
        Intents.init();
    }

@@ -91,7 +91,7 @@ public class BluetoothOppLauncherActivityTest {
    public void tearDown() {
        BluetoothMethodProxy.setInstanceForTesting(null);
        Intents.release();
        enableActivity(false);
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }

    @Test
+46 −32
Original line number Diff line number Diff line
@@ -35,8 +35,8 @@ import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2;

import androidx.test.core.app.ActivityScenario;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

@@ -53,10 +53,10 @@ import org.mockito.Spy;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@RunWith(AndroidJUnit4.class)
public class BluetoothOppTransferActivityTest extends
        ActivityInstrumentationTestCase2<BluetoothOppTransferActivity> {
public class BluetoothOppTransferActivityTest {
    @Mock
    Cursor mCursor;
    @Spy
@@ -67,10 +67,6 @@ public class BluetoothOppTransferActivityTest extends
    Intent mIntent;
    Context mTargetContext;

    public BluetoothOppTransferActivityTest() {
        super(BluetoothOppTransferActivity.class);
    }

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
@@ -78,7 +74,6 @@ public class BluetoothOppTransferActivityTest extends
        BluetoothMethodProxy.setInstanceForTesting(mBluetoothMethodProxy);

        Uri dataUrl = Uri.parse("content://com.android.bluetooth.opp.test/random");
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        mIntent = new Intent();
@@ -128,11 +123,15 @@ public class BluetoothOppTransferActivityTest extends
        mCursorMockDataList.add(new CursorMockData(BluetoothShare.CURRENT_BYTES, 4, 0));
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        setActivityIntent(mIntent);
        BluetoothOppTransferActivity activity = getActivity();
        AtomicBoolean check = new AtomicBoolean(false);
        ActivityScenario<BluetoothOppTransferActivity> activityScenario = ActivityScenario.launch(
                mIntent);

        activityScenario.onActivity(activity -> {
            check.set(activity.mWhichDialog == DIALOG_SEND_ONGOING);
        });

        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        assertThat(activity.mWhichDialog).isEqualTo(DIALOG_SEND_ONGOING);
        assertThat(check.get()).isTrue();
    }

    @Test
@@ -148,12 +147,15 @@ public class BluetoothOppTransferActivityTest extends
                new CursorMockData(BluetoothShare.CURRENT_BYTES, 4, 100));
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        setActivityIntent(mIntent);
        BluetoothOppTransferActivity activity = getActivity();
        AtomicBoolean check = new AtomicBoolean(false);
        ActivityScenario<BluetoothOppTransferActivity> activityScenario = ActivityScenario.launch(
                mIntent);

        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        activityScenario.onActivity(activity -> {
            check.set(activity.mWhichDialog == DIALOG_SEND_COMPLETE_SUCCESS);
        });

        assertThat(activity.mWhichDialog).isEqualTo(DIALOG_SEND_COMPLETE_SUCCESS);
        assertThat(check.get()).isTrue();
    }

    @Test
@@ -167,12 +169,15 @@ public class BluetoothOppTransferActivityTest extends
        mCursorMockDataList.add(new CursorMockData(BluetoothShare.CURRENT_BYTES, 4, 42));
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        setActivityIntent(mIntent);
        BluetoothOppTransferActivity activity = getActivity();
        AtomicBoolean check = new AtomicBoolean(false);
        ActivityScenario<BluetoothOppTransferActivity> activityScenario = ActivityScenario.launch(
                mIntent);

        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        activityScenario.onActivity(activity -> {
            check.set(activity.mWhichDialog == DIALOG_SEND_COMPLETE_FAIL);
        });

        assertThat(activity.mWhichDialog).isEqualTo(DIALOG_SEND_COMPLETE_FAIL);
        assertThat(check.get()).isTrue();
    }

    @Test
@@ -186,12 +191,15 @@ public class BluetoothOppTransferActivityTest extends
        mCursorMockDataList.add(new CursorMockData(BluetoothShare.CURRENT_BYTES, 4, 0));
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        setActivityIntent(mIntent);
        BluetoothOppTransferActivity activity = getActivity();
        AtomicBoolean check = new AtomicBoolean(false);
        ActivityScenario<BluetoothOppTransferActivity> activityScenario = ActivityScenario.launch(
                mIntent);

        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        activityScenario.onActivity(activity -> {
            check.set(activity.mWhichDialog == DIALOG_RECEIVE_ONGOING);
        });

        assertThat(activity.mWhichDialog).isEqualTo(DIALOG_RECEIVE_ONGOING);
        assertThat(check.get()).isTrue();
    }

    @Test
@@ -208,12 +216,15 @@ public class BluetoothOppTransferActivityTest extends

        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        setActivityIntent(mIntent);
        BluetoothOppTransferActivity activity = getActivity();
        AtomicBoolean check = new AtomicBoolean(false);
        ActivityScenario<BluetoothOppTransferActivity> activityScenario = ActivityScenario.launch(
                mIntent);

        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        activityScenario.onActivity(activity -> {
            check.set(activity.mWhichDialog == DIALOG_RECEIVE_COMPLETE_SUCCESS);
        });

        assertThat(activity.mWhichDialog).isEqualTo(DIALOG_RECEIVE_COMPLETE_SUCCESS);
        assertThat(check.get()).isTrue();
    }

    @Test
@@ -228,11 +239,14 @@ public class BluetoothOppTransferActivityTest extends

        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);

        setActivityIntent(mIntent);
        BluetoothOppTransferActivity activity = getActivity();
        AtomicBoolean check = new AtomicBoolean(false);
        ActivityScenario<BluetoothOppTransferActivity> activityScenario = ActivityScenario.launch(
                mIntent);

        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        activityScenario.onActivity(activity -> {
            check.set(activity.mWhichDialog == DIALOG_RECEIVE_COMPLETE_FAIL);
        });

        assertThat(activity.mWhichDialog).isEqualTo(DIALOG_RECEIVE_COMPLETE_FAIL);
        assertThat(check.get()).isTrue();
    }
}