diff --git a/src/com/android/settings/print/PrintSettingsFragment.java b/src/com/android/settings/print/PrintSettingsFragment.java index ed21b6f7f92..cd80998170c 100644 --- a/src/com/android/settings/print/PrintSettingsFragment.java +++ b/src/com/android/settings/print/PrintSettingsFragment.java @@ -28,6 +28,7 @@ import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import android.os.UserManager; import android.print.PrintJob; import android.print.PrintJobId; import android.print.PrintJobInfo; @@ -45,6 +46,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; +import androidx.annotation.VisibleForTesting; import androidx.loader.app.LoaderManager.LoaderCallbacks; import androidx.loader.content.AsyncTaskLoader; import androidx.loader.content.Loader; @@ -92,6 +94,22 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment private PrintServicesController mPrintServicesController; private Button mAddNewServiceButton; + @VisibleForTesting + boolean mIsUiRestricted; + + public PrintSettingsFragment() { + super(UserManager.DISALLOW_PRINTING); + } + + @Override + protected String getLogTag() { + return TAG; + } + + @Override + protected int getPreferenceScreenResId() { + return R.xml.print_settings; + } @Override public int getMetricsCategory() { @@ -107,12 +125,19 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = super.onCreateView(inflater, container, savedInstanceState); - addPreferencesFromResource(R.xml.print_settings); + mIsUiRestricted = isUiRestricted(); + setupPreferences(); + return root; + } - mActivePrintJobsCategory = (PreferenceCategory) findPreference( - PRINT_JOBS_CATEGORY); - mPrintServicesCategory = (PreferenceCategory) findPreference( - PRINT_SERVICES_CATEGORY); + @VisibleForTesting + void setupPreferences() { + if (mIsUiRestricted) { + return; + } + + mActivePrintJobsCategory = (PreferenceCategory) findPreference(PRINT_JOBS_CATEGORY); + mPrintServicesCategory = (PreferenceCategory) findPreference(PRINT_SERVICES_CATEGORY); getPreferenceScreen().removePreference(mActivePrintJobsCategory); mPrintJobsController = new PrintJobsController(); @@ -120,20 +145,20 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment mPrintServicesController = new PrintServicesController(); getLoaderManager().initLoader(LOADER_ID_PRINT_SERVICES, null, mPrintServicesController); - - return root; - } - - @Override - public void onStart() { - super.onStart(); - setHasOptionsMenu(true); - startSubSettingsIfNeeded(); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + setupEmptyViews(); + } + + @VisibleForTesting + void setupEmptyViews() { + if (mIsUiRestricted) { + return; + } + ViewGroup contentRoot = (ViewGroup) getListView().getParent(); View emptyView = getActivity().getLayoutInflater().inflate( R.layout.empty_print_state, contentRoot, false); @@ -152,6 +177,23 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment setEmptyView(emptyView); } + @Override + public void onStart() { + super.onStart(); + startSettings(); + } + + @VisibleForTesting + void startSettings() { + if (mIsUiRestricted) { + getPreferenceScreen().removeAll(); + return; + } + + setHasOptionsMenu(true); + startSubSettingsIfNeeded(); + } + @Override protected String getIntentActionString() { return Settings.ACTION_PRINT_SETTINGS; diff --git a/src/com/android/settings/print/ProfileSettingsPreferenceFragment.java b/src/com/android/settings/print/ProfileSettingsPreferenceFragment.java index e41e1dad3a1..63b83f1bf34 100644 --- a/src/com/android/settings/print/ProfileSettingsPreferenceFragment.java +++ b/src/com/android/settings/print/ProfileSettingsPreferenceFragment.java @@ -27,13 +27,17 @@ import android.widget.AdapterView; import android.widget.Spinner; import com.android.settings.R; -import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.dashboard.RestrictedDashboardFragment; import com.android.settings.dashboard.profileselector.UserAdapter; /** * Base fragment class for per profile settings. */ -public abstract class ProfileSettingsPreferenceFragment extends SettingsPreferenceFragment { +public abstract class ProfileSettingsPreferenceFragment extends RestrictedDashboardFragment { + + public ProfileSettingsPreferenceFragment(String restrictionKey) { + super(restrictionKey); + } @Override public void onViewCreated(View view, Bundle savedInstanceState) { diff --git a/tests/unit/src/com/android/settings/print/PrintSettingsFragmentTest.java b/tests/unit/src/com/android/settings/print/PrintSettingsFragmentTest.java new file mode 100644 index 00000000000..c52c5bc7f28 --- /dev/null +++ b/tests/unit/src/com/android/settings/print/PrintSettingsFragmentTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.print; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +import android.content.Context; +import android.os.Looper; +import android.view.View; + +import androidx.preference.PreferenceManager; +import androidx.preference.PreferenceScreen; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +@RunWith(AndroidJUnit4.class) +public class PrintSettingsFragmentTest { + @Rule + public final MockitoRule mMockitoRule = MockitoJUnit.rule(); + @Spy + private final Context mContext = ApplicationProvider.getApplicationContext(); + + private PrintSettingsFragment mFragment; + private PreferenceManager mPreferenceManager; + private PreferenceScreen mPreferenceScreen; + + @Before + public void setUp() { + if (Looper.myLooper() == null) { + Looper.prepare(); + } + mPreferenceManager = new PreferenceManager(mContext); + mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext); + + InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { + mFragment = spy(new PrintSettingsFragment()); + doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen(); + }); + } + + @Test + public void setupPreferences_uiIsRestricted_doNotAddPreferences() { + mFragment.mIsUiRestricted = true; + + mFragment.setupPreferences(); + + verify(mFragment, never()).findPreference(any(CharSequence.class)); + } + + @Test + public void setupEmptyViews_uiIsRestricted_doNotSetEmptyView() { + mFragment.mIsUiRestricted = true; + + mFragment.setupEmptyViews(); + + verify(mFragment, never()).setEmptyView(any(View.class)); + } + + @Test + public void startSettings_uiIsRestricted_removeAllPreferences() { + mFragment.mIsUiRestricted = true; + + mFragment.startSettings(); + + assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0); + verify(mFragment, never()).setHasOptionsMenu(true); + } +}