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 package com.android.cts.overlay.app; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertTrue; 20 21 import android.content.Context; 22 import android.content.res.AssetFileDescriptor; 23 24 import androidx.test.InstrumentationRegistry; 25 import androidx.test.runner.AndroidJUnit4; 26 27 import java.io.InputStreamReader; 28 import java.util.concurrent.Executor; 29 import java.util.concurrent.FutureTask; 30 import java.util.concurrent.TimeUnit; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @RunWith(AndroidJUnit4.class) 37 public class OverlayableTest { 38 private static final String TARGET_PACKAGE = "com.android.cts.overlay.target"; 39 40 private static final String POLICY_ALL_PACKAGE = "com.android.cts.overlay.all"; 41 42 private static final String OVERLAID = "You have been overlaid"; 43 private static final String NOT_OVERLAID = "Not overlaid"; 44 45 private static final long TIMEOUT = 30; 46 47 private static class R { 48 class string { 49 private static final int not_overlayable = 0x7f010000; 50 private static final int policy_product = 0x7f010001; 51 private static final int policy_public = 0x7f010002; 52 private static final int policy_system = 0x7f010003; 53 private static final int policy_vendor = 0x7f010004; 54 private static final int policy_signature = 0x7f010005; 55 private static final int other_policy_public = 0x7f010006; 56 } 57 } 58 59 private Executor mExecutor; 60 61 @Before setUp()62 public void setUp() throws Exception { 63 mExecutor = (command) -> new Thread(command).start(); 64 } 65 getTargetContext()66 private Context getTargetContext() throws Exception { 67 return InstrumentationRegistry.getTargetContext().createPackageContext(TARGET_PACKAGE, 0); 68 } 69 assertOverlayEnabled(Context context, String overlayPackage)70 private void assertOverlayEnabled(Context context, String overlayPackage) throws Exception { 71 // Wait for the overlay changes to propagate 72 FutureTask<Boolean> task = new FutureTask<>(() -> { 73 while (true) { 74 for (String path : context.getAssets().getApkPaths()) { 75 if (path.contains(overlayPackage)) { 76 return true; 77 } 78 } 79 } 80 }); 81 82 mExecutor.execute(task); 83 assertTrue("Failed to load overlay " + overlayPackage, task.get(TIMEOUT, TimeUnit.SECONDS)); 84 } 85 86 @Test testOverlayPolicyAll()87 public void testOverlayPolicyAll() throws Exception { 88 Context context = getTargetContext(); 89 assertOverlayEnabled(context, POLICY_ALL_PACKAGE); 90 91 String result = context.getResources().getString(R.string.not_overlayable); 92 assertEquals(NOT_OVERLAID, result); 93 94 result = context.getResources().getString(R.string.policy_public); 95 assertEquals(OVERLAID, result); 96 97 result = context.getResources().getString(R.string.policy_product); 98 assertEquals(NOT_OVERLAID, result); 99 100 result = context.getResources().getString(R.string.policy_system); 101 assertEquals(NOT_OVERLAID, result); 102 103 result = context.getResources().getString(R.string.policy_vendor); 104 assertEquals(NOT_OVERLAID, result); 105 106 result = context.getResources().getString(R.string.policy_signature); 107 assertEquals(OVERLAID, result); 108 109 result = context.getResources().getString(R.string.other_policy_public); 110 assertEquals(NOT_OVERLAID, result); 111 } 112 113 @Test testSameSignatureNoOverlayableSucceeds()114 public void testSameSignatureNoOverlayableSucceeds() throws Exception { 115 Context context = getTargetContext(); 116 assertOverlayEnabled(context, POLICY_ALL_PACKAGE); 117 118 String result = context.getResources().getString(R.string.not_overlayable); 119 assertEquals(OVERLAID, result); 120 121 result = context.getResources().getString(R.string.policy_public); 122 assertEquals(OVERLAID, result); 123 124 result = context.getResources().getString(R.string.policy_product); 125 assertEquals(OVERLAID, result); 126 127 result = context.getResources().getString(R.string.policy_system); 128 assertEquals(OVERLAID, result); 129 130 result = context.getResources().getString(R.string.policy_vendor); 131 assertEquals(OVERLAID, result); 132 133 result = context.getResources().getString(R.string.policy_signature); 134 assertEquals(OVERLAID, result); 135 136 result = context.getResources().getString(R.string.other_policy_public); 137 assertEquals(OVERLAID, result); 138 } 139 140 @Test testFrameworkDoesNotDefineOverlayable()141 public void testFrameworkDoesNotDefineOverlayable() throws Exception { 142 assertTrue(InstrumentationRegistry.getTargetContext().getAssets().getOverlayableMap( 143 "android").isEmpty()); 144 } 145 146 @Test testCannotOverlayAssets()147 public void testCannotOverlayAssets() throws Exception { 148 Context context = getTargetContext(); 149 AssetFileDescriptor d = context.getAssets().openNonAssetFd("assets/asset.txt"); 150 InputStreamReader inputStreamReader = new InputStreamReader(d.createInputStream()); 151 152 StringBuilder output = new StringBuilder(); 153 for (int c = inputStreamReader.read(); c >= 0; c = inputStreamReader.read()) { 154 output.append((char) c); 155 } 156 157 assertEquals(NOT_OVERLAID, output.toString()); 158 } 159 }