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.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.testutils.SettingsRobolectricTestRunner; 28 import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.annotation.Config; 34 import org.robolectric.RuntimeEnvironment; 35 36 @RunWith(SettingsRobolectricTestRunner.class) 37 @Config(shadows = SettingsShadowResourcesImpl.class) 38 public class SummaryPreferenceTest { 39 40 private PreferenceViewHolder mHolder; 41 private SummaryPreference mPreference; 42 43 @Before setUp()44 public void setUp() { 45 final Context context = RuntimeEnvironment.application; 46 mPreference = new SummaryPreference(context, null); 47 48 LayoutInflater inflater = LayoutInflater.from(context); 49 final View view = 50 inflater.inflate(mPreference.getLayoutResource(), new LinearLayout(context), false); 51 52 mHolder = PreferenceViewHolder.createInstanceForTests(view); 53 } 54 55 @Test disableChart_shouldNotRender()56 public void disableChart_shouldNotRender() { 57 mPreference.setChartEnabled(false); 58 mPreference.onBindViewHolder(mHolder); 59 60 final TextView textView1 = (TextView) mHolder.findViewById(android.R.id.text1); 61 assertThat(textView1.getText()).isEqualTo(""); 62 63 final TextView textView2 = (TextView) mHolder.findViewById(android.R.id.text2); 64 assertThat(textView2.getText()).isEqualTo(""); 65 } 66 67 @Test enableChart_shouldRender()68 public void enableChart_shouldRender() { 69 final String testLabel1 = "label1"; 70 final String testLabel2 = "label2"; 71 mPreference.setChartEnabled(true); 72 mPreference.setLabels(testLabel1, testLabel2); 73 mPreference.onBindViewHolder(mHolder); 74 75 final TextView textView1 = (TextView) mHolder.findViewById(android.R.id.text1); 76 assertThat(textView1.getText()).isEqualTo(testLabel1); 77 78 final TextView textView2 = (TextView) mHolder.findViewById(android.R.id.text2); 79 assertThat(textView2.getText()).isEqualTo(testLabel2); 80 } 81 } 82