1 /* 2 * Copyright (C) 2018 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 18 package com.android.settings.bluetooth; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 26 import android.content.Context; 27 28 import com.android.settings.R; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 import com.android.settings.testutils.SliceTester; 31 import com.android.settings.testutils.shadow.ShadowLocalBluetoothAdapter; 32 import com.android.settings.testutils.shadow.ShadowLocalBluetoothProfileManager; 33 import com.android.settingslib.bluetooth.LocalBluetoothAdapter; 34 import com.android.settingslib.bluetooth.LocalBluetoothManager; 35 36 import android.content.Intent; 37 import android.content.res.Resources; 38 import android.support.v4.graphics.drawable.IconCompat; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.annotation.Config; 46 47 import java.util.List; 48 49 import androidx.slice.Slice; 50 import androidx.slice.SliceItem; 51 import androidx.slice.SliceMetadata; 52 import androidx.slice.SliceProvider; 53 import androidx.slice.core.SliceAction; 54 import androidx.slice.widget.SliceLiveData; 55 56 @RunWith(SettingsRobolectricTestRunner.class) 57 @Config(shadows = {ShadowLocalBluetoothAdapter.class, ShadowLocalBluetoothProfileManager.class}) 58 public class BluetoothSliceBuilderTest { 59 60 private Context mContext; 61 62 @Before setUp()63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mContext = spy(RuntimeEnvironment.application); 66 67 // Prevent crash in SliceMetadata. 68 Resources resources = spy(mContext.getResources()); 69 doReturn(60).when(resources).getDimensionPixelSize(anyInt()); 70 doReturn(resources).when(mContext).getResources(); 71 72 // Set-up specs for SliceMetadata. 73 SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS); 74 } 75 76 @Test getBluetoothSlice_correctSliceContent()77 public void getBluetoothSlice_correctSliceContent() { 78 final Slice BluetoothSlice = BluetoothSliceBuilder.getSlice(mContext); 79 final SliceMetadata metadata = SliceMetadata.from(mContext, BluetoothSlice); 80 81 final List<SliceAction> toggles = metadata.getToggles(); 82 assertThat(toggles).hasSize(1); 83 84 final SliceAction primaryAction = metadata.getPrimaryAction(); 85 final IconCompat expectedToggleIcon = IconCompat.createWithResource(mContext, 86 R.drawable.ic_settings_bluetooth); 87 assertThat(primaryAction.getIcon().toString()).isEqualTo(expectedToggleIcon.toString()); 88 89 final List<SliceItem> sliceItems = BluetoothSlice.getItems(); 90 SliceTester.assertTitle(sliceItems, mContext.getString(R.string.bluetooth_settings_title)); 91 } 92 93 @Test handleUriChange_updatesBluetooth()94 public void handleUriChange_updatesBluetooth() { 95 final LocalBluetoothAdapter adapter = LocalBluetoothManager.getInstance(mContext, 96 null /* callback */).getBluetoothAdapter(); 97 final Intent intent = new Intent(); 98 intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true); 99 adapter.setBluetoothEnabled(false /* enabled */); 100 101 BluetoothSliceBuilder.handleUriChange(mContext, intent); 102 103 assertThat(adapter.isEnabled()).isTrue(); 104 } 105 }