• 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.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 
25 import com.android.settings.testutils.SettingsRobolectricTestRunner;
26 import com.android.settingslib.RestrictedPreference;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.RuntimeEnvironment;
34 
35 @RunWith(SettingsRobolectricTestRunner.class)
36 public class TimePreferenceControllerTest {
37 
38     @Mock
39     private Context mContext;
40     @Mock
41     private TimePreferenceController.TimePreferenceHost mHost;
42     @Mock
43     private AutoTimePreferenceController mAutoTimePreferenceController;
44 
45     private TimePreferenceController mController;
46     private RestrictedPreference mPreference;
47 
48     @Before
setUp()49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51         mPreference = new RestrictedPreference(RuntimeEnvironment.application);
52         mController = new TimePreferenceController(mContext, mHost, mAutoTimePreferenceController);
53     }
54 
55     @Test
isAlwaysAvailable()56     public void isAlwaysAvailable() {
57         assertThat(mController.isAvailable()).isTrue();
58     }
59 
60     @Test
updateState_autoTimeEnabled_shouldDisablePref()61     public void updateState_autoTimeEnabled_shouldDisablePref() {
62         // Make sure not disabled by admin.
63         mPreference.setDisabledByAdmin(null);
64 
65         when(mAutoTimePreferenceController.isEnabled()).thenReturn(true);
66         mController.updateState(mPreference);
67 
68         assertThat(mPreference.isEnabled()).isFalse();
69     }
70 
71     @Test
updateState_autoTimeDisabled_shouldEnablePref()72     public void updateState_autoTimeDisabled_shouldEnablePref() {
73         // Make sure not disabled by admin.
74         mPreference.setDisabledByAdmin(null);
75 
76         when(mAutoTimePreferenceController.isEnabled()).thenReturn(false);
77         mController.updateState(mPreference);
78 
79         assertThat(mPreference.isEnabled()).isTrue();
80     }
81 
82     @Test
clickPreference_showTimePicker()83     public void clickPreference_showTimePicker() {
84         // Click a preference that's not controlled by this controller.
85         mPreference.setKey("fake_key");
86         assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse();
87 
88         // Click a preference controlled by this controller.
89         mPreference.setKey(mController.getPreferenceKey());
90         mController.handlePreferenceTreeClick(mPreference);
91         // Should show date picker
92         verify(mHost).showTimePicker();
93     }
94 }
95