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 static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doNothing;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.provider.Settings;
28 
29 import androidx.preference.PreferenceScreen;
30 import androidx.preference.SwitchPreference;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class RtlLayoutPreferenceControllerTest {
42 
43     @Mock
44     private PreferenceScreen mScreen;
45     @Mock
46     private SwitchPreference mPreference;
47 
48     private Context mContext;
49 
50     private RtlLayoutPreferenceController mController;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         mContext = RuntimeEnvironment.application;
56         mController = new RtlLayoutPreferenceController(mContext);
57         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
58         mController.displayPreference(mScreen);
59     }
60 
61     @Test
updateState_forceRtlEnabled_shouldCheckedPreference()62     public void updateState_forceRtlEnabled_shouldCheckedPreference() {
63         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVELOPMENT_FORCE_RTL,
64                 RtlLayoutPreferenceController.SETTING_VALUE_ON);
65 
66         mController.updateState(mPreference);
67 
68         verify(mPreference).setChecked(true);
69     }
70 
71     @Test
updateState_forceRtlDisabled_shouldUncheckedPreference()72     public void updateState_forceRtlDisabled_shouldUncheckedPreference() {
73         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVELOPMENT_FORCE_RTL,
74                 RtlLayoutPreferenceController.SETTING_VALUE_OFF);
75 
76         mController.updateState(mPreference);
77 
78         verify(mPreference).setChecked(false);
79     }
80 
81     @Test
onPreferenceChange_preferenceChecked_shouldEnableForceRtl()82     public void onPreferenceChange_preferenceChecked_shouldEnableForceRtl() {
83         mController = spy(mController);
84         doNothing().when(mController).updateLocales();
85         mController.onPreferenceChange(mPreference, true /* new value */);
86 
87         final int rtlLayoutMode = Settings.Global.getInt(mContext.getContentResolver(),
88                 Settings.Global.DEVELOPMENT_FORCE_RTL, -1 /* default */);
89 
90         assertThat(rtlLayoutMode).isEqualTo(RtlLayoutPreferenceController.SETTING_VALUE_ON);
91     }
92 
93     @Test
onPreferenceChange__preferenceUnchecked_shouldDisableForceRtl()94     public void onPreferenceChange__preferenceUnchecked_shouldDisableForceRtl() {
95         mController = spy(mController);
96         doNothing().when(mController).updateLocales();
97         mController.onPreferenceChange(mPreference, false /* new value */);
98 
99         final int rtlLayoutMode = Settings.Global.getInt(mContext.getContentResolver(),
100                 Settings.Global.DEVELOPMENT_FORCE_RTL, -1 /* default */);
101 
102         assertThat(rtlLayoutMode).isEqualTo(RtlLayoutPreferenceController.SETTING_VALUE_OFF);
103     }
104 
105     @Test
onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled()106     public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled() {
107         mController = spy(mController);
108         doNothing().when(mController).updateLocales();
109         mController.onDeveloperOptionsSwitchDisabled();
110 
111         final int rtlLayoutMode = Settings.Global.getInt(mContext.getContentResolver(),
112                 Settings.Global.DEVELOPMENT_FORCE_RTL, -1 /* default */);
113 
114         assertThat(rtlLayoutMode).isEqualTo(RtlLayoutPreferenceController.SETTING_VALUE_OFF);
115         verify(mPreference).setEnabled(false);
116         verify(mPreference).setChecked(false);
117     }
118 }
119