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