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

Commit 4a41b7bc authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add tests for classes in package pbapclient"

parents c6cd9e20 3d4fd658
Loading
Loading
Loading
Loading
+52 −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.pbapclient;

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

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.obex.HeaderSet;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class BluetoothPbapRequestPullPhoneBookSizeTest {

    BluetoothPbapRequestPullPhoneBookSize mRequest;

    @Before
    public void setUp() {
        mRequest = new BluetoothPbapRequestPullPhoneBookSize(/*pbName=*/"phonebook", /*filter=*/1);
    }

    @Test
    public void readResponseHeaders() {
        try {
            HeaderSet headerSet = new HeaderSet();
            mRequest.readResponseHeaders(headerSet);
            assertThat(mRequest.getSize()).isEqualTo(0);
        } catch (Exception e) {
            assertWithMessage("Exception should not happen.").fail();
        }
    }
}
+101 −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.pbapclient;

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

import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;

import android.accounts.Account;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.obex.HeaderSet;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.io.InputStream;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class BluetoothPbapRequestPullPhoneBookTest {

    private static final String PB_NAME = "phonebook";
    private static final Account ACCOUNT = mock(Account.class);

    @Test
    public void constructor_wrongMaxListCount_throwsIAE() {
        final long filter = 0;
        final byte format = PbapClientConnectionHandler.VCARD_TYPE_30;
        final int listStartOffset = 10;

        final int wrongMaxListCount = -1;

        assertThrows(IllegalArgumentException.class, () ->
                new BluetoothPbapRequestPullPhoneBook(PB_NAME, ACCOUNT, filter, format,
                        wrongMaxListCount, listStartOffset));
    }

    @Test
    public void constructor_wrongListStartOffset_throwsIAE() {
        final long filter = 0;
        final byte format = PbapClientConnectionHandler.VCARD_TYPE_30;
        final int maxListCount = 100;

        final int wrongListStartOffset = -1;

        assertThrows(IllegalArgumentException.class, () ->
                new BluetoothPbapRequestPullPhoneBook(PB_NAME, ACCOUNT, filter, format,
                        maxListCount, wrongListStartOffset));
    }

    @Test
    public void readResponse_failWithMockInputStream() {
        final long filter = 1;
        final byte format = 0; // Will be properly handled as VCARD_TYPE_21.
        final int maxListCount = 0; // Will be specially handled as 65535.
        final int listStartOffset = 10;
        BluetoothPbapRequestPullPhoneBook request = new BluetoothPbapRequestPullPhoneBook(
                PB_NAME, ACCOUNT, filter, format, maxListCount, listStartOffset);

        InputStream is = mock(InputStream.class);
        assertThrows(IOException.class, () -> request.readResponse(is));
    }

    @Test
    public void readResponseHeaders() {
        final long filter = 1;
        final byte format = 0; // Will be properly handled as VCARD_TYPE_21.
        final int maxListCount = 0; // Will be specially handled as 65535.
        final int listStartOffset = 10;
        BluetoothPbapRequestPullPhoneBook request = new BluetoothPbapRequestPullPhoneBook(
                PB_NAME, ACCOUNT, filter, format, maxListCount, listStartOffset);

        try {
            HeaderSet headerSet = new HeaderSet();
            request.readResponseHeaders(headerSet);
            assertThat(request.getNewMissedCalls()).isEqualTo(-1);
        } catch (Exception e) {
            assertWithMessage("Exception should not happen.").fail();
        }
    }
}
+48 −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.pbapclient;

import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;

import android.accounts.Account;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.io.InputStream;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class BluetoothPbapVcardListTest {

    private static final Account ACCOUNT = mock(Account.class);

    @Test
    public void constructor_withMockInputStream_throwsIOException() {
        InputStream is = mock(InputStream.class);

        assertThrows(IOException.class, () ->
                new BluetoothPbapVcardList(ACCOUNT, is, PbapClientConnectionHandler.VCARD_TYPE_30));
        assertThrows(IOException.class, () ->
                new BluetoothPbapVcardList(ACCOUNT, is, PbapClientConnectionHandler.VCARD_TYPE_21));
    }
}