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