153196f43b44ff02da07c243798168d7e5614ec34Brett Chabot/* 253196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * Copyright (C) 2012 The Android Open Source Project 353196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * 453196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * Licensed under the Apache License, Version 2.0 (the "License"); 553196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * you may not use this file except in compliance with the License. 653196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * You may obtain a copy of the License at 753196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * 853196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * http://www.apache.org/licenses/LICENSE-2.0 953196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * 1053196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * Unless required by applicable law or agreed to in writing, software 1153196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * distributed under the License is distributed on an "AS IS" BASIS, 1253196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1353196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * See the License for the specific language governing permissions and 1453196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * limitations under the License. 1553196f43b44ff02da07c243798168d7e5614ec34Brett Chabot */ 1653196f43b44ff02da07c243798168d7e5614ec34Brett Chabotpackage com.android.test; 1753196f43b44ff02da07c243798168d7e5614ec34Brett Chabot 1853196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport android.app.Instrumentation; 1953196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport android.content.Context; 2053196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport android.test.AndroidTestCase; 2153196f43b44ff02da07c243798168d7e5614ec34Brett Chabot 2253196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport junit.framework.Test; 2353196f43b44ff02da07c243798168d7e5614ec34Brett Chabot 2453196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport org.junit.Before; 2553196f43b44ff02da07c243798168d7e5614ec34Brett Chabot 2653196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport java.lang.annotation.ElementType; 2753196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport java.lang.annotation.Retention; 2853196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport java.lang.annotation.RetentionPolicy; 2953196f43b44ff02da07c243798168d7e5614ec34Brett Chabotimport java.lang.annotation.Target; 3053196f43b44ff02da07c243798168d7e5614ec34Brett Chabot 3153196f43b44ff02da07c243798168d7e5614ec34Brett Chabot/** 323c941fb7233210b06245f52cd9630b33da15d344Brett Chabot * Use this to inject a {@link Context} representing {@link Instrumentation#getTargetContext()} 3353196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * into your JUnit4-style test. 3453196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * <p/> 3553196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * To use, just add the correct annotation to an {@link Context} field like this: 3653196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * <pre> 3753196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * @InjectContext public Context mMyContext; 3853196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * </pre> 3953196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * The test runner will set the value of this field with the {@link Instrumentation} after 4053196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * object construction but before any {@link Before} methods are called. 4153196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * <p/> 4253196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * Declaring this in a JUnit3 test (ie a class that is a {@link Test}) will have no effect. 4353196f43b44ff02da07c243798168d7e5614ec34Brett Chabot * Use {@link AndroidTestCase} instead for JUnit3 style tests. 4453196f43b44ff02da07c243798168d7e5614ec34Brett Chabot */ 4553196f43b44ff02da07c243798168d7e5614ec34Brett Chabot@Retention(RetentionPolicy.RUNTIME) 4653196f43b44ff02da07c243798168d7e5614ec34Brett Chabot@Target(ElementType.FIELD) 4753196f43b44ff02da07c243798168d7e5614ec34Brett Chabotpublic @interface InjectContext { 4853196f43b44ff02da07c243798168d7e5614ec34Brett Chabot 4953196f43b44ff02da07c243798168d7e5614ec34Brett Chabot} 50