Add new expandable preference component

Bug: 177406865
Bug: 185187729
Test: make SettingsRoboTests
Test: make SettingsGoogleRoboTests
Change-Id: Id2b2a1f4fa557467efcaa013aa9e978b2c66d974
This commit is contained in:
ykhung
2021-04-21 15:53:51 +08:00
committed by YUKAI HUNG
parent ee2086872e
commit b8721cf980
5 changed files with 308 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public final class ExpandDividerPreferenceTest {
private Context mContext;
private ExpandDividerPreference mExpandDividerPreference;
@Mock private ImageView mImageView;
@Mock private TextView mTextView;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mExpandDividerPreference = new ExpandDividerPreference(mContext);
doReturn(R.id.expand_title).when(mTextView).getId();
doReturn(R.id.expand_icon).when(mImageView).getId();
}
@Test
public void testConstructor_returnExpectedResult() {
assertThat(mExpandDividerPreference.getKey())
.isEqualTo(ExpandDividerPreference.PREFERENCE_KEY);
assertThat(mExpandDividerPreference.getLayoutResource())
.isEqualTo(R.layout.preference_expand_divider);
}
@Test
public void testSetTitle_setTitleContentIntoTextView() {
final String titleContent = "title content";
mExpandDividerPreference.mTextView = mTextView;
mExpandDividerPreference.setTitle(titleContent);
verify(mTextView).setText(titleContent);
}
@Test
public void testOnClick_switchExpandStateAndInvokeCallback() {
final boolean[] isExpandedArray = new boolean[] {false};
mExpandDividerPreference.mImageView = mImageView;
mExpandDividerPreference.setOnExpandListener(
isExpanded -> isExpandedArray[0] = isExpanded);
// Click the item first time from false -> true.
mExpandDividerPreference.onClick();
// Verifies the first time click result.
verify(mImageView).setImageResource(R.drawable.ic_settings_expand_less);
assertThat(isExpandedArray[0]).isTrue();
// Clicks the item second time from true -> false.
mExpandDividerPreference.onClick();
// Verifies the second time click result.
verify(mImageView).setImageResource(R.drawable.ic_settings_expand_more);
assertThat(isExpandedArray[0]).isFalse();
}
@Test
public void testSetIsExpanded_updateStateButNotInvokeCallback() {
final boolean[] isExpandedArray = new boolean[] {false};
mExpandDividerPreference.mImageView = mImageView;
mExpandDividerPreference.setOnExpandListener(
isExpanded -> isExpandedArray[0] = isExpanded);
mExpandDividerPreference.setIsExpanded(true);
verify(mImageView).setImageResource(R.drawable.ic_settings_expand_less);
assertThat(isExpandedArray[0]).isFalse();
}
}