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

Commit 77e33b49 authored by Hieu Dang's avatar Hieu Dang Committed by Gerrit Code Review
Browse files

Merge "Add more tests for Opp package"

parents 44d52633 065c142f
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -106,7 +106,8 @@ public class BluetoothOppManager {


    private static final String ARRAYLIST_ITEM_SEPERATOR = ";";
    private static final String ARRAYLIST_ITEM_SEPERATOR = ";";


    private static final int ALLOWED_INSERT_SHARE_THREAD_NUMBER = 3;
    @VisibleForTesting
    static final int ALLOWED_INSERT_SHARE_THREAD_NUMBER = 3;


    // used to judge if need continue sending process after received a
    // used to judge if need continue sending process after received a
    // ENABLED_ACTION
    // ENABLED_ACTION
+55 −2
Original line number Original line Diff line number Diff line
@@ -16,13 +16,19 @@


package com.android.bluetooth.opp;
package com.android.bluetooth.opp;


import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.Intents.intending;
import static androidx.test.espresso.intent.matcher.IntentMatchers.anyIntent;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;

import static com.android.bluetooth.opp.BluetoothOppManager.ALLOWED_INSERT_SHARE_THREAD_NUMBER;
import static com.android.bluetooth.opp.BluetoothOppManager.OPP_PREFERENCE_FILE;
import static com.android.bluetooth.opp.BluetoothOppManager.OPP_PREFERENCE_FILE;


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


import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.nullable;
import static org.mockito.Mockito.nullable;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.timeout;
@@ -34,7 +40,9 @@ import android.content.ContentValues;
import android.content.Context;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.ContextWrapper;
import android.net.Uri;
import android.net.Uri;
import android.util.Log;


import androidx.test.espresso.intent.Intents;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.runner.AndroidJUnit4;


@@ -48,6 +56,7 @@ import org.mockito.MockitoAnnotations;


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


@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
public class BluetoothOppManagerTest {
public class BluetoothOppManagerTest {
@@ -66,6 +75,8 @@ public class BluetoothOppManagerTest {


        doReturn(null).when(mCallProxy).contentResolverInsert(
        doReturn(null).when(mCallProxy).contentResolverInsert(
                any(), eq(BluetoothShare.CONTENT_URI), any());
                any(), eq(BluetoothShare.CONTENT_URI), any());

        Intents.init();
    }
    }


    @After
    @After
@@ -74,6 +85,8 @@ public class BluetoothOppManagerTest {
        BluetoothOppUtility.sSendFileMap.clear();
        BluetoothOppUtility.sSendFileMap.clear();
        mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0).edit().clear().apply();
        mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0).edit().clear().apply();
        BluetoothOppManager.sInstance = null;
        BluetoothOppManager.sInstance = null;

        Intents.release();
    }
    }


    @Test
    @Test
@@ -177,11 +190,51 @@ public class BluetoothOppManagerTest {
        BluetoothDevice device = (mContext.getSystemService(BluetoothManager.class))
        BluetoothDevice device = (mContext.getSystemService(BluetoothManager.class))
                .getAdapter().getRemoteDevice(address);
                .getAdapter().getRemoteDevice(address);
        bluetoothOppManager.startTransfer(device);
        bluetoothOppManager.startTransfer(device);
        // add 2 files
        verify(mCallProxy, timeout(5_000).times(1)).contentResolverInsert(any(),
        verify(mCallProxy, timeout(5_000).times(1)).contentResolverInsert(any(),
                nullable(Uri.class), nullable(ContentValues.class));
                nullable(Uri.class), nullable(ContentValues.class));
    }
    }


    @Test
    public void startTransferMoreThanAllowedInsertShareThreadNumberTimes_blockExceedingTransfer()
            throws InterruptedException {
        BluetoothOppManager bluetoothOppManager = BluetoothOppManager.getInstance(mContext);
        String address = "AA:BB:CC:DD:EE:FF";
        bluetoothOppManager.saveSendingFileInfo("text/plain", "content:///abc/xyz.txt",
                false, true);
        BluetoothDevice device = (mContext.getSystemService(BluetoothManager.class))
                .getAdapter().getRemoteDevice(address);

        AtomicBoolean intended = new AtomicBoolean(false);
        intending(anyIntent()).respondWithFunction(
                intent -> {
                    // verify that at least one exceeding thread is blocked
                    intended.set(true);
                    return null;
                });

        // try flushing the transferring queue,
        for (int i = 0; i < ALLOWED_INSERT_SHARE_THREAD_NUMBER + 15; i++) {
            bluetoothOppManager.startTransfer(device);
        }

        // success at least ALLOWED_INSERT_SHARE_THREAD_NUMBER times
        verify(mCallProxy,
                timeout(5_000).atLeast(ALLOWED_INSERT_SHARE_THREAD_NUMBER)).contentResolverInsert(
                any(), nullable(Uri.class), nullable(ContentValues.class));

        // there is at least a failed attempt
        assertThat(intended.get()).isTrue();
    }

    @Test
    public void isEnabled() {
        BluetoothOppManager bluetoothOppManager = BluetoothOppManager.getInstance(mContext);
        doReturn(true).when(mCallProxy).bluetoothAdapterIsEnabled(any());
        assertThat(bluetoothOppManager.isEnabled()).isTrue();
        doReturn(false).when(mCallProxy).bluetoothAdapterIsEnabled(any());
        assertThat(bluetoothOppManager.isEnabled()).isFalse();
    }

    @Test
    @Test
    public void cleanUpSendingFileInfo_fileInfoCleaned() {
    public void cleanUpSendingFileInfo_fileInfoCleaned() {
        BluetoothOppUtility.sSendFileMap.clear();
        BluetoothOppUtility.sSendFileMap.clear();
+27 −1
Original line number Original line Diff line number Diff line
@@ -36,6 +36,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.database.Cursor;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri;
import android.view.KeyEvent;


import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ActivityScenario;
@@ -58,6 +59,7 @@ import org.mockito.Spy;


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


// Long class name cause problem with Junit4. It will raise java.lang.NoClassDefFoundError
// Long class name cause problem with Junit4. It will raise java.lang.NoClassDefFoundError
@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
@@ -137,7 +139,8 @@ public class IncomingFileConfirmActivityTest {


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


        // To work around (possibly) ActivityScenario's bug.
        // To work around (possibly) ActivityScenario's bug.
        // The dialog button is clicked (no error throw) but onClick() is not triggered.
        // The dialog button is clicked (no error throw) but onClick() is not triggered.
@@ -190,4 +193,27 @@ public class IncomingFileConfirmActivityTest {
        Thread.sleep(3_000);
        Thread.sleep(3_000);
        assertThat(scenario.getState()).isEqualTo(Lifecycle.State.DESTROYED);
        assertThat(scenario.getState()).isEqualTo(Lifecycle.State.DESTROYED);
    }
    }

    @Test
    public void onKeyDown() throws Exception {
        BluetoothOppTestUtils.setUpMockCursor(mCursor, mCursorMockDataList);
        ActivityScenario<BluetoothOppIncomingFileConfirmActivity> scenario =
                ActivityScenario.launch(mIntent);
        AtomicBoolean atomicBoolean = new AtomicBoolean();
        scenario.onActivity(activity -> {
            atomicBoolean.set(activity.onKeyDown(KeyEvent.KEYCODE_A,
                    new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A)));
        });

        assertThat(atomicBoolean.get()).isFalse();
        assertThat(scenario.getState()).isNotEqualTo(Lifecycle.State.DESTROYED);

        scenario.onActivity(activity -> {
            atomicBoolean.set(activity.onKeyDown(KeyEvent.KEYCODE_BACK,
                    new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)));
        });

        assertThat(atomicBoolean.get()).isTrue();
        assertThat(scenario.getState()).isEqualTo(Lifecycle.State.DESTROYED);
    }
}
}