1 /*
2  * Copyright (C) 2016 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;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.content.Context;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.widget.LinearLayout;
24 import android.widget.TextView;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 import org.robolectric.RuntimeEnvironment;
33 
34 @RunWith(RobolectricTestRunner.class)
35 public class SummaryPreferenceTest {
36 
37     private PreferenceViewHolder mHolder;
38     private SummaryPreference mPreference;
39 
40     @Before
setUp()41     public void setUp() {
42         final Context context = RuntimeEnvironment.application;
43         mPreference = new SummaryPreference(context, null);
44 
45         LayoutInflater inflater = LayoutInflater.from(context);
46         final View view =
47             inflater.inflate(mPreference.getLayoutResource(), new LinearLayout(context), false);
48 
49         mHolder = PreferenceViewHolder.createInstanceForTests(view);
50     }
51 
52     @Test
disableChart_shouldNotRender()53     public void disableChart_shouldNotRender() {
54         mPreference.setChartEnabled(false);
55         mPreference.onBindViewHolder(mHolder);
56 
57         final TextView textView1 = (TextView) mHolder.findViewById(android.R.id.text1);
58         assertThat(textView1.getText()).isEqualTo("");
59 
60         final TextView textView2 = (TextView) mHolder.findViewById(android.R.id.text2);
61         assertThat(textView2.getText()).isEqualTo("");
62     }
63 
64     @Test
enableChart_shouldRender()65     public void enableChart_shouldRender() {
66         final String testLabel1 = "label1";
67         final String testLabel2 = "label2";
68         mPreference.setChartEnabled(true);
69         mPreference.setLabels(testLabel1, testLabel2);
70         mPreference.onBindViewHolder(mHolder);
71 
72         final TextView textView1 = (TextView) mHolder.findViewById(android.R.id.text1);
73         assertThat(textView1.getText()).isEqualTo(testLabel1);
74 
75         final TextView textView2 = (TextView) mHolder.findViewById(android.R.id.text2);
76         assertThat(textView2.getText()).isEqualTo(testLabel2);
77     }
78 }
79