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

Commit 23d89da0 authored by Hieu Dang's avatar Hieu Dang Committed by Gerrit Code Review
Browse files

Merge "Revert "Add BluetoothOppReceiverTest""

parents caff027b 192895e1
Loading
Loading
Loading
Loading
+8 −13
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.R;
import com.android.bluetooth.Utils;

@@ -109,8 +108,7 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            Uri uri = intent.getData();
            ContentValues values = new ContentValues();
            values.put(BluetoothShare.USER_CONFIRMATION, BluetoothShare.USER_CONFIRMATION_DENIED);
            BluetoothMethodProxy.getInstance().contentResolverUpdate(context.getContentResolver(),
                    uri, values, null, null);
            context.getContentResolver().update(uri, values, null, null);
            cancelNotification(context, BluetoothOppNotification.NOTIFICATION_ID_PROGRESS);

        } else if (action.equals(Constants.ACTION_ACCEPT)) {
@@ -122,8 +120,7 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            ContentValues values = new ContentValues();
            values.put(BluetoothShare.USER_CONFIRMATION,
                    BluetoothShare.USER_CONFIRMATION_CONFIRMED);
            BluetoothMethodProxy.getInstance().contentResolverUpdate(context.getContentResolver(),
                    uri, values, null, null);
            context.getContentResolver().update(uri, values, null, null);
        } else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) {
            if (V) {
                if (action.equals(Constants.ACTION_OPEN)) {
@@ -186,8 +183,8 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            if (V) {
                Log.v(TAG, "Receiver hide for " + intent.getData());
            }
            Cursor cursor = BluetoothMethodProxy.getInstance().contentResolverQuery(
                    context.getContentResolver(), intent.getData(), null, null, null, null);
            Cursor cursor =
                    context.getContentResolver().query(intent.getData(), null, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY);
@@ -199,9 +196,7 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
                            && visibility == BluetoothShare.VISIBILITY_VISIBLE) {
                        ContentValues values = new ContentValues();
                        values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
                        BluetoothMethodProxy.getInstance().contentResolverUpdate(
                                context.getContentResolver(), intent.getData(), values, null,
                                null);
                        context.getContentResolver().update(intent.getData(), values, null, null);
                        if (V) {
                            Log.v(TAG, "Action_hide received and db updated");
                        }
@@ -215,8 +210,8 @@ public class BluetoothOppReceiver extends BroadcastReceiver {
            }
            ContentValues updateValues = new ContentValues();
            updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
            BluetoothMethodProxy.getInstance().contentResolverUpdate(
                    context.getContentResolver(), BluetoothShare.CONTENT_URI, updateValues,
            context.getContentResolver()
                    .update(BluetoothShare.CONTENT_URI, updateValues,
                            BluetoothOppNotification.WHERE_COMPLETED, null);
        } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) {
            if (V) {
+0 −311
Original line number Diff line number Diff line
/*
 * Copyright 2022 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.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 androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.app.Activity;
import android.app.Instrumentation;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevicePicker;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;

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

import com.android.bluetooth.BluetoothMethodProxy;

import com.google.common.base.Objects;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;

@RunWith(AndroidJUnit4.class)
public class BluetoothOppReceiverTest {
    Context mContext;

    @Mock
    BluetoothMethodProxy mBluetoothMethodProxy;
    BluetoothOppReceiver mReceiver;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(new ContextWrapper(
                InstrumentationRegistry.getInstrumentation().getTargetContext()));

        // mock instance so query/insert/update/etc. will not be executed
        BluetoothMethodProxy.setInstanceForTesting(mBluetoothMethodProxy);

        mReceiver = new BluetoothOppReceiver();

        Intents.init();

        BluetoothOppTestUtils.enableOppActivities(true, mContext);
    }

    @After
    public void tearDown() {
        BluetoothMethodProxy.setInstanceForTesting(null);

        Intents.release();
    }

    @Test
    public void onReceive_withActionDeviceSelected_transferStarted() {
        BluetoothOppManager bluetoothOppManager = spy(BluetoothOppManager.getInstance(mContext));
        BluetoothOppManager.setInstance(bluetoothOppManager);
        String address = "AA:BB:CC:DD:EE:FF";
        BluetoothDevice device = mContext.getSystemService(BluetoothManager.class)
                .getAdapter().getRemoteDevice(address);
        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 -> {
            mReceiver.onReceive(mContext, intent);
        });
        verify(bluetoothOppManager).startTransfer(eq(device));
    }

    @Test
    public void onReceive_withActionIncomingFileConfirm_startActivity() {
        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()));
    }

    @Test
    public void onReceive_withActionAccept_contentUpdated() {
        Uri uri = Uri.parse("content:///important");
        Intent intent = new Intent();
        intent.setAction(Constants.ACTION_ACCEPT);
        intent.setData(uri);
        mReceiver.onReceive(mContext, intent);
        verify(mBluetoothMethodProxy).contentResolverUpdate(any(), eq(uri), argThat(arg ->
                Objects.equal(BluetoothShare.USER_CONFIRMATION_CONFIRMED,
                        arg.get(BluetoothShare.USER_CONFIRMATION))), any(), any());
    }

    @Test
    public void onReceive_withActionDecline_contentUpdated() {
        Uri uri = Uri.parse("content:///important");
        Intent intent = new Intent();
        intent.setAction(Constants.ACTION_DECLINE);
        intent.setData(uri);
        mReceiver.onReceive(mContext, intent);
        verify(mBluetoothMethodProxy).contentResolverUpdate(any(), eq(uri), argThat(arg ->
                Objects.equal(BluetoothShare.USER_CONFIRMATION_DENIED,
                        arg.get(BluetoothShare.USER_CONFIRMATION))), any(), any());
    }

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

        mReceiver.onReceive(mContext, intent);
        intended(hasComponent(BluetoothOppTransferHistory.class.getName()));
        intended(hasExtra("direction", BluetoothShare.DIRECTION_OUTBOUND));
    }

    @Test
    public void onReceive_withActionInboundTransfer_activityStart() {
        Intent intent = new Intent();
        intent.setAction(Constants.ACTION_OPEN_INBOUND_TRANSFER);
        intent.setData(Uri.parse("content:///not/important"));
        intending(anyIntent()).respondWith(
                new Instrumentation.ActivityResult(Activity.RESULT_OK, new Intent()));
        mReceiver.onReceive(mContext, intent);
        intended(hasComponent(BluetoothOppTransferHistory.class.getName()));
        intended(hasExtra("direction", BluetoothShare.DIRECTION_INBOUND));
    }

    @Test
    public void onReceive_withActionOpenReceivedFile_activityStart() {
        Intent intent = new Intent();
        intent.setAction(Constants.ACTION_OPEN_RECEIVED_FILES);
        intent.setData(Uri.parse("content:///not/important"));
        mReceiver.onReceive(mContext, intent);
        intended(hasComponent(BluetoothOppTransferHistory.class.getName()));
        intended(hasExtra("direction", BluetoothShare.DIRECTION_INBOUND));
        intended(hasExtra(Constants.EXTRA_SHOW_ALL_FILES, true));
    }

    @Test
    public void onReceive_withActionHide_contentUpdate() {
        List<BluetoothOppTestUtils.CursorMockData> cursorMockDataList;
        Cursor cursor = mock(Cursor.class);
        cursorMockDataList = new ArrayList<>(List.of(
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.VISIBILITY, 0,
                        BluetoothShare.VISIBILITY_VISIBLE),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.USER_CONFIRMATION, 1,
                        BluetoothShare.USER_CONFIRMATION_PENDING)
        ));

        BluetoothOppTestUtils.setUpMockCursor(cursor, cursorMockDataList);

        doReturn(cursor).when(mBluetoothMethodProxy).contentResolverQuery(any(), any(), any(),
                any(), any(), any());
        doReturn(true).when(cursor).moveToFirst();

        Intent intent = new Intent();
        intent.setAction(Constants.ACTION_HIDE);
        mReceiver.onReceive(mContext, intent);

        verify(mBluetoothMethodProxy).contentResolverUpdate(any(), any(),
                argThat(arg -> Objects.equal(BluetoothShare.VISIBILITY_HIDDEN,
                        arg.get(BluetoothShare.VISIBILITY))), any(), any());
    }

    @Test
    public void onReceive_withActionCompleteHide_contentUpdate() {
        Intent intent = new Intent();
        intent.setAction(Constants.ACTION_COMPLETE_HIDE);
        mReceiver.onReceive(mContext, intent);
        verify(mBluetoothMethodProxy).contentResolverUpdate(any(), eq(BluetoothShare.CONTENT_URI),
                argThat(arg -> Objects.equal(BluetoothShare.VISIBILITY_HIDDEN,
                        arg.get(BluetoothShare.VISIBILITY))), any(), any());
    }

    @Test
    public void onReceive_withActionTransferComplete_withHandoverInitiated_contextSendBroadcast() {
        List<BluetoothOppTestUtils.CursorMockData> cursorMockDataList;
        Cursor cursor = mock(Cursor.class);
        int idValue = 1234;
        Long timestampValue = 123456789L;
        String destinationValue = "AA:BB:CC:00:11:22";
        String fileTypeValue = "text/plain";

        cursorMockDataList = new ArrayList<>(List.of(
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare._ID, 0, idValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.STATUS, 1,
                        BluetoothShare.STATUS_SUCCESS),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.DIRECTION, 2,
                        BluetoothShare.DIRECTION_OUTBOUND),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.TOTAL_BYTES, 3, 100),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.CURRENT_BYTES, 4, 100),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.MIMETYPE, 5, fileTypeValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.TIMESTAMP, 6,
                        timestampValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.DESTINATION, 7,
                        destinationValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare._DATA, 8, null),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.FILENAME_HINT, 9, null),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.URI, 10,
                        "content://textfile.txt"),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.USER_CONFIRMATION, 11,
                        BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED)
        ));

        BluetoothOppTestUtils.setUpMockCursor(cursor, cursorMockDataList);

        doReturn(cursor).when(mBluetoothMethodProxy).contentResolverQuery(any(), any(), any(),
                any(), any(), any());
        doReturn(true).when(cursor).moveToFirst();

        Intent intent = new Intent();
        intent.setAction(BluetoothShare.TRANSFER_COMPLETED_ACTION);
        mReceiver.onReceive(mContext, intent);
        verify(mContext).sendBroadcast(any(), eq(Constants.HANDOVER_STATUS_PERMISSION), any());
    }

    @Test
    public void onReceive_withActionTransferComplete_withoutHandoverInitiated_noBroadcastSent() {
        List<BluetoothOppTestUtils.CursorMockData> cursorMockDataList;
        Cursor cursor = mock(Cursor.class);
        int idValue = 1234;
        Long timestampValue = 123456789L;
        String destinationValue = "AA:BB:CC:00:11:22";
        String fileTypeValue = "text/plain";

        cursorMockDataList = new ArrayList<>(List.of(
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare._ID, 0, idValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.STATUS, 1,
                        BluetoothShare.STATUS_SUCCESS),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.DIRECTION, 2,
                        BluetoothShare.DIRECTION_OUTBOUND),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.TOTAL_BYTES, 3, 100),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.CURRENT_BYTES, 4, 100),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.MIMETYPE, 5, fileTypeValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.TIMESTAMP, 6,
                        timestampValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.DESTINATION, 7,
                        destinationValue),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare._DATA, 8, null),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.FILENAME_HINT, 9, null),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.URI, 10,
                        "content://textfile.txt"),
                new BluetoothOppTestUtils.CursorMockData(BluetoothShare.USER_CONFIRMATION, 11,
                        BluetoothShare.USER_CONFIRMATION_CONFIRMED)
        ));

        BluetoothOppTestUtils.setUpMockCursor(cursor, cursorMockDataList);

        doReturn(cursor).when(mBluetoothMethodProxy).contentResolverQuery(any(), any(), any(),
                any(), any(), any());
        doReturn(true).when(cursor).moveToFirst();

        Intent intent = new Intent();
        intent.setAction(BluetoothShare.TRANSFER_COMPLETED_ACTION);

        ActivityScenario<BluetoothOppBtEnableActivity> activityScenario
                = ActivityScenario.launch(BluetoothOppBtEnableActivity.class);
        activityScenario.onActivity(activity -> {
            mReceiver.onReceive(mContext, intent);
        });

        // check Toast with Espresso seems not to work on Android 11+. Check not send broadcast
        // context instead

        verify(mContext, never()).sendBroadcast(any(), eq(Constants.HANDOVER_STATUS_PERMISSION),
                any());
    }
}