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 android.bluetooth.BluetoothAdapter; 23 import android.content.Context; 24 import android.content.Intent; 25 26 import androidx.core.graphics.drawable.IconCompat; 27 import androidx.slice.Slice; 28 import androidx.slice.SliceMetadata; 29 import androidx.slice.SliceProvider; 30 import androidx.slice.core.SliceAction; 31 import androidx.slice.widget.SliceLiveData; 32 33 import com.android.settings.R; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 42 import java.util.List; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class BluetoothSliceBuilderTest { 46 47 private Context mContext; 48 49 @Before setUp()50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 mContext = RuntimeEnvironment.application; 53 54 // Set-up specs for SliceMetadata. 55 SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS); 56 } 57 58 @Test getBluetoothSlice_correctSliceContent()59 public void getBluetoothSlice_correctSliceContent() { 60 final Slice BluetoothSlice = BluetoothSliceBuilder.getSlice(mContext); 61 62 final SliceMetadata metadata = SliceMetadata.from(mContext, BluetoothSlice); 63 assertThat(metadata.getTitle()).isEqualTo( 64 mContext.getString(R.string.bluetooth_settings_title)); 65 66 final List<SliceAction> toggles = metadata.getToggles(); 67 assertThat(toggles).hasSize(1); 68 69 final SliceAction primaryAction = metadata.getPrimaryAction(); 70 final IconCompat expectedToggleIcon = IconCompat.createWithResource(mContext, 71 com.android.internal.R.drawable.ic_settings_bluetooth); 72 assertThat(primaryAction.getIcon().toString()).isEqualTo(expectedToggleIcon.toString()); 73 } 74 75 @Test handleUriChange_updatesBluetooth()76 public void handleUriChange_updatesBluetooth() { 77 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 78 final Intent intent = new Intent(); 79 intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true); 80 adapter.disable()/* enabled */; 81 82 BluetoothSliceBuilder.handleUriChange(mContext, intent); 83 84 assertThat(adapter.isEnabled()).isTrue(); 85 } 86 } 87