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.enterprise;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.provider.Settings;
25 import android.text.format.DateUtils;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.testutils.FakeFeatureFactory;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Answers;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 
41 /**
42  * Common base for testing subclasses of {@link AdminActionPreferenceControllerBase}.
43  */
44 public abstract class AdminActionPreferenceControllerTestBase {
45 
46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
47     protected Context mContext;
48     protected FakeFeatureFactory mFeatureFactory;
49 
50     protected AdminActionPreferenceControllerBase mController;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         mFeatureFactory = FakeFeatureFactory.setupForTest();
56     }
57 
setDate(Date date)58     public abstract void setDate(Date date);
59 
60     @Test
testUpdateState()61     public void testUpdateState() {
62         final Preference preference = new Preference(mContext, null, 0, 0);
63         when(mContext.getString(R.string.enterprise_privacy_none)).thenReturn("None");
64         Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24, "24");
65 
66         setDate(null);
67         mController.updateState(preference);
68         assertThat(preference.getSummary()).isEqualTo("None");
69 
70         final Date date = new GregorianCalendar(2011 /* year */, 10 /* month */, 9 /* dayOfMonth */,
71                 8 /* hourOfDay */, 7 /* minute */, 6 /* second */).getTime();
72         setDate(date);
73         mController.updateState(preference);
74         assertThat(preference.getSummary()).isEqualTo(DateUtils.formatDateTime(
75                 mContext, date.getTime(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE));
76     }
77 
78     @Test
testIsAvailable()79     public void testIsAvailable() {
80         assertThat(mController.isAvailable()).isTrue();
81     }
82 
83     @Test
testHandlePreferenceTreeClick()84     public void testHandlePreferenceTreeClick() {
85         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
86                 .isFalse();
87     }
88 
getPreferenceKey()89     public abstract String getPreferenceKey();
90 
91     @Test
testGetPreferenceKey()92     public void testGetPreferenceKey() {
93         assertThat(mController.getPreferenceKey()).isEqualTo(getPreferenceKey());
94     }
95 }
96