1 /* 2 * Copyright (C) 2021 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.fuelgauge; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Matchers.anyInt; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.never; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.content.Context; 31 import android.widget.ImageView; 32 import android.widget.TextView; 33 34 import com.android.settings.R; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.ArgumentCaptor; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.RuntimeEnvironment; 43 44 @RunWith(RobolectricTestRunner.class) 45 public final class ExpandDividerPreferenceTest { 46 47 private Context mContext; 48 private ExpandDividerPreference mExpandDividerPreference; 49 50 private ImageView mImageView; 51 private TextView mTextView; 52 53 @Before setUp()54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 mContext = spy(RuntimeEnvironment.application); 57 mImageView = spy(new ImageView(mContext)); 58 mTextView = spy(new TextView(mContext)); 59 mExpandDividerPreference = new ExpandDividerPreference(mContext); 60 doReturn(R.id.expand_title).when(mTextView).getId(); 61 doReturn(R.id.expand_icon).when(mImageView).getId(); 62 } 63 64 @Test testConstructor_returnExpectedResult()65 public void testConstructor_returnExpectedResult() { 66 assertThat(mExpandDividerPreference.getKey()) 67 .isEqualTo(ExpandDividerPreference.PREFERENCE_KEY); 68 assertThat(mExpandDividerPreference.getLayoutResource()) 69 .isEqualTo(R.layout.preference_expand_divider); 70 } 71 72 @Test testSetTitle_setTitleContentIntoTextView()73 public void testSetTitle_setTitleContentIntoTextView() { 74 final String titleContent = "title content"; 75 mExpandDividerPreference.mTextView = mTextView; 76 77 mExpandDividerPreference.setTitle(titleContent); 78 final ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); 79 verify(mTextView).postDelayed(captor.capture(), eq(50L)); 80 captor.getValue().run(); 81 verify(mTextView).setText(titleContent); 82 } 83 84 @Test testOnClick_switchExpandStateAndInvokeCallback()85 public void testOnClick_switchExpandStateAndInvokeCallback() { 86 final boolean[] isExpandedArray = new boolean[] {false}; 87 mExpandDividerPreference.mImageView = mImageView; 88 mExpandDividerPreference.setOnExpandListener( 89 isExpanded -> isExpandedArray[0] = isExpanded); 90 91 // Click the item first time from false -> true. 92 mExpandDividerPreference.onClick(); 93 // Verifies the first time click result. 94 verify(mImageView).setImageResource(R.drawable.ic_settings_expand_less); 95 assertThat(isExpandedArray[0]).isTrue(); 96 97 // Clicks the item second time from true -> false. 98 mExpandDividerPreference.onClick(); 99 // Verifies the second time click result. 100 verify(mImageView).setImageResource(R.drawable.ic_settings_expand_more); 101 assertThat(isExpandedArray[0]).isFalse(); 102 } 103 104 @Test testSetIsExpanded_updateStateButNotInvokeCallback()105 public void testSetIsExpanded_updateStateButNotInvokeCallback() { 106 final boolean[] isExpandedArray = new boolean[] {false}; 107 mExpandDividerPreference.mImageView = mImageView; 108 mExpandDividerPreference.setOnExpandListener( 109 isExpanded -> isExpandedArray[0] = isExpanded); 110 111 mExpandDividerPreference.setIsExpanded(true); 112 113 verify(mImageView).setImageResource(R.drawable.ic_settings_expand_less); 114 assertThat(isExpandedArray[0]).isFalse(); 115 } 116 } 117