1 /* 2 * Copyright (C) 2020 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 17 package android.server.wm; 18 19 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; 20 21 import static org.junit.Assert.assertFalse; 22 import static org.junit.Assert.assertTrue; 23 24 import android.content.Context; 25 import android.os.UserHandle; 26 import android.platform.test.annotations.Presubmit; 27 import android.view.View; 28 import android.view.WindowManager; 29 30 import org.junit.Test; 31 32 @Presubmit 33 public class AddWindowAsUserTest extends ActivityManagerTestBase { 34 @Test testAddWindowSecondaryUser()35 public void testAddWindowSecondaryUser() { 36 // Get original userId from context first, not every platform use SYSTEM as default user. 37 final int myUserId = mContext.getUserId(); 38 testAddWindowWithUser(UserHandle.of(myUserId), false /* shouldCatchException */); 39 40 // Doesn't grant INTERACT_ACROSS_USERS_FULL permission, so any other user should not 41 // able to add window. 42 testAddWindowWithUser(UserHandle.ALL, true); 43 } 44 testAddWindowWithUser(UserHandle user, boolean shouldCatchException)45 private void testAddWindowWithUser(UserHandle user, boolean shouldCatchException) { 46 mInstrumentation.runOnMainSync(() -> { 47 final View view = new View(mContext); 48 final WindowManager wm = getWindowManagerForUser(user); 49 boolean catchException = false; 50 try { 51 wm.addView(view, new WindowManager.LayoutParams(TYPE_APPLICATION_OVERLAY)); 52 } catch (WindowManager.BadTokenException exception) { 53 catchException = true; 54 } finally { 55 if (!catchException) { 56 wm.removeViewImmediate(view); 57 } 58 } 59 if (shouldCatchException) { 60 assertTrue("Should receive exception for user " + user, catchException); 61 } else { 62 assertFalse("Shouldn't receive exception for user " + user, catchException); 63 } 64 }); 65 } 66 getWindowManagerForUser(UserHandle user)67 private WindowManager getWindowManagerForUser(UserHandle user) { 68 final Context userContext = mContext.createContextAsUser(user, 0); 69 return userContext.getSystemService(WindowManager.class); 70 } 71 } 72