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