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 android.support.v7.preference.tests; 18 19import static org.mockito.ArgumentMatchers.anyBoolean; 20import static org.mockito.Mockito.never; 21import static org.mockito.Mockito.verify; 22import static org.mockito.Mockito.when; 23 24import android.support.test.InstrumentationRegistry; 25import android.support.test.filters.SmallTest; 26import android.support.test.runner.AndroidJUnit4; 27import android.support.v7.preference.Preference; 28import android.support.v7.preference.PreferenceViewHolder; 29import android.view.ViewGroup; 30import android.widget.TextView; 31 32import org.junit.Before; 33import org.junit.Test; 34import org.junit.runner.RunWith; 35import org.mockito.Mock; 36import org.mockito.MockitoAnnotations; 37 38@SmallTest 39@RunWith(AndroidJUnit4.class) 40public class PreferenceSingleLineTitleTest { 41 42 private Preference mPreference; 43 44 @Mock 45 private ViewGroup mViewGroup; 46 @Mock 47 private TextView mTitleView; 48 49 @Before 50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 when(mViewGroup.findViewById(android.R.id.title)).thenReturn(mTitleView); 53 54 mPreference = new Preference(InstrumentationRegistry.getTargetContext()); 55 mPreference.setTitle("Test Title"); 56 } 57 58 @Test 59 public void bindViewHolder_singleLineTitleNotSet_shouldNotSetSingleLine() { 60 PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mViewGroup); 61 mPreference.onBindViewHolder(holder); 62 63 verify(mTitleView, never()).setSingleLine(anyBoolean()); 64 } 65 66 @Test 67 public void bindViewHolder_singleLineTitleSetToTrue_shouldSetSingleLineToTrue() { 68 PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mViewGroup); 69 mPreference.setSingleLineTitle(true); 70 mPreference.onBindViewHolder(holder); 71 72 verify(mTitleView).setSingleLine(true); 73 } 74 75 @Test 76 public void bindViewHolder_singleLineTitleSetToFalse_shouldSetSingleLineToFalse() { 77 PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(mViewGroup); 78 mPreference.setSingleLineTitle(false); 79 mPreference.onBindViewHolder(holder); 80 81 verify(mTitleView).setSingleLine(false); 82 } 83 84} 85