1 /* 2 * Copyright (C) 2019 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.os.cts; 18 19 import static org.junit.Assert.assertTrue; 20 import static org.junit.Assert.fail; 21 22 import android.app.Instrumentation; 23 import android.content.ActivityNotFoundException; 24 import android.content.pm.PackageManager; 25 import android.net.Uri; 26 import android.os.image.DynamicSystemClient; 27 import android.platform.test.annotations.AppModeFull; 28 import android.platform.test.annotations.AppModeNonSdkSandbox; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import org.junit.After; 35 import org.junit.Assume; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 @AppModeFull(reason = "Instant apps cannot access DynamicSystemClient") 41 @AppModeNonSdkSandbox(reason = "SDK Sandboxes cannot access DynamicSystemClient") 42 @SmallTest 43 @RunWith(AndroidJUnit4.class) 44 public class DynamicSystemClientTest implements DynamicSystemClient.OnStatusChangedListener { 45 private boolean mUpdated; 46 private Instrumentation mInstrumentation; 47 48 private static final String DSU_PACKAGE_NAME = "com.android.dynsystem"; 49 getPackageManager()50 private PackageManager getPackageManager() { 51 return mInstrumentation.getContext().getPackageManager(); 52 } 53 onStatusChanged(int status, int cause, long progress, Throwable detail)54 public void onStatusChanged(int status, int cause, long progress, Throwable detail) { 55 mUpdated = true; 56 } 57 58 @Before setUp()59 public void setUp() { 60 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 61 mInstrumentation.getUiAutomation().adoptShellPermissionIdentity(); 62 } 63 64 @Test testDynamicSystemClient()65 public void testDynamicSystemClient() { 66 DynamicSystemClient dSClient = new DynamicSystemClient(mInstrumentation.getTargetContext()); 67 dSClient.setOnStatusChangedListener(this); 68 try { 69 dSClient.bind(); 70 } catch (SecurityException e) { 71 fail(); 72 } 73 Uri uri = Uri.parse("https://www.google.com/").buildUpon().build(); 74 mUpdated = false; 75 try { 76 dSClient.start(uri, 1024L << 10); 77 } catch (ActivityNotFoundException e) { 78 try { 79 getPackageManager().getPackageInfo(DSU_PACKAGE_NAME, 0); 80 } catch (PackageManager.NameNotFoundException ignore) { 81 Assume.assumeNoException(ignore); 82 } 83 throw e; 84 } catch (SecurityException e) { 85 fail(); 86 } 87 try { 88 Thread.sleep(3 * 1000); 89 } catch (InterruptedException e) { 90 fail(); 91 } 92 assertTrue(mUpdated); 93 try { 94 dSClient.unbind(); 95 } catch (SecurityException e) { 96 fail(); 97 } 98 } 99 100 @Test testDynamicSystemClient_withoutOnStatusChangedListener()101 public void testDynamicSystemClient_withoutOnStatusChangedListener() { 102 DynamicSystemClient dSClient = new DynamicSystemClient(mInstrumentation.getTargetContext()); 103 try { 104 dSClient.bind(); 105 } catch (SecurityException e) { 106 fail(); 107 } 108 Uri uri = Uri.parse("https://www.google.com/").buildUpon().build(); 109 try { 110 dSClient.start(uri, 1024L << 10); 111 } catch (ActivityNotFoundException e) { 112 try { 113 getPackageManager().getPackageInfo(DSU_PACKAGE_NAME, 0); 114 } catch (PackageManager.NameNotFoundException ignore) { 115 Assume.assumeNoException(ignore); 116 } 117 throw e; 118 } catch (SecurityException e) { 119 fail(); 120 } 121 try { 122 Thread.sleep(3 * 1000); 123 } catch (InterruptedException e) { 124 fail(); 125 } 126 try { 127 dSClient.unbind(); 128 } catch (SecurityException e) { 129 fail(); 130 } 131 } 132 133 @After tearDown()134 public void tearDown() { 135 mInstrumentation.getUiAutomation().dropShellPermissionIdentity(); 136 } 137 } 138