1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2021 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker public class TestCase { 18*795d594fSAndroid Build Coastguard Worker test()19*795d594fSAndroid Build Coastguard Worker public static void test() { 20*795d594fSAndroid Build Coastguard Worker testPublicSdk(); 21*795d594fSAndroid Build Coastguard Worker testUnsupportedAppUsage(); 22*795d594fSAndroid Build Coastguard Worker } 23*795d594fSAndroid Build Coastguard Worker testPublicSdk()24*795d594fSAndroid Build Coastguard Worker public static void testPublicSdk() { 25*795d594fSAndroid Build Coastguard Worker // This call should be successful as the method is accessible through the interface. 26*795d594fSAndroid Build Coastguard Worker int value = new InheritAbstract().methodPublicSdkNotInAbstractParent(); 27*795d594fSAndroid Build Coastguard Worker if (value != 42) { 28*795d594fSAndroid Build Coastguard Worker throw new Error("Expected 42, got " + value); 29*795d594fSAndroid Build Coastguard Worker } 30*795d594fSAndroid Build Coastguard Worker } 31*795d594fSAndroid Build Coastguard Worker testUnsupportedAppUsage()32*795d594fSAndroid Build Coastguard Worker public static void testUnsupportedAppUsage() { 33*795d594fSAndroid Build Coastguard Worker // This call should throw an exception as the only accessible method is unsupportedappusage. 34*795d594fSAndroid Build Coastguard Worker try { 35*795d594fSAndroid Build Coastguard Worker new InheritAbstract().methodUnsupportedNotInAbstractParent(); 36*795d594fSAndroid Build Coastguard Worker throw new Error("Expected NoSuchMethodError"); 37*795d594fSAndroid Build Coastguard Worker } catch (NoSuchMethodError e) { 38*795d594fSAndroid Build Coastguard Worker // Expected. 39*795d594fSAndroid Build Coastguard Worker } 40*795d594fSAndroid Build Coastguard Worker } 41*795d594fSAndroid Build Coastguard Worker testNative(String library)42*795d594fSAndroid Build Coastguard Worker public static void testNative(String library) { 43*795d594fSAndroid Build Coastguard Worker System.load(library); 44*795d594fSAndroid Build Coastguard Worker int value = testNativeInternal(); 45*795d594fSAndroid Build Coastguard Worker if (value != 42) { 46*795d594fSAndroid Build Coastguard Worker throw new Error("Expected 42, got " + value); 47*795d594fSAndroid Build Coastguard Worker } 48*795d594fSAndroid Build Coastguard Worker 49*795d594fSAndroid Build Coastguard Worker // Test that we consistently return we cannot access a hidden method. 50*795d594fSAndroid Build Coastguard Worker 51*795d594fSAndroid Build Coastguard Worker // Dedupe hidden api warnings to trigger the optimization described below. 52*795d594fSAndroid Build Coastguard Worker dedupeHiddenApiWarnings(); 53*795d594fSAndroid Build Coastguard Worker assertFalse(testAccessInternal( 54*795d594fSAndroid Build Coastguard Worker InheritAbstract.class, "methodUnsupportedNotInAbstractParent", "()I")); 55*795d594fSAndroid Build Coastguard Worker // Access the accessible method through reflection. This will do an optimization pretending the 56*795d594fSAndroid Build Coastguard Worker // method is public API. 57*795d594fSAndroid Build Coastguard Worker try { 58*795d594fSAndroid Build Coastguard Worker NotInAbstractInterface.class.getDeclaredMethod("methodUnsupportedNotInAbstractParent"). 59*795d594fSAndroid Build Coastguard Worker invoke(new InheritAbstract()); 60*795d594fSAndroid Build Coastguard Worker } catch (Exception e) { 61*795d594fSAndroid Build Coastguard Worker throw new Error(e); 62*795d594fSAndroid Build Coastguard Worker } 63*795d594fSAndroid Build Coastguard Worker assertFalse(testAccessInternal( 64*795d594fSAndroid Build Coastguard Worker InheritAbstract.class, "methodUnsupportedNotInAbstractParent", "()I")); 65*795d594fSAndroid Build Coastguard Worker } 66*795d594fSAndroid Build Coastguard Worker assertTrue(boolean value)67*795d594fSAndroid Build Coastguard Worker public static void assertTrue(boolean value) { 68*795d594fSAndroid Build Coastguard Worker if (!value) { 69*795d594fSAndroid Build Coastguard Worker throw new Error("Expected true value"); 70*795d594fSAndroid Build Coastguard Worker } 71*795d594fSAndroid Build Coastguard Worker } 72*795d594fSAndroid Build Coastguard Worker assertFalse(boolean value)73*795d594fSAndroid Build Coastguard Worker public static void assertFalse(boolean value) { 74*795d594fSAndroid Build Coastguard Worker if (value) { 75*795d594fSAndroid Build Coastguard Worker throw new Error("Expected false value"); 76*795d594fSAndroid Build Coastguard Worker } 77*795d594fSAndroid Build Coastguard Worker } 78*795d594fSAndroid Build Coastguard Worker testNativeInternal()79*795d594fSAndroid Build Coastguard Worker public static native int testNativeInternal(); dedupeHiddenApiWarnings()80*795d594fSAndroid Build Coastguard Worker public static native void dedupeHiddenApiWarnings(); testAccessInternal(Class<?> cls, String method, String signature)81*795d594fSAndroid Build Coastguard Worker public static native boolean testAccessInternal(Class<?> cls, String method, String signature); 82*795d594fSAndroid Build Coastguard Worker } 83