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