1/* 2 * Copyright (C) 2017 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 17package com.android.settingslib; 18 19 20import android.content.Context; 21import android.support.v7.preference.Preference; 22import android.support.v7.preference.PreferenceViewHolder; 23import android.view.View; 24import android.widget.TextView; 25 26import org.junit.Before; 27import org.junit.Test; 28import org.junit.runner.RunWith; 29import org.mockito.Mock; 30import org.mockito.MockitoAnnotations; 31import org.robolectric.annotation.Config; 32 33import static org.mockito.Mockito.RETURNS_DEEP_STUBS; 34import static org.mockito.Mockito.mock; 35import static org.mockito.Mockito.never; 36import static org.mockito.Mockito.verify; 37import static org.mockito.Mockito.when; 38 39@RunWith(SettingLibRobolectricTestRunner.class) 40@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 41public class RestrictedPreferenceHelperTest { 42 43 44 @Mock 45 private Context mContext; 46 @Mock 47 private Preference mPreference; 48 49 private PreferenceViewHolder mViewHolder; 50 private RestrictedPreferenceHelper mHelper; 51 52 @Before 53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 mViewHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class)); 56 mHelper = new RestrictedPreferenceHelper(mContext, mPreference, null); 57 } 58 59 @Test 60 public void bindPreference_disabled_shouldDisplayDisabledSummary() { 61 final TextView summaryView = mock(TextView.class, RETURNS_DEEP_STUBS); 62 when(mViewHolder.itemView.findViewById(android.R.id.summary)) 63 .thenReturn(summaryView); 64 when(summaryView.getContext().getText(R.string.disabled_by_admin_summary_text)) 65 .thenReturn("test"); 66 67 mHelper.useAdminDisabledSummary(true); 68 mHelper.setDisabledByAdmin(new RestrictedLockUtils.EnforcedAdmin()); 69 mHelper.onBindViewHolder(mViewHolder); 70 71 verify(summaryView).setText("test"); 72 verify(summaryView, never()).setVisibility(View.GONE); 73 } 74 75 @Test 76 public void bindPreference_notDisabled_shouldNotHideSummary() { 77 final TextView summaryView = mock(TextView.class, RETURNS_DEEP_STUBS); 78 when(mViewHolder.itemView.findViewById(android.R.id.summary)) 79 .thenReturn(summaryView); 80 when(summaryView.getContext().getText(R.string.disabled_by_admin_summary_text)) 81 .thenReturn("test"); 82 when(summaryView.getText()).thenReturn("test"); 83 84 mHelper.useAdminDisabledSummary(true); 85 mHelper.setDisabledByAdmin(null); 86 mHelper.onBindViewHolder(mViewHolder); 87 88 verify(summaryView).setText(null); 89 verify(summaryView, never()).setVisibility(View.GONE); 90 } 91} 92