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 package com.android.settings.fuelgauge; 17 18 import android.content.Context; 19 import android.graphics.drawable.Drawable; 20 import android.graphics.drawable.VectorDrawable; 21 import android.support.v7.preference.PreferenceViewHolder; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.widget.LinearLayout; 25 import android.widget.TextView; 26 27 import com.android.settings.R; 28 import com.android.settings.TestConfig; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RuntimeEnvironment; 36 import org.robolectric.annotation.Config; 37 38 import static com.google.common.truth.Truth.assertThat; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 42 public class PowerGaugePreferenceTest { 43 private static final String SUBTITLE = "Summary"; 44 private static final String CONTENT_DESCRIPTION = "Content description"; 45 private Context mContext; 46 private PowerGaugePreference mPowerGaugePreference; 47 private View mRootView; 48 private View mWidgetView; 49 private PreferenceViewHolder mPreferenceViewHolder; 50 51 @Before 52 public void setUp() { 53 MockitoAnnotations.initMocks(this); 54 55 mContext = RuntimeEnvironment.application; 56 mRootView = LayoutInflater.from(mContext).inflate(R.layout.preference, 57 null); 58 mWidgetView = LayoutInflater.from(mContext).inflate(R.layout.preference_widget_summary, 59 null); 60 ((LinearLayout) mRootView.findViewById(android.R.id.widget_frame)).addView(mWidgetView); 61 mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests(mRootView); 62 63 mPowerGaugePreference = new PowerGaugePreference(mContext); 64 } 65 66 @Test 67 public void testOnBindViewHolder_bindSubtitle() { 68 mPowerGaugePreference.setSubtitle(SUBTITLE); 69 mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder); 70 71 assertThat(((TextView) mPreferenceViewHolder.findViewById( 72 R.id.widget_summary)).getText()).isEqualTo(SUBTITLE); 73 } 74 75 @Test 76 public void testOnBindViewHolder_showAnomaly_bindAnomalyIcon() { 77 mPowerGaugePreference.shouldShowAnomalyIcon(true); 78 mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder); 79 80 final Drawable[] drawables = ((TextView) mPreferenceViewHolder.findViewById( 81 R.id.widget_summary)).getCompoundDrawablesRelative(); 82 83 assertThat(drawables[0]).isInstanceOf(VectorDrawable.class); 84 } 85 86 @Test 87 public void testOnBindViewHolder_notShowAnomaly_bindAnomalyIcon() { 88 mPowerGaugePreference.shouldShowAnomalyIcon(false); 89 mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder); 90 91 final Drawable[] drawables = ((TextView) mPreferenceViewHolder.findViewById( 92 R.id.widget_summary)).getCompoundDrawablesRelative(); 93 94 assertThat(drawables[0]).isNull(); 95 } 96 97 @Test 98 public void testOnBindViewHolder_bindContentDescription() { 99 mPowerGaugePreference.setContentDescription(CONTENT_DESCRIPTION); 100 mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder); 101 102 assertThat(mPreferenceViewHolder.findViewById(android.R.id.title).getContentDescription()) 103 .isEqualTo(CONTENT_DESCRIPTION); 104 } 105 } 106