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