Home | History | Annotate | Download | only in component
      1 /*
      2  * Copyright (C) 2018 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.server.am.component;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 
     22 import android.content.ComponentName;
     23 
     24 /**
     25  * Base class for Components constants holding class.
     26  *
     27  * Every testing APK should have Components class in Java package that equals to package of APK.
     28  * Those Components class should extends {@link ComponentsBase}.
     29  */
     30 public class ComponentsBase {
     31 
     32     /**
     33      * Build {@link ComponentName} constant which belongs to {@code componentsClass}'s package.
     34      * @param componentsClass Components class object which has the same package of APK.
     35      * @param className simple class name (has no '.') or fully qualified class name.
     36      * @return {@link ComponentName} object.
     37      */
     38     protected static ComponentName component(
     39             Class<? extends ComponentsBase> componentsClass, String className) {
     40         assertFalse("className should not start with '.'", className.startsWith("."));
     41         final String packageName = getPackageName(componentsClass);
     42         final boolean isSimpleClassName = className.indexOf('.') < 0;
     43         final String fullClassName = isSimpleClassName ? packageName + "." + className : className;
     44         return new ComponentName(packageName, fullClassName);
     45     }
     46 
     47     /**
     48      * Get package name of {@code componentsClass}.
     49      * @param componentsClass Components class object which has the same package of APK.
     50      * @return package name of APK.
     51      */
     52     protected static String getPackageName(Class<? extends ComponentsBase> componentsClass) {
     53         assertEquals("Components", componentsClass.getSimpleName());
     54         return componentsClass.getPackage().getName();
     55     }
     56 }
     57