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