• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.datetime;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 
26 import com.android.settings.testutils.SettingsRobolectricTestRunner;
27 import com.android.settingslib.RestrictedPreference;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.RuntimeEnvironment;
35 
36 @RunWith(SettingsRobolectricTestRunner.class)
37 public class TimeZonePreferenceControllerTest {
38 
39     @Mock
40     private AutoTimeZonePreferenceController mAutoTimeZonePreferenceController;
41 
42     private Context mContext;
43     private TimeZonePreferenceController mController;
44     private RestrictedPreference mPreference;
45 
46     @Before
setUp()47     public void setUp() {
48         MockitoAnnotations.initMocks(this);
49         mContext = RuntimeEnvironment.application;
50         mPreference = new RestrictedPreference(mContext);
51         mController = spy(new TimeZonePreferenceController(mContext,
52                 mAutoTimeZonePreferenceController));
53     }
54 
55     @Test
isAlwaysAvailable()56     public void isAlwaysAvailable() {
57         assertThat(mController.isAvailable()).isTrue();
58     }
59 
60     @Test
updateState_autoTimeZoneEnabled_shouldDisablePref()61     public void updateState_autoTimeZoneEnabled_shouldDisablePref() {
62         // Make sure not disabled by admin.
63         mPreference.setDisabledByAdmin(null);
64 
65         doReturn("test timezone").when(mController).getTimeZoneOffsetAndName();
66         when(mAutoTimeZonePreferenceController.isEnabled()).thenReturn(true);
67         mController.updateState(mPreference);
68 
69         assertThat(mPreference.isEnabled()).isFalse();
70     }
71 
72     @Test
updateState_autoTimeZoneDisabled_shouldEnablePref()73     public void updateState_autoTimeZoneDisabled_shouldEnablePref() {
74         // Make sure not disabled by admin.
75         mPreference.setDisabledByAdmin(null);
76 
77         doReturn("test timezone").when(mController).getTimeZoneOffsetAndName();
78         when(mAutoTimeZonePreferenceController.isEnabled()).thenReturn(false);
79         mController.updateState(mPreference);
80 
81         assertThat(mPreference.isEnabled()).isTrue();
82     }
83 }
84