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

Commit 57f096b1 authored by Lei Yu's avatar Lei Yu Committed by Android (Google) Code Review
Browse files

Merge "Add userhandle testcases in SettingsDrawerActivity."

parents 3c6e418e 8accac9e
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ 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;
@@ -298,6 +299,11 @@ public class SettingsDrawerActivity extends Activity {
        }
    }

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

    protected void onTileClicked(Tile tile) {
        if (openTile(tile)) {
            finish();
+3 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := tests
LOCAL_CERTIFICATE := platform

LOCAL_SRC_FILES := $(call all-java-files-under, src)

@@ -23,7 +24,8 @@ LOCAL_JAVA_LIBRARIES := android.test.runner telephony-common

LOCAL_PACKAGE_NAME := SettingsLibTests

LOCAL_STATIC_JAVA_LIBRARIES := mockito-target
LOCAL_STATIC_JAVA_LIBRARIES := mockito-target \
    android-support-test

include frameworks/base/packages/SettingsLib/common.mk

+5 −1
Original line number Diff line number Diff line
@@ -17,11 +17,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.settingslib">

    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
    <uses-permission android:name="android.permission.MANAGE_USERS" />

    <application>
        <uses-library android:name="android.test.runner" />
        <activity android:name=".drawer.SettingsDrawerActivityTest$TestActivity"/>
    </application>

    <instrumentation android:name="android.test.InstrumentationTestRunner"
    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
        android:targetPackage="com.android.settingslib"
        android:label="Tests for SettingsLib">
    </instrumentation>
+107 −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 static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

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 org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@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 Activity in this test.
     *
     * Use this activity because SettingsDrawerActivity hasn't been registered in its
     * AndroidManifest.xml
     */
    public static class TestActivity extends SettingsDrawerActivity {}

}