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