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.flashlight;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.content.Context;
23 import android.hardware.camera2.CameraCharacteristics;
24 import android.hardware.camera2.CameraManager;
25 import android.provider.Settings;
26 
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.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.Shadows;
41 import org.robolectric.annotation.Config;
42 import org.robolectric.shadow.api.Shadow;
43 import org.robolectric.shadows.ShadowCameraCharacteristics;
44 import org.robolectric.shadows.ShadowCameraManager;
45 
46 import java.util.List;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class FlashlightSliceTest {
50 
51     private Context mContext;
52     private ShadowCameraManager mShadowCameraManager;
53 
54     @Before
setUp()55     public void setUp() {
56         mContext = RuntimeEnvironment.application;
57         mShadowCameraManager = Shadows.shadowOf(mContext.getSystemService(CameraManager.class));
58 
59         // Set-up specs for SliceMetadata.
60         SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
61     }
62 
63     @Test
getFlashlightSlice_correctData()64     public void getFlashlightSlice_correctData() {
65         Settings.Secure.putInt(
66                 mContext.getContentResolver(), Settings.Secure.FLASHLIGHT_AVAILABLE, 1);
67 
68         final Slice slice = new FlashlightSlice(mContext).getSlice();
69 
70         final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
71         assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.power_flashlight));
72 
73         final List<SliceAction> toggles = metadata.getToggles();
74         assertThat(toggles).hasSize(1);
75     }
76 
77     @Test
isFlashlightAvailable_nullSecureAttr_noFlashUnit_returnFalse()78     public void isFlashlightAvailable_nullSecureAttr_noFlashUnit_returnFalse() {
79         assertThat(FlashlightSlice.isFlashlightAvailable(mContext)).isFalse();
80     }
81 
82     @Test
isFlashlightAvailable_nullSecureAttr_hasFlashUnit_returnTrue()83     public void isFlashlightAvailable_nullSecureAttr_hasFlashUnit_returnTrue() {
84         final CameraCharacteristics characteristics =
85                 ShadowCameraCharacteristics.newCameraCharacteristics();
86         final ShadowCameraCharacteristics shadowCharacteristics = Shadow.extract(characteristics);
87         shadowCharacteristics.set(CameraCharacteristics.FLASH_INFO_AVAILABLE, true);
88         shadowCharacteristics
89                 .set(CameraCharacteristics.LENS_FACING, CameraCharacteristics.LENS_FACING_BACK);
90         mShadowCameraManager.addCamera("camera_id", characteristics);
91 
92         assertThat(FlashlightSlice.isFlashlightAvailable(mContext)).isTrue();
93     }
94 }
95