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

Commit 2551184c authored by My Name's avatar My Name Committed by Sungsoo Lim
Browse files

Add tests to cover OPP activities tests

Test: atest BluetoothInstrumentationTests
Bug: 237467631
Tag: #refactor
Change-Id: I33ac0f4b2d7f953ae397d25158999c5ee6debf9c
Merged-In: I33ac0f4b2d7f953ae397d25158999c5ee6debf9c
(cherry picked from commit 134951c0)
parent 94b37914
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -139,7 +139,8 @@ public class BluetoothOppBtEnablingActivity extends AlertActivity {
        }
    };

    private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
    @VisibleForTesting
    final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
+2 −1
Original line number Diff line number Diff line
@@ -413,7 +413,8 @@ public class BluetoothOppLauncherActivity extends Activity {
        return text;
    }

    private void sendFileInfo(String mimeType, String uriString, boolean isHandover,
    @VisibleForTesting
    void sendFileInfo(String mimeType, String uriString, boolean isHandover,
            boolean fromExternal) {
        BluetoothOppManager manager = BluetoothOppManager.getInstance(getApplicationContext());
        try {
+14 −7
Original line number Diff line number Diff line
@@ -22,20 +22,15 @@ import static android.content.pm.PackageManager.DONT_KILL_APP;

import static androidx.lifecycle.Lifecycle.State.DESTROYED;

import static com.android.bluetooth.opp.BluetoothOppBtEnablingActivity.sBtEnablingTimeoutMs;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.view.KeyEvent;

import androidx.lifecycle.Lifecycle;
@@ -44,7 +39,6 @@ import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.TestUtils;

import org.junit.After;
import org.junit.Before;
@@ -129,6 +123,19 @@ public class BluetoothOppBtEnablingActivityTest {
        assertActivityState(activityScenario, DESTROYED);
    }

    @Test
    public void broadcastReceiver_onReceive_finishImmediately() throws Exception {
        doReturn(false).when(mBluetoothMethodProxy).bluetoothAdapterIsEnabled(any());
        ActivityScenario<BluetoothOppBtEnablingActivity> activityScenario = ActivityScenario.launch(
                mIntent);
        activityScenario.onActivity(activity -> {
            Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
            intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
            activity.mBluetoothReceiver.onReceive(mTargetContext, intent);
        });
        assertActivityState(activityScenario, DESTROYED);
    }

    private void assertActivityState(ActivityScenario activityScenario, Lifecycle.State state)
      throws Exception {
        // TODO: Change this into an event driven systems
+23 −2
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

@@ -59,6 +60,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.File;
@@ -71,6 +73,8 @@ public class BluetoothOppLauncherActivityTest {
    Intent mIntent;

    BluetoothMethodProxy mMethodProxy;
    @Mock
    BluetoothOppManager mBluetoothOppManager;

    @Before
    public void setUp() {
@@ -84,12 +88,14 @@ public class BluetoothOppLauncherActivityTest {
        mIntent.setClass(mTargetContext, BluetoothOppLauncherActivity.class);

        BluetoothOppTestUtils.enableOppActivities(true, mTargetContext);
        BluetoothOppManager.setInstance(mBluetoothOppManager);
        Intents.init();
    }

    @After
    public void tearDown() {
        BluetoothMethodProxy.setInstanceForTesting(null);
        BluetoothOppManager.setInstance(null);
        Intents.release();
        BluetoothOppTestUtils.enableOppActivities(false, mTargetContext);
    }
@@ -168,8 +174,8 @@ public class BluetoothOppLauncherActivityTest {

        final Uri[] fileUri = new Uri[1];
        final String shareContent =
                "a string to trigger pattern match with url: www.google.com, phone number: "
                        + "+821023456798, and email: abc@test.com";
                "\na < b & c > a string to trigger pattern match with url: \r"
                        + "www.google.com, phone number: +821023456798, and email: abc@test.com";
        scenario.onActivity(activity -> {
            fileUri[0] = activity.createFileForSharedContent(activity, shareContent);

@@ -181,6 +187,21 @@ public class BluetoothOppLauncherActivityTest {
        assertThat(file.length()).isGreaterThan(shareContent.length());
    }

    @Test
    public void sendFileInfo_finishImmediately() throws Exception {
        doReturn(true).when(mMethodProxy).bluetoothAdapterIsEnabled(any());
        // Unsupported action, the activity will stay without being finished right the way
        mIntent.setAction("unsupported-action");
        ActivityScenario<BluetoothOppLauncherActivity> scenario = ActivityScenario.launch(mIntent);
        doThrow(new IllegalArgumentException()).when(mBluetoothOppManager).saveSendingFileInfo(
                any(), any(String.class), any(), any());
        scenario.onActivity(activity -> {
            activity.sendFileInfo("text/plain", "content:///abc.txt", false, false);
        });

        assertActivityState(scenario, Lifecycle.State.DESTROYED);
    }

    private void assertActivityState(ActivityScenario activityScenario, Lifecycle.State state)
            throws Exception {
        Thread.sleep(2_000);