1 /*
2  * Copyright (C) 2021 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.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.content.Context;
24 import android.view.View;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.RobolectricTestRunner;
34 import org.robolectric.RuntimeEnvironment;
35 import org.robolectric.util.ReflectionHelpers;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public class SettingsMainSwitchPreferenceTest {
39 
40     @Mock
41     private EnforcedAdmin mEnforcedAdmin;
42     private SettingsMainSwitchPreference mPreference;
43     private PreferenceViewHolder mHolder;
44 
45     @Before
setUp()46     public void setUp() {
47         MockitoAnnotations.initMocks(this);
48         final Context context = RuntimeEnvironment.application;
49         final SettingsMainSwitchBar switchBar = new SettingsMainSwitchBar(context);
50         mPreference = new SettingsMainSwitchPreference(context);
51         ReflectionHelpers.setField(mPreference, "mEnforcedAdmin", mEnforcedAdmin);
52         ReflectionHelpers.setField(mPreference, "mMainSwitchBar", switchBar);
53         final View rootView = View.inflate(context, com.android.settings.R.layout.preference_widget_main_switch,
54                 null /* parent */);
55         mHolder = PreferenceViewHolder.createInstanceForTests(rootView);
56     }
57 
58     @Test
show_preferenceShouldDisplay()59     public void show_preferenceShouldDisplay() {
60         mPreference.show();
61 
62         mPreference.onBindViewHolder(mHolder);
63 
64         assertThat(mPreference.isShowing()).isTrue();
65         assertThat(mPreference.isVisible()).isTrue();
66     }
67 
68     @Test
hide_preferenceShouldNotDisplay()69     public void hide_preferenceShouldNotDisplay() {
70         mPreference.hide();
71 
72         mPreference.onBindViewHolder(mHolder);
73 
74         assertThat(mPreference.isShowing()).isFalse();
75         assertThat(mPreference.isVisible()).isFalse();
76     }
77 }
78