Home | History | Annotate | Download | only in settings
      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 android.content.Context;
     19 import android.support.v7.preference.PreferenceViewHolder;
     20 import android.text.TextUtils;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.widget.LinearLayout;
     24 import android.widget.TextView;
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 import org.robolectric.RuntimeEnvironment;
     29 import org.robolectric.annotation.Config;
     30 
     31 import static junit.framework.Assert.assertEquals;
     32 import static junit.framework.Assert.assertTrue;
     33 
     34 @RunWith(SettingsRobolectricTestRunner.class)
     35 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     36 public class SummaryPreferenceTest {
     37 
     38     private Context mContext;
     39     private PreferenceViewHolder mHolder;
     40     private SummaryPreference mPreference;
     41 
     42     @Before
     43     public void setUp() {
     44         mContext = RuntimeEnvironment.application;
     45         mPreference = new SummaryPreference(mContext, null);
     46 
     47         LayoutInflater inflater = LayoutInflater.from(mContext);
     48         final View view = inflater.inflate(mPreference.getLayoutResource(),
     49                 new LinearLayout(mContext), false);
     50 
     51         mHolder = PreferenceViewHolder.createInstanceForTests(view);
     52     }
     53 
     54     @Test
     55     public void disableChart_shouldNotRender() {
     56         mPreference.setChartEnabled(false);
     57         mPreference.onBindViewHolder(mHolder);
     58 
     59         assertTrue(
     60                 TextUtils.isEmpty(((TextView) mHolder.findViewById(android.R.id.text1)).getText()));
     61         assertTrue(
     62                 TextUtils.isEmpty(((TextView) mHolder.findViewById(android.R.id.text2)).getText()));
     63     }
     64 
     65     @Test
     66     public void enableChart_shouldRender() {
     67         final String testLabel1 = "label1";
     68         final String testLabel2 = "label2";
     69         mPreference.setChartEnabled(true);
     70         mPreference.setLabels(testLabel1, testLabel2);
     71         mPreference.onBindViewHolder(mHolder);
     72 
     73         assertEquals(testLabel1,
     74                 ((TextView) mHolder.findViewById(android.R.id.text1)).getText());
     75         assertEquals(testLabel2,
     76                 ((TextView) mHolder.findViewById(android.R.id.text2)).getText());
     77     }
     78 
     79 }
     80