Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1f24c6a9 authored by Weng Su's avatar Weng Su
Browse files

[DO NOT MERGE] Modify Wi-Fi details settings to restricted style

- Show restricted text in guest mode

- Screenshot:
  https://screenshot.googleplex.com/6nYcmazMM46TxaB

Bug: 177573895
Test: manual test
make RunSettingsRoboTests \
       ROBOTEST_FILTER=WifiNetworkDetailsFragmentTest

Change-Id: I5f857b2079e0f550e4be601d27dd54dac56b2f57
Merged-In: I5f857b2079e0f550e4be601d27dd54dac56b2f57
parent 4d7853f3
Loading
Loading
Loading
Loading
+40 −5
Original line number Diff line number Diff line
@@ -25,12 +25,15 @@ import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.UserManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.VisibleForTesting;

import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.wifi.WifiConfigUiBase;
import com.android.settings.wifi.WifiDialog;
import com.android.settingslib.RestrictedLockUtils;
@@ -47,15 +50,45 @@ import java.util.List;
 * <p>The AccessPoint should be saved to the intent Extras when launching this class via
 * {@link AccessPoint#saveWifiState(Bundle)} in order to properly render this page.
 */
public class WifiNetworkDetailsFragment extends DashboardFragment implements
public class WifiNetworkDetailsFragment extends RestrictedDashboardFragment implements
        WifiDialog.WifiDialogListener {

    private static final String TAG = "WifiNetworkDetailsFrg";

    @VisibleForTesting
    boolean mIsUiRestricted;

    private AccessPoint mAccessPoint;
    private WifiDetailPreferenceController mWifiDetailPreferenceController;
    private List<WifiDialog.WifiDialogListener> mWifiDialogListeners = new ArrayList<>();

    public WifiNetworkDetailsFragment() {
        super(UserManager.DISALLOW_CONFIG_WIFI);
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setIfOnlyAvailableForAdmins(true);
        mIsUiRestricted = isUiRestricted();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mIsUiRestricted) {
            restrictUi();
        }
    }

    @VisibleForTesting
    void restrictUi() {
        if (!isUiRestrictedByOnlyAdmin()) {
            getEmptyTextView().setText(R.string.wifi_empty_list_user_restricted);
        }
        getPreferenceScreen().removeAll();
    }

    @Override
    public void onAttach(Context context) {
        mAccessPoint = new AccessPoint(context, getArguments());
@@ -98,9 +131,11 @@ public class WifiNetworkDetailsFragment extends DashboardFragment implements

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if (!mIsUiRestricted) {
            MenuItem item = menu.add(0, Menu.FIRST, 0, R.string.wifi_modify);
            item.setIcon(com.android.internal.R.drawable.ic_mode_edit);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }
        super.onCreateOptionsMenu(menu, inflater);
    }

+99 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.wifi.details;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.view.Menu;
import android.view.MenuInflater;
import android.widget.TextView;

import androidx.preference.PreferenceScreen;

import com.android.settings.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class WifiNetworkDetailsFragmentTest {

    @Mock
    Menu mMenu;
    private WifiNetworkDetailsFragment mFragment;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mFragment = new WifiNetworkDetailsFragment();
    }

    @Test
    public void onCreateOptionsMenu_uiRestricted_shouldNotAddEditMenu() {
        mFragment.mIsUiRestricted = true;

        mFragment.onCreateOptionsMenu(mMenu, mock(MenuInflater.class));

        verify(mMenu, never()).add(anyInt(), anyInt(), anyInt(), eq(R.string.wifi_modify));
    }

    @Test
    public void restrictUi_shouldShowRestrictedText() {
        final WifiNetworkDetailsFragmentTest.FakeFragment
                fragment = spy(new WifiNetworkDetailsFragmentTest.FakeFragment());
        final PreferenceScreen screen = mock(PreferenceScreen.class);
        final TextView restrictedText = mock(TextView.class);
        doReturn(screen).when(fragment).getPreferenceScreen();
        doReturn(false).when(fragment).isUiRestrictedByOnlyAdmin();
        doReturn(restrictedText).when(fragment).getEmptyTextView();

        fragment.restrictUi();

        verify(restrictedText).setText(anyInt());
    }

    @Test
    public void restrictUi_shouldRemoveAllPreferences() {
        final WifiNetworkDetailsFragmentTest.FakeFragment
                fragment = spy(new WifiNetworkDetailsFragmentTest.FakeFragment());
        final PreferenceScreen screen = mock(PreferenceScreen.class);
        doReturn(screen).when(fragment).getPreferenceScreen();
        doReturn(true).when(fragment).isUiRestrictedByOnlyAdmin();

        fragment.restrictUi();

        verify(screen).removeAll();
    }

    // Fake WifiNetworkDetailsFragment to override the protected method as public.
    public class FakeFragment extends WifiNetworkDetailsFragment {
        @Override
        public boolean isUiRestrictedByOnlyAdmin() {
            return super.isUiRestrictedByOnlyAdmin();
        }
    }
}
 No newline at end of file