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.settings.widget;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 
27 import android.content.Context;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.LinearLayout;
31 
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceViewHolder;
34 import androidx.preference.R;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class DisabledCheckBoxPreferenceTest {
44 
45     private Context mContext;
46     private View mRootView;
47     private DisabledCheckBoxPreference mPref;
48     private PreferenceViewHolder mHolder;
49 
50     @Before
setUp()51     public void setUp() {
52         mContext = RuntimeEnvironment.application;
53         mRootView = View.inflate(mContext, R.layout.preference, null /* parent */);
54         mHolder = PreferenceViewHolder.createInstanceForTests(mRootView);
55         mPref = new DisabledCheckBoxPreference(mContext);
56     }
57 
inflatePreference()58     private void inflatePreference() {
59         final LayoutInflater inflater = LayoutInflater.from(mContext);
60         final LinearLayout widgetView = mHolder.itemView.findViewById(android.R.id.widget_frame);
61         assertThat(widgetView).isNotNull();
62         inflater.inflate(R.layout.preference_widget_checkbox, widgetView, true);
63         mPref.onBindViewHolder(mHolder);
64     }
65 
66     @Test
onBindViewHolder_checkboxDisabled()67     public void onBindViewHolder_checkboxDisabled() {
68         inflatePreference();
69         assertThat(mRootView.findViewById(android.R.id.checkbox).isEnabled()).isFalse();
70     }
71 
72     @Test
checkboxOnClick_checkboxDisabled()73     public void checkboxOnClick_checkboxDisabled() {
74         Preference.OnPreferenceClickListener onClick =
75                 mock(Preference.OnPreferenceClickListener.class);
76         mPref.setOnPreferenceClickListener(onClick);
77         inflatePreference();
78 
79         mPref.enableCheckbox(false);
80         mPref.performClick(mRootView);
81 
82         verify(onClick, never()).onPreferenceClick(any());
83     }
84 
85     @Test
checkboxOnClick_checkboxEnabled()86     public void checkboxOnClick_checkboxEnabled() {
87         Preference.OnPreferenceClickListener onClick =
88                 mock(Preference.OnPreferenceClickListener.class);
89         mPref.setOnPreferenceClickListener(onClick);
90         inflatePreference();
91 
92         mPref.enableCheckbox(true);
93         mPref.performClick(mRootView);
94 
95         verify(onClick, times(1)).onPreferenceClick(any());
96     }
97 }
98