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 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 
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.RobolectricTestRunner;
35 import org.robolectric.RuntimeEnvironment;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public class TimeZonePreferenceControllerTest {
39 
40     @Mock
41     private AutoTimeZonePreferenceController mAutoTimeZonePreferenceController;
42 
43     private Context mContext;
44     private TimeZonePreferenceController mController;
45     private RestrictedPreference mPreference;
46 
47     @Before
setUp()48     public void setUp() {
49         MockitoAnnotations.initMocks(this);
50         mContext = RuntimeEnvironment.application;
51         mPreference = new RestrictedPreference(mContext);
52         mController = spy(new TimeZonePreferenceController(mContext,
53                 mAutoTimeZonePreferenceController));
54     }
55 
56     @Test
isAlwaysAvailable()57     public void isAlwaysAvailable() {
58         assertThat(mController.isAvailable()).isTrue();
59     }
60 
61     @Test
updateState_autoTimeZoneEnabled_shouldDisablePref()62     public void updateState_autoTimeZoneEnabled_shouldDisablePref() {
63         // Make sure not disabled by admin.
64         mPreference.setDisabledByAdmin(null);
65 
66         doReturn("test timezone").when(mController).getTimeZoneOffsetAndName();
67         when(mAutoTimeZonePreferenceController.isEnabled()).thenReturn(true);
68         mController.updateState(mPreference);
69 
70         assertThat(mPreference.isEnabled()).isFalse();
71     }
72 
73     @Test
updateState_autoTimeZoneDisabled_shouldEnablePref()74     public void updateState_autoTimeZoneDisabled_shouldEnablePref() {
75         // Make sure not disabled by admin.
76         mPreference.setDisabledByAdmin(null);
77 
78         doReturn("test timezone").when(mController).getTimeZoneOffsetAndName();
79         when(mAutoTimeZonePreferenceController.isEnabled()).thenReturn(false);
80         mController.updateState(mPreference);
81 
82         assertThat(mPreference.isEnabled()).isTrue();
83     }
84 }
85