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.modules.updatablesharedlibs.apps.targetS;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import junit.framework.Assert;
24 
25 import com.google.common.truth.Correspondence;
26 
27 import android.content.Context;
28 import android.content.pm.ApplicationInfo;
29 import android.content.pm.PackageManager;
30 import androidx.test.platform.app.InstrumentationRegistry;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 /**
34  * Tests an app targeting S.
35  *
36  * <p>One of the shared libraries included in this test claims to be in the
37  * BOOTCLASSPATH before T so it should be added transparently to this app.
38  */
39 @RunWith(AndroidJUnit4.class)
40 public class UpdatableSharedLibraryTargetSTest {
41     private static String SHARED_LIB_PATH = "/apex/test_com.android.modules.updatablesharedlibs"
42             + "/javalib/com.android.modules.updatablesharedlibs.libs.before.t.jar";
43 
44     private static final Context sContext = InstrumentationRegistry.getInstrumentation()
45             .getContext();
46 
47     @Test
checkSharedLibrary()48     public void checkSharedLibrary() throws Exception {
49         String packageName = sContext.getPackageName();
50         ApplicationInfo appInfo = sContext.getPackageManager().getApplicationInfo(packageName,
51                 PackageManager.GET_SHARED_LIBRARY_FILES);
52 
53         assertThat(appInfo.sharedLibraryFiles)
54                 .asList()
55                 .contains(SHARED_LIB_PATH);
56     }
57 
58     @Test
callApi()59     public void callApi() throws Exception {
60         Object api = Class.forName("com.android.modules.updatablesharedlibs.libs.before.t.Api")
61                 .getDeclaredConstructor()
62                 .newInstance();
63 
64         String actual = (String) api.getClass().getDeclaredMethod("methodBeforeT").invoke(api);
65         assertThat(actual).isEqualTo("Success");
66     }
67 
68     @Test
testGetSystemSharedLibraryNames()69     public void testGetSystemSharedLibraryNames() {
70         String[] libraries = sContext.getPackageManager().getSystemSharedLibraryNames();
71         assertThat(libraries)
72                 .asList()
73                 .containsAtLeast(
74                     "com.android.modules.updatablesharedlibs.libs.before.t",
75                     "com.android.modules.updatablesharedlibs.libs.since.t");
76     }
77 }
78