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

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

Add openReceivedFile tests in BluetoothOppUtilityTest

Test: atest BluetoothOppUtilityTest
Tag: #refactor
Bug: 237467631
Change-Id: I79f098b61e95cc37a86da733483b9f8c87937bda
parent 7132b71b
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -21,11 +21,13 @@ import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.obex.HeaderSet;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
@@ -92,6 +94,15 @@ public class BluetoothMethodProxy {
        return contentResolver.update(contentUri, contentValues, where, selectionArgs);
    }

    /**
     * Proxies {@link ContentResolver#openFileDescriptor(Uri, String)}.
     */
    public ParcelFileDescriptor contentResolverOpenFileDescriptor(ContentResolver contentResolver,
            final Uri uri, final String mode) throws FileNotFoundException {
        return contentResolver.openFileDescriptor(uri, mode);
    }


    /**
     * Proxies {@link HeaderSet#getHeader}.
     */
+4 −2
Original line number Diff line number Diff line
@@ -233,7 +233,8 @@ public class BluetoothOppUtility {
            if (V) {
                Log.d(TAG, "This uri will be deleted: " + uri);
            }
            context.getContentResolver().delete(uri, null, null);
            BluetoothMethodProxy.getInstance().contentResolverDelete(context.getContentResolver(),
                    uri, null, null);
            return;
        }

@@ -272,7 +273,8 @@ public class BluetoothOppUtility {
        String readOnlyMode = "r";
        ParcelFileDescriptor pfd = null;
        try {
            pfd = resolver.openFileDescriptor(uri, readOnlyMode);
            pfd = BluetoothMethodProxy.getInstance()
                    .contentResolverOpenFileDescriptor(resolver, uri, readOnlyMode);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
+113 −10
Original line number Diff line number Diff line
@@ -19,13 +19,21 @@ package com.android.bluetooth.opp;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

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

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;

@@ -41,8 +49,10 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class BluetoothOppUtilityTest {
@@ -123,6 +133,99 @@ public class BluetoothOppUtilityTest {
        }
    }

    @Test
    public void openReceivedFile_fileNotExist() {
        Uri contentResolverUri = Uri.parse("content://com.android.bluetooth.opp/btopp/0123");
        Uri fileUri = Uri.parse("content:///tmp/randomFileName.txt");

        Context spiedContext = spy(new ContextWrapper(mContext));

        doReturn(mCursor).when(mCallProxy).contentResolverQuery(any(),
                eq(contentResolverUri), any(), eq(null),
                eq(null), eq(null));

        doReturn(true).when(mCursor).moveToFirst();
        doReturn(fileUri.toString()).when(mCursor).getString(anyInt());

        BluetoothOppUtility.openReceivedFile(spiedContext, "randomFileName.txt",
                "text/plain", 0L, contentResolverUri);

        verify(spiedContext).startActivity(argThat(argument
                -> Objects.equals(argument.getComponent().getClassName(),
                BluetoothOppBtErrorActivity.class.getName())
        ));
    }

    @Test
    public void openReceivedFile_fileExist_HandlingApplicationExist() throws FileNotFoundException {
        Uri contentResolverUri = Uri.parse("content://com.android.bluetooth.opp/btopp/0123");
        Uri fileUri = Uri.parse("content:///tmp/randomFileName.txt");

        Context spiedContext = spy(new ContextWrapper(mContext));
        // Control BluetoothOppUtility#fileExists flow
        doReturn(mCursor).when(mCallProxy).contentResolverQuery(any(),
                eq(contentResolverUri), any(), eq(null),
                eq(null), eq(null));

        doReturn(true).when(mCursor).moveToFirst();
        doReturn(fileUri.toString()).when(mCursor).getString(anyInt());

        doReturn(0).when(mCallProxy).contentResolverDelete(any(), any(), any(), any());
        doReturn(null).when(mCallProxy).contentResolverOpenFileDescriptor(any(),
                eq(fileUri), any());

        // Control BluetoothOppUtility#isRecognizedFileType flow
        PackageManager mockManager = mock(PackageManager.class);
        doReturn(mockManager).when(spiedContext).getPackageManager();
        doReturn(List.of(new ResolveInfo())).when(mockManager).queryIntentActivities(any(),
                anyInt());

        BluetoothOppUtility.openReceivedFile(spiedContext, "randomFileName.txt",
                "text/plain", 0L, contentResolverUri);

        verify(spiedContext).startActivity(argThat(argument
                        -> Objects.equals(
                        argument.getData(), Uri.parse("content:///tmp/randomFileName.txt")
                ) && Objects.equals(argument.getAction(), Intent.ACTION_VIEW)
        ));
    }

    @Test
    public void openReceivedFile_fileExist_HandlingApplicationNotExist()
            throws FileNotFoundException {

        Uri contentResolverUri = Uri.parse("content://com.android.bluetooth.opp/btopp/0123");
        Uri fileUri = Uri.parse("content:///tmp/randomFileName.txt");

        Context spiedContext = spy(new ContextWrapper(mContext));
        // Control BluetoothOppUtility#fileExists flow
        doReturn(mCursor).when(mCallProxy).contentResolverQuery(any(),
                eq(contentResolverUri), any(), eq(null),
                eq(null), eq(null));

        doReturn(true).when(mCursor).moveToFirst();
        doReturn(fileUri.toString()).when(mCursor).getString(anyInt());


        doReturn(0).when(mCallProxy).contentResolverDelete(any(), any(), any(), any());
        doReturn(null).when(mCallProxy).contentResolverOpenFileDescriptor(any(),
                eq(fileUri), any());

        // Control BluetoothOppUtility#isRecognizedFileType flow
        PackageManager mockManager = mock(PackageManager.class);
        doReturn(mockManager).when(spiedContext).getPackageManager();
        doReturn(List.of()).when(mockManager).queryIntentActivities(any(), anyInt());

        BluetoothOppUtility.openReceivedFile(spiedContext, "randomFileName.txt",
                "text/plain", 0L, contentResolverUri);

        verify(spiedContext).startActivity(
                argThat(argument -> argument.getComponent().getClassName().equals(
                        BluetoothOppBtErrorActivity.class.getName())
                ));
    }


    @Test
    public void fillRecord_filledAllProperties() {
        int idValue = 1234;