• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 package com.android.car.settings.common;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.view.ContextThemeWrapper;
24 import android.view.View;
25 
26 import androidx.annotation.XmlRes;
27 import androidx.preference.PreferenceViewHolder;
28 
29 import com.android.car.settings.CarSettingsRobolectricTestRunner;
30 import com.android.car.settings.R;
31 import com.android.car.settings.testutils.FragmentController;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.RuntimeEnvironment;
37 
38 @RunWith(CarSettingsRobolectricTestRunner.class)
39 public class TwoActionPreferenceTest {
40 
41     private static class TestTwoActionPreference extends TwoActionPreference {
42 
TestTwoActionPreference(Context context)43         TestTwoActionPreference(Context context) {
44             super(context);
45         }
46 
47         @Override
onBindWidgetFrame(View actionContainer)48         protected void onBindWidgetFrame(View actionContainer) {
49             // Intentionally empty.
50         }
51     }
52 
53     private Context mContext;
54     private PreferenceViewHolder mViewHolder;
55     private TestTwoActionPreference mTestTwoActionPreference;
56 
57     @Before
setUp()58     public void setUp() {
59         mContext = RuntimeEnvironment.application;
60         Context themedContext = new ContextThemeWrapper(mContext, R.style.CarSettingTheme);
61         View rootView = View.inflate(themedContext, R.layout.two_action_preference,
62                 null);
63         mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
64         mTestTwoActionPreference = new TestTwoActionPreference(mContext);
65     }
66 
67     @Test
showAction_true_buttonVisible()68     public void showAction_true_buttonVisible() {
69         mTestTwoActionPreference.showAction(true);
70         mTestTwoActionPreference.onBindViewHolder(mViewHolder);
71 
72         assertThat(mViewHolder.findViewById(
73                 R.id.action_widget_container).getVisibility()).isEqualTo(View.VISIBLE);
74     }
75 
76     @Test
showAction_false_buttonGone()77     public void showAction_false_buttonGone() {
78         mTestTwoActionPreference.showAction(false);
79         mTestTwoActionPreference.onBindViewHolder(mViewHolder);
80 
81         assertThat(mViewHolder.findViewById(
82                 R.id.action_widget_container).getVisibility()).isEqualTo(View.GONE);
83     }
84 
85     @Test
isActionShown_true()86     public void isActionShown_true() {
87         mTestTwoActionPreference.showAction(true);
88         assertThat(mTestTwoActionPreference.isActionShown()).isTrue();
89     }
90 
91     @Test
isActionShown_false()92     public void isActionShown_false() {
93         mTestTwoActionPreference.showAction(false);
94         assertThat(mTestTwoActionPreference.isActionShown()).isFalse();
95     }
96 
97     @Test
preferenceConstructed_attrDefined_actionShown()98     public void preferenceConstructed_attrDefined_actionShown() {
99         TestSettingsFragment fragment = TestSettingsFragment.newInstance(
100                 R.xml.two_action_preference_shown);
101         FragmentController.of(fragment).setup();
102 
103         TwoActionPreference preference =
104                 (TwoActionPreference) fragment.getPreferenceScreen().findPreference(
105                         mContext.getString(R.string.tpk_two_action_preference));
106 
107         assertThat(preference.isActionShown()).isTrue();
108     }
109 
110     @Test
preferenceConstructed_defaultValue_actionShown()111     public void preferenceConstructed_defaultValue_actionShown() {
112         TestSettingsFragment fragment = TestSettingsFragment.newInstance(
113                 R.xml.two_action_preference_action_shown_not_specified);
114         FragmentController.of(fragment).setup();
115 
116         TwoActionPreference preference =
117                 (TwoActionPreference) fragment.getPreferenceScreen().findPreference(
118                         mContext.getString(R.string.tpk_two_action_preference));
119 
120         assertThat(preference.isActionShown()).isTrue();
121     }
122 
123     @Test
preferenceConstructed_attrDefined_actionHidden()124     public void preferenceConstructed_attrDefined_actionHidden() {
125         TestSettingsFragment fragment = TestSettingsFragment.newInstance(
126                 R.xml.two_action_preference_hidden);
127         FragmentController.of(fragment).setup();
128 
129         TwoActionPreference preference =
130                 (TwoActionPreference) fragment.getPreferenceScreen().findPreference(
131                         mContext.getString(R.string.tpk_two_action_preference));
132 
133         assertThat(preference.isActionShown()).isFalse();
134     }
135 
136     /**
137      * Test settings fragment which creates the xml screen provided via {@link #newInstance(int)}.
138      */
139     public static class TestSettingsFragment extends SettingsFragment {
140 
141         private static final String ARG_SCREEN_RES = "arg_screen_res";
142 
143         @XmlRes
144         private int mScreenResId;
145 
newInstance(@mlRes int screenResId)146         public static TestSettingsFragment newInstance(@XmlRes int screenResId) {
147             Bundle arguments = new Bundle();
148             arguments.putInt(ARG_SCREEN_RES, screenResId);
149 
150             TestSettingsFragment fragment = new TestSettingsFragment();
151             fragment.setArguments(arguments);
152             return fragment;
153         }
154 
155         @Override
onAttach(Context context)156         public void onAttach(Context context) {
157             super.onAttach(context);
158             mScreenResId = getArguments().getInt(ARG_SCREEN_RES);
159         }
160 
161         @Override
162         @XmlRes
getPreferenceScreenResId()163         protected int getPreferenceScreenResId() {
164             return mScreenResId;
165         }
166     }
167 }
168