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