xref: /aosp_15_r20/art/test/817-hiddenapi/src-ex/TestCase.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 public class TestCase {
18 
test()19   public static void test() {
20     testPublicSdk();
21     testUnsupportedAppUsage();
22   }
23 
testPublicSdk()24   public static void testPublicSdk() {
25     // This call should be successful as the method is accessible through the interface.
26     int value = new InheritAbstract().methodPublicSdkNotInAbstractParent();
27     if (value != 42) {
28       throw new Error("Expected 42, got " + value);
29     }
30   }
31 
testUnsupportedAppUsage()32   public static void testUnsupportedAppUsage() {
33     // This call should throw an exception as the only accessible method is unsupportedappusage.
34     try {
35       new InheritAbstract().methodUnsupportedNotInAbstractParent();
36       throw new Error("Expected NoSuchMethodError");
37     } catch (NoSuchMethodError e) {
38       // Expected.
39     }
40   }
41 
testNative(String library)42   public static void testNative(String library) {
43     System.load(library);
44     int value = testNativeInternal();
45     if (value != 42) {
46       throw new Error("Expected 42, got " + value);
47     }
48 
49     // Test that we consistently return we cannot access a hidden method.
50 
51     // Dedupe hidden api warnings to trigger the optimization described below.
52     dedupeHiddenApiWarnings();
53     assertFalse(testAccessInternal(
54         InheritAbstract.class, "methodUnsupportedNotInAbstractParent", "()I"));
55     // Access the accessible method through reflection. This will do an optimization pretending the
56     // method is public API.
57     try {
58       NotInAbstractInterface.class.getDeclaredMethod("methodUnsupportedNotInAbstractParent").
59           invoke(new InheritAbstract());
60     } catch (Exception e) {
61       throw new Error(e);
62     }
63     assertFalse(testAccessInternal(
64         InheritAbstract.class, "methodUnsupportedNotInAbstractParent", "()I"));
65   }
66 
assertTrue(boolean value)67   public static void assertTrue(boolean value) {
68     if (!value) {
69       throw new Error("Expected true value");
70     }
71   }
72 
assertFalse(boolean value)73   public static void assertFalse(boolean value) {
74     if (value) {
75       throw new Error("Expected false value");
76     }
77   }
78 
testNativeInternal()79   public static native int testNativeInternal();
dedupeHiddenApiWarnings()80   public static native void dedupeHiddenApiWarnings();
testAccessInternal(Class<?> cls, String method, String signature)81   public static native boolean testAccessInternal(Class<?> cls, String method, String signature);
82 }
83