1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.bluetooth.cts; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.bluetooth.BluetoothClass; 22 import android.os.Parcel; 23 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 import androidx.test.filters.SmallTest; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 /** Unit test cases for {@link BluetoothClass}. */ 32 @RunWith(AndroidJUnit4.class) 33 @SmallTest 34 public class BluetoothClassTest { 35 36 private BluetoothClass mBluetoothClassHeadphones; 37 private BluetoothClass mBluetoothClassPhone; 38 private BluetoothClass mBluetoothClassService; 39 createBtClass(int deviceClass)40 private BluetoothClass createBtClass(int deviceClass) { 41 Parcel p = Parcel.obtain(); 42 p.writeInt(deviceClass); 43 p.setDataPosition(0); // reset position of parcel before passing to constructor 44 45 BluetoothClass bluetoothClass = BluetoothClass.CREATOR.createFromParcel(p); 46 p.recycle(); 47 return bluetoothClass; 48 } 49 50 @Before setUp()51 public void setUp() { 52 mBluetoothClassHeadphones = createBtClass(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES); 53 mBluetoothClassPhone = createBtClass(BluetoothClass.Device.Major.PHONE); 54 mBluetoothClassService = createBtClass(BluetoothClass.Service.NETWORKING); 55 } 56 57 @Test hasService()58 public void hasService() { 59 assertThat(mBluetoothClassService.hasService(BluetoothClass.Service.NETWORKING)).isTrue(); 60 assertThat(mBluetoothClassService.hasService(BluetoothClass.Service.TELEPHONY)).isFalse(); 61 } 62 63 @Test getMajorDeviceClass()64 public void getMajorDeviceClass() { 65 assertThat(mBluetoothClassHeadphones.getMajorDeviceClass()) 66 .isEqualTo(BluetoothClass.Device.Major.AUDIO_VIDEO); 67 assertThat(mBluetoothClassPhone.getMajorDeviceClass()) 68 .isEqualTo(BluetoothClass.Device.Major.PHONE); 69 } 70 71 @Test getDeviceClass()72 public void getDeviceClass() { 73 assertThat(mBluetoothClassHeadphones.getDeviceClass()) 74 .isEqualTo(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES); 75 assertThat(mBluetoothClassPhone.getDeviceClass()) 76 .isEqualTo(BluetoothClass.Device.PHONE_UNCATEGORIZED); 77 } 78 79 @Test getClassOfDevice()80 public void getClassOfDevice() { 81 assertThat(mBluetoothClassHeadphones.getDeviceClass()) 82 .isEqualTo(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES); 83 assertThat(mBluetoothClassPhone.getMajorDeviceClass()) 84 .isEqualTo(BluetoothClass.Device.Major.PHONE); 85 } 86 87 @Test doesClassMatch()88 public void doesClassMatch() { 89 assertThat(mBluetoothClassHeadphones.doesClassMatch(BluetoothClass.PROFILE_A2DP)).isTrue(); 90 assertThat(mBluetoothClassHeadphones.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) 91 .isFalse(); 92 93 assertThat(mBluetoothClassPhone.doesClassMatch(BluetoothClass.PROFILE_OPP)).isTrue(); 94 assertThat(mBluetoothClassPhone.doesClassMatch(BluetoothClass.PROFILE_HEADSET)).isFalse(); 95 96 assertThat(mBluetoothClassService.doesClassMatch(BluetoothClass.PROFILE_PANU)).isTrue(); 97 assertThat(mBluetoothClassService.doesClassMatch(BluetoothClass.PROFILE_OPP)).isFalse(); 98 } 99 100 @Test innerClasses()101 public void innerClasses() { 102 // Just instantiate static inner classes for exposing constants 103 // to make test coverage tool happy. 104 BluetoothClass.Device device = new BluetoothClass.Device(); 105 BluetoothClass.Device.Major major = new BluetoothClass.Device.Major(); 106 BluetoothClass.Service service = new BluetoothClass.Service(); 107 } 108 } 109