1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.cts.crossprofileappstest; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static junit.framework.Assert.assertNotNull; 21 22 import static org.junit.Assert.assertEquals; 23 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.pm.CrossProfileApps; 27 import android.os.Bundle; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.support.test.InstrumentationRegistry; 31 import android.support.test.runner.AndroidJUnit4; 32 import android.support.test.uiautomator.By; 33 import android.support.test.uiautomator.UiDevice; 34 import android.support.test.uiautomator.UiObject2; 35 import android.support.test.uiautomator.Until; 36 37 import org.junit.After; 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.List; 43 import java.util.concurrent.TimeUnit; 44 45 /** 46 * Test that runs {@link CrossProfileApps} APIs against valid target user. 47 */ 48 @RunWith(AndroidJUnit4.class) 49 public class CrossProfileAppsTargetUserTest { 50 private static final String PARAM_TARGET_USER = "TARGET_USER"; 51 private static final String ID_USER_TEXTVIEW = 52 "com.android.cts.crossprofileappstest:id/user_textview"; 53 private static final long TIMEOUT_WAIT_UI = TimeUnit.SECONDS.toMillis(10); 54 55 private CrossProfileApps mCrossProfileApps; 56 private UserHandle mTargetUser; 57 private Context mContext; 58 private UiDevice mDevice; 59 private long mUserSerialNumber; 60 61 @Before setupCrossProfileApps()62 public void setupCrossProfileApps() { 63 mContext = InstrumentationRegistry.getContext(); 64 mCrossProfileApps = mContext.getSystemService(CrossProfileApps.class); 65 } 66 67 @Before wakeupDeviceAndPressHome()68 public void wakeupDeviceAndPressHome() throws Exception { 69 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 70 mDevice.wakeUp(); 71 mDevice.pressMenu(); 72 mDevice.pressHome(); 73 } 74 75 @Before readTargetUser()76 public void readTargetUser() { 77 Context context = InstrumentationRegistry.getContext(); 78 Bundle arguments = InstrumentationRegistry.getArguments(); 79 UserManager userManager = context.getSystemService(UserManager.class); 80 mUserSerialNumber = Long.parseLong(arguments.getString(PARAM_TARGET_USER)); 81 mTargetUser = userManager.getUserForSerialNumber(mUserSerialNumber); 82 assertNotNull(mTargetUser); 83 } 84 85 @After pressHome()86 public void pressHome() { 87 mDevice.pressHome(); 88 } 89 90 @Test testTargetUserIsIngetTargetUserProfiles()91 public void testTargetUserIsIngetTargetUserProfiles() { 92 List<UserHandle> targetProfiles = mCrossProfileApps.getTargetUserProfiles(); 93 assertThat(targetProfiles).contains(mTargetUser); 94 } 95 96 /** 97 * Verify we succeed to start the activity in another profile by checking UI element. 98 */ 99 @Test testCanStartMainActivity()100 public void testCanStartMainActivity() throws Exception { 101 mCrossProfileApps.startMainActivity( 102 MainActivity.getComponentName(mContext), mTargetUser); 103 104 // Look for the text view to verify that MainActivity is started. 105 UiObject2 textView = mDevice.wait( 106 Until.findObject(By.res(ID_USER_TEXTVIEW)), 107 TIMEOUT_WAIT_UI); 108 assertNotNull("Failed to start activity in target user", textView); 109 // Look for the text in textview, it should be the serial number of target user. 110 assertEquals("Activity is started in wrong user", 111 String.valueOf(mUserSerialNumber), 112 textView.getText()); 113 } 114 115 @Test(expected = SecurityException.class) testCannotStartNotExportedActivity()116 public void testCannotStartNotExportedActivity() throws Exception { 117 mCrossProfileApps.startMainActivity( 118 NonExportedActivity.getComponentName(mContext), mTargetUser); 119 } 120 121 @Test(expected = SecurityException.class) testCannotStartNonMainActivity()122 public void testCannotStartNonMainActivity() throws Exception { 123 mCrossProfileApps.startMainActivity( 124 NonMainActivity.getComponentName(mContext), mTargetUser); 125 } 126 127 @Test(expected = SecurityException.class) testCannotStartActivityInOtherPackage()128 public void testCannotStartActivityInOtherPackage() throws Exception { 129 mCrossProfileApps.startMainActivity(new ComponentName( 130 "com.android.cts.launcherapps.simpleapp", 131 "com.android.cts.launcherapps.simpleapp.SimpleActivity"), 132 mTargetUser 133 ); 134 } 135 136 @Test testGetProfileSwitchingLabel()137 public void testGetProfileSwitchingLabel() throws Exception { 138 assertNotNull(mCrossProfileApps.getProfileSwitchingLabel(mTargetUser)); 139 } 140 141 @Test testGetProfileSwitchingIconDrawable()142 public void testGetProfileSwitchingIconDrawable() throws Exception { 143 assertNotNull(mCrossProfileApps.getProfileSwitchingIconDrawable(mTargetUser)); 144 } 145 } 146 147