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

Commit f546be4d authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Move checking tile user logic into ProfileSelectDialog."

parents b8ce18a2 11b65072
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -26,10 +26,15 @@ import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import java.util.List;

public class ProfileSelectDialog extends DialogFragment implements OnClickListener {

    private static final String TAG = "ProfileSelectDialog";
    private static final String ARG_SELECTED_TILE = "selectedTile";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    private Tile mSelectedTile;

@@ -68,4 +73,20 @@ public class ProfileSelectDialog extends DialogFragment implements OnClickListen
        getActivity().startActivityAsUser(mSelectedTile.intent, user);
        ((SettingsDrawerActivity) getActivity()).onProfileTileOpen();
    }

    public static void updateUserHandlesIfNeeded(Context context, Tile tile) {
        List<UserHandle> userHandles = tile.userHandle;
        if (tile.userHandle == null || tile.userHandle.size() <= 1) {
            return;
        }
        final UserManager userManager = UserManager.get(context);
        for (int i = userHandles.size() - 1; i >= 0; i--) {
            if (userManager.getUserInfo(userHandles.get(i).getIdentifier()) == null) {
                if (DEBUG) {
                    Log.d(TAG, "Delete the user: " + userHandles.get(i).getIdentifier());
                }
                userHandles.remove(i);
            }
        }
    }
}
+1 −21
Original line number Diff line number Diff line
@@ -28,10 +28,8 @@ import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v4.widget.DrawerLayout;
import android.util.ArraySet;
import android.util.Log;
@@ -312,7 +310,7 @@ public class SettingsDrawerActivity extends Activity {
            return true;
        }
        try {
            updateUserHandlesIfNeeded(tile);
            ProfileSelectDialog.updateUserHandlesIfNeeded(this /* context */, tile);
            int numUserHandles = tile.userHandle.size();
            if (numUserHandles > 1) {
                ProfileSelectDialog.show(getFragmentManager(), tile);
@@ -334,24 +332,6 @@ public class SettingsDrawerActivity extends Activity {
        return true;
    }

    private void updateUserHandlesIfNeeded(Tile tile) {
        List<UserHandle> userHandles = tile.userHandle;

        for (int i = userHandles.size() - 1; i >= 0; i--) {
            if (mUserManager.getUserInfo(userHandles.get(i).getIdentifier()) == null) {
                if (DEBUG) {
                    Log.d(TAG, "Delete the user: " + userHandles.get(i).getIdentifier());
                }
                userHandles.remove(i);
            }
        }
    }

    @VisibleForTesting
    public void setUserManager(UserManager userManager) {
        mUserManager = userManager;
    }

    protected void onTileClicked(Tile tile) {
        if (openTile(tile)) {
            finish();
+87 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.settingslib.drawer;

import android.content.Context;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

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

import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class ProfileSelectDialogTest {

    @Mock
    private Context mContext;
    @Mock
    private UserManager mUserManager;
    private static final UserHandle NORMAL_USER = UserHandle.of(1111);
    private static final UserHandle REMOVED_USER = UserHandle.of(2222);

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
        final UserInfo userInfo = new UserInfo(
                NORMAL_USER.getIdentifier(), "test_user", UserInfo.FLAG_RESTRICTED);
        when(mUserManager.getUserInfo(NORMAL_USER.getIdentifier())).thenReturn(userInfo);
    }

    @Test
    public void testUpdateUserHandlesIfNeeded_Normal() {
        final Tile tile = new Tile();
        tile.intent = new Intent();
        tile.userHandle.add(NORMAL_USER);

        ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);

        assertEquals(tile.userHandle.size(), 1);
        assertEquals(tile.userHandle.get(0).getIdentifier(), NORMAL_USER.getIdentifier());
        verify(mUserManager, never()).getUserInfo(NORMAL_USER.getIdentifier());
    }

    @Test
    public void testUpdateUserHandlesIfNeeded_Remove() {
        final Tile tile = new Tile();
        tile.intent = new Intent();
        tile.userHandle.add(REMOVED_USER);
        tile.userHandle.add(NORMAL_USER);
        tile.userHandle.add(REMOVED_USER);

        ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);

        assertEquals(tile.userHandle.size(), 1);
        assertEquals(tile.userHandle.get(0).getIdentifier(), NORMAL_USER.getIdentifier());
        verify(mUserManager, times(1)).getUserInfo(NORMAL_USER.getIdentifier());
        verify(mUserManager, times(2)).getUserInfo(REMOVED_USER.getIdentifier());
    }
}
+1 −57
Original line number Diff line number Diff line
@@ -19,25 +19,16 @@ package com.android.settingslib.drawer;
import android.app.Instrumentation;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.annotation.Nullable;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.test.filters.SmallTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.android.settingslib.drawer.SettingsDrawerActivity;
import com.android.settingslib.drawer.Tile;
import com.android.settingslib.R;

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

import static android.support.test.espresso.Espresso.onView;
@@ -45,66 +36,19 @@ import static android.support.test.espresso.assertion.ViewAssertions.doesNotExis
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class SettingsDrawerActivityTest {
    @Mock
    private UserManager mUserManager;


    @Rule
    public ActivityTestRule<TestActivity> mActivityRule =
            new ActivityTestRule<>(TestActivity.class, true, true);

    private static final UserHandle NORMAL_USER = UserHandle.of(1111);
    private static final UserHandle REMOVED_USER = UserHandle.of(2222);

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        final UserInfo userInfo = new UserInfo(
                NORMAL_USER.getIdentifier(), "test_user", UserInfo.FLAG_RESTRICTED);
        when(mUserManager.getUserInfo(NORMAL_USER.getIdentifier())).thenReturn(userInfo);
    }

    @Test
    public void testUpdateUserHandlesIfNeeded_Normal() {
        TestActivity activity = mActivityRule.getActivity();
        activity.setUserManager(mUserManager);

        Tile tile = new Tile();
        tile.intent = new Intent();
        tile.userHandle.add(NORMAL_USER);

        activity.openTile(tile);

        assertEquals(tile.userHandle.size(), 1);
        assertEquals(tile.userHandle.get(0).getIdentifier(), NORMAL_USER.getIdentifier());
        verify(mUserManager, times(1)).getUserInfo(NORMAL_USER.getIdentifier());
    }

    @Test
    public void testUpdateUserHandlesIfNeeded_Remove() {
        TestActivity activity = mActivityRule.getActivity();
        activity.setUserManager(mUserManager);

        Tile tile = new Tile();
        tile.intent = new Intent();
        tile.userHandle.add(REMOVED_USER);
        tile.userHandle.add(NORMAL_USER);
        tile.userHandle.add(REMOVED_USER);

        activity.openTile(tile);

        assertEquals(tile.userHandle.size(), 1);
        assertEquals(tile.userHandle.get(0).getIdentifier(), NORMAL_USER.getIdentifier());
        verify(mUserManager, times(1)).getUserInfo(NORMAL_USER.getIdentifier());
        verify(mUserManager, times(2)).getUserInfo(REMOVED_USER.getIdentifier());
    }

    @Test