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.content.wm.cts; 18 19 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY; 20 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC; 21 import static android.view.Display.DEFAULT_DISPLAY; 22 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; 23 24 import static org.junit.Assert.fail; 25 26 import android.app.Activity; 27 import android.app.Service; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.cts.ContextTest; 31 import android.content.cts.MockActivity; 32 import android.content.cts.MockService; 33 import android.graphics.PixelFormat; 34 import android.hardware.display.DisplayManager; 35 import android.hardware.display.VirtualDisplay; 36 import android.media.ImageReader; 37 import android.os.Binder; 38 import android.os.IBinder; 39 import android.view.Display; 40 import android.window.WindowProviderService; 41 42 import androidx.test.core.app.ApplicationProvider; 43 import androidx.test.rule.ActivityTestRule; 44 import androidx.test.rule.ServiceTestRule; 45 46 import org.junit.Before; 47 import org.junit.Rule; 48 49 import java.util.concurrent.TimeoutException; 50 51 /** 52 * Used for providing various kinds of contexts. This test base often used for verifying APIs 53 * which have different behaviors on different kinds of {@link Context}s 54 */ 55 public class ContextTestBase { 56 public Context mApplicationContext = ApplicationProvider.getApplicationContext(); 57 private Display mDefaultDisplay; 58 private Display mSecondaryDisplay; 59 60 @Rule 61 public final ActivityTestRule<MockActivity> mActivityRule = 62 new ActivityTestRule<>(MockActivity.class); 63 64 @Before setUp()65 public final void setUp() { 66 final DisplayManager dm = mApplicationContext.getSystemService(DisplayManager.class); 67 mDefaultDisplay = dm.getDisplay(DEFAULT_DISPLAY); 68 mSecondaryDisplay = createSecondaryDisplay(); 69 } 70 createSecondaryDisplay()71 private Display createSecondaryDisplay() { 72 final DisplayManager displayManager = mApplicationContext 73 .getSystemService(DisplayManager.class); 74 final int width = 800; 75 final int height = 480; 76 final int density = 160; 77 ImageReader reader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 78 2 /* maxImages */); 79 VirtualDisplay virtualDisplay = displayManager.createVirtualDisplay( 80 ContextTest.class.getName(), width, height, density, reader.getSurface(), 81 VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY); 82 return virtualDisplay.getDisplay(); 83 } 84 getDefaultDisplay()85 public Display getDefaultDisplay() { 86 return mDefaultDisplay; 87 } 88 getSecondaryDisplay()89 public Display getSecondaryDisplay() { 90 return mSecondaryDisplay; 91 } 92 createWindowContext()93 public Context createWindowContext() { 94 return mApplicationContext.createDisplayContext(mDefaultDisplay).createWindowContext( 95 TYPE_APPLICATION_OVERLAY, null /* options */); 96 } 97 getTestActivity()98 public Activity getTestActivity() throws Throwable { 99 MockActivity[] activity = new MockActivity[1]; 100 mActivityRule.runOnUiThread(() -> activity[0] = mActivityRule.getActivity()); 101 return activity[0]; 102 } 103 createTestService()104 public Service createTestService() throws TimeoutException { 105 final Intent intent = new Intent(mApplicationContext, MockService.class); 106 final ServiceTestRule serviceRule = new ServiceTestRule(); 107 IBinder serviceToken; 108 serviceToken = serviceRule.bindService(intent); 109 return ((MockService.MockBinder) serviceToken).getService(); 110 } 111 createWindowTestService()112 public Service createWindowTestService() { 113 final Intent intent = new Intent(mApplicationContext, TestWindowService.class); 114 final ServiceTestRule serviceRule = new ServiceTestRule(); 115 try { 116 IBinder serviceToken = serviceRule.bindService(intent); 117 return ((TestWindowService.TestToken) serviceToken).getService(); 118 } catch (TimeoutException e) { 119 fail("Test fail because of " + e); 120 return null; 121 } 122 } 123 124 public static class TestWindowService extends WindowProviderService { 125 private final IBinder mToken = new TestToken(); 126 127 @Override onBind(Intent intent)128 public IBinder onBind(Intent intent) { 129 return mToken; 130 } 131 132 @Override getWindowType()133 public int getWindowType() { 134 return TYPE_APPLICATION_OVERLAY; 135 } 136 137 public class TestToken extends Binder { getService()138 private TestWindowService getService() { 139 return TestWindowService.this; 140 } 141 } 142 } 143 } 144