1 /* 2 * Copyright (C) 2021 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 com.android.sharedlibloadingtest.client; 18 19 import static org.testng.Assert.assertEquals; 20 21 import android.content.Context; 22 import android.content.res.Resources; 23 24 import androidx.test.core.app.ApplicationProvider; 25 import androidx.test.ext.junit.runners.AndroidJUnit4; 26 27 import com.android.internal.util.Preconditions; 28 import com.android.sharedlibloadingtest.ClientClass; 29 import com.android.sharedlibloadingtest.DuplicateClassA; 30 import com.android.sharedlibloadingtest.DuplicateClassB; 31 import com.android.sharedlibloadingtest.SharedClassAfter; 32 import com.android.sharedlibloadingtest.StdSharedClass; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.util.Collections; 38 import java.util.HashSet; 39 40 @RunWith(AndroidJUnit4.class) 41 public class SharedLibraryLoadingOrderTest { 42 43 @Test testLoadingOfStdShareLibsShouldBeFirst()44 public void testLoadingOfStdShareLibsShouldBeFirst() { 45 Preconditions.checkArgument(!getLibsLoadedAfter() 46 .contains("com.android.sharedlibloadingtest.shared_library")); 47 DuplicateClassA clazz = new DuplicateClassA(); 48 assertEquals(clazz.toString(), "Standard Shared Lib's Version"); 49 50 StdSharedClass stdSharedClass = new StdSharedClass(); 51 assertEquals(stdSharedClass.toString(), "Nothing Special Lib"); 52 53 ClientClass clientCode = new ClientClass(); 54 assertEquals(clientCode.toString(), "Client Code"); 55 } 56 57 @Test testLoadingOfShareLibsIsAfter()58 public void testLoadingOfShareLibsIsAfter() { 59 Preconditions.checkArgument(getLibsLoadedAfter() 60 .contains("com.android.sharedlibloadingtest.shared_library_after")); 61 DuplicateClassB clazz = new DuplicateClassB(); 62 assertEquals(clazz.toString(), "Client's Version B"); 63 64 SharedClassAfter stdSharedClass = new SharedClassAfter(); 65 assertEquals(stdSharedClass.toString(), "Also Nothing Special"); 66 67 ClientClass clientCode = new ClientClass(); 68 assertEquals(clientCode.toString(), "Client Code"); 69 } 70 71 @Test testLoadingOfResource()72 public void testLoadingOfResource() { 73 // aapt compiler gives each lib their own namespace so this test just confirming 74 // the resources can be loaded from the same context object 75 Context context = ApplicationProvider.getApplicationContext(); 76 String clientString = context.getResources().getString(R.string.identical_resource_key); 77 assertEquals(clientString, "client value"); 78 assertEquals(StdSharedClass.getResString(context), "std lib value"); 79 assertEquals(SharedClassAfter.getResString(context), "loaded after value"); 80 81 } 82 getLibsLoadedAfter()83 private HashSet<String> getLibsLoadedAfter() { 84 Resources systemR = Resources.getSystem(); 85 HashSet<String> libsToLoadAfter = new HashSet<>(); 86 Collections.addAll(libsToLoadAfter, systemR.getStringArray( 87 com.android.internal.R.array.config_sharedLibrariesLoadedAfterApp)); 88 return libsToLoadAfter; 89 } 90 } 91