1 /*
2  * Copyright (C) 2019 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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.os.Vibrator;
23 import android.provider.Settings;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.BasePreferenceController;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 import org.robolectric.RuntimeEnvironment;
33 
34 @RunWith(RobolectricTestRunner.class)
35 public class VibrationPreferenceControllerTest {
36 
37     private static final String VIBRATION_ON = "On";
38     private static final String VIBRATION_OFF = "Off";
39 
40     private Context mContext;
41     private VibrationPreferenceController mController;
42 
43     @Before
setUp()44     public void setUp() {
45         mContext = RuntimeEnvironment.application;
46         mController = new VibrationPreferenceController(mContext, "vibration_pref");
47     }
48 
49     @Test
getAvailabilityStatus_byDefault_shouldReturnAvailable()50     public void getAvailabilityStatus_byDefault_shouldReturnAvailable() {
51         assertThat(mController.getAvailabilityStatus())
52                 .isEqualTo(BasePreferenceController.AVAILABLE);
53     }
54 
55     @Test
getSummary_disabledVibration_shouldReturnOffSummary()56     public void getSummary_disabledVibration_shouldReturnOffSummary() {
57         Settings.System.putInt(mContext.getContentResolver(),
58                 Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
59         Settings.System.putInt(mContext.getContentResolver(),
60                 Settings.System.NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
61         Settings.System.putInt(mContext.getContentResolver(),
62                 Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
63         final String expectedResult = mContext.getString(R.string.switch_off_text);
64 
65         assertThat(mController.getSummary()).isEqualTo(expectedResult);
66     }
67 
68     @Test
getSummary_enabledSomeVibration_shouldReturnVibrationOnSummary()69     public void getSummary_enabledSomeVibration_shouldReturnVibrationOnSummary() {
70         Settings.System.putInt(mContext.getContentResolver(),
71                 Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
72         Settings.System.putInt(mContext.getContentResolver(),
73                 Settings.System.VIBRATE_WHEN_RINGING, 1);
74         Settings.System.putInt(mContext.getContentResolver(),
75                 Settings.System.NOTIFICATION_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
76         Settings.System.putInt(mContext.getContentResolver(),
77                 Settings.System.HAPTIC_FEEDBACK_INTENSITY, Vibrator.VIBRATION_INTENSITY_MEDIUM);
78         Settings.System.putInt(mContext.getContentResolver(),
79                 Settings.System.HAPTIC_FEEDBACK_ENABLED, 1);
80         final String expectedResult = mContext.getString(R.string.accessibility_vibration_summary,
81                 VIBRATION_ON /* ring */,
82                 VIBRATION_OFF /* notification */,
83                 VIBRATION_ON /* touch */);
84 
85         assertThat(mController.getSummary()).isEqualTo(expectedResult);
86     }
87 }
88