1 /* 2 * Copyright (C) 2017 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 com.android.settings.development; 18 19 import android.content.Context; 20 import android.os.SystemProperties; 21 import android.support.v14.preference.SwitchPreference; 22 import android.support.v7.preference.PreferenceScreen; 23 24 import com.android.settings.R; 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 import com.android.settings.TestConfig; 27 import com.android.settings.testutils.shadow.SettingsShadowSystemProperties; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Answers; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.annotation.Config; 37 import org.robolectric.RuntimeEnvironment; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Mockito.verify; 41 import static org.mockito.Mockito.when; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 45 shadows = {SettingsShadowSystemProperties.class}) 46 public class CameraHalHdrplusPreferenceControllerTest { 47 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private Context mContext; 50 @Mock 51 private PreferenceScreen mScreen; 52 @Mock 53 private SwitchPreference mPreference; 54 55 static final String USERDEBUG_BUILD = "userdebug"; 56 57 private CameraHalHdrplusPreferenceController mController; 58 59 @Before 60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mController = new CameraHalHdrplusPreferenceController(mContext); 63 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 64 when(mPreference.getKey()).thenReturn(mController.getPreferenceKey()); 65 } 66 67 @After 68 public void tearDown() { 69 SettingsShadowSystemProperties.clear(); 70 } 71 72 @Test 73 public void isAvailable_withConfigNoShow_shouldReturnFalse() { 74 when(mContext.getResources().getBoolean(R.bool.config_show_camera_hal_hdrplus)) 75 .thenReturn(false); 76 assertThat(mController.isAvailable()).isFalse(); 77 } 78 79 @Test 80 public void displayPreference_cameraHalHdrplusEnabled_shouldCheckedPreference() { 81 when(mContext.getResources().getBoolean(R.bool.config_show_camera_hal_hdrplus)) 82 .thenReturn(true); 83 84 SettingsShadowSystemProperties.set( 85 CameraHalHdrplusPreferenceController.PROPERTY_CAMERA_HAL_HDRPLUS, 86 CameraHalHdrplusPreferenceController.ENABLED); 87 SettingsShadowSystemProperties.set( 88 CameraHalHdrplusPreferenceController.BUILD_TYPE, USERDEBUG_BUILD); 89 90 mController.displayPreference(mScreen); 91 92 verify(mPreference).setChecked(true); 93 } 94 95 @Test 96 public void displayPreference_cameraHalHdrplusEnabled_shouldUncheckedPreference() { 97 when(mContext.getResources().getBoolean(R.bool.config_show_camera_hal_hdrplus)) 98 .thenReturn(true); 99 100 SettingsShadowSystemProperties.set( 101 CameraHalHdrplusPreferenceController.PROPERTY_CAMERA_HAL_HDRPLUS, 102 CameraHalHdrplusPreferenceController.DISABLED); 103 SettingsShadowSystemProperties.set( 104 CameraHalHdrplusPreferenceController.BUILD_TYPE, USERDEBUG_BUILD); 105 106 mController.displayPreference(mScreen); 107 108 verify(mPreference).setChecked(false); 109 } 110 111 @Test 112 public void handlePreferenceTreeClick_preferenceChecked_shouldEnableCameraHalHdrplus() { 113 when(mContext.getResources().getBoolean(R.bool.config_show_camera_hal_hdrplus)) 114 .thenReturn(true); 115 116 when(mPreference.isChecked()).thenReturn(true); 117 118 when(mContext.getResources().getString(R.string.camera_hal_hdrplus_toast)).thenReturn( 119 RuntimeEnvironment.application.getString(R.string.camera_hal_hdrplus_toast)); 120 121 mController.handlePreferenceTreeClick(mPreference); 122 123 assertThat(CameraHalHdrplusPreferenceController.ENABLED.equals( 124 SystemProperties.get( 125 CameraHalHdrplusPreferenceController.PROPERTY_CAMERA_HAL_HDRPLUS, 126 CameraHalHdrplusPreferenceController.DISABLED))).isTrue(); 127 } 128 129 @Test 130 public void handlePreferenceTreeClick_preferenceUnchecked_shouldDisableCameraHalHdrplus() { 131 when(mContext.getResources().getBoolean(R.bool.config_show_camera_hal_hdrplus)) 132 .thenReturn(true); 133 134 when(mPreference.isChecked()).thenReturn(false); 135 136 when(mContext.getResources().getString(R.string.camera_hal_hdrplus_toast)).thenReturn( 137 RuntimeEnvironment.application.getString(R.string.camera_hal_hdrplus_toast)); 138 139 mController.handlePreferenceTreeClick(mPreference); 140 141 assertThat(CameraHalHdrplusPreferenceController.DISABLED.equals( 142 SystemProperties.get( 143 CameraHalHdrplusPreferenceController.PROPERTY_CAMERA_HAL_HDRPLUS, 144 CameraHalHdrplusPreferenceController.DISABLED))).isTrue(); 145 } 146 } 147