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

Commit bddf5149 authored by Sunny Shao's avatar Sunny Shao
Browse files

Fix the crash while tapping "app data usage" as second user

Second user or guest is limited some mobile network access. We need to check current user
is admin or not to decide the preferece page will be found in search or not.

Bug: 133466016
Fixes: 133466016
Test: Manual test & make RunSettingsRoboTests -j56 ROBOTEST_FILTER=com.android.settings.network

Change-Id: I48d3064a8aa28ac1f2ac699b42a999b9682b1b52
parent f010cb40
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.network;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.UserManager;
import android.provider.SearchIndexableResource;

import com.android.settings.R;
@@ -66,5 +67,10 @@ public class MobileNetworkListFragment extends DashboardFragment {
                    result.add(sir);
                    return result;
                }

                @Override
                protected boolean isPageSearchEnabled(Context context) {
                    return context.getSystemService(UserManager.class).isAdminUser();
                }
            };
}
+6 −0
Original line number Diff line number Diff line
@@ -277,5 +277,11 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment {
                    result.add(sir);
                    return result;
                }

                /** suppress full page if user is not admin */
                @Override
                protected boolean isPageSearchEnabled(Context context) {
                    return context.getSystemService(UserManager.class).isAdminUser();
                }
            };
}
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.network;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.when;

import android.content.Context;
import android.os.UserManager;

import com.android.settings.search.BaseSearchIndexProvider;

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;
import org.robolectric.util.ReflectionHelpers;


@RunWith(RobolectricTestRunner.class)
public class MobileNetworkListFragmentTest {
    @Mock
    private Context mContext;
    @Mock
    private UserManager mUserManager;

    private MobileNetworkListFragment mFragment;

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

    @Test
    public void isPageSearchEnabled_adminUser_shouldReturnTrue() {
        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
        when(mUserManager.isAdminUser()).thenReturn(true);
        final BaseSearchIndexProvider provider =
                (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;

        final Object obj = ReflectionHelpers.callInstanceMethod(provider, "isPageSearchEnabled",
                ReflectionHelpers.ClassParameter.from(Context.class, mContext));
        final boolean isEnabled = (Boolean) obj;

        assertThat(isEnabled).isTrue();
    }

    @Test
    public void isPageSearchEnabled_nonAdminUser_shouldReturnFalse() {
        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
        when(mUserManager.isAdminUser()).thenReturn(false);
        final BaseSearchIndexProvider provider =
                (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;

        final Object obj = ReflectionHelpers.callInstanceMethod(provider, "isPageSearchEnabled",
                ReflectionHelpers.ClassParameter.from(Context.class, mContext));
        final boolean isEnabled = (Boolean) obj;

        assertThat(isEnabled).isFalse();
    }
}
+33 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.app.usage.NetworkStatsManager;
import android.content.Context;
import android.net.NetworkPolicyManager;
import android.os.Bundle;
import android.os.UserManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

@@ -38,6 +39,7 @@ import androidx.fragment.app.FragmentActivity;
import com.android.settings.core.FeatureFlags;
import com.android.settings.datausage.DataUsageSummaryPreferenceController;
import com.android.settings.development.featureflags.FeatureFlagPersistent;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -50,6 +52,7 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;

import java.util.List;

@@ -135,4 +138,34 @@ public class MobileNetworkSettingsTest {
        mFragment.onActivityResult(REQUEST_CODE_DELETE_SUBSCRIPTION, 0, null);
        verify(mActivity).finish();
    }

    @Test
    public void isPageSearchEnabled_adminUser_shouldReturnTrue() {
        final UserManager userManager = mock(UserManager.class);
        when(mContext.getSystemService(UserManager.class)).thenReturn(userManager);
        when(userManager.isAdminUser()).thenReturn(true);
        final BaseSearchIndexProvider provider =
                (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;

        final Object obj = ReflectionHelpers.callInstanceMethod(provider, "isPageSearchEnabled",
                ReflectionHelpers.ClassParameter.from(Context.class, mContext));
        final boolean isEnabled = (Boolean) obj;

        assertThat(isEnabled).isTrue();
    }

    @Test
    public void isPageSearchEnabled_nonAdminUser_shouldReturnFalse() {
        final UserManager userManager = mock(UserManager.class);
        when(mContext.getSystemService(UserManager.class)).thenReturn(userManager);
        when(userManager.isAdminUser()).thenReturn(false);
        final BaseSearchIndexProvider provider =
                (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;

        final Object obj = ReflectionHelpers.callInstanceMethod(provider, "isPageSearchEnabled",
                ReflectionHelpers.ClassParameter.from(Context.class, mContext));
        final boolean isEnabled = (Boolean) obj;

        assertThat(isEnabled).isFalse();
    }
}