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

Commit 791d4984 authored by Hieu Dang's avatar Hieu Dang Committed by Gerrit Code Review
Browse files

Merge "Add BluetoothOppHandoverReceiverTest"

parents 08715b05 1f1eac45
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.android.bluetooth.BluetoothMethodProxy;

import java.util.ArrayList;

public class BluetoothOppHandoverReceiver extends BroadcastReceiver {
@@ -93,7 +95,8 @@ public class BluetoothOppHandoverReceiver extends BroadcastReceiver {
                if (D) {
                    Log.d(TAG, "Stopping handover transfer with Uri " + contentUri);
                }
                context.getContentResolver().delete(contentUri, null, null);
                BluetoothMethodProxy.getInstance().contentResolverDelete(
                        context.getContentResolver(), contentUri, null, null);
            }
        } else {
            if (D) {
+10 −5
Original line number Diff line number Diff line
@@ -139,6 +139,14 @@ public class BluetoothOppManager {
        }
    }

    /**
     * Set Singleton instance. Intended for testing purpose
     */
    @VisibleForTesting
    static void setInstance(BluetoothOppManager instance) {
        sInstance = instance;
    }

    /**
     * init
     */
@@ -485,9 +493,7 @@ public class BluetoothOppManager {
                            BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED);
                }
                final Uri contentUri = BluetoothMethodProxy.getInstance().contentResolverInsert(
                        mContext.getContentResolver(), BluetoothShare.CONTENT_URI, values
                );

                        mContext.getContentResolver(), BluetoothShare.CONTENT_URI, values);
                if (V) {
                    Log.v(TAG, "Insert contentUri: " + contentUri + "  to device: " + getDeviceName(
                            mRemoteDevice));
@@ -508,8 +514,7 @@ public class BluetoothOppManager {
                        BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED);
            }
            final Uri contentUri = BluetoothMethodProxy.getInstance().contentResolverInsert(
                    mContext.getContentResolver(), BluetoothShare.CONTENT_URI, values
            );
                    mContext.getContentResolver(), BluetoothShare.CONTENT_URI, values);
            if (V) {
                Log.v(TAG, "Insert contentUri: " + contentUri + "  to device: " + getDeviceName(
                        mRemoteDevice));
+136 −0
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 org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.nullable;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.test.filters.MediumTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.BluetoothMethodProxy;

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

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

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

    @Spy
    BluetoothMethodProxy mCallProxy = BluetoothMethodProxy.getInstance();

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        BluetoothMethodProxy.setInstanceForTesting(mCallProxy);
        doReturn(0).when(mCallProxy).contentResolverDelete(any(), any(Uri.class), any(), any());
        doReturn(null).when(mCallProxy).contentResolverInsert(
          any(), eq(BluetoothShare.CONTENT_URI), any());
    }

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

    @Test
    public void onReceive_withActionHandoverSend_startTransfer() {
        Intent intent = new Intent(Constants.ACTION_HANDOVER_SEND);
        String address = "AA:BB:CC:DD:EE:FF";
        Uri uri = Uri.parse("content:///abc/xyz.txt");
        BluetoothDevice device = (mContext.getSystemService(BluetoothManager.class))
                .getAdapter().getRemoteDevice(address);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.setType("text/plain");

        BluetoothOppManager spyManager = spy(new BluetoothOppManager());
        BluetoothOppManager.setInstance(spyManager);
        new BluetoothOppHandoverReceiver().onReceive(mContext, intent);

        verify(spyManager, timeout(3_000)).startTransfer(any());

        // this will run BluetoothOppManager#startTransfer, which will then make
        // InsertShareInfoThread insert into content resolver
        verify(mCallProxy, timeout(3_000).times(1)).contentResolverInsert(any(),
                eq(BluetoothShare.CONTENT_URI), nullable(ContentValues.class));
    }

    @Test
    public void onReceive_withActionHandoverSendMultiple_startTransfer() {
        Intent intent = new Intent(Constants.ACTION_HANDOVER_SEND_MULTIPLE);
        String address = "AA:BB:CC:DD:EE:FF";
        ArrayList<Uri> uris = new ArrayList<Uri>(
                List.of(Uri.parse("content:///abc/xyz.txt"),
                        Uri.parse("content:///a/b/c/d/x/y/z.txt"),
                        Uri.parse("content:///123/456.txt")));
        BluetoothDevice device = (mContext.getSystemService(BluetoothManager.class))
                .getAdapter().getRemoteDevice(address);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(Intent.EXTRA_STREAM, uris);
        intent.setType("text/plain");

        BluetoothOppManager spyManager = spy(new BluetoothOppManager());
        BluetoothOppManager.setInstance(spyManager);
        new BluetoothOppHandoverReceiver().onReceive(mContext, intent);

        verify(spyManager, timeout(3_000)).startTransfer(any());

        // this will run BluetoothOppManager#startTransfer, which will then make
        // InsertShareInfoThread insert into content resolver
        verify(mCallProxy, timeout(3_000).times(3)).contentResolverInsert(any(),
                eq(BluetoothShare.CONTENT_URI), nullable(ContentValues.class));
    }

    @Test
    public void onReceive_withActionStopHandover_triggerContentResolverDelete() {
        Intent intent = new Intent(Constants.ACTION_STOP_HANDOVER);
        String address = "AA:BB:CC:DD:EE:FF";
        BluetoothDevice device = (mContext.getSystemService(BluetoothManager.class))
                .getAdapter().getRemoteDevice(address);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_ID, 0);

        new BluetoothOppHandoverReceiver().onReceive(mContext, intent);

        verify(mCallProxy).contentResolverDelete(any(), any(),
                nullable(String.class), nullable(String[].class));
    }
}
 No newline at end of file
+1 −1

File changed.

Contains only whitespace changes.