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