1 /* 2 * Copyright 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 17 package android.view.inspector.cts; 18 19 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertTrue; 24 25 import android.platform.test.annotations.AppModeSdkSandbox; 26 import android.view.View; 27 import android.view.inspector.InspectionCompanion; 28 import android.view.inspector.PropertyMapper; 29 import android.view.inspector.PropertyReader; 30 import android.view.inspector.StaticInspectionCompanionProvider; 31 import android.widget.RelativeLayout; 32 33 import androidx.test.filters.SmallTest; 34 import androidx.test.runner.AndroidJUnit4; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 /** 41 * Tests for {@link StaticInspectionCompanionProvider} 42 */ 43 @SmallTest 44 @RunWith(AndroidJUnit4.class) 45 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 46 public class StaticInspectionCompanionProviderTest { 47 private StaticInspectionCompanionProvider mProvider; 48 49 @Before setup()50 public void setup() { 51 mProvider = new StaticInspectionCompanionProvider(); 52 } 53 54 @Test testViewWorks()55 public void testViewWorks() { 56 InspectionCompanion<View> companion = mProvider.provide(View.class); 57 assertNotNull(companion); 58 assertEquals("android.view.View$InspectionCompanion", companion.getClass().getName()); 59 } 60 61 @Test testRelativeLayoutWorks()62 public void testRelativeLayoutWorks() { 63 InspectionCompanion<RelativeLayout.LayoutParams> companion = 64 mProvider.provide(RelativeLayout.LayoutParams.class); 65 assertNotNull(companion); 66 assertEquals( 67 "android.widget.RelativeLayout$LayoutParams$InspectionCompanion", 68 companion.getClass().getName()); 69 } 70 71 static class NestedClassTest { 72 public static final class InspectionCompanion 73 implements android.view.inspector.InspectionCompanion<NestedClassTest> { 74 @Override mapProperties(PropertyMapper propertyMapper)75 public void mapProperties(PropertyMapper propertyMapper) { 76 77 } 78 79 @Override readProperties(NestedClassTest inspectable, PropertyReader propertyReader)80 public void readProperties(NestedClassTest inspectable, PropertyReader propertyReader) { 81 82 } 83 } 84 } 85 86 @Test testNestedClassLoading()87 public void testNestedClassLoading() { 88 InspectionCompanion<NestedClassTest> companion = mProvider.provide(NestedClassTest.class); 89 assertNotNull(companion); 90 assertTrue(companion instanceof NestedClassTest.InspectionCompanion); 91 } 92 93 static class NonImplementingNestedClassTest { 94 public static final class InspectionCompanion { 95 } 96 } 97 98 @Test testNonImplementingNestedClass()99 public void testNonImplementingNestedClass() { 100 InspectionCompanion<NonImplementingNestedClassTest> companion = 101 mProvider.provide(NonImplementingNestedClassTest.class); 102 assertNull(companion); 103 } 104 105 class NoCompanionTest { 106 } 107 108 @Test testNoCompanion()109 public void testNoCompanion() { 110 InspectionCompanion<NoCompanionTest> companion = mProvider.provide(NoCompanionTest.class); 111 assertNull(companion); 112 } 113 } 114