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

Commit 9c3557b2 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun Committed by Gerrit Code Review
Browse files

Merge "Close file descriptor" into main

parents 1ff8fc02 25016640
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -302,10 +302,9 @@ public class BluetoothOppUtility {
        // Open a specific media item using ParcelFileDescriptor.
        ContentResolver resolver = context.getContentResolver();
        String readOnlyMode = "r";
        ParcelFileDescriptor pfd = null;
        try {
            pfd = BluetoothMethodProxy.getInstance()
                    .contentResolverOpenFileDescriptor(resolver, uri, readOnlyMode);
        try (ParcelFileDescriptor unusedPfd =
                BluetoothMethodProxy.getInstance()
                        .contentResolverOpenFileDescriptor(resolver, uri, readOnlyMode)) {
            return true;
        } catch (IOException e) {
            ContentProfileErrorReportUtils.report(
+11 −10
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

import androidx.test.platform.app.InstrumentationRegistry;

@@ -57,7 +58,6 @@ import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -177,10 +177,12 @@ public class BluetoothOppUtilityTest {
    }

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

        ParcelFileDescriptor pfd = mock(ParcelFileDescriptor.class);

        Context spiedContext = spy(new ContextWrapper(mContext));
        // Control BluetoothOppUtility#fileExists flow
        doReturn(mCursor).when(mCallProxy).contentResolverQuery(any(),
@@ -191,8 +193,7 @@ public class BluetoothOppUtilityTest {
        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());
        doReturn(pfd).when(mCallProxy).contentResolverOpenFileDescriptor(any(), eq(fileUri), any());

        // Control BluetoothOppUtility#isRecognizedFileType flow
        PackageManager mockManager = mock(PackageManager.class);
@@ -208,14 +209,15 @@ public class BluetoothOppUtilityTest {
                        argument.getData(), Uri.parse("content:///tmp/randomFileName.txt")
                ) && Objects.equals(argument.getAction(), Intent.ACTION_VIEW)
        ));

        verify(pfd).close();
    }

    @Test
    public void openReceivedFile_fileExist_HandlingApplicationNotExist()
            throws FileNotFoundException {

    public void openReceivedFile_fileExist_HandlingApplicationNotExist() throws Exception {
        Uri contentResolverUri = Uri.parse("content://com.android.bluetooth.opp/btopp/0123");
        Uri fileUri = Uri.parse("content:///tmp/randomFileName.txt");
        ParcelFileDescriptor pfd = mock(ParcelFileDescriptor.class);

        Context spiedContext = spy(new ContextWrapper(mContext));
        // Control BluetoothOppUtility#fileExists flow
@@ -228,8 +230,7 @@ public class BluetoothOppUtilityTest {


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

        // Control BluetoothOppUtility#isRecognizedFileType flow
        PackageManager mockManager = mock(PackageManager.class);
@@ -246,9 +247,9 @@ public class BluetoothOppUtilityTest {
                argThat(argument -> argument.getComponent().getClassName().equals(
                        BluetoothOppBtErrorActivity.class.getName())
                ));
        verify(pfd).close();
    }


    @Test
    public void fillRecord_filledAllProperties() {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();