1 /*
2  * Copyright (C) 2023 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.print;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 
27 import android.content.Context;
28 import android.os.Looper;
29 import android.view.View;
30 
31 import androidx.preference.PreferenceManager;
32 import androidx.preference.PreferenceScreen;
33 import androidx.test.core.app.ApplicationProvider;
34 import androidx.test.ext.junit.runners.AndroidJUnit4;
35 import androidx.test.platform.app.InstrumentationRegistry;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Spy;
42 import org.mockito.junit.MockitoJUnit;
43 import org.mockito.junit.MockitoRule;
44 
45 @RunWith(AndroidJUnit4.class)
46 public class PrintSettingsFragmentTest {
47     @Rule
48     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
49     @Spy
50     private final Context mContext = ApplicationProvider.getApplicationContext();
51 
52     private PrintSettingsFragment mFragment;
53     private PreferenceManager mPreferenceManager;
54     private PreferenceScreen mPreferenceScreen;
55 
56     @Before
setUp()57     public void setUp() {
58         if (Looper.myLooper() == null) {
59             Looper.prepare();
60         }
61         mPreferenceManager = new PreferenceManager(mContext);
62         mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
63 
64         InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
65             mFragment = spy(new PrintSettingsFragment());
66             doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
67         });
68     }
69 
70     @Test
setupPreferences_uiIsRestricted_doNotAddPreferences()71     public void setupPreferences_uiIsRestricted_doNotAddPreferences() {
72         mFragment.mIsUiRestricted = true;
73 
74         mFragment.setupPreferences();
75 
76         verify(mFragment, never()).findPreference(any(CharSequence.class));
77     }
78 
79     @Test
setupEmptyViews_uiIsRestricted_doNotSetEmptyView()80     public void setupEmptyViews_uiIsRestricted_doNotSetEmptyView() {
81         mFragment.mIsUiRestricted = true;
82 
83         mFragment.setupEmptyViews();
84 
85         verify(mFragment, never()).setEmptyView(any(View.class));
86     }
87 
88     @Test
startSettings_uiIsRestricted_removeAllPreferences()89     public void startSettings_uiIsRestricted_removeAllPreferences() {
90         mFragment.mIsUiRestricted = true;
91 
92         mFragment.startSettings();
93 
94         assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
95         verify(mFragment, never()).setHasOptionsMenu(true);
96     }
97 }
98