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

Commit 66084a53 authored by Anton Philippov's avatar Anton Philippov
Browse files

Hide "Factory reset" item for secondary users

Bug: 35415714
Test: make RunSettingsRoboTests
Change-Id: I48d6d094476c5fc83dbc17906d84cc848e499624
parent 01b721ee
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.system;

import android.content.Context;

import android.os.UserManager;

import com.android.settings.R;
import com.android.settings.core.PreferenceController;

public class FactoryResetPreferenceController extends PreferenceController {
    /** Key of the "Factory reset" preference in {@link R.xml.system_dashboard_fragment}.*/
    private static final String KEY_FACTORY_RESET = "factory_reset";

    private final UserManager mUm;

    public FactoryResetPreferenceController(Context context, UserManager um) {
        super(context);
        mUm = um;
    }

    /** Hide "Factory reset" settings for secondary users. */
    @Override
    public boolean isAvailable() {
        return mUm.isAdminUser();
    }

    @Override
    public String getPreferenceKey() {
        return KEY_FACTORY_RESET;
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ public class SystemDashboardFragment extends DashboardFragment {
        final List<PreferenceController> controllers = new ArrayList<>();
        controllers.add(new SystemUpdatePreferenceController(context, UserManager.get(context)));
        controllers.add(new AdditionalSystemUpdatePreferenceController(context));
        controllers.add(new FactoryResetPreferenceController(context, UserManager.get(context)));
        return controllers;
    }

+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.system;

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

import static org.mockito.Answers.RETURNS_DEEP_STUBS;

import static org.mockito.Mockito.when;

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

import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;

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


@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class FactoryResetPreferenceControllerTest {

    private static final String FACTORY_RESET_KEY = "factory_reset";

    @Mock(answer = RETURNS_DEEP_STUBS)
    private Context mContext;
    @Mock
    private UserManager mUserManager;

    private FactoryResetPreferenceController mController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mController = new FactoryResetPreferenceController(mContext, mUserManager);
    }

    @Test
    public void isAvailable_systemUser() {
        when(mUserManager.isAdminUser()).thenReturn(true);

        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    public void isAvailable_nonSystemUser() {
        when(mUserManager.isAdminUser()).thenReturn(false);

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test public void getPreferenceKey() {
        assertThat(mController.getPreferenceKey()).isEqualTo(FACTORY_RESET_KEY);
    }
}